B
    ٻdf                 @   s4  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 ed	G d
d dejZedG dd dejZedG dd dejZedG dd de	jZedG dd de	jZedG dd de	jZededdG dd de	jZedG d d! d!e	jZdS )"z%API for specifying `tf.data` options.    N)logging)dataset_options_pb2)	model_pb2)options)deprecation)	tf_exportz#data.experimental.AutotuneAlgorithmc               @   s<   e Zd ZdZdZdZdZdZdZe	dd Z
e	d	d
 ZdS )AutotuneAlgorithma  Represents the type of autotuning algorithm to use.

  DEFAULT: The default behavior is implementation specific and may change over
  time.

  HILL_CLIMB: In each optimization step, this algorithm chooses the optimial
  parameter and increases its value by 1.

  GRADIENT_DESCENT: In each optimization step, this algorithm updates the
  parameter values in the optimal direction.

  MAX_PARALLELISM: Similar to HILL_CLIMB but uses a relaxed stopping condition,
  allowing the optimization to oversubscribe the CPU.

  STAGE_BASED: In each optimization step, this algorithm chooses the worst
  bottleneck parameter and increases its value by 1.
  r               c             C   sp   || j krtjj S || jkr$tjjS || jkr6tjjS || jkrHtjjS || jkrZtjjS td|j dd S )NzkInvalid `obj.` Supported values include `DEFAULT`, `HILL_CLIMB` `GRADIENT_DESCENT`, and `STAGE_BASED`. Got .)	DEFAULTr   r   
HILL_CLIMBGRADIENT_DESCENTMAX_PARALLELISMSTAGE_BASED
ValueErrorname)clsobj r   T/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/data/ops/options.py	_to_proto5   s    




zAutotuneAlgorithm._to_protoc             C   sn   |t jjkr| jS |t jjkr$| jS |t jjkr6| jS |t jjkrH| jS |t jjkrZ| jS td| dd S )NzjInvalid `pb.` Supported values include `DEFAULT`, `HILL_CLIMB`, `GRADIENT_DESCENT` and `STAGE_BASED`. Got r   )r   r   r   r   r   r   r   r   )r   pbr   r   r   _from_protoE   s    zAutotuneAlgorithm._from_protoN)__name__
__module____qualname____doc__r   r   r   r   r   classmethodr   r   r   r   r   r   r      s   r   z!data.experimental.AutoShardPolicyc               @   s<   e Zd ZdZdZdZdZdZdZe	dd Z
e	d	d
 ZdS )AutoShardPolicyaT  Represents the type of auto-sharding to use.

  OFF: No sharding will be performed.

  AUTO: Attempts FILE-based sharding, falling back to DATA-based sharding.

  FILE: Shards by input files (i.e. each worker will get a set of files to
  process). When this option is selected, make sure that there is at least as
  many files as workers. If there are fewer input files than workers, a runtime
  error will be raised.

  DATA: Shards by elements produced by the dataset. Each worker will process the
  whole dataset and discard the portion that is not for itself. Note that for
  this mode to correctly partitions the dataset elements, the dataset needs to
  produce elements in a deterministic order.

  HINT: Looks for the presence of `shard(SHARD_HINT, ...)` which is treated as a
  placeholder to replace with `shard(num_workers, worker_index)`.
  r   r	   r
   r   c             C   sp   || j krtjj S || jkr$tjjS || jkr6tjjS || jkrHtjjS || jkrZtjjS td|j ddS )zConvert enum to proto.zVInvalid `obj.` Supported values include `OFF`, `FILE`, `DATA`,`AUTO`, and `HINT`. Got r   N)	OFFr   r!   FILEDATAAUTOHINTr   r   )r   r   r   r   r   r   t   s    




zAutoShardPolicy._to_protoc             C   sn   |t jjkr| jS |t jjkr$| jS |t jjkr6| jS |t jjkrH| jS |t jjkrZ| jS td| ddS )zConvert proto to enum.zUInvalid `pb.` Supported values include `OFF`, `FILE`, `DATA`,`AUTO`, and `HINT`. Got r   N)r   r!   r#   r$   r%   r&   r'   r   )r   r   r   r   r   r      s    zAutoShardPolicy._from_protoN)r   r   r   r   r#   r&   r$   r%   r'   r    r   r   r   r   r   r   r!   V   s   r!   z%data.experimental.ExternalStatePolicyc               @   s4   e Zd ZdZdZdZdZedd Zedd Z	d	S )
ExternalStatePolicyzRepresents how to handle external state during serialization.

  See the `tf.data.Options.experimental_external_state_policy` documentation
  for more information.
  r   r	   r
   c             C   sL   || j krtjjS || jkr$tjjS || jkr6tjjS td|j	 ddS )zConvert enum to proto.zZInvalid `obj.` Supported values include `POLICY_IGNORE`,`POLICY_FAIL`, `POLICY_WARN`. Got r   N)
