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

Method _gemm_helper

test/python/test_operation.py:2553–2621  ·  view source on GitHub ↗
(self, dev)

Source from the content-addressed store, hash-verified

2551 self._prelu_broadcast_helper(gpu_dev)
2552
2553 def _gemm_helper(self, dev):
2554 configs = [
2555 # alpha, beta, transA, transB, shapeA, shapeB, shapeC, shapeY
2556 [0.25, 0.35, 0, 0, (3, 4), (4, 5), (1, 5), (3, 5)],
2557 [0.25, 0.35, 0, 1, (3, 4), (5, 4), (1, 5), (3, 5)],
2558 [0.25, 0.35, 1, 0, (4, 3), (4, 5), (1, 5), (3, 5)],
2559 [0.25, 0.35, 1, 1, (4, 3), (5, 4), (1, 5), (3, 5)],
2560 ]
2561 for config in configs:
2562 alpha = config[0]
2563 beta = config[1]
2564 transA = config[2]
2565 transB = config[3]
2566 shapeA = config[4]
2567 shapeB = config[5]
2568 shapeC = config[6]
2569 shapeY = config[7]
2570
2571 A = np.random.randn(*shapeA).astype(np.float32)
2572 DY = np.ones(shapeY, dtype=np.float32)
2573
2574 if transB == 0:
2575 out_features = shapeB[1]
2576 else:
2577 out_features = shapeB[0]
2578
2579 a = tensor.from_numpy(A)
2580 a.to_device(dev)
2581 dy = tensor.from_numpy(DY)
2582 dy.to_device(dev)
2583
2584 gemm = layer.Gemm(out_features, alpha, beta, transA == 1,
2585 transB == 1)
2586 result = gemm(a)
2587
2588 params = gemm.get_params()
2589 B = tensor.to_numpy(params['W'])
2590 C = tensor.to_numpy(params['b'])
2591
2592 da, db, dc = result.creator.backward(dy.data)
2593
2594 # Y = alpha * A' * B' + beta * C
2595 _A = A if transA == 0 else A.T
2596 _B = B if transB == 0 else B.T
2597 C = C if C is not None else np.array(0)
2598 Y = alpha * np.dot(_A, _B) + beta * C
2599
2600 DA = alpha * np.matmul(DY, _B.T)
2601 DA = DA if transA == 0 else DA.T
2602 DB = alpha * np.matmul(_A.T, DY)
2603 DB = DB if transB == 0 else DB.T
2604 DC = beta * np.sum(DY, axis=axis_helper(Y.shape, C.shape)).reshape(
2605 C.shape)
2606
2607 np.testing.assert_array_almost_equal(tensor.to_numpy(result),
2608 Y,
2609 decimal=5)
2610 np.testing.assert_array_almost_equal(tensor.to_numpy(

Callers 2

test_gemm_cpuMethod · 0.95
test_gemm_gpuMethod · 0.95

Calls 7

get_paramsMethod · 0.95
gemmFunction · 0.85
dotMethod · 0.80
axis_helperFunction · 0.70
to_deviceMethod · 0.45
backwardMethod · 0.45
reshapeMethod · 0.45

Tested by

no test coverage detected