§
    ‚ŠtjŽ  ã                  ó"  — 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dS )/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<   dS )ÚSonicMoEzAEntry points exposed by the `kernels-community/sonic-moe` kernel.ÚtypeÚactivation_type_enumr   Úmoe_general_routing_inputsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__© ó    ú`/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/integrations/sonicmoe.pyr   r   (   s0   € € € € € € àKÐKàÐÐÑØ(Ð(Ð(Ñ(Ð(Ð(r   r   Úreturnc                 ó  — t           j                             ¦   «         st          d¦  «        ‚t           j                             ¦   «         d         } | dk     rt          d| › d�¦  «        ‚t          d¦  «        }|€t          d¦  «        ‚t          t          |d	d¦  «        d
d¦  «        }t          |dd¦  «        }d„ d|fd|ffD ¦   «         }|r&t          dd                     |¦  «        › d�¦  «        ‚t          ||¬¦  «        S )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   c                ó   — g | ]	\  }}|­|‘Œ
S )Nr   )Ú.0ÚnameÚattrs      r   ú
<listcomp>z)_load_sonicmoe_kernel.<locals>.<listcomp>P   s-   € ð ð ð áˆD�$ð ˆ<ð 	ð
 ˆ<ˆ<r   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   Úmissings        r   Ú_load_sonicmoe_kernelr3   0   st  € õ Œ:×"Ò"Ñ$Ô$ð 
ÝØrñ
ô 
ð 	
õ
 ŒJ×,Ò,Ñ.Ô.¨qÔ1€EØˆq‚y€yÝð[Ø&+ð[ð [ð [ñ
ô 
ð 	
õ
 ˜kÑ*Ô*€FØ€~Ýð;ñ
ô 
ð 	
õ
 #¥7¨6°7¸DÑ#AÔ#AÐCSÐUYÑZÔZÐÝ!(¨Ð1MÈtÑ!TÔ!TÐðð ð $Ð%9Ð:Ø)Ð+EÐFð
ðñ ô €Gð ð 
Ýð[¸T¿YºYÀwÑ=OÔ=Oð [ð [ð [ñ
ô 
ð 	
õ
 Ø1Ø#=ðñ ô ð r   Úhidden_statesútorch.TensorÚrouter_scoresÚ
expert_idsÚ	token_idxÚw1Úb1útorch.Tensor | NoneÚw2Úb2Úact_nameÚstrÚnum_expertsÚintÚconcat_layoutÚboolÚis_inference_mode_enabledc                óü   — t          ¦   «         }|j        }t          |t                               |d¦  «                             ¦   «         |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_typerD   rB   Ú	stream_id)r3   r   r.   ÚACT_MAPÚgetÚupperÚSWIGLUr   )r4   r6   r7   r8   r9   r:   r<   r=   r>   r@   rB   rD   Úsonicmoer   rG   ÚoutputÚ_s                    r   Ú_sonicmoe_wrapperrP   d   sš   € õ0 %Ñ&Ô&€HØ#Ô8ÐÝØ�gŸkšk¨(°HÑ=Ô=×CÒCÑEÔEÐG[ÔGbñô €Oð ×3Ò3ØØØØØ
Ø
Ø
Ø
Ø
Ø'Ø";Ø#Øð 4ñ ô �I€FˆAð €Mr   Úselfútorch.nn.ModuleÚtop_k_indexÚtop_k_weightsc                óú  — | j         st          d¦  «        ‚|j        j        dk    rt          d¦  «        ‚|j        }|                     d¦  «        }|                     d¦  «        }t          j        ||¬¦  «                             d¦  «                             d|¦  «         	                    d¦  «         
                    ¦   «         }| 	                    d¦  «                             |j        ¦  «        }| 	                    d¦  «         
                    ¦   «         }	t          | j        ¦  «        }
t          | j        ¦  «        }| j        rt          | j        ¦  «        nd }| j        rt          | j        ¦  «        nd }t'          | j        dd	¦  «                             ¦   «         }| j        rd
nd} |
j        |Ž }
 |j        |Ž }t1          |||	||
||||| j        | j        t          j        ¦   «          ¬¦  «        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   )r4   r6   r7   r8   r9   r:   r<   r=   r>   r@   rB   rD   )Úhas_gateÚ
ValueErrorrW   r   Úsizer)   ÚarangeÚ	unsqueezeÚexpandÚreshaperA   ÚtoÚdtyper
   Úgate_up_projÚ	down_projÚhas_biasÚgate_up_proj_biasÚdown_proj_biasr.   ÚconfigÚlowerÚis_transposedÚpermuterP   r@   Úis_concatenatedÚis_grad_enabled)rQ   r4   rS   rT   rW   Ú	num_top_kÚ
num_tokensr8   r6   r7   r9   r<   r:   r=   r>   Úperms                   r   Úsonicmoe_experts_forwardrp   “   sõ  € ð Œ=ð LÝÐJÑKÔKÐKØÔÔ  FÒ*Ð*ÝÐ8Ñ9Ô9Ð9àÔ!€FØ× Ò  Ñ$Ô$€IØ×#Ò# AÑ&Ô&€Jõ ”˜Z°Ð7Ñ7Ô7×AÒAÀ!ÑDÔD×KÒKÈBÐPYÑZÔZ×bÒbÐceÑfÔf×jÒjÑlÔl€IØ!×)Ò)¨"Ñ-Ô-×0Ò0°Ô1DÑEÔE€MØ×$Ò$ RÑ(Ô(×,Ò,Ñ.Ô.€Jõ 
�$Ô#Ñ	$Ô	$€BÝ	�$”.Ñ	!Ô	!€BØ-1¬]Ð	D��$Ô(Ñ	)Ô	)Ð	)À€BØ*.¬-Ð	A��$Ô%Ñ	&Ô	&Ð	&¸T€Bõ �t”{ L°&Ñ9Ô9×?Ò?ÑAÔA€Hð Ô*Ð9ˆ9ˆ9°	€DØ	ˆŒ�TÐ	€BØ	ˆŒ�TÐ	€BåØ#Ø#ØØØØØØØØÔ$ØÔ*Ý&+Ô&;Ñ&=Ô&=Ð"=ðñ ô ð r   )r   r   )r4   r5   r6   r5   r7   r5   r8   r5   r9   r5   r:   r;   r<   r5   r=   r;   r>   r?   r@   rA   rB   rC   rD   rC   r   r5   )
rQ   rR   r4   r5   rS   r5   rT   r5   r   r5   )r   Ú
__future__r   Ú	functoolsÚcollections.abcr   Údataclassesr   r)   Úutilsr   Úhub_kernelsr	   Útensor_parallelr
   Ú
get_loggerr   ÚloggerrI   r   Úcacher3   Ú_dynamoÚallow_in_graphrP   rp   r   r   r   ú<module>r}      sa  ððð ð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $Ø !Ð !Ð !Ð !Ð !Ð !à €€€à Ð Ð Ð Ð Ð Ø )Ð )Ð )Ð )Ð )Ð )Ø %Ð %Ð %Ð %Ð %Ð %ð 
ˆÔ	˜HÑ	%Ô	%€ð  W°gÐ
>Ð
>€ð €�$ÐÑÔð)ð )ð )ð )ð )ñ )ô )ñ Ôð)ð „ð0ð 0ð 0ñ „ð0ðf „Ôð+ð +ð +ñ Ôð+ð\5ð 5ð 5ð 5ð 5ð 5r   