Ë
    ÛÓ^j­¢  ã                  ó:  — d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	 ddl
mZ ddlZddlmZ ddlmZ dd	lmZmZmZmZmZ d
dlmZ d
dlmZ  ej6                  e«      Z ed¬«       G d„ d«      «       Zej>                  d(d„«       Z ej>                  d)d„«       Z!ej>                  d*d+d„«       Z"ejF                  jH                  d*d,d„«       Z%d*d-d„Z&ej>                  d.d„«       Z'd/d„Z(d0d„Z)d1d2d„Z*	 	 	 	 	 	 	 	 	 	 d3d„Z+	 	 	 	 	 	 	 	 	 	 d4d„Z,d5d„Z-d6d„Z.	 	 	 	 	 	 	 	 	 	 	 	 	 	 d7d„Z/	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d8d„Z0 ed d!¬"«      	 	 	 	 d9	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d:d#„«       Z1	 	 	 	 	 	 	 	 	 	 d;d$„Z2	 	 	 	 	 	 	 	 	 	 d;d%„Z3d<d&„Z4	 d1	 	 	 	 	 	 	 	 	 	 	 d=d'„Z5y)>uS  DeepGEMM integration: fused grouped GEMM kernels from `kernels-community/deep-gemm`.

Provides:
- `deepgemm_bf16_experts_forward`: BF16 M-grouped experts forward.
- `deepgemm_fp8_fp4_linear`: end-to-end FP8/FP4 linear (output dtype follows the input).
- `deepgemm_fp8_fp4_experts_forward`: FP8 (or FP4 on SM100+) M-grouped experts forward.
- `deepgemm_fp8_fp4_megamoe_experts_forward`: FP8xFP4 Mega MoE forward (SM100+).

Requirements: CUDA, Hopper (SM90+), CUDA runtime â‰¥ 12.3, kernels-community/deep-gemm
â‰¥ 2.5 (Mega MoE symbols required). Mega MoE additionally needs SM100+ at call time.
é    )ÚannotationsN)ÚCallable)Ú	dataclassé   )Úlogging)Údeprecate_kwarg)ÚKERNELS_MAX_VERSIONÚKERNELS_MIN_VERSIONÚis_kernels_availableÚis_torchdynamo_compilingÚresolve_internal_importé   )Úlazy_load_kernel)Úto_localT)Úfrozenc                  ó€   — e Zd ZU dZded<   ded<   ded<   ded<   ded<   ded<   ded	<   ded
<   ded<   ded<   ded<   y)ÚDeepGEMMz>Curated entry points exposed by `kernels-community/deep-gemm`.r   Úfp8_fp4_matmulÚgrouped_fp8_fp4_matmul_ntÚgrouped_fp8_fp4_matmul_nnÚgrouped_bf16_matmul_ntÚgrouped_bf16_matmul_nnÚper_token_cast_to_fp8Ú!transform_sf_into_required_layoutÚtransform_weights_for_mega_moeÚget_symm_buffer_for_mega_moeÚfp8_fp4_mega_moeÚintÚm_alignmentN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© ó    úm/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/integrations/deepgemm.pyr   r   9   sI   … áHàÓØ'Ó'Ø'Ó'Ø$Ó$Ø$Ó$Ø#Ó#Ø'/Ó/Ø$,Ó,Ø"*Ó*ØÓð Ôr&   r   c                 ór  — t         j                  j                  d«      xs t         j                  j                  d«      } | r| S t        j                  d«      }|r<t         j
                  j                  t         j
                  j                  |«      «      S t         j
                  j                  d«      ryy)u‚  Resolve the CUDA toolkit root the way DeepGEMM's JIT does:
    ``CUDA_HOME`` â†’ ``CUDA_PATH`` â†’ dir of ``which nvcc`` â†’ ``/usr/local/cuda`` (``None`` if none found).

    Mirrors DeepGEMM's own ``_find_cuda_home`` so we agree on the path it will actually use, rather than
    reusing ``torch.utils.cpp_extension.CUDA_HOME`` whose resolution inits a CUDA context (fork-unsafe).
    Ú	CUDA_HOMEÚ	CUDA_PATHÚnvccz/usr/local/cudaN)ÚosÚenvironÚgetÚshutilÚwhichÚpathÚdirnameÚisdir)Ú	cuda_homer+   s     r'   Ú_get_cuda_homer5   P   sy   € ô —
‘
—‘˜{Ó+ÒJ¬r¯z©z¯~©~¸kÓ/J€IÙØÐÜ�<‰<˜Ó€DÙÜ�w‰w�‰œrŸw™wŸ™¨tÓ4Ó5Ð5Ü	‡w�w‡}�}Ð&Ô'Ø Ør&   c                 ó*  — t        «       } | €yt        j                  j                  | d«      }t        j                  j	                  |«      r‰	 t        |«      5 }t        j                  |«      }ddd«       j                  d|j                  di «      «      j                  dd«      }|j                  d«      dd \  }}t        |«      t        |«      fS t        j                  j                  | d	«      }t        j                  j	                  |«      rp	 t        |«      5 }t        j                  d
|j!                  «       «      }ddd«       r4t        |j#                  d«      «      t        |j#                  d«      «      fS 	 t        j                  j                  | dd«      }	t        j                  j	                  |	«      rd	 t        |	«      5 }t        j                  d|j!                  «       «      }ddd«       r't        |j#                  d«      «      }
|
dz  |
dz  dz  fS 	 yy# 1 sw Y   �Œ½xY w# t        t        t        f$ r Y �Œxw xY w# 1 sw Y   �ŒxY w# t        t        f$ r Y Œëw xY w# 1 sw Y   ŒxY w# t        t        f$ r Y yw xY w)a‹  Version of the CUDA toolkit nvcc will use, as ``(major, minor)``, read off disk without a
    subprocess from (in order) ``{CUDA_HOME}/version.json``, ``version.txt``, or the ``CUDA_VERSION``
    define in ``include/cuda.h``. ``None`` if unreadable. This is the compiler that builds the kernels,
    unlike ``torch.version.cuda`` (torch's bundled runtime, which never drives a JIT compile).
    Nzversion.jsonÚ	cuda_nvccÚcudaÚversionÚ ú.r   zversion.txtzCUDA Version (\d+)\.(\d+)r   Úincludezcuda.hz#define CUDA_VERSION (\d+)iè  é
   )r5   r,   r1   ÚjoinÚisfileÚopenÚjsonÚloadr.   Úsplitr   ÚOSErrorÚ
