a
    /Sic  ã                   @   sÐ   d Z ddlZddlmZ ddlmZ edeƒ dƒZG dd„ deƒZ	d)dd„Z
dd„ Zdd„ ZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ Zejdd„ ƒZdd„ Zd*dd „Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ ZdS )+zGradient tape utilities.é    N)Ú
pywrap_tfe)Ú
LazyLoaderÚdistribution_strategy_contextz:tensorflow.python.distribute.distribution_strategy_contextc                   @   s&   e Zd ZdZdgZdd„ Zdd„ ZdS )ÚTapez(Represents a gradient propagation trace.Ú_tapec                 C   s
   || _ d S ©N)r   )ÚselfÚtape© r
   úX/var/www/html/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/eager/tape.pyÚ__init__$   s    zTape.__init__c                 C   s   t  | j¡S r   )r   ÚTFE_Py_TapeWatchedVariablesr   ©r   r
   r
   r   Úwatched_variables'   s    zTape.watched_variablesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   r   r
   r
   r
   r   r      s   r   FTc                 C   s   t  | |¡}t|ƒS )z&Pushes a new tape onto the tape stack.)r   ÚTFE_Py_TapeSetNewr   )Ú
persistentZwatch_accessed_variablesr	   r
   r
   r   Úpush_new_tape+   s    r   c                 C   s   t  | j¡ dS )z,Pushes an existing tape onto the tape stack.N)r   ÚTFE_Py_TapeSetAddr   ©r	   r
   r
   r   Ú	push_tape1   s    r   c                 C   s   t  | j|¡ dS )z2Marks this tensor to be watched by the given tape.N)r   ÚTFE_Py_TapeWatchr   )r	   Útensorr
   r
   r   Úwatch6   s    r   c                   @   s6   e Zd ZdZdgZdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚVariableWatchera/  A scope that tracks all trainable variable accesses within it.

  This explicitly ignores variables that are not marked as trainable.

  Sample usage:

  var = tf.Variable(0.0)
  with VariableWatcher() as variable_watcher:
    var.assign_add(1.0)

  assert variable_watcher.watched_variables == [var]
  Ú_variable_watcherc                 C   s
   d | _ d S r   )r   r   r
   r
   r   r   K   s    zVariableWatcher.__init__c                 C   s   t  ¡ | _| S r   )r   ÚTFE_Py_VariableWatcherNewr   r   r
   r
   r   Ú	__enter__N   s    
zVariableWatcher.__enter__c                 C   s   t  | j¡ d S r   )r   ÚTFE_Py_VariableWatcherRemover   )r   ÚtypÚvalueÚ	tracebackr
   r
   r   Ú__exit__R   s    zVariableWatcher.__exit__c                 C   s   t  | j¡S )z7Returns a tuple of variables accessed under this scope.)r   Ú&TFE_Py_VariableWatcherWatchedVariablesr   r   r
   r
   r   r   U   s    ÿz!VariableWatcher.watched_variablesN)	r   r   r   r   r   r   r!   r&   r   r
   r
   r
   r   r   ;   s   r   c                 C   sP   t  ¡ \}}|r |j |¡g}n
| |¡}|D ]}t | j|¡ t |¡ q.dS )z4Marks this variable to be watched by the given tape.N)	r   Ú get_strategy_and_replica_contextÚextendedÚvalue_containerÚexperimental_local_resultsr   ÚTFE_Py_TapeWatchVariabler   Ú&TFE_Py_VariableWatcherVariableAccessed)r	   ÚvariableÚstrategyÚcontextÚ	variablesÚvarr
   r
   r   Úwatch_variable[   s    ÿ
r3   c                 C   sL   t  ¡ \}}|r |j | ¡g}n
| | ¡}|D ]}t |¡ t |¡ q.dS )ztNotifies all tapes in the stack that a variable has been accessed.

  Args:
    variable: variable to be watched.
  N)r   r(   r)   r*   r+   r   ÚTFE_Py_TapeVariableAccessedr-   )r.   r/   r0   r1   r2   r
   r
   r   Úvariable_accessedh   s    ÿ

