-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest.py
More file actions
74 lines (64 loc) · 2.32 KB
/
Copy pathtest.py
File metadata and controls
74 lines (64 loc) · 2.32 KB
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
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
#
# Copyright(C) 2025 Advanced Micro Devices, Inc. All rights reserved.
# SPDX-License-Identifier: MIT
from transformers import AutoModelForVision2Seq, AutoProcessor
from PIL import Image
import torch
import time
device = "cuda"
# Load Processor & VLA
print("\nLoading OpenVLA model...")
processor = AutoProcessor.from_pretrained("openvla/openvla-7b", trust_remote_code=True)
vla = AutoModelForVision2Seq.from_pretrained(
"openvla/openvla-7b",
attn_implementation="sdpa", # options are eager, sdpa, flash_attention_2
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
trust_remote_code=True
).to(device)
print("Model loaded successfully")
# Grab image input & format prompt
image_path = "/ryzers/data/toucan.jpg"
image = Image.open(image_path).convert("RGB")
prompt = "In: What action should the robot take to turn left?"
inputs = processor(prompt, image).to(device, dtype=torch.bfloat16)
# Warmup
print("Warming up...")
for _ in range(3):
with torch.no_grad():
_ = vla.predict_action(**inputs, unnorm_key="bridge_orig", do_sample=False)
torch.cuda.synchronize()
# Benchmark
print("Running benchmark...")
torch.cuda.reset_peak_memory_stats()
num_iterations = 100
latencies = []
for _ in range(num_iterations):
torch.cuda.synchronize()
start = time.perf_counter()
with torch.no_grad():
action = vla.predict_action(**inputs, unnorm_key="bridge_orig", do_sample=False)
torch.cuda.synchronize()
elapsed = time.perf_counter() - start
latencies.append(elapsed * 1000) # Convert to ms
avg_latency_ms = sum(latencies) / len(latencies)
std_latency = (sum((x - avg_latency_ms)**2 for x in latencies) / len(latencies)) ** 0.5
min_latency = min(latencies)
max_latency = max(latencies)
avg_latency_s = avg_latency_ms / 1000
# Get action dimension from output
action_dim = len(action)
avg_hz = 1 / avg_latency_s
print(f"\n{'='*60}")
print("OpenVLA Results")
print(f"{'='*60}")
print(f"Action dimension: {action_dim}")
print(f"Iterations: {num_iterations}")
print(f"Avg latency: {avg_latency_ms:.2f} ms ({avg_latency_s:.6f} s)")
print(f"Std deviation: {std_latency:.2f} ms")
print(f"Min latency: {min_latency:.2f} ms")
print(f"Max latency: {max_latency:.2f} ms")
print(f"Avg Hz: {avg_hz:.2f} Hz")
print(f"Max GPU memory: {torch.cuda.max_memory_allocated() / 1024**2:.2f} MB")
print(f"{'='*60}")