ValueErrorÚAttributeErrorÚreÚsearchÚreadÚgroup)r4   Úversion_jsonÚfÚ
componentsr9   ÚmajorÚminorÚversion_txtÚmatchÚcuda_hÚcuda_versions              r'   Ú_get_nvcc_versionrT   c   s>  € ô Ó €IØÐØä—7‘7—<‘< 	¨>Ó:€LÜ	‡w�w‡~�~�lÔ#ð	Ü�lÓ#ð * qÜ!ŸY™Y q›\�
÷*à —n‘n [°*·.±.ÀÈÓ2LÓM×QÑQÐR[Ð]_Ó`ˆGØ"Ÿ=™=¨Ó-¨b¨qÐ1‰LˆE�5Ü�u“:œs 5›zÐ)Ð)ô —'‘'—,‘,˜y¨-Ó8€KÜ	‡w�w‡~�~�kÔ"ð	Ü�kÓ"ð J aÜŸ	™	Ð">ÀÇÁÃÓI�÷JáÜ˜5Ÿ;™; q›>Ó*¬C°·±¸A³Ó,?Ð?Ð?ð ô �W‰W�\‰\˜) Y°Ó9€FÜ	‡w�w‡~�~�fÔð	Ü�f“ð K ÜŸ	™	Ð"?ÀÇÁÃÓJ�÷KáÜ" 5§;¡;¨q£>Ó2�Ø# tÑ+¨l¸TÑ.AÀbÑ-HÐHÐHð ñ ÷=*ñ *ûô
 œ¤^Ð4ò 	Úð	ú÷Jñ Jûô œÐ$ò 	Ùð	ú÷Kð Kûô
 œÐ$ò 	Øàð	ús   ÁH7 ÁH*Á/A&H7 ÄI Ä!%IÅ=I ÇJ  Ç%I4Ç60J  È*H4È/H7 È7IÉIÉIÉI ÉI1É0I1É4I=É9J  Ê JÊJc                óà  — t        «       �sCt        «       sdt        › dt        › dt        › d�S t        j
                  j                  «       syt        j
                  j                  «       \  }}| rdnd}||vr| rdnd	}d
|› d|› |› d�S |dk(  rdnd}t        «       }|€d|d   › d|d   › d�S t        j                  j                  t        j                  j                  |dd«      «      sd|› d|d   › d|d   › d�S t        «       }|€d|› d|d   › d|d   › d�S ||k  r/d|› |› d|d   › d|d   › d|d   › d|d   › d |› d!|d   › d|d   › d�S t        d"«      }|€y#t        |d$d«      }	t        |d%d«      }
t        |d&d«      }t        |d'd«      }t        |d(d«      }t!        |d)¬*«      }t        |d+d«      }t        |d,d«      }t        |d-d«      }t        |d.d«      }t        |d/d«      }d$|	fd%|
fd&|fd'|fd(|fd)|fd+|fd,|fd-|fd.|fd/|ffD ��cg c]	  \  }}|€|‘Œ }}}|r*d0d1j                  |«      › d2t        › dt        › dt        › d�	S t#        |	|
|||||||| |«       ¬3«      S c c}}w )4aÕ  Load DeepGEMM once or returns an error message if env or any required symbol is missing. This is wrapped in a
    function that will raise an `ImportError` with the error message. The reason we raise in the wrapper rather than
    here is that @functools.cache will only cache a return value, not an exception.

    `requires_sm100` raises a Blackwell-specific error for callers (FP4 / Mega MoE) that won't work on Hopper, instead
    of the generic SM90+ message.
    zUDeepGEMM kernel requires the `kernels` package. Please install a compatible version (z <= version < z), e.g. `pip install kernels==ú`z9DeepGEMM kernel requires CUDA, but CUDA is not available.)r=   )é	   r=   zBlackwell (SM100)z"Hopper (SM90) or Blackwell (SM100)zDeepGEMM requires z; current device is SMr;   r=   )é   rW   )rX   é   Nu(   DeepGEMM's JIT needs a CUDA toolkit â‰¥ r   r   z8, but none was found. Set `CUDA_HOME` to a CUDA toolkit.Úbinr+   z:DeepGEMM's JIT compiles with nvcc, but none was found in `u,   /bin`. Point `CUDA_HOME` at a full CUDA â‰¥ z& toolkit (not a runtime-only install).zDeepGEMM found nvcc in `u–   /bin` but could not read its CUDA version (no parseable `version.json`, `version.txt`, or `include/cuda.h`). Point `CUDA_HOME` at a complete CUDA â‰¥ z	 toolkit.zDeepGEMM on SMu    needs a CUDA â‰¥ z toolkit, but nvcc z in `u.   ` is too old. Point `CUDA_HOME` at a CUDA â‰¥ z	deep-gemmuc   Failed to load `kernels-community/deep-gemm` â€” check that a build matches the current torch/CUDA.Úfp8_fp4_gemm_ntÚ$m_grouped_fp8_fp4_gemm_nt_contiguousÚ$m_grouped_fp8_fp4_gemm_nn_contiguousÚ!m_grouped_bf16_gemm_nt_contiguousÚ!m_grouped_bf16_gemm_nn_contiguouszutils.per_token_cast_to_fp8)Úchained_pathr   r   r   Ú&get_mk_alignment_for_contiguous_layoutr   z-DeepGEMM kernel is missing required symbols: z, z'. Please install a compatible version ()r   r   r   r   r   r   r   r   r   r   r   )r   r   r
   r	   Útorchr8   Úis_availableÚget_device_capabilityr5   r,   r1   r?   r>   rT   r   Úgetattrr   r   )Úrequires_sm100rN   rO   ÚallowedÚarchÚmin_cudar4   Únvcc_versionÚkernelr   r   r   r   r   r   r   r   r   Úget_mk_alignmentr   ÚnameÚattrÚmissings                          r'   Ú_load_deepgemm_kernelrp   ’   s«  € ô $Õ%Ü#Ô%àgÜ&Ð' ~Ô6IÐ5JÐJhÜ&Ð' qð*ðô
 �z‰z×&Ñ&Ô(ØNä—z‘z×7Ñ7Ó9‰ˆˆuñ *‘%¨wˆØ˜ÑÙ*8Ñ&Ð>bˆDØ'¨ vÐ-CÀEÀ7È5È'ÐQRÐSÐSð $ ršk‘7¨wˆÜ"Ó$ˆ	ØÐà:¸8ÀA¹;¸-ÀqÈÐRSÉÈð V5ð 5ðô �w‰w�~‰~œbŸg™gŸl™l¨9°e¸VÓDÔEàLÈYÈKð X2Ø2:¸1±+°¸aÀÈÁ¸}ÐLrðtðô )Ó*ˆØÐà*¨9¨+ð 6%à%-¨a¡[ M°°8¸A±;°-¸yðJðð
 ˜(Ò"à   ¨ wÐ.@ÀÈ!ÁÀÈQÈxÐXYÉ{ÈmÐ[nØ ‘?Ð# 1 \°!¡_Ð$5°U¸9¸+ð FØ$ Q™K˜=¨¨(°1©+¨°iðAðô ˜kÓ*€FØ€~Øtä˜VÐ%6¸Ó=€NÜ '¨Ð0VÐX\Ó ]ÐÜ '¨Ð0VÐX\Ó ]ÐÜ$ VÐ-PÐRVÓWÐÜ$ VÐ-PÐRVÓWÐÜ3°FÐIfÔgÐÜ(/°Ð8[Ð]aÓ(bÐ%Ü%,¨VÐ5UÐW[Ó%\Ð"Ü#*¨6Ð3QÐSWÓ#XÐ Ü˜vÐ'OÐQUÓVÐÜ˜vÐ'9¸4Ó@Ðð
  Ð/Ø3Ð5NÐOØ3Ð5NÐOØ0Ð2HÐIØ0Ð2HÐIØ*Ð,AÐBØ0Ð2SÐTØ-Ð/MÐNØ+Ð-IÐJØ5Ð7GÐHØÐ!1Ð2ð
÷áˆD�$ð ˆ<ò 	ð€Gñ ñ" à;¸D¿I¹IÀgÓ<NÐ;Oð P4Ü4GÐ3HÈÔWjÐVkð l*Ü*=Ð)>¸aðAð	
ô Ø%Ø";Ø";Ø5Ø5Ø3Ø*KØ'EØ%AØ)Ù$Ó&ôð ùó1s   ÈI*c                ó   — t        | ¬«       y)u÷  Warm the `_load_deepgemm_kernel` cache from an opaque graph node, so Dynamo never traces the loader.

    Under `torch.compile`, Dynamo ignores `@functools.cache` and traces into `_load_deepgemm_kernel`,
    whose cold path (hub download + dynamic import via `lazy_load_kernel`) is untraceable and errors under
    `fullgraph`. `@allow_in_graph` turns the call into an opaque fx node instead â€” but an fx node's return
    must be proxyable, and the `DeepGEMM` bundle of Python callables isn't (`Unsupported: torch.* op
    returned non-Tensor`), so we can't just decorate the real loader. Hence two loaders: this one is
    opaque, returns `None`, and only warms the cache; the real `_load_deepgemm_kernel` right after is then
    a plain cache lookup.
    ©rf   N)rp   rr   s    r'   Ú_populate_deepgemm_kernelrs     s   € ô ¨Ö8r&   c                ól   — t        | ¬«       t        | ¬«      }t        |t        «      rt	        |«      ‚|S )Nrr   )rs   rp   Ú
isinstanceÚstrÚImportError)rf   Údeepgemm_or_errors     r'   Úload_deepgemm_kernelry     s2   € Ü¨^Õ<Ü-¸^ÔLÐÜÐ#¤SÔ)ÜÐ+Ó,Ð,ØÐr&   c                óL   — t         j                  j                  | «      d   dk\  S )z—``True`` for Blackwell (SM100+). Cached: device capability is fixed for the
    process lifetime and this gets hit on every linear/expert forward.
    r   r=   )rb   r8   rd   ©Údevices    r'   Ú	_is_sm100r}      s#   € ô
 �:‰:×+Ñ+¨FÓ3°AÑ6¸"Ñ<Ð<r&   c                ó€   — t        | j                  «      sy| j                  t        j                  k7  ryt        d«      ‚)uº  On B200 (SM100) DeepGEMM only supports UE8M0 (power-of-two) scales; the float32 scales
    that work on H100 (SM90) have no SM100 path. UE8M0 scales load as ``float8_e8m0fnu`` (the
    loader normalizes even float32-container checkpoints like dsv4-flash-base), so a plain
    ``float32`` scale here means a genuine non-UE8M0 checkpoint â€” fail loud rather than let
    ``_coerce_sf_for_kernel`` silently round it and corrupt the output.
    NaÚ  DeepGEMM's Blackwell (SM100) experts kernel requires power-of-two (UE8M0) scale factors, but this checkpoint's expert scales are plain float32 (quantization_config.scale_fmt='float'). Rounding them to UE8M0 would scale the dequantized expert weights incorrectly and silently corrupt the output. Use a checkpoint quantized with scale_fmt='ue8m0', or an experts implementation that consumes float32 block scales directly, e.g. `model.set_experts_implementation('grouped_mm')`.)r}   r|   Údtyperb   Úfloat32rE   )Úscales    r'   Ú_assert_sm100_scales_are_ue8m0r‚   (  s8   € ô �U—\‘\Ô"ØØ‡{�{”e—m‘mÒ#ØÜ
ð	<óð r&   c                ó¢   — | j                  t        j                  «      }|dz   j                  d«      j                  t        j                  «      S )uÅ  Round each fp32 SF up to the nearest power of 2 (zero mantissa).

    Mirrors `deep_gemm.utils.math.ceil_to_ue8m0`. On SM100 the kernel's
    `pack_fp32_into_ue8m0` cleanly extracts the biased exponent only when the
    mantissa is already zero â€” its inner shifts (`>> 15`, `>> 7`, `<< 1`)
    otherwise leak mantissa bits into adjacent UE8M0 byte slots and silently
    corrupt the SF. SM90 consumes raw fp32 SFs without going through this path.
    iÿÿ i  €ÿ)Úviewrb   Úint32Úbitwise_and_Úfloat)ÚsfÚint_views     r'   Ú_ceil_to_ue8m0rŠ   >  s<   € ð �w‰w”u—{‘{Ó#€HØ˜Ñ&×4Ñ4Ð5EÓF×KÑKÌEÏKÉKÓXÐXr&   c                óÄ  — t        | j                  «      }| j                  t        j                  k(  r~|�;| j                  d«      |k  r'|| j                  d«      z  }| j                  |d¬«      } |r.| j                  «       j                  t        j                  «      } n;| j                  «       } n*| j                  t        j                  k(  r|rt        | «      } | j                  «       dvrt        d| j                  «       › d�«      ‚|s| j                  «       S | j                  d«      }| j                  d«      }d| j                  «       z  }| |z   |z  }| j                  «       dk(  rd	|fn||z  d	|f}t!        | j#                  «       «      |k(  r| S t        j$                  | j&                  || j                  | j                  ¬
«      }	|	j)                  | «       |	S )u¢  Lay out `sf` as DeepGEMM's dispatch expects, per arch.

    On SM100 the int-SF path only *checks* the SF (`tma_stride_check`) and never
    transforms it, so we hand it a TMA-aligned MN-major layout (`stride(-2) == 1`,
    `stride(-1) == align(mn, 16/esize)`). On SM90 DeepGEMM transforms SFA itself
    (`get_mn_major_tma_aligned_tensor`) and only *checks* SFB against
    `sm90_sfb_check`, which rejects TMA padding (`stride(-1)` must equal `size(-2)`,
    not `align(mn, â€¦)`); a padded weight SF trips `layout.hpp` whenever `mn` isn't a
    multiple of `16/esize` (e.g. N=576 â†’ mn=5). So on SM90 we return the raw
    row-major SF and let DeepGEMM lay it out.

    Inputs come in three flavors:
      - `float8_e8m0fnu` on SM100: raw UE8M0 bytes â€” pack 4 K-bytes â†’ int32
        (last dim /4) for the kernel's `(INT, 1, gran_k)` path.
      - `float8_e8m0fnu` on SM90: SM90 dispatch only accepts FP32 SFs, so cast
        UE8M0 â†’ FP32 (exact upcast â€” UE8M0 is the biased-exponent half of a
        pow-of-2 FP32, so `.float()` rebuilds the original FP32 scale exactly).
      - `float32`: per-token / per-block SFs from `per_token_cast_to_fp8` or
        on-disk weights â€” round to UE8M0 on SM100 (see `_ceil_to_ue8m0`).
      - `int32`: already-packed UE8M0 â€” pass through.

    When `expected_mn` is set and the SF's M-dim is smaller (block-quantized
    UE8M0, e.g. DSv4-Flash compressor weights with `(N/128, K/128)` SFs), we
    repeat the SF on the M-axis to per-row before packing â€” the `(INT, 1, gran_k)`
    DeepGEMM kernel branch is the only UE8M0 path on SM100; for `gran_mn > 1`
    the kernel only handles FP32 SFs and would otherwise reject our INT SF here.
    éþÿÿÿ©Údim)r   rY   z"DeepGEMM SF must be 2D or 3D, got ÚDéÿÿÿÿé   r   r   ©r   r|   )r}   r|   r   rb   Úfloat8_e8m0fnuÚsizeÚrepeat_interleaveÚ
contiguousr„   r…   r‡   r€   rŠ   rŽ   rE   Úelement_sizeÚtupleÚstrideÚempty_stridedÚshapeÚcopy_)
rˆ   Úexpected_mnÚis_sm100Úgran_mnÚmnÚkfÚalign_toÚ
aligned_mnÚtarget_stridesÚouts
             r'   Ú_coerce_sf_for_kernelr¦   K  s‰  € ô8 ˜Ÿ™Ó#€HØ	‡x�x”5×'Ñ'Ò'ØÐ" r§w¡w¨r£{°[Ò'@Ø! R§W¡W¨R£[Ñ0ˆGØ×%Ñ% g°2Ð%Ó6ˆBÙØ—‘“×%Ñ%¤e§k¡kÓ2‰Bà—‘“‰BØ	�‰”U—]‘]Ò	"¡xÜ˜BÓˆà	‡v�vƒx�vÑÜÐ=¸b¿f¹f»h¸ZÀqÐIÓJÐJñ Ø�}‰}‹Ðà	�‰�‹€BØ	�‰�‹€BØ�R—_‘_Ó&Ñ&€HØ�3˜(‘?Ð# hÑ.€JØ(*¯©«°Aª�a˜‘_¸BÀ¹OÈQÐPZÐ;[€NäˆR�Y‰Y‹[Ó˜^Ò+Øˆ	Ü
×
Ñ
˜bŸh™h¨¸b¿h¹hÈrÏyÉyÔ
Y€CØ‡I�Iˆb„MØ€Jr&   c                óò   — | j                   t        j                  k(  rddddœS |€t        d«      ‚t	        |«      }|dvrt        d|› d�«      ‚|j                   t        j
                  k(  r|rddddœS d	dd
œS )uC  Pick the `per_token_cast_to_fp8` kwargs from weight dtype + SF dtype + arch.

    Cases mirror the kernel's recipes:
      - FP4 weights (`int8`): gran_k=32 packed-UE8M0 SF. SM100+ only.
      - FP8 weights + UE8M0 SF on SM100: gran_k=128 packed-UE8M0 SF (DSv4).
      - FP8 weights + UE8M0 SF on SM90: gran_k=128 FP32 SF â€” the SM90 dispatch in
        `layout.hpp` only matches FP32 SFs, so we keep act SFs as FP32 (and float
        the weight SF in `_coerce_sf_for_kernel`; UE8M0 â†’ FP32 is an exact upcast).
      - FP8 weights + float SF: gran_k=128 float SF (DSv3).
    Té    ©Ú	use_ue8m0Úgran_kÚuse_packed_ue8m0z]DeepGEMM requires block-wise quantized FP8 weights, but the experts have no `block_size` set.))é€   r­   )r   r­   u?   DeepGEMM requires `block_size` âˆˆ {(128, 128), (1, 128)}, got r;   r­   F)rª   r«   )r   rb   Úint8rE   r˜   r“   )ÚweightÚweight_scale_invÚ
block_sizerž   s       r'   Ú_select_fp8_cast_kwargsr²   ˆ  s’   € ð ‡|�|”u—z‘zÒ!Ø!¨RÀTÑJÐJàÐÜØkó
ð 	
ô �zÓ"€JØÐ/Ñ/ÜÐ\Ð]gÐ\hÐhiÐjÓkÐkØ×Ñ¤×!5Ñ!5Ò5¹(Ø!¨SÀdÑKÐKØ¨#Ñ.Ð.r&   c                óœ  — | j                   }| j                  d«      }t        j                  | j	                  «       |d|dz
  ¬«      j                  «       }||z   dz
  |z  |z  }|t        ||«      |dz
  z  z   }||z
  }	t        j                  j                  j                  |	j                  d«      d«      }
t        j                  ||¬«      |
|    z   }|r |j                  d«      j	                  «       }nSt        j                  |fd|t        j                  ¬«      }t        j                  | |k  | j	                  «       d«      ||<   |||fS )aŽ  Build the TMA-aligned grouped layout DeepGEMM expects.

    Returns `(sorted_to_padded, grouped_layout, total_padded_rows)`:
      - `grouped_layout` is per-row expert id (Hopper, with `-1` for padding /
        sentinels) or a cumsum of aligned per-expert counts (Blackwell).
      - EP sentinels (values == `num_experts`) are routed past the last expert
        block so DeepGEMM skips them.
    r   r   )ÚbinsÚminÚmax)r   r   r{   r�   ©r|   r   )r|   r”   rb   Úhistcr   Úlongrµ   ÚnnÚ
functionalÚpadÚcumsumÚarangeÚfullr…   Úwhere)Úexpert_ids_sortedÚnum_expertsÚ	alignmentÚuse_psum_layoutr|   Ú
num_tokensÚtokens_per_expertÚaligned_tokens_per_expertÚtotal_padded_rowsÚpadding_per_expertÚcumulative_paddingÚsorted_to_paddedÚgrouped_layouts                r'   Ú!_build_deepgemm_contiguous_layoutrÍ   §  sL  € ð ×%Ñ%€FØ"×'Ñ'¨Ó*€JäŸ™Ð$5×$9Ñ$9Ó$;À+ÐSTÐZeÐhiÑZiÔj×oÑoÓqÐØ"3°iÑ"?À!Ñ"CÈ	Ñ!QÐU^Ñ ^Ðà"¤S¨°[Ó%AÀYÐQRÁ]Ñ%SÑSÐð 3Ð5FÑFÐÜŸ™×,Ñ,×0Ñ0Ð1C×1JÑ1JÈ1Ó1MÈvÓVÐÜ—|‘| J°vÔ>ÐASÐTeÑAfÑfÐáØ2×9Ñ9¸!Ó<×@Ñ@ÓB‰äŸ™Ð%6Ð$8¸"ÀVÔSX×S^ÑS^Ô_ˆÜ+0¯;©;Ð7HÈ;Ñ7VÐXi×XmÑXmÓXoÐqsÓ+tˆÐ'Ñ(à˜^Ð->Ð>Ð>r&   c                ó†   — t        j                  |g| j                  dd ¢­| j                  | j                  dœŽ}| ||<   |S )z;Pad a sorted tensor into the TMA-aligned contiguous layout.r   Nr·   )rb   Úemptyr›   r|   r   )ÚxrË   rÈ   Úpaddeds       r'   Ú_pad_for_deepgemmrÒ   É  sB   € ä�[‰[Ð*ÐY¨Q¯W©W°Q°R¨[ÑYÀÇÁÐQR×QXÑQXÒY€FØ €FÐÑØ€Mr&   c                ó   — | |   S ©Nr%   )Úx_paddedrË   s     r'   Ú&_unpad_from_deepgemm_contiguous_layoutrÖ   Ð  s   € ØÐ$Ñ%Ð%r&   c                ó<  — |j                  d«      }|j                  d«      }|j                  d«      }t        j                  |«      \  }	}
| |
|z     }||
   }t	        |	|||«      \  }}}|	|k\  j                  d«      }|	j                  |dz
  ¬«       |||	||
|||fS )zóSort tokens by expert id and build the M-grouped padded layout.

    Returns `(sorted_hidden_states_g, sample_weights_g, expert_ids_g,
              sentinel_mask, perm, sorted_to_padded, grouped_layout,
              total_padded_rows)`.
    r�   r   )r¶   )r”   Úreshaperb   ÚsortrÍ   Ú	unsqueezeÚclamp_)Úhidden_statesÚtop_k_indexÚtop_k_weightsrÂ   r   rÄ   Ú	num_top_kÚ
expert_idsÚsample_weightsÚexpert_ids_gÚpermÚsorted_hidden_states_gÚsample_weights_grË   rÌ   rÈ   Úsentinel_masks                    r'   Ú_dispatch_routed_inputrç   ×  sÑ   € ð × Ñ  Ó$€IØ×$Ñ$ RÓ(€JØ"×*Ñ*¨2Ó.€Nô Ÿ™ JÓ/Ñ€L�$Ø*¨4°9Ñ+<Ñ=ÐØ% dÑ+Ðô
 ;\Ø�k ;°ó;Ñ7Ð�nÐ&7ð " [Ñ0×;Ñ;¸BÓ?€MØ×Ñ˜K¨!™OÐÔ,àØØØØØØØð	ð 	r&   c	                ó’  — t        | |«      }	|	|j                  |	j                  «      j                  d«      z  }
|
j	                  |d«       t        j                  |«      }t        j                  |j                  d«      |	j                  ¬«      ||<   |
|   j                  |||«      j                  d¬«      j                  |«      S )uR   Unpad â†’ weighted multiply â†’ mask sentinels â†’ restore order â†’ top-k reduce.r�   g        r   r{   r   r�   )rÖ   Útor   rÚ   Úmasked_fill_rb   Ú
empty_liker¾   r”   r|   r„   Úsum)Ú
out_paddedÚsorted_weightsræ   rã   rË   rÅ   rß   Ú
hidden_dimÚ	out_dtyper¥   ÚweightedÚinv_perms               r'   Ú_combine_routed_outputró   	  s«   € ô 1°Ð=MÓ
N€CØ�^×&Ñ& s§y¡yÓ1×;Ñ;¸BÓ?Ñ?€Hð ×Ñ˜-¨Ô-Ü×Ñ Ó%€HÜ—\‘\ $§)¡)¨A£,°s·z±zÔB€HˆT�Nà�HÑ×"Ñ" :¨y¸*ÓE×IÑIÈaÐIÓP×SÑSÐT]Ó^Ð^r&   Úoutput_dtypezv5.16)r9   c           
     ó„  — |�t        d«      ‚| j                  t        j                  t        j                  fvrt        d| j                  › �«      ‚t        |j                  t        j                  k(  ¬«      }t        |||t        | j                  «      «      }| j                  d| j                  d   «      }	 |j                  |	fi |¤Ž\  }
}t        j                  |
j                  d   |j                  d   | j                  | j                  ¬«      }|j                  d«      rd	d	|d
   fnd}|j!                  |
t#        ||
j%                  d«      ¬«      f|t#        ||j%                  d«      ¬«      f||¬«       |j                  | j                  dd |j                  d   fz   «      }|�|j'                  |«       |S )uó   End-to-end DeepGEMM linear: per-token activation quant + FP8/FP4 matmul.

    Static (per-tensor) activation quantization is rejected â€” DeepGEMM needs
    per-row SFs. Callers should route static activations through the Triton fallback.
    Nz@DeepGEMM linear does not support static activation quantization.z7DeepGEMM linear requires FP16 or BF16 activations, got rr   r�   r   r·   r¬   r   r«   ©r�   )Úrecipe)ÚNotImplementedErrorr   rb   Úbfloat16Úfloat16rE   ry   r®   r²   r}   r|   r„   r›   r   rÏ   r.   r   r¦   r”   Úadd_)Úinputr¯   r°   Úbiasr±   rô   Úactivation_scaleÚdeepgemmÚcast_kwargsÚinput_2dÚ	qinput_2dÚscale_2dÚoutputÚ	sf_recipes                 r'   Údeepgemm_fp8_fp4_linearr  #  s“  € ð Ð#Ü!Ð"dÓeÐeØ‡{�{œ5Ÿ>™>¬5¯=©=Ð9Ñ9ÜÐRÐSX×S^ÑS^ÐR_Ð`ÓaÐaä#°6·<±<Ä5Ç:Á:Ñ3MÔN€HÜ)¨&Ð2BÀJÔPYÐZ_×ZfÑZfÓPgÓh€Kà�z‰z˜"˜eŸk™k¨"™oÓ.€HØ8˜(×8Ñ8¸ÑQÀ[ÑQÑ€IˆxÜ�[‰[˜Ÿ™¨Ñ+¨V¯\©\¸!©_ÀUÇ\Á\ÐY^×YdÑYdÔe€Fð 2=·±ÐASÔ1T��A�{ 8Ñ,Ñ-ÐZ^€IØ×ÑØ	Ô)¨(À	ÇÁÈqÓ@QÔRÐSØ	Ô&Ð'7ÀVÇ[Á[ÐQRÃ^ÔTÐUØØð	 ô ð �[‰[˜Ÿ™ S bÐ)¨V¯\©\¸!©_Ð,>Ñ>Ó?€FØÐØ�‰�DÔØ€Mr&   c                ób  — |j                   t        j                  k7  rt        d|j                   › �«      ‚t	        «       }| j
                  r|j                  n|j                  }|j                  }|j                  d«      }|j                  d«      }|j                  d«      }	t        |||| j                  |j                  t        |«      «      \  }
}}}}}}}t        | j                  r| j                   n| j"                  «      }t        | j$                  «      }| j&                  r-t        | j                  r| j(                  n| j*                  «      nd }| j&                  rt        | j,                  «      nd }| j
                  r|j.                  d   n|j.                  d   }t1        |
