o
    ?e                     @   sP   d Z ddlmZ ddlmZ G dd deZG dd deZG dd	 d	eZ	d
S )z@The step function abstraction represents a single training step.    )backprop)	optimizerc                   @   s4   e Zd ZdZdd Zedd Zdd Zdd	 Zd
S )Stepz;Interface for performing each step of a training algorithm.c                 C   s
   || _ d S N_distribution)selfdistribution r
   e/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/distribute/step_fn.py__init__   s   
zStep.__init__c                 C   s   | j S r   r   r   r
   r
   r   r	      s   zStep.distributionc                 C   s   g S r   r
   r   r
   r
   r   
initialize   s   zStep.initializec                 C   s   t d)z,Perform one step of this training algorithm.z"must be implemented in descendants)NotImplementedErrorr   r
   r
   r   __call__"   s   zStep.__call__N)	__name__
__module____qualname____doc__r   propertyr	   r   r   r
   r
   r
   r   r      s    
r   c                       s(   e Zd ZdZ fddZdd Z  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                    s(   t t| | | fdd| _d S )Nc                    s     S r   r
   )_
dataset_fnr
   r   <lambda>3   s    z,StandardInputStep.__init__.<locals>.<lambda>)superr   r   Zmake_input_fn_iterator	_iterator)r   r   r	   	__class__r   r   r   1   s   zStandardInputStep.__init__c                 C   s   | j jS r   )r   Zinitializerr   r
   r
   r   r   5   s   zStandardInputStep.initialize)r   r   r   r   r   r   __classcell__r
   r
   r   r   r   )   s    r   c                       s,   e Zd ZdZ	d fdd	Zdd Z  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                    s(   t t| || || _|| _|| _d S r   )r   r    r   _loss_fn
_optimizer_iterations_per_step)r   r   Zloss_fnr   r	   Ziterations_per_stepr   r
   r   r   T   s   
zStandardSingleLossStep.__init__c                    sV    j    fdd} jj| j j}|jW  d    S 1 s$w   Y  d S )Nc                    s<   t  j}t|} jjj|| |fd} j	 j|S )z-Function to run one iteration with one input.)args)
r   Zimplicit_gradr"   optimizer_libZget_filtered_grad_fnr	   extendedZcall_for_each_replicar#   Z_distributed_apply)ctxZinputsZgradients_fnZgrads_and_varsr   r
   r   step_fn]   s   
z0StandardSingleLossStep.__call__.<locals>.step_fn)r   scoper	   r'   Z"experimental_run_steps_on_iteratorr   r$   Zrun_op)r   r)   r(   r
   r   r   r   [   s   
$zStandardSingleLossStep.__call__)r!   )r   r   r   r   r   r   r   r
   r
   r   r   r    9   s
    r    N)
r   Ztensorflow.python.eagerr   Ztensorflow.python.trainingr   r&   objectr   r   r    r
   r
   r
   r   <module>   s   