
    ^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)zSonicMoE 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
 	

 JJ,,.q1Eqy&&+W,Y[
 	

 k*F~;
 	

 #767D#ACSUYZ!(1Mt!T
 $%9:)+EF
D$ < 	G  <TYYw=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gkk(H=CCEG[GbGbO 33




'";# 4 IFA 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 devicer   )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KBPYZbbcefjjlI!))"-001D1DEM$$R(,,.J 
$##	$B	$..	!B-1]]$((	)B*.--$%%	&TB t{{L&9??AH **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	% Wg
> $) ) ) 0 0f +++ + 	+
 	+ 	+ 	+ 	+ + + +  $+ + +\5
55 5  	5
 5r   