ttnn.transformer.ring_mla

ttnn.transformer.ring_mla(input_tensor_q: ttnn.Tensor, input_tensor_kv: ttnn.Tensor, *, persistent_output_buffer_kv: ttnn.Tensor, head_dim_v: int, logical_n: int, program_config: ttnn.SDPAProgramConfig, scale: float = None, compute_kernel_config: ttnn.DeviceComputeKernelConfig = None, dim: int, multi_device_global_semaphore: List[ttnn.GlobalSemaphore], num_links: int, cluster_axis: int | None, mesh_device: ttnn.MeshDevice, topology: ttnn.ccl.Topology, subdevice_id: tt.tt_metal.SubDeviceId | None = None, ccl_core_grid_offset: ttnn.CoreCoord, use_column_major_ccl: bool = False, is_balanced: bool = False, kv_cache_batch_idx: int = None, kv_actual_isl: int | None, slot_id: ttnn.Tensor | None, kv_actual_isl_tensor: ttnn.Tensor | None, kv_cache_num_layers: int | None, kv_cache_layer_idx: int | None, kv_valid_end_tensor: ttnn.Tensor | None)

Causal Ring MLA attention over a single KV tensor.

K and V are represented by one tensor. QK uses the full KV head dimension and QKT@V uses the first head_dim_v columns as V. The V dimension must be smaller than K and tile aligned. The KV tensor must have one shared KV head.

Parameters:
  • input_tensor_q (ttnn.Tensor)Queries [b x nqh x N/num_devices x dh] (N/S under split KV).

  • input_tensor_kv (ttnn.Tensor)Shared KV tensor [b x nkv x N/num_devices x dh].

Keyword Arguments:
  • persistent_output_buffer_kv (ttnn.Tensor)Persistent buffer for gathered KV tensor.

  • head_dim_v (int)Tile-aligned V hidden dimension, read from KV’s prefix.

  • logical_n (int)Logical global sequence length before sharding.

  • program_config (ttnn.SDPAProgramConfig)Program configuration for the operation.

  • scale (float, optional)Scale factor for QK^T. Defaults to None.

  • compute_kernel_config (ttnn.DeviceComputeKernelConfig, optional)Defaults to None.

  • dim (int)Dimension for ring all-gather.

  • multi_device_global_semaphore (List[ttnn.GlobalSemaphore])Global semaphores for CCL synchronization.

  • num_links (int)Number of CCL links.

  • cluster_axis (int, optional)Mesh axis for an axis ring. Pass None for one full-mesh snake ring.

  • mesh_device (ttnn.MeshDevice)Multi-device mesh.

  • topology (ttnn.ccl.Topology)Communication topology.

  • subdevice_id (Optional[tt.tt_metal.SubDeviceId])Sub-device identifier. Defaults to None.

  • ccl_core_grid_offset (ttnn.CoreCoord)Core grid offset for CCL workers.

  • use_column_major_ccl (bool)If true, allocate CCL workers column-major. Defaults to False.

  • is_balanced (bool)Whether to use balanced causal work distribution. Defaults to False.

  • kv_cache_batch_idx (int, optional)Selects one batch slot from an indexed K/V cache. Defaults to None.

  • kv_actual_isl (int, optional)Prior valid global KV length before this fixed-size chunk. When passed, enables KV-pad-aware rotation and derives current valid tokens as logical_n - kv_actual_isl.

  • slot_id (ttnn.Tensor, optional)Cache-user slot read on-device during trace replay. Must be a one-element UINT32 ROW_MAJOR DRAM tensor on the same mesh device as Q. Must be supplied together with kv_actual_isl_tensor. Defaults to None.

  • kv_actual_isl_tensor (ttnn.Tensor, optional)Prior valid global KV length read on-device during trace replay. Has the same one-element UINT32 ROW_MAJOR DRAM contract as slot_id and must be supplied together with it. Its value must be tile-aligned and below cache capacity. Padded Q rows beyond the cache boundary are ignored. Defaults to None.

  • kv_cache_num_layers (int, optional)Number of layers packed into each cache-user slot. None uses 1. The selected cache batch is slot_id[0] * kv_cache_num_layers + kv_cache_layer_idx.

  • kv_cache_layer_idx (int, optional)Layer within the cache-user slot. None uses 0 and the value must be less than kv_cache_num_layers.

  • kv_valid_end_tensor (ttnn.Tensor, optional)Global count of real tokens, read on-device; attention stops at the tile holding the last real token of a partially filled chunk. Output rows in the tiles past it are not written and hold unspecified values. Same one-element UINT32 contract as slot_id; requires the metadata path and no sliding window. Defaults to None.

Metadata path and cache fold: as ring_joint_scaled_dot_product_attention (see its docstring).

Split KV (cluster_axis=None only): Q is sequence-sharded over the S devices of mesh axis 0 only (heads may be split over axis 1) while KV is sequence-sharded over all R devices; R/S must equal the size of mesh axis 1. Q and the output then hold N/S rows per device. A Q slab of s tiles spans R/S KV regions of s*S/R tiles per global chunk of s*S tiles, laid out block-cyclically: global region g lives on row-major device g % R at local offset (g // R) * region. Attention covers the full causal prefix across all sources. Supported for dense causal chunked prefill only (no balancing, joint tokens, sliding window or circular cache) with fp32_dest_acc_en=False. The gathered KV buffer must hold at least R times the local KV extent, and logical_n must not exceed R times the local KV extent (not the gathered buffer size). Without kv_actual_isl or kv_actual_isl_tensor, logical_n must be a nonzero whole number of global chunks.

Returns:

(ttnn.Tensor, ttnn.Tensor) - Attention output [b x nqh x N/num_devices x head_dim_v] (N/S rows under split KV). - Streaming statistics scratch [b x nqh x 2*N/num_devices x 1] (2*N/S rows under split KV).