IGNOREr   r(   POLICY_IGNOREFAILPOLICY_FAILWARNPOLICY_WARNr   r   )r   r   r   r   r   r      s    


zExternalStatePolicy._to_protoc             C   sJ   |t jjkr| jS |t jjkr$| jS |t jjkr6| jS td| ddS )zConvert proto to enum.zYInvalid `pb.` Supported values include `POLICY_IGNORE`,`POLICY_FAIL`, `POLICY_WARN`. Got r   N)	r   r(   r*   r)   r,   r+   r.   r-   r   )r   r   r   r   r   r      s    zExternalStatePolicy._from_protoN)
r   r   r   r   r-   r)   r+   r    r   r   r   r   r   r   r(      s   r(   z!data.experimental.AutotuneOptionsc               @   sh   e Zd ZdZejdeddZejdeddZ	ejdeddZ
ejd	ed
dZdd Zdd Zdd ZdS )AutotuneOptionszRepresents options for autotuning dataset performance.

  ```python
  options = tf.data.Options()
  options.autotune.enabled = False
  dataset = dataset.with_options(options)
  ```
  enabledzKWhether to automatically tune performance knobs. If None, defaults to True.)r   ty	docstring
cpu_budgetzWhen autotuning is enabled (through `autotune`), determines the CPU budget to use. Values greater than the number of schedulable CPU cores are allowed but may result in CPU contention. If None, defaults to the number of schedulable CPU cores.
ram_budgetzWhen autotuning is enabled (through `autotune`), determines the RAM budget to use. Values greater than the available RAM in bytes may result in OOM. If None, defaults to half of the available RAM in bytes.autotune_algorithmzQWhen autotuning is enabled (through `autotune`), determines the algorithm to use.c             C   sZ   t  }| jd k	r| j|_| jd k	r,| j|_| jd k	r>| j|_| jd k	rVt| j|_|S )N)r   r/   r0   r3   r4   r5   r   r   )selfr   r   r   r   r      s    




zAutotuneOptions._to_protoc             C   sb   | dd k	r|j| _| dd k	r,|j| _| dd k	rB|j| _| dd k	r^t|j| _d S )NZoptional_enabledZoptional_cpu_budgetZoptional_ram_budgetZoptional_autotune_algorithm)
WhichOneofr0   r3   r4   r   r   r5   )r6   r   r   r   r   r      s    zAutotuneOptions._from_protoc             C   s   t | d| dS )zFChange the mutability value to `mutable` on this options and children._mutableN)object__setattr__)r6   mutabler   r   r   _set_mutable   s    zAutotuneOptions._set_mutableN)r   r   r   r   options_libcreate_optionboolr0   intr3   r4   r   r5   r   r   r<   r   r   r   r   r/      s(   	r/   z#data.experimental.DistributeOptionsc               @   sF   e Zd ZdZejdeddd dZejdedd	Z	d
d Z
dd ZdS )DistributeOptionsa  Represents options for distributed data processing.

  You can set the distribution options of a dataset through the
  `experimental_distribute` property of `tf.data.Options`; the property is
  an instance of `tf.data.experimental.DistributeOptions`.

  ```python
  options = tf.data.Options()
  options.experimental_distribute.auto_shard_policy = tf.data.experimental.AutoShardPolicy.OFF
  dataset = dataset.with_options(options)
  ```
  auto_shard_policyzcThe type of sharding to use. See `tf.data.experimental.AutoShardPolicy` for additional information.c               C   s   t jS )N)r!   r&   r   r   r   r   <lambda>      zDistributeOptions.<lambda>)r   r1   r2   default_factorynum_deviceszoThe number of devices attached to this input pipeline. This will be automatically set by `MultiDeviceIterator`.)r   r1   r2   c             C   s,   t  }t| j|_| jd k	r(| j|_|S )N)r   rA   r!   r   rB   rF   )r6   r   r   r   r   r   !  s
    
