
    #jb              
           d Z ddlZddlZddlmZ ddlmZ  ej        e          Z	ddd	d
Z
ej        d             Zdej        j        dej        dej        dej        dej        f
dZdS )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.
    N   )logging   )lazy_load_kernelswiglugeglureglu)silugelureluc                      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          ||fS )a   
    Load sonic-moe once and return its required symbols.

    Raises:
        ImportError if the kernel or required symbols are not found.

    Returns:
        Tuple of (ActivationType, moe_general_routing_inputs function) from the sonic-moe kernel.
    z	sonic-moeNzisonic-moe kernel not found. Make sure you have the `kernels` and `nvidia-cutlass-dsl` packages installed.enumsActivationTypemoe_general_routing_inputsc                     g | ]	\  }}||
S )N ).0nameattrs      l/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/transformers/integrations/sonicmoe.py
<listcomp>z&_load_sonic_kernel.<locals>.<listcomp>8   s-       D$ < 	
 <<    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   ImportErrorgetattrjoin)kernelr   r   missings       r   _load_sonic_kernelr   #   s     k**F~w
 
 	
 WVWd;;=MtTTN!(1Mt!T!T  $^4)+EF
  G  
[TYYw=O=O [ [ [
 
 	

 555r   selfhidden_statestop_k_indextop_k_weightsreturnc                 v   | j         st          d          |j        j        dk    rt          d          t	                      \  }}|j        }|                    d          }|                    d          }t          j        ||                              d          	                    d|          
                    d                                          }	|
                    d                              |j                  }
|
                    d                                          }t          | j        dd	                                          }t          |t"                              |d
                                          |j                  }| j        rdnd} | j        j        | } | j        j        | }| j        r| j        nd }| j        r| j        nd } |||
|	|||||| j        |t          j                            |          j        t          j                      | j!                  \  }}|S )Nz/sonicmoe requires gated experts (has_gate=True)cudazsonicmoe requires CUDA devicer   )devicer   
hidden_actr
   r   )r   r   r   )r   r   r   )Eactivation_type	stream_idis_inference_mode_enabledconcat_layout)"has_gate
ValueErrorr'   typer   sizetorcharange	unsqueezeexpandreshapeinttodtyper   configlowerACT_MAPgetupperSWIGLUis_transposedgate_up_projpermute	down_projhas_biasgate_up_proj_biasdown_proj_biasnum_expertsr%   current_streamcuda_streamis_grad_enabledis_concatenated)r   r    r!   r"   r   r   r'   	num_top_k
num_tokens	token_idxrouter_scores
expert_idsact_namer*   permw1w2b1b2output_s                        r   sonicmoe_experts_forwardrY   I   s,    = LJKKK F**89991C1E1E.N.!F  $$I##A&&J Z777AA!DDKKBPYZZbbceffjjllI!))"--001DEEM$$R((,,..J t{L&99??AAHngkk(H.M.M.S.S.U.UWeWlmmO
 *999	D	"		"D	)B			&B#'=	:		dB $	7		4B**





'*++F33?&+&;&=&="=*  IFA  Mr   )__doc__	functoolsr2   utilsr   hub_kernelsr   
get_logger__name__loggerr<   cacher   nnModuleTensorrY   r   r   r   <module>re      s                ) ) ) ) ) ) 
	H	%	% Wg
>
> "6 "6 "6J3
(/3<3 3 <	3
 \3 3 3 3 3 3r   