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

Function test_elemwise_add

imperative/python/test/unit/core/test_autodiff.py:241–265  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

239
240
241def test_elemwise_add():
242 x_np = np.random.rand(10).astype("float32")
243 y_np = np.random.rand(10, 10).astype("float32")
244 dz_np = np.random.rand(10, 10).astype("float32")
245 x = mge.Tensor(x_np)
246 y = mge.Tensor(y_np)
247 dz = mge.Tensor(dz_np)
248
249 refs = {}
250
251 def f(x, y):
252 x = x * 2
253 refs["x"] = TensorWeakRef(x)
254 refs["y"] = TensorWeakRef(y)
255 return x + y
256
257 with Grad() as grad:
258 grad.wrt(x, callback=save_to(x))
259 z = f(x, y)
260 del y
261 for k, r in refs.items():
262 assert r() is None
263 grad(z, dz)
264
265 np.testing.assert_almost_equal(x.grad.numpy(), dz_np.sum(0) * 2, decimal=5)
266
267
268def test_elemwise_relu():

Callers

nothing calls this directly

Calls 10

GradClass · 0.90
save_toFunction · 0.85
gradFunction · 0.85
wrtMethod · 0.80
itemsMethod · 0.80
fFunction · 0.70
astypeMethod · 0.45
TensorMethod · 0.45
numpyMethod · 0.45
sumMethod · 0.45

Tested by

no test coverage detected