Gemm#
Domain:
ai.onnxSince version: 29
Generalized matrix multiplication: Y = alpha * A’ * B’ + beta * C. A has rank at least 1 and B has rank 2. transA swaps A’s final two axes and has no effect on vectors; transB transposes B. For A’ of shape (…, K) and B’ of shape (K, N), Y has shape (…, N). C is optional and must be unidirectionally broadcastable to Y. If C is absent or beta is zero, its contribution and data read are skipped.
Inputs
A (T): Rank >= 1 tensor. After applying transA, its shape is (…, K).
B (T): Rank 2 tensor. Its shape is (K, N) if transB is 0, or (N, K) otherwise.
C (T): Optional bias, unidirectionally broadcastable to output shape (…, N). If absent, the computation is done as if C is a scalar 0.
Outputs
Y (T): Output tensor of shape (…, N).
Attributes
alpha (float): Scales the matrix product.
beta (float): Scales the bias.
transA (int): Transposes the final two axes of A.
transB (int): Transposes B.
Type Constraints
T: Constrain input and output types to float/int tensors. Allowed types: tensor(bfloat16), tensor(double), tensor(float), tensor(float16), tensor(int32), tensor(int64), tensor(uint32), tensor(uint64).
Examples#
test_cc_gemm_high_rank_transpose
Node:
Gemm(A, B, C) -> (Y)
Attributes:
transA = 1
transB = 1
alpha = 0.25
beta = 0.5
Inputs:
A: shape=(2, 5, 3), dtype=float32
[[[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.]],
[[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.],
[2., 2., 2.]]]
B: shape=(7, 5), dtype=float32
[[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.]]
C: shape=(2, 1, 7), dtype=float32
[[[ 0., 2., 4., 6., 8., 10., 12.]],
[[14., 16., 18., 20., 22., 24., 26.]]]
Outputs:
Y: shape=(2, 3, 7), dtype=float32
[[[ 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5],
[ 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5],
[ 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5]],
[[14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5],
[14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5],
[14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5]]]
test_cc_gemm_linear_empty_batch_bias
Node:
Gemm(A, B, C) -> (Y)
Attributes:
transB = 1
Inputs:
A: shape=(0, 3, 5), dtype=float32
[]
B: shape=(7, 5), dtype=float32
[[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.]]
C: shape=(7,), dtype=float32
[1., 1., 1., 1., 1., 1., 1.]
Outputs:
Y: shape=(0, 3, 7), dtype=float32
[]
test_cc_gemm_linear_empty_batch_no_bias
Node:
Gemm(A, B) -> (Y)
Attributes:
transB = 1
Inputs:
A: shape=(0, 3, 5), dtype=float32
[]
B: shape=(7, 5), dtype=float32
[[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.]]
Outputs:
Y: shape=(0, 3, 7), dtype=float32
[]
test_cc_gemm_linear_empty_features_bias
Node:
Gemm(A, B, C) -> (Y)
Attributes:
transB = 1
Inputs:
A: shape=(2, 3, 0), dtype=float32
[]
B: shape=(7, 0), dtype=float32
[]
C: shape=(7,), dtype=float32
[1., 1., 1., 1., 1., 1., 1.]
Outputs:
Y: shape=(2, 3, 7), dtype=float32
[[[1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1.]]]
test_cc_gemm_linear_empty_features_no_bias
Node:
Gemm(A, B) -> (Y)
Attributes:
transB = 1
Inputs:
A: shape=(2, 3, 0), dtype=float32
[]
B: shape=(7, 0), dtype=float32
[]
Outputs:
Y: shape=(2, 3, 7), dtype=float32
[[[0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.]],
[[0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.]]]
test_cc_gemm_linear_rank3_bias
Node:
Gemm(A, B, C) -> (Y)
Attributes:
transB = 1
Inputs:
A: shape=(2, 3, 5), dtype=float32
[[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]]]
B: shape=(7, 5), dtype=float32
[[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.]]
C: shape=(7,), dtype=float32
[1., 1., 1., 1., 1., 1., 1.]
Outputs:
Y: shape=(2, 3, 7), dtype=float32
[[[31., 31., 31., 31., 31., 31., 31.],
[31., 31., 31., 31., 31., 31., 31.],
[31., 31., 31., 31., 31., 31., 31.]],
[[31., 31., 31., 31., 31., 31., 31.],
[31., 31., 31., 31., 31., 31., 31.],
[31., 31., 31., 31., 31., 31., 31.]]]
test_cc_gemm_linear_rank3_no_bias
Node:
Gemm(A, B) -> (Y)
Attributes:
transB = 1
Inputs:
A: shape=(2, 3, 5), dtype=float32
[[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]]]
B: shape=(7, 5), dtype=float32
[[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.]]
Outputs:
Y: shape=(2, 3, 7), dtype=float32
[[[30., 30., 30., 30., 30., 30., 30.],
[30., 30., 30., 30., 30., 30., 30.],
[30., 30., 30., 30., 30., 30., 30.]],
[[30., 30., 30., 30., 30., 30., 30.],
[30., 30., 30., 30., 30., 30., 30.],
[30., 30., 30., 30., 30., 30., 30.]]]
test_cc_gemm_linear_rank4_bias
Node:
Gemm(A, B, C) -> (Y)
Attributes:
transB = 1
Inputs:
A: shape=(2, 3, 4, 5), dtype=float32
[[[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]]],
[[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]]]]
B: shape=(7, 5), dtype=float32
[[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.]]
C: shape=(7,), dtype=float32
[1., 1., 1., 1., 1., 1., 1.]
Outputs:
Y: shape=(2, 3, 4, 7), dtype=float32
[[[[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.]],
[[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.]],
[[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.]]],
[[[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.]],
[[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.]],
[[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.],
[31., 31., 31., ..., 31., 31., 31.]]]]
test_cc_gemm_linear_rank4_no_bias
Node:
Gemm(A, B) -> (Y)
Attributes:
transB = 1
Inputs:
A: shape=(2, 3, 4, 5), dtype=float32
[[[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]]],
[[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]],
[[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]]]]
B: shape=(7, 5), dtype=float32
[[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.]]
Outputs:
Y: shape=(2, 3, 4, 7), dtype=float32
[[[[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.]],
[[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.]],
[[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.]]],
[[[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.]],
[[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.]],
[[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.],
[30., 30., 30., ..., 30., 30., 30.]]]]
test_cc_gemm_linear_vector_bias
Node:
Gemm(A, B, C) -> (Y)
Attributes:
transB = 1
Inputs:
A: shape=(5,), dtype=float32
[2., 2., 2., 2., 2.]
B: shape=(7, 5), dtype=float32
[[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.]]
C: shape=(7,), dtype=float32
[1., 1., 1., 1., 1., 1., 1.]
Outputs:
Y: shape=(7,), dtype=float32
[31., 31., 31., 31., 31., 31., 31.]
test_cc_gemm_linear_vector_no_bias
Node:
Gemm(A, B) -> (Y)
Attributes:
transB = 1
Inputs:
A: shape=(5,), dtype=float32
[2., 2., 2., 2., 2.]
B: shape=(7, 5), dtype=float32
[[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3.]]
Outputs:
Y: shape=(7,), dtype=float32
[30., 30., 30., 30., 30., 30., 30.]
Differences with previous version (13)#
SchemaDiff: Gemm (domain 'ai.onnx')
old version: 13
new version: 29
breaking: no
Attributes:
added ‘transA’: type=INT; required=False; default=0
added ‘transB’: type=INT; required=False; default=0
added ‘alpha’: type=FLOAT; required=False; default=1
added ‘beta’: type=FLOAT; required=False; default=1
Documentation:
line similarity: 0.00 (+6/-11 lines)
--- Gemm v13
+++ Gemm v29
@@ -1,11 +1,6 @@
-General Matrix multiplication:
-https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3
-
-A' = transpose(A) if transA else A
-
-B' = transpose(B) if transB else B
-
-Compute Y = alpha * A' * B' + beta * C, where input tensor A has shape (M, K) or (K, M),
-input tensor B has shape (K, N) or (N, K), input tensor C is broadcastable to shape (M, N),
-and output tensor Y has shape (M, N). A will be transposed before doing the
-computation if attribute transA is non-zero, same for B and transB.
+Generalized matrix multiplication: Y = alpha * A' * B' + beta * C.
+A has rank at least 1 and B has rank 2. transA swaps A's final two axes
+and has no effect on vectors; transB transposes B. For A' of shape (..., K)
+and B' of shape (K, N), Y has shape (..., N).
+C is optional and must be unidirectionally broadcastable to Y.
+If C is absent or beta is zero, its contribution and data read are skipped.