MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / test_module_api

Function test_module_api

imperative/python/test/unit/module/test_module.py:68–172  ·  view source on GitHub ↗
(test_traced_module)

Source from the content-addressed store, hash-verified

66
67@pytest.mark.parametrize("test_traced_module", [True, False])
68def test_module_api(test_traced_module):
69 m = MyModule()
70 if test_traced_module:
71 buff = m.buff
72 param = m.param
73 m = trace_module(m, Tensor(np.random.random((1, 4, 16, 16))))
74 assert "buff" not in m.__dict__
75 assert "param" not in m.__dict__
76 m.buff = buff
77 m.param = param
78
79 assert list(m.children()) == [m.bn, m.i]
80 assert list(m.named_children()) == [("bn", m.bn), ("i", m.i)]
81 assert list(m.modules()) == [m, m.bn, m.i, m.i.bn]
82 assert list(m.named_modules()) == [
83 ("", m),
84 ("bn", m.bn),
85 ("i", m.i),
86 ("i.bn", m.i.bn),
87 ]
88 assert list(m.named_modules(prefix="x")) == [
89 ("x", m),
90 ("x.bn", m.bn),
91 ("x.i", m.i),
92 ("x.i.bn", m.i.bn),
93 ]
94 assert list(m.buffers()) == [
95 m.bn.running_mean,
96 m.bn.running_var,
97 m.buff,
98 m.i.bn.running_mean,
99 m.i.bn.running_var,
100 ]
101 assert list(m.buffers(recursive=False)) == [m.buff]
102 assert list(m.named_buffers()) == [
103 ("bn.running_mean", m.bn.running_mean),
104 ("bn.running_var", m.bn.running_var),
105 ("buff", m.buff),
106 ("i.bn.running_mean", m.i.bn.running_mean),
107 ("i.bn.running_var", m.i.bn.running_var),
108 ]
109 assert list(m.parameters()) == [
110 m.bn.bias,
111 m.bn.weight,
112 m.i.bn.bias,
113 m.i.bn.weight,
114 m.param,
115 ]
116 assert list(m.named_parameters()) == [
117 ("bn.bias", m.bn.bias),
118 ("bn.weight", m.bn.weight),
119 ("i.bn.bias", m.i.bn.bias),
120 ("i.bn.weight", m.i.bn.weight),
121 ("param", m.param),
122 ]
123 assert list(m.tensors()) == [
124 m.bn.bias,
125 m.bn.running_mean,

Callers

nothing calls this directly

Calls 15

trace_moduleFunction · 0.90
TensorClass · 0.90
listFunction · 0.85
named_childrenMethod · 0.80
modulesMethod · 0.80
named_modulesMethod · 0.80
buffersMethod · 0.80
named_buffersMethod · 0.80
parametersMethod · 0.80
named_parametersMethod · 0.80
tensorsMethod · 0.80
named_tensorsMethod · 0.80

Tested by

no test coverage detected