mmdeploy/tests/test_apis/test_onnx2ascend.py
Li Zhang 792c27b054
[Feature] Ascend backend (#747)
* add acl backend

* support dynamic batch size and dynamic image size

* add preliminary ascend backend

* support dtypes other than float

* support dynamic_dims in SDK

* fix dynamic batch size

* better error handling

* remove debug info

* [WIP] dynamic shape support

* fix static shape

* fix dynamic batch size

* add retinanet support

* fix dynamic image size

* fix dynamic image size

* fix dynamic dims

* fix dynamic dims

* simplify config files

* fix yolox support

* fix negative index

* support faster rcnn

* add seg config

* update benchmark

* fix onnx2ascend dynamic shape

* update docstring and benchmark

* add unit test, update documents

* fix wrapper

* fix ut

* fix for vit

* error handling

* context handling & multi-device support

* build with stub libraries

* add ci

* fix lint

* fix lint

* update doc ref

* fix typo

* down with `target_link_directories`

* setup python

* makedir

* fix ci

* fix ci

* remove verbose logs

* fix UBs

* export Error

* fix lint

* update checkenv

Co-authored-by: grimoire <yaoqian@sensetime.com>
2022-09-05 12:08:36 +08:00

72 lines
1.7 KiB
Python

# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
import tempfile
import mmcv
import pytest
import torch
import torch.nn as nn
from mmdeploy.utils import Backend
from mmdeploy.utils.test import backend_checker
onnx_file = tempfile.NamedTemporaryFile(suffix='.onnx').name
test_img = torch.rand([1, 3, 8, 8])
@pytest.mark.skip(reason='This a not test class but a utility class.')
class TestModel(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x * 0.5
test_model = TestModel().eval()
def generate_onnx_file(model):
with torch.no_grad():
dynamic_axes = {
'input': {
0: 'batch',
2: 'width',
3: 'height'
},
'output': {
0: 'batch'
}
}
torch.onnx.export(
model,
test_img,
onnx_file,
output_names=['output'],
input_names=['input'],
keep_initializers_as_inputs=True,
do_constant_folding=True,
verbose=False,
opset_version=11,
dynamic_axes=dynamic_axes)
assert osp.exists(onnx_file)
@backend_checker(Backend.ASCEND)
def test_onnx2ascend():
from mmdeploy.apis.ascend import from_onnx
model = test_model
generate_onnx_file(model)
work_dir, _ = osp.split(onnx_file)
file_name = osp.splitext(onnx_file)[0]
om_path = osp.join(work_dir, file_name + '.om')
model_inputs = mmcv.Config(
dict(
dynamic_batch_size=[1, 2, 4],
input_shapes=dict(input=[-1, 3, 224, 224])))
from_onnx(onnx_file, work_dir, model_inputs)
assert osp.exists(work_dir)
assert osp.exists(om_path)