• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Directives are special no-op functions that serve as compilation markers.
16
17They provide static information like type hints, compilation and TensorFlow
18overrides.
19
20These serve as annotations in the compiled code, allowing the user some control
21over the compilation process. They have no functional role at runtime.
22"""
23
24from __future__ import absolute_import
25from __future__ import division
26from __future__ import print_function
27
28from tensorflow.python.util.tf_export import tf_export
29
30UNSPECIFIED = object()
31
32
33def set_element_type(entity, dtype, shape=UNSPECIFIED):
34  """Indicates that the entity is expected hold items of specified type/shape.
35
36  The staged TensorFlow ops will reflect and assert this data type. Ignored
37  otherwise.
38
39  Args:
40    entity: The entity to annotate.
41    dtype: TensorFlow dtype value to assert for entity.
42    shape: Optional shape to assert for entity.
43  """
44  del entity
45  del dtype
46  del shape
47
48
49@tf_export('autograph.experimental.set_loop_options')
50def set_loop_options(
51    parallel_iterations=UNSPECIFIED,
52    swap_memory=UNSPECIFIED,
53    maximum_iterations=UNSPECIFIED,
54    shape_invariants=UNSPECIFIED):
55  """Specifies additional arguments to be passed to the enclosing while_loop.
56
57  The parameters apply to and only to the immediately enclosing loop. It only
58  has effect if the loop is staged as a TF while_loop; otherwise the parameters
59  have no effect.
60
61  Usage:
62
63    >>> @tf.function(autograph=True)
64    ... def f():
65    ...   n = 0
66    ...   for i in tf.range(10):
67    ...     tf.autograph.experimental.set_loop_options(maximum_iterations=3)
68    ...     n += 1
69    ...   return n
70
71    >>> @tf.function(autograph=True)
72    ... def f():
73    ...   v = tf.constant((0,))
74    ...   for i in tf.range(3):
75    ...     tf.autograph.experimental.set_loop_options(
76    ...         shape_invariants=[(v, tf.TensorShape([None]))]
77    ...     )
78    ...     v = tf.concat((v, [i]), 0)
79    ...   return v
80
81  Also see tf.while_loop.
82
83  Args:
84    parallel_iterations: The maximum number of iterations allowed to run in
85        parallel at any given time. Note that this does not guarantee parallel
86        execution.
87    swap_memory: Whether to store intermediate values needed for
88        gradients on the CPU instead of GPU.
89    maximum_iterations: Allows limiting the total number of iterations executed
90        by the loop.
91    shape_invariants: Allows controlling the argument with the same name passed
92        to tf.while_loop. Unlike tf.while_loop, this is a list of
93        `(tensor, shape)` pairs.
94  """
95  del parallel_iterations
96  del swap_memory
97  del maximum_iterations
98  del shape_invariants
99