Sub#

  • Domain: ai.onnx

  • Since version: 14

Performs element-wise binary subtraction (with Numpy-style broadcasting support).

This operator supports multidirectional (i.e., Numpy-style) broadcasting; for more details please check the broadcasting behavior in ONNX.

Inputs

  • A (T): First operand.

  • B (T): Second operand.

Outputs

  • C (T): Result, has same element type as two inputs

Type Constraints

  • T: Constrain input and output types to all numeric tensors. Allowed types: tensor(bfloat16), tensor(double), tensor(float), tensor(float16), tensor(int16), tensor(int32), tensor(int64), tensor(int8), tensor(uint16), tensor(uint32), tensor(uint64), tensor(uint8).

Examples#

test_cc_sub

Inputs:
  x: shape=(2, 3), dtype=float32
    [[1., 2., 3.],
     [4., 5., 6.]]
  y: shape=(2, 3), dtype=float32
    [[10., 20., 30.],
     [40., 50., 60.]]

Outputs:
  z: shape=(2, 3), dtype=float32
    [[ -9., -18., -27.],
     [-36., -45., -54.]]

test_cc_sub_bcast

Inputs:
  x: shape=(2, 2), dtype=float32
    [[1., 2.],
     [3., 4.]]
  y: shape=(), dtype=float32
    0.5

Outputs:
  z: shape=(2, 2), dtype=float32
    [[0.5, 1.5],
     [2.5, 3.5]]

test_sub

Inputs:
  x: shape=(3, 4, 5), dtype=float32
    [[[ 1.4243481 , -0.61890423, -0.5907667 ,  1.4329695 ,  0.5837956 ],
      [-1.3854368 ,  1.2791865 ,  0.32735094,  0.6038593 ,  0.24222691],
      [-0.89236253, -1.1303798 , -0.09180629, -0.12591071, -1.2615176 ],
      [-0.55638343, -0.747256  , -0.59118223, -0.9279915 , -0.73401135]],

     [[ 1.1054384 , -0.69560546, -2.1534553 ,  0.11396561, -0.8268097 ],
      [ 1.2137318 , -0.22223468,  0.32949635, -0.21049212,  1.3518724 ],
      [ 0.01262847,  0.6841954 , -1.2623075 ,  0.20052178, -1.1255072 ],
      [-0.6395738 ,  1.5355366 , -1.1466801 , -0.42676184, -0.74427605]],

     [[-1.5989435 ,  2.3646672 , -1.0641551 ,  0.90345967, -0.24993172],
      [-1.784248  , -0.47239977,  0.09873669, -0.36464727,  0.6651279 ],
      [-1.01641   , -0.39525023,  0.45574856, -0.3439513 , -0.5487247 ],
      [ 0.06280329, -0.14411083, -1.1603392 ,  0.49200374, -0.16951095]]]
  y: shape=(3, 4, 5), dtype=float32
    [[[ 0.5802847 , -1.4953256 ,  0.42665341,  0.16854794, -0.7854995 ],
      [-0.3418555 , -0.1149836 ,  0.95487213,  1.3025413 ,  0.5168173 ],
      [-0.6690302 ,  0.806894  , -0.2736855 , -0.47230572,  1.3611885 ],
      [-1.6060044 , -1.2981083 ,  0.58045334, -0.13972436, -0.35279942]],

     [[ 0.63143295, -0.770354  ,  0.3310645 ,  0.32500747, -0.91997206],
      [-0.51626676, -0.31889752, -1.4302723 ,  0.6759698 , -0.7796309 ],
      [-0.5247361 , -0.12912966, -0.40980053, -0.36663505,  0.8133978 ],
      [-0.12241841,  1.2133354 ,  1.0200249 ,  1.4152457 ,  0.62612057]],

     [[ 2.0329802 , -0.90274274,  1.2706685 , -0.04628804, -1.3271873 ],
      [ 0.2806637 , -0.9965783 , -0.57684577,  1.7659245 , -0.6384131 ],
      [ 0.14784828,  0.08228748,  0.4838705 ,  0.6750839 , -1.2548709 ],
      [ 0.79701257, -0.5844758 ,  0.3095379 ,  1.2291852 ,  2.0687242 ]]]

