ttnn.linear

ttnn.linear(input_tensor_a: ttnn.Tensor, input_tensor_b: ttnn.Tensor, *, bias: ttnn.Tensor = None, transpose_a: bool | None, transpose_b: bool = False, memory_config: ttnn.MemoryConfig = None, dtype: ttnn.DataType = None, program_config: MatmulProgramConfig = None, activation: str or ttnn.UnaryWithParam = None, compute_kernel_config: ttnn.DeviceComputeKernelConfig = None, core_grid: ttnn.CoreGrid = None, output_tile: List of [int] = None, optional_output_tensor: ttnn.Tensor = None, global_cb: ttnn.GlobalCircularBuffer = None, prefetcher_pipes: List[ttnn.experimental.PrefetcherPipe] = an empty list (none, in0_column_offset: int = None) → ttnn.Tensor

Returns the linear transformation of the inputs.

The limitations and behaviours are the same as for matmul.

Note

The tensors support the following data types and layouts:

input_tensor_a

dtype

layout

BFLOAT8_B, BFLOAT4_B, BFLOAT16, FLOAT32

TILE

input_tensor_b

dtype

layout

BFLOAT8_B, BFLOAT4_B, BFLOAT16, FLOAT32

TILE

bias

dtype

layout

BFLOAT8_B, BFLOAT4_B, BFLOAT16, FLOAT32

TILE

Parameters:
  • input_tensor_a (ttnn.Tensor)the first tensor to be multiplied. Needs to be on the device.

  • input_tensor_b (ttnn.Tensor)the second tensor to be multiplied. Needs to be on the device.

Keyword Arguments:
  • bias (ttnn.Tensor, optional)the bias tensor to be added. If specified, needs to be on the device. Defaults to None. Most program configs take a row-vector bias of shape [1, N] (or [1, 1, 1, N]), broadcast across the output rows. The batched MatmulMultiCoreReuseProgramConfig path additionally supports a full-tile bias of shape [M, N], added to the output and broadcast over the batch dimension.

  • transpose_a (bool, optional)Whether to transpose input_tensor_a. An L1-sharded input_tensor_a supports only one tile per batch. Defaults to False.

  • transpose_b (bool, optional)Whether to transpose input_tensor_b. Defaults to False.

  • memory_config (ttnn.MemoryConfig, optional)the memory configuration of the output tensor. Defaults to None, which will result in using ttnn.DRAM_MEMORY_CONFIG.

  • dtype (ttnn.DataType, optional)the data type of the output tensor. Defaults to None.

  • program_config (MatmulProgramConfig, optional)the program configuration for the matmul operation. Defaults to None.

  • activation (str or ttnn.UnaryWithParam, optional)the activation function to be applied. Defaults to None. When using sharded tensors, the fused_activation parameter of the program_config should be used instead.

  • compute_kernel_config (ttnn.DeviceComputeKernelConfig, optional)the compute kernel configuration for the matmul operation. Defaults to None.

  • core_grid (ttnn.CoreGrid, optional)the grid on which to distribute the sharded tensor on (writes to the cores L1s). Defaults to None.

  • output_tile (List of [int], optional)Specifies the output tile configuration. Defaults to None.

  • optional_output_tensor (ttnn.Tensor, optional)User provided on-device output tensor where the result of linear is to be written. Defaults to None.

  • global_cb (ttnn.GlobalCircularBuffer, optional)GlobalCircularBuffer the worker-core prefetcher (ttnn.dram_prefetcher) streams in1 K-blocks through. Defaults to None.

  • prefetcher_pipes (List[ttnn.experimental.PrefetcherPipe], optional)PrefetcherPipes that deliver in1 K-blocks into the workers, as an alternative to global_cb: every pipe of one create_prefetcher_pipes_for_tensor_prefetcher call, or worker-sender pipes whose producer delivers the receiver at row-major position i the K-blocks of output column block i, in K order. Supported for MatmulMultiCoreReuseMultiCast1DProgramConfig with mcast_in0=True or gather_in0=True. For gather_in0 the producer delivers each worker its K-blocks one in0 shard deep, in ring order starting with the K-block of its own in0 shard when stream_in1=True, in K order otherwise, into a pipe ring that holds them all. Pipes re-carved in place of these, at the same addresses with the same geometry, reuse the programs cached for them. Defaults to an empty list (none).

  • in0_column_offset (int, optional)read input_tensor_a’s K columns starting at this tile-aligned column of a wider interleaved input_tensor_a, whose width only needs to hold in0_column_offset + K columns, instead of all of its columns. Requires a MatmulMultiCoreReuseMultiCastProgramConfig, an interleaved (DRAM or L1) input_tensor_a and no transpose_a. Defaults to None.

Returns:

ttnn.Tensorthe output tensor.

Example

# Define input tensors
activations = ttnn.rand((10, 64, 32), dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=device)
weight = ttnn.rand((32, 128), dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=device)
bias = ttnn.rand((128,), dtype=ttnn.bfloat16, layout=ttnn.TILE_LAYOUT, device=device)
# Perform linear transformation

output = ttnn.linear(activations, weight, bias=bias)
logger.info(f"Output tensor shape: {output.shape}")  # Output tensor shape: Shape([10, 64, 128])