MatMul#

  • Domain: ai.onnx

  • Since version: 13

Matrix product that behaves like numpy.matmul.

Inputs

  • A (T): N-dimensional matrix A

  • B (T): N-dimensional matrix B

Outputs

  • Y (T): Matrix multiply results from A * 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_matmul_1d_1d

Node:
  MatMul(A, B) -> (Y)
Inputs:
  A: shape=(3,), dtype=float32
    [-0.77932763, -0.11304837, -0.02405588]
  B: shape=(3,), dtype=float32
    [ 0.85281533, -2.0860636 ,  0.6411558 ]

Outputs:
  Y: shape=(), dtype=float32
    -0.44422

test_cc_matmul_1d_3d

Node:
  MatMul(A, B) -> (Y)
Inputs:
  A: shape=(4,), dtype=float32
    [ 1.0815    , -0.10584228,  0.22107787, -0.69061667]
  B: shape=(2, 4, 3), dtype=float32
    [[[-0.12395199,  1.9905189 ,  1.2212905 ],
      [ 0.15173835, -0.15438159, -1.3464159 ],
      [-1.5605167 ,  1.0879043 ,  0.6705888 ],
      [-0.64781123, -0.34378362,  0.88492155]],

     [[-1.316785  , -0.14799444, -2.1535275 ],
      [-1.3787158 , -0.2012813 ,  0.4926724 ],
      [ 0.17217311, -0.7630504 ,  1.0977937 ],
      [-0.03981643, -0.14653187, -0.03874683]]]

Outputs:
  Y: shape=(2, 3), dtype=float32
    [[-0.04772091,  2.6470208 ,  1.0004442 ],
     [-1.212615  , -0.20624812, -2.1117284 ]]

test_cc_matmul_2d

Node:
  MatMul(A, B) -> (Y)
Inputs:
  A: shape=(2, 3), dtype=float32
    [[-0.04804087, -0.30510044, -1.6544707 ],
     [-0.07651462,  0.15347514, -0.8623296 ]]
  B: shape=(3, 4), dtype=float32
    [[ 1.7542539 ,  0.12595381,  0.27244222,  0.15426704],
     [ 1.6261207 , -0.38990438, -1.2887475 ,  0.4211177 ],
     [ 2.2889955 , -0.14774142, -0.1519243 ,  1.7868859 ]]

Outputs:
  Y: shape=(2, 4), dtype=float32
    [[-4.367482  ,  0.35734293,  0.6314634 , -3.0922446 ],
     [-1.8585256 ,  0.05792387, -0.08762771, -1.4880573 ]]

test_cc_matmul_3d

Node:
  MatMul(A, B) -> (Y)
Inputs:
  A: shape=(2, 3, 4), dtype=float32
    [[[ 0.6826989 , -1.0636308 , -0.49599636,  0.84497356],
      [-1.5365669 ,  0.24368583,  0.5968516 ,  1.0224026 ],
      [ 1.7418789 , -2.5481198 ,  0.23338544,  0.47942224]],

     [[ 0.9440713 ,  0.01825   , -0.8448958 , -0.9899119 ],
      [ 0.24289607, -2.3707175 ,  0.54944754, -1.2383972 ],
      [ 0.5260892 , -0.24617857, -1.3518533 , -0.90498984]]]
  B: shape=(2, 4, 3), dtype=float32
    [[[ 0.85030043,  1.2073021 , -1.6735553 ],
      [-0.96159   ,  0.71818644,  0.9220662 ],
      [ 0.67404723, -1.0520265 , -0.2305586 ],
      [ 0.02250137,  0.65962905,  0.51988316]],

     [[-0.03499714,  1.2981033 , -0.7142262 ],
      [ 0.31851605,  0.47932276, -1.0689867 ],
      [ 1.3093405 , -1.2813706 , -0.66246367],
      [ 0.08013254, -0.5265419 , -0.6697319 ]]]

Outputs:
  Y: shape=(2, 3, 3), dtype=float32
    [[[ 1.287964  ,  1.139509  , -1.5696286 ],
      [-1.1155577 , -1.6335857 ,  3.1901448 ],
      [ 4.0994673 ,  0.34366202, -5.069231  ]],

     [[-1.2128073 ,  2.8381047 ,  0.5288989 ],
      [-0.14343432, -0.87301266,  2.8261878 ],
      [-1.9393789 ,  2.7736592 ,  1.3890691 ]]]

test_cc_matmul_4d

Node:
  MatMul(A, B) -> (Y)
