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 tensorflow.python.util.tf_export import tf_export 25 26UNSPECIFIED = object() 27 28 29def set_element_type(entity, dtype, shape=UNSPECIFIED): 30 """Indicates that the entity is expected hold items of specified type/shape. 31 32 The staged TensorFlow ops will reflect and assert this data type. Ignored 33 otherwise. 34 35 Args: 36 entity: The entity to annotate. 37 dtype: TensorFlow dtype value to assert for entity. 38 shape: Optional shape to assert for entity. 39 """ 40 del entity 41 del dtype 42 del shape 43 44 45@tf_export('autograph.experimental.set_loop_options') 46def set_loop_options( 47 parallel_iterations=UNSPECIFIED, 48 swap_memory=UNSPECIFIED, 49 maximum_iterations=UNSPECIFIED, 50 shape_invariants=UNSPECIFIED): 51 """Specifies additional arguments to be passed to the enclosing while_loop. 52 53 The parameters apply to and only to the immediately enclosing loop. It only 54 has effect if the loop is staged as a TF while_loop; otherwise the parameters 55 have no effect. 56 57 Usage: 58 59 >>> @tf.function(autograph=True) 60 ... def f(): 61 ... n = 0 62 ... for i in tf.range(10): 63 ... tf.autograph.experimental.set_loop_options(maximum_iterations=3) 64 ... n += 1 65 ... return n 66 67 >>> @tf.function(autograph=True) 68 ... def f(): 69 ... v = tf.constant((0,)) 70 ... for i in tf.range(3): 71 ... tf.autograph.experimental.set_loop_options( 72 ... shape_invariants=[(v, tf.TensorShape([None]))] 73 ... ) 74 ... v = tf.concat((v, [i]), 0) 75 ... return v 76 77 Also see tf.while_loop. 78 79 Args: 80 parallel_iterations: The maximum number of iterations allowed to run in 81 parallel at any given time. Note that this does not guarantee parallel 82 execution. 83 swap_memory: Whether to store intermediate values needed for 84 gradients on the CPU instead of GPU. 85 maximum_iterations: Allows limiting the total number of iterations executed 86 by the loop. 87 shape_invariants: Allows controlling the argument with the same name passed 88 to tf.while_loop. Unlike tf.while_loop, this is a list of 89 `(tensor, shape)` pairs. 90 """ 91 del parallel_iterations 92 del swap_memory 93 del maximum_iterations 94 del shape_invariants 95