+
    "Ý³jï^  ã                   óÒ   € R t ^ RIHtHtHt ^ RIt^ RIHt ^RIH	t	 ^RI
Ht ^RIHt ^RIHt ]! 4       tR R lt ! R	 R
4      t ! R R]4      t ! R R]4      t ! R R]4      tR# )zÒ
MoE (Mixture of Experts) lifecycle hooks system.

This module provides a base class for model-specific MoE lifecycle hooks that allow
customization of MoE forward passes and routing logic during quantization.
)ÚAnyÚDictÚOptionalN)ÚStopForward)Ú
get_device)Úsetup_logger)Úmove_toc                óz   € V ^8„  d   QhR\         P                  R\        R\        \         P                  ,          /# )é   ÚparentÚrelative_pathÚreturn)ÚnnÚModuleÚstrr   )Úformats   "Ú_/home/luca/screen-checking/.venv/lib/python3.14/site-packages/gptqmodel/models/moe_lifecycle.pyÚ__annotate__r      s1   € ÷ !ñ !¬¯©ð !Ä3ð !Ì8ÔTV×T]ÑT]ÕK^ñ !ó    c                óv  € V'       g   V # VP                  R4      pT p\        V4       FÝ  w  rERP                  VRV^,            4      p\        W54      '       d   \	        W54      pK>  \        VR4      '       d,   VP                  4       '       d    V\        V4      ,          pK{  \        V4       Uu. uF  qˆP                  R4      '       d   K  VNK  	  upR,          p	\        RV R	V R
\        V4      P                   RV	 24      h	  V#   \        \        3 d   p\        RT RT 24      hRp?ii ; iu upi )a  
Get a submodule from a parent module by relative path.

Args:
    parent: The parent module (e.g., a layer replica)
    relative_path: Dot-separated path to the submodule (e.g., 'mlp.experts.0.gate_proj')

Returns:
    The submodule if found, None otherwise
Ú.NÚ__getitem__z[MoE PATH] Failed indexing 'z': Ú_:Né   Nz[MoE PATH] Failed at 'z' (z), current=z, attrs=)ÚsplitÚ	enumerateÚjoinÚhasattrÚgetattrÚisdigitÚintÚ
IndexErrorÚKeyErrorÚ
ValueErrorÚdirÚ
startswithÚtypeÚ__name__)
r   r   ÚpartsÚcurrentÚiÚpartÚpath_so_farÚeÚaÚattrss
   &&        r   Ú_get_module_by_relative_pathr0      s+  € ÷ Øˆà×Ñ Ó$€EØ€Gä˜UÖ#‰ˆØ—h‘h˜u V a¨!¥e˜}Ó-ˆÜ�7×!Ò!Ü˜gÓ,ŠGÜ�W˜m×,Ò,°·±·²ðNØ!¤# d£)Õ,’ô
 !$ G¤ÓF¡˜1·L±LÀ×4E—Q�Q¡ÑFÀsÕKˆEÜØ(¨¨¨c°+°¸kÌ$ÈwË-×J`ÑJ`ÐIaÐaiÐjoÐipÐqósð sñ $ð  €Nøô ¤Ð)ô NÜ Ð#?À¸vÀSÈÈÐ!LÓMÐMûðNüò Gs$   ÂDÂ1D6ÃD6ÄD3ÄD.Ä.D3c                   óÊ   a € ] tR t^?t o RtR.tRR.tV 3R lR ltRV 3R lR	 lltV 3R
 lR lt	V 3R lR lt
V 3R lR ltV 3R lR ltV 3R lR ltRV 3R lR lltRtV tR# )ÚMoELifecycleHooksaA  
Base class for model-specific MoE lifecycle hooks.

This class provides customization points for MoE-specific operations during quantization.
Subclasses should override these methods to provide model-specific implementations.

