diff --git a/mindspore-lite/test/st/python/python_api/test_model.py b/mindspore-lite/test/st/python/python_api/test_model.py new file mode 100644 index 0000000000000000000000000000000000000000..2f0810b9f50ce4d0572bcd5ccb41ce098589876a --- /dev/null +++ b/mindspore-lite/test/st/python/python_api/test_model.py @@ -0,0 +1,116 @@ +# Copyright 2025 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +""" +Test for MindSpore Lite Model +""" + +import pytest +import mindspore_lite as mslite +import numpy as np + +# ----------- config file ----------- +# [acl_build_options] +# input_format="ND" +# input_shape="sample:2,4,-1,-1;timestep:1;encoder_hidden_states:2,77,768" +# ge.dynamicDims="64,64;96,96" + +MODEL_FILE = "./sd1.5_unet.onnx_graph.mindir" +DEVICE_ID = 0 + + +# For Model Resize +def test_python_api_resize_001(): + context = mslite.Context() + context.target = ["ascend"] + context.ascend.device_id = DEVICE_ID + model = mslite.Model() + model.build_from_file(model_path=MODEL_FILE, model_type=mslite.ModelType.MINDIR, context=context) + model.resize(model.get_inputs(), [[2, 4, 64, 64], [1], [2, 77, 768]]) + + +def test_python_api_fi_resize_001(): + with pytest.raises(RuntimeError) as raise_info: + context = mslite.Context() + context.target = ["ascend"] + context.ascend.device_id = DEVICE_ID + model = mslite.Model() + model.build_from_file(model_path=MODEL_FILE, model_type=mslite.ModelType.MINDIR, context=context) + model.resize(model.get_inputs(), [[10, 4, 64, 64], [1], [2, 77, 768]]) + assert "resize failed! Error is Common error code." in str(raise_info.value) + + +def test_python_api_fi_resize_002(): + with pytest.raises(RuntimeError) as raise_info: + context = mslite.Context() + context.target = ["ascend"] + context.ascend.device_id = DEVICE_ID + model = mslite.Model() + model.build_from_file(model_path=MODEL_FILE, model_type=mslite.ModelType.MINDIR, context=context) + model.resize(model.get_inputs(), [[2, 4, -1, -1], [1], [2, 77, 768]]) + assert "resize failed! Error is Invalid input param by user." in str(raise_info.value) + + +def test_python_api_func_resize_random_shape_001(): + context = mslite.Context() + context.target = ["ascend"] + context.ascend.device_id = DEVICE_ID + model = mslite.Model() + model.build_from_file(model_path=MODEL_FILE, model_type=mslite.ModelType.MINDIR, context=context) + + # data for 64 + input_1_data_1 = np.random.rand(2, 4, 64, 64).astype(np.float32) + input_2_data_1 = np.random.rand(1).astype(np.float32) + input_3_data_1 = np.random.rand(2, 77, 768).astype(np.float32) + # data for 96 + input_1_data_2 = np.random.rand(2, 4, 96, 96).astype(np.float32) + input_2_data_2 = np.random.rand(1).astype(np.float32) + input_3_data_2 = np.random.rand(2, 77, 768).astype(np.float32) + + # predict for shape of 64 + model.resize(model.get_inputs(), [[2, 4, 64, 64], [1], [2, 77, 768]]) + model.predict([input_1_data_1, input_2_data_1, input_3_data_1]) + # predict for shape of 96 + model.resize(model.get_inputs(), [[2, 4, 96, 96], [1], [2, 77, 768]]) + model.predict([input_1_data_2, input_2_data_2, input_3_data_2]) + # predict for shape of 64 + model.resize(model.get_inputs(), [[2, 4, 64, 64], [1], [2, 77, 768]]) + model.predict([input_1_data_1, input_2_data_1, input_3_data_1]) + + +def test_python_api_func_resize_random_shape_002(): + context = mslite.Context() + context.target = ["ascend"] + context.ascend.device_id = DEVICE_ID + model = mslite.Model() + model.build_from_file(model_path=MODEL_FILE, model_type=mslite.ModelType.MINDIR, context=context) + + # data for 64 + input_1_data_1 = np.random.rand(2, 4, 64, 64).astype(np.float32) + input_2_data_1 = np.random.rand(1).astype(np.float32) + input_3_data_1 = np.random.rand(2, 77, 768).astype(np.float32) + # data for 96 + input_1_data_2 = np.random.rand(2, 4, 96, 96).astype(np.float32) + input_2_data_2 = np.random.rand(1).astype(np.float32) + input_3_data_2 = np.random.rand(2, 77, 768).astype(np.float32) + + # predict for shape of 96 + model.resize(model.get_inputs(), [[2, 4, 96, 96], [1], [2, 77, 768]]) + model.predict([input_1_data_2, input_2_data_2, input_3_data_2]) + # predict for shape of 64 + model.resize(model.get_inputs(), [[2, 4, 64, 64], [1], [2, 77, 768]]) + model.predict([input_1_data_1, input_2_data_1, input_3_data_1]) + # predict for shape of 96 + model.resize(model.get_inputs(), [[2, 4, 96, 96], [1], [2, 77, 768]]) + model.predict([input_1_data_2, input_2_data_2, input_3_data_2]) diff --git a/mindspore-lite/test/st/python/python_api/test_model_parallel_runner.py b/mindspore-lite/test/st/python/python_api/test_model_parallel_runner.py new file mode 100644 index 0000000000000000000000000000000000000000..5ba5bee591a9bd7136132ae8aa52aa350a7c7859 --- /dev/null +++ b/mindspore-lite/test/st/python/python_api/test_model_parallel_runner.py @@ -0,0 +1,75 @@ +# Copyright 2025 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +""" +Test for MindSpore Lite Model +""" + +import pytest +import mindspore_lite as mslite +import numpy as np + +# ----------- config file ----------- +# [acl_build_options] +# input_format="ND" +# input_shape="sample:2,4,-1,-1;timestep:1;encoder_hidden_states:2,77,768" +# ge.dynamicDims="64,64;96,96" + +MODEL_FILE = "./sd1.5_unet.onnx_graph.mindir" +DEVICE_ID = 0 + + +def test_python_api_func_parallel_001(): + context = mslite.Context() + context.target = ["ascend"] + context.parallel.workers_num = 2 + runner = mslite.ModelParallelRunner() + runner.build_from_file(model_path=MODEL_FILE, context=context) + + input_1 = mslite.Tensor(np.random.rand(2, 4, 64, 64).astype(np.float32)) + input_2 = mslite.Tensor(np.random.rand(1).astype(np.float32)) + input_3 = mslite.Tensor(np.random.rand(2, 77, 768).astype(np.float32)) + runner.predict([input_1, input_2, input_3]) + + +def test_python_api_fi_parallel_003(): + with pytest.raises(RuntimeError) as raise_info: + context = mslite.Context() + context.target = ["ascend"] + context.parallel.workers_num = 2 + runner = mslite.ModelParallelRunner() + runner.build_from_file(model_path="xxx", context=context) + assert "ModelParallelRunner's build from file failed, model_path does not exist!" in str(raise_info) + + +def test_python_api_fi_parallel_005(): + with pytest.raises(RuntimeError) as raise_info: + context = mslite.Context() + context.target = ["ascend"] + context.parallel.workers_num = 2 + runner = mslite.ModelParallelRunner() + runner.build_from_file(model_path=MODEL_FILE, context=context) + runner.predict([]) + assert "ModelParallelRunner's build from file failed! Error is Common error code." in str(raise_info.value) + + +def test_python_api_fi_parallel_006(): + with pytest.raises(RuntimeError) as raise_info: + context = mslite.Context() + context.target = ["ascend"] + context.parallel.workers_num = 2 + runner = mslite.ModelParallelRunner() + inputs = runner.get_inputs() + runner.predict(inputs) + assert "predict failed!" in str(raise_info.value) diff --git a/mindspore-lite/test/st/scripts/ascend/run_cloud_arm_a2.sh b/mindspore-lite/test/st/scripts/ascend/run_cloud_arm_a2.sh index 512f72584d846e3bd6e1479402bafbdcf4b90293..84cd8382928381b9c448720d39cba2133d770a36 100644 --- a/mindspore-lite/test/st/scripts/ascend/run_cloud_arm_a2.sh +++ b/mindspore-lite/test/st/scripts/ascend/run_cloud_arm_a2.sh @@ -384,13 +384,13 @@ if [[ ${run_python_status} != 0 ]]; then fi # run python api test -cd ${basepath}/python/python_api/ -pytest test_tensor.py -s -ret=$? -if [ ${ret} -ne 0 ]; then - echo "run test_tensor.py failed." - exit ${ret} - fi +echo "---------- Run MindSpore Lite API ----------" +cd ${basepath}/python/python_api/ || exit 1 +cp -r ${ms_models_path}/sd1.5_unet.onnx* . || exit 1 # for Model Predict ST +pytest test_tensor.py -s || exit 1 +pytest test_model.py -s || exit 1 +pytest test_model_parallel_runner.py -s || exit 1 +echo "---------- Run MindSpore Lite API SUCCESS ----------" #--------------------------------------------------------- echo "success" exit 0