Inputs:
  A: shape=(1, 2, 3, 4), dtype=float32
    [[[[-1.049021  , -0.1805252 ,  0.37299186, -1.2512525 ],
       [-0.81569797, -0.51167107,  0.58341527, -0.28096995],
       [ 0.2715433 ,  1.9851228 , -0.78261596, -0.520758  ]],

      [[-0.79341817, -1.150229  , -0.08576501, -1.2088639 ],
       [-0.6402479 , -0.5225338 , -1.3520603 ,  0.02162574],
       [-0.4249209 ,  1.0771086 ,  1.4951609 ,  0.90683544]]]]
  B: shape=(1, 2, 4, 3), dtype=float32
    [[[[-1.1800952 ,  1.5282804 , -0.927722  ],
       [ 0.15227515, -1.7560214 , -0.8705888 ],
       [-1.0943954 , -1.4968148 , -1.7600063 ],
       [ 1.0469959 , -0.0310445 ,  0.53998035]],

      [[ 0.35856768, -0.32643768,  0.9875404 ],
       [ 0.47058868, -0.32706943, -1.4004327 ],
       [-0.90529305,  1.1939697 ,  2.043314  ],
       [-0.5126836 ,  0.5243394 ,  1.5440351 ]]]]

Outputs:
  Y: shape=(1, 2, 3, 3), dtype=float32
    [[[[-0.5078017 , -1.8056474 , -0.20175672],
       [-0.04797488, -1.212652  ,  0.02366318],
       [ 0.2930978 , -1.8833259 , -0.88393235]],

      [[-0.12837178, -0.10104942, -1.2144873 ],
       [ 0.737453  , -1.223074  , -2.6297903 ],
       [-1.4639661 ,  2.0470872 ,  2.5272243 ]]]]

test_cc_matmul_4d_1d

Node:
  MatMul(A, B) -> (Y)
Inputs:
  A: shape=(2, 3, 4, 5), dtype=float32
    [[[[ 0.31121665,  1.0258594 , -3.1395116 , -1.7966223 ,  0.06096684],
       [ 0.70985043, -0.19639817, -2.1368458 , -1.3620541 , -1.0705928 ],
       [ 1.3230867 ,  0.44482955, -1.9483898 , -0.28574938, -1.1584307 ],
       [-0.8643142 , -0.02712508, -1.7259822 , -0.15632714,  0.911871  ]],

      [[-0.79225   , -0.60901046, -1.0763875 , -0.21682826, -0.0795441 ],
       [ 0.02737821, -1.2745905 , -0.7107443 , -0.67446405, -0.4757256 ],
       [-1.7844657 , -0.91062653,  2.4688623 , -1.4964907 , -0.01263986],
       [ 0.4145471 , -2.1071372 ,  0.9477474 , -0.739947  , -0.24369246]],

      [[ 0.82412815,  0.36990932, -0.1963979 ,  0.31643096,  2.1840649 ],
       [ 0.366072  , -1.5286286 , -0.5150546 ,  1.3649099 , -0.9546026 ],
       [ 0.5755407 ,  1.6345203 , -1.6135515 ,  1.4676467 , -0.73048383],
       [-0.44720328, -0.7687108 , -0.48270035, -0.4476123 ,  1.1505837 ]]],


     [[[ 1.1424936 , -0.29250628, -0.08325006, -1.6794462 , -1.4925268 ],
       [-0.30276   ,  1.5639706 ,  1.1856202 ,  0.25384635, -1.5321335 ],
       [ 0.7618358 , -0.7771582 ,  1.0461091 ,  0.5619589 , -0.46400434],
       [-1.2625351 , -0.62398404, -0.6426454 , -0.9245578 , -0.7261043 ]],

      [[-0.78411883, -0.34273082, -0.29962128, -0.30654153, -0.35167047],
       [-0.8070442 , -0.5993325 ,  0.20615938,  0.20545484, -0.7227905 ],
       [ 1.3237495 , -0.13217437,  1.6803715 , -0.5422651 ,  0.9664133 ],
       [-0.5330957 , -0.23887439, -0.57175446,  0.5857247 ,  2.0575044 ]],

      [[ 2.2572389 ,  1.0258298 , -0.8692631 ,  0.00976911,  1.0242034 ],
       [ 0.7892137 , -2.0407262 ,  1.1582901 ,  0.8455595 , -1.5845172 ],
       [-1.5852208 , -0.07479479, -0.55122626,  1.2241193 , -0.135294  ],
       [-0.7148618 ,  0.5093421 ,  0.69073987, -0.13595772, -0.99741244]]]]
  B: shape=(5,), dtype=float32
    [ 0.7913058 , -1.1757126 , -0.6486962 ,  0.87162185, -2.2659822 ]

