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

Function test_stack

imperative/python/test/unit/functional/test_tensor.py:176–217  ·  view source on GitHub ↗
(is_varnode)

Source from the content-addressed store, hash-verified

174
175@pytest.mark.parametrize("is_varnode", [True, False])
176def test_stack(is_varnode):
177 if is_varnode:
178 network = Network()
179 else:
180 network = None
181
182 data1 = np.random.random((3, 2, 2)).astype("float32")
183 data2 = np.random.random((3, 2, 2)).astype("float32")
184 data3 = np.random.random((3, 2, 2)).astype("float32")
185
186 cases = [{"input": [data1, data2]}, {"input": [data1, data3]}]
187 for ai in range(3):
188
189 def run(data1, data2):
190 return F.stack([data1, data2], axis=ai)
191
192 opr_test(
193 cases, run, ref_fn=lambda x, y: np.stack([x, y], axis=ai), network=network
194 )
195
196 x1 = Tensor(np.arange(0, 3, dtype=np.float32).reshape((3)))
197 x2 = Tensor(np.arange(6, 9, dtype=np.float32).reshape((3)))
198 y = F.stack([x1, x2], axis=-1)
199 np.testing.assert_equal(
200 y.numpy(), np.array([[0, 6], [1, 7], [2, 8]]).astype(np.float32)
201 )
202
203 x1 = Tensor(np.arange(0, 3, dtype=np.float32).reshape((3)))
204 x2 = Tensor(np.arange(6, 9, dtype=np.float32).reshape((3)))
205 y = F.stack([x1, x2], axis=-1)
206 np.testing.assert_equal(
207 y.numpy(), np.array([[0, 6], [1, 7], [2, 8]]).astype(np.float32)
208 )
209
210 x1 = Tensor(np.random.rand(600))
211 x2 = F.broadcast_to(Tensor(np.array(3)), (600,))
212
213 y = F.stack([x2, x1], axis=0)
214 np.testing.assert_equal(y.numpy(), np.stack((x2.numpy(), x1.numpy()), axis=0))
215
216 y = F.stack([x2, x2], axis=0)
217 np.testing.assert_equal(y.numpy(), np.stack((x2.numpy(), x2.numpy()), axis=0))
218
219
220@pytest.mark.parametrize("is_varnode", [True, False])

Callers

nothing calls this directly

Calls 10

numpyMethod · 0.95
NetworkClass · 0.90
opr_testFunction · 0.90
TensorClass · 0.90
assert_equalMethod · 0.80
arrayMethod · 0.80
broadcast_toMethod · 0.80
astypeMethod · 0.45
reshapeMethod · 0.45
numpyMethod · 0.45

Tested by

no test coverage detected