o
    ?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   Z TF_DATA_SnapshotMetadataFilePathr   Z
ParseLinesr   ZDistributedSnapshotMetadataZ
ParseErrorr   DecodeErrorUnicodeDecodeError)fpath c/home/www/facesmatcher.com/pyenv/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>)Zcycle_lengthZnum_parallel_calls)
interleavemultiprocessing	cpu_countr   ZAUTOTUNE)Zdatasetsr   r   r   r   3   s
    z_load.<locals>.<lambda>rb)r	   r   osr   joinr   ZDATASET_SPEC_FILENAMEread_parse_element_spec_validate_snapshot_load_distributed_snapshot_LoadDataset)r   element_speccompressionreader_funcr   r   Zencoded_specZdistributed_snapshot_metadatar   r   r   _load   s(   
r&   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   Z TF_DATA_CommittedChunksDirectoryr	   ZListDirectoryr   ZDatasetZfrom_tensor_slicesmap)r   r,   r%   Zchunk_filesZdatasetr   )r(   r,   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())Zinput_structure)Zreader_func_other_argsr$   r%   )_path_element_spec_compressionr   ZStructuredFunctionWrapperr   ZDatasetSpecZ_reader_funcged_opsZload_datasetfunctionZcaptured_inputs_flat_structuresuper__init__)selfr   r#   r$   r%   variant_tensor	__class__r   r   r6   Y   s(   
z_LoadDataset.__init__c                 C      | j S r   r0   r7   r   r   r   r#   l      z_LoadDataset.element_spec__name__
__module____qualname____doc__r6   propertyr#   __classcell__r   r   r9   r   r"   V   s
    r"   c                       r.   )r*   zAA dataset for one chunk file from a tf.data distributed snapshot.c                    s4   || _ || _tj|fd|i| j}t | d S )Nr$   )Z_chunk_filer0   r2   Zsnapshot_chunk_datasetr4   r5   r6   )r7   r+   r#   r$   r8   r9   r   r   r6   t   s   z_SnapshotChunkDataset.__init__c                 C   r;   r   r<   r=   r   r   r   r#   }   r>   z"_SnapshotChunkDataset.element_specr?   r   r   r9   r   r*   q   s
    	r*   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	   ZExists
ValueErrorr   ZTF_DATA_SnapshotErrorFilePathr   r   ZTF_DATA_SnapshotDoneFilePathr   r#   r$   )r   r,   r#   r$   Z
error_filer   Z	done_fileZsnapshot_element_specr   r   r   r       sH   







r    c                 C   s   t j }||  t |S r   )r
   Z
struct_pb2ZStructuredValueZParseFromStringZdecode_proto)Zencoded_element_specZ	struct_pbr   r   r   r      s   


r   )rC   r   r   Zgoogle.protobufr   r   Ztensorflow.core.protobufr   Z+tensorflow.python.data.experimental.servicer   Ztensorflow.python.data.opsr   r   Ztensorflow.python.opsr   r2   Ztensorflow.python.platformr	   Ztensorflow.python.saved_modelr
   r&   r!   ZDatasetSourcer"   r*   r    r   r   r   r   r   <module>   s$   (-