
    9j                        U d dl mZ d dlZd dlZ ej                  e      Zi Zded<   	 	 	 	 	 	 ddZ		 	 	 	 ddZ
ddZddZ	 	 	 	 	 	 dd	Z	 	 	 	 	 	 dd
ZddZddZ	 	 	 	 	 	 ddZddZy)    )annotationsNz2dict[torch._ops.OpOverload, torch._ops.OpOverload]_manual_out_variant_registryc                    |t         | <   y)z0Register a functional op -> out variant mapping.N)r   )functional_opout_ops     [/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_library/_out_variant.pyregister_out_variantr	      s    
 39 /    c                ,    t         j                  |       S )z;Return the manually registered out variant for op, or None.)r   get)ops    r   lookup_manual_out_variantr      s     (++B//r
   c                    | j                   j                  d      d   }|j                  d      ryt        d | j                  D               S )zd
    A schema is functional if no argument is written to and the name doesn't
    end with '_'.
    ::_Fc              3  4   K   | ]  }|j                     y wN)is_write).0args     r   	<genexpr>z!_is_functional.<locals>.<genexpr>'   s     <C3<<<s   )namesplitendswithany	arguments)schemaop_names     r   _is_functionalr       sH    
 kk%b)G<6+;+;<<<<r
   c                N    | j                   d uxr | j                   j                  S r   )
alias_infor   )r   s    r   _is_mutable_argr#   *   s     >>%A#..*A*AAr
   c                   | j                   D cg c]  }t        |      r| }}|j                   D cg c]  }t        |      r| }}t        |      t        |      k7  ryt        ||      D ]h  \  }}|j                  |j                  k7  r yt        |j                        t        |j                        k7  r y|j                  |j                  k7  sh y yc c}w c c}w )zOCompare two schemas by their non-mutable arguments (name, type, default value).FT)r   r#   lenzipr   strtypedefault_value)schema_aschema_br   non_mutable_args_anon_mutable_args_babs          r   _signatures_matchr0   .   s    
 *2););X#?SVCW#XX)1););X#?SVCW#XX
#&8"99&(:; 166QVVqvv;#aff+%??aoo-  YXs   CCC C c                H   t        | j                        dk(  ryt        | j                        t        |      k7  ryt        | j                  |      D ]N  \  }}|j                  |j                   y|j                  j                  |j                  j                  k7  sN y y)zFOut variant must return either nothing or the mutable args themselves.r   TF)r%   returnsr&   r"   
before_set)r   mutable_argsretr   s       r   _has_valid_out_variant_returnsr6   A   s    
 6>>a
6>>c,// 5 S>>!S^^%;>>$$(A(AA	
 r
   c                    | j                   }|j                  D cg c]  }t        |      s|j                   c}S c c}w )z5Get the names of out arguments for an out variant op.)_schemar   r#   r   )r   r   r   s      r   get_out_arg_namesr9   U   s0    ^^F & 0 0IOC4HCHHIIIs   ==c                   | j                   }t        |      st        d|  d|       | j                  }|j                  j                  d      d   }t        t        t        j                  |      |      }|j                         D ]  }t        ||      }t        j                  j                  |j                  vr6|j                   }t        ||      sO|j                  D cg c]  }t        |      s| }	}t!        |	      t!        |j"                        k7  rt%        ||	      st        d| d|       |c S  yc c}w )zU
    Given a functional operator overload, return its corresponding out variant.
    z#Failed to find out variant for op 'z&' as its schema is not functional. 
  r      zOut variant z^ has invalid returns. Expected either no returns or returns that alias the mutable args, got: N)r8   r    RuntimeError	namespacer   r   getattrtorchops	overloadsTagout_varianttagsr0   r   r#   r%   r2   r6   )
r   r   r=   r   torch_packetoverload_name	candidatecandidate_schemar   r4   s
             r   to_out_variantrI   [   sS    ZZF&!1" 6
 	
 Ikk%a(G7599i8'BL &//1 L-8	 99  	6$,, )9: ,55
9MC
 
 |FNN 33-.>Myk *()+  58 
s   $E 5E c           	         t        |       }| t        |       }t        d|  d|xs d       ||k7  rt        d|  d| d| d      y)	z
    Checks that to_out_variant returns the expected out variant for a functional op.
    Raises AssertionError if the out variant is not valid.
    Nz#We did not find an out variant for a  . Some common mistakes include:
  1. The out variant is missing the torch.Tag.out_variant tag.
  2. The out variant is not an overload of the original op (e.g., 'op.out' or 'op.overload_out') 
  3. The out variant's input arguments does not match the functional op's signature (excluding the mutable args).
  4. The original operator is not functional.
Overloads tagged with out_variant:
z  (none)zto_out_variant(z) returned z, but expected z8. The out variant name does not match the functional op.)rI   _get_out_variants_infoAssertionError)r   expected_out_opr   tagged_infos       r   check_out_variantrO      s     M*F~,];1- A3 (j)+
 	
  m_Kx @+, -EF
 	
 !r
   c                   | j                   }| j                  j                  j                  d      d   }t	        t	        t
        j                  |      |      }g }|j                         D ]V  }t	        ||      }t
        j                  j                  |j                  v s6|j                  d| d|j                          X dj                  |      S )zJCollect information about overloads tagged with out_variant for debugging.r   r;   z  - z: 
)r=   r8   r   r   r>   r?   r@   rA   rB   rC   rD   appendjoin)r   r=   r   rE   overloads_inforF   rG   s          r   rK   rK      s    ''I##((..t4Q7G7599i8'BL "N%//1 OL-8	99  INN2!!Dr):K:K9L"MN	O 99^$$r
   )r   torch._ops.OpOverloadr   rU   returnNone)r   rU   rV   ztorch._ops.OpOverload | None)r   torch._C.FunctionSchemarV   bool)r   ztorch._C.ArgumentrV   rY   )r*   rX   r+   rX   rV   rY   )r   rX   r4   zlist[torch._C.Argument]rV   rY   )r   rU   rV   z	list[str])r   rU   rM   rU   rV   rW   )rV   r'   )
__future__r   loggingr?   	getLogger__name__logr   __annotations__r	   r   r    r#   r0   r6   r9   rI   rO   rK    r
   r   <module>ra      s    "   g!
 TV P U9(9!9 
900!0=B%% 
&#) 
(J.b
(
;P
	
6%r
   