||«      }t        j2                  ||||j                   ¬«      } |||||t        |«      ¬«       | j&                  r|j5                  d|||   «       | j                  r| j7                  |«      n| j9                  |«      }t        j2                  ||	||j                   ¬«      } |||||t        |«      ¬«       | j&                  r|j5                  d|||   «       t;        ||||||||	|j                   «	      S )Nú;DeepGEMM experts path requires bfloat16 hidden states, got r�   r   r   r·   )rÄ   )r   rb   rù   rE   ry   Úis_transposedr   r   r|   r”   rç   rÂ   r   r}   r   Úhas_gateÚgate_up_projÚup_projÚ	down_projÚhas_biasÚgate_up_proj_biasÚup_proj_biasÚdown_proj_biasr›   rÒ   rÏ   Ú
index_add_Ú_apply_gateÚact_fnró   )ÚselfrÜ   rÝ   rÞ   rÿ   Úgrouped_bf16_matmulr|   rß   rÅ   rï   Úsorted_hiddenrî   râ   ræ   rã   rË   rÌ   rÈ   Ú	weight_upÚweight_downÚup_biasÚ	down_biasÚ
up_out_dimÚactÚproj_outr¥   s                             r'   Údeepgemm_bf16_experts_forwardr  M  sj  € ð ×ÑœeŸn™nÒ,ÜÐVÐWd×WjÑWjÐVkÐlÓmÐmä#Ó%€Hà=A×=OÒ=O˜(×9Ò9ÐU]×UtÑUtÐà×!Ñ!€FØ× Ñ  Ó$€IØ×#Ñ# AÓ&€JØ×#Ñ# BÓ'€Jô 	Ø�{ M°4×3CÑ3CÀX×EYÑEYÔ[dÐekÓ[ló	ñ	ØØØØØØØØô
 ¨d¯mªm˜×*Ò*ÀÇÁÓN€IÜ˜4Ÿ>™>Ó*€KØZ^×ZgÒZgŒh°·²�t×-Ò-ÀD×DUÑDUÔVÐmq€GØ15·²”˜×,Ñ,Ô-ÀD€Ið )-×(:Ò(:�—‘ Ò$À	ÇÁÐPQÑ@R€JÜ
˜MÐ+;Ð=NÓ
O€CÜ�{‰{Ð,¨jÀÈ}×ObÑObÔc€HÙ˜˜Y¨°.ÔR[Ð\bÓRcÕdØ‡}‚}Ø×Ñ˜AÐ/°¸Ñ1FÔGà-1¯]ª]ˆt×Ñ Ô)ÀÇÁÈHÓ@U€Hô �+‰+Ð'¨¸FÈ-×J]ÑJ]Ô
^€CÙ˜ +¨s°NÔT]Ð^dÓTeÕfØ‡}‚}Ø�‰�qÐ*¨I°lÑ,CÔDä!ØØØØØØØØØ×Ñó
ð 
r&   c                óä  — | j                   rt        d«      ‚t        | j                  «       | j                  dk(  rt        d«      ‚|j                  t        j                  k7  rt        d|j                  › �«      ‚t        | j                  j                  t        j                  k(  ¬«      }| j                  r|j                  n|j                  }|j                   }|j#                  d«      }|j#                  d«      }|j#                  d«      }	t%        | j&                  r| j(                  n| j*                  «      }
t%        | j&                  r| j,                  n| j.                  «      }t%        | j                  «      }t%        | j                  «      }t1        |
|| j2                  t5        |«      «      }t7        |||| j8                  |j:                  t5        |«      «      \  }}}}}}}}|j=                  d«      rd	d	|d
   fnd } |j>                  |fi |¤Ž\  }}tA        |||«      }tA        |||«      }t        jB                  ||