Outputs:
  Y: shape=(2, 3, 4), dtype=float32
    [[[-0.62738395,  3.4175282 ,  4.1638055 , -1.7349491 ],
      [ 0.778611  ,  2.4713817 , -3.2186966 ,  2.0978696 ],
      [-4.328611  ,  5.773814  ,  2.5149035 , -2.1343167 ]],

     [[ 3.2201662 ,  0.84558177,  2.3791943 ,  0.99093074],
      [ 0.5065291 ,  1.7491984 , -2.5496862 , -3.9218369 ],
      [-1.1683389 ,  6.5999346 ,  0.5646642 ,  0.52902055]]]

test_cc_matmul_batch_broadcast

Node:
  MatMul(A, B) -> (Y)
Inputs:
  A: shape=(2, 2, 3), dtype=float32
    [[[-0.24912022,  0.37703994, -0.69417447],
      [-0.7060292 ,  0.20510858, -0.6286968 ]],

     [[ 1.6711484 ,  0.33107895,  0.7037558 ],
      [-1.3299477 ,  1.2570701 ,  1.9146868 ]]]
  B: shape=(1, 3, 4), dtype=float32
    [[[-1.313993  , -0.9773617 , -0.6343613 , -0.979214  ],
      [-0.58685726,  1.6898878 ,  0.08713032,  1.1718034 ],
      [ 0.79937506,  0.02316316,  0.29876754,  0.3574321 ]]]

Outputs:
  Y: shape=(2, 2, 4), dtype=float32
    [[[-0.44883218,  0.86455643, -0.01651295,  0.4376384 ],
      [ 0.30478334,  1.0220938 ,  0.27791458,  0.70698416]],

     [[-1.8276086 , -1.057529  , -0.8210055 , -0.9969075 ],
      [ 2.540374  ,  3.4684975 ,  1.5252426 ,  3.459713  ]]]

test_cc_matmul_bcast

Node:
  MatMul(A, B) -> (Y)
Inputs:
  A: shape=(2, 3, 4), dtype=float32
    [[[-0.03015089, -0.9815131 ,  1.0847169 , -0.78242064],
      [-1.7737532 ,  1.6795189 ,  1.4342878 , -1.7393011 ],
      [-0.55563307, -0.7712569 , -0.30710608,  0.86632395]],

     [[ 0.67197675, -0.9347355 ,  0.4981012 ,  0.37234926],
      [-1.0757359 ,  2.4978497 , -0.56668866,  0.15581536],
      [ 0.8665466 ,  1.1699449 , -0.3296375 , -0.18988499]]]
  B: shape=(4, 5), dtype=float32
    [[-0.5667766 , -0.12876017, -1.0483112 ,  0.88921374, -1.775621  ],
     [ 0.9362056 , -0.7740069 ,  1.0434127 ,  1.1432025 , -0.31927752],
     [-0.49463278,  0.5030952 ,  0.32739678, -1.0140055 ,  0.9192106 ],
     [ 1.1337914 ,  0.6644079 ,  0.2099517 ,  0.45998812, -0.10830159]]

Outputs:
  Y: shape=(2, 3, 5), dtype=float32
    [[[-2.3254476 ,  0.7894496 , -0.8016534 , -2.6086922 ,  1.4487324 ],
      [-0.10375357, -1.5055926 ,  3.7162888 , -1.911649  ,  4.1200624 ],
      [ 0.7270001 ,  1.0895904 , -0.1409222 , -0.66587335,  0.8567193 ]],

     [[-1.0801761 ,  1.1349521 , -1.4385036 , -0.80486214, -0.47720212],
      [ 3.4051678 , -1.9764144 ,  3.5811756 ,  2.5452876 ,  0.57481086],
      [ 0.5519302 , -1.3091223 ,  0.1645359 ,  2.3549385 , -2.194637  ]]]

test_cc_matmul_vector_matrix

Node:
  MatMul(A, B) -> (Y)
Inputs:
  A: shape=(3,), dtype=float32
    [ 0.8741822,  0.553631 , -1.1274734]
  B: shape=(3, 2), dtype=float32
    [[ 1.4420623 , -0.9769679 ],
     [ 0.03334304, -0.0760201 ],
     [-0.9492963 , -1.0993737 ]]

Outputs:
  Y: shape=(2,), dtype=float32
    [2.3493915 , 0.34337956]

Differences with previous version (9)#

SchemaDiff: MatMul (domain 'ai.onnx')

  • old version: 9

  • new version: 13

  • breaking: no

Type constraints:

  • changed ‘T’: added types: [‘tensor(bfloat16)’]

Version History#