Update utils.py
Browse files
utils.py
CHANGED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from time import perf_counter
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from optimum.amd.ryzenai.models import YoloV8ImageProcessor
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def measure_latency(model, image):
|
| 8 |
+
processor = YoloV8ImageProcessor()
|
| 9 |
+
pixel_values = processor(image, return_tensors="np").pixel_values
|
| 10 |
+
|
| 11 |
+
latencies = []
|
| 12 |
+
|
| 13 |
+
# warm up
|
| 14 |
+
for _ in range(10):
|
| 15 |
+
_ = model(pixel_values)
|
| 16 |
+
# Timed run
|
| 17 |
+
for _ in range(10):
|
| 18 |
+
start_time = perf_counter()
|
| 19 |
+
_ = model(pixel_values)
|
| 20 |
+
latency = perf_counter() - start_time
|
| 21 |
+
latencies.append(latency)
|
| 22 |
+
# Compute run statistics
|
| 23 |
+
time_avg_ms = 1000 * np.mean(latencies)
|
| 24 |
+
time_std_ms = 1000 * np.std(latencies)
|
| 25 |
+
return time_avg_ms, time_std_ms
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def plot_latency(cpu_latency, ryzen_latency):
|
| 29 |
+
"""
|
| 30 |
+
Creates a bar plot comparing CPU and Ryzen latencies.
|
| 31 |
+
|
| 32 |
+
Parameters:
|
| 33 |
+
cpu_latency (float): Average latency for the CPU.
|
| 34 |
+
ryzen_latency (float): Average latency for the Ryzen.
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
# Labels for the bars
|
| 38 |
+
labels = ['RyzenAI', 'CPU']
|
| 39 |
+
|
| 40 |
+
# Latency values
|
| 41 |
+
latency_values = [ryzen_latency, cpu_latency]
|
| 42 |
+
|
| 43 |
+
# Colors for the bars
|
| 44 |
+
colors = ['blue', 'green']
|
| 45 |
+
|
| 46 |
+
# Create a bar plot
|
| 47 |
+
x = np.arange(len(labels)) # the label locations
|
| 48 |
+
width = 0.35 # the width of the bars
|
| 49 |
+
|
| 50 |
+
fig, ax = plt.subplots()
|
| 51 |
+
bars = ax.bar(x, latency_values, width, label='Latency', color=colors)
|
| 52 |
+
|
| 53 |
+
# Add some text for labels, title and custom x-axis tick labels, etc.
|
| 54 |
+
ax.set_xlabel('Processor')
|
| 55 |
+
ax.set_ylabel('Average Latency (ms)')
|
| 56 |
+
ax.set_title('Average Latency by Processor')
|
| 57 |
+
ax.set_xticks(x)
|
| 58 |
+
ax.set_xticklabels(labels)
|
| 59 |
+
ax.legend()
|
| 60 |
+
|
| 61 |
+
# Attach a text label above each bar in *bars*, displaying its height.
|
| 62 |
+
for bar in bars:
|
| 63 |
+
height = bar.get_height()
|
| 64 |
+
ax.annotate(f'{height:.2f}',
|
| 65 |
+
xy=(bar.get_x() + bar.get_width() / 2, height),
|
| 66 |
+
xytext=(0, 3), # 3 points vertical offset
|
| 67 |
+
textcoords="offset points",
|
| 68 |
+
ha='center', va='bottom')
|
| 69 |
+
|
| 70 |
+
fig.tight_layout()
|
| 71 |
+
|
| 72 |
+
plt.show()
|