
    BVhQ                         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Z G d dej$                        Zy)z0The implementation of `tf.data.Dataset.rebatch`.    N)dataset_ops)nest)dtypes)ops)tensor_shape)tensor_util)gen_experimental_dataset_ops)numpy_compatc                     t        | |||      S N)_RebatchDataset)input_dataset
batch_sizedrop_remaindernames       U/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/data/ops/rebatch_op.py_rebatchr      s    	
ND	II    c                   D     e Zd ZdZ	 	 d fd	Zd Zd Zed        Z xZ	S )r   ae  A `Dataset` that rebatches elements from its input into new batch sizes.

  `_RebatchDataset(input_dataset, batch_sizes)` is functionally equivalent to
  `input_dataset.unbatch().batch(N)`, where the value of N cycles through the
  `batch_sizes` input list. The elements produced by this dataset have the same
  rank as the elements of the input dataset.
  c                    || _         t        j                  |t        j                  d      | _        t        j                  |t        j                  d      | _        || _        | j                         t        j                  fdt        j                  |            | _        t        j                  |      }t!        j"                  |j$                  f||d| j&                  }t(        | U  ||       y)z"See `Dataset.rebatch` for details.batch_sizes)dtyper   r   c                 B    | j                         j                        S r   )_unbatch_batch)tsnew_batch_dims    r   <lambda>z*_RebatchDataset.__init__.<locals>.<lambda>:   s    2;;=''6 r   )r   r   N)_input_datasetr   convert_to_tensorr   int64_batch_sizesbool_drop_remainder_name_compute_static_batch_dimr   map_structurer   get_structure_element_specnormalize_to_denseged_opsrebatch_dataset_v2_variant_tensor_flat_structuresuper__init__)selfr   r   r   r   variant_tensorr   	__class__s         @r   r0   z_RebatchDataset.__init__*   s     (D--6<<m=D00fkk0@BDDJ224M ++6!!-02D  22=AM//%% %  

	 N 
G]N3r   c                    t        j                  | j                        }|yt        |t        j
                        rtt        |j                        dk(  r"t	        j                  ||d   k(        r|d   }n;yt        |j                        dkD  r"t        dt        |j                         d      | j                  |      ry|S )a0  Computes the static batch dimension of a dataset if it can be determined.

    Given the RebatchDataset parameters, determines the batch dimension of this
    dataset statically. Returns None if this cannot be determined or is
    variable.

    Returns:
      An integer representing the batch dimension of the dataset. If it cannot
      be determined statically, returns None.

    Raises:
      ValueError: The batch_sizes parameter is malformed, input_dataset is
      not batched, or input_dataset batch sizes are incompatible with each
      other.
    N   r   ziInvalid `batch_sizes`. Expected `batch_sizes` to be a scalar or a vector. Received `batch_sizes` of rank .)r   constant_valuer"   
isinstancenpndarraylenshapeall
ValueError_may_form_partial_batches)r1   r   s     r   r&   z)_RebatchDataset._compute_static_batch_dimJ   s       ..t/@/@AM-,	]  	!Q	&66-=#334'*-}""#a'8=&&'(+, 	,
 %%m4r   c                    t        j                  | j                        ryd }t        j                  t        j                  | j                              D cg c]
  } ||       }}|D cg c]  }||	 }}|syt        j                  |      }t        j                  ||d   k(        st        d|d    d| d      |d   |z  dk7  S c c}w c c}w )z6Returns whether this dataset may form partial batches.Fc                     	 | j                         }t        |t        j                        sy |j
                  y t        |      dk  rt        d      |j                  d   j                  S # t        $ r Y y w xY w)Nr5   zInvalid `batch_sizes`. Expected dataset with rank of >= 1 but found a dataset with scalar elements. Fix the issue by adding the `batch` transformation to the dataset.r   )
_to_legacy_output_shapesNotImplementedErrorr8   r   TensorShaperankr;   r>   dimsvalue)	type_specr<   s     r   get_batch_dimz@_RebatchDataset._may_form_partial_batches.<locals>.get_batch_dimt   s~    224 |778			Ua : ; 	; ZZ]    ! s   A, ,	A87A8Tr   z?Invalid `input_dataset.` The batch dimension of component 0 is z., while the batch dimension of component i is r6   )r   r7   r$   r   flattenr   r(   r   r
   
np_asarrayr9   r=   r>   )r1   desired_batch_sizerI   r   input_batch_dimsdknown_input_batch_dimss          r   r?   z)_RebatchDataset._may_form_partial_batcheso   s    !!$"6"67!$ ,,{889L9LMN 	b  *:KAQ]aKK!)445KL66(,B1,EEF&q)* +56a9: :
 "!$'99Q>>! Ls   C-C5Cc                     | j                   S r   )r)   )r1   s    r   element_specz_RebatchDataset.element_spec   s    r   FN)
__name__
__module____qualname____doc__r0   r&   r?   propertyrQ   __classcell__)r3   s   @r   r   r   !   s6     $	4@#J%?N  r   r   rR   )rV   numpyr9   tensorflow.python.data.opsr   tensorflow.python.data.utilr   tensorflow.python.frameworkr   r   r   r   tensorflow.python.opsr	   r+   tensorflow.python.utilr
   r   UnaryDatasetr    r   r   <module>ra      s?    7  2 , . + 4 3 I /Jwk.. wr   