
    ΧgV                       U d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlZd dlmZ d dlmZmZmZmZ d dlmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% d dl&m'Z' d d	l(m)Z)m*Z* d d
l+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6m7Z7 d dl8m9Z9 eee-ee:ef         f         e
e:         f         Z;i a<de=d<    e>e,          Z?dMdZ@dNdZAe'dOd            ZBdPd&ZCdQd)ZDdRd2ZEdSd4ZFdTd@ZGdAZHdUdCZIdVdGZJdWdIZKdXdLZLdS )Y    )annotationsN)defaultdict)AnyCounterDictSequenceSetTuple)cpp)
DerivativeDifferentiabilityInfoForwardDerivativeSavedAttribute)	BaseCTypeBindingboolTCppSignatureGrouplayoutTlongT
NamedCTypeOptionalCTypescalarTypeTSpecialArgNamestringTsymIntArrayRefTSymIntTtensorGeometryTtensorOptionsTtypeAndSizeTVectorCType)with_native_function)$get_grouped_by_view_native_functionsparse_native_yaml)AUTOGRAD_KEYSFunctionSchemaNativeFunctionNativeFunctionsViewGroupOperatorName
SchemaKindTypeVariant)	concatMapIDENT_REGEXsplit_name_params)
YamlLoaderz$dict[tuple[str, str], DerivativeRet]_GLOBAL_LOAD_DERIVATIVE_CACHEinfos6dict[FunctionSchema, dict[str, DifferentiabilityInfo]]view_groupslist[NativeFunctionsViewGroup]returnNonec                   d |D             }i }|                                  D ]}d }i }|                                D ]\\  }}|                    |j        j        j        d           }|0|j        )|                    |          }	|	|	j        j        }
|	||<   \ t          |          dk    r|
| vr	|
J |||
<   |                     |           d S )Nc                2    i | ]}|j         j        j        |S  )viewfuncname.0gs     g/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchgen/packaged/autograd/load_derivatives.py
<dictcomp>z-add_view_copy_derivatives.<locals>.<dictcomp>H   s4     H H H !!H H H    r   )	valuesitemsgetr;   r<   	view_copy%create_view_copy_from_view_derivativelenupdate)r1   r3   view_name_to_group
view_infosinfo_dispatch_dictmaybe_view_group!view_copy_differentiability_infosdispatch_keyinfoview_copy_info	fn_schemas              r@   add_view_copy_derivativesrS   C   s&   
H H%0H H H J#llnn F F,.)"4":":"<"< 
	 
	L$155din6I4PP+0@0J0V!%!K!K$" " "- . 3 8IFT5lC011A55)5:P:P((($EJy!	LLrB   derivatives_yaml_pathstrnative_yaml_pathtags_yaml_pathDerivativeRetc                z   | |f}|t           vrt          |           5 }t          j        |t                    }d d d            n# 1 swxY w Y   t          ||          j        }t          |          }t          d |          }d |D             }	t          t                    }
i }|D ]c}|
|j                                                                     |           t          |j                  |vsJ ||t          |j                  <   dt          t                               }i }t!                      }|D ]Z}d|vr9|                    d          }|                    dd           }|d|id}|r||d<   t%          ||
|||          \  }}|||<   [t'          ||	           ||ft           |<   t           |         S )	N)Loaderc                x    t          | t                    r| gn"t          |                     d                    S )NT)include_copy)
isinstancer&   list	functions)r?   s    r@   <lambda>z"load_derivatives.<locals>.<lambda>t   s8    !^,,6qccakktk4455 rB   c                <    g | ]}t          |t                    |S r9   )r]   r'   r=   s     r@   
<listcomp>z$load_derivatives.<locals>.<listcomp>y   s9     
 
 
!566

 
 
rB   dispatchr<   output_differentiabilityDefault)r<   rc   )r0   openyamlloadr/   r#   native_functionsr"   r,   r   r^   r;   	signatureappendrU   r   setpopcreate_differentiability_inforS   )rT   rV   rW   keyfdefinitionsfuncs!native_functions_with_view_groupsri   r3   functions_by_signaturefunctions_by_schemafunction
op_counterr1   used_dispatch_keys	defn_dictspecificationrd   r<   per_dispatch_diffinfoss                        r@   load_derivativesr|   e   se   
 !"2
