Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions example/EP_Context_Cache/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FLEXML_XTC ?= /proj/testcases/xtc/HEAD/tc/open/flexml
MODEL_SOURCE_PATH := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))

ifeq ($(shell test -d $(MODEL_SOURCE_PATH)/../../../common && echo -n yes),yes)
PERFORCE_HOME = $(MODEL_SOURCE_PATH)/../../..
else
PERFORCE_HOME = $(FLEXML_XTC)
endif

ifeq ($(shell test -d $(MODEL_SOURCE_PATH)/../../common && echo -n yes),yes)
PERFORCE_E2E_HOME = $(MODEL_SOURCE_PATH)/../../
else
PERFORCE_E2E_HOME = $(FLEXML_XTC)/e2e-hw
endif

MODEL = ResNet18
INPUT_MODEL = ResNet18.onnx
TENSOR_SHAPE = 1x3x224x224
VAIMLEP_RUN_SCRIPT = run.py
DEVICE = stx
FLEXML_USE_STD_SYSTEM_CALL = 1

include $(PERFORCE_E2E_HOME)/common/Makefile-common.mk
74 changes: 74 additions & 0 deletions example/EP_Context_Cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<table class="sphinxhide" width="100%">
<tr width="100%">
<td align="center"><img src="https://raw.githubusercontent.com/Xilinx/Image-Collateral/main/xilinx-logo.png" width="30%"/><h1> Ryzen™ AI EP Context Cache Example </h1>
</td>
</tr>
</table>

# Getting started with Ryzen AI EP Context Cache

This is an example showing how to compile and run the ResNet18 model from https://github.com/onnx/models/blob/main/validated/vision/classification/resnet/model/resnet18-v2-7.onnx
on AMD's Ryzen AI NPU by new convenient EP Context Cache with ease of usage (start from Ryzen AI 1.5).



# Activate Ryzen AI conda environment


```bash
#Install Ryzen AI msi with relative NPU driver
conda activate ryzen-ai-1.x
```

# Generate EP Context Cache directly, no need to quantize firstly

```bash
# https://github.com/onnx/models/blob/main/validated/vision/classification/resnet/model/resnet18-v2-7.onnx

python compile.py resnet18-v2-7.onnx

WARNING: Logging before InitGoogleLogging() is written to STDERR
I20250729 08:31:30.094357 12396 vitisai_compile_model.cpp:1157] Vitis AI EP Load ONNX Model Success
I20250729 08:31:30.094357 12396 vitisai_compile_model.cpp:1158] Graph Input Node Name/Shape (1)
I20250729 08:31:30.094357 12396 vitisai_compile_model.cpp:1162] data : [-1x3x224x224]
I20250729 08:31:30.095352 12396 vitisai_compile_model.cpp:1168] Graph Output Node Name/Shape (1)
I20250729 08:31:30.095352 12396 vitisai_compile_model.cpp:1172] resnetv22_dense0_fwd : [-1x1000]
Adding RYZEN_AI_INSTALLATION_PATH=C:\Program Files\RyzenAI\... to installation search path

subpartition path = ....\resnet18-ep-context\resnet18-v2-7\vaiml_par_0\0
[Vitis AI EP] No. of Operators : VAIML 60
[Vitis AI EP] No. of Subgraphs : VAIML 1

# EP context cache model is saved as resnet18-v2-7_ctx.onnx

```

# Run Inference with EP Context Cache
```bash

python run.py resnet18-v2-7_ctx.onnx
WARNING: Logging before InitGoogleLogging() is written to STDERR
I20250729 08:39:23.587743 2180 vitisai_compile_model.cpp:1157] Vitis AI EP Load ONNX Model Success
I20250729 08:39:23.587743 2180 vitisai_compile_model.cpp:1158] Graph Input Node Name/Shape (1)
I20250729 08:39:23.587743 2180 vitisai_compile_model.cpp:1162] data : [-1x3x224x224]
I20250729 08:39:23.587743 2180 vitisai_compile_model.cpp:1168] Graph Output Node Name/Shape (1)
I20250729 08:39:23.587743 2180 vitisai_compile_model.cpp:1172] resnetv22_dense0_fwd : [-1x1000]
[Vitis AI EP] No. of Subgraphs supported by Vitis AI EP: VAIML 1
Top 3 Probabilities
[208 207 176]
------------------------------------|------------
Classification |Percentage
------------------------------------|------------
Labrador retriever | 67.43
------------------------------------|------------
golden retriever | 9.13
------------------------------------|------------
Saluki, gazelle hound | 8.05
------------------------------------|------------
INFO: Test passed

```




Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions example/EP_Context_Cache/compile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import sys
import json
import onnxruntime as ort

onnx_model_path = sys.argv[1]
model_name = os.path.splitext(os.path.basename(onnx_model_path))[0]
ctx_cache = model_name + "_ctx.onnx"

# Delete prexisting EP context cache model
if os.path.exists(ctx_cache):
print(f"INFO: EP context model {ctx_cache} already exists. Deleting it.")
os.remove(ctx_cache)

session_options = ort.SessionOptions()
session_options.add_session_config_entry('ep.context_enable', '1')
session_options.add_session_config_entry('ep.context_file_path', ctx_cache)
session_options.add_session_config_entry('ep.context_embed_mode', '1')
onnx_session = ort.InferenceSession(
onnx_model_path,
sess_options=session_options,
providers=["VitisAIExecutionProvider"],
provider_options=[{"cache_dir": os.getcwd(),
"cache_key": model_name,}]
)
79 changes: 79 additions & 0 deletions example/EP_Context_Cache/image_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from typing import Any, List, Tuple

import numpy as np
import numpy.typing as npt
import torch
from PIL import Image # type: ignore [import-untyped]
from torchvision import transforms


def load_and_preprocess_image(image_file: str) -> torch.Tensor:
"""
Load and preprocess image_file for inference test
It works for all imagenet images
"""

img = Image.open(image_file).convert("RGB")
# preprocessing pipeline
preprocess = transforms.Compose(
[
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
]
)
img_preprocessed = preprocess(img)
return torch.unsqueeze(img_preprocessed, 0)


def softmax(vector: npt.NDArray[np.float32]) -> npt.NDArray[np.float32]:
"""
Calculate softmax of a vector
"""
e = np.exp(vector)
res: npt.NDArray[np.float32] = e / e.sum()
return res


def top_n_probabilities(
res: npt.NDArray[np.float32],
labels: List[str],
top_n: int = 3,
run_softmax: bool = False,
) -> List[Tuple[Any, Any]]:
"""
Compute probabilities of top 3 classifications from res
Inputs:
data_in: output as 1-D numpy array from full connected layer or softmax
run_softmax: whether or not to run softmax on data_in
"""
indices = np.flip(np.argsort(res))
if run_softmax:
percentage = softmax(res) * 100
else:
percentage = res * 100

print(indices[:3])
top_n_result = [(labels[idx], percentage[idx].item()) for idx in indices[:3]]

return top_n_result


def top3_probabilities(
data_in: npt.NDArray[np.float32], labels: List[str], run_softmax: bool = False
) -> List[Tuple[Any, Any]]:
"""
Helper function to get top 3 probabilities for backward compatibility
"""
top3 = top_n_probabilities(data_in, labels, top_n=3, run_softmax=run_softmax)

return top3


def load_labels(label_file: str) -> List[str]:
classes_fh = open(label_file)
labels = [line.strip() for line in classes_fh]

classes_fh.close()
return labels
Loading