
    AVh                     b    d Z ddlmZ ddlmZ  G d de      Z G d de      Z G d d	e      Z	y
)z@The step function abstraction represents a single training step.    )backprop)	optimizerc                   2    e Zd ZdZd Zed        Zd Zd Zy)Stepz;Interface for performing each step of a training algorithm.c                     || _         y N_distribution)selfdistributions     T/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/distribute/step_fn.py__init__zStep.__init__   s
    %D    c                     | j                   S r   r	   r   s    r   r   zStep.distribution   s    r   c                     g S r    r   s    r   
initializezStep.initialize   s    Ir   c                     t        d      )z,Perform one step of this training algorithm.z"must be implemented in descendants)NotImplementedErrorr   s    r   __call__zStep.__call__"   s    
B
CCr   N)	__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r   r   r      s)    C&  Dr   r   c                   (     e Zd ZdZ fdZd Z xZS )StandardInputStepzStep with a standard implementation of input handling.

  Args:
    dataset_fn: a function that returns a tf.data Dataset that produces the
      input for the model.
  c                 `    t         t        |   |       |j                  fd      | _        y )Nc                             S r   r   )_
dataset_fns    r   <lambda>z,StandardInputStep.__init__.<locals>.<lambda>3   s	    :< r   )superr   r   make_input_fn_iterator	_iterator)r   r"   r   	__class__s    ` r   r   zStandardInputStep.__init__1   s&    	
T+L9!889OPDNr   c                 .    | j                   j                  S r   )r&   initializerr   s    r   r   zStandardInputStep.initialize5   s    >>%%%r   )r   r   r   r   r   r   __classcell__r'   s   @r   r   r   )   s    Q&r   r   c                   ,     e Zd ZdZ	 d fd	Zd Z xZS )StandardSingleLossStepa  A step function that implements a training step for a feed forward network.

  An instance of this class is intended to be used as a callable:

  ```python
  ...
  step = step_fn.StandardSingleLossStep(
      dataset, loss_fn, optimizer, distribution)

  # Run a single training step on a given DistributionStrategy:
  step(distribution)
  ...
  ```

  Args:
    dataset_fn: a function that returns a tf.data Dataset that produces the
      input for the model.
    loss_fn: a function that takes a context and inputs as arguments. It returns
      the loss for those inputs. `context` is an instance of
      `values.MultiStepContext` that will be passed when `loss_fn` is run.
      `context` can be used to specify the outputs to be returned from
      `loss_fn`, among other things.
    optimizer: an optimizer that implements an update rule.
    distribution: a `DistributionStrategy` object.
  c                 X    t         t        |   ||       || _        || _        || _        y r   )r$   r-   r   _loss_fn
_optimizer_iterations_per_step)r   r"   loss_fnr   r   iterations_per_stepr'   s         r   r   zStandardSingleLossStep.__init__T   s+    	
 $0\JDMDO 3Dr   c                       j                   j                         5   fd} j                  j                  j	                  | j
                   j                        }|j                  cd d d        S # 1 sw Y   y xY w)Nc                 
   t        j                  j                        }t        j                  |      }j
                  j                  j                  || |f      }j                  j                  j
                  |      S )z-Function to run one iteration with one input.)args)
r   implicit_gradr/   optimizer_libget_filtered_grad_fnr   extendedcall_for_each_replicar0   _distributed_apply)ctxinputsgradients_fngrads_and_varsr   s       r   step_fnz0StandardSingleLossStep.__call__.<locals>.step_fn]   sv    --dmm<$99,G**33IIV} J . 11~/ 	/r   )r
   scoper   r:   "experimental_run_steps_on_iteratorr&   r1   run_op)r   rA   r=   s   `  r   r   zStandardSingleLossStep.__call__[   se    				!	!	# / &&II
4>>4#<#<>cZZ%  s   AA22A;)   )r   r   r   r   r   r   r*   r+   s   @r   r-   r-   9   s    6 $%4r   r-   N)
r   tensorflow.python.eagerr   tensorflow.python.trainingr   r8   objectr   r   r-   r   r   r   <module>rI      s8    G , AD6 D(& & 5. 5r   