
    sg                         d dl Z d dlmZ d dlZdj	                         Z G d dej                  j                        Zd Z	d Z
 e j                         Zed        Zd	 Zy)
    N)contextmanagera   
This is used when dynamo traces torch.nn.Parameter, which normally would not trace properly
with AOTAutograd.  We instead create a placeholder torch.nn.Parameter before the graph, which
becomes a graph arg and has no storage backing it.  At the point in the graph where the parameter
actually should be created we mutate this sacrificial placeholder into it.  This allows gradients
to flow into the parameter as if it were an input to the graph (which is the only thing we are
allowed to compute gradients on).
c                   ,    e Zd Zed        Zed        Zy)TracableCreateParameterc                 @    |j                   rJ |j                  |      S N)requires_gradset_)ctxtensorplaceholders      T/var/www/html/venv/lib/python3.12/site-packages/torch/_dynamo/create_parameter_op.pyforwardzTracableCreateParameter.forward   s"    ''''''    c                 
    d |fS r    )r
   grads     r   backwardz TracableCreateParameter.backward   s    Tzr   N)__name__
__module____qualname__staticmethodr   r   r   r   r   r   r      s(    ( (  r   r   c                     t        j                  |j                        5  t        j	                  | |      }d d d        |S # 1 sw Y   S xY wr   )torchset_grad_enabledr   r   apply)r   r   outs      r   tracable_create_parameterr      sE    			 9 9	: A%++FK@AJAJs   AAc                     t         j                  j                  t        j                  | ||      |      }|j	                         j                  d       |S )z8Create a placeholder to be passed to the above functions)dtypedevice)r   r   )r   nn	Parameteremptyuntyped_storageresize_)sizer   r    r   results        r   new_parameter_placeholderr(   #   sK    XXDf5]   F
 $$Q'Mr   c               #      K   t        t        dd      } dt        _        	 d | t        _        y # | t        _        w xY ww)Nconvert_tracable_parameterTF)getattr_TLSr*   )old_flags    r   $do_not_convert_to_tracable_parameterr.   1   s7     t94@H&+D#3*2'('s   A 0 A =A c                  $    t        t        dd      S )Nr*   T)r+   r,   r   r   r   !can_convert_to_tracable_parameterr0   ;   s    45t<<r   )	threading
contextlibr   r   stripdocautogradFunctionr   r   r(   localr,   r.   r0   r   r   r   <module>r8      sj     %  
EG enn55  y 3 3=r   