U
    yh!                     @   s   d dl mZ d dlmZ d dlmZ d dl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dd Zdd ZG dd deZdS )    )
namedtuple)deepcopy)combinationsN)normalize_function)_pytree)TorchDispatchMode)tree_mapMutationZop_namearg_nameAliasingZoutput_numberc              
   C   sv   t | tjrdS z8t| dkr$W dS t| D ]}t |tjs, W dS q,W n( tk
rp } z
W Y dS d }~X Y nX dS )NFr   T)
isinstancetorchTensorleniter	TypeError)iterablette r   U/var/www/html/venv/lib/python3.8/site-packages/torch/_subclasses/schema_check_mode.pyis_iterable_of_tensors    s    r   c                 C   sZ   g }| D ]L}t |tjr,||   qt|rJ|dd |D  q|| q|S )Nc                 S   s   g | ]}|   qS r   )detachclone).0r   r   r   r   
<listcomp>6   s     z clone_inputs.<locals>.<listcomp>)r   r   r   appendr   r   r   )argsinputsargr   r   r   clone_inputs/   s    r    c                   @   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selfr   r   r   __init__>   s    zSchemaCheckMode.__init__c                 C   s"   | j   | j  | j  d S r"   )r#   clearr$   r%   r&   r   r   r   reset_cacheF   s    

zSchemaCheckMode.reset_cachec                 C   s   t | jddi d S )Nsep,)printr#   r&   r   r   r   display_opsK   s    zSchemaCheckMode.display_opsr   Nc              	      s   dd   f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 ]}|jj| }||j}|
|d k	r||}||}|
|}tt|D ]}d}||| |r|jj|kr|ttj|ttj|std| dn| jt|jj|d|  ||| krLt|tjrL|ttj|sL|tj jj j!tj jj"j!fkrLtdt#| d| qLt$fddtt%&|t%&||D r|ttj|sztd| dq| j't(|jj| qt)tt|jj*dD ]R\}}||| || r|ttj|ttj|std| d| dq|	S ) Nc                 S   s&   | j rt| |S tj| |ddS d S )NT)Z	equal_nan)Zis_quantizedr   equalZallclose)lhsrhsr   r   r   bitwise_equalO   s    z9SchemaCheckMode.__torch_dispatch__.<locals>.bitwise_equalc                    sz   t | tjkot |tjk}|rv| jtjkrv|jtjkrv|  | kor | |or|d | kor|d | jk S dS )Nr      F)	typer   r   layout
sparse_csrsizestride_typed_storage_cdata)beforeaftermdZare_tensors)r2   r   r   has_mutatedW   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)r0   r1   	exceptionr   r   r   has_aliasedf   s    
z7SchemaCheckMode.__torch_dispatch__.<locals>.has_aliasedc                 S   s   | dkr| S dS )Nr'   inputr   )namer   r   r   standardize_nameo   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"   )r   r   r   r4   elemAttributeError)er   r   r   r   unwrapr   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"   )r   r   r   r4   rH   r   r8   r9   r:   rI   r5   r6   )rJ   currentr   r   r   r   parse_metadataz   s    
z:SchemaCheckMode.__torch_dispatch__.<locals>.parse_metadataT)Znormalize_to_only_use_kwargsc                    s   i | ]}|t  |qS r   r   getr   rF   )c_p_argsrK   r   r   
<dictcomp>   s     z6SchemaCheckMode.__torch_dispatch__.<locals>.<dictcomp>c              	      s,   i | ]$}| fd dt |D qS )c                    s   g | ]} |qS r   r   )r   a)rM   r   r   r      s    zASchemaCheckMode.__torch_dispatch__.<locals>.<dictcomp>.<listcomp>)pytreetree_leavesrO   rP   )rM   pre_argumentsr   r   rR      s
    
c                    s   i | ]}|t  |qS r   rN   rP   )rV   rK   r   r   rR      s     )zaten::_unsafe_viewzaten::unsafe_splitz	Argument z0 is not defined to alias output but was aliasingZoutput_zoDispatcher operators below autograd are not allowed to directly return inputs.
However, we found that `outputs[z] is c                 3   s    | ]\}}} |||V  qd S r"   r   )r   rS   bc)r>   r   r   	<genexpr>   s   z5SchemaCheckMode.__torch_dispatch__.<locals>.<genexpr>z* is not defined as mutable but was mutated   zOutputs z and z alias unexpectedly)+r#   r   Z_schemarF   r   kwargsdictzipkeysr    valuesr   tupler   
SchemaInfoZadd_argument_valuesranger   	argumentsrO   Zmay_contain_aliasSchemaArgumentSchemaArgTypeoutputrE   RuntimeErrorr%   r   r   r   Z
is_mutableZatenZliftdefaultZ
lift_freshrA   anyrT   rU   r$   r	   r   Zreturns)r'   functypesr   r[   rD   rG   Zcloned_argumentsZcloned_metadataoutrc   Z	tuple_outZschema_infoir   rF   r;   r=   r<   jZ
unsafe_opsr   )r2   rQ   r>   rM   rV   rK   r   __torch_dispatch__N   s    	   














  



z"SchemaCheckMode.__torch_dispatch__)r   N)__name__
__module____qualname__r(   r*   r.   ro   r   r   r   r   r!   =   s   r!   )collectionsr   copyr   	itertoolsr   r   Ztorch.fx.operator_schemasr   Ztorch.utilsr   rT   Ztorch.utils._python_dispatchr   Ztorch.utils._pytreer   r	   r   r?   Z_SchemaArgumentrd   Z_SchemaArgTypere   Z_SchemaInfora   r   r    r!   r   r   r   r   <module>   s   