2424from typing import Optional , Sequence
2525from enum import Enum
2626from . import types as tle
27+ from .mthreads import common as mthreads_common
28+ from .mthreads import buffer as mthreads_buffer
2729from .mthreads import copy as mthreads_copy
30+ from .mthreads import warp_specialize as mthreads_warp_specialize
31+ from .mthreads import wgmma as mthreads_wgmma
2832from .iluvatar import copy as iluvatar_copy
2933from triton .compiler .code_generator import flatten_values_to_ir , unflatten_ir_values
3034
4549def _mark_wgmma_user_promise (_semantic , _generator ):
4650 if _generator is None or _semantic is None :
4751 return
52+ # This module attribute is NVIDIA-specific and must not leak into mthreads IR.
53+ if mthreads_common .enabled ():
54+ return
4855 _generator .module .set_attr (
4956 _WGMMA_PIPELINE_MODE_ATTR ,
5057 _semantic .builder .get_string_attr (_WGMMA_PIPELINE_MODE_USER_PROMISE ),
@@ -186,8 +193,12 @@ def warp_specialize(functions_and_args, worker_num_warps, worker_num_regs, _sema
186193 if _generator is None :
187194 raise ValueError ("warp_specialize requires a Triton code generator" )
188195 functions_and_args = tl ._unwrap_if_constexpr (functions_and_args )
189- worker_num_warps = [tl ._unwrap_if_constexpr (w ) for w in tl ._unwrap_if_constexpr (worker_num_warps )]
190- worker_num_regs = [tl ._unwrap_if_constexpr (r ) for r in tl ._unwrap_if_constexpr (worker_num_regs )]
196+ mthreads_enabled = mthreads_common .enabled ()
197+ if mthreads_enabled :
198+ worker_num_warps , worker_num_regs = mthreads_warp_specialize .normalize_config (worker_num_warps , worker_num_regs )
199+ else :
200+ worker_num_warps = [tl ._unwrap_if_constexpr (w ) for w in tl ._unwrap_if_constexpr (worker_num_warps )]
201+ worker_num_regs = [tl ._unwrap_if_constexpr (r ) for r in tl ._unwrap_if_constexpr (worker_num_regs )]
191202 if len (functions_and_args ) < 1 :
192203 raise ValueError ("warp_specialize requires at least a default partition function" )
193204 num_partitions = len (functions_and_args ) - 1
@@ -200,8 +211,9 @@ def warp_specialize(functions_and_args, worker_num_warps, worker_num_regs, _sema
200211
201212 builder = _semantic .builder
202213 insert_pt = builder .get_insertion_point ()
203- inline_user_promise = _is_wgmma_user_promise_marked (_generator )
204- if inline_user_promise :
214+ if mthreads_enabled :
215+ call_jit_function = mthreads_warp_specialize .partition_function_caller (_generator )
216+ elif _is_wgmma_user_promise_marked (_generator ):
205217 call_jit_function = _generator .inline_JitFunction
206218 else :
207219 call_jit_function = _generator .call_JitFunction
@@ -225,7 +237,10 @@ def warp_specialize(functions_and_args, worker_num_warps, worker_num_regs, _sema
225237 worker_arg_handles , worker_items = _deduplicate_warp_specialize_captures (worker_items )
226238
227239 builder .restore_insertion_point (insert_pt )
228- ws_op = builder .create_warp_specialize (result_types , worker_arg_handles , worker_num_warps )
240+ if mthreads_enabled :
241+ ws_op = mthreads_warp_specialize .create_op (builder , result_types , worker_num_warps )
242+ else :
243+ ws_op = builder .create_warp_specialize (result_types , worker_arg_handles , worker_num_warps )
229244 real_default_block = builder .create_block_with_parent (ws_op .get_default_region (), [])
230245 default_block .merge_block_before (real_default_block )
231246 default_block = real_default_block
@@ -234,7 +249,10 @@ def warp_specialize(functions_and_args, worker_num_warps, worker_num_regs, _sema
234249 ws_op .set_requested_registers (worker_num_regs )
235250
236251 builder .create_block_with_parent (ws_op .get_partition_op_holder (), [])
237- partitions_op = builder .create_warp_specialize_partitions (num_partitions )
252+ if mthreads_enabled :
253+ partitions_op = mthreads_warp_specialize .create_partitions (builder , worker_arg_handles , num_partitions )
254+ else :
255+ partitions_op = builder .create_warp_specialize_partitions (num_partitions )
238256 partition_arg_types = [arg .get_type () for arg in worker_arg_handles ]
239257 for idx , (worker_fn , worker_args , flattened , remapped ) in enumerate (worker_items ):
240258 block = builder .create_block_with_parent (partitions_op .get_region (idx ), partition_arg_types )
@@ -288,6 +306,9 @@ def alloc(
288306 dtype: Data type
289307 layout: Memory layout encoding (optional)
290308 scope: Storage type (default to shared memory)
309+ nv_mma_shared_layout: Select an MMA-consumer-defined shared layout when
310+ ``layout`` is None. On mthreads this is materialized by the SQMMA
311+ lowering rather than as an NVIDIA encoding.
291312 _semantic: Semantic analyzer (internal use)
292313
293314 Returns:
@@ -331,16 +352,22 @@ def alloc(
331352
332353 # Map scope to storage (backward compatibility)
333354 storage = scope
355+ mthreads_auto_sqmma_shared_layout = (mthreads_common .enabled () and storage == tle .smem
356+ and mthreads_wgmma .use_auto_shared_layout (layout , nv_mma_shared_layout ))
334357
335358 try :
336359 unwrapped_shape = [tl ._unwrap_if_constexpr (dim ) for dim in shape ]
337360 full_shape = unwrapped_shape
361+ use_mthreads_buffer = (mthreads_common .enabled () and mthreads_buffer .needs_non_power_of_two_leading_dim (
362+ _semantic .builder , unwrapped_shape ))
363+ if use_mthreads_buffer :
364+ mthreads_buffer .validate_shape (unwrapped_shape )
338365 dtype = tl ._unwrap_if_constexpr (dtype )
339366 elem_type = dtype .to_ir (_semantic .builder )
340367
341368 if layout is None :
342369 if storage == tle .smem :
343- if not nv_mma_shared_layout :
370+ if mthreads_auto_sqmma_shared_layout or not nv_mma_shared_layout :
344371 layout = tle .swizzled_shared_layout .make_default (rank = len (shape ))
345372 layout_handle = _semantic .builder .make_swizzled_shared_encoding_attr (
346373 layout .vectorSize ,
@@ -383,9 +410,20 @@ def alloc(
383410 tensor_handle = _semantic .builder .create_local_alloc (mutable_ty , init_value .handle )
384411 else :
385412 tensor_handle = _semantic .builder .create_local_alloc (full_shape , elem_type , layout_handle )
413+ if mthreads_auto_sqmma_shared_layout :
414+ mthreads_wgmma .mark_auto_shared_layout (_semantic .builder , tensor_handle )
386415 else :
387416 raise ValueError (f"Storage type { storage } not yet supported" )
388417
418+ if use_mthreads_buffer :
419+ return mthreads_buffer .create_buffered_tensor (
420+ tensor_handle ,
421+ dtype ,
422+ unwrapped_shape ,
423+ storage ,
424+ layout ,
425+ _semantic ,
426+ )
389427 return tle .buffered_tensor (tensor_handle , dtype , unwrapped_shape , storage , layout , _semantic )
390428
391429 except Exception as e :
@@ -773,16 +811,23 @@ def wgmma(
773811 """
774812 trans_a = _require_wgmma_bool (trans_a , "trans_a" )
775813 trans_b = _require_wgmma_bool (trans_b , "trans_b" )
776- a , b = _canonicalize_wgmma_operands (a , b , trans_a , trans_b , _semantic )
814+ mthreads_enabled = mthreads_common .enabled ()
815+ if mthreads_enabled :
816+ mthreads_wgmma .validate_operands (a , b , acc , trans_a , trans_b )
817+ else :
818+ a , b = _canonicalize_wgmma_operands (a , b , trans_a , trans_b , _semantic )
777819
778820 m , k = [int (tl ._unwrap_if_constexpr (dim )) for dim in a .type .shape ]
779821 k_b , n = [int (tl ._unwrap_if_constexpr (dim )) for dim in b .type .shape ]
780822 if k != k_b :
781823 raise ValueError (f"wgmma shape mismatch: a is { a .type .shape } , b is { b .type .shape } " )
782- if m < 64 or m % 64 != 0 :
783- raise ValueError ("wgmma result M dimension must be divisible by 64" )
784- if n < 8 or n % 8 != 0 :
785- raise ValueError ("wgmma result N dimension must be divisible by 8" )
824+ if mthreads_enabled :
825+ mthreads_wgmma .validate_dimensions (m , n )
826+ else :
827+ if m < 64 or m % 64 != 0 :
828+ raise ValueError ("wgmma result M dimension must be divisible by 64" )
829+ if n < 8 or n % 8 != 0 :
830+ raise ValueError ("wgmma result N dimension must be divisible by 8" )
786831 if k < 16 :
787832 raise ValueError ("wgmma K dimension must be at least 16" )
788833
@@ -813,6 +858,9 @@ def wgmma(
813858 if max_num_imprecise_acc < 0 :
814859 raise ValueError ("max_num_imprecise_acc must be non-negative" )
815860
861+ if mthreads_enabled :
862+ mthreads_wgmma .validate_options (max_num_imprecise_acc , out_dtype )
863+
816864 ret_scalar_ty = _wgmma_ret_scalar_ty (a .dtype , out_dtype )
817865 ret_ty = tl .block_type (ret_scalar_ty , [m , n ])
818866 builder = _semantic .builder
@@ -851,6 +899,8 @@ def wgmma_wait(pendings, acc=None, _semantic=None, _generator=None) -> tl.tensor
851899 pendings = _require_wgmma_int (pendings , "pendings" )
852900 if pendings < 0 :
853901 raise ValueError ("wgmma_wait pendings must be non-negative" )
902+ if mthreads_common .enabled ():
903+ mthreads_wgmma .validate_wait_pendings (pendings )
854904 if not isinstance (acc , tl .tensor ):
855905 raise ValueError (f"wgmma_wait acc must be a tl.tensor, got { type (acc ).__name__ } " )
856906 result = _semantic .builder .create_tle_wgmma_wait (acc .handle , pendings )
@@ -921,7 +971,7 @@ def copy(
921971 tle.copy(tma_desc, local_buf, [64, 64], [x_offset, y_offset], barrier=bar)
922972 tle.gpu.barrier_wait(bar, phaseIdx=0)
923973 """
924- mthreads_enabled = mthreads_copy .enabled ()
974+ mthreads_enabled = mthreads_common .enabled ()
925975 iluvatar_enabled = iluvatar_copy .enabled ()
926976
927977 def normcopy (
@@ -1078,9 +1128,12 @@ def tmacopy(
10781128 raise ValueError ("copy barrier is only supported for TMA global-to-shared copy" )
10791129 return normcopy (src , dst , shape , direction , _semantic )
10801130 if mthreads_enabled :
1131+ barrier_slot = None
10811132 if barrier is not None :
1082- raise ValueError ("TMA copy barrier is only supported on NVIDIA backend" )
1083- return mthreads_copy .tmacopy (src , dst , direction , shape , offsets , _semantic )
1133+ if direction != CopyDirection .GM_TO_LOCAL :
1134+ raise ValueError ("TMA copy barrier is only supported for global-to-shared TMA copy" )
1135+ barrier_slot = _tma_completion_barrier_slot (barrier , _semantic )
1136+ return mthreads_copy .tmacopy (src , dst , direction , shape , offsets , barrier_slot , _semantic )
10841137 else :
10851138 return tmacopy (src , dst , direction , shape , offsets , barrier , _semantic )
10861139
0 commit comments