3C
///'(( 	:A)Aj999K	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: ""2NCCT -QQV,W,W)$6 6 .	
 

 
6
 
 
  	 :<( 	? 	?H"8=#:#:#<#<=DDXNNNx}%%-@@@@@6>HM 2 233 S\^^

 IK'*uu$ 	1 	1I** )f 5 5+4==., ,( &3I@VWW	+ U<TI89+H&#", ,(D( 1E$KK!%555
 .34F-F%c*(--s   AA
A
rp   r&   Sequence[Binding]c                    t          j        | d          }|j        |j                                        S |j                                        S )NF)method)r   from_native_functionsymint_signature	argumentsrj   )rp   sigss     r@   cpp_argumentsr      sI    1!EBBBD($..000~'')))rB   formula	var_namestuple[str, ...]available_named_gradientsSequence[str]r   c                   }d t          |           D             }t          d t          j        |           D                       }t          d | j        j        D                       }d t          ||          D             }t          ||          \  }	t          ||          \  }
fd|D             }t                    D ]f}|t          | j        j                  k    rGt          dt          j        | j                   d| dt          | j        j                   d	          gt          |||	|
|
          S )Nc                @    g | ]}|j                                         S r9   )nctyperemove_const_refr>   as     r@   rb   z%create_derivative.<locals>.<listcomp>   s3     # # #()!!### # #rB   c              3  *   K   | ]}|d k    r|ndV  dS )selfresultNr9   )r>   ns     r@   	<genexpr>z$create_derivative.<locals>.<genexpr>   s.      UUAa6kkxUUUUUUrB   c              3  f   K   | ],}t          j        |d                                           V  -dS )T)symintN)r   return_typer   r>   rs     r@   r   z$create_derivative.<locals>.<genexpr>   sN        ?@$'''88::     rB   c                4    g | ]\  }}t          ||          S r9   )r   )r>   r<   types      r@   rb   z%create_derivative.<locals>.<listcomp>   s3       #-4
4  rB   c                b    h | ]+}t          j        t          j        |                    )|,S r9   )researchr-   format)r>   r<   r   s     r@   	<setcomp>z$create_derivative.<locals>.<setcomp>   sF       9['--w77  rB   z3Out of bounds grads access: derivative formula for z used grads[z ], but the forward only returns z	 outputs.)r   original_formular   saved_inputssaved_outputsnamed_gradients)r   tupler   return_namesr;   returnszipsaved_variablesused_gradient_indicesrH   RuntimeErrorr<   r   )rp   r   r   r   r   r   r   return_typesnamed_returnsr   r   used_named_gradientsis    `           r@   create_derivativer      s    # #-:1-=-=# # #I UUAQRSATATUUUUUL  DEFN    L 14\<1P1P  M ,GY	JJG\,WmYOOG]   -   #7++  AFN####`chqvFVFV ` `` `ADQV^ATAT` ` `   $ )!#,   rB   namesr   c           	        |}d }| j         j        D ]}|j        |v r|d}||j        fz   }||dk    r8t	          | j         j                  dk    sJ | j         j        d         j        f}na|D ]^}t          j        d|          }t	          |          dk    r4|d}t          |d                   }|| j         j        |         j        fz   }_|
J d            t          |||d d dd          S )	Nr9   )r      r   z^result(\d+)$z4No matching output for forward derivative definitionFr   r   	var_typesrequired_inputs_fw_gradrequired_inputs_primalrequired_original_self_valueis_reusing_outplace_formula)	r;   r   r<   r   rH   r   findallintr   )	rp   r   r   r   r   r   var_nameresarg_idxs	            r@   create_forward_derivativer      s5    I)-IV^ . .6Y 	!QVI-I ##qv~&&!++++*/1II% L Lj!18<<s88q== ($&	!#a&kkG )QV^G-D-I,K KI  "X    $#%*$)   rB   	defn_nameall_arg_names	list[str]derivativeslist[Derivative]forward_derivativeslist[ForwardDerivative]args_with_derivativesc                    d" fd}g }|D ]}|j         }	 ||	d          }
|	dk    rW j                                        t          j        k    sJ d	 j        j         d
            t                    dk    r1t          |          dk    st          |d         j                  dk    rt          d d          t          |          dk    st          d d          |d         j	        }d         j        d#fd}t          j        t          j        d          ||          }D ]7}|j        d#fd}t          j        t          j                  ||          }8d| d}t          |          }