Main use case: Forward whole calibration dataset to all experts instead of only routed ones.
ÚexpertsÚshared_expertsÚshared_expertc                ód   <€ V ^8„  d   QhRS[ P                  RS[RS[S[ P                  ,          /# )r
   Úlayer_moduleÚmodel_classr   ©r   r   r&   r   )r   Ú__classdict__s   "€r   r   ÚMoELifecycleHooks.__annotate__O   s2   ø€ ÷ ñ ©"¯)©)ð Á$ð É8ÑTV×T]ÑT]ÕK^ñ r   c                óœ   € VP                  4       pVf%   \        P                  RVP                   24       R# \	        W^ ,          R4      pV# )aÀ  
Extract the MoE block from a layer module using the :moe flag from module_tree.

Args:
    layer_module: The layer module (e.g., DecoderLayer)
    model_class: The model class (to access module_tree)
Returns:
    The MoE block module, or None if not found

Example:
    For GLM-4, module_tree has "mlp:moe", so this returns layer_module.mlp
    For MiniMax-M2, module_tree has "block_sparse_moe:moe", so this returns layer_module.block_sparse_moe
Nz&No :moe flag found in module_tree for )Úget_moe_module_nameÚlogÚerrorr'   r   )Úselfr7   r8   Úmoe_module_nameÚ	moe_blocks   &&&  r   Úget_moe_blockÚMoELifecycleHooks.get_moe_blockO   sO   € ð &×9Ñ9Ó;ˆàÒ"Ü�I‰IÐ>¸{×?SÑ?SÐ>TÐUÔVÙô ˜L¸!Õ*<¸dÓCˆ	àÐr   Nc          	      ó�   <€ V ^8„  d   QhRS[ P                  RS[RS[S[S[S[3,          ,          RS[S[ P                  ,          /# )r
   r7   r8   Úcurrent_subsetr   )r   r   r&   r   r   r   r   )r   r:   s   "€r   r   r;   i   sM   ø€ ÷ =ñ =á—i‘ið=ñ ð=ñ !¡¡c©3 h¥Õ0ð	=ñ
 
‘"—)‘)Õ	ñ=r   c                ó$   € V P                  W4      # )zòResolve the MoE root for a quantization subset.

