o
    پi                     @  s   d dl mZ d dlZd dlmZmZmZmZmZm	Z	m
Z
 d dlZededZe
ddddddddZe
ddddd ddZe
dddddd!ddZe
ddddd"ddZ	d#ddddd$ddZG dd dZdS )%    )annotationsN)AnyCallableListOptionalTypeVarUnionoverloadF)boundTop_namemutates_args	out_shapeeagerfnr   Optional[str]r   Optional[List[str]]r   Optional[Union[int, str]]r   boolreturnc                C     d S N )r   r   r   r   r   r   r   N/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/utils/custom_op.pyregister_custom_op      r   )r   r   r   	fake_implOptional[Callable]c                C  r   r   r   )r   r   r   r   r   r   r   r   r      r   Callable[[F], F]c                 C  r   r   r   r   r   r   r   r   !      c                 C  r   r   r   )r   r   r   r   r   r   r   r   +   r    r   c          
        s   t  }t ddh}||ksJ d||  dv }dv }|r)|r)J d|s1|s1dd< d fd	d
}	| durC|	| S |	S )a*  
    A decorator to register a custom operator.

    Example usage:
    ```python
    # inplace operator, out_shape is None by default
    @register_custom_op(mutates_args=["x"])
    def add_1_(x: torch.Tensor) -> None:
        x.add_(1)

    # operator with output, out_shape indicates the position of output
    @register_custom_op(mutates_args=["x"], out_shape=0)
    def add(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
        return x.add_(y)
    ```

    :param fn: The function to be registered as a custom operator.
               If None, return a decorator.
    :type fn: Callable
    :param op_name: The name of the operator. If None, use the function name
    :type op_name: Optional[str]
    :param mutates_args: A list of argument names that are mutated in-place.
    :type mutates_args: List[str]
    :param out_shape: The position (int for positional, str for keyword) of the output-shape tensor.
                      It is used to generate a fake implementation for torch.compile compatibility.
                      If the operator is inplace and has no output, set to None.
    :type out_shape: Optional[List[Union[int, str]]]
    :param fake_impl: A fake implementation for the operator.
                      Only one of `out_shape` or `fake_impl` should be provided.
    :type fake_impl: Optional[Callable]
    :param eager: Whether to register the operator eagerly.
                  If False, the registration will be deferred until the first call.
                  If you met any issue with torch.compile, try to set eager=True.
                  Currently, to avoid misuse, we set eager=True by default.
    :type eager: bool
    :return: The registered JIT custom operator, or a decorator.
             NOTE: the real register will occur at the first call of the function.
    :rtype: Callable
    r   r   zUnexpected extra kwargs: z:Only one of `out_shape` or `fake_impl` should be provided.Nop_funcr   r   c                   s.   t dp| j| p
g d} r|jS |S )N)r   r!   r   r   )CustomOpWrapper__name__	real_impl)r!   wrapperr   extra_kwargsr   r   r   r   	decoratort   s   z%register_custom_op.<locals>.decorator)r!   r   r   r   )setkeys)
r   r   r   r   r'   extra_kwarg_keysexpected_kwarg_keyshas_out_shapehas_fake_implr(   r   r&   r   r   6   s&   /
	c                   @  s:   e Zd ZdddZd	d
 ZedddZedddZdS )r"   r   strr!   r   r   	List[str]c                 K  s"   || _ || _|| _|| _d | _d S r   )r   r!   r   r'   _impl)selfr   r!   r   r'   r   r   r   __init__   s
   
zCustomOpWrapper.__init__c                 O  s   | j |i |S r   )r$   )r2   argskwargsr   r   r   __call__   s   zCustomOpWrapper.__call__r   c                 C  sd   | j d u r/ttjj| jsddlm} || j| j| j	| j
d ttjj| j| _ | j d us/J | j S )Nr   )direct_register_custom_op)r   r!   r   r   )r1   hasattrtorchopssglangr   sglang.srt.utils.commonr7   r!   r   r   getattr)r2   r7   r   r   r   r$      s   
zCustomOpWrapper.real_implc                   sL   dj v r
j d S dj v sJ tjj d   fdd}|S )Nr   r   c                    s    d u rd S j | i |}|  ztt tr!|j  W S |j  W S  tt	fy?   t
d  dj d dw )Nz)Cannot find output argument at position `z` for custom operator `z` with signature `z`.)bindapply_defaultsr9   
empty_like
isinstanceintr4   	arguments
IndexErrorKeyErrorRuntimeErrorr   )r4   r5   r   r   r2   	signaturer   r   r      s(   
z,CustomOpWrapper.fake_impl.<locals>.fake_impl)r'   inspectrH   r!   )r2   r   r   rG   r   r      s   


zCustomOpWrapper.fake_implN)r   r/   r!   r   r   r0   )r   r   )r#   
__module____qualname__r3   r6   propertyr$   r   r   r   r   r   r"      s    
r"   )r   r
   r   r   r   r   r   r   r   r   r   r
   )r   r
   r   r   r   r   r   r   r   r   r   r
   )
r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )
__future__r   rI   typingr   r   r   r   r   r   r	   r9   r
   r   r"   r   r   r   r   <module>   sD    $

	L