o
    h/                     @  s  d dl mZ d dlZd dlmZ d dlmZmZ d dlm	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 d d	lmZ erRd d
lmZ eddG dd dZeddG dd dZeddG dd dZ eddG dd dZ!dMddZ"dNddZ#dNdd Z$eddG d!d" d"Z%eddG d#d$ d$Z&eddG d%d& d&Z'dOd(d)Z(dPd,d-Z)d.hZ*h d/Z+h d0Z,dQd3d4Z-	5dRdSd<d=Z.dTdBdCZ/dUdGdHZ0	5dRdVdKdLZ1dS )W    )annotationsN)	dataclass)castTYPE_CHECKING)local)cpp)	BaseCTypeBinding
NamedCTypetensorListT)BaseTyBaseTypeFunctionSchemaListTypeNativeFunctionNativeFunctionsViewGroup
SchemaKindType)IDENT_REGEX)SequenceT)frozenc                   @  s   e Zd ZU ded< ded< dS )SavedAttributer
   nctypestrexprN__name__
__module____qualname____annotations__ r    r    i/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torchgen/api/autograd.pyr      s   
 r   c                   @  s>   e Zd ZU ded< ded< ded< ded< ded< d	ed
< dS )
Derivativer   formulaoriginal_formulatuple[str, ...]	var_namesztuple[SavedAttribute, ...]saved_inputssaved_outputsset[str]named_gradientsNr   r    r    r    r!   r"   +   s   
 r"   c                   @  sF   e Zd ZU ded< ded< ded< ded< ded	< d
ed< d
ed< dS )ForwardDerivativer   r#   r%   r&   ztuple[Type, ...]	var_typesztuple[str, ...] | Nonerequired_inputs_fw_gradrequired_inputs_primalboolrequired_original_self_valueis_reusing_outplace_formulaNr   r    r    r    r!   r+   G   s   
 r+   c                   @  s   e Zd ZU ded< ded< ded< ded< d	ed
< ded< ded< ded< ded< ded< ded< ded< ded< ed#ddZd$d d!Zd"S )%DifferentiabilityInfor   namer   funcz
str | NoneopzSequence[Derivative]derivativeszSequence[ForwardDerivative]forward_derivativeszSequence[SavedAttribute]all_saved_inputsall_saved_outputszSequence[str]available_named_gradientsr)   used_named_gradientszSequence[Binding]args_with_derivativesnon_differentiable_arg_nameszlist[bool] | Noneoutput_differentiabilityzlist[str] | None#output_differentiability_conditionsreturnr/   c                 C  s   t | jdkS )Nr   )lenr<   selfr    r    r!   has_derivatives   s   z%DifferentiabilityInfo.has_derivativesgr   DifferentiabilityInfo | Nonec                 C  s   |j d u rd S |j }| jjddd}|d  dd|dd   }| jd u r)d n| j d}t|||| j| j| j| j	| j
| j| j| j| j| jdS )	N.   )maxsplitr   z_copy.   _copyr3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   )	view_copyr3   splitjoinr5   r2   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   )rC   rE   fname_split_by_periodview_copy_nameview_copy_op_namer    r    r!   %create_view_copy_from_view_derivative   s.   

z;DifferentiabilityInfo.create_view_copy_from_view_derivativeN)r@   r/   )rE   r   r@   rF   )r   r   r   r   propertyrD   rT   r    r    r    r!   r2   f   s"   
 r2   inforF   identr   r@   r/   c                 C  s:   | d u rdS | j D ]}|j}tt||r dS q	dS )NFT)r6   r#   researchr   format)rV   rW   
derivativer#   r    r    r!   
uses_ident   s   
r\   c                 C  
   t | dS )Nretain_variablesr\   rV   r    r    r!   uses_retain_variables      
