|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
import urllib |
|
import traceback |
|
import time |
|
import sys |
|
import numpy as np |
|
import cv2 |
|
from rknn.api import RKNN |
|
from math import exp |
|
from sys import exit |
|
rknn = RKNN(verbose=True) |
|
|
|
|
|
os.chdir(os.path.dirname(os.path.abspath(__file__))) |
|
|
|
|
|
|
|
|
|
ONNX_MODEL="model.onnx" |
|
RKNN_MODEL=ONNX_MODEL.replace(".onnx",".rknn") |
|
DATASET="dataset.txt" |
|
QUANTIZE=False |
|
detailed_performance_log = True |
|
|
|
|
|
|
|
seq_len = 256 |
|
|
|
|
|
print('--> Config model') |
|
rknn.config(quantized_algorithm='normal', quantized_method='channel', target_platform='rk3588', optimization_level=3) |
|
print('done') |
|
|
|
|
|
print('--> Loading model') |
|
ret = rknn.load_onnx(model=ONNX_MODEL, |
|
inputs=["input_ids", "attention_mask", "token_type_ids"], |
|
input_size_list=[[1, seq_len], [1, seq_len], [1, seq_len]], |
|
outputs=["/encoder/layer.21/output/LayerNorm/Add_1_output_0"]) |
|
if ret != 0: |
|
print('Load model failed!') |
|
exit(ret) |
|
print('done') |
|
|
|
|
|
print('--> Building model') |
|
ret = rknn.build(do_quantization=QUANTIZE, dataset=DATASET, rknn_batch_size=None) |
|
if ret != 0: |
|
print('Build model failed!') |
|
exit(ret) |
|
print('done') |
|
|
|
|
|
print('--> Export RKNN model') |
|
ret = rknn.export_rknn(RKNN_MODEL) |
|
if ret != 0: |
|
print('Export RKNN model failed!') |
|
exit(ret) |
|
print('done') |