MCPcopy
hub / github.com/apache/tvm / set_input_trial

Function set_input_trial

tests/python/relax/test_vm_build.py:1063–1093  ·  view source on GitHub ↗
(vm: relax.VirtualMachine, device: tvm.runtime.Device)

Source from the content-addressed store, hash-verified

1061
1062
1063def 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
1096def set_input_attempt_stateless(vm: relax.VirtualMachine, device: tvm.runtime.Device) -> None:

Callers 1

test_set_inputFunction · 0.85

Calls 6

tupleFunction · 0.85
set_inputMethod · 0.80
invoke_statefulMethod · 0.80
get_outputsMethod · 0.80
numpyMethod · 0.80
astypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…