
    BVh$                        d Z ddlmZmZmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ 	 	 	 ddedef   dedef   deee      deej0                     dedefdZ	 	 	 ddededeej4                  ef   f   deeej4                        deej0                     dedeej4                     fdZy)z<Library for constructing a training loop, suitable for TPUs.    )AnyCallableIterableListOptionalUnion)xla)ops)	array_ops)control_flow_ops)
while_loop)tensor_tracer)tpu_feed)tpu_function)coreN	condition.bodyinputsinfeed_queuenamereturnc           
      P    ~|g n"|D cg c]  }t        j                  |       c}}|D cg c]  }|j                   c}t        |      t	        j
                        }|g,t        d d|D cg c]  }|j                   c} d|       t        d d|D cg c]  }|j                   c} dj                   d|       t	        j
                   d      }|[,t        d d|D cg c]  }|j                   c} d|       t        d d|D cg c]  }|j                   c} d| d	       fd
}	fd}
dk(  rt        j                  d      g}t        j                  |	|
|dd      S c c}w c c}w c c}w c c}w c c}w c c}w )a  Builds a training loop for TPUs.

  The set of loop-carried tensors corresponds to `inputs`.  Both
  `condition` and `body` take the current value of the loop-carried
  tensors. 'body' additionally takes a tuple of infeed from
  infeed_queue if infeed_queue is not None. `condition` must return a
  single boolean value that determines whether iteration
  continues. `body` must return an updated list of values for the
  loop-carried tensors.

  Args:
    condition: a Python function that builds the loop condition.
    body: a Python function that builds the loop body.
    inputs: a list of initial values passed into the training loop, or None
      (equivalent to an empty list).
    infeed_queue: if not None, the infeed queue from which to append a tuple of
      arguments as inputs to condition.
    name: (Deprecated) Does nothing.

  Returns:
    The final values of the loop-carried tensors.

  Raises:
    TypeError: if body or condition has the wrong signature.
  NzVSupplied loop body function cannot be called with the specified inputs. You specified z	 inputs: z, but the loop body needs z and z: additional inputs from infeed, but the computation needs z[Supplied loop condition function cannot be called with the specified inputs. You specified z, but the loop condition needs z7. Note that infeed is not passed to the loop condition.c                      dk(  rg }  |  S )Nr    )r   r   input_aritys    S/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tpu/training_loop.pycondition_wrapperz%while_loop.<locals>.condition_wrapper`   s    aff    c                  t   t        |       } dk(  rg } rZt        j                         j                  }|t	        d      j                  |       j                         D cg c]  }| }}ng } | |z    }t        |t         t        f      s|f}|D cg c]3  }t        |t        j                        r|nt        j                  |      5 }}|D cg c]  }t        |t        j                        s|! }}|D cg c]  }t        |t        j                        s|  }}|||z   k7  rt	        d      |D cg c]  }|j                   }	}|	k7  rt        dj                  |	            ||z  }|st        j                   d      }|rt#        j                  ||      }t$        j&                  j)                         r\t        j                         j                  }
|
d}
t%        j&                         }|j+                  t        j,                         |d|
      }|S c c}w c c}w c c}w c c}w c c}w )zBWrapper around `body` that handles infeed queues and control deps.r   NzCan't build training loop with infeed when there is no tpu_shard_context. Are you building a loop or graph directly rather than from inside tpu.rewrite, tpu.batch_parallel, tpu.shard, or tpu.replicate?zbTPU training loop body must return zero or more Tensor values followed by zero or more Operations.zNMismatch between input types and output types for training loop body: {} vs {})control_inputs   )listr   get_tpu_contextnumber_of_shards
ValueErrorset_number_of_shardsgenerate_dequeue_op
isinstancetupler
   	Operationconvert_to_tensordtype	TypeErrorformatr   constantr   r   TensorTracer
is_enabled	trace_tpuget_default_graph)r   r$   ddequeue_opsoutputsooutput_operationsoutput_tensorsopoutput_typesnum_replicasttr   r   r   input_typess               r   body_wrapperz while_loop.<locals>.body_wrapperf   sU   &\F af %557HH		! L M 	M ''(89 , @ @ BC1QCkCkVk)+G ge}-
g  3==)s/D/DQ/GGG  %,Lqz!S]]/KLL!( ;A'3==9  ;N ; .#44412 2 (66BHH6L6l"!6+|<> > $  ))!,n (--n=NPn !!,,.!113DDl		%%'b||C$9$9$;$2D$02n k D M; 7s$   $	H!8H&H+5H+?#H0;H5r    r!   )r   parallel_iterations)r
   r+   r,   lenr	   check_function_argument_countr-   r   number_of_tuple_elementsr   r/   while_loop_tfr   )r   r   r   r   r   xbody_arg_erroricondition_arg_errorr   r?   r   r>   s   `` `       @@r   r   r      s   < 2+1&3&' '*&;&;A&> &3&"()Q)+F+44
K'.##.-y&9QQ!&&9Q8RRlm{l|~ 
 ##.-y&9QQ!&&9Q8RRWXdX}X}W~ //=.>@A A 99d$$--8MTZC[qAFFC[B\ ]0134 4
 --8MTZC[qAFFC[B\ ]011hj FT A  #$F		!	!vBA
O Og&3) :R
 :R D\
 D\s"   F
F;F'F;F'F#nc                      d  fd}fd}|dgndg |      z   }t        |||||      } |      }t        |      dk(  r|d   j                  S |dd S )a  Builds a training loop that executes a fixed number of iterations.

  The set of loop-carried tensors correspond to `inputs`.
  `body` must be a function that takes and returns the values of the
  loop-carried tensors.

  Args:
    n: the number of loop iterations
    body: a Python function that builds the loop body.
    inputs: a list of initial values passed into the training loop or None
      (equivalent to an empty list).
    infeed_queue: if not None, the infeed queue from which to append a tuple of
      arguments as inputs to condition.
    name: (Deprecated) Does nothing.

  Returns:
    The final values of the loop-carried tensors.
  Raises:
    ValueError: if there is a type error.
  c                 J    t        | t        t        f      s| gS t        |       S N)r(   r"   r)   )xss    r   _convert_to_listz repeat.<locals>._convert_to_list   s     b4-(Tk"Xor   c                     ~| k  S rM   r   )rH   argsrJ   s     r   condzrepeat.<locals>.cond   s    q5Lr   c                 (    | dz   g  |       z   S )Nr!   r   )rH   rQ   rO   r   s     r   r?   zrepeat.<locals>.body_wrapper   s    E7%dDk222r   Nr   )r   r   r   r!   )r   rB   r:   )	rJ   r   r   r   r   rR   r?   r6   rO   s	   ``      @r   repeatrT      st    43 .A3qc,<V,D&D&
LlO'W%'\Q1:==12;r   )NNN)__doc__typingr   r   r   r   r   r   tensorflow.python.compiler.xlar	   tensorflow.python.frameworkr
   tensorflow.python.opsr   r   r   rE   tensorflow.python.tpur   r   r   tensorflow.python.typesr   
core_typesInfeedQueueint
TensorLikerT   r   r   r   <module>r`      s.    C A A . + + 2 = / * . 6
 .2>B	TO(38, TOc3h'TOS	*TO &h&:&:;TO 	TO %(	TOt 5937/
/
3j33X=>>
?/ T*//01/ 8//0	/
 /
 j334/r   