Outputs:
  z: shape=(3, 4, 5), dtype=float32
    [[[ 0.8440634 ,  0.87642133, -1.0174202 ,  1.2644215 ,  1.3692951 ],
      [-1.0435812 ,  1.39417   , -0.62752116, -0.69868195, -0.27459037],
      [-0.22333235, -1.9372737 ,  0.1818792 ,  0.34639502, -2.6227062 ],
      [ 1.0496209 ,  0.55085236, -1.1716356 , -0.78826714, -0.38121194]],

     [[ 0.4740054 ,  0.07474852, -2.4845197 , -0.21104187,  0.09316236],
      [ 1.7299986 ,  0.09666283,  1.7597687 , -0.8864619 ,  2.1315033 ],
      [ 0.5373646 ,  0.81332505, -0.852507  ,  0.56715685, -1.938905  ],
      [-0.5171554 ,  0.32220125, -2.1667051 , -1.8420075 , -1.3703966 ]],

     [[-3.6319237 ,  3.2674098 , -2.3348236 ,  0.9497477 ,  1.0772556 ],
      [-2.0649116 ,  0.5241785 ,  0.67558247, -2.1305718 ,  1.303541  ],
      [-1.1642582 , -0.47753772, -0.02812195, -1.0190352 ,  0.7061462 ],
      [-0.7342093 ,  0.440365  , -1.4698771 , -0.7371815 , -2.238235  ]]]

test_sub_bcast

Inputs:
  x: shape=(3, 4, 5), dtype=float32
    [[[ 0.16602525,  0.11929144,  0.5103543 , -0.07833039, -0.2179262 ],
      [-0.35897505, -0.9920161 ,  1.1369699 ,  0.15822072, -0.66695   ],
      [ 0.1640578 ,  0.5148549 ,  0.692694  ,  1.2886952 , -2.3116457 ],
      [-0.3991889 , -1.763249  , -0.46325874,  1.1941477 ,  0.1423654 ]],

     [[-0.7589299 ,  1.5638951 , -1.3538998 , -0.78615636, -0.7008719 ],
      [-0.49739143,  1.2461402 , -0.824719  , -1.2749853 , -0.44074798],
      [ 1.8726983 , -1.2862704 ,  0.38438547,  1.782237  , -0.76287836],
      [-0.53627247, -2.1623473 ,  0.3568203 ,  0.67731917, -0.40523142]],

     [[ 2.2794771 , -1.7118682 ,  0.17869362, -0.12453986,  0.51542616],
      [-0.34672984, -0.04749961, -1.1327435 ,  0.7626039 , -0.18739803],
      [-0.31375307, -1.0803736 ,  0.05428931,  0.53482765, -0.33528003],
      [-1.5306095 , -0.61205   , -1.8999155 ,  1.1543474 , -0.30809247]]]
  y: shape=(5,), dtype=float32
    [ 1.2357849 , -0.33475664,  1.0486782 ,  0.70084286,  0.7489813 ]

Outputs:
  z: shape=(3, 4, 5), dtype=float32
    [[[-1.0697596 ,  0.4540481 , -0.5383239 , -0.77917325, -0.9669075 ],
      [-1.59476   , -0.65725946,  0.08829176, -0.54262215, -1.4159312 ],
      [-1.071727  ,  0.8496115 , -0.35598415,  0.58785236, -3.060627  ],
      [-1.6349738 , -1.4284924 , -1.5119369 ,  0.49330485, -0.6066159 ]],

     [[-1.9947147 ,  1.8986517 , -2.4025779 , -1.4869993 , -1.4498532 ],
      [-1.7331764 ,  1.5808969 , -1.8733971 , -1.9758282 , -1.1897292 ],
      [ 0.6369134 , -0.95151377, -0.6642927 ,  1.0813942 , -1.5118597 ],
      [-1.7720573 , -1.8275907 , -0.6918579 , -0.02352369, -1.1542127 ]],

     [[ 1.0436922 , -1.3771116 , -0.8699845 , -0.8253827 , -0.23355514],
      [-1.5825148 ,  0.28725702, -2.1814218 ,  0.06176102, -0.9363793 ],
      [-1.5495379 , -0.74561703, -0.9943888 , -0.16601521, -1.0842613 ],
      [-2.7663944 , -0.27729335, -2.9485936 ,  0.45350456, -1.0570738 ]]]

test_sub_example

Inputs:
  x: shape=(3,), dtype=float32
    [1., 2., 3.]
  y: shape=(3,), dtype=float32
    [3., 2., 1.]

Outputs:
  z: shape=(3,), dtype=float32
    [-2.,  0.,  2.]

Differences with previous version (13)#

SchemaDiff: Sub (domain 'ai.onnx')

  • old version: 13

  • new version: 14

  • breaking: no

Type constraints:

  • changed ‘T’: added types: [‘tensor(int16)’, ‘tensor(int8)’, ‘tensor(uint16)’, ‘tensor(uint8)’]

Version History#