| 239 | |
| 240 | |
| 241 | def 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 | |
| 268 | def test_elemwise_relu(): |