MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / add

Function add

imperative/python/megengine/functional/elemwise.py:76–140  ·  view source on GitHub ↗

r"""Element-wise addition. Calculates the sum for each element :math:`x_i` of the input tensor :math:`x` with the respective element :math:`y_i` of the input tensor :math:`y`. Args: x: first input tensor. Should have a numeric data type. y: second input tensor. Must be

(x: Tensor, y: Tensor)

Source from the content-addressed store, hash-verified

74
75
76def add(x: Tensor, y: Tensor) -> Tensor:
77 r"""Element-wise addition.
78
79 Calculates the sum for each element :math:`x_i` of the input tensor :math:`x` with the respective element :math:`y_i` of the input tensor :math:`y`.
80
81 Args:
82 x: first input tensor. Should have a numeric data type.
83 y: second input tensor. Must be compatible with :math:`x` (see :ref:`broadcasting-rule` ). Should have a numeric data type.
84
85 Returns:
86 A tensor containing the element-wise sums.
87 The returned tensor must have a data type determined by :ref:`dtype-promotion`.
88
89 .. admonition:: Special cases
90
91 For floating-point operands,
92
93 * If either :math:`x` or :math:`y` is ``NaN``, the result is ``NaN``.
94 * If :math:`x` is ``+infinity`` and :math:`y` is ``-infinity``, the result is ``NaN``.
95 * If :math:`x` is ``-infinity`` and :math:`y` is ``+infinity``, the result is ``NaN``.
96 * If :math:`x` is ``+infinity`` and :math:`y` is ``+infinity``, the result is ``+infinity``.
97 * If :math:`x` is ``-infinity`` and :math:`y` is ``-infinity``, the result is ``-infinity``.
98 * If :math:`x` is ``+infinity`` and :math:`y` is a finite number, the result is ``+infinity``.
99 * If :math:`x` is ``-infinity`` and :math:`y` is a finite number, the result is ``-infinity``.
100 * If :math:`x` is a finite number and :math:`y` is ``+infinity``, the result is ``+infinity``.
101 * If :math:`x` is a finite number and :math:`y` is ``-infinity``, the result is ``-infinity``.
102 * If :math:`x` is ``-0`` and :math:`y` is ``-0``, the result is ``-0``.
103 * If :math:`x` is ``-0`` and :math:`y` is ``+0``, the result is ``+0``.
104 * If :math:`x` is ``+0`` and :math:`y` is ``-0``, the result is ``+0``.
105 * If :math:`x` is ``+0`` and :math:`y` is ``+0``, the result is ``+0``.
106 * If :math:`x` is either ``+0`` or ``-0`` and :math:`y` is a nonzero finite number, the result is :math:`y`.
107 * If :math:`x` is a nonzero finite number and :math:`y` is either ``+0`` or ``-0``, the result is :math:`x`.
108 * If :math:`x` is a nonzero finite number and :math:`y` is :math:`-x`, the result is ``+0``.
109 * In the remaining cases, when neither ``infinity``, ``+0``, ``-0``, nor a ``NaN`` is involved,
110 and the operands have the same mathematical sign or have different magnitudes,
111 the sum must be computed and rounded to the nearest representable value according to
112 IEEE 754-2019 and a supported round mode. If the magnitude is too large to represent,
113 the operation overflows and the result is an infinity of appropriate mathematical sign.
114
115 .. note::
116
117 * Floating-point addition is a commutative operation, but not always associative.
118 * The ``+`` operator can be used as a shorthand for ``add`` on tensors.
119
120 Examples:
121
122 >>> F.add(1.0, 4.0)
123 Tensor(5.0, device=xpux:0)
124
125 Element-wise addition:
126
127 >>> x = Tensor([[1, 2, 3], [4, 5, 6]])
128 >>> y = Tensor([[1, 1, 1], [2, 2, 2]])
129 >>> F.add(x, y)
130 Tensor([[2 3 4]
131 [6 7 8]], dtype=int32, device=xpux:0)
132
133 Broadcasting:

Callers 15

forwardMethod · 0.85
__add__Method · 0.85
__radd__Method · 0.85
dynamic_registryFunction · 0.85
TESTFunction · 0.85
TESTFunction · 0.85
run_graphFunction · 0.85
test_aplusbFunction · 0.85
TESTFunction · 0.85
operator+Method · 0.85

Calls 1

_elwiseFunction · 0.50

Tested by 8

TESTFunction · 0.68
TESTFunction · 0.68
run_graphFunction · 0.68
test_aplusbFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68
TESTFunction · 0.68