ra   c                 C  r]   )Ngradr_   r`   r    r    r!   uses_single_grad   rb   rd   c                   @  &   e Zd ZU ded< ded< ded< dS )DifferentiableInputr   r3   r   typecpp_typeNr   r    r    r    r!   rf         
 rf   c                   @  re   )DifferentiableOutputr   r3   r   rg   rh   Nr   r    r    r    r!   rj      ri   rj   c                   @  s&   e Zd ZU ded< ded< ded< dS )'NativeFunctionWithDifferentiabilityInfor   r4   z'dict[str, DifferentiabilityInfo] | NonerV   z-dict[str, Sequence[ForwardDerivative]] | Nonefw_derivativesNr   r    r    r    r!   rk     s   
 rk   fnc                 C  s2   | j js| jdurtdd | j D rdS dS )a  How are we going to call the underlying implementation of a
    declaration?  There are two strategies:
        - use_derived: we want to call the implementation on CPUDoubleType
          (or a similar, derived Type instance).  Because these derived
          instances deal in Tensors, not Variables (it's a completely different
          object, so it doesn't dispatch back to VariableType), code on
          this dispatch path needs to wrap/unwrap tensors.  If the
          derived implementation takes and returns tensors, the
          implementation is usually differentiable (although we also use
          the derived dispatch path for non-differentiable functions
          that we still want to dispatch on the derived Type instance;
          e.g., size())
        - use_type: we want to call the implementation on Type, because
          it is implemented concretely, and the functions it invokes will
          get dispatched back to VariableType (which will ensure that they
          are differentiable.)
    Nc                 s  s    | ]}|j V  qd S N)rD   .0rV   r    r    r!   	<genexpr>"  s    z$dispatch_strategy.<locals>.<genexpr>use_deriveduse_type)r4   is_abstractrV   anyvalues)rm   r    r    r!   dispatch_strategy  s   "rw   rP   r   c                 C  s   | j jjjdS )N	_foreach_)r4   r3   base
startswith)rP   r    r    r!   is_foreach_func<  s   r{   _foreach_zero_>   _foreach_add.Tensor_foreach_div.Tensor_foreach_mul.Tensor>   _foreach_add.Scalar_foreach_sub.Scalar_foreach_add_.Scalar_foreach_sub_.Scalar_foreach_add.ScalarList_foreach_sub.ScalarList_foreach_add_.ScalarList_foreach_sub_.ScalarListfunction_schemar   c                 C  s   | j jjjdd |jjjkoC|jjj pt| j jtv oCt| j jtv p2t| j j	j
t|j	j
koCtdd t| j j	j
|j	j
D S )Nrx   c                 s  s.    | ]\}}|j |j t|j d dfv V  qdS )elemN)rg   getattr)rp   argref_argr    r    r!   rq   g  s
    
z+is_reference_for_foreach.<locals>.<genexpr>)r4   r3   ry   rN   inplacer   _foreach_with_inplace_ref_skip_argument_len_checkrA   	argumentsflat_non_outallzip)rP   r   r    r    r!   is_reference_for_foreachX  s     

r   Defaultforeach_functionfunctional_info_by_signature6dict[FunctionSchema, dict[str, DifferentiabilityInfo]] non_functional_info_by_signaturedispatch_key)tuple[DifferentiabilityInfo | None, bool]c           &        s~  d}|  D ]\}}t| |sq|| }|dur nq|du rH| j tjkrHt| jjtv rH|  D ]\}}t| |s=q3|| }|durG nq3|du rNdS | j tjkrZ|dfS i i }t	t
| jjj|jjD ]\}\}	}
|	j|
j< |	||	j< qkg g g }} g }t	|jD ]\}}|jdddd}g g }}tj| j| jdv |jD ]J}|jd	d
 }| }t|| jtr|d }n|}|j||}|tt|jj|}tj|| |d}t|j|j }| t!||d q|j"D ]}|jjdkr| t!tdt#t$ddd qt%dW d   n	1 s#w   Y  fdd|j&D } '| |'| |'| t(||jt)|t)|t)|t* d}| | qtj| j| jd  fdd| jjjD }W d   n	1 szw   Y  g }|j+D ]}t,|j&}t,|j-}g } g }!|j.durt,|j.} |j/rt,|j/}!|j}d|v r|jdd}t
| jjj|jjjjD ]\}"}
t|"jtr|"j0 s||
j|"jd }nV|"j0 r't|"jts|"jt1t2j3krt| jjt4v sJ | jj d|"j dD ]}#|
j|# }$|$|v r$|"j|# }||$|}qn|"j|
jkr6||
j|"j}t	|D ]\}}%|%|
jkrN|"j||< |"j||< q:t	| D ]\}}%|%|
jkrc|"j| |< qTt	|!D ]\}}%|%|
jkrx|"j|!|< qiq| t5|t)|t)|t)| t)|!|j6|j7d qt8| jjjj9| d|j: | jjj; ||t)t*|t)t*|dt* |g ddddfS )zGenerate DifferentiabilityInfo for out-place foreach function, return the existing one for in-place.

    The second return value indicates whether the info is generated in this function.
    NNFFrc   zgrads[i]resultz	result[i])!use_const_ref_for_mutable_tensorsuse_ilistref_for_tensor_listsrG   r   z[i]binds)r   r   )r3   rg    c                   s   g | ]} | qS r    r    )rp   var)map_refarg2foreachargr    r!   
<listcomp>  s    z.gen_foreach_derivativeinfo.<locals>.<listcomp>)r#   r$   r&   r'   r(   r*   c                   s4   g | ]}|j  v rt|j tj||j d |ddqS )r   N)r3   r   argumentdefault)r3   r	   r   argument_type)rp   r   )all_var_namesr    r!   r     s    
z, )_p_tr#   r&   r,   r-   r.   r0   r1   Foreachr    rL   T)<itemsr   r4   kindr   r   r   r3   r   	enumerater   r   r   r6   r#   replacer   parametrizer   part_of_structured_groupr'   r   rN   
isinstancerg   r   r   r   r   r   r
   remove_const_refappendr   r(   r   r   RuntimeErrorr&   extendr"   tuplesetr7   listr,   r-   r.   is_tensor_liker   r   Tensor_foreach_with_tensor_overloadr+   r0   r1   r2   ry   r5   overload_name)&r   r   r   r   ref_diff_infor   	diff_infomap_name2argir   r   r8   r9   modified_derivative_formulasr[   modified_formular'   r(   	ref_inputref_input_jit_namemapped_namemapped_exprnew_exprr   canonical_nctype
ref_outputr&   modified_derivativer<   r7   fw_derivativer,   r-   r.   foreach_argsuffix	curr_exprr3   r    )r   r   r!   gen_foreach_derivativeinfor  s`  







	#




















