MCPcopy Create free account
hub / github.com/open-mmlab/mmengine / test_init_weights

Method test_init_weights

tests/test_model/test_base_module.py:127–263  ·  view source on GitHub ↗

Config model (FooModel, Linear: weight=1, bias=2, Conv1d: weight=3, bias=4, Conv2d: weight=5, bias=6) ├──component1 (FooConv1d) ├──component2 (FooConv2d) ├──component3 (FooLinear) ├──component4 (FooLinearConv1d) ├──

(self)

Source from the content-addressed store, hash-verified

125 assert self.BaseModule.is_init is False
126
127 def test_init_weights(self):
128 """
129 Config
130 model (FooModel, Linear: weight=1, bias=2, Conv1d: weight=3, bias=4,
131 Conv2d: weight=5, bias=6)
132 ├──component1 (FooConv1d)
133 ├──component2 (FooConv2d)
134 ├──component3 (FooLinear)
135 ├──component4 (FooLinearConv1d)
136 ├──linear (FooLinear)
137 ├──conv1d (FooConv1d)
138 ├──reg (nn.Linear)
139 Parameters after initialization
140 model (FooModel)
141 ├──component1 (FooConv1d, weight=3, bias=4)
142 ├──component2 (FooConv2d, weight=5, bias=6)
143 ├──component3 (FooLinear, weight=1, bias=2)
144 ├──component4 (FooLinearConv1d)
145 ├──linear (FooLinear, weight=1, bias=2)
146 ├──conv1d (FooConv1d, weight=3, bias=4)
147 ├──reg (nn.Linear, weight=1, bias=2)
148 """
149 self.model.init_weights()
150
151 assert torch.equal(
152 self.model.component1.conv1d.weight,
153 torch.full(self.model.component1.conv1d.weight.shape, 3.0))
154 assert torch.equal(
155 self.model.component1.conv1d.bias,
156 torch.full(self.model.component1.conv1d.bias.shape, 4.0))
157 assert torch.equal(
158 self.model.component2.conv2d.weight,
159 torch.full(self.model.component2.conv2d.weight.shape, 5.0))
160 assert torch.equal(
161 self.model.component2.conv2d.bias,
162 torch.full(self.model.component2.conv2d.bias.shape, 6.0))
163 assert torch.equal(
164 self.model.component3.linear.weight,
165 torch.full(self.model.component3.linear.weight.shape, 1.0))
166 assert torch.equal(
167 self.model.component3.linear.bias,
168 torch.full(self.model.component3.linear.bias.shape, 2.0))
169 assert torch.equal(
170 self.model.component4.linear.linear.weight,
171 torch.full(self.model.component4.linear.linear.weight.shape, 1.0))
172 assert torch.equal(
173 self.model.component4.linear.linear.bias,
174 torch.full(self.model.component4.linear.linear.bias.shape, 2.0))
175 assert torch.equal(
176 self.model.component4.conv1d.conv1d.weight,
177 torch.full(self.model.component4.conv1d.conv1d.weight.shape, 3.0))
178 assert torch.equal(
179 self.model.component4.conv1d.conv1d.bias,
180 torch.full(self.model.component4.conv1d.conv1d.bias.shape, 4.0))
181 assert torch.equal(self.model.reg.weight,
182 torch.full(self.model.reg.weight.shape, 1.0))
183 assert torch.equal(self.model.reg.bias,
184 torch.full(self.model.reg.bias.shape, 2.0))

Callers

nothing calls this directly

Calls 7

FakeDDPClass · 0.85
CustomLinearClass · 0.85
cloneMethod · 0.80
childrenMethod · 0.80
init_weightsMethod · 0.45
state_dictMethod · 0.45
buildMethod · 0.45

Tested by

no test coverage detected