ttnn.divide
- ttnn.divide() None
-
Divides
input_tensor_abyinput_tensor_band returns a tensor with the same layout asinput_tensor_a, or as the tensor operand wheninput_tensor_ais a Number\[\begin{split}\mathrm{output}_i = \begin{cases} \mathrm{\left(\frac{\mathrm{input\_tensor\_a}_i}{\mathrm{input\_tensor\_b}_i}\right)}, & \text{if } \mathrm{round\_mode} = \mathrm{None} \\ \mathrm{\text{floor}\left(\frac{\mathrm{input\_tensor\_a}_i}{\mathrm{input\_tensor\_b}_i}\right)}, & \text{if } \mathrm{round\_mode} = \mathrm{floor} \\ \mathrm{\text{trunc}\left(\frac{\mathrm{input\_tensor\_a}_i}{\mathrm{input\_tensor\_b}_i}\right)}, & \text{if } \mathrm{round\_mode} = \mathrm{trunc} \end{cases}\end{split}\]- Args:
-
input_tensor_a (ttnn.Tensor or Number): the input tensor, or the numerator when a Number. input_tensor_b (ttnn.Tensor or Number): the input tensor. At least one of the two operands must be a tensor; there is no scalar-scalar overload.
- Keyword args:
-
memory_config (ttnn.MemoryConfig, optional): memory configuration for the operation. Defaults to None. fast_and_approximate_mode (bool, optional): true if input_tensor_b is non-zero for fast approximation, else false for accurate division (Only if the input tensor is not ComplexTensor). Defaults to false. rounding_mode (string, optional): can be None, floor and trunc (only if the input tensor is not ComplexTensor). Defaults to None. activations (List[str], optional): list of activation functions to apply to the output tensor. Defaults to None. input_tensor_a_activations (List[str], optional): list of activation functions to apply to input_a before the operation. A Number in that position takes this list. Defaults to None. input_tensor_b_activations (List[str], optional): list of activation functions to apply to input_b before the operation. Defaults to None. dtype (ttnn.DataType, optional): dtype of the output tensor. Defaults to the dtype of the tensor operand, or ttnn.float32 when an INT32 input is promoted by a floating-point scalar. Integer division with rounding_mode=None only accepts None or ttnn.float32, and always yields ttnn.float32. output_tensor (ttnn.Tensor, optional): preallocated output tensor. Defaults to None.
- Returns:
-
ttnn.Tensor: the output tensor.
Binary elementwise operations, C=op(A,B), support input tensors A and B in tile and row major layouts (unless dtype requires specific layout, e.g. BFLOAT8_B or BFLOAT4_B), in interleaved or sharded format (height, width or block sharded), in DRAM or L1. A and B are independent, and can have different tensor specs, with restrictions as in notes below.
Broadcast of A and B operands is supported up to dimension 5 (DNCHW). Any dimensions of size 1 in either A or B will be expanded to match the other input, and data will be duplicated along that dimension. For example, if the shape of A is [2,1,1,32] and B is [1,16,8,1], the output shape will be [2,16,8,32]. The size of dimensions higher than 5 must match between A and B.
The output C supports the same layouts and formats as A and B, under the same dtype restriction. For operations that return a new tensor, the layout and memory configuration of C are independent of A and B. The memory configuration can be set with memory_config, and the full tensor spec with output_tensor where the operation takes one; if neither is given, the operation will attempt a best decision at an appropriate tensor spec. The dimensions of C, or of output_tensor if given, must match the broadcast-matched size of A and B. By default, C takes the dtype of A unless an operation-specific rule applies: comparison ops return a boolean mask, and a mixed float pair follows A rather than the wider dtype. There is no boolean dtype, so the mask is carried as 1 for true and 0 for false in the output dtype. Where supported, the output dtype can be overridden by the dtype argument or output_tensor. In-place operations write into A, so C is A and keeps its layout, memory configuration and dtype.
Performance considerations: Elementwise operations operate natively in tile format, tiled tensors are preferred as an input, and row-major tensors are tilized and untilized during the operation. L1 sharded layout is preferred, with no broadcast and matching tensor specs for A, B and C.
- Note:
-
Supported dtypes and layouts:
Dtypes
Layouts
BFLOAT16, BFLOAT8_B, BFLOAT4_B, FLOAT32, INT32
TILE, ROW_MAJOR
If the input tensor is ROW_MAJOR layout, it will be internally converted to TILE layout.
Operands may mix float-family dtypes (BFLOAT16, BFLOAT8_B, BFLOAT4_B, FLOAT32); all other dtype pairs must match.
UINT32 (range: [0, 4294967295]) is not a natively supported operand dtype. When one operand is float-family and the other is INT32 or UINT32, the integer operand is automatically typecast to the float operand’s dtype before device execution; output dtype follows the promoted float dtype.
With INT32 tensor operands or an INT32 tensor and an integer scalar, rounding_mode None produces a FLOAT32 output and output datatype conversion is not supported, while floor and trunc produce an INT32 output. For these INT32 operands with rounding_mode floor or trunc, division by zero and INT32_MIN / -1 are undefined, regardless of
fast_and_approximate_mode. For floating-point division (including promoted INT32 inputs), whenfast_and_approximate_modeis True, operation assumes thatinput_tensor_bis not zero for fast approximation. Whenfast_and_approximate_modeis False (default), floating-point division properly handles division by zero (accurate mode).An INT32 tensor with a floating-point scalar is promoted to FLOAT32 before computation, including integral-valued floats such as 2.0. The default output dtype is FLOAT32. Use an integer scalar (for example, 2 instead of 2.0) to retain integer-scalar semantics.
Example
# Create two tensors for division tensor1 = ttnn.from_torch( torch.tensor([[1, 2], [3, 4]], dtype=torch.bfloat16), layout=ttnn.TILE_LAYOUT, device=device ) tensor2 = ttnn.from_torch( torch.tensor([[1, 2], [3, 4]], dtype=torch.bfloat16), layout=ttnn.TILE_LAYOUT, device=device ) # Perform element-wise division with fast approximation mode output = ttnn.divide(tensor1, tensor2, fast_and_approximate_mode=True) logger.info(f"Division (tensor-tensor) result: {output}") # Create tensor and scalar for division tensor = ttnn.from_torch( torch.tensor([[1, 2], [3, 4]], dtype=torch.bfloat16), layout=ttnn.TILE_LAYOUT, device=device ) scalar = 3 # Perform division by a scalar value output = ttnn.divide(tensor, scalar, fast_and_approximate_mode=False) logger.info(f"Division (tensor-scalar) result: {output}")