Ë
    ÛÓ^jŽ  ã                  ól  — d Z ddlm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  e
j                  e«      ZddddœZ ed¬«       G d„ d«      «       Zej(                  dd„«       Zej,                  j.                  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       Z	 	 	 	 	 	 	 	 	 	 dd„Zy)zõSonicMoE integration: fused MoE using CuteDSL kernels from `kernels-community/sonic-moe`.

Provides `sonicmoe_experts_forward` registered as "sonicmoe" in the ExpertsInterface.
Requirements: CUDA, `kernels`, `nvidia-cutlass-dsl`, has_gate=True.
é    )ÚannotationsN)ÚCallable)Ú	dataclassé   )Úloggingé   )Úlazy_load_kernel)Úto_localÚswigluÚgegluÚreglu)ÚsiluÚgeluÚreluT)Úfrozenc                  ó&   — e Zd ZU dZded<   ded<   y)ÚSonicMoEzAEntry points exposed by the `kernels-community/sonic-moe` kernel.ÚtypeÚactivation_type_enumr   Úmoe_general_routing_inputsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© ó    úm/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/integrations/sonicmoe.pyr   r   (   s   … áKàÓØ (Ô(r   r   c                 óÖ  — t         j                  j                  «       st        d«      ‚t         j                  j	                  «       d   } | dk  rt        d| › d�«      ‚t        d«      }|€t        d«      ‚t        t        |d	d«      d
d«      }t        |dd«      }d|fd|ffD ��cg c]	  \  }}|€|‘Œ }}}|rt        ddj                  |«      › d�«      ‚t        ||¬«      S c c}}w )z·
    Load sonic-moe once and return its entry points.

    Raises `ImportError` if CUDA/hardware requirements are not met, or if the kernel or
    required symbols are not found.
    zdsonic-moe kernel requires CUDA, but CUDA is not available. Use a different `experts_implementation`.r   é	   z`sonic-moe requires a Hopper (SM90+) or newer GPU, but the current device has compute capability z-.x. Use a different `experts_implementation`.z	sonic-moeNu}   Failed to load the sonic-moe kernel â€” check that `kernels-community/sonic-moe` has a build matching the current torch/CUDA.ÚenumsÚActivationTyper   zenums.ActivationTypez.sonic-moe kernel is missing required symbols: z, zN. Make sure you have the `kernels` package and `nvidia-cutlass-dsl` installed.)r   r   )	ÚtorchÚcudaÚis_availableÚImportErrorÚget_device_capabilityr	   ÚgetattrÚjoinr   )ÚmajorÚkernelr   r   ÚnameÚattrÚmissings          r   Ú_load_sonicmoe_kernelr/   0   s;  € ô �:‰:×"Ñ"Ô$ÜØró
ð 	
ô
 �J‰J×,Ñ,Ó.¨qÑ1€EØˆq‚yÜð&Ø&+ WÐ,Yð[ó
ð 	
ô
 ˜kÓ*€FØ€~Üð;ó
ð 	
ô
 #¤7¨6°7¸DÓ#AÐCSÐUYÓZÐÜ!(¨Ð1MÈtÓ!TÐð
 $Ð%9Ð:Ø)Ð+EÐFð
÷áˆD�$ð ˆ<ò 	ð€Gñ ñ ÜØ<¸T¿Y¹YÀwÓ=OÐ<Pð Q[ð [ó
ð 	
ô
 Ø1Ø#=ôð ùós   Â'C%c                óæ   — t        «       }|j                  }t        |t        j	                  |d«      j                  «       |j                  «      }|j                  | ||||||||	|||
d¬«      \  }}|S )us  Module-level shim around `moe_general_routing_inputs` so `allow_in_graph` can wrap it.

    sonicmoe asserts `not torch.compiler.is_compiling()` internally because it dispatches
    CuteDSL kernels, which Dynamo can't trace. `allow_in_graph` keeps the call in the FX
    graph as a single opaque node (no tracing into the body, no graph break) while still
    running the real Python at runtime â€” autograd through `_UpProjection` / `_DownProjection`
    flows normally. The decorator must be applied at module load time, not inside the compiled
    function â€” hence this shim plus the `allow_in_graph` decorator above.
    r   N)ÚEÚactivation_typeÚis_inference_mode_enabledÚconcat_layoutÚ	stream_id)r/   r   r(   ÚACT_MAPÚgetÚupperÚSWIGLUr   )Úhidden_statesÚrouter_scoresÚ
expert_idsÚ	token_idxÚw1Úb1Úw2Úb2Úact_nameÚnum_expertsr4   r3   Úsonicmoer   r2   ÚoutputÚ_s                    r   Ú_sonicmoe_wrapperrG   d   s�   € ô0 %Ó&€HØ#×8Ñ8ÐÜØœgŸk™k¨(°HÓ=×CÑCÓEÐG[×GbÑGbó€Oð ×3Ñ3ØØØØØ
Ø
Ø
Ø
Ø
Ø'Ø";Ø#Øð 4ó �I€FˆAð €Mr   c                ó  — | j                   st        d«      ‚|j                  j                  dk7  rt        d«      ‚|j                  }|j	                  d«      }|j	                  d«      }t        j                  ||¬«      j                  d«      j                  d|«      j                  d«      j                  «       }|j                  d«      j                  |j                  «      }|j                  d«      j                  «       }	t        | j                  «      }
t        | j                  «      }| j                   rt        | j"                  «      nd }| j                   rt        | j$                  «      nd }t'        | j(                  dd	«      j+                  «       }| j,                  rd
nd} |
j.                  |Ž }
 |j.                  |Ž }t1        |||	||
||||| j2                  | j4                  t        j6                  «        ¬«      S )Nz/sonicmoe requires gated experts (has_gate=True)r$   zsonicmoe requires CUDA deviceéÿÿÿÿr   )Údevicer   Ú
hidden_actr   )r   r   r   )r   r   r   )r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   r4   r3   )Úhas_gateÚ
ValueErrorrJ   r   Úsizer#   ÚarangeÚ	unsqueezeÚexpandÚreshapeÚintÚtoÚdtyper
   Úgate_up_projÚ	down_projÚhas_biasÚgate_up_proj_biasÚdown_proj_biasr(   ÚconfigÚlowerÚis_transposedÚpermuterG   rC   Úis_concatenatedÚis_grad_enabled)Úselfr:   Útop_k_indexÚtop_k_weightsrJ   Ú	num_top_kÚ
num_tokensr=   r;   r<   r>   r@   r?   rA   rB   Úperms                   r   Úsonicmoe_experts_forwardrg   “   sÃ  € ð �=Š=ÜÐJÓKÐKØ×Ñ× Ñ  FÒ*ÜÐ8Ó9Ð9à×!Ñ!€FØ× Ñ  Ó$€IØ×#Ñ# AÓ&€Jô —‘˜Z°Ô7×AÑAÀ!ÓD×KÑKÈBÐPYÓZ×bÑbÐceÓf×jÑjÓl€IØ!×)Ñ)¨"Ó-×0Ñ0°×1DÑ1DÓE€MØ×$Ñ$ RÓ(×,Ñ,Ó.€Jô 
�$×#Ñ#Ó	$€BÜ	�$—.‘.Ó	!€BØ-1¯]ª]Œ�$×(Ñ(Ô	)À€BØ*.¯-ª-Œ�$×%Ñ%Ô	&¸T€Bô �t—{‘{ L°&Ó9×?Ñ?ÓA€Hð ×*Ò*‰9°	€DØ	ˆ�‰�TÐ	€BØ	ˆ�‰�TÐ	€BäØ#Ø#ØØØØØØØØ×$Ñ$Ø×*Ñ*Ü&+×&;Ñ&;Ó&=Ð"=ôð r   )Úreturnr   )r:   útorch.Tensorr;   ri   r<   ri   r=   ri   r>   ri   r?   útorch.Tensor | Noner@   ri   rA   rj   rB   ÚstrrC   rS   r4   Úboolr3   rl   rh   ri   )
ra   ztorch.nn.Moduler:   ri   rb   ri   rc   ri   rh   ri   )r   Ú
__future__r   Ú	functoolsÚcollections.abcr   Údataclassesr   r#   Úutilsr   Úhub_kernelsr	   Útensor_parallelr
   Ú
get_loggerr   Úloggerr6   r   Úcacher/   Ú_dynamoÚallow_in_graphrG   rg   r   r   r   ú<module>ry      sM  ðñõ #ã Ý $Ý !ã å Ý )Ý %ð 
ˆ×	Ñ	˜HÓ	%€ð  W°gÑ
>€ñ �$Ô÷)ð )ó ð)ð ‡�ò0ó ð0ðf ‡�×Ñð+Øð+àð+ð ð+ð ð	+ð
 	ð+ð 	ð+ð 	ð+ð 	ð+ð ð+ð ð+ð ð+ð  $ð+ð ò+ó ð+ð\5Ø
ð5àð5ð ð5ð  ð	5ð
 ô5r   