r   native_functionslist[NativeFunction]differentiability_infos-list[NativeFunctionWithDifferentiabilityInfo]c                   s  dd    D dd    D d/ fdd	}g }| D ]F}||\}}|j tjkrY|d
urY| D ] }|jD ]}d|jv rW|jD ]}	d|	j	vsVJ d|j qGq=q8|sf|
t|d
d
d qi }
|  D ]\}}|jsxg |
|< ql|j}|j tjkrWt|jdksJ |jd }|j}d0dd}ttd|r|rtd|jj d||d}||d}d1dd}ttd||}|j}ttd|r|r|d  nd }|s1d!}d!}d
}d
}td"|}|r|d|d#}}d2d&d'}||}|o||jk}|r)|d
usJ |d
usJ d(| d)| d*| }nd+| d*| }tttd,|pGtttd-|}t|d |j|j||| d.g}||
|< ql|
t|||
d q|S )3zSets the "derivative" key on declarations to matching autograd function
    In-place functions will use the out-of-place derivative definition if there
    is no in-place specific derivative.
    c                 S  s,   i | ]\}}|  tjkr|jd d|qS Tstrip_defaultr   r   
functional	signaturerp   schema	info_dictr    r    r!   
<dictcomp>S  
    z0match_differentiability_info.<locals>.<dictcomp>c                 S  s,   i | ]\}}|  tjkr|jd d|qS r   r   r   r    r    r!   r   X  r   rP   r   r@   4tuple[dict[str, DifferentiabilityInfo] | None, bool]c                   s  d| j v r| j tjkrdS | j v r | j dfS | jjdd}|v r0t| s0| dfS d| j v rW|v rW| }tdd | D rSJ dt	| j d	|dfS t| r| j vsbJ t
| \}}|d u rpdS d
|i}|r| | j< || j< ||fS dS )N	generatedr   Tr   Fc                 s  s$    | ]}t d d |jD V  qdS )c                 s  s     | ]}d t |jjv V  qdS )rC   N)r   r   r3   )rp   inptr    r    r!   rq   {  s    zLmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>.<genexpr>N)ru   r8   ro   r    r    r!   rq   z  s
    
