
    BVh)                         d Z ddl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lmZ ddlmZ 	 	 	 	 ddZd Zd Zd Zd Z G d dej.                        Zy)z5The implementation of `tf.data.Dataset.padded_batch`.    N)dataset_ops)nest)	structure)constant_op)dtypes)ops)
smart_cond)tensor_shape)tensor_spec)tensor_util)gen_dataset_opsc                     |Lt        j                  |       }t        t        j                  |            D ]  \  }}|r	t        d| d       t        | |||||      S )z/See `tf.data.Dataset.padded_batch` for details.z<You must provide `padded_shapes` argument because component z has unknown rank.name)r   get_legacy_output_shapes	enumerater   flatten
ValueError_PaddedBatchDataset)input_dataset
batch_sizepadded_shapespadding_valuesdrop_remainderr   ishapes           Z/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/data/ops/padded_batch_op.py_padded_batchr       s~     88GMdll=9: =5 &&'S(:< = 	==
 

     c                 H   | j                   |j                   yt        | j                         t        |j                         k7  ryt        | j                   |j                         D ]:  \  }}|j                  |j                   |j                  |j                  k  s: y y)a  Returns `True` if `input_component_shape` can be padded to `padded_shape`.

  Args:
    padded_shape: A `tf.TensorShape`.
    input_component_shape: A `tf.TensorShape`.

  Returns:
    `True` if `input_component_shape` can be padded to `padded_shape`, otherwise
    `False`.
  TF)dimslenzipvalue)padded_shapeinput_component_shape
padded_dim	input_dims       r    _is_padded_shape_compatible_withr)   7   s     "7"<"<"D		s#8#=#=>>"<#4#4#8#=#= ? j)$)D9??*	
 
r   c                    	 t        j                  |       }t        j                  |j	                         D cg c]  }||nd
 c}t
        j                        }t!        ||      st        d
| d| d      |S c c}w # t        t        f$ r}t        j                  | t
        j                        }|j                  j                  ?t        |j                  j                        dk7  rt        d|  d|j                   d      ||j                  t
        j                  k7  r't        d|  d	|j                  j                   d      |t        j                  |      }Y d}~d}~ww xY w)aS  Converts `padded_shape` to a `tf.Tensor` representing that shape.

  Args:
    padded_shape: A shape-like object, which may be a `tf.TensorShape`, a Python
      sequence, or a 1-D `tf.Tensor` of `tf.int64` elements.
    input_component_shape: A `tf.TensorShape`, with which `padded_shape` must be
      compatible.

  Returns:
    A 1-D `tf.Tensor` of `tf.int64` elements, representing `padded_shape`.

  Raises:
    ValueError: If `padded_shape` is not a shape or not compatible with
      `input_component_shape`.
    TypeError: If `padded_shape` is not convertible to a `tf.int64` tensor.
  Ndtype)preferred_dtype   zPadded shape z7 must be a `tf.int64` vector tensor, but its shape was .z> must be a `tf.int64` vector tensor, but its element type was zThe padded shape z" is not compatible with the shape & of the corresponding input component.)r
   as_shaper   convert_to_tensoras_listr   int64	TypeErrorr   r   r!   r"   r-   r   r   constant_value_as_shaper)   )r%   r&   padded_shape_as_shapedimretes         r   _padded_shape_to_tensorr<   O   s~   "E(11,? 

(002! B&! '-ll	4C( 
**?*?
A
()>(? @22G1H I:; < < 
*5!
 Z	  E 

fll
KC
yy~~!c#))..&9Q&>, ("yyk,-234 yyFLL , (..1iinn-=Q@AFGH (??DEs)   6B A<B <B E.CE))E.c                     d }d }t        j                  |t        j                  |            }t        j                  | || |      S )zGReturns padding values with None elements replaced with default values.c                 X   | j                   t        j                  k(  ry| j                   t        j                  k(  rt	        d      | j                   t        j
                  k(  r%t        j                  dt        j
                        S t        j                  | j                               S )N zIUnable to create default padding value for a component of type 'variant'.r   r,   )
base_dtyper   stringvariantr6   bfloat16r   constantnp
zeros_likeas_numpy_dtype)ts    r   	make_zeroz-_padding_values_or_default.<locals>.make_zero   sv    ||v}}$	
	' + , ,	
	(!!!6??;;]]1++-..r   c                     | |S | S N )r$   defaults     r   value_or_defaultz4_padding_values_or_default.<locals>.value_or_default   s    m7..r   )r   map_structurer   get_legacy_output_typesmap_structure_up_to)r   r   rI   rN   default_paddings        r   _padding_values_or_defaultrS      sN    
