
    .`i0                     &   U d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
  ee          Zi Zeeed         ed         z  f         ed<   i Zeeed         ed         z  f         ed	<    G d
 dej                  Z G d dej                  ZdS )    N)get_cached_compilation_config)init_logger)maybe_disable_graph_partition)current_platformCustomOpPluggableLayerop_registryop_registry_ootc                   b     e Zd ZdZ fdZedefd            Zeddedz  fd            Z xZ	S )r   a_  
    Base class for pluggable layers.

    A PluggableLayer is a *module-composing* abstraction: it may instantiate other
    ``torch.nn.Module`` objects as sub-layers, and its functionality depends on
    these sub-layers following a generalized invocation sequence. Also, it is stateful
    and may hold parameters or buffers.

    Unlike :class:`CustomOp`, PluggableLayer does NOT provide per-platform
    ``forward_*`` dispatch. Instead, it supports out-of-tree (OOT) replacement
    of the entire layer class at instantiation time, allowing customized
    initialization and submodule composition.
    c                 *   	 | j         }n'# t          $ r t          d| j          d          d w xY w|t          vr| }n6t          |         }t                              d|t          |                     t                                          |          S )NCannot instantiate 'z': its 'name' attribute was not set, possibly because it was not decorated with @PluggableLayer.register, or it's the PluggableLayer itself.z*Instantiating pluggable layer: %s using %s	__name__AttributeError	TypeErrorr
   loggerdebugstrsuper__new__)clsargskwargslayer_class_namelayer_cls_to_instantiate	__class__s        q/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/model_executor/custom_op.pyr   zPluggableLayer.__new__%   s    	"| 	 	 	Ps| P P P  		 ?22'*$$'67G'H$LL< ,--  
 ww7888    $/namec                     fd}|S )Nc                 V    t           vsJ d             | _        | t           <   | S NzDuplicate op name: r	   r   op_clsr   s    r   	decoratorz*PluggableLayer.register.<locals>.decorator=   ;    {***,H$,H,H***FK &KM     r   r   r&   s    ` r   registerzPluggableLayer.register;   $    	 	 	 	 	 r(   Nc                 x      fd}||S t          |t                    r ||          S t          d          )Nc                 l    nj         }|t          vsJ d|             || _        | t          |<   | S )NzDuplicate layer name: r   r
   r   )	layer_clsreg_namer   r   s     r   r&   z.PluggableLayer.register_oot.<locals>.decoratorK   sL    #/ttS\H?2224WX4W4W222%IN(1OH%r(   )Decorator can only be applied to classes.
isinstancetyper   )r   _decorated_layer_clsr   r&   s   ` ` r   register_ootzPluggableLayer.register_ootI   sf    	 	 	 	 	 	  ' ,d33 	I91222GHHHr(   NN)
r   
__module____qualname____doc__r   classmethodr   r+   r7   __classcell__r   s   @r   r   r      s         9 9 9 9 9, C    [ I I3: I I I [I I I I Ir(   c                       e Zd ZdZ fdZddddedef fdZd Zd	 Zd
 Z	d Z
d Zd Zd Zd ZdefdZdddefdZedefd            Zedefd            Zedefd            Zeddedz  fd            Z xZS )r   zb
    Base class for custom ops.
    Dispatches the forward method to the appropriate backend.
    c                 *   	 | j         }n'# t          $ r t          d| j          d          d w xY w|t          vr| }n6t          |         }t                              d|t          |                     t                                          |          S )Nr   z': its 'name' attribute was not set, possibly because it was not decorated with @CustomOp.register, or it's the CustomOp base class itself.z$Instantiating custom op: %s using %sr   )r   r   r   op_nameop_cls_to_instantiater   s        r   r   zCustomOp.__new__c   s    	lGG 	 	 	Os| O O O  		 /))$'!!$3G$<!LL6)**  
 ww4555r   F)enforce_enablecompile_nativerC   rD   c                    t                                                       || _        |                     |          | _        d S )N)rD   )r   __init___enforce_enabledispatch_forward_forward_method)selfrC   rD   r   s      r   rF   zCustomOp.__init__x   s@    -#44N4SSr(   c                      | j         |i |S N)rI   rJ   r   r   s      r   forwardzCustomOp.forward}   s    #t#T4V444r(   c                     t           )zPyTorch-native implementation of the forward method.
        This method is optional. If implemented, it can be used with compilers
        such as torch.compile or PyTorch XLA. Also, it can be used for testing
        purposes.
        NotImplementedErrorrM   s      r   forward_nativezCustomOp.forward_native   s
     "!r(   c                     t           rL   rP   rM   s      r   forward_cudazCustomOp.forward_cuda   s    !!r(   c                      | j         |i |S rL   )rT   rM   s      r   forward_hipzCustomOp.forward_hip   s     t $1&111r(   c                      | j         |i |S rL   rR   rM   s      r   forward_xpuzCustomOp.forward_xpu        #t"D3F333r(   c                      | j         |i |S rL   rX   rM   s      r   forward_cpuzCustomOp.forward_cpu   rZ   r(   c                      | j         |i |S rL   rX   rM   s      r   forward_tpuzCustomOp.forward_tpu   s     #t"D3F333r(   c                      | j         |i |S rL   rX   rM   s      r   forward_ootzCustomOp.forward_oot   rZ   r(   c                 <   t                      }| j        p|                                 }|r&|j                            | j        j        g           n%|j                            | j        j        g           |s|                     | j	        |          S t          j                    r| j        S t          j                    r| j        S t          j                    r| j        S t          j                    r| j        S t          j                    r| j        S | j        S )Nenable)r   rG   enabledenabled_custom_opsupdater   r   disabled_custom_opsmaybe_compilerR   r   is_rocmrV   is_cpur\   is_tpur^   is_xpurY   is_out_of_treer`   rT   )rJ   rD   compilation_configrd   s       r   rH   zCustomOp.dispatch_forward   s#    ;<< &8$,,.. 	Q188$.:M9NOOOO2994>;N:OPPP 	R %%d&9.%QQQ#%% 	%##$&& 		%##$&& 	%##$&& 	%##,.. 	%##$$r(   Trb   rc   c                    ddl m} |s|S t                      }|j        |j        k    r|S |j        dk    r|S t          j        |dt          j	        t          t          j	                            S )aP  
        Compile fn if compilation enabled.
        Useful for CustomOp instances called from within a torch custom op,
        meaning the forward call is hidden from the model-level torch.compile.

        NOTE: this does not enable fusion across ops, so opaque custom ops
        should still be unwrapped wherever possible.
        r   )CompilationModeeagerT)dynamicbackendoptions)vllm.config.compilationrp   r   modeNONErs   torchcompiler   simple_compile_backendr   )rJ   fnrc   rp   rn   s        r   rh   zCustomOp.maybe_compile   s     	<;;;;; 	I ;<<"o&:::I %00I }$;1 7 	
 
 
 	
