Views
No views yet
1# Example code
2import onnxruntime as ort
3import numpy as np
4from transformers import AutoTokenizer
5import os
6
7# Load the tokenizer
8tokenizer = AutoTokenizer.from_pretrained('google-bert/bert-base-multilingual-uncased')
9
10# Prepare inputs
11text = 'Replace this text with your input.'
12inputs = tokenizer(text, return_tensors='np')
13
14# Specify the model paths
15# Test both the ONNX model and the ORT model
16model_paths = [
17 'onnx_models/model_opt.onnx', # ONNX model
18 'ort_models/model.ort' # ORT format model
19]
20
21# Run inference with each model
22for model_path in model_paths:
23 print(f'\n===== Using model: {model_path} =====')
24 # Get the model extension
25 model_extension = os.path.splitext(model_path)[1]
26
27 # Load the model
28 if model_extension == '.ort':
29 # Load the ORT format model
30 session = ort.InferenceSession(model_path, providers=['CPUExecutionProvider'])
31 else:
32 # Load the ONNX model
33 session = ort.InferenceSession(model_path)
34
35 # Run inference
36 outputs = session.run(None, dict(inputs))
37
38 # Display the output shapes
39 for idx, output in enumerate(outputs):
40 print(f'Output {idx} shape: {output.shape}')
41
42 # Display the results (add further processing if needed)
43 print(outputs)onnx_models/model.onnx: Original ONNX model converted from google-bert/bert-base-multilingual-uncasedonnx_models/model_opt.onnx: Optimized ONNX modelonnx_models/model_fp16.onnx: FP16 quantized modelonnx_models/model_int8.onnx: INT8 quantized modelonnx_models/model_uint8.onnx: UINT8 quantized modelort_models/model.ort: ORT model using the optimized ONNX modelort_models/model_fp16.ort: ORT model using the FP16 quantized modelort_models/model_int8.ort: ORT model using the INT8 quantized modelort_models/model_uint8.ort: ORT model using the UINT8 quantized model