For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).
Mojo function
generic_fused_qk_rope_bshd_paged_ragged
def generic_fused_qk_rope_bshd_paged_ragged[dtype: DType, freq_dtype: DType, //, *, interleaved: Bool, has_position_ids: Bool, target: StringSlice[ImmStaticOrigin], mrope_types: TypeList[mrope_types.values] = *?, mrope_section: Optional[Coord[mrope_types]] = None](q_proj: TileTensor[dtype, Storage=q_proj.Storage, linear_idx_type=q_proj.linear_idx_type], input_row_offsets: TileTensor[DType.uint32, Storage=input_row_offsets.Storage, linear_idx_type=input_row_offsets.linear_idx_type], kv_collection: PagedKVCacheCollection[scale_dtype_=kv_collection.scale_dtype_, quantization_granularity_=kv_collection.quantization_granularity_], freqs_cis: TileTensor[freq_dtype, Storage=freqs_cis.Storage, linear_idx_type=freqs_cis.linear_idx_type], position_ids: TileTensor[DType.uint32, Storage=position_ids.Storage, linear_idx_type=position_ids.linear_idx_type], layer_idx: UInt32, output: TileTensor[dtype, Storage=output.Storage, linear_idx_type=output.linear_idx_type], context: DeviceContext)
Performs a fused RoPE projection for Q and K projections.
We have a manually fused QKV projection with mo.opaque dtypes in our Llama model. Due to a limitation in custom op definitions, we can't declare both a tensor and opaque dtype as output from a custom kernel. This requires us to only note Q_proj as an output from the QKV projection. If we immediately follow the QKV proj kernel with a RoPE kernel applied to K, we'll get a race condition because the graph compiler doesn't know about the dependency between these kernels in the graph definition. Here we fuse the RoPE kernel applied to Q_proj with K_proj, so K_proj RoPE is only executed after QKV completes.
Parameters:
- βdtype (
DType): Data type of theq_projandoutputtensors (inferred). - βfreq_dtype (
DType): Data type of thefreqs_cisRoPE frequency table (inferred). - βinterleaved (
Bool): Whether RoPE applies interleaved (GPT-NeoX style) rotation to adjacent element pairs. - βhas_position_ids (
Bool): Whether per-tokenposition_idsare provided; whenFalse,position_idsis unused. - βtarget (
StringSlice[ImmStaticOrigin]): Target device string for kernel dispatch. - βmrope_types (
TypeList[mrope_types.values]): TypeList of coordinate element types constrainingmrope_sectionfor multimodal RoPE. - βmrope_section (
Optional[Coord[mrope_types]]): Optional section sizes splitting the head dimension into temporal, height, and width spans for multimodal RoPE (defaults toNone).
Args:
- βq_proj (
TileTensor[dtype, Storage=q_proj.Storage, linear_idx_type=q_proj.linear_idx_type]): Query projection tile tensor with shape (sum(seq_lens), num_heads, head_size). - βinput_row_offsets (
TileTensor[DType.uint32, Storage=input_row_offsets.Storage, linear_idx_type=input_row_offsets.linear_idx_type]): Tile tensor with shape (batch_size + 1,) denoting the start of each sequence along the ragged sequence dimension. - βkv_collection (
PagedKVCacheCollection[scale_dtype_=kv_collection.scale_dtype_, quantization_granularity_=kv_collection.quantization_granularity_]): The paged KV cache collection storing the K cache for this layer, retrieved vialayer_idx. - βfreqs_cis (
TileTensor[freq_dtype, Storage=freqs_cis.Storage, linear_idx_type=freqs_cis.linear_idx_type]): Precomputed RoPE frequency table applied to Q and K. - βposition_ids (
TileTensor[DType.uint32, Storage=position_ids.Storage, linear_idx_type=position_ids.linear_idx_type]): Per-token position indices used to index intofreqs_cis; ignored whenhas_position_idsisFalse. - βlayer_idx (
UInt32): The index of the layer being executed, used to retrieve the K cache fromkv_collection. - βoutput (
TileTensor[dtype, Storage=output.Storage, linear_idx_type=output.linear_idx_type]): The pre-allocated output tile tensor receiving the rotated Q projection. - βcontext (
DeviceContext): The call context pointer, passed by the graph compiler.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!