r(   returnc                 V   t                      }|j        }t          | d          s9t                              d| j                   t                                          S d| j         |v }d| j         |v }|r|rJ d| j                     t                                          s|o| S )Nr   zCustom op %s was not registered, which means it won't appear in the op registry. It will be enabled/disabled based on the global settings.+-zCannot enable and disable )	r   
custom_opshasattrr   warning_oncer   r   
default_onr   )r   rn   r   rd   disableds        r   rd   zCustomOp.enabled   s     ;<<'2
sF## 	)# 	   &&((( ch..J.!sx>>Z/RRR+R+R+RRR)##%%0B(lBr(   c                      t                      } | j                            d          }| j                            d          }||z   dk    sJ |dk     p|dk    S )z
        Behavior controlled by `CompilationConfig.custom_ops`: On by default if
        'all', off by default if 'none'.
        When PyTorch Inductor is used, 'none' is the default value,
        otherwise 'all'.
        noneall   r   )r   r   count)rn   
count_none	count_alls      r   r   zCustomOp.default_on   sh     ;<<'288@@
&177>>	I%****>!2Y]2r(   r   c                     fd}|S )Nc                 V    t           vsJ d             | _        | t           <   | S r"   r#   r$   s    r   r&   z$CustomOp.register.<locals>.decorator  r'   r(   r)   r*   s    ` r   r+   zCustomOp.register  r,   r(   Nc                 x      fd}||S t          |t                    r ||          S t          d          )Nc                 l    nj         }|t          vsJ d|             || _        | t          |<   | S r"   r/   )r%   r1   r   r   s     r   r&   z(CustomOp.register_oot.<locals>.decorator"  sK    #/ttS\H?2224T(4T4T222"FK(.OH%Mr(   r2   r3   )r   _decorated_op_clsr   r&   s   ` ` r   r7   zCustomOp.register_oot   sj    	 	 	 	 	 	 $
 )400 	I 9./// GHHHr(   r8   )r   r9   r:   r;   r   boolrF   rN   rR   rT   rV   rY   r\   r^   r`   rH   rh   r<   rd   staticmethodr   r   r+   r7   r=   r>   s   @r   r   r   ]   s        
6 6 6 6 6* 27u T T T$ T T T T T T T
5 5 5" " "" " "2 2 24 4 4
4 4 4
4 4 44 4 4
!%t !% !% !% !%F 37  
  
  
4  
  
  
  
D C C C C [C& 3 3 3 3 \3 C    [$ I Id
 I I I [I I I I Ir(   )rx   torch.nnnnvllm.configr   vllm.loggerr   vllm.model_executor.utilsr   vllm.platformsr   r   r   r	   dictr   r5   __annotations__r
   Moduler   r   r)   r(   r   <module>r      sZ          5 5 5 5 5 5 # # # # # # C C C C C C + + + + + +	X		 EGT#tJ'$/?*@@@A F F FHJc4
+d3C.DDDE J J JDI DI DI DI DIRY DI DI DINYI YI YI YI YIry YI YI YI YI YIr(   