File size: 2,121 Bytes
4355471
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = []

    # warm up
    for _ in range(10):
        _ = model(pixel_values)
    # Timed run
    for _ in range(10):
        start_time = perf_counter()
        _ = model(pixel_values)
        latency = perf_counter() - start_time
        latencies.append(latency)
    # Compute run statistics
    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 for the bars
    labels = ['RyzenAI', 'CPU']
    
    # Latency values
    latency_values = [ryzen_latency, cpu_latency]
    
    # Colors for the bars
    colors = ['blue', 'green']
    
    # Create a bar plot
    x = np.arange(len(labels))  # the label locations
    width = 0.35  # the width of the bars
    
    fig, ax = plt.subplots()
    bars = ax.bar(x, latency_values, width, label='Latency', color=colors)
    
    # Add some text for labels, title and custom x-axis tick labels, etc.
    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()
    
    # Attach a text label above each bar in *bars*, displaying its height.
    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),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')
    
    fig.tight_layout()
    
    plt.show()