|
from time import perf_counter |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
from optimum.amd.ryzenai.models import YoloV8ImageProcessor |
|
|
|
|
|
def measure_latency(model, image): |
|
processor = YoloV8ImageProcessor() |
|
pixel_values = processor(image, return_tensors="np").pixel_values |
|
|
|
latencies = [] |
|
|
|
|
|
for _ in range(10): |
|
_ = model(pixel_values) |
|
|
|
for _ in range(10): |
|
start_time = perf_counter() |
|
_ = model(pixel_values) |
|
latency = perf_counter() - start_time |
|
latencies.append(latency) |
|
|
|
time_avg_ms = 1000 * np.mean(latencies) |
|
time_std_ms = 1000 * np.std(latencies) |
|
return time_avg_ms, time_std_ms |
|
|
|
|
|
def plot_latency(cpu_latency, ryzen_latency): |
|
""" |
|
Creates a bar plot comparing CPU and Ryzen latencies. |
|
|
|
Parameters: |
|
cpu_latency (float): Average latency for the CPU. |
|
ryzen_latency (float): Average latency for the Ryzen. |
|
""" |
|
|
|
|
|
labels = ['RyzenAI', 'CPU'] |
|
|
|
|
|
latency_values = [ryzen_latency, cpu_latency] |
|
|
|
|
|
colors = ['blue', 'green'] |
|
|
|
|
|
x = np.arange(len(labels)) |
|
width = 0.35 |
|
|
|
fig, ax = plt.subplots() |
|
bars = ax.bar(x, latency_values, width, label='Latency', color=colors) |
|
|
|
|
|
ax.set_xlabel('Processor') |
|
ax.set_ylabel('Average Latency (ms)') |
|
ax.set_title('Average Latency by Processor') |
|
ax.set_xticks(x) |
|
ax.set_xticklabels(labels) |
|
ax.legend() |
|
|
|
|
|
for bar in bars: |
|
height = bar.get_height() |
|
ax.annotate(f'{height:.2f}', |
|
xy=(bar.get_x() + bar.get_width() / 2, height), |
|
xytext=(0, 3), |
|
textcoords="offset points", |
|
ha='center', va='bottom') |
|
|
|
fig.tight_layout() |
|
|
|
plt.show() |