o
    ?e                     @   s   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dZG dd dejZdS )z0The implementation of `tf.data.Dataset.rebatch`.    N)dataset_ops)nest)dtypes)ops)tensor_shape)tensor_util)gen_experimental_dataset_opsFc                 C   s   t | |||S N)_RebatchDataset)input_datasetZ
batch_sizedrop_remaindername r   f/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/data/ops/rebatch_op.py_rebatch   s   r   c                       sB   e Zd ZdZ		d fdd	Zdd Zdd	 Zed
d Z  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.
  FNc                    s   || _ tj|tjdd| _tj|tjdd| _|| _| 	  t
 fddt|| _t|}tj|jf||d| j}t || dS )z"See `Dataset.rebatch` for details.batch_sizes)Zdtyper   r   c                    s   |    S r	   )Z_unbatchZ_batch)tsnew_batch_dimr   r   <lambda>9   s    z*_RebatchDataset.__init__.<locals>.<lambda>)r   r   N)_input_datasetr   Zconvert_to_tensorr   Zint64_batch_sizesbool_drop_remainder_name_compute_static_batch_dimr   Zmap_structurer   get_structure_element_specZnormalize_to_denseged_opsZrebatch_dataset_v2Z_variant_tensorZ_flat_structuresuper__init__)selfr   r   r   r   Zvariant_tensor	__class__r   r   r    )   s.   

z_RebatchDataset.__init__c                 C   s   t | j}|du rdS t|tjr;t|jdkr)t||d kr'|d }q;dS t|jdkr;t	dt|j d| 
|rBdS |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npZndarraylenshapeall
ValueError_may_form_partial_batches)r!   r   r   r   r   r   I   s"   

z)_RebatchDataset._compute_static_batch_dimc                    s   t | jrdS dd   fddtt| jD }dd |D }|s'dS t	|}t
||d ksBtd	|d  d
| d|d | dkS )z6Returns whether this dataset may form partial batches.Fc                 S   s`   z|   }W n
 ty   Y d S w t|tjsd S |jd u r d S t|dk r*td|jd j	S )Nr$   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   )
Z_to_legacy_output_shapesNotImplementedErrorr'   r   ZTensorShapeZrankr)   r,   dimsvalue)Z	type_specr*   r   r   r   get_batch_dims   s   
z@_RebatchDataset._may_form_partial_batches.<locals>.get_batch_dimc                    s   g | ]} |qS r   r   ).0r   r1   r   r   
<listcomp>   s    z=_RebatchDataset._may_form_partial_batches.<locals>.<listcomp>c                 S   s   g | ]}|d ur|qS r	   r   )r2   dr   r   r   r4      s    Tr   z?Invalid `input_dataset.` The batch dimension of component 0 is z., while the batch dimension of component i is r%   )r   r&   r   r   flattenr   r   r   r(   Zasarrayr+   r,   )r!   Zdesired_batch_sizeZinput_batch_dimsZknown_input_batch_dimsr   r3   r   r-   n   s&   

z)_RebatchDataset._may_form_partial_batchesc                 C   s   | j S r	   )r   )r!   r   r   r   element_spec   s   z_RebatchDataset.element_specFN)
__name__
__module____qualname____doc__r    r   r-   propertyr7   __classcell__r   r   r"   r   r
       s     %'r
   r8   )r<   numpyr(   Ztensorflow.python.data.opsr   Ztensorflow.python.data.utilr   Ztensorflow.python.frameworkr   r   r   r   Ztensorflow.python.opsr   r   r   ZUnaryDatasetr
   r   r   r   r   <module>   s   