|}	n1|	dk    r*t          |          dk    st          |d         j                  dk    rt          d d          d D             }t          |          dk    sJ g }|D ] |v rdz   |                               ! j                                        rdz  t"          j         j        v rd dd                    |           d}nAt"          j         j        v sJ |d          d dd                    |dd                     d}t          |          }
|}	 ||	d          }|                    t-          |	|j        |j        |
|d d !                     |S )$Nr   rU   postfixr5   r   c                   	j         j        j        j                            d          }t	                      }D ]}|j        dv r|s|j        }t          j        t          j	        |          |           }|rt          d d| d| d| d	          t          j        t          j	        ||z             |           }|r|                    |           t          |          S )N	_foreach_)zat::TensorListzconst at::ITensorListRef &zThe forward formula for z is using the base name of the z- argument which is ambiguous. You should use z"_p to access the primal value and z_t to access the tangent.)r;   r<   base
startswithrl   r   r   r   r-   r   r   addr   )
r   r   
is_foreachrequired_inputsargarg_namefoundr   r   rp   s
          r@   find_required_inputsz=postprocess_forward_derivatives.<locals>.find_required_inputs  s&   V[%*55kBB
%%( 	. 	.CLLL" M xHIk0::GDDE "Ey E EYa E ECKE E!)E E E   Ik0G1CDDgNNE .##H---_%%%rB   _tauto_element_wisez"Cannot use auto_element_wise with z" because it is an in-place variantr   r   Derivative definition of z in derivatives.yaml defines the forward definition of gradient as element_wise but this only works for functions with a single differentiable input and a single differentiable output.z in derivatives.yaml defines the forward definition of gradient as element_wise but it does not defines the gradient formula for its argument which is required.mr   c                b    |                      d            d|                      d           S )Nr   z	_t.conj()   group)r   
input_names    r@   replz-postprocess_forward_derivatives.<locals>.repl\  s0    ''!**GjGG1771::GGGrB   gradc                b    |                      d            d|                      d           S )Nr   _pr   r   )r   r   s    r@   r   z-postprocess_forward_derivatives.<locals>.reple  s0    ggajjB(BBaggajjBBBrB   (z).conj()auto_linearz in derivatives.yaml defines the forward definition of gradient as linear but this only works for functions with a single differentiable output.c                    g | ]	}|j         
S r9   r<   )r>   r   s     r@   rb   z3postprocess_forward_derivatives.<locals>.<listcomp>  s    HHH3chHHHrB   _symintzat::z, ).r   Fr   )r   rU   r   rU   r5   r   )r   r   r5   rU   )r   r;   kindr)   inplacer<   rH   r   r   r   r   subr-   r   r   rk   
has_symintr+   rv   variantsjoinr   r   r   )rp   r   r   r   r   r   r   updated_derivativesdefnr   required_inputs_tangentbackward_formular   
fw_formular   diff_arg_namesnew_argsr   r   r   s   ``   `            @@r@   postprocess_forward_derivativesr     s4   & & & & & & & &4 46# z
 z
,"6"6w"E"E)))!3333cAFKccc 433 .//144*++a//*1-7881<<"4	 4 4 4   {##q(("W	 W W W  "  +1~>.q16JH H H H H H  26 : :DBRSSJ - T T8C C C C C C  VK$6x$@$@$
SS

 2Z111J ',M&:&:# GG%%'((1,,*1-7881<<"I	 I I I   IH2GHHHN~&&**** H) * *~--'$H)))) v  "" 'Y&	 1:--FIFF		(0C0CFFF

~3333 (TTiTT$))HQRRL:Q:QTTT
 ',N&;&;# G "6!5gt!D!D""..(?'=-2,1  
	
 
	
 
	
 
	
 rB   boolc                4    |D ]}|| vc S t          d          )Nz Expected `names` to be non-empty)r   )r   r   r<   s      r@    is_forward_derivative_definitionr     s3      ) )=((((
9
:
::rB   ry   dict[Any, Any]rt   *dict[FunctionSchema, list[NativeFunction]]ru   dict[str, NativeFunction]rw   Counter[str]rx   set[str]7tuple[FunctionSchema, dict[str, DifferentiabilityInfo]]c                6   d2d}d3d
d4dt           d5fd            }|                     d          }t          |          \  }|                     dd          d}	rDt          d D                       r+t	                    dk    rt          d| d          }	dg|                    |          }
|
sHd                    fd|                                D                       }t          d| d|           |
j	        
                                }||         }t	          |          dk    rKd                    fd|                                D                       }t          d | d!| d"|            ||          }d#d$ t          |          D             v rt          d% d&          d'd( t          |          D             v rt          d% d)          i }| d*                                         D ] \  }|d+k    r&|t          vrt          d,| d-| d.t                     ||vr|                    |            ||          \  }}}}}t                      }|D ]}||j        z  }d}|r6t!                    }|d+k    r||z   }| ||          }||xx         dz  cc<   t#          ||||t%          d/ |D                       t%          d0 |D                       |||||	1          ||<   |j	        |fS )6z3Processes a single entry `defn` in derivatives.yamlr_   Sequence[NativeFunction]r<   rU   r5   r&   c                   | D ]Z}|j                                         s?|j                                         s&|t          |j         j        j                  k    r|c S [|dz   t          j        | d         j                   k    sJ | d         S )N_r   )r;   is_functional_fn	is_out_fnrU   r<   r   )r_   r<   rp   s      r@   canonical_functionz9create_differentiability_info.<locals>.canonical_function  s      	 	AF++--((** C 01111czSXil&7888888|rB   	raw_namesr   c                Z    t          d |                     d          D                       S )z(Given "foo, bar", return ["foo", "bar"].c              3  >   K   | ]}|                                 V  d S N)strip)r>   xs     r@   r   zEcreate_differentiability_info.<locals>.split_names.<locals>.<genexpr>  s*      ==1QWWYY======rB   ,)r   split)r  s    r@   split_namesz2create_differentiability_info.<locals>.split_names  s+    ==	(<(<======rB   r   r   Sequence[Derivative]r6   c           	        d}d}d}g }|D ]}|j         }|p3t          t          j        t	          j        d          |                    }|t          t          j        t	          j        d          |                    z  }|pt          |j                  }|                    t          |                     |t          |          k    sJ |t          |          k    }|r|dk    rt          d|  d          |r't          |          dhk    rt          d|  d          |r|s|dk    rt          d|  d          d	S d	S )
z
        Check for some subtle mistakes one might make when writing derivatives.
        These mistakes will compile, but will be latent until a function is
        used with double backwards.
        Fr   r   gradsr   zx in derivatives.yaml illegally mixes use of 'grad' and 'grads'. Consider replacing occurrences of 'grad' with 'grads[0]'z in derivatives.yaml solely refers to 'grads[0]'.  If the first output is indeed the only differentiable output, replace 'grads[0]' with 'grad'; otherwise, there is a likely error in your derivatives declaration.z in derivatives.yaml illegally mixes use of "grad_RETURN_NAME" and "grad" or "grads[x]". Use only one method for identifying gradients.N)r   r   r   r   r-   r   rH   r   extendr   r   rl   )	r   r   	uses_gradnum_grads_usesuses_named_gradsused_grads_indicesdr   only_used_grads_indicess	            r@   check_grad_usagez7create_differentiability_info.<locals>.check_grad_usage  s    	 (* 	F 	FAiG! T
;-f55w??& &I c"*[-?-H-H'"R"RSSSN/J48I3J3J%%&;G&D&DEEEE
 %7!8!88888 #1C8J4K4K"K 	!++8I 8 8 8   # 	s+='>'>1#'E'EI       	 	nq.@.@=I = = =  	 	.@.@rB   rp   ituple[Sequence[Derivative], Sequence[ForwardDerivative], Sequence[Binding], Sequence[str], Sequence[str]]c           
        g }g }g }t                      d t          |           D             }d | j        j        D             }pdgt	          | j        j                  z  }d t          | j        j        |          D             }t                                                    D ]}|         }	 |          }
|
D ](}||v r"||v rJ d| j        j         d| d| d            )t          ||
          r%|
                    t          | |	|
                     u|	                                                                d	k    r||
z  }t          | |	|
|          }|
                    |           t          |
          z  ߉                    |          }|rt!          d
 d|           fdt          |           D             }t#          | ||||          } |           |||||fS )Nc                    g | ]	}|j         
S r9   r   r   s     r@   rb   zMcreate_differentiability_info.<locals>.set_up_derivatives.<locals>.<listcomp>  s    :::A:::rB   c                    g | ]	}|j         
S r9   r   r   s     r@   rb   zMcreate_differentiability_info.<locals>.set_up_derivatives.<locals>.<listcomp>  s'     
 
 
AF
 
 
rB   Tc                l    g | ]1\  }}|r*|j         |j                                        'd|j          2S )Ngrad_)r<   r   is_tensor_like)r>   retdifferentiables      r@   rb   zMcreate_differentiability_info.<locals>.set_up_derivatives.<locals>.<listcomp>(  sZ     	%
 	%
 	%
#^		%
 $'')) % CH
 %$$rB   z-While processing the derivative formula for 'z' wrt 'z', expected 'z0' to not be both an input arg and named return. non_differentiablezderivatives definition for zB have overlapped non_differentiable and differentiable variables: c                &    g | ]}|j         v |S r9   r   )r>   r   args_with_derivatives_sets     r@   rb   zMcreate_differentiability_info.<locals>.set_up_derivatives.<locals>.<listcomp>S  s-     !
 !
 !
165N+N+NA+N+N+NrB   )rl   r   r;   r   rH   r   sortedkeysr<   r   rk   r   lowerr  r   intersectionr   r   )rp   r   r   non_differentiable_arg_namesr   all_ret_namesdifferentiabilityr   r  r   r   r<   
derivativeoverlapr   r&  r  r   r   rd   r  s                  @r@   set_up_derivativesz9create_differentiability_info.<locals>.set_up_derivatives	  s    )+7924$.1ee!::q)9)9:::
 
FN
 
 
 5TQV^ATAT8T	%
 	%
'*16>;L'M'M	%
 	%
 	%
!  		,, 	< 	<I9oGK	**E   M11dm6K6K6KXAFK X XX\ X X!%X X X 7L6K6K
 0uEE 
<#**+DQQV+W+WXXXX==??((**.BBB0E900!27E+D" "J  &&z222-U;--+889UVV 	;d ; ;18; ;  !
 !
 !
 !
$Q''!
 !
 !

 >!
 
 	K000 !(%
 	
rB   rd   Nc              3  @   K   | ]}t          |t                    V  d S r
  )r]   rU   )r>   diffs     r@   r   z0create_differentiability_info.<locals>.<genexpr>s  s=       ( ("&
4( ( ( ( ( (rB   r   zNot supported: for z,output_differentiability must either be List[bool] or a List[str] where each str is a condition. In the case where it is a condition, we only support single-output functions. Please file us an issue. T
c              3  \   K   | ]&\  }}t          j        |j                  k    "|V  'd S r
  )r   r<   r;   r>   kvr   s      r@   r   z0create_differentiability_info.<locals>.<genexpr>  sF       
 
!Q!&9I9IY9V9VA9V9V9V9V
 
rB   z)could not find ATen function for schema: z .  Available signatures:
r   c              3  l   K   | ].\  }}t          j        |          k    t          |          V  /d S r
  )r   r<   rU   r5  s      r@   r   z0create_differentiability_info.<locals>.<genexpr>  sL       
 
1x{{i'' FF''''
 
rB   z3could not find ATen function for legacy signature: z corresponding to schema z9.  Please report a bug to PyTorch. Available signatures:
grad_input_maskc              3  $   K   | ]}|j         V  d S r
  r   r   s     r@   r   z0create_differentiability_info.<locals>.<genexpr>  s$      FFQVFFFFFFrB   zSchema for z has an argument named grad_input_mask, but this name would be shadowed by our codegen. Please use a different name in native_functions.yaml.r   c              3  $   K   | ]}|j         V  d S r
  r   r   s     r@   r   z0create_differentiability_info.<locals>.<genexpr>  s$      ==qAF======rB   zy has an argument named result, but this is only allowed for outputs.Please use a different name in native_functions.yaml.rc   re   zInvalid dispatch key z in derivatives.yaml for z, expected key to be one of c                &    g | ]}|j         D ]}|S r9   )r   r>   r  r7  s      r@   rb   z1create_differentiability_info.<locals>.<listcomp>  s'    @@@q@@A@@@@rB   c                &    g | ]}|j         D ]}|S r9   )r   r=  s      r@   rb   z1create_differentiability_info.<locals>.<listcomp>  s'    AAAqAAAAAAArB   )r<   r;   opr   r   all_saved_inputsall_saved_outputsr   r   r   r+  rd   #output_differentiability_conditions)r_   r  r<   rU   r5   r&   )r  rU   r5   r   )r   rU   r   r  r5   r6   )rp   r&   r5   r  )r!   rm   r.   anyrH   r   rE   r   rD   r;   rj   r   _VALID_AUTOGRAD_KEYSr   rl   r   _create_op_prefixr   
dedup_vars)ry   rt   ru   rw   rx   r  r0  rz   r  rB  schema_functionavailrj   r_   	canonicaldiffinfo_dictro   r   r   r   r+  r   r   r  r?  	op_prefixr  r   r   rd   r  s                             @@@@@r@   rn   rn     s0      > > > >1 1 1 1f `
 `
 `
 `
 `
 `
 `
 `
 `
 `
F MM&))M$]33LIq  )}}-GNN*.' *C ( (*B( ( ( % % * '((A---m - - -   /G+$(6 )--m<<O 
		 
 
 
 
-3355
 
 
 
 
 1 1 1).1 1
 
 	
  $..00I&y1I
9~~		 
 
 
 
.4466
 
 
 
 

 .) . .'4. .&+. .
 
 	
 #"9i88IFF]9-E-EFFFFFD) D D D
 
 	
 ==M)$<$<=====D) D D D
 
 	
 Mz*0022 0
 0
	T)+? ? ?E E Em E E.BE E   (((""3''' y))	
!(% *- 	6 	6A A$55     	'))44Ii%O	6z)466By!!!Q&!!!2# 3'@@K@@@  )AAKAAA  '@!5"7)E%=0S#
 
 
c( >=((rB   z(?:^|\W)grads\[(\d+)\]	list[int]c                J    d t          j        t          |           D             S )zDetermine a list of gradient indices (the i in grads[i]) that
    are used by the formula.

    >>> used_gradient_indices("foo(grads[0], grads[1])")
    [0, 1]
    c                ,    g | ]}t          |          S r9   )r   )r>   r   s     r@   rb   z)used_gradient_indices.<locals>.<listcomp>  s    BBBqCFFBBBrB   )r   r   GRAD_INDEX_REGEX)r   s    r@   r   r     s&     CBBJ'7AABBBBrB   nctypeslist[NamedCType]&tuple[str, tuple[SavedAttribute, ...]]c                4  	
 dRfd}ddd dfd	d
d d dfddd d dfddd dfddd d d dfdd d dfddd  dfd!d"d# dfd$d%d& dfd'd(d) dfd*d+d, dfd-d.d/ dfd0d1d2 dfd3d4d5 dfd6d7d8 |dfd9d:d; dfd<d=d> dfg}g 
d?| v sd@| v rt          dAdB|  z             t          j        dC|           st          j        dD|           rt          dEdF|  z             dG| v sdH| v rt          dIdJ|  z             |D ]}t          |j        t
                    r|j        j        n|j        	|D ]6\  }dS	
fdM}t          j        |                    	          ||           } 7|j        t          t          t                              k    r t          j        dN	 dN	 dO	 dP|           } t          j        t          j        	          |           r$
                    t          |	Q                     | t          
          fS )TNr<   rU   r5   c                8    | fk    s
J d            d|  d|  dS )NzReplacement for ".strides()" is currently only supported for single derivatives of the same tensor that ".strides()" is being called on.zstrides_or_error(z, "z")r9   )r<   r   s    r@   stride_exprz$saved_variables.<locals>.stride_expr  s>    TG###4 $## 5444D4444rB   z{}.sym_sizes\(\)
_sym_sizesc                F    t          | t          t                              S r
  r   r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>      z$	/8R8R'S'S rB   )suffixr   z{}->sym_sizes\(\)_sym_sizes_optc                `    t          | t          t          t                                        S r
  r   r   r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>	  %    z-	/(B(BCC( ( rB   c                    |  d|  dS )Nz2.has_value() ? std::optional<c10::SymIntArrayRef>(z->sym_sizes()) : std::nulloptr9   r   s    r@   r`   z!saved_variables.<locals>.<lambda>  s'      &D  &D`d  &D  &D  &D rB   )rZ  r   exprz{}.sym_blocksize\(\)_self_sym_blocksize_optc                `    t          | t          t          t                                        S r
  r]  r   s    r@   r`   z!saved_variables.<locals>.<lambda>  r^  rB   c                    d|  dS )Nz#at::sparse_csr::getSymIntBlockSize(r   r9   r   s    r@   r`   z!saved_variables.<locals>.<lambda>  s    %R4%R%R%R rB   z{}.options\(\)_optionsc                F    t          | t          t                              S r
  )r   r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>  s    z$	.8Q8Q'R'R rB   zzeros_like\({}\)_infoc                F    t          | t          t                              S r
  )r   r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>'  s    z$	,8O8O'P'P rB   c                    | S r
  r9   r   s    r@   r`   z!saved_variables.<locals>.<lambda>(  s    T rB   c                    | dz   S )Nz_info.zeros()r9   r   s    r@   r`   z!saved_variables.<locals>.<lambda>)  s    D?$: rB   )rZ  r   r`  r   z{}.sym_size\((-?\w+)\)c                d    d|                                  d                             dd           S )N_sym_argsize_r   -minus_)groupsreplace)r   s    r@   r`   z!saved_variables.<locals>.<lambda>0  s+    $ZAHHJJqM4I4I#x4X4X$Z$Z rB   c                F    t          | t          t                              S r
  r   r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>1      z$	'8J8J'K'K rB   z{}.numel\(\)_numelc                F    t          | t          t                              S r
  r   r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>9      z$	%8H8H'I'I rB   z{}.sym_numel\(\)
