o
    Xi                     @  s   d Z ddlmZ ddlZddlZddlmZmZmZm	Z	 ddl
Z
ddlmZ edZG dd dZG d	d
 d
Ze ZdddZddddddddZdS )Registry for aten functions.    )annotationsN)AnyCallable	GeneratorOptional)
_constantszV^(?P<namespace>[a-zA-Z0-9_]+)::(?P<name>[a-zA-Z0-9_]+)(?P<overload>\.[a-zA-Z0-9._]+)?$c                   @  s   e Zd ZdZdddZdS )OverloadedFunctionzOverloaded function.

    Attributes:
        name: Name of the op. E.g. "aten::add".
        overloads: Overloads function.
        complex: Support complex functions.
    namestrc                 C  s   || _ g | _g | _d S N)r
   	overloadscomplexselfr
    r   c/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/function_libs/torch_lib/registration.py__init__   s   
zOverloadedFunction.__init__N)r
   r   )__name__
__module____qualname____doc__r   r   r   r   r   r	      s    r	   c                   @  s\   e Zd ZdZdd ZdddddZdd Zdd Zdd Zdd Z	d ddZ
d!ddZdS )"Registryr   c                 C  s
   i | _ d S r   	_registryr   r   r   r   r   &      
zRegistry.__init__Fr   funcr   r
   r   r   boolreturnNonec                C  s   | j |t|}|r'|jrtjd| d|j ddd dS |j| dS |jr;tjd| d|j ddd dS |j| dS )zRegister a function.zComplex overload for 'z' already registered: .   )
stacklevelNzReal overload for ')r   
setdefaultr	   r   warningswarnappendr   )r   r   r
   r   overloaded_functionr   r   r   register)   s    zRegistry.registerc                 C  s
   | j | S r   r   r   r   r   r   __getitem__>   r   zRegistry.__getitem__c                 C  s
   || j v S r   r   r   r   r   r   __contains__A   r   zRegistry.__contains__c                 C  
   t | jS r   )iterr   r   r   r   r   __iter__D   r   zRegistry.__iter__c                 C  r-   r   )reprr   r   r   r   r   __repr__G   r   zRegistry.__repr__5Generator[tuple[str, OverloadedFunction], None, None]c                 c      | j  E d H  d S r   )r   itemsr   r   r   r   r4   J      zRegistry.items)Generator[OverloadedFunction, None, None]c                 c  r3   r   )r   valuesr   r   r   r   r7   M   r5   zRegistry.valuesN)r   r   r
   r   r   r   r    r!   )r    r2   )r    r6   )r   r   r   r   r   r*   r+   r,   r/   r1   r4   r7   r   r   r   r   r   #   s    
r   r
   str | tuple[str, ...]r    tuple[str, ...]c                 C  s`   t | tr	| f}n| }t |tstd|  |D ]}|ds%t|s-td| dq|S )Nz1Name must be a string or a tuple of strings, got z.defaultzInvalid name 'zq'. Must be in the form 'namespace::name' for default overloads or 'namespace::name.overload' for other overloads.)
isinstancer   tuple	TypeErrorendswith_QUALIFIED_OPERATOR_NAME_REGEX	fullmatch
ValueError)r
   namesname_r   r   r   _check_and_normalize_namesU   s   


rC   F)registry
trace_onlyprivater   rD   Optional[Registry]rE   r   rF   r   TCallable[[Callable], onnxscript.OnnxFunction | onnxscript.values.TracedOnnxFunction]c                  s&   du rt d fdd}|S )	a  Register a torch op.

    Args:
        name: Qualified ATen name of the function. E.g. "aten::relu", "aten::add.Tensor".
            Or a tuple of names e.g. ("aten::add.Scalar", "aten::add.Tensor").
            Default overloads should be specified by omitting the overload part,
            i.e. "aten::relu" instead of "aten::relu.default".
        registry: Registry to register the function to. If None, the default registry is used.
        trace_only: Whether the function should only be traced and not compiled.
        private: Whether the function is private (not directly exposed). It should
            be true for all functions with names starting with "_".
        complex: Whether the function expects complex-valued inputs.
    Nr   r   r    >onnxscript.OnnxFunction | onnxscript.values.TracedOnnxFunctionc                   sj   t jjtjdd}rt j|| }nt j|d| }d us!J tD ]}r*q%j|| d q%|S )N   )domainversion)opsetr   )	
onnxscriptr7   Opsetr   DOMAINTracedOnnxFunctionscriptrC   r*   )r   custom_opsetprocessed_funcrB   r   r
   rF   rD   rE   r   r   wrapper   s   ztorch_op.<locals>.wrapper)r   r   r    rI   )default_registry)r
   rD   rE   rF   r   rV   r   rU   r   torch_oph   s   rX   )r
   r8   r    r9   )r
   r8   rD   rG   rE   r   rF   r   r   r   r    rH   )r   
__future__r   rer&   typingr   r   r   r   rN   "onnxscript.function_libs.torch_libr   compiler>   r	   r   rW   rC   rX   r   r   r   r   <module>   s&   /
