ó
    pyüi-  ã                  ó`  • S r SSKJr  SSKrSSKJr  SSKJr  SSKrSSK	J
r
  SS	KJr  \
R                  " \5      rS
SSS.r\" SS9 " S S5      5       r\R$                  SS j5       r\R(                  R*                                            SS j5       r          SS jrg)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ÚswigluÚgegluÚreglu)ÚsiluÚgeluÚreluT)Úfrozenc                  ó.   • \ rS rSr% SrS\S'   S\S'   Srg)	ÚSonicMoEé'   zAEntry points exposed by the `kernels-community/sonic-moe` kernel.ÚtypeÚactivation_type_enumr   Úmoe_general_routing_inputs© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__Ú__static_attributes__r   ó    Ú_/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/integrations/sonicmoe.pyr   r   '   s   ‡ áKàÓØ (Ö(r   r   c                 óò  • [         R                  R                  5       (       d  [        S5      e[         R                  R	                  5       S   n U S:  a  [        SU  S35      e[        S5      nUc  [        S5      e[        [        US	S5      S
S5      n[        USS5      nSU4SU44 VVs/ s H  u  pEUb  M
  UPM     nnnU(       a  [        SSR                  U5       S35      e[        UUS9$ s  snnf )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_kernelr1   /   sA  € ô �:‰:×"Ñ"×$Ñ$ÜØ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ñ
ôò
‰JˆDð ÷ 	ñ
ð ñ ö ÜØ<¸T¿Y¹YÀwÓ=OÐ<Pð Q[ð [ó
ð 	
ô
 Ø1Ø#=ñð ùós   Â.	C3Â;C3c                óà   • [        5       nUR                  n[        U[        R	                  US5      R                  5       UR                  5      nUR                  U UUUUUUUU	UUU
SS9u  nnU$ )uW  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)r1   r   r*   ÚACT_MAPÚgetÚupperÚSWIGLUr   )Úhidden_statesÚrouter_scoresÚ
expert_idsÚ	token_idxÚw1Úb1Úw2Úb2Úact_nameÚnum_expertsr6   r5   Úsonicmoer   r4   ÚoutputÚ_s                    r    Ú_sonicmoe_wrapperrI   c   s�   € ô0 %Ó&€HØ#×8Ñ8ÐÜØœgŸk™k¨(°HÓ=×CÑCÓEÐG[×GbÑGbó€Oð ×3Ñ3ØØØØØ
Ø
Ø
Ø
Ø
Ø'Ø";Ø#Øð 4ð �I€FˆAð €Mr   c                óè  • U R                   (       d  [        S5      eUR                  R                  S:w  a  [        S5      eUR                  nUR	                  S5      nUR	                  S5      n[
        R                  " XdS9R                  S5      R                  SU5      R                  S5      R                  5       nUR                  S5      R                  UR                  5      nUR                  S5      R                  5       n	U R                  n
U R                  nU R                  (       a  U R                   OS nU R                  (       a  U R"                  OS n[%        U
[
        R&                  R(                  R*                  5      (       aJ  U
R-                  5       n
UR-                  5       nUb  UR-                  5       OS nUb  UR-                  5       OS n[/        U R0                  SS	5      R3                  5       nU R4                  (       a  S
OSnU
R6                  " U6 n
UR6                  " U6 n[9        UUU	UU
UUUUU R:                  U R<                  [
        R>                  " 5       (       + S9$ )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@   rA   rB   rC   rD   rE   r6   r5   ) Úhas_gateÚ
ValueErrorrL   r   Úsizer%   ÚarangeÚ	unsqueezeÚexpandÚreshapeÚintÚtoÚdtypeÚgate_up_projÚ	down_projÚhas_biasÚgate_up_proj_biasÚdown_proj_biasÚ
isinstanceÚdistributedÚtensorÚDTensorÚto_localr*   ÚconfigÚlowerÚis_transposedÚpermuterI   rE   Úis_concatenatedÚis_grad_enabled)Úselfr<   Útop_k_indexÚtop_k_weightsrL   Ú	num_top_kÚ
num_tokensr?   r=   r>   r@   rB   rA   rC   rD   Úperms                   r    Úsonicmoe_experts_forwardrn   ’   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Ø#'§=§=ˆ×	Ò	°d€BØ $§§ˆ×	Ò	°4€BÜ�"”e×'Ñ'×.Ñ.×6Ñ6×7Ñ7Ø�[‰[‹]ˆØ�[‰[‹]ˆØ ™nˆR�[‰[Œ]°$ˆØ ™nˆR�[‰[Œ]°$ˆô �t—{‘{ L°&Ó9×?Ñ?ÓA€Hð ×*×*‰9°	€DØ	�Š�TÐ	€BØ	�Š�TÐ	€BäØ#Ø#ØØØØØØØØ×$Ñ$Ø×*Ñ*Ü&+×&;Ò&;Ó&=Ô"=ñð r   )Úreturnr   )r<   útorch.Tensorr=   rp   r>   rp   r?   rp   r@   rp   rA   útorch.Tensor | NonerB   rp   rC   rq   rD   ÚstrrE   rU   r6   Úboolr5   rs   ro   rp   )
rh   ztorch.nn.Moduler<   rp   ri   rp   rj   rp   ro   rp   )r   Ú
__future__r   Ú	functoolsÚcollections.abcr   Údataclassesr   r%   Úutilsr   Úhub_kernelsr	   Ú
get_loggerr   Úloggerr8   r   Úcacher1   Ú_dynamoÚallow_in_graphrI   rn   r   r   r    Ú<module>r      sH  ðñõ #ã Ý $Ý !ã å Ý )ð 
×	Ò	˜HÓ	%€ð  W°gÑ
>€ñ �$Ñ÷)ð )ó ð)ð ‡�ó0ó ð0ðf ‡�×Ñð+Øð+àð+ð ð+ð ð	+ð
 	ð+ð 	ð+ð 	ð+ð 	ð+ð ð+ð ð+ð ð+ð  $ð+ð ó+ó ð+ð\=Ø
ð=àð=ð ð=ð  ð	=ð
 õ=r   