§
    9²¸jY$  ã                  ó`  — U 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mZ d
dlmZ  ej        e¦  «        ZddddœZdddœZe	j        j        d(d)d„¦   «         Z ed¬¦  «         G d„ d¦  «        ¦   «         Zdaded<   e	j        j        d*d„¦   «         Z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)Úversioné   )Úlogging)Úis_kernels_availableÚmaybe_import_erroré   )Úlazy_load_kernelÚswigluÚgegluÚreglu)ÚsiluÚgeluÚreluz4.6.0z0.1.10)znvidia-cutlass-dslzapache-tvm-ffiFÚraise_errorÚboolÚreturnc           
     ó�  — t          ¦   «         st          d| ¬¦  «        S t          j                             ¦   «         st          d| ¬¦  «        S t          j                             ¦   «         d         }|dk     rt          d|› d�| ¬¦  «        S t                               ¦   «         D ]ž\  }}	 t          j	         
                    |¦  «        }n1# t          j	        j        $ r t          d|› d	�| ¬¦  «        cY c S w xY wt          j        |¦  «        t          j        |¦  «        k     rt          d|› d
|› d|› d�| ¬¦  «        c S ŒŸdS )u+  Whether the sonic-moe kernel can be loaded in this environment: `kernels` installed, a CUDA GPU on
    Hopper (SM90) or newer, and `nvidia-cutlass-dsl` / `apache-tvm-ffi` no newer than the versions the
    kernel was validated against (`SONICMOE_DEPENDENCIES`) â€” newer releases may break its CuteDSL / TVM
    FFI dispatch. A one-glance gate for callers, including external stacks. With `raise_error=True` (used
    by the loader) it re-raises the specific `ImportError` instead of returning `False`.

    `@assume_constant_result` makes dynamo evaluate this once at trace time and inline the bool (its probe
    is untraceable â€” an `lru_cache`'d import check, the `get_device_capability` pybind, and
    `importlib.metadata` version lookups), so it stays compile-safe if called from a compiled region.
    zSsonic-moe requires the `kernels` package. Use a different `experts_implementation`.©r   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-moe requires `z`, but it is not installed.z` >= z, but z is installed.T)r	   r
   ÚtorchÚcudaÚis_availableÚget_device_capabilityÚSONICMOE_DEPENDENCIESÚitemsÚ	importlibÚmetadatar   ÚPackageNotFoundErrorÚparse)r   ÚmajorÚdistributionÚmin_versionÚ	installeds        úr/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/integrations/sonicmoe.pyÚis_sonicmoe_loadabler(   -   sÈ  € õ  Ñ!Ô!ð 
Ý!ØaØ#ð
ñ 
ô 
ð 	
õ Œ:×"Ò"Ñ$Ô$ð 
Ý!ØrØ#ð
ñ 
ô 
ð 	
õ ŒJ×,Ò,Ñ.Ô.¨qÔ1€EØˆq‚y€yÝ!ðOØðOð Oð Oà#ð
ñ 
ô 
ð 	
õ
 &;×%@Ò%@Ñ%BÔ%Bð ð Ñ!ˆ�kð
	Ý!Ô*×2Ò2°<Ñ@Ô@ˆIˆIøÝÔ!Ô6ð 	ð 	ð 	Ý%ØP |ÐPÐPÐPÐ^iðñ ô ð ð ð ð ð ð	øøøõ Œ=˜Ñ#Ô#¥g¤m°KÑ&@Ô&@Ò@Ð@Ý%Øf |ÐfÐf¸+ÐfÐfÈYÐfÐfÐfØ'ðñ ô ð ð ð ð Að
 ˆ4s   Â-CÃ)C;Ã:C;T)Ú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__© ó    r'   r+   r+   ]   s0   € € € € € € àKÐKàÐÐÑØ(Ð(Ð(Ñ(Ð(Ð(r5   r+   zSonicMoE | NoneÚ	_SONICMOEÚNonec                 óp  — t           �dS t          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          ||¬¦  «        a dS )aÈ  
    Load sonic-moe once into the `_SONICMOE` module global.

    `@allow_in_graph` makes `torch.compile` treat the untraceable hub download + dynamic import as a
    single opaque node instead of tracing into it; it returns `None` (proxyable) and populates the
    global, which `load_sonicmoe_kernel` then returns.

    Under NO circumstances may this function return a value: an `@allow_in_graph` fx node's
    return must be proxyable, and returning the bundle (e.g. from the warm-cache
    short-circuit) breaks torch.compile with `Unsupported: torch.* op returned non-Tensor`.

    Raises `ImportError` if CUDA/hardware requirements are not met, or if the kernel or
    required symbols are not found.
    NTr   z	sonic-moeu}   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 ©Nr4   )Ú.0ÚnameÚattrs      r'   ú
