MCPcopy Create free account
hub / github.com/apache/tvm / test_keep_params

Function test_keep_params

tests/python/relax/test_frontend_from_fx.py:4992–5043  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4990
4991
4992def test_keep_params():
4993 class Conv2D1(Module):
4994 def __init__(self):
4995 super().__init__()
4996 self.conv = torch.nn.Conv2d(3, 6, 7, bias=True)
4997
4998 def forward(self, input):
4999 return self.conv(input)
5000
5001 @tvm.script.ir_module
5002 class expected1:
5003 @R.function
5004 def main(
5005 input_1: R.Tensor((1, 3, 10, 10), dtype="float32"),
5006 w1: R.Tensor((6,), dtype="float32"),
5007 w2: R.Tensor((6, 3, 7, 7), dtype="float32"),
5008 ) -> R.Tensor((1, 6, 4, 4), dtype="float32"):
5009 R.func_attr({"num_input": 1})
5010 # block 0
5011 with R.dataflow():
5012 lv1: R.Tensor((1, 6, 4, 4), dtype="float32") = R.nn.conv2d(
5013 input_1,
5014 w2,
5015 strides=[1, 1],
5016 padding=[0, 0, 0, 0],
5017 dilation=[1, 1],
5018 data_layout="NCHW",
5019 kernel_layout="OIHW",
5020 out_layout="NCHW",
5021 out_dtype="float32",
5022 )
5023 lv2: R.Tensor((1, 6, 1, 1), dtype="float32") = R.reshape(w1, [1, 6, 1, 1])
5024 lv3: R.Tensor((1, 6, 4, 4), dtype="float32") = R.add(lv1, lv2)
5025 gv: R.Tensor((1, 6, 4, 4), dtype="float32") = lv3
5026 R.output(gv)
5027 return gv
5028
5029 model = Conv2D1()
5030 graph_model = fx.symbolic_trace(model)
5031 mod = from_fx(graph_model, [([1, 3, 10, 10], "float32")], keep_params_as_input=True)
5032 mod, params = detach_params(mod)
5033 tvm.ir.assert_structural_equal(mod, expected1)
5034 func = mod["main"]
5035 params = params["main"]
5036
5037 assert len(params) == len(func.params) - 1
5038 for param_var, param_tensor in zip(func.params[1:], params):
5039 assert tuple(x.value for x in param_var.struct_info.shape.values) == param_tensor.shape
5040 assert param_var.struct_info.dtype == param_tensor.dtype
5041
5042 tvm.testing.assert_allclose(params[0].numpy(), model.conv.bias.detach().detach().numpy())
5043 tvm.testing.assert_allclose(params[1].numpy(), model.conv.weight.detach().detach().numpy())
5044
5045
5046def test_unwrap_unit_return_tuple():

Callers

nothing calls this directly

Calls 5

from_fxFunction · 0.90
detach_paramsFunction · 0.90
tupleFunction · 0.85
numpyMethod · 0.80
Conv2D1Class · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…