
    BVhe-                     R   d 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lmZ ddlmZ ddlmZ ddlmZ  ed      dd       Z ed      dd       Z G d dej4                        Z G d dej8                        Zd Zy)z#Python wrapper for prefetching_ops.    )dataset_ops)iterator_ops)structured_function)	structure)def_function)device)dtypes)ops)tensor_spec)	array_ops)functional_ops)gen_dataset_ops)gen_experimental_dataset_ops)resource_variable_ops)	tf_exportz$data.experimental.prefetch_to_deviceNc                       fd}|S )a  A transformation that prefetches dataset values to the given `device`.

  NOTE: Although the transformation creates a `tf.data.Dataset`, the
  transformation must be the final `Dataset` in the input pipeline.

  For example,
  >>> dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3])
  >>> dataset = dataset.apply(tf.data.experimental.prefetch_to_device("/cpu:0"))
  >>> for element in dataset:
  ...   print(f'Tensor {element} is on device {element.device}')
  Tensor 1 is on device /job:localhost/replica:0/task:0/device:CPU:0
  Tensor 2 is on device /job:localhost/replica:0/task:0/device:CPU:0
  Tensor 3 is on device /job:localhost/replica:0/task:0/device:CPU:0

  Args:
    device: A string. The name of a device to which elements will be prefetched.
    buffer_size: (Optional.) The number of elements to buffer on `device`.
      Defaults to an automatically chosen value.

  Returns:
    A `Dataset` transformation function, which can be passed to
    `tf.data.Dataset.apply`.
  c                 X    | j                  t                    j                        S )N)target_device)applycopy_to_deviceprefetch)datasetbuffer_sizer   s    g/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/data/experimental/ops/prefetching_ops.py	_apply_fnz%prefetch_to_device.<locals>._apply_fn:   s'    ==V,..6h{.CD     )r   r   r   s   `` r   prefetch_to_devicer   !   s    2D 
r   z data.experimental.copy_to_devicec                       fd}|S )aZ  A transformation that copies dataset elements to the given `target_device`.

  Args:
    target_device: The name of a device to which elements will be copied.
    source_device: The original device on which `input_dataset` will be placed.

  Returns:
    A `Dataset` transformation function, which can be passed to
    `tf.data.Dataset.apply`.
  c                      t        |       S )N)r   source_device)_CopyToDeviceDataset)r   r!   r   s    r   r   z!copy_to_device.<locals>._apply_fnN   s    }MK Kr   r   )r   r!   r   s   `` r   r   r   A   s    K 
r   c                   .     e Zd ZdZd fd	Z fdZ xZS )r"   z3A `Dataset` that copies elements to another device.c                     |j                          _        | _        t        j                         j                   j                        }|j                  dk(   _        | _        t        j                  |       _        t        j                   j                  j                        t        j                           fd       }|j#                         t        j                           fd       }|j#                          _         j$                  j&                   _        t        j                   t+        j,                  g t.        j0                        g       fd       }|j#                         t        j                   t+        j,                  g t.        j0                        gddi       fd	       }|j#                          _         j2                  j&                   _        t        j                   t+        j,                  g t.        j0                        g       fd
       }	|	j#                         t        j                   t+        j,                  g t.        j0                        g       fd       }
|
j#                          _         j6                  j&                   _        t        j:                         } j$                  j=                  |        j2                  j=                  |        j6                  j=                  |       t        j>                   j                        5  t        j@                   j(                   j4                   j8                  f j$                   j2                   j6                  d j                  jB                  }ddd       tD        tF           |       y# 1 sw Y   xY w)zConstructs a _CopyToDeviceDataset.

    Args:
      input_dataset: `Dataset` to be copied
      target_device: The name of the device to which elements would be copied.
      source_device: Device where input_dataset would be placed.
    GPUc                  *   t        j                        } t        j                  di j                  j                  }t        j                  t        j                  | |      g      5  t        j                  |      cddd       S # 1 sw Y   yxY w)zCreates an iterator for the input dataset.

      Returns:
        A `string` tensor that encapsulates the iterator created.
      Nr   )	r   unwrap_dataset_variantanonymous_iterator_input_dataset_flat_structurer
   control_dependenciesmake_iteratoriterator_to_string_handle)
ds_variantresourceselfwrap_ds_variants     r   
_init_funcz1_CopyToDeviceDataset.__init__.<locals>._init_funcm   s     #99/Jj 33 1//1h##((X>
?A C88BC C Cs   *B		Bc                  |    t        j                  j                   j                  t        j
                  g       S N)targetargsToutf)r   remote_call_source_devicecaptured_inputsr	   string)init_func_concreter0   s   r   _remote_init_funcz8_CopyToDeviceDataset.__init__.<locals>._remote_init_func}   s4    ''$$!11	   r   )input_signaturec           	      ~   t        j                  j                        5  t        j                  j                  | t        j                        t        j                        t        j                              }ddd       t        j                  j                  j                               S # 1 sw Y   7xY w)zCalls get_next for created iterator.

      Args:
        string_handle: An iterator string handle created by _init_func
      Returns:
        The elements generated from `input_dataset`
      N)r
   r   _source_device_stringr   Iteratorfrom_string_handler   get_legacy_output_typesget_legacy_output_shapesget_legacy_output_classesr   to_tensor_listelement_specget_next)string_handleiteratorr0   s     r   