zDistributeOptions._to_protoc             C   s(   t |j| _|dd k	r$|j| _d S )NZoptional_num_devices)r!   r   rB   r7   rF   )r6   r   r   r   r   r   (  s    zDistributeOptions._from_protoN)r   r   r   r   r=   r>   r!   rB   r@   rF   r   r   r   r   r   r   rA     s   rA   z%data.experimental.OptimizationOptionsc               @   s   e Zd ZdZejdeddZejdeddZejdeddZ	ejd	ed
dZ
ejdeddZejdeddZejdeddZejdeddZejdeddZejdeddZejdeddZdd Zdd Zdd ZdS ) OptimizationOptionsa  Represents options for dataset optimizations.

  You can set the optimization options of a dataset through the
  `experimental_optimization` property of `tf.data.Options`; the property is
  an instance of `tf.data.experimental.OptimizationOptions`.

  ```python
  options = tf.data.Options()
  options.experimental_optimization.noop_elimination = True
  options.experimental_optimization.apply_default_optimizations = False
  dataset = dataset.with_options(options)
  ```
  apply_default_optimizationszWhether to apply default graph optimizations. If False, only graph optimizations that have been explicitly enabled will be applied.)r   r1   r2   filter_fusionzCWhether to fuse filter transformations. If None, defaults to False.filter_parallelizationzTWhether to parallelize stateless filter transformations. If None, defaults to False.inject_prefetchzWhether to inject prefetch transformation as the last transformation when the last transformation is a synchronous transformation. If None, defaults to True.map_and_batch_fusionzIWhether to fuse map and batch transformations. If None, defaults to True.map_and_filter_fusionzKWhether to fuse map and filter transformations. If None, defaults to False.
map_fusionz@Whether to fuse map transformations. If None, defaults to False.map_parallelizationzPWhether to parallelize stateless map transformations. If None, defaults to True.noop_eliminationzFWhether to eliminate no-op transformations. If None, defaults to True.parallel_batchzLWhether to parallelize copying of batch elements. If None, defaults to True.shuffle_and_repeat_fusionzNWhether to fuse shuffle and repeat transformations. If None, defaults to True.c             C   s   t  }| jd k	r| j|_| jd k	r,| j|_| jd k	r>| j|_| jd k	rP| j|_| jd k	rb| j|_| jd k	rt| j|_| jd k	r| j|_| j	d k	r| j	|_	| j
d k	r| j
|_
| jd k	r| j|_| jd k	r| j|_|S )N)r   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   )r6   r   r   r   r   r     s0    










zOptimizationOptions._to_protoc             C   s   | dd k	r|j| _| dd k	r,|j| _| dd k	rB|j| _| dd k	rX|j| _| dd k	rn|j| _| dd k	r|j| _| dd k	r|j| _| dd k	r|j| _| d	d k	r|j	| _	| d
d k	r|j
| _
| dd k	r|j| _d S )NZ$optional_apply_default_optimizationsZoptional_filter_fusionZoptional_filter_parallelizationZoptional_inject_prefetchZoptional_map_and_batch_fusionZoptional_map_and_filter_fusionZoptional_map_fusionZoptional_map_parallelizationZoptional_noop_eliminationZoptional_parallel_batchZ"optional_shuffle_and_repeat_fusion)r7   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   )r6   r   r   r   r   r     s,    zOptimizationOptions._from_protoc             C   s   t | d| dS )zFChange the mutability value to `mutable` on this options and children.r8   N)r9   r:   )r6   r;   r   r   r   r<     s    z OptimizationOptions._set_mutableN)r   r   r   r   r=   r>   r?   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   r   r   r<   r   r   r   r   rG   .  s`   rG   z"data.experimental.ThreadingOptionszdata.ThreadingOptionsc               @   s@   e Zd ZdZejdeddZejdeddZdd Z	d	d
 Z
dS )ThreadingOptionsam  Represents options for dataset threading.

  You can set the threading options of a dataset through the
  `experimental_threading` property of `tf.data.Options`; the property is
  an instance of `tf.data.ThreadingOptions`.

  ```python
  options = tf.data.Options()
  options.threading.private_threadpool_size = 10
  dataset = dataset.with_options(options)
  ```
  max_intra_op_parallelismz@If set, it overrides the maximum degree of intra-op parallelism.)r   r1   r2   private_threadpool_sizezIf set, the dataset will use a private threadpool of the given size. The value 0 can be used to indicate that the threadpool size should be determined at runtime based on the number of available CPU cores.c             C   s0   t  }| jd k	r| j|_| jd k	r,| j|_|S )N)r   rS   rT   rU   )r6   r   r   r   r   r     s    