_sym_numelc                F    t          | t          t                              S r
  rq  r   s    r@   r`   z!saved_variables.<locals>.<lambda>A  rr  rB   zto_args_sizes\({}\)_args_sizesc           
     z    t          | t          t          t          t                                                  S r
  )r   r    r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>I  s-    z+k)E2B2B&C&CDD( ( rB   zto_args_sizes_symint\({}\)_args_sizes_symintc           
     z    t          | t          t          t          t                                                  S r
  )r   r    r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>S  s-    z+k)G2D2D&E&EFF( ( rB   zto_args_scalartypes\({}\)_args_scalartypesc                `    t          | t          t          t                                        S r
  )r   r    r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>]  s%    z+i&<&<==( ( rB   zTensorGeometry\({}\)	_geometryc                F    t          | t          t                              S r
  )r   r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>g  rY  rB   z{}.scalar_type\(\)_scalar_typec                F    t          | t          t                              S r
  )r   r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>n  s    z$	+8N8N'O'O rB   z
{}.dim\(\)_dimc                F    t          | t          t                              S r
  ru  r   s    r@   r`   z!saved_variables.<locals>.<lambda>v  rv  rB   z{}.sym_strides\(\)_sym_stridesc                F    t          | t          t                              S r
  rX  r   s    r@   r`   z!saved_variables.<locals>.<lambda>~  rY  rB   z{}.layout\(\)_layoutc                F    t          | t          t                              S r
  )r   r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>  rr  rB   z{}.is_conj\(\)
