(self, dev)
| 2353 | self._sub_broadcast_helper(gpu_dev) |
| 2354 | |
| 2355 | def _mul_broadcast_helper(self, dev): |
| 2356 | cases = [ |
| 2357 | ([3, 4, 5], [5]), # 3d vs 1d |
| 2358 | ([3, 4, 5], [4, 5]), # 3d vs 2d |
| 2359 | ([3, 4, 5, 6], [5, 6]), # 4d vs 2d |
| 2360 | ([3, 4, 5, 6], [4, 5, 6]), # 4d vs 3d |
| 2361 | ([1, 4, 1, 6], [3, 1, 5, 6]) # 4d vs 4d |
| 2362 | ] |
| 2363 | for in1, in2 in cases: |
| 2364 | x = np.random.randn(*in1).astype(np.float32) |
| 2365 | x1 = np.random.randn(*in2).astype(np.float32) |
| 2366 | y = x * x1 |
| 2367 | |
| 2368 | dy = np.random.randn(*y.shape) |
| 2369 | grad0 = np.sum(x1 * dy, axis=axis_helper(y.shape, |
| 2370 | x.shape)).reshape(x.shape) |
| 2371 | grad1 = np.sum(x * dy, axis=axis_helper(y.shape, |
| 2372 | x1.shape)).reshape(x1.shape) |
| 2373 | |
| 2374 | x = tensor.from_numpy(x) |
| 2375 | x1 = tensor.from_numpy(x1) |
| 2376 | dy = tensor.from_numpy(dy) |
| 2377 | x.to_device(dev) |
| 2378 | x1.to_device(dev) |
| 2379 | dy.to_device(dev) |
| 2380 | |
| 2381 | result = autograd.mul(x, x1) |
| 2382 | dx0, dx1 = result.creator.backward(dy.data) |
| 2383 | np.testing.assert_array_almost_equal(tensor.to_numpy(result), |
| 2384 | y, |
| 2385 | decimal=5) |
| 2386 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 2387 | tensor.from_raw_tensor(dx0)), |
| 2388 | grad0, |
| 2389 | decimal=5) |
| 2390 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 2391 | tensor.from_raw_tensor(dx1)), |
| 2392 | grad1, |
| 2393 | decimal=5) |
| 2394 | |
| 2395 | def test_mul_broadcast_cpu(self): |
| 2396 | self._mul_broadcast_helper(cpu_dev) |
no test coverage detected