MCPcopy Create free account
hub / github.com/apache/singa / _tensor_arithmetic_op_broadcast_helper

Method _tensor_arithmetic_op_broadcast_helper

test/python/test_api.py:250–282  ·  view source on GitHub ↗
(self, dev)

Source from the content-addressed store, hash-verified

248 self._softmax_api_helper(gpu_dev)
249
250 def _tensor_arithmetic_op_broadcast_helper(self, dev):
251
252 def _run_test(dev, singa_op, np_op, s1, s2):
253 x_0 = np.random.random(s1).astype(np.float32)
254 y_0 = np.random.random(s2).astype(np.float32)
255 x0 = tensor.Tensor(device=dev, data=x_0)
256 y0 = tensor.Tensor(device=dev, data=y_0)
257
258 z0 = tensor._call_singa_func(singa_op, x0.data, y0.data)
259 z0.to_host()
260 np.testing.assert_array_almost_equal(tensor.to_numpy(z0),
261 np_op(x_0, y_0))
262 return
263
264 for s_op, n_op in zip([
265 singa_api.Pow,
266 singa_api.__add__,
267 singa_api.__div__,
268 singa_api.__sub__,
269 singa_api.__mul__,
270 ], [np.power, np.add, np.divide, np.subtract, np.multiply]):
271 _run_test(dev, s_op, n_op, [6], [1])
272 _run_test(dev, s_op, n_op, [2, 3], [2, 3])
273 _run_test(dev, s_op, n_op, [3, 2], [1])
274 _run_test(dev, s_op, n_op, [3, 1, 2], [3, 1, 1])
275 _run_test(dev, s_op, n_op, [2, 3, 4, 5], [5])
276 _run_test(dev, s_op, n_op, [2, 3, 4, 5], [1, 1, 1])
277 _run_test(dev, s_op, n_op, [2, 3, 4, 5], [1, 1, 1, 1])
278 _run_test(dev, s_op, n_op, [2, 3, 4, 5], [4, 5]) # 45+2345=2345
279 _run_test(dev, s_op, n_op, [3, 1, 2, 1], [3, 1, 2])
280 _run_test(dev, s_op, n_op, [4, 5], [2, 3, 4, 5]) # 45+2345=2345
281 _run_test(dev, s_op, n_op, [1, 4, 5], [2, 3, 1, 1]) # 145+2311=2345
282 _run_test(dev, s_op, n_op, [3, 4, 5], [2, 1, 1, 1]) # 345+2111=2345
283
284 def test_tensor_arithmetic_op_broadcast_cpu(self):
285 self._tensor_arithmetic_op_broadcast_helper(cpu_dev)

Calls

no outgoing calls

Tested by

no test coverage detected