def test_code_add_transpose(self): idi = numpy.identity(2, dtype=numpy.float32) onx = OnnxTranspose(OnnxAdd('X', idi, op_version=get_opset_number_from_onnx()), output_names=['Y'], op_version=get_opset_number_from_onnx()) model_def = onx.to_onnx({'X': idi.astype(numpy.float32)}) oinf = OnnxInference(model_def, runtime='python') res = oinf.to_python(inline=False) self.assertNotEmpty(res) self.assertIsInstance(res, dict) self.assertEqual(len(res), 2) self.assertIn('onnx_pyrt_Ad_Addcst.pkl', res) self.assertIn('onnx_pyrt_main.py', res) cd = res['onnx_pyrt_main.py'] self.assertIn('def pyrt_Add(X, Ad_Addcst):', cd) self.assertIn('def run(self, X):', cd) # inline temp = get_temp_folder(__file__, "temp_code_add_transpose") res = oinf.to_python(inline=True, dest=temp) self.assertNotEmpty(res) name = os.path.join(temp, 'onnx_pyrt_main.py') self.assertExists(name) # test code test_code = """ X = numpy.array([[1, 2], [3, 4]], dtype=numpy.float32) oinf = OnnxPythonInference() Y = oinf.run(X) print(Y) """ X = numpy.array([[1, 2], [3, 4]], dtype=numpy.float32) exp = oinf.run({'X': X})['Y'] sexp = str(exp) self.auto_test_script(name, test_code, sexp)
def test_code_add_except(self): idi = numpy.identity(2, dtype=numpy.float32) onx = OnnxAdd('X', idi, output_names=['Y'], op_version=TARGET_OPSET) model_def = onx.to_onnx({'X': idi.astype(numpy.float32)}, target_opset=TARGET_OPSET) model_def.ir_version = get_ir_version(TARGET_OPSET) oinf = OnnxInference(model_def, runtime='onnxruntime1') try: oinf.to_python() except ValueError: pass