jD                  d	   |t        j                  ¬«      } ||tG        ||¬«      f|
tG        ||
j#                  d«      ¬«      f|||t5        |«      ¬«       | j&                  r| jI                  |«      n| jK                  |«      } |j>                  |fi |¤Ž\  }}t        jB                  ||	|t        j                  ¬«      } ||tG        ||¬«      f|tG        ||j#                  d«      ¬«      f|||t5        |«      ¬«       tM        ||||||||	|j                  «	      S )NzðDeepGEMM experts selected on a model spanning multiple CUDA devices in one process; its kernels are bound to a single CUDA context and corrupt across devices. Use `experts_implementation='grouped_mm'`, or run one device per process (TP/EP).ÚstaticzJDeepGEMM experts dispatch does not support static activation quantization.r  rr   r�   r   r¬   r   r«   r·   rö   rŒ   )r÷   rÄ   )'Ú_deepgemm_disabledÚRuntimeErrorr‚   Údown_proj_scale_invÚactivation_schemerø   r   rb   rù   rE   ry   r  r®   r	  r   r   r|   r”   r   r
  r  r  Úgate_up_proj_scale_invÚup_proj_scale_invr²   r±   r}   rç   rÂ   r   r.   r   rÒ   rÏ   r›   r¦   r  r  ró   )r  rÜ   rÝ   rÞ   rÿ   Úgrouped_fp8_fp4_matmulr|   rß   rÅ   rï   r  Úweight_scale_upr  Úweight_scale_downr   r  rî   Ú_expert_ids_græ   rã   rË   rÌ   rÈ   r  Úact_fp8Ú