_conjugatec                F    t          | t          t                              S r
  )r   r   r   r   s    r@   r`   z!saved_variables.<locals>.<lambda>  rv  rB   z.sizes()z	->sizes()zY.sizes() is not supported in derivative formulas. Instead, please use the SymInt version,z<.sym_sizes(), which returned a c10::SymIntArrayRef. formula=z\.size\([-]?\d+\)z->size\([-]?\d+\)z[.size(int) is not supported in derivative formulas. Instead, please use the SymInt version,z>.sym_size(int), which returned a c10::SymIntArrayRef. formula=z
.strides()z->strides()z[.strides() is not supported in derivative formulas. Instead, please use the SymInt version,z>.sym_strides(), which returned a c10::SymIntArrayRef. formula=r   re.Match[str]c                X   t          d                   r d         |           nd         }dv r d                   n|                     d          }                    t           d         |z             |                     dv r d                   }|S |z   S )NrZ  r`  r   r   r   r`  r   )callabler   rk   r   )r   rZ  r`  replacementrP   r<   saveds       r@   r   zsaved_variables.<locals>.repl  s    )1$x.)A)AUNDN1%%%tH~  39D..LDL...aggajj"-tH~dVm<<!     D=='2tE{4'8'8K&&f}$rB   z\bz/.has_value() ? std::optional<c10::string_view>(z.value()) : std::nulloptr  r<   rU   r5   rU   )r   r  r5   rU   )r   r   r   r]   r<   r   r   r   r   r   r   r   r-   rk   r   r   )r   rP  r   rU  REPLACEMENTSr   regexr   rP   r<   r  s     `     @@@r@   r   r     sy   
5 5 5 5 5 5  &SS 	
 !*  D  D 		
 $3  SR 		
 $RR 	
  !PP))::	 	
 &ZZKK 	
 "II 	
  &KK 	
 #'  	
 *.  	
 )-  	
 $%SS 	
 "(OO 	
  II 	
 "(SS# 	
 #KK 	
 &II 	