_next_funcz1_CopyToDeviceDataset.__init__.<locals>._next_func   s     ::d001 9((;;//500611$7	99 %%d&7&79J9J9LMM9 9s   AB33B<experimental_ints_on_deviceT)r?   experimental_attributesc                     t        j                  j                  | gj                  z   j                  j
                        S r4   )r   r9   r:   r;   r)   _flat_types)rJ   next_func_concreter0   s    r   _remote_next_funcz8_CopyToDeviceDataset.__init__.<locals>._remote_next_func   sD     ''$$!3!C!CC""..	   r   c                 "   t        j                  | fi j                  j                  }t	        j
                  t        j                  |d      g      5  t        j                  dt        j                        cddd       S # 1 sw Y   yxY w)zDestroys the iterator resource created.

      Args:
        string_handle: An iterator string handle created by _init_func
      Returns:
        Tensor constant 0
      T)ignore_lookup_errorr   N)r   iterator_from_string_handle_v2r)   r*   r
   r+   r   destroy_resource_opr   constantr	   int64)rJ   iterator_resourcer0   s     r   _finalize_funcz5_CopyToDeviceDataset.__init__.<locals>._finalize_func   s     *HH
1//1 ##

3
3T;%< = 3 !!!V\\23 3 3s   $BBc                     t        j                  j                  | gj                  z   t        j
                  g      S r4   )r   r9   r:   r;   r	   rX   )rJ   finalize_func_concreter0   s    r   _remote_finalize_funcz<_CopyToDeviceDataset.__init__.<locals>._remote_finalize_func   s>     ''$$!7!G!GG~"	$ $r   )	init_func	next_funcfinalize_funcN)%_apply_debug_optionsr)   _target_deviceframework_device
DeviceSpecfrom_stringdevice_type_is_gpu_targetrA   r
   convert_to_tensorr:   r   wrap_dataset_variant_variant_tensorr   functionget_concrete_functionr2   r;   _init_captured_argsr   
TensorSpecr	   r<   rL   _next_captured_argsrZ   _finalize_captured_argsget_default_graphadd_to_graphr   generator_datasetr*   superr"   __init__)r0   input_datasetr   r!   specr2   r>   rL   rR   rZ   r]   gvariant_tensorr\   r=   rQ   r1   	__class__s   `            @@@@r   ru   z_CopyToDeviceDataset.__init__[   s,    (<<>D'D&&(44T5H5HID++u4D!.D//>D%::++-O C C $99;    (==?DO#>>D$//FMMBCENEN  $99;$//FMMBC!> EG G  (==?DO#>>D$//FMMBCE3E3  ,AAC$//FMMBCE$E$ 0EE D#'#6#6#F#FD AOO  #OO  #$$Q' 
D''	( 1&88

"
"

"
"

&
&1 OOOO++1 //1n1 

.}nM1 1s   5A-N??Oc                 V    | j                   rt        d      t        t        |          S )Nzz`make_one_shot_iterator` is not compatible with GPU execution. Please use `Dataset.make_initializable_iterator()` instead.)rg   
ValueErrorrt   r"   make_one_shot_iterator)r0   rz   s    r   r}   z+_CopyToDeviceDataset.make_one_shot_iterator   s2    H 
 'EGGr   z/cpu:0)__name__
__module____qualname____doc__ru   r}   __classcell__rz   s   @r   r"   r"   X   s    ;~NJH Hr   r"   c                   @     e Zd ZdZd fd	Zd Zed        Zd Z xZ	S )_MapOnGpuDatasetzBA `Dataset` that maps a function over elements in its using a GPU.c                    || _         || _        t        j                  || j	                         |ddi      | _        t        j                  | j                   j                  | j
                  j                  j                  f| j
                  j                  | j                  d| j                  }t        t        | ;  ||       y)z See `Dataset.map()` for details.rM   T)r   defun_kwargs)r8   use_inter_op_parallelismN)r)   _use_inter_op_parallelismr   StructuredFunctionWrapper_transformation_name	_map_funcged_opsexperimental_map_datasetrj   rk   r;   r*   rt   r   ru   )r0   rv   map_funcr   ry   rz   s        r   ru   z_MapOnGpuDataset.__init__   s    'D%=D"(BB!!#3T:	<DN
 55++//  ..
!
!!%!?!?	 
 

 N 

D*=.Ir   c                     | j                   gS N)r   r0   s    r   
_functionsz_MapOnGpuDataset._functions   s    NNr   c                 .    | j                   j                  S r   )r   output_structurer   s    r   rH   z_MapOnGpuDataset.element_spec  s    >>***r   c                      y)Nzmap_on_gpu()r   r   s    r   r   z%_MapOnGpuDataset._transformation_name  s    r   )T)
r   r   r   r   ru   r   propertyrH   r   r   r   s   @r   r   r      s*    JJ$ + +r   r   c                       fd}|S )aM  Maps `map_func` across the elements of this dataset.

  NOTE: This is a highly experimental version of `tf.data.Dataset.map` that runs
  `map_func` on GPU. It must be used after applying the
  `tf.data.experimental.copy_to_device` transformation with a GPU device
  argument.

  Args:
    map_func: A function mapping a nested structure of tensors (having shapes
      and types defined by `self.output_shapes` and `self.output_types`) to
      another nested structure of tensors.

  Returns:
    A `Dataset` transformation function, which can be passed to
    `tf.data.Dataset.apply`.
  c                     t        |       S r   )r   )r   r   s    r   r   zmap_on_gpu.<locals>._apply_fn  s    GX..r   r   )r   r   s   ` r   
map_on_gpur   
  s    $/ 
r   r   r~   )r   tensorflow.python.data.opsr   r   r   tensorflow.python.data.utilr   tensorflow.python.eagerr   tensorflow.python.frameworkr   rc   r	   r
   r   tensorflow.python.opsr   r   r   r   r   r    tensorflow.python.util.tf_exportr   r   r   UnaryUnchangedStructureDatasetr"   UnaryDatasetr   r   r   r   r   <module>r      s    * 2 3 : 1 0 B . + 3 + 0 1 I 7 6 12 3> -. /,OH;EE OHd{// @r   