o
    Zh!                     @   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g d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Mutationop_namearg_nameAliasing)r
   r   Zoutput_numberc                 C   s`   t | tjrdS zt| dkrW dS t| D ]}t |tjs" W dS qW dS  ty/   Y dS w )NFr   T)
isinstancetorchTensorleniter	TypeError)iterablet r   R/var/www/auris/lib/python3.10/site-packages/torch/_subclasses/schema_check_mode.pyis_iterable_of_tensors!   s   r   c                 C   sZ   g }| D ]&}t |tjr||   qt|r%|dd |D  q|| q|S )Nc                 S   s   g | ]}|   qS r   )detachclone).0r   r   r   r   
<listcomp>7   s    z clone_inputs.<locals>.<listcomp>)r   r   r   appendr   r   r   )argsZinputsargr   r   r   clone_inputs0   s   r   c                   @   s0   e Zd ZdddZdd Zdd Zdd
dZdS )SchemaCheckModereturnNc                 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_cacheG   s   

zSchemaCheckMode.reset_cachec                 C   s   t | jddi d S )Nsep,)printr#   r&   r   r   r   display_opsL   s   zSchemaCheckMode.display_opsr   c              	      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 }||i |}	fddD }
t|	trd|	n|	f}t|}t|j}| tt|jjD ]}|jj| }||j}|
|d urE||}||}|
|}tt|D ]i}d}||| |r|jj|vr|ttj|ttj|std| d| jt|jj|d|  ||| u rt|tjr|ttj|s|tj jj j!tj jj"j!fvrtdt#| d| qt$fddtt%&|t%&||D rE|ttj|s:td| d| j't(|jj| q~t)tt|jj*dD ])\}}||| || ry|ttj|ttj|sytd| d| dqQ|	S ) Nc                 S   s"   | j r	t| |S tj| |ddS )NT)Z	equal_nan)Zis_quantizedr   equalZallclose)lhsrhsr   r   r   bitwise_equalP   s   z9SchemaCheckMode.__torch_dispatch__.<locals>.bitwise_equalc                    sz   t | tjkot |tjk}|r;| jtjkr;|jtjkr;|  | ko9 | |o9|d | ko9|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_mutatedX   s   z7SchemaCheckMode.__torch_dispatch__.<locals>.has_mutatedc              
   S   sJ   zt j| |W S  ty$ } zt|drW Y d }~dS |d }~ww )NzCannot inspect value of type F)r   _CZ	_overlaps	Exceptionstr
startswith)r0   r1   	exceptionr   r   r   has_aliasedg   s   z7SchemaCheckMode.__torch_dispatch__.<locals>.has_aliasedc                 S   s   | dkr| S dS )Nr'   inputr   )namer   r   r   standardize_namep   s   z<SchemaCheckMode.__torch_dispatch__.<locals>.standardize_namec                 S   s>   t | tjrt| tjksz| jW S  ty   |  Y S w | S r"   )r   r   r   r4   elemAttributeError)er   r   r   unwraps   s   z2SchemaCheckMode.__torch_dispatch__.<locals>.unwrapc                 S   st   t | tjr8t| tjks'z| j}t| | jfW S  t	y&   Y d S w | j
tjkr8t|  |  jfS d S r"   )r   r   r   r4   rH   r   r8   r9   r:   rI   r5   r6   )rJ   currentr   r   r   parse_metadata{   s   
z:SchemaCheckMode.__torch_dispatch__.<locals>.parse_metadataT)Znormalize_to_only_use_kwargsc                       i | ]}|t  |qS r   r   getr   rF   )c_p_argsrK   r   r   
<dictcomp>       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_leavesrP   rQ   )rM   pre_argumentsr   r   rS      s    c                    rN   r   rO   rQ   )rX   rK   r   r   rS      rT   )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   rU   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   	argumentsrP   Zmay_contain_aliasSchemaArgumentSchemaArgTypeoutputrE   RuntimeErrorr%   r   r   r   Z
is_mutableZatenZliftdefaultZ
lift_freshrA   anyrV   rW   r$   r	   r   returns)r'   functypesr   r]   rD   rG   Zcloned_argumentsZcloned_metadataoutre   Z	tuple_outZschema_infoir   rF   r;   r=   r<   jZ
unsafe_opsr   )r2   rR   r>   rM   rX   rK   r   __torch_dispatch__O   s   	

















z"SchemaCheckMode.__torch_dispatch__)r!   N)r   N)__name__
__module____qualname__r(   r*   r.   rr   r   r   r   r   r    >   s
    
r    )collectionsr   copyr   	itertoolsr   r   Ztorch.fx.operator_schemasr   Ztorch.utilsr   rV   Ztorch.utils._python_dispatchr   Ztorch.utils._pytreer   r	   r   r?   Z_SchemaArgumentrf   Z_SchemaArgTyperg   Z_SchemaInforc   r   r   r    r   r   r   r   <module>   s    