zBmatch_differentiability_info.<locals>.find_info.<locals>.<genexpr>zzAttempted to convert a derivative formula for a mutable operator
 to be used by automatically by its functional variant ("zV").
 this is not currently supported (we'd need to fix up the formula in the codegen).r   )tagsr4   r   r   outr   r{   ru   rv   r   r   )rP   f_sigr   r   is_generateddiff_info_dictr   r   r   r    r!   	find_info^  s@   



z/match_differentiability_info.<locals>.find_infoNrC   strides_or_errorz`Calling '.strides()' in the 'self' derivative formula of an in-place function is not supported: )r4   rV   rl   rJ   r   r#   r   postfixc                   s(   d fdd}t td  || S )	Nmre.Match[str]r@   r   c                   s   |  d d  |  d S )NrJ   original_selfrH   groupr   r   r    r!   repl  s   zSmatch_differentiability_info.<locals>.replace_self_with_original_self.<locals>.replrC   r   r   r@   r   )rX   subr   rZ   )r#   r   r   r    r   r!   replace_self_with_original_self  s   zEmatch_differentiability_info.<locals>.replace_self_with_original_selfself_pzThe formula for "z" is using the original value of self that is being modified inplace. This would lead to wrong forward gradients. Please use "result" in the formula only.r   r   r   r   c                 S  s   |  d d|  d S )NrJ   r   rH   r   r   r    r    r!   r     s   z*match_differentiability_info.<locals>.replr   rB   Fzself_t.([\w]*)\((.*)\)rH   sr/   c                 S  s@   d}| D ]}|dkr|d8 }|dkr dS |dkr|d7 }qdS )NrJ   )r   F(Tr    )r   levelchr    r    r!   check_parens_nest_level_gt_zero	  s   zEmatch_differentiability_info.<locals>.check_parens_nest_level_gt_zeroz"self_t_raw.defined() ? self_t_raw.z_(z) : z(self_t_raw.defined() ? self_t_raw.copy_(original_self_poriginal_self_tr   )rP   r   r@   r   )r#   r   r   r   r@   r   r   )r   r   r@   r/   )r   r4   r   r   r   rv   r6   r&   r'   r   r   rk   r7   rA   r#   rX   rY   r   rZ   r   r3   r   r.   	fullmatchr   r/   r+   r,   r-   )r   r   r   r   rP   r   is_exact_matchrV   r[   saved_inputfw_derivative_dictkeyr7   fw_infor#   r   r   required_primalsis_single_method_on_self_tdirectly_do_inplaceop_namebetween_parensmatchr   r0   r    r   r!   match_differentiability_infoJ  s   	8






	



r  r3   rg   r   c                 C  s   |  o|d u p| |jvS rn   )r   r=   )r3   rg   rV   r    r    r!   is_differentiable?  s   r  r  list[DifferentiableOutput]c           	        s   | j }| jr| j| nd  dd tt||j jD } r! jnd }|d urct|t|kr>tdt| dt| dg }d|v rP|j 	 t
jkrPtdt||D ]\}}|r`|| qU|S tt fdd	|}t rx|d d
 S |S )Nc              	   S  s.   g | ]\}}t ||jtj|d d dqS )T)symint)r3   rg   rh   )rj   rg   r   return_typerh   )rp   r3   retr    r    r!   r   L  s    z.gen_differentiable_outputs.<locals>.<listcomp>z(The length of output_differentiability (z)), does not match the number of outputs (z).FzXoutput_differentiability=False for inplace operation (version_counter won't get updated)c                   s   t | j| j S rn   )r  r3   rg   )rr`   r    r!   <lambda>e  s    z,gen_differentiable_outputs.<locals>.<lambda>rJ   )r4   rV   r   r   return_namesreturnsr>   rA   r   r   r   r   r   r   filterrd   )	rm   r  rP   outputsr>   differentiable_outputsdifferentiableoutput candidate_differentiable_outputsr    r`   r!   gen_differentiable_outputsG  s:   
r  )rV   rF   rW   r   r@   r/   )rV   rF   r@   r/   )rm   rk   r@   r   )rP   r   r@   r/   )rP   r   r   r   r@   r/   )r   )
r   r   r   r   r   r   r   r   r@   r   )r   r   r   r   r@   r   )r3   r   rg   r   rV   rF   r@   r/   )rm   rk   r  r   r@   r  )2
__future__r   rX   dataclassesr   typingr   r   torchgenr   torchgen.apir   torchgen.api.typesr   r	   r
   r   torchgen.modelr   r   r   r   r   r   r   r   torchgen.utilsr   collections.abcr   r   r"   r+   r2   r\   ra   rd   rf   rj   rk   rw   r{   r   r   r   r   r   r  r  r  r    r    r    r!   <module>   sT    (

k


	

1
" 
Y 
v	