// &&44]CE/		!	!.2B"0/
C Cr   c                    t        j                  | d      } | j                  j                  t	        j
                  g             st        d| j                   d      | j                  |k7  rt        d| j                   d| d      | S )a#  Converts the padding value to a tensor.

  Args:
    value: The padding value.
    output_type: Its expected dtype.

  Returns:
    A scalar `Tensor`.

  Raises:
    ValueError: if the padding value is not a scalar.
    TypeError: if the padding value's type does not match `output_type`.
  padding_valuer   zMInvalid `padding_values`. `padding_values` values should be scalars, but got r0   z7Invalid `padding_values`. `padding_values` values type z does not match type r1   )	r   r3   r   is_compatible_withr
   TensorShaper   r-   r6   )r$   output_types     r   _padding_value_to_tensorrY      s     

O
<%		'	'(@(@(D	E
 338;;-qB C C
[[K
 !KK=(=k] K<= > > 
,r   c                   6     e Zd ZdZ	 d fd	Zed        Z xZS )r   zEA `Dataset` that batches and pads contiguous elements from its input.c                     | _         d }t        j                  ||j                         | _         t	        j
                  |t        j                  d       _        t        |      t        j                  |      }t        j                  ||      }	g }
t        t        j                  |      |	      D ]   \  }}|
j                  t!        ||             " t        j"                  ||
       _        t        j&                  |      r.t        j&                        st        j                  fd|      t        j(                  |t*        t        j,                  |             _        t	        j
                  |t        j0                  d       _         fd}t        j                  | j$                        }t5        j6                  t        j,                   j                         |t        j8                   j                                _        | _        t?        j@                  |jB                   j                  t        j                   j$                        D cg c]'  }t	        j
                  |t        j                        ) c}t        j                   j.                         j2                  t5        jD                   j:                         jF                  jI                               }tJ           ||       y	c c}w )
z"See `Dataset.batch()` for details.c                     t        | t        j                        s4t        | t        j                        rt        d      t        d|  d      y )Nz8`padded_batch` is not supported for datasets of datasetsz`padded_batch` is only supported for datasets that produce tensor elements but type spec of elements in the input dataset is not a subclass of TensorSpec: `z`.)
isinstancer   
TensorSpecr   DatasetSpecr6   )component_specs    r   check_typesz1_PaddedBatchDataset.__init__.<locals>.check_types   s[    (>(>?nk&=&=>HJ J  ++2/ 0 	0	 @r   r   )r-   r   c                     S rK   rL   )_r   s    r   <lambda>z._PaddedBatchDataset.__init__.<locals>.<lambda>   s    N r   r   c                     t        j                  t        j                  j                        rt        j                  j                        nd g      j                  t        j                  |             S rK   )
r
   rW   r	   smart_constant_value_drop_remainderr   constant_value_batch_sizeconcatenater7   )sselfs    r   _padded_shape_to_batch_shapezB_PaddedBatchDataset.__init__.<locals>._padded_shape_to_batch_shape   s_    %%,,T-A-AB 
$
$T%5%5
6HL' 	 
[88;	<=r   r,   )r   r   r   r   output_shapesmetadataN)'_input_datasetr   rO   element_specr   r3   r   r5   ri   rS   r   r   flatten_up_tor#   r   appendr<   pack_sequence_as_padded_shapes	is_nestedrQ   rY   rP   _padding_valuesboolrg   r   convert_legacy_structureget_legacy_output_classes
_structure_namer   padded_batch_dataset_v2_variant_tensorget_flat_tensor_shapes	_metadataSerializeToStringsuper__init__)rl   r   r   r   r   r   r   ra   input_shapesflat_padded_shapesflat_padded_shapes_as_tensorsr&   r%   rm   rn   rk   variant_tensor	__class__s   `   `            r   r   z_PaddedBatchDataset.__init__   sy    (D0 	{M$>$>?'D,,&,,\;D/NN77FL++L-H$&!/2\"$608 H+|#**
!,0E
FHH
 //0MOD
 ~~l#DNN>,J))*B*68n  33.++M:<D 00fkk0@BD= &&'C'+':':<M88++D,?,?@---d.A.ABDDO DJ$<<%%## \\$"5"56
 !!!6<<8
 ||D$8$89++66tG113
5N 
G]N3
s    ,Lc                     | j                   S rK   )r{   )rl   s    r   rq   z _PaddedBatchDataset.element_spec  s    ??r   rK   )__name__
__module____qualname____doc__r   propertyrq   __classcell__)r   s   @r   r   r      s&    M L4\  r   r   )NNFN)r   numpyrE   tensorflow.python.data.opsr   tensorflow.python.data.utilr   r   tensorflow.python.frameworkr   r   r   r	   r
   r   r   tensorflow.python.opsr   r   r)   r<   rS   rY   UnaryDatasetr   rL   r   r   <module>r      sk    <  2 , 1 3 . + 2 4 3 3 1
 !%!%!&.00fC02S+22 Sr   