aW6Lt #%EWw 6 6gVWVVW
 
 	
 
y%w// 
29g4 4 
 iXwXXY
 
 	
 w-7":":iXwXXY
 
 	
  + + *6; G GXFKV[ 	
 ( 	@ 	@KE4% % % % % % % %  fU\\$//w??GG
 ;-	'(:(:;;;;fdfffff G 9['--w77 	LL!     E%LL  rB   r<   c                    d                     d |                     d          D                       }|dz                       dd          S )aW  Takes a native function name converts to a op prefix name.

    Note that the "name" parameter must be the native function name
    without the optional variant suffix, so "add" instead of
    "add.out".

    OP names correspond to classes, hence the change to title case.

    Example::
    >>> _create_op_prefix('add')
    'AddBackward'
     c                6    g | ]}|                                 S r9   )title)r>   ps     r@   rb   z%_create_op_prefix.<locals>.<listcomp>  s     ===!''))===rB   r  BackwardForwardBackward)r   r  ro  )r<   
camel_cases     r@   rE  rE    sJ     ==TZZ__===>>J#,,->
KKKrB   varsSequence[SavedAttribute]c                   t                      }g }| D ]m}t          |j        j        t                    r|j        j        j        n|j        j        }||v rC|                    |           |                    |           n|S r
  )rl   r]   r   r<   r   r   rk   )r  seenr  varr<   s        r@   rF  rF    s    UUD"$E 	 	 #*/>::!CJO   	
 4<<SLrB   )r1   r2   r3   r4   r5   r6   )rT   rU   rV   rU   rW   rU   r5   rX   )rp   r&   r5   r}   )
