U
    <c                     @   s   d dl Z d dlmZm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 d dlmZ ed	d
dgZedd
ddgZe jjZe jjZe jjZG dd de	ZdS )    N)tree_flattentree_map)normalize_function)clone_inputs)TorchDispatchMode)combinations)
namedtuple)deepcopyMutationZop_nameZarg_nameAliasingZoutput_numberc                   @   s.   e Zd Zdd Zdd Zdd Zdd	d
ZdS )SchemaCheckModec                 C   s   g | _ g | _g | _d S N)opsmutatedaliasingself r   M/tmp/pip-unpacked-wheel-gikjz4vx/torch/testing/_internal/schema_check_mode.py__init__   s    zSchemaCheckMode.__init__c                 C   s"   | j   | j  | j  d S r   )r   clearr   r   r   r   r   r   reset_cache"   s    

zSchemaCheckMode.reset_cachec                 C   s   t | jddi d S )Nsep,)printr   r   r   r   r   display_ops'   s    zSchemaCheckMode.display_opsr   Nc              	      s  dd dd }dd }dd d	d
 | j |jj t|||ddjtt t	
   fdd D }fddD }|||}	fddD }
t|	tr|	n|	f}t|}t|j}| tt|jjD ]0}|jj| }||j}|
|d k	r||}||}|
|}tt|D ]r}||| |r@|jjdkr@|ttj|ttj|std| dn| jt|jj|d|  q@tfddtt|d t|d |D r|ttj|std| dq| jt |jj| qt!tt|jj"dD ]R\}}||| || r6|ttj|ttj|s6td| d| dq6|	S )Nc                 S   s   t | tjkot |tjk}|r|| jtjkr||jtjkr||  | koxtj| |ddox|d | kox|d | j	k S dS )NT)Z	equal_nanr      F)
typetorchTensorlayout
sparse_csrsizeZallclosestridestorage_cdata)beforeaftermdZare_tensorsr   r   r   has_mutated+   s    z7SchemaCheckMode.__torch_dispatch__.<locals>.has_mutatedc              
   S   sT   zt j| |W S  tk
rN } z t|dr:W Y dS |W 5 d }~X Y nX d S )NzCannot inspect value of type F)r   _CZ	_overlaps	Exceptionstr
startswith)lhsrhs	exceptionr   r   r   has_aliased6   s    
z7SchemaCheckMode.__torch_dispatch__.<locals>.has_aliasedc                 S   s   | dkr| S dS )Nr   inputr   )namer   r   r   standardize_name?   s    z<SchemaCheckMode.__torch_dispatch__.<locals>.standardize_namec              
   S   sR   t | tjrNt| tjksNz| jW S  tk
rL } z|  W Y S d }~X Y nX | S r   )
isinstancer   r   r   elemAttributeError)etr   r   r   unwrapB   s    z2SchemaCheckMode.__torch_dispatch__.<locals>.unwrapc              
   S   s   t | tjrt| tjksdz| j}t| | jfW S  t	k
r` } z
W Y d S d }~X Y qX n"| j
tjkrt|  |  jfS d S r   )r5   r   r   r   r6   r	   r#   r$   r%   r7   r    r!   )r8   currentr9   r   r   r   parse_metadataJ   s    z:SchemaCheckMode.__torch_dispatch__.<locals>.parse_metadataT)Znormalize_to_only_use_kwargsc                    s   i | ]}|t  |qS r   r   get.0r3   )c_p_argsr:   r   r   
<dictcomp>b   s      z6SchemaCheckMode.__torch_dispatch__.<locals>.<dictcomp>c              	      s&   i | ]}|t  t|d  qS )r   )r   r   r>   r?   )r<   pre_argumentsr   r   rB   c   s      c                    s   i | ]}|t  |qS r   r=   r?   )rC   r:   r   r   rB   f   s      zaten::_unsafe_viewz	Argument z0 is not defined to alias output but was aliasingZoutput_c                 3   s    | ]\}}} |||V  qd S r   r   )r@   abc)r)   r   r   	<genexpr>~   s     z5SchemaCheckMode.__torch_dispatch__.<locals>.<genexpr>r   z* is not defined as mutable but was mutated   zOutputs z and z alias unexpectedly)#r   appendZ_schemar3   r   kwargsdictzipkeysr   valuesr5   tupler   
SchemaInfoZadd_argument_valuesrangelen	argumentsr>   Zmay_contain_aliasSchemaArgumentSchemaArgTypeoutputr2   RuntimeErrorr   r   anyr   Z
is_mutabler   r
   r   Zreturns)r   functypesargsrJ   r1   r4   Zcloned_argumentsZcloned_metadataoutrS   Z	tuple_outZschema_infoiargr3   r&   r(   r'   jr   )rA   r)   r<   rC   r:   r   __torch_dispatch__*   s`    	









".

z"SchemaCheckMode.__torch_dispatch__)r   N)__name__
__module____qualname__r   r   r   r`   r   r   r   r   r      s   r   )r   Ztorch.utils._pytreer   r   Ztorch.fx.operator_schemasr   Z!torch.testing._internal.jit_utilsr   Ztorch.utils._python_dispatchr   	itertoolsr   collectionsr   copyr	   r
   r   r*   Z_SchemaArgumentrT   Z_SchemaArgTyperU   Z_SchemaInforP   r   r   r   r   r   <module>   s   