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

Method _transpose_and_arithmetic_op_broadcast_helper

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

Source from the content-addressed store, hash-verified

289 self._tensor_arithmetic_op_broadcast_helper(gpu_dev)
290
291 def _transpose_and_arithmetic_op_broadcast_helper(self, dev):
292
293 def _test(s1, s2, axis1, axis2, s3, s_op, n_op, dev):
294 x_0 = np.random.random(s1).astype(np.float32)
295 y_0 = np.random.random(s2).astype(np.float32)
296
297 x0 = tensor.Tensor(device=dev, data=x_0)
298 y0 = tensor.Tensor(device=dev, data=y_0)
299
300 x1 = x0.transpose(axis1)
301 y1 = y0.transpose(axis2)
302
303 z0 = tensor._call_singa_func(s_op, x1.data, y1.data)
304 z0.to_host()
305
306 np.testing.assert_array_almost_equal(
307 tensor.to_numpy(z0),
308 n_op(x_0.transpose(axis1), y_0.transpose(axis2)))
309 np.testing.assert_array_almost_equal(z0.shape, s3)
310 return
311
312 for s_op, n_op in zip([
313 singa_api.Pow,
314 singa_api.__add__,
315 singa_api.__div__,
316 singa_api.__sub__,
317 singa_api.__mul__,
318 ], [np.power, np.add, np.divide, np.subtract, np.multiply]):
319 s1 = [1, 5, 1, 3]
320 s2 = [3, 1, 1, 4]
321 axis1 = [3, 2, 1, 0] # 3121
322 axis2 = [1, 0, 2, 3] # 1314
323 s3 = [3, 3, 5, 4]
324 _test(s1, s2, axis1, axis2, s3, s_op, n_op, dev)
325
326 s1 = [1, 5, 1]
327 s2 = [1, 3, 2]
328 axis1 = [2, 1, 0] # 151
329 axis2 = [1, 0, 2] # 312
330 s3 = [3, 5, 2]
331 _test(s1, s2, axis1, axis2, s3, s_op, n_op, dev)
332
333 s1 = [5, 1]
334 s2 = [1, 3]
335 axis1 = [1, 0] # 15
336 axis2 = [1, 0] # 31
337 s3 = [3, 5]
338 _test(s1, s2, axis1, axis2, s3, s_op, n_op, dev)
339
340 def test_transpose_and_arithmetic_op_broadcast_cpu(self):
341 self._transpose_and_arithmetic_op_broadcast_helper(cpu_dev)

Calls

no outgoing calls

Tested by

no test coverage detected