rp   r&   r   rU   r   r   r   r   r5   r   )rp   r&   r   rU   r   r   r5   r   )rp   r&   r   rU   r   r   r   r   r   r   r   r}   r5   r   )r   r   r   r   r5   r   )ry   r   rt   r   ru   r   rw   r   rx   r   r5   r   )r   rU   r5   rL  )r   rU   rP  rQ  r   r   r5   rR  r  )r  r  r5   r  )M
__future__r   r   collectionsr   typingr   r   r   r   r	   r
   rg   torchgen.apir   torchgen.api.autogradr   r   r   r   torchgen.api.typesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    torchgen.contextr!   torchgen.genr"   r#   torchgen.modelr$   r%   r&   r'   r(   r)   r*   r+   torchgen.utilsr,   r-   r.   torchgen.yaml_utilsr/   rU   rX   r0   __annotations__rl   rD  rS   r|   r   r   r   r   r   rn   rO  r   r   rE  rF  r9   rB   r@   <module>r     s   # " " " " " " 				 # # # # # # ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;                                                       & 2 1 1 1 1 1 P P P P P P P P	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 E D D D D D D D D D * * * * * * d>45J0J+KKLcRUhVWFH  H H H Hs=))    DJ. J. J. J.\ * * * *, , , ,^" " " "J` ` ` `F; ; ; ;d) d) d) d)N	 - C C C Cf! f! f! f!RL L L L"     rB   