(vm: relax.VirtualMachine, device: tvm.runtime.Device)
| 1061 | |
| 1062 | |
| 1063 | def set_input_trial(vm: relax.VirtualMachine, device: tvm.runtime.Device) -> None: |
| 1064 | a = tvm.runtime.tensor(np.random.rand(32, 32).astype("float32"), device) |
| 1065 | b = tvm.runtime.tensor(np.random.rand(32, 32).astype("float32"), device) |
| 1066 | vm.set_input("main", a, b) |
| 1067 | vm.invoke_stateful("main") |
| 1068 | res0 = vm.get_outputs("main") |
| 1069 | |
| 1070 | data_dict = {"x": a, "w": b} |
| 1071 | vm.set_input("main", **data_dict) |
| 1072 | vm.invoke_stateful("main") |
| 1073 | res1 = vm.get_outputs("main") |
| 1074 | tvm.testing.assert_allclose(res0.numpy(), a.numpy() * b.numpy(), rtol=1e-7, atol=1e-7) |
| 1075 | tvm.testing.assert_allclose(res0.numpy(), res1.numpy(), rtol=1e-7, atol=1e-7) |
| 1076 | |
| 1077 | # bug! If you don't bind the Tensor to a var, the memory will get corrupted. |
| 1078 | # Possibly due to object lifecycles and other FFI issues |
| 1079 | a = tvm.runtime.tensor(np.array(2).astype("int32"), device) |
| 1080 | vm.set_input("test_vm_tuple", a) |
| 1081 | vm.invoke_stateful("test_vm_tuple") |
| 1082 | res2 = vm.get_outputs("test_vm_tuple") |
| 1083 | # the results are Tensors wrapped around scalars, |
| 1084 | # so we have to get the scalar out of the Tensor |
| 1085 | assert tuple(map(lambda a: int(a.numpy()), res2)) == (2, 2) |
| 1086 | |
| 1087 | b = tvm.runtime.tensor(np.array(1).astype("int32"), device) |
| 1088 | vm.set_input("test_vm_nested_tuple", b) |
| 1089 | vm.invoke_stateful("test_vm_nested_tuple") |
| 1090 | res3 = vm.get_outputs("test_vm_nested_tuple") |
| 1091 | assert len(res3) == 2 and len(res3[0]) == 2 and len(res3[0][1]) == 1 |
| 1092 | result_cast = ((int(res3[0][0].numpy()), (int(res3[0][1][0].numpy()),)), int(res3[1].numpy())) |
| 1093 | assert result_cast == ((1, (1,)), 1) |
| 1094 | |
| 1095 | |
| 1096 | def set_input_attempt_stateless(vm: relax.VirtualMachine, device: tvm.runtime.Device) -> None: |
no test coverage detected
searching dependent graphs…