forked from dusty-nv/jetson-containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
66 lines (51 loc) · 2.11 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import os
import time
import logging
#logging.basicConfig(level=logging.DEBUG)
os.environ["XLA_PYTHON_CLIENT_PREALLOCATE"] = "false"
import jax
import jax.numpy as jnp
import numpy as np
jax.print_environment_info()
# https://jax.readthedocs.io/en/latest/persistent_compilation_cache.html
jax.config.update("jax_compilation_cache_dir", "/tmp/jax_cache")
jax.config.update("jax_persistent_cache_min_entry_size_bytes", -1)
jax.config.update("jax_persistent_cache_min_compile_time_secs", 0)
#jax.config.update("jax_enable_x64", False)
# load the model
from crossformer.model.crossformer_model import CrossFormerModel
model = CrossFormerModel.load_pretrained("hf://rail-berkeley/crossformer")
print(model.get_pretty_spec())
# create a random image
img = np.random.randint(0, 255, size=(224, 224, 3))
# add batch and observation history dimension (CrossFormer accepts a history of up to 5 time-steps)
img = img[None, None]
# our bimanual training data has an overhead view and two wrist views
observation = {
"image_high": img,
"image_left_wrist": img,
"image_right_wrist": img,
"timestep_pad_mask": np.array([[True]]),
}
# create a task dictionary for a language task
task = model.create_tasks(texts=["uncap the pen"])
# benchmark performance
print(f"Running crossformer inference")
for n in range(20):
time_begin = time.perf_counter()
action = model.sample_actions(observation, task, head_name="bimanual", rng=jax.random.PRNGKey(0))
time_elapsed = time.perf_counter() - time_begin
#print(action) # [batch, action_chunk, action_dim]
print(f"crossformer frame={n} latency={time_elapsed*1000:.2f} ms action_dims={action.shape}")
'''
import jax.experimental.jax2tf as jax2tf
import tensorflow as tf
import tf2onnx
print("converting JAX -> TF")
tf_fn = tf.function(jax2tf.convert(model.sample_actions, enable_xla=False))
tf_args = [tf.TensorSpec(jnp.shape(x), jnp.result_type(x)) for x in [img, task["pad_mask_dict"]["language_instruction"]]] # pyright: ignore
print("converting TF -> ONNX")
onnx_fn = tf2onnx.convert.from_function(tf_fn, input_signature=tf_args)
print("onnx", onnx_fn)
'''