Mod#

  • Domain: ai.onnx

  • Since version: 28

Performs an element-wise binary modulo operation. The fmod attribute determines how the quotient is rounded. Its value must be 0 (default) or 1.

If fmod is 0, the output is calculated as A - floor(A / B) * B. The result has the same sign as B. For floating-point inputs, the following special cases apply:

  • If x is ±0 and y is nonzero, ±0 with the sign of y is returned.

  • If x is ±∞ and y is not NaN, NaN is returned.

  • If y is ±0 and x is not NaN, NaN is returned.

  • If y is ±∞ and x is finite and nonzero, x is returned when x and y have the same sign; otherwise, y is returned.

  • If either argument is NaN, NaN is returned.

If fmod is 1, the output is calculated as A - trunc(A / B) * B. The result has the same sign as A, except that either signed zero may be returned when A is -0 and B is positive. For floating-point inputs, the following special cases apply:

  • If x is -0 and y is greater than zero, either +0 or -0 may be returned.

  • If x is ±∞ and y is not NaN, NaN is returned.

  • If y is ±0 and x is not NaN, NaN should be returned.

  • If y is ±∞ and x is finite, x is returned.

  • If either argument is NaN, NaN is returned.

This operator supports multidirectional (i.e., NumPy-style) broadcasting; for more details please check the doc.

Inputs

  • A (T): Dividend tensor

  • B (T): Divisor tensor

Outputs

  • C (T): Remainder tensor

Attributes

  • fmod (int): Whether the operator should use floor (0) or truncation (1) to calculate the quotient.

Type Constraints

  • T: Constrain input and output types to 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_mod_float16_mixed_sign_fmod_0

Node:
  Mod(x, y) -> (z)
  Attributes:
    fmod = 0
Inputs:
  x: shape=(6,), dtype=float16
    [-4.3,  7.2,  5. ,  4.3, -7.2,  8. ]
  y: shape=(6,), dtype=float16
    [ 2.1, -3.4,  8. , -2.1,  3.4,  5. ]

Outputs:
  z: shape=(6,), dtype=float16
    [ 1.998, -3.002,  5.   , -1.998,  3.002,  3.   ]

test_mod_float32_mixed_sign_fmod_0

Node:
  Mod(x, y) -> (z)
  Attributes:
    fmod = 0
Inputs:
  x: shape=(6,), dtype=float32
    [-4.3,  7.2,  5. ,  4.3, -7.2,  8. ]
  y: shape=(6,), dtype=float32
    [ 2.1, -3.4,  8. , -2.1,  3.4,  5. ]

Outputs:
  z: shape=(6,), dtype=float32
    [ 1.9999995, -3.0000005,  5.       , -1.9999995,  3.0000005,  3.       ]

test_mod_float64_mixed_sign_fmod_0

Node:
  Mod(x, y) -> (z)
  Attributes:
    fmod = 0
Inputs:
  x: shape=(6,), dtype=float64
    [-4.3,  7.2,  5. ,  4.3, -7.2,  8. ]
  y: shape=(6,), dtype=float64
    [ 2.1, -3.4,  8. , -2.1,  3.4,  5. ]

Outputs:
  z: shape=(6,), dtype=float64
    [ 2., -3.,  5., -2.,  3.,  3.]

test_mod_float_edge_cases_fmod_0_float16

Node:
  Mod(x, y) -> (z)
  Attributes:
    fmod = 0
Inputs:
  x: shape=(14,), dtype=float16
    [  0.,  -0.,   0.,  -0.,  -3.,   3.,  -1.,   1.,  inf, -inf,   1.,   1.,  nan,
       1.]
  y: shape=(14,), dtype=float16
    [ -2.,   2.,   2.,  -2.,  inf,  inf, -inf, -inf,   2.,   2.,   0.,  -0.,   2.,
      nan]

Outputs:
  z: shape=(14,), dtype=float16
    [ -0.,   0.,   0.,  -0.,  inf,   3.,  -1., -inf,  nan,  nan,  nan,  nan,  nan,
      nan]

test_mod_float_edge_cases_fmod_0_float32

Node:
  Mod(x, y) -> (z)
  Attributes:
    fmod = 0
Inputs:
  x: shape=(14,), dtype=float32
    [  0.,  -0.,   0.,  -0.,  -3.,   3.,  -1.,   1.,  inf, -inf,   1.,   1.,  nan,
       1.]
  y: shape=(14,), dtype=float32
    [ -2.,   2.,   2.,  -2.,  inf,  inf, -inf, -inf,   2.,   2.,   0.,  -0.,   2.,
      nan]

Outputs:
  z: shape=(14,), dtype=float32
    [ -0.,   0.,   0.,  -0.,  inf,   3.,  -1., -inf,  nan,  nan,  nan,  nan,  nan,
      nan]

test_mod_float_edge_cases_fmod_0_float64

Node:
  Mod(x, y) -> (z)
  Attributes:
    fmod = 0
Inputs:
  x: shape=(14,), dtype=float64
    [  0.,  -0.,   0.,  -0.,  -3.,   3.,  -1.,   1.,  inf, -inf,   1.,   1.,  nan,
       1.]
  y: shape=(14,), dtype=float64
    [ -2.,   2.,   2.,  -2.,  inf,  inf, -inf, -inf,   2.,   2.,   0.,  -0.,   2.,
      nan]

Outputs:
  z: shape=(14,), dtype=float64
    [ -0.,   0.,   0.,  -0.,  inf,   3.,  -1., -inf,  nan,  nan,  nan,  nan,  nan,
      nan]

Differences with previous version (13)#

SchemaDiff: Mod (domain 'ai.onnx')

  • old version: 13

  • new version: 28

  • breaking: no

Attributes:

  • added ‘fmod’: type=INT; required=False; default=0

Documentation:

  • line similarity: 0.07 (+25/-0 lines)

--- Mod v13
+++ Mod v28
@@ -1 +1,26 @@
+
 Performs an element-wise binary modulo operation.
+The `fmod` attribute determines how the quotient is rounded. Its value must be
+`0` (default) or `1`.
+
+If `fmod` is `0`, the output is calculated as `A - floor(A / B) * B`.
+The result has the same sign as `B`.
+For floating-point inputs, the following special cases apply:
+- If `x` is `±0` and `y` is nonzero, `±0` with the sign of `y` is returned.
+- If `x` is `±∞` and `y` is not `NaN`, `NaN` is returned.
+- If `y` is `±0` and `x` is not `NaN`, `NaN` is returned.
+- If `y` is `±∞` and `x` is finite and nonzero, `x` is returned when `x` and
+  `y` have the same sign; otherwise, `y` is returned.
+- If either argument is `NaN`, `NaN` is returned.
+
+If `fmod` is `1`, the output is calculated as `A - trunc(A / B) * B`.
+The result has the same sign as `A`, except that either signed zero may be
+returned when `A` is `-0` and `B` is positive. For floating-point inputs,
+the following special cases apply:
+- If `x` is `-0` and `y` is greater than zero, either `+0` or `-0` may be returned.
+- If `x` is `±∞` and `y` is not `NaN`, `NaN` is returned.
+- If `y` is `±0` and `x` is not `NaN`, `NaN` should be returned.
+- If `y` is `±∞` and `x` is finite, `x` is returned.
+- If either argument is `NaN`, `NaN` is returned.
+
+This operator supports **multidirectional (i.e., NumPy-style) broadcasting**; for more details please check [the doc](Broadcasting.md).

Version History#