act_scalesr  Úproj_fp8Úproj_scalesr¥   s                                 r'   Ú deepgemm_fp8_fp4_experts_forwardr0  Ž  sG  € ð ×Òô ð\ó
ð 	
ô # 4×#;Ñ#;Ô<à×Ñ Ò)Ü!Ð"nÓoÐoØ×ÑœeŸn™nÒ,ÜÐVÐWd×WjÑWjÐVkÐlÓmÐmä#°4·>±>×3GÑ3GÌ5Ï:É:Ñ3UÔV€Hà.2×.@Ò.@ˆ×*Ò*Àh×FhÑFhð ð ×!Ñ!€FØ× Ñ  Ó$€IØ×#Ñ# AÓ&€JØ×#Ñ# BÓ'€Jä¨d¯mªm˜×*Ò*ÀÇÁÓN€IÜ¸d¿mºm˜t×:Ò:ÐQU×QgÑQgÓh€OÜ˜4Ÿ>™>Ó*€KÜ  ×!9Ñ!9Ó:Ðä)¨)°_ÀdÇoÁoÔW`ÐagÓWhÓi€Kô 	Ø�{ M°4×3CÑ3CÀX×EYÑEYÔ[dÐekÓ[ló	ñ	ØØØØØØØØð 2=·±ÐASÔ1T��A�{ 8Ñ,Ñ-ÐZ^€Ið 9˜(×8Ñ8¸ÑVÈ+ÑVÑ€GˆZÜ Ð)9Ð;LÓM€GÜ" :Ð/?ÐARÓS€JÜ�{‰{Ð,¨i¯o©o¸aÑ.@ÈÔW\×WeÑWeÔf€HÙØ	Ô'¨
Ð@QÔRÐSØ	Ô)¨/ÀyÇ~Á~ÐVXÓGYÔZÐ[ØØØÜ! &Ó)õð .2¯]ª]ˆt×Ñ Ô)ÀÇÁÈHÓ@U€Hð ;˜H×:Ñ:¸8ÑSÀ{ÑSÑ€HˆkÜ
�+‰+Ð'¨¸FÌ%Ï.É.Ô
Y€CÙØ	Ô(¨ÐBSÔTÐUØ	Ô+Ð,=È;×K[ÑK[Ð\^ÓK_Ô`ÐaØØØÜ! &Ó)õô "ØØØØØØØØØ×Ñó
ð 
r&   c                óP  — t        d¬«      }t        | j                  j                  «      }t        | j                  j                  «      }t        | j
                  j                  «      j                  t        j                  «      j                  «       }t        | j                  j                  «      j                  t        j                  «      j                  «       }| j                  }| j                  }| j                  }|dz  dk7  s|dz  dk7  rt        d|› d|› d�«      ‚|j                  |j!                  «       d|z  |d	|¬
«      }	|j                  |j!                  «       ||d	|¬
«      }
|j#                  ||	f||
f«      \  \  }}	\  }}
t        j$                  j'                  |d¬«      | _        t        j$                  j'                  |	d¬«      | _        t        j$                  j'                  |d¬«      | _
        t        j$                  j'                  |
d¬«      | _        y)u!  One-shot pack + permute of an FP8Experts module's L1/L2 weights into the
    Mega MoE UTCCP layout. Called lazily on the first megamoe forward; idempotent
    via the caller's ``_megamoe_transformed`` flag.

    Steps:
      1. Cast UE8M0 SF â†’ FP32 and call ``transform_sf_into_required_layout`` â†’
         packed int32 in MN-major TMA-aligned layout.
      2. Run ``transform_weights_for_mega_moe``: interleaves gate/up on L1 and
         transposes both SFs for UTCCP.
      3. Overwrite the loader-side parameters in place; the interleave preserves
         the ``[E_local, 2*I, *]`` leading dims so downstream ``.size(...)`` reads
         stay valid.

    Unwraps any ``DTensor`` wrappers FSDP2/EP may have placed around the loader-
    side Parameters â€” the kernel takes raw pointers.
    Trr   r¨   r   zwDeepGEMM Mega MoE requires `hidden_dim` and `intermediate_hidden` divisible by 32 (FP8 SF granularity); got hidden_dim=z, intermediate_hidden=r;   r   )r   r¨   )r÷   Ú
num_groupsF)Úrequires_gradN)ry   r   r&  Údatar$  r  r„   rb   r®   r–   r  Úintermediate_dimrÂ   rï   rE   r   r‡   r   rº   Ú	Parameter)Úmodulerÿ   Úgate_up_sf_rawÚdown_sf_rawÚ	gate_up_wÚdown_wÚintermediate_hiddenÚnum_local_expertsrï   Ú
gate_up_sfÚdown_sfÚgate_upÚdowns                r'   Úsetup_megamoe_weightsrB  ê  s  € ô" $°4Ô8€HÜ˜f×;Ñ;×@Ñ@ÓA€NÜ˜6×5Ñ5×:Ñ:Ó;€Kä˜×,Ñ,×1Ñ1Ó2×7Ñ7¼¿
¹
ÓC×NÑNÓP€IÜ�f×&Ñ&×+Ñ+Ó,×1Ñ1´%·*±*Ó=×HÑHÓJ€Fà ×1Ñ1ÐØ×*Ñ*ÐØ×"Ñ"€Jà�B�˜!ÒÐ2°RÑ7¸1Ò<Üð4Ø4>°<Ð?UÐViÐUjÐjkðmó
ð 	
ð
 ×;Ñ;Ø×ÑÓØ	ÐÑØØØ$ð <ó €Jð ×8Ñ8Ø×ÑÓØØØØ$ð 9ó €Gð .6×-TÑ-TØ	�JÐØ	�Ðó.Ñ*Ñ€Wˆj™?˜D 'ô  Ÿ(™(×,Ñ,¨WÀEÐ,ÓJ€FÔÜ$)§H¡H×$6Ñ$6°zÐQVÐ$6Ó$W€FÔ!Ü—x‘x×)Ñ)¨$¸eÐ)ÓD€FÔÜ!&§¡×!3Ñ!3°GÈ5Ð!3Ó!Q€FÕr&   c                ó€  — t        | j                  «       | j                  j                  t        j
                  k7  r#t        d| j                  j                  › d�«      ‚|€t        d«      ‚t        d¬«      }t        | dd«      st        | «       d| _        |j                  d	«      }|j                  d
«      }|j                  d	«      }| j                  j                  d
«      }	| j                  j                  d«      dz  }
|	|j                  «       z  }t        | dd«      �| j                  j                  |k  r|j                  ||||||
¬«      | _        |j!                  |ddd¬«      \  }}| j                  j"                  d| j%                  |«       | j                  j&                  d| j%                  |«       | j                  j(                  d| j%                  |«       | j                  j*                  d| j%                  |«       t	        j,                  ||ft        j.                  |j0                  ¬«      }|j3                  || j                  | j4                  f| j6                  | j                  f| j                  t        t        | dd«      dd«      ¬«       |j9                  |j                  «      S )uï  FP8 acts Ã— FP4 weights Mega MoE forward (SM100+).

    Fuses EP dispatch + L1 + SwiGLU + L2 + EP combine into one kernel,
    overlapping NVLink with tensor-core compute. The kernel handles the full
    `(num_tokens, hidden) â†’ (num_tokens, hidden)` MoE forward including the
    weighted top-k reduction; the caller must NOT all-reduce the output.

    `process_group` is supplied automatically by `MoeTensorParalellExperts._prepare_input_fn`
    when the module is wrapped for TP â€” it's required for the symm-buffer rendezvous
    on first forward. `top_k_index` is GLOBAL expert ids (`-1` marks skipped slots).

    Caller-managed `self` attributes:
      - `gate_up_proj`, `gate_up_proj_scale_inv`: L1 weight + UE8M0 SF.
      - `down_proj`, `down_proj_scale_inv`: L2 weight + UE8M0 SF.
      Both pairs must be transformed together via
      `transform_weights_for_mega_moe((gate_up, gate_up_sf), (down, down_sf))`.
      - `config.swiglu_limit` (optional): SwiGLU clamp; absent â†’ unclamped.
    zJDeepGEMM Mega MoE requires FP4-packed expert weights (dtype=`int8`), got `z/`. Use the 'deepgemm' dispatch for FP8 experts.Nz©DeepGEMM Mega MoE requires a `process_group` for the EP group. The TP wrapping (MoeTensorParalellMegaMoeExperts) supplies it automatically; pass it explicitly otherwise.Trr   Ú_megamoe_transformedFr�   r   r   r   Úsymm_buffer)ÚhiddenÚnum_topkrÂ   Únum_max_tokens_per_rankr<  r¨   r©   r’   ÚconfigÚswiglu_limit)Úactivation_clamp)r‚   r$  r  r   rb   r®   r#  rE   ry   re   rB  rD  r”   rE  rH  r   r   rÐ   rœ   Úx_sfÚtopk_idxÚtopk_weightsrÏ   rù   r|   r   r&  r  ré   )r  rÜ   rÝ   rÞ   Úprocess_grouprÿ   rß   rÅ   rï   r=  r<  Únum_global_expertsÚx_fp8rL  Úys                  r'   Ú(deepgemm_fp8_fp4_megamoe_experts_forwardrS  $  sž  € ô2 # 4×#;Ñ#;Ô<à×Ñ×Ñ¤%§*¡*Ò,ÜðØ×!Ñ!×'Ñ'Ð(Ð(WðYó
ð 	
ð
 ÐÜðió
ð 	
ô
 $°4Ô8€Hô �4Ð/°Ô7Ü˜dÔ#Ø$(ˆÔ!à× Ñ  Ó$€IØ×#Ñ# AÓ&€JØ×#Ñ# BÓ'€JØ×)Ñ)×.Ñ.¨qÓ1ÐØ×+Ñ+×0Ñ0°Ó3°qÑ8ÐØ*¨]×-?Ñ-?Ó-AÑAÐô ˆt�] DÓ)Ð1°T×5EÑ5E×5]Ñ5]Ð`jÒ5jØ#×@Ñ@ØØØØ*Ø$.Ø 3ð Aó 
ˆÔð ×0Ñ0°È$ÐWYÐlpÐ0Óq�K€Eˆ4Ø×Ñ×Ñ�{˜
Ð#×)Ñ)¨%Ô0Ø×Ñ×Ñ˜+˜:Ð&×,Ñ,¨TÔ2Ø×Ñ×Ñ˜k˜zÐ*×0Ñ0°Ô=Ø×Ñ×!Ñ! + :Ð.×4Ñ4°]ÔCô 	�‰�Z Ð,´E·N±NÈ=×K_ÑK_Ô`€AØ×ÑØ	Ø	×	Ñ	˜D×7Ñ7Ð8Ø	�‰˜×1Ñ1Ð2Ø×ÑÜ ¤¨¨x¸Ó!>ÀÐPTÓUð ô ð �4‰4�×#Ñ#Ó$Ð$r&   )Úreturnz
str | None)rT  útuple[int, int] | None)F)rf   ÚboolrT  zDeepGEMM | str)rf   rV  rT  ÚNone)rf   rV  rT  r   )r|   ztorch.devicerT  rV  )r�   útorch.TensorrT  rW  )rˆ   rX  rT  rX  rÔ   )rˆ   rX  r�   z
int | NonerT  rX  )
r¯   rX  r°   rX  r±   ztuple | Nonerž   rV  rT  Údict)
rÁ   rX  rÂ   r   rÃ   r   rÄ   rV  rT  z&tuple[torch.Tensor, torch.Tensor, int])rÐ   rX  rË   rX  rÈ   r   rT  rX  )rÕ   rX  rË   rX  rT  rX  )rÜ   rX  rÝ   rX  rÞ   rX  rÂ   r   r   r   rÄ   rV  rT  r˜   )rí   rX  rî   rX  ræ   rX  rã   rX  rË   rX  rÅ   r   rß   r   rï   r   rð   ztorch.dtyperT  rX  )NNNN)rü   rX  r¯   rX  r°   rX  rý   útorch.Tensor | Noner±   rU  rô   ztorch.dtype | Nonerþ   rZ  rT  rX  )
r  útorch.nn.ModulerÜ   rX  rÝ   rX  rÞ   rX  rT  rX  )r7  r[  rT  rW  )r  r[  rÜ   rX  rÝ   rX  rÞ   rX  rO  z%torch.distributed.ProcessGroup | NonerT  rX  )6r#   Ú
__future__r   Ú	functoolsrA   r,   rG   r/   Úcollections.abcr   Údataclassesr   rb   Úutilsr   Úutils.deprecationr   Úutils.import_utilsr	   r
   r   r   r   Úhub_kernelsr   Útensor_parallelr   Ú