The default delegates to the original two-argument hook so existing
model-specific ``get_moe_block`` overrides remain compatible. Models
with multiple expert families can override this method.
)rC   )r@   r7   r8   rF   s   &&&&r   Úget_moe_block_for_subsetÚ*MoELifecycleHooks.get_moe_block_for_subseti   s   € ð ×!Ñ! ,Ó<Ð<r   c                ód   <€ V ^8„  d   QhRS[ P                  RS[RS[S[ P                  ,          /# ©r
   rB   r8   r   r9   )r   r:   s   "€r   r   r;   w   s2   ø€ ÷ ñ ©B¯I©Ið ÁDð ÉXÑVX×V_ÑV_ÕM`ñ r   c                óR   € V P                  V4      pV'       d   \        W4      pV# R# )a  
Extract experts module from MoE block by checking common attribute names.

Args:
    moe_block: The MoE block module
    model_class: The model class (for compatibility, unused)

Returns:
    The experts module, or None if not found

Example:
    Returns moe_block.experts if it exists
N)Úget_experts_module_namer   )r@   rB   r8   ÚnameÚexperts_modules   &&&  r   Úget_experts_moduleÚ$MoELifecycleHooks.get_experts_modulew   s*   € ð ×+Ñ+¨IÓ6ˆßÜ$ YÓ5ˆNØ!Ð!ár   c                óJ   <€ V ^8„  d   QhRS[ P                  RS[S[,          /# ©r
   rB   r   ©r   r   r   r   )r   r:   s   "€r   r   r;   �   s#   ø€ ÷ ñ ±·±ð ¹xÉ½}ñ r   c                óV   € V P                    F  p\        W4      '       g   K  Vu # 	  R# )a6  
Get the attribute name for experts module if it exists.

Args:
    moe_block: The MoE block module

Returns:
    The attribute name ('experts', 'expert_list', 'expert_modules', etc.) or None

Example:
    name = hooks.get_experts_module_name(moe_block)
    if name:
        experts = getattr(moe_block, name)
N)Úexpert_block_namesr   ©r@   rB   rN   s   && r   rM   Ú)MoELifecycleHooks.get_experts_module_name�   s)   € ð  ×+Ô+ˆDÜ�y×'Ô'Ø’ñ ,ñ r   c                óJ   <€ V ^8„  d   QhRS[ P                  RS[S[,          /# rS   rT   )r   r:   s   "€r   r   r;   ¢   s#   ø€ ÷ ñ ¹¿	¹	ð ÁhÉsÅmñ r   c                óV   € V P                    F  p\        W4      '       g   K  Vu # 	  R# )aB  
Get the attribute name for shared experts module if it exists.

Args:
    moe_block: The MoE block module

Returns:
    The attribute name ('shared_experts', 'shared_expert', etc.) or None

Example:
    name = hooks.get_shared_experts_module_name(moe_block)
    if name:
        shared_experts = getattr(moe_block, name)
N)Úshared_expert_block_namesr   rW   s   && r   Úget_shared_experts_module_nameÚ0MoELifecycleHooks.get_shared_experts_module_name¢   s)   € ð  ×2Ô2ˆDÜ�y×'Ô'Ø’ñ 3ñ r   c                ód   <€ V ^8„  d   QhRS[ P                  RS[RS[S[ P                  ,          /# rK   r9   )r   r:   s   "€r   r   r;   ·   s3   ø€ ÷ ñ ±2·9±9ð É4ð ÑT\Ñ]_×]fÑ]fÕTgñ r   c                óR   € V P                  V4      pV'       d   \        W4      pV# R# )aq  
Extract shared experts module from MoE block by checking common attribute names.

Args:
    moe_block: The MoE block module
    model_class: The model class (for compatibility, unused)

Returns:
    The shared experts module, or None if not found (shared experts are optional)

Example:
    Returns moe_block.shared_experts or moe_block.shared_expert if either exists
N)r\   r   )r@   rB   r8   rN   Úshared_experts_modules   &&&  r   Úget_shared_experts_moduleÚ+MoELifecycleHooks.get_shared_experts_module·   s-   € ð ×2Ñ2°9Ó=ˆßÜ$+¨IÓ$<Ð!Ø(Ð(ñ r   c                óˆ   <€ V ^8„  d   QhRS[ S[,          RS[S[,          RS[S[,          RS[S[,          RS[ S[,          /# )r
   Úordered_module_namesÚmoe_block_prefixÚexperts_attr_nameÚshared_expert_attr_namer   )Úlistr   r   )r   r:   s   "€r   r   r;   Î   sQ   ø€ ÷  ñ  á"&¡s¥)ð ñ '¡s�mð ñ  (©�}ð	 ñ
 &.©c¥]ð ñ 
‰c�ñ r   c                ól  € V'       d   Vf   . # . pV'       d	   V RV R2MRpV'       d	   V RV R2MRpV Fy  pV'       d2   VP                  V4      '       d   RV9  d   VP                  R4       K<  V'       g   KF  VP                  V4      '       g   K_  RV9  g   Kh  VP                  R4       K{  	  V# )a~  
Infer shared-expert vs routed-expert replay order from explicit subset names.

The module tree is designed to mirror forward execution order, and
subset planning preserves that ordered module list explicitly.
Replay should therefore follow the first occurrence of each MoE family
in `ordered_module_names` instead of relying on dict iteration or
model-specific hardcoded ordering.
Nr   Úsharedr3   )r%   Úappend)	r@   rd   re   rf   rg   ÚorderÚexpert_prefixÚshared_prefixÚkeys	   &&&&&    r   Úget_subset_execution_orderÚ,MoELifecycleHooks.get_subset_execution_orderÎ   s­   € ÷  $Ð'7Ò'?ØˆIàˆßFWÐ+Ð,¨AÐ.?Ð-@ÀÑBÐ]aˆßLcÐ+Ð,¨AÐ.EÐ-FÀaÑHÐimˆã'ˆCß §¡°×!>Ò!>Ø 5Ô(Ø—L‘L Ô*Ùß‰} §¡°×!>Ô!>Ø EÖ)Ø—L‘L Ö+ñ (ð ˆr   c                óØ   <€ V ^8„  d   QhRS[ P                  RS[P                  RS[RS[S[S[3,          RS[S[S[,          ,          RS[	RS[
RS[R	S[S[,          R
S[P                  /
# )r
   rB   Úhidden_statesÚ	processorÚsubsetrd   Úoriginal_forwardr8   Úmodule_looperre   r   ©r   r   ÚtorchÚTensorr   r   r   r   rh   Úcallabler&   )r   r:   s   "€r   r   r;   ð   s�   ø€ ÷ $2ñ $2á—y‘yð$2ñ !Ÿ<™<ð$2ñ ð	$2ñ
 ™™c˜•Nð$2ñ #+©4±­9Õ"5ð$2ñ 'ð$2ñ ð$2ñ ð$2ñ '¡s�mð$2ñ 
�‰ñ$2r   c
                ól   € \         P                  R\        V4      P                   R24       V! V3/ V
B # )a_  
Forward hidden states to all experts, bypassing routing.
Subclasses should implement this with model-specific logic.

Args:
    moe_block: The MoE block module
    hidden_states: Input tensor
    processor: The quantization processor
    subset: Dict[str, NamedModule] being calibrated
    original_forward: Original forward function
    model_class: The model class
    moe_block_prefix: Optional prefix for MoE block modules (optimized parameter)
    module_looper: ModuleLooper instance for TLS-based hooks pausing
    **kwargs: Additional arguments

Returns:
    Output tensor from routed forward pass
z+forward_to_all_experts not implemented for zY. Falling back to normal forward. Override this method for model-specific implementation.)r>   r?   r&   r'   )r@   rB   rs   rt   ru   rd   rv   r8   rw   re   Úkwargss   &&&&&&&&&&,r   Úforward_to_all_expertsÚ(MoELifecycleHooks.forward_to_all_expertsð   s?   € ô> 	�	‰	Ø9¼$¸y»/×:RÑ:RÐ9Sð T1ð 2ô	
ñ
 ˜Ñ1¨&Ñ1Ð1r   © )N)r'   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rV   r[   rC   rH   rP   rM   r\   ra   rp   r~   Ú__static_attributes__Ú__classdictcell__©r:   s   @r   r2   r2   ?   sw   ø‡ € ñð $˜Ðð "2°?Ð CÐ÷ð ÷4=ò =÷ð ÷,ð ÷*ð ÷*ð ÷. ð  ÷D$2÷ $2ð $2r   r2   c                   ó€   a € ] tR tRt o RtRtRtRtRV 3R lR lltR t	V 3R lR lt
RV 3R	 lR
 lltV 3R ltRtV tR# )Ú!ExpertProjectionMoELifecycleHooksi  a®  
Base MoE lifecycle hooks for expert architectures with 3 projections.

This class extracts the common logic and allows subclasses to configure
the attribute names used for the three projections.

Expert forward pattern: output = down_proj(act_fn(gate_proj(x)) * up_proj(x))

Subclasses should set:
- gate_proj_name: Name of gate/w1 projection
- up_proj_name: Name of up/w3 projection
- down_proj_name: Name of down/w2 projection
Nc                ó,   <€ V ^8„  d   QhRS[ RS[ RS[ /# ©r
   Úgate_proj_nameÚup_proj_nameÚdown_proj_name©r   )r   r:   s   "€r   r   Ú.ExpertProjectionMoELifecycleHooks.__annotate__+  s#   ø€ ÷ ñ ¡sð Áð Ñ]`ñ r   c                ó
  € Ve   Wn         Ve   W n        Ve   W0n        \        V P                   V P                  V P                  .4      '       g3   \	        RV P                    RV P                   RV P                   24      hR# )a  
Initialize with custom projection names.

Args:
    gate_proj_name: Name of gate projection (e.g., "gate_proj" or "w1")
    up_proj_name: Name of up projection (e.g., "up_proj" or "w3")
    down_proj_name: Name of down projection (e.g., "down_proj" or "w2")
Nz]Projection names must be set either as class attributes or constructor parameters. Got: gate=z, up=z, down=)rŒ   r�   rŽ   Úallr#   )r@   rŒ   r�   rŽ   s   &&&&r   Ú__init__Ú*ExpertProjectionMoELifecycleHooks.__init__+  s“   € ð Ò%Ø"0ÔØÒ#Ø ,ÔØÒ%Ø"0Ôô �D×'Ñ'¨×):Ñ):¸D×<OÑ<OÐP×QÒQÜðØ!×0Ñ0Ð1°°t×7HÑ7HÐ6IÈÐQU×QdÑQdÐPeðgóð ñ Rr   c                ó0  € \        VRR4      p\        V4      '       d    V! \        P                  ! W4.RR7      4      # \	        VR4      '       d   VP                  V4      V,          # \        P                  P                  P                  V4      V,          # )z8Apply the model's fused expert gate when it exposes one.Ú_apply_gateN)ÚdimÚact_fnéÿÿÿÿ)	r   r{   ry   Úcatr   r˜   r   Ú
functionalÚsilu)r@   rO   ÚexpertÚgate_outÚup_outÚ
fused_gates   &&&&& r   Úapply_expert_activationÚ9ExpertProjectionMoELifecycleHooks.apply_expert_activationB  sw   € ô ˜^¨]¸DÓAˆ
Ü�J×ÒÙœeŸiši¨Ð(:ÀÔCÓDÐDÜ�6˜8×$Ò$Ø—=‘= Ó*¨VÕ3Ð3Ü�x‰x×"Ñ"×'Ñ'¨Ó1°FÕ:Ð:r   c                óf   <€ V ^8„  d   QhRS[ S[S[3,          RS[P                  RS[S[,          /# )r
   ru   rB   r   )r   r   r   r   r   r   )r   r:   s   "€r   r   r�   L  s5   ø€ ÷ ñ ±±S¹#°Xµð É2Ï9É9ð ÑYaÑbeÕYfñ r   c                óT  € V'       g   R# V P                  V4      pV P                  V4      pVP                  4        Fg  pV'       d*   RV R2V9   d   VP                  RV R24      ^ ,          u # V'       g   K>  RV R2V9   g   KK  VP                  RV R24      ^ ,          u # 	  R# )zË
Extract moe_block_prefix from subset keys.

Args:
    subset: Dict[str, NamedModule] being calibrated
    moe_block: The MoE block module

Returns:
    The moe_block_prefix string, or None if not found
Nr   )rM   r\   Úkeysr   )r@   ru   rB   rf   rg   ro   s   &&&   r   Ú_extract_moe_block_prefixÚ;ExpertProjectionMoELifecycleHooks._extract_moe_block_prefixL  s¬   € ÷ Ùà ×8Ñ8¸ÓCÐØ"&×"EÑ"EÀiÓ"PÐà—;‘;–=ˆCß  qÐ):Ð(;¸1Ð%=ÀÔ%DØ—y‘y 1Ð%6Ð$7°qÐ!9Ó:¸1Õ=Ò=ß&Ñ&¨QÐ/FÐ.GÀqÐ+IÈSÖ+PØ—y‘y 1Ð%<Ð$=¸QÐ!?Ó@ÀÕCÒCñ	 !ñ r   c                ó  <€ V ^8„  d   QhRS[ P                  RS[P                  RS[RS[S[S[3,          RS[S[S[,          ,          RS[	RS[
RS[R	S[S[,          R
S[S[ P                  ,          RS[P                  /# )r
   rB   rs   rt   ru   rd   rv   r8   rw   re   Úreplica_moduler   rx   )r   r:   s   "€r   r   r�   e  s°   ø€ ÷ Vñ Vá—y‘yðVñ !Ÿ<™<ðVñ ð	Vñ
 ™™c˜•NðVñ #+©4±­9Õ"5ðVñ 'ðVñ ðVñ ðVñ '¡s�mðVñ %¡R§Y¡YÕ/ðVñ 
�‰ñVr   c           
     ó¼  a aaaa	a
aaaaaaaa € V'       d	   V'       g#   Rp\         P                  V4       \        V4      hS	f
   V! S3/ VB # \        VRR4      p\	        V4      '       d
   V! S	S4       ^ oRo S P
                  S P                  S P                  .pR V
V3R lloS P                  SV4      oS P                  SV4      pS P                  S4      oS P                  S4      oRoVe/   S'       d'   S	'       d   V F  pS	 RS RV 2pVS9   g   K  Ro M	  RoSe¬   \        SR	4      '       dš   \        S4      ^ 8”  dŠ   S'       d‚   S	'       dz   S	 RS R2pSP                  4        F]  pVP                  V4      '       g   K  V\        V4      R P!                  R4      p\        V4      ^8¼  g   KK  V^,          V9   g   K[  Ro M	  VVVVVV 3R
 lpVVVVVVV	V V V3
R lpS P#                  T;'       g    . S	SSR7      pS'       d   RV9  d   VP%                  R4       S'       d   RV9  d   VP%                  R4       V F#  pVR8X  d
   V! 4        K  VR8X  g   K  V! 4        K%  	  S '       d   \'        4       hS^ 8”  d1   VP)                  VR4        V! S3/ VB pVP)                  VR4       V# V! S3/ VB pV#   TP)                  TR4       i ; i)aØ  
Forward to all experts using configurable projection names.

Args:
    moe_block: The MoE block module
    hidden_states: Input tensor
    processor: The quantization processor (needed for hook pausing)
    subset: Dict[str, NamedModule] containing modules currently being calibrated
    original_forward: Original forward function to call for final output
    model_class: The model class (to access module_tree)
    replica_module: Optional replica of the layer module. When provided, modules
                   are looked up from the replica instead of using subset directly.
                   This is needed for multi-GPU parallel execution where the replica
                   is on a different device than the original modules in subset.
    **kwargs: Additional arguments (attention_mask, etc.)

This implementation replays shared-expert and routed-expert paths in the
order they appear in the subset/module tree, then calls the original
routed forward for the final output.
z%Missing processor or original_forwardNÚrecord_moe_root_input_featureFc                ó$   € V ^8„  d   QhR\         /# )r
   ro   r�   )r   s   "r   r   ÚNExpertProjectionMoELifecycleHooks.forward_to_all_experts.<locals>.__annotate__™  s   € ÷ 	!ñ 	!¤Sñ 	!r   c                ór   <€ Se!   \        SV 4      pVe   V# \        RV  24      hSP                  V 4      pV# )a5  
Get the callable module for a given subset key.

When replica_module is provided, resolves the module from the replica
using the key as a relative path. This ensures forward passes happen
on the correct device for multi-GPU execution.

Falls back to subset[key] when replica is not provided or lookup fails.
z.[MoE DEBUG] replica_submodule is None for key=)r0   r#   Úget)ro   Úreplica_submoduleÚsubset_moduler©   ru   s   &  €€r   Úget_callable_moduleÚUExpertProjectionMoELifecycleHooks.forward_to_all_experts.<locals>.get_callable_module™  sO   ø€ ð Ò)Ü$@ÀÐQTÓ$UÐ!Ø$Ò0Ø,Ð,ä$Ð'UÐVYÐUZÐ%[Ó\Ð\ð #ŸJ™J s›OˆMØ Ð r   r   TÚ__iter__c                  óº   <€ S'       d	   S'       g   R #  \        SS4      p \        V 4      pV ! \        SV4      4       S^,          oR #   \         d    Ro R # i ; i)NT)r   r   r   r   )Úshared_expert_moduleÚshared_expert_deviceÚexpert_countÚhas_shared_expertsrs   rB   rg   Ústop_forward_raiseds     €€€€€€r   Úrun_shared_expertsÚTExpertProjectionMoELifecycleHooks.forward_to_all_experts.<locals>.run_shared_expertsÑ  sZ   ø€ ç%×-DÙð+Ü'.¨yÐ:QÓ'RÐ$Ü'1Ð2FÓ'GÐ$Ù$¤W¨]Ð<PÓ%QÔRØ Õ!’øÜô +Ø&*Ó#ð+ús   •2A	 Á	AÁAc                  óÈ  <
€ S'       d   Se   \        SR4      '       d	   S'       g   R # SP                  4       ^8X  d%   SP                  RSP                  R,          4      p MSp \	        S4       EFT  w  rS RS RV RSP
                   2pS RS RV RSP                   2pS RS RV RSP                   2pVS9  d   VS9  d
   VS9  d   Kb  \        VSP
                  R 4      pVe   \        V4      M
\        V4      p\        W4      p VS9   dm   \        VSP
                  4      p	\        VSP                  4      p
V	! V4      pV
! V4      pSP                  SVVVR7      p??S! V4      ! V4       ?S^,          oMARpVS9   d   S! V4      ! V4       RpVS9   d   S! V4      ! V4       RpV'       d
   S^,          o?EKW  	  R #   \         d    Ro Li ; i  ?i ; i)Nr´   r   )rO   r�   rž   rŸ   FTr™   )r   r—   ÚreshapeÚshaper   rŒ   r�   rŽ   r   r   r   r¡   r   )Úhidden_states_2dÚ
expert_idxr�   Úgate_keyÚup_keyÚdown_keyÚgate_module_refÚexpert_deviceÚexpert_inputÚgate_moduleÚ	up_modulerž   rŸ   ÚintermediateÚ
called_anyr¸   rf   rO   r²   Úhas_expert_projsrs   re   r@   rº   ru   s                  €€€€€€€€€€r   Úrun_routed_expertsÚTExpertProjectionMoELifecycleHooks.forward_to_all_experts.<locals>.run_routed_expertsÝ  s%  ø€ ç# ~Ò'=ÄWÈ^Ð]g×EhÒEh÷  qBÙà× Ñ Ó" aÔ'Ø#0×#8Ñ#8¸¸]×=PÑ=PÐQSÕ=TÓ#UÑ à#0Ð ä&/°×&?Ñ"�
Ø.Ð/¨qÐ1BÐ0CÀ1ÀZÀLÐPQÐRV×ReÑReÐQfÐg�Ø,Ð-¨QÐ/@Ð.AÀÀ:À,ÈaÐPT×PaÑPaÐObÐc�Ø.Ð/¨qÐ1BÐ0CÀ1ÀZÀLÐPQÐRV×ReÑReÐQfÐg�à 6Ô)¨f¸FÔ.BÀxÐW]ÔG]Ùä")¨&°$×2EÑ2EÀtÓ"L�Ø?NÒ?Z¤
¨?Ô ;Ô`jÐkqÓ`r�Ü&Ð'7ÓG�ð %Ø 6Ô)Ü&-¨f°d×6IÑ6IÓ&J˜Ü$+¨F°D×4EÑ4EÓ$F˜	Ù#.¨|Ó#<˜Ù!*¨<Ó!8˜à'+×'CÑ'CØ+9Ø#)Ø%-Ø#)ð	 (Dó (˜ð % fá+¨HÔ5°lÔCØ(Ø$¨Õ)™à%*˜
Ø# vÔ-Ù/°Ô9¸,ÔGØ)-˜JØ! VÔ+Ù/°Ô7¸ÔEØ)-˜Jß%Ø(¨AÕ-˜Ló
 %óY '@øôR #ô /Ø*.Ò'ð/ûñ %ús7   ÄA2GÆGÆ7GÆ:	GÇGÇGÇGÇGÇG!)rd   re   rf   rg   rj   r3   )r>   r?   r#   r   r{   rŒ   r�   rŽ   rP   ra   r\   rM   r   Úlenr¥   r%   r   rp   rk   r   Ú_set_processor_hooks_paused)!r@   rB   rs   rt   ru   rd   rv   r8   rw   re   r©   r}   Ú	error_msgÚroot_recorderÚ
proj_namesr`   rN   ro   rm   r(   r»   rÍ   Úexecution_orderÚ
group_nameÚresultr¸   rf   rO   r²   rÌ   r¹   rg   rº   s!   fff&f&&&&ff,             @@@@@@@@r   r~   Ú8ExpertProjectionMoELifecycleHooks.forward_to_all_expertse  sè  ÿý€ ÷D × 0Ø?ˆIÜ�I‰I�iÔ Ü˜YÓ'Ð'àÒ#ñ $ MÑ<°VÑ<Ð<ä 	Ð+JÈDÓQˆÜ�M×"Ò"ÙÐ*¨MÔ:àˆØ#ÐØ×)Ñ)¨4×+<Ñ+<¸d×>QÑ>QÐRˆ
÷	!ñ 	!ð0 ×0Ñ0°¸KÓHˆØ $× >Ñ >¸yÈ+Ó VÐØ"&×"EÑ"EØó#Ðà ×8Ñ8¸ÓCÐð #ÐØ Ò,×1H×M]ã"�Ø)Ð*¨!Ð,CÐ+DÀAÀdÀVÐL�Ø˜&–=Ø)-Ð&Ùñ	 #ð !ÐØÒ%¬'°.À*×*MÒ*MÔRUØóS Ø"#ôS$ß(9×>Nà/Ð0°Ð2CÐ1DÀAÐFˆMØ—{‘{–}�Ø—>‘> -×0Ô0à¤ MÓ 2Ð 3Ð4×:Ñ:¸3Ó?�EÜ˜5“z Q–¨5°­8°zÖ+AØ+/Ð(Ùñ %÷
	+ò 
	+÷6	%ö 6	%ðp ×9Ñ9Ø!5×!;Ð!;¸Ø-Ø/Ø$;ð	 :ó 
ˆ÷  (°/Ô"AØ×"Ñ" 8Ô,ß 	°Ô @Ø×"Ñ" 9Ô-ã)ˆJØ˜XÔ%Ù"Ö$Ø˜yÖ(Ù"Ö$ñ	 *÷ ä“-Ðð ˜!Ôð ×5Ñ5°iÀÔFðLÙ)¨-ÑB¸6ÑB�à×9Ñ9¸)ÀUÔKð
 ˆñ & mÑ>°vÑ>ˆFàˆøð ×9Ñ9¸)ÀUÕKús   Ê	K ËKc                ó>   <€ V ^8„  d   Qh/ S[ ;R&   S[ ;R&   S[ ;R&   # r‹   r�   )r   r:   s   "€r   r   r�     s,   ø‡ ‚ ñ  Ññ! ñ" Ññ# ñ$ Ñò% r   )rŽ   rŒ   r�   )NNN)NN)r'   r�   r‚   rƒ   r„   rŒ   r�   rŽ   r“   r¡   r¦   r~   Ú__annotate_func__r…   r†   r‡   s   @r   r‰   r‰     sH   ø‡ € ñð €NØ€LØ€N÷ò ò.;÷ð ÷2Vò V÷] ƒ r   r‰   c                   ó&   € ] tR tRtRtRtRtRtRtR# )ÚGateUpDownMoELifecycleHooksi>  z–
MoE lifecycle hooks for models using gate_proj/up_proj/down_proj naming.

Used by: GLM-4, Qwen2-MoE, Mixtral, Phi-3 MoE, most Llama-based MoE models
Ú	gate_projÚup_projÚ	down_projr€   N©	r'   r�   r‚   rƒ   r„   rŒ   r�   rŽ   r…   r€   r   r   rÛ   rÛ   >  s   † ñð
 !€NØ€LØ „Nr   rÛ   c                   ó&   € ] tR tRtRtRtRtRtRtR# )ÚW1W3W2MoELifecycleHooksiI  zŽ
MoE lifecycle hooks for models using w1/w3/w2 naming.

Used by: Some architectures that explicitly name layers w1, w3, w2 (e.g., MiniMax-M2)
Úw1Úw3Úw2r€   Nrß   r€   r   r   rá   rá   I  s   † ñð
 €NØ€LØ„Nr   rá   )r„   Útypingr   r   r   ry   Útorch.nnr   Únn_modules.hooked_linearr   Úutils.devicer   Úutils.loggerr   Úutils.modelr   r>   r0   r2   r‰   rÛ   rá   r€   r   r   Ú<module>rë      sj   ðñ÷ 'Ñ &ã Ý å 2Ý %Ý 'Ý !ñ ƒn€õ!÷HU2ñ U2ôpdÐ(9ô dôN	!Ð"Cô !ôÐ?ö r   