ttnn.multiply_
- ttnn.multiply_(input_tensor_a: ttnn.Tensor, input_tensor_b: ttnn.Tensor, *, fast_and_approximate_mode: bool = False, sub_core_grids: ttnn.CoreRangeSet = None, sub_device_id: ttnn.SubDeviceId = ``None``) ttnn.Tensor
-
Performs in-place multiplication operation on
input_aandinput_band returns the tensor with the same layout asinput_tensor\[\verb|multiply|(\mathrm{{input\_tensor\_a,input\_tensor\_b}})\]- Parameters:
-
input_tensor_a (ttnn.Tensor)the input tensor.
input_tensor_b (ttnn.Tensor)the input tensor.
- Keyword Arguments:
-
fast_and_approximate_mode (bool, optional)Use the fast and approximate mode. Defaults to False.
sub_core_grids (ttnn.CoreRangeSet, optional)sub core grids for the operation. Defaults to None.
sub_device_id (ttnn.SubDeviceId, optional)sub device ID for core resolution. Mutually exclusive with sub_core_grids. Defaults to
None.
- Returns:
-
ttnn.Tensorthe 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, UINT16 (range: [0, 65535]), UINT32 (range: [0, 4294967295])
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.
When one operand is float-family and the other is INT32 or UINT32 (range: [0, 4294967295]), the integer operand is automatically typecast to the float operand’s dtype before device execution; output dtype follows the promoted float dtype.
When
fast_and_approximate_modeis True for bfloat16 datatype, the operation uses FPU implementation for better performance. Whenfast_and_approximate_modeis False for bfloat16 datatype, the operation uses SFPU with the result rounded to nearest even (RNE). The operation is not supported for INT32 inputs since the outputs are returned as FLOAT32.Example
# Create two tensors for inplace multiplication tensor1 = ttnn.from_torch( torch.tensor([[2, 2], [2, 2]], dtype=torch.bfloat16), layout=ttnn.TILE_LAYOUT, device=device ) tensor2 = ttnn.from_torch( torch.tensor([[1, 1], [1, 1]], dtype=torch.bfloat16), layout=ttnn.TILE_LAYOUT, device=device ) # Perform inplace operation ttnn.multiply_(tensor1, tensor2) logger.info("Inplace multiplication completed")