get_loggerr    Úloggerr   Úcacher5   rT   rp   Ú_dynamoÚallow_in_graphrs   ry   r}   r‚   rŠ   r¦   r²   rÍ   rÒ   rÖ   rç   ró   r  r  r0  rB  rS  r%   r&   r'   ú<module>rj     s[  ðñ
õ #ã Û Û 	Û 	Û Ý $Ý !ã å Ý /÷õ õ *Ý %ð 
ˆ×	Ñ	˜HÓ	%€ñ
 �$Ô÷ð ó ðð, ‡�òó ðð$ ‡�ò+ó ð+ð\ ‡�ópó ðpðf ‡�×Ñó9ó ð9ôð ‡�ò=ó ð=óó,
Yô:ðz/Øð/Ø,8ð/ØFRð/Ø^bð/à	ó/ð>?Ø#ð?Ø25ð?ØBEð?ØX\ð?à+ó?óDó&ð/Øð/àð/ð  ð/ð ð	/ð
 ð/ð ð/ð ó/ðd_Øð_à ð_ð  ð_ð ð	_ð
 #ð_ð ð_ð ð_ð ð_ð ð_ð ó_ñ4 �¨Ô1ð
 !%Ø)-Ø'+Ø,0ð&Øð&àð&ð #ð&ð ð	&ð
 'ð&ð %ð&ð *ð&ð ò&ó 2ð&ðR>Ø
ð>àð>ð ð>ð  ð	>ð
 ó>ðBYØ
ðYàðYð ðYð  ð	Yð
 óYóx7Rð~ <@ðS%Ø
ðS%àðS%ð ðS%ð  ð	S%ð
 9ðS%ð ôS%r&   