• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""
16Test lite converter python API.
17Note:
18    Please make sure "export MSLITE_ENABLE_CONVERTER=ON" before compiling and using the API.
19"""
20
21import mindspore_lite as mslite
22import pytest
23
24
25# ============================ Converter ============================
26def test_converter():
27    converter = mslite.Converter()
28    assert "weight_fp16:" in str(converter)
29
30
31def test_converter_weight_fp16_type_error():
32    with pytest.raises(TypeError) as raise_info:
33        converter = mslite.Converter()
34        converter.weight_fp16 = "1"
35    assert "weight_fp16 must be bool" in str(raise_info.value)
36
37
38def test_converter_input_shape_type_error():
39    with pytest.raises(TypeError) as raise_info:
40        converter = mslite.Converter()
41        converter.input_shape = "{'input': [1, 112, 112, 3]}"
42    assert "input_shape must be dict" in str(raise_info.value)
43
44
45def test_converter_input_shape_key_type_error():
46    with pytest.raises(TypeError) as raise_info:
47        converter = mslite.Converter()
48        converter.input_shape = {1: '[1, 112, 112, 3]'}
49    assert "input_shape key must be str" in str(raise_info.value)
50
51
52def test_converter_input_shape_value_type_error():
53    with pytest.raises(TypeError) as raise_info:
54        converter = mslite.Converter()
55        converter.input_shape = {'input': '[1, 112, 112, 3]'}
56    assert "input_shape value must be list" in str(raise_info.value)
57
58
59def test_converter_input_shape_value_element_type_error():
60    with pytest.raises(TypeError) as raise_info:
61        converter = mslite.Converter()
62        converter.input_shape = {'input': [1, '112', 112, 3]}
63    assert "input_shape value's element must be int" in str(raise_info.value)
64
65
66def test_converter_input_format_type_error():
67    with pytest.raises(TypeError) as raise_info:
68        converter = mslite.Converter()
69        converter.input_format = 1
70    assert "input_format must be Format" in str(raise_info.value)
71
72
73def test_converter_input_format_value_error():
74    with pytest.raises(ValueError) as raise_info:
75        converter = mslite.Converter()
76        converter.input_format = mslite.Format.HW
77    assert "input_format must be in" in str(raise_info.value)
78
79
80def test_converter_input_data_type_type_error():
81    with pytest.raises(TypeError) as raise_info:
82        converter = mslite.Converter()
83        converter.input_data_type = 1
84    assert "input_data_type must be DataType" in str(raise_info.value)
85
86
87def test_converter_input_data_type_value_error():
88    with pytest.raises(ValueError) as raise_info:
89        converter = mslite.Converter()
90        converter.input_data_type = mslite.DataType.INVALID
91    assert "input_data_type must be in" in str(raise_info.value)
92
93
94def test_converter_output_data_type_type_error():
95    with pytest.raises(TypeError) as raise_info:
96        converter = mslite.Converter()
97        converter.output_data_type = 1
98    assert "output_data_type must be DataType" in str(raise_info.value)
99
100
101def test_converter_output_data_type_value_error():
102    with pytest.raises(ValueError) as raise_info:
103        converter = mslite.Converter()
104        converter.output_data_type = mslite.DataType.INVALID
105    assert "output_data_type must be in" in str(raise_info.value)
106
107
108def test_converter_save_type_type_error():
109    with pytest.raises(TypeError) as raise_info:
110        converter = mslite.Converter()
111        converter.save_type = 1
112    assert "save_type must be ModelType" in str(raise_info.value)
113
114
115def test_converter_decrypt_key_type_error():
116    with pytest.raises(TypeError) as raise_info:
117        converter = mslite.Converter()
118        converter.decrypt_key = 1
119    assert "decrypt_key must be str" in str(raise_info.value)
120
121
122def test_converter_decrypt_mode_type_error():
123    with pytest.raises(TypeError) as raise_info:
124        converter = mslite.Converter()
125        converter.decrypt_mode = 1
126    assert "decrypt_mode must be str" in str(raise_info.value)
127
128
129def test_converter_decrypt_mode_value_error():
130    with pytest.raises(ValueError) as raise_info:
131        converter = mslite.Converter()
132        converter.decrypt_mode = "1"
133    assert "decrypt_mode must be in" in str(raise_info.value)
134
135
136def test_converter_enable_encryption_type_error():
137    with pytest.raises(TypeError) as raise_info:
138        converter = mslite.Converter()
139        converter.enable_encryption = ""
140    assert "enable_encryption must be bool" in str(raise_info.value)
141
142
143def test_converter_encrypt_key_type_error():
144    with pytest.raises(TypeError) as raise_info:
145        converter = mslite.Converter()
146        converter.encrypt_key = 1
147    assert "encrypt_key must be str" in str(raise_info.value)
148
149
150def test_converter_infer_type_error():
151    with pytest.raises(TypeError) as raise_info:
152        converter = mslite.Converter()
153        converter.infer = "1"
154    assert "infer must be bool" in str(raise_info.value)
155
156
157def test_converter_optimize_type_error():
158    with pytest.raises(TypeError) as raise_info:
159        converter = mslite.Converter()
160        converter.optimize = 1
161    assert "optimize must be str" in str(raise_info.value)
162
163
164def test_converter_device_type_error():
165    with pytest.raises(TypeError) as raise_info:
166        converter = mslite.Converter()
167        converter.device = 1
168    assert "device must be str" in str(raise_info.value)
169
170
171def test_converter_device_value_error():
172    with pytest.raises(ValueError) as raise_info:
173        converter = mslite.Converter()
174        converter.device = "INVALID"
175    assert "device must be in" in str(raise_info.value)
176
177
178def test_converter_set_config_info_section_type_error():
179    with pytest.raises(TypeError) as raise_info:
180        converter = mslite.Converter()
181        section = 2
182        config_info = {"device": "3"}
183        converter.set_config_info(section, config_info)
184    assert "section must be str" in str(raise_info.value)
185
186
187def test_converter_set_config_info_config_info_type_error():
188    with pytest.raises(TypeError) as raise_info:
189        converter = mslite.Converter()
190        section = "acl_param"
191        config_info = ["device_id", 3]
192        converter.set_config_info(section, config_info)
193    assert "config_info must be dict" in str(raise_info.value)
194
195
196def test_converter_set_config_info_config_info_key_type_error():
197    with pytest.raises(TypeError) as raise_info:
198        converter = mslite.Converter()
199        section = "acl_param"
200        config_info = {2: "3"}
201        converter.set_config_info(section, config_info)
202    assert "config_info key must be str" in str(raise_info.value)
203
204
205def test_converter_set_config_info_config_info_value_type_error():
206    with pytest.raises(TypeError) as raise_info:
207        converter = mslite.Converter()
208        section = "acl_param"
209        config_info = {"device_id": 3}
210        converter.set_config_info(section, config_info)
211    assert "config_info val must be str" in str(raise_info.value)
212
213
214def test_converter_set_config_info():
215    converter = mslite.Converter()
216    section = "acl_param"
217    config_info = {"device_id": "3"}
218    converter.set_config_info(section, config_info)
219    assert "config_info: {'acl_param': {'device_id': '3'}" in str(converter)
220
221
222def test_converter_convert_fmk_type_type_error():
223    with pytest.raises(TypeError) as raise_info:
224        converter = mslite.Converter()
225        converter.convert(fmk_type="", model_file="test.tflite", output_file="test.tflite")
226    assert "fmk_type must be FmkType" in str(raise_info.value)
227
228
229def test_converter_convert_model_file_type_error():
230    with pytest.raises(TypeError) as raise_info:
231        converter = mslite.Converter()
232        converter.convert(fmk_type=mslite.FmkType.TFLITE, model_file=1, output_file="mobilenetv2.tflite")
233    assert "model_file must be str" in str(raise_info.value)
234
235
236def test_converter_convert_model_file_not_exist_error():
237    with pytest.raises(RuntimeError) as raise_info:
238        converter = mslite.Converter()
239        converter.convert(fmk_type=mslite.FmkType.TFLITE, model_file="test.tflite", output_file="mobilenetv2.tflite")
240    assert "model_file does not exist" in str(raise_info.value)
241
242
243def test_converter_convert_output_file_type_error():
244    with pytest.raises(TypeError) as raise_info:
245        converter = mslite.Converter()
246        converter.convert(fmk_type=mslite.FmkType.TFLITE, model_file="mobilenetv2.tflite", output_file=1)
247    assert "output_file must be str" in str(raise_info.value)
248
249
250def test_converter_convert_weight_file_type_error():
251    with pytest.raises(TypeError) as raise_info:
252        converter = mslite.Converter()
253        converter.convert(fmk_type=mslite.FmkType.TFLITE, model_file="mobilenetv2.tflite",
254                          output_file="mobilenetv2.tflite", weight_file=1)
255    assert "weight_file must be str" in str(raise_info.value)
256
257
258def test_converter_convert_weight_file_not_exist_error():
259    with pytest.raises(RuntimeError) as raise_info:
260        converter = mslite.Converter()
261        converter.convert(fmk_type=mslite.FmkType.TFLITE, model_file="mobilenetv2.tflite",
262                          output_file="mobilenetv2.tflite", weight_file="test.caffemodel")
263    assert "weight_file does not exist" in str(raise_info.value)
264
265
266def test_converter_convert_config_file_type_error():
267    with pytest.raises(TypeError) as raise_info:
268        converter = mslite.Converter()
269        converter.convert(fmk_type=mslite.FmkType.TFLITE, model_file="mobilenetv2.tflite",
270                          output_file="mobilenetv2.tflite", config_file=1)
271    assert "config_file must be str" in str(raise_info.value)
272
273
274def test_converter_convert_config_file_not_exist_error():
275    with pytest.raises(RuntimeError) as raise_info:
276        converter = mslite.Converter()
277        converter.convert(fmk_type=mslite.FmkType.TFLITE, model_file="mobilenetv2.tflite",
278                          output_file="mobilenetv2.tflite", config_file="mobilenetv2_full_quant.cfg")
279    assert "config_file does not exist" in str(raise_info.value)
280
281
282def test_converter_convert():
283    converter = mslite.Converter()
284    converter.convert(fmk_type=mslite.FmkType.TFLITE, model_file="mobilenetv2.tflite",
285                      output_file="mobilenetv2.tflite", weight_file="")
286    assert "weight_fp16:" in str(converter)
287