o
    i e?                     @   s   d Z ddl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 Zdd ZG dd dejZG dd dejZdd Zdd ZdS )z(Implementation of LoadDataset in Python.    N)message)text_format)snapshot_pb2)_pywrap_snapshot_utils)dataset_ops)structured_function)gen_experimental_dataset_ops)gfile)nested_structure_coderc                    s    fdd}|du rdd }|du r6t tj tjd}| }W d   n1 s-w   Y  t|}| }|rHt	 ||| t
 ||S t |||S )z$Loads dataset from tf.data snapshot.c               
      sj   z%t t d} t| t W  d   W S 1 sw   Y  W dS  tjt	j
tfy4   Y dS w )zReads the distributed snapshot metadata.

    Returns:
      DistributedSnapshotMetadata if the snapshot is a distributed snapshot.
      Returns None if it is a non-distributed snapshot.
    rN)r	   GFiler    TF_DATA_SnapshotMetadataFilePathr   
ParseLinesr   DistributedSnapshotMetadata
ParseErrorr   DecodeErrorUnicodeDecodeError)fpath Q/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/data/ops/load_op.py"_get_distributed_snapshot_metadata"   s   
(z1_load.<locals>._get_distributed_snapshot_metadataNc                 S   s   | j dd t tjdS )Nc                 S   s   | S Nr   )xr   r   r   <lambda>4   s    z)_load.<locals>.<lambda>.<locals>.<lambda>)cycle_lengthnum_parallel_calls)
interleavemultiprocessing	cpu_countr   AUTOTUNE)datasetsr   r   r   r   3   s
    z_load.<locals>.<lambda>rb)r	   r   osr   joinr   DATASET_SPEC_FILENAMEread_parse_element_spec_validate_snapshot_load_distributed_snapshot_LoadDataset)r   element_speccompressionreader_funcr   r   encoded_specdistributed_snapshot_metadatar   r   r   _load   s(   
r1   c                    sH   t |   fddt D }tj|}|fdd}||S )zLoads a distributed snapshot.c                    s   g | ]	}t j |qS r   )r$   r   r%   ).0r   )
chunks_dirr   r   
<listcomp>K   s    z._load_distributed_snapshot.<locals>.<listcomp>c                    s   t | t j jdS )N)r,   r-   )_SnapshotChunkDatasetr(   r,   r-   )
chunk_file)metadatar   r   r   O   s
    z,_load_distributed_snapshot.<locals>.<lambda>)r    TF_DATA_CommittedChunksDirectoryr	   ListDirectoryr   Datasetfrom_tensor_slicesmap)r   r7   r.   chunk_filesdatasetr   )r3   r7   r   r*   G   s   


r*   c                       ,   e Zd ZdZ fddZedd Z  ZS )r+   z.A dataset that loads previously saved dataset.c                    sh   || _ || _|| _tj|dtt| jd| _tj	|f| jj
j|| jj
d| j}t | d S )Nzload())input_structure)reader_func_other_argsr-   r.   )_path_element_spec_compressionr   StructuredFunctionWrapperr   DatasetSpec_reader_funcged_opsload_datasetfunctioncaptured_inputs_flat_structuresuper__init__)selfr   r,   r-   r.   variant_tensor	__class__r   r   rN   Y   s(   
z_LoadDataset.__init__c                 C      | j S r   rC   rO   r   r   r   r,   l      z_LoadDataset.element_spec__name__
__module____qualname____doc__rN   propertyr,   __classcell__r   r   rQ   r   r+   V   s
    r+   c                       r?   )r5   zAA dataset for one chunk file from a tf.data distributed snapshot.c                    s4   || _ || _tj|fd|i| j}t | d S )Nr-   )_chunk_filerC   rH   snapshot_chunk_datasetrL   rM   rN   )rO   r6   r,   r-   rP   rQ   r   r   rN   t   s   z_SnapshotChunkDataset.__init__c                 C   rS   r   rT   rU   r   r   r   r,   }   rV   z"_SnapshotChunkDataset.element_specrW   r   r   rQ   r   r5   q   s
    	r5   c                 C   s   t | std|  dt| }t |r2t |d}td|  d|  1 s-w   Y  t| }t |sDtd|  dt|j	}|r]||kr]td|  d| d| d	|rs||j
krutd|  d
| d|j
 d	dS dS )a8  Validates a tf.data distributed snapshot.

  Args:
    path: Root path of the distributed snapshot.
    metadata: The DistributedSnapshotMetadata of the snapshot.
    element_spec: Dataset element_spec.
    compression: Compression method used for saving.

  Raises:
    ValueError if the snapshot is invalid.
  z#Failed to load tf.data snapshot at z(: The snapshot directory does not exist.r   z+. The save job failed to write it. Status: Nz5. The save job has not finished writing the snapshot.z. User specified element_spec z!, but the actual element_spec is .z. User specified compression z , but the actual compression is )r	   Exists
ValueErrorr   TF_DATA_SnapshotErrorFilePathr   r'   TF_DATA_SnapshotDoneFilePathr(   r,   r-   )r   r7   r,   r-   
error_filer   	done_filesnapshot_element_specr   r   r   r)      sH   







r)   c                 C   s   t j }||  t |S r   )r
   
struct_pb2StructuredValueParseFromStringdecode_proto)encoded_element_spec	struct_pbr   r   r   r(      s   


r(   )r[   r   r$   google.protobufr   r   tensorflow.core.protobufr   +tensorflow.python.data.experimental.servicer   tensorflow.python.data.opsr   r   tensorflow.python.opsr   rH   tensorflow.python.platformr	   tensorflow.python.saved_modelr
   r1   r*   DatasetSourcer+   r5   r)   r(   r   r   r   r   <module>   s$   (-