<listcomp>z)_load_sonicmoe_kernel.<locals>.<listcomp>‰   s-   € ð ð ð áˆD�$ð ˆ<ð 	ð
 ˆ<ˆ<r5   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.   )r6   r(   r   ÚImportErrorÚgetattrÚjoinr+   )Úkernelr-   r.   Úmissings       r'   Ú_load_sonicmoe_kernelrF   k   s  € õ" ÐØˆÝ TÐ*Ñ*Ô*Ð*Ý˜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Ø#=ðñ ô €I€I€Ir5   c                 ó,   — t          ¦   «          t          S r<   )rF   r6   r4   r5   r'   Úload_sonicmoe_kernelrH   �   s   € ÝÑÔÐÝÐr5   Úselfútorch.nn.ModuleÚhidden_statesútorch.TensorÚtop_k_indexÚtop_k_weightsc                óJ  — | j         st          d¦  «        ‚|j        }|                     d¦  «        }|                     d¦  «        }t	          j        ||¬¦  «                             d¦  «                             d|¦  «                             d¦  «         	                    ¦   «         }|                     d¦  «         
                    |j        ¦  «        }|                     d¦  «         	                    ¦   «         }	| j        }
| j        }| j        r| j        nd }| j        r| j        nd }t#          | j        dd¦  «                             ¦   «         }|t(          vr(t          d|›d	t+          t(          ¦  «        › d
�¦  «        ‚| j        rdnd} |
j        |Ž }
 |j        |Ž }t1          ¦   «         }|                     ||||	|
|||| j        t#          |j        t(          |                              ¦   «         ¦  «        t	          j        ¦   «          | j        d ¬¦  «        \  }}|S )Nz/sonicmoe requires gated experts (has_gate=True)éÿÿÿÿr   )Údevicer   Ú
hidden_actr   zsonicmoe does not support the z activation; expected one of ú.)r   r   r   )r   r   r   )ÚEÚactivation_typeÚis_inference_mode_enabledÚconcat_layoutÚ	stream_id)Úhas_gateÚ
ValueErrorrQ   Úsizer   ÚarangeÚ	unsqueezeÚexpandÚreshapeÚintÚtoÚdtypeÚgate_up_projÚ	down_projÚhas_biasÚgate_up_proj_biasÚdown_proj_biasrB   ÚconfigÚlowerÚACT_MAPÚsortedÚis_transposedÚpermuterH   r.   Únum_expertsr-   ÚupperÚis_grad_enabledÚis_concatenated)rI   rK   rM   rN   rQ   Ú	num_top_kÚ
num_tokensÚ	token_idxÚrouter_scoresÚ
expert_idsÚw1Úw2Úb1Úb2Úact_nameÚpermÚsonicmoeÚoutputÚ_s                      r'   Úsonicmoe_experts_forwardr€   ¢   s&  € ð Œ=ð LÝÐJÑKÔKÐKàÔ!€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Ø#'¤=Ð	:ˆÔ	Ð	°d€BØ $¤Ð	7ˆÔ	Ð	°4€Bõ �t”{ L°&Ñ9Ô9×?Ò?ÑAÔA€HØ•wÐÐÝÐu¸(ÐuÐuÕciÕjqÑcrÔcrÐuÐuÐuÑvÔvÐvð
 Ô*Ð9ˆ9ˆ9°	€DØ	ˆŒ�TÐ	€BØ	ˆŒ�TÐ	€Bå#Ñ%Ô%€HØ×3Ò3ØØØØØ
Ø
Ø
Ø
Ø
Ô
Ý Ô =½wÀxÔ?P×?VÒ?VÑ?XÔ?XÑYÔYÝ&+Ô&;Ñ&=Ô&=Ð"=ØÔ*Øð 4ñ ô �I€FˆAð €Mr5   )F)r   r   r   r   )r   r7   )r   r+   )
rI   rJ   rK   rL   rM   rL   rN   rL   r   rL   )"r2   Ú
__future__r   Úimportlib.metadatar   Úcollections.abcr   Údataclassesr   r   Ú	packagingr   Úutilsr   Úutils.import_utilsr	   r
   Úhub_kernelsr   Ú
get_loggerr/   Úloggerrj   r   Ú_dynamoÚassume_constant_resultr(   r+   r6   r3   Úallow_in_graphrF   rH   r€   r4   r5   r'   ú<module>rŽ      s¾  ððð ð ð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $Ø !Ð !Ð !Ð !Ð !Ð !à €€€Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ð Ð Ø IÐ IÐ IÐ IÐ IÐ IÐ IÐ IØ )Ð )Ð )Ð )Ð )Ð )ð 
ˆÔ	˜HÑ	%Ô	%€ð  W°gÐ
>Ð
>€ð 07È(ÐSÐSÐ ð „Ô%ð,ð ,ð ,ð ,ñ &Ô%ð,ð^ €�$ÐÑÔð)ð )ð )ð )ð )ñ )ô )ñ Ôð)ð "€	Ð !Ð !Ð !Ñ !ð „Ôð.ð .ð .ñ Ôð.ðbð ð ð ð
9ð 9ð 9ð 9ð 9ð 9r5   