r5   c                    sj   t  ¡ \‰ }g }|r(‡ fdd„| D ƒ}n | D ]}|jr,| ˆ  |¡¡ q,|D ]}t |¡ t |¡ qLdS )z¼Notifies all tapes in the stack that variables have been accessed.

  Only trainable variables are marked as accessed.

  Args:
    variables: iterable of variables to mark as accessed.
  c                    s   g | ]}|j rˆ j |¡‘qS r
   )Ú	trainabler)   r*   )Ú.0r.   ©r/   r
   r   Ú
<listcomp>…   s   ÿz&variables_accessed.<locals>.<listcomp>N)r   r(   r6   Úextendr+   r   r4   r-   )r1   r0   Úaccessedr.   r2   r
   r8   r   Úvariables_accessedy   s    	ÿ
ÿ
r<   c                 C   s   t  | j¡ dS )z!Pops the given tape in the stack.N)r   ÚTFE_Py_TapeSetRemover   r   r
   r
   r   Úpop_tape‘   s    r>   c                  c   s>   t  ¡ } z"| st  ¡  dV  W | s:t  ¡  n| s8t  ¡  0 dS )z7Stop all gradient recording (backprop and forwardprop).N)r   ÚTFE_Py_TapeSetIsStoppedÚTFE_Py_TapeSetStopOnThreadÚTFE_Py_TapeSetRestartOnThread)Z
is_stoppedr
   r
   r   Ústop_recording–   s    
ÿrB   c                 C   s
   t  | ¡S )a  Returns true if any tape in the stack watches any of these tensors.

  Only takes GradientTapes into account, not forward accumulators.

  Args:
    tensors: Tensors to check, typically inputs to an operation.

  Returns:
    Boolean, whether any tape watches any of `tensors`.
  )r   Ú"TFE_Py_TapeSetShouldRecordBackprop)Útensorsr
   r
   r   Úshould_record_backprop£   s    rE   c                 C   s   t  | ||||¡ dS )z0Records the operation on all tapes in the stack.N)r   ÚTFE_Py_TapeSetRecordOperation)Úop_typeÚoutput_tensorsÚinput_tensorsÚbackward_functionZforward_functionr
   r
   r   Úrecord_operation±   s    þrK   c                 C   s   t  | |||¡ dS )z9Records the operation on all backward tapes in the stack.N)r   Ú%TFE_Py_TapeSetRecordOperationBackprop)rG   rH   rI   rJ   r
   r
   r   Úrecord_operation_backprop_only¹   s    þrM   c                 C   s   t  | ||||¡ dS )a  Records the operation on all forward accumulators in the stack.

  Args:
    op_type: a string for the operation type, used in the backprop code
    output_tensors: a list of Python Tensor objects output by the operation
    input_tensors: a list of input Tensors to the recorded operation
    backward_function: the function to be called to, given the gradients of the
      output tensors, produce the gradients of the input tensors. This function
      is automatically transposed to produce output gradients given input
      gradients.
    forwardprop_output_indices: indicates any output_tensors which contain JVPs.
      Typically these will have come from TFE_Py_PackForwardGradients. May be
      None or an empty sequence if there are no JVP outputs from the operation.
  N)r   Ú(TFE_Py_TapeSetRecordOperationForwardprop)rG   rH   rI   rJ   Zforwardprop_output_indicesr
   r
   r   Ú!record_operation_forwardprop_onlyÁ   s    þrO   c                 C   s   t  | ¡ dS )z;Deletes traces for this Tensor from all tapes in the stack.N)r   ÚTFE_Py_TapeSetDeleteTrace)Ú	tensor_idr
   r
   r   Údelete_trace×   s    rR   c                   C   s
   t  ¡  S )z#Returns True if any tape is active.)r   ÚTFE_Py_TapeSetIsEmptyr
   r
   r
   r   Úcould_possibly_recordÜ   s    rT   )FT)N)r   Ú
contextlibÚtensorflow.pythonr   Ú"tensorflow.python.util.lazy_loaderr   Úglobalsr   Úobjectr   r   r   r   r   r3   r5   r<   r>   ÚcontextmanagerrB   rE   rK   rM   rO   rR   rT   r
   r
   r
   r   Ú<module>   s2   þ
 
 ÿ
