U
    yh                     @   s   d dl Z d dlZddlmZmZmZmZ dejedddZ	dd	 Z
d
d Zedeje dedededejdiZdd Ze Ze jedddZdS )    N   )devicedtypeTensortypes )prototype_functionreturnc              	      s  t |   fdd}g }t }d}t j D ]P\}\}}t|sP|d |jt jj	krp|sp|
d d}|jt jjkr|d| d |jt kr|d| d	|j d
t  d t|j }	||kr|	ds|d| d d| d|	tdd  }	|| |jt jjkr8|
|	 d|  q2|jdk	rht|jtttfsh|d| d |
|	 d| d|j  q2t|| }
t|
dkr||
 d t j|}dd| d| S )a  Given a function with type hints, parses a schema.

    We make some assumptions to make our lives easier that correspond to how people
    write custom ops in real life:
    - none of the outputs alias any of the inputs or each other.
    - only the args listed in mutates_args are being mutated.

    Callers (e.g. the custom ops API) are responsible for checking these assumptions.
    c                    s   t d|  d  dd S )Nzinfer_schema(func): z Got func with signature ))
ValueError)whatsigr   M/var/www/html/venv/lib/python3.8/site-packages/torch/_library/infer_schema.pyerror_fn   s    zinfer_schema.<locals>.error_fnFz>We do not support positional-only args, varargs, or varkwargs.*Tz
Parameter z must have a type annotation.z has unsupported type . The valid types are: .r   zM is in mutable_args but only Tensors or collections of Tensors can be mutatedzTensor(az!)N z has an unsupported default value (we only support int, float, bool, None). Please file an issue on GitHub so we can prioritize this.=r   z in mutates_args were not found in the custom op's signature. mutates_args should contain the names of all args that the custom op mutates.(, z) -> )inspect	signatureset	enumerate
parametersitemssupported_paramkind	ParameterKEYWORD_ONLYappend
annotationemptySUPPORTED_PARAM_TYPESkeys
startswithlenadddefault
isinstanceintfloatboolparse_returnreturn_annotationjoin)r   Zmutates_argsr   paramsZ	seen_argsZsaw_kwarg_only_argidxnameparamZschema_typeZmutates_args_not_seenretr   r   r   infer_schema   sV    







 
r7   c                 C   s   | |ft j|  | dfg}dd }|rJ|| D ]}||| df q0|rv|t j|  D ]}||| df q\|r|| D ]}|t j| | df q|S )N?c                 S   s   t j|  t j|  gS N)typingSequenceList)typr   r   r   derived_seq_typesW   s    z(derived_types.<locals>.derived_seq_typesz[]z?[]z[]?)r:   Optionalr"   )Z	base_typeZcpp_typeZ	list_baseZoptional_base_listZoptional_list_baseresultr>   Zseq_typr   r   r   derived_typesO   s    rA   c                  C   s   t ddddftddddftddddftddddftddddftjddddftd	dddftd
dddfg} g }| D ]}|	t
|  qnt|S )Nr   TFSymIntr-   r.   strScalarZ
ScalarTypeZDevice)r   r,   r-   r.   rC   r   Numberr   r   extendrA   dict)datar@   liner   r   r   get_supported_param_typesi   s    rJ   r   zTensor[]rB   r-   r.   rD   c                 C   s   | d krdS t | }|tk	rH| t kr@|d|  dt d t|  S t | }|D ]"}|tkrV|d|  dt d qVdddd |D  d	 S )
Nz()zReturn has unsupported type r   r   r   r   c                 S   s   g | ]}t | qS r   )SUPPORTED_RETURN_TYPES).0argr   r   r   
<listcomp>   s     z parse_return.<locals>.<listcomp>r
   )r:   
get_origintuplerK   r&   get_argsr1   )r#   r   originargsrM   r   r   r   r/      s     

r/   )r5   r	   c                 C   s   | j tjjtjjfkS r9   )r   r   r    POSITIONAL_OR_KEYWORDr!   )r5   r   r   r   r      s    r   )r   )r   r:    r   r   r   r   CallablerC   r7   rA   rJ   r<   r,   r-   r.   rE   rK   r/   r%   r    r   r   r   r   r   <module>   s*   G      
