(self, dev)
| 2259 | self._less_broadcast_helper(gpu_dev) |
| 2260 | |
| 2261 | def _add_broadcast_helper(self, dev): |
| 2262 | cases = [ |
| 2263 | ([3, 4, 5], [5]), # 3d vs 1d |
| 2264 | ([3, 4, 5], [4, 5]), # 3d vs 2d |
| 2265 | ([3, 4, 5, 6], [5, 6]), # 4d vs 2d |
| 2266 | ([3, 4, 5, 6], [4, 5, 6]), # 4d vs 3d |
| 2267 | ([1, 4, 1, 6], [3, 1, 5, 6]) # 4d vs 4d |
| 2268 | ] |
| 2269 | for in1, in2 in cases: |
| 2270 | x = np.random.randn(*in1).astype(np.float32) |
| 2271 | x1 = np.random.randn(*in2).astype(np.float32) |
| 2272 | y = x + x1 |
| 2273 | |
| 2274 | dy = np.random.randn(*y.shape) |
| 2275 | grad0 = np.sum(dy, axis=axis_helper(y.shape, |
| 2276 | x.shape)).reshape(x.shape) |
| 2277 | grad1 = np.sum(dy, axis=axis_helper(y.shape, |
| 2278 | x1.shape)).reshape(x1.shape) |
| 2279 | |
| 2280 | x = tensor.from_numpy(x) |
| 2281 | x1 = tensor.from_numpy(x1) |
| 2282 | dy = tensor.from_numpy(dy) |
| 2283 | x.to_device(dev) |
| 2284 | x1.to_device(dev) |
| 2285 | dy.to_device(dev) |
| 2286 | |
| 2287 | result = autograd.add(x, x1) |
| 2288 | dx0, dx1 = result.creator.backward(dy.data) |
| 2289 | np.testing.assert_array_almost_equal(tensor.to_numpy(result), |
| 2290 | y, |
| 2291 | decimal=5) |
| 2292 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 2293 | tensor.from_raw_tensor(dx0)), |
| 2294 | grad0, |
| 2295 | decimal=5) |
| 2296 | np.testing.assert_array_almost_equal(tensor.to_numpy( |
| 2297 | tensor.from_raw_tensor(dx1)), |
| 2298 | grad1, |
| 2299 | decimal=5) |
| 2300 | |
| 2301 | def test_add_broadcast_cpu(self): |
| 2302 | self._add_broadcast_helper(cpu_dev) |
no test coverage detected