zThreadingOptions._to_protoc             C   s0   | dd k	r|j| _| dd k	r,|j| _d S )NZ!optional_max_intra_op_parallelismZ optional_private_threadpool_size)r7   rT   rU   )r6   r   r   r   r   r     s    zThreadingOptions._from_protoN)r   r   r   r   r=   r>   r@   rT   rU   r   r   r   r   r   r   rS     s   rS   zdata.Optionsc                   s   e Zd ZdZejdededZejdeddZ	ejded	dZ
ejd
ededZejdeddZejdededZejdeddZejdeddZejdededZ fddZ fddZdd Zdd Zdd Zd d! Z  ZS )"Optionsal  Represents options for `tf.data.Dataset`.

  A `tf.data.Options` object can be, for instance, used to control which static
  optimizations to apply to the input pipeline graph or whether to use
  performance modeling to dynamically tune the parallelism of operations such as
  `tf.data.Dataset.map` or `tf.data.Dataset.interleave`.

  The options are set for the entire dataset and are carried over to datasets
  created through tf.data transformations.

  The options can be set by constructing an `Options` object and using the
  `tf.data.Dataset.with_options(options)` transformation, which returns a
  dataset with the options set.

  >>> dataset = tf.data.Dataset.range(42)
  >>> options = tf.data.Options()
  >>> options.deterministic = False
  >>> dataset = dataset.with_options(options)
  >>> print(dataset.options().deterministic)
  False

  Note: A known limitation of the `tf.data.Options` implementation is that the
  options are not preserved across tf.function boundaries. In particular, to
  set options for a dataset that is iterated within a tf.function, the options
  need to be set within the same tf.function.
  autotunezpThe autotuning options associated with the dataset. See `tf.data.experimental.AutotuneOptions` for more details.)r   r1   r2   rE   deterministiczZWhether the outputs need to be produced in deterministic order. If None, defaults to True.)r   r1   r2   experimental_deterministicz(DEPRECATED. Use `deterministic` instead.experimental_distributez}The distribution strategy options associated with the dataset. See `tf.data.experimental.DistributeOptions` for more details."experimental_external_state_policyaN  This option can be used to override the default policy for how to handle external state when serializing a dataset or checkpointing its iterator. There are three settings available - IGNORE: External state is ignored without a warning; WARN: External state is ignored and a warning is logged; FAIL: External state results in an error.experimental_optimizationzvThe optimization options associated with the dataset. See `tf.data.experimental.OptimizationOptions` for more details.experimental_slacka+  Whether to introduce 'slack' in the last `prefetch` of the input pipeline, if it exists. This may reduce CPU contention with accelerator host-side activity at the start of a step. The slack frequency is determined by the number of devices attached to this input pipeline. If None, defaults to False.experimental_threadingz$DEPRECATED. Use `threading` instead.	threadingzcThe threading options associated with the dataset. See `tf.data.ThreadingOptions` for more details.c                s>   |dkrt d t| dS |dkr.t| dS tt| |S )Nr^   zLoptions.experimental_threading is deprecated. Use options.threading instead.r_   rY   rX   )r   warninggetattrsuperrV   __getattribute__)r6   r   )	__class__r   r   rc   J  s    


zOptions.__getattribute__c                s\   |dkr(t d tt| d| d S |dkrFtt| d| d S tt| || d S )Nr^   zLoptions.experimental_threading is deprecated. Use options.threading instead.r_   rY   rX   )r   r`   rb   rV   r:   )r6   r   value)rd   r   r   r:   V  s    
zOptions.__setattr__c             C   s   t  }| jd k	r| j|_|j| j  |j| j  | j	d k	rVt
| j	|_|j| j  | jd k	rz| j|_|j| j  |S )N)r   rV   rX   autotune_optionsZCopyFromrW   r   distribute_optionsrZ   r[   r(   external_state_policyoptimization_optionsr\   r]   slackthreading_optionsr_   )r6   r   r   r   r   r   d  s    



zOptions._to_protoc             C   s   | dd k	r|j| _| j|j | j|j | dd k	rNt|j| _	| j
|j | dd k	rr|j| _| j|j d S )NZoptional_deterministicZoptional_external_state_policyZoptional_slack)r7   rX   rW   r   rf   rZ   rg   r(   rh   r[   r\   ri   rj   r]   r_   rk   )r6   r   r   r   r   r   t  s    
zOptions._from_protoc             C   sB   t | d| | j| | j| | j| | j| dS )zFChange the mutability value to `mutable` on this options and children.r8   N)r9   r:   rW   r<   rZ   r\   r_   )r6   r;   r   r   r   r<     s
    zOptions._set_mutablec             C   s   t | |S )a  Merges itself with the given `tf.data.Options`.

    If this object and the `options` to merge set an option differently, a
    warning is generated and this object's value is updated with the `options`
    object's value.

    Args:
      options: The `tf.data.Options` to merge with.

    Returns:
      New `tf.data.Options` object which is the result of merging self with
      the input `tf.data.Options`.
    )r=   Zmerge_options)r6   r   r   r   r   merge  s    zOptions.merge)r   r   r   r   r=   r>   r/   rW   r?   rX   rY   rA   rZ   r(   r[   rG   r\   r]   rS   r^   r_   rc   r:   r   r   r<   rl   __classcell__r   r   )rd   r   rV     s^   	rV   )r   enumZabslr   Ztensorflow.core.frameworkr   r   Ztensorflow.python.data.utilr   r=   Ztensorflow.python.utilr   Z tensorflow.python.util.tf_exportr   Enumr   IntEnumr!   r(   ZOptionsBaser/   rA   rG   Zdeprecated_endpointsrS   rV   r   r   r   r   <module>   s2   
9
B
%
D
)
 +