1# Copyright 2019 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"""Utilities for managing forward accumulators. 16 17A separate file from forwardprop.py so that functions can use these utilities. 18""" 19 20from __future__ import absolute_import 21from __future__ import division 22from __future__ import print_function 23 24import collections 25import contextlib 26 27from tensorflow.python import pywrap_tfe 28 29 30class TangentInfo( 31 collections.namedtuple("TangentInfo", ["indices", "tangents"])): 32 """Packed forward accumulator state. The return value of `pack_tangents`.""" 33 34 def __new__(cls, indices=None, tangents=None): 35 if indices is None: 36 indices = () 37 if tangents is None: 38 tangents = [] 39 return super(TangentInfo, cls).__new__(cls, indices, tangents) 40 41 42def pack_tangents(tensors): 43 """Packs forward accumulator state into a TangentInfo tuple. 44 45 Args: 46 tensors: A flat list of Tensors to pack forward accumulator state for. 47 48 Returns: 49 A tuple of (indices, tangents): 50 indices: A sequence of sequences of two-element tuples. Each forward 51 accumulator is represented as a sequence of tuples with (primal_index, 52 jvp_index). Both integers index into the concatenated `tensors + jvps` 53 array. 54 tangents: A flat list of Tensors. Best interpreted as a sequence to be 55 appended to `tensors`. 56 """ 57 return TangentInfo(*pywrap_tfe.TFE_Py_PackJVPs(tensors)) 58 59 60@contextlib.contextmanager 61def push_forwardprop_state(): 62 """Temporarily push or pop transient state for accumulators in the active set. 63 64 Allows an accumulator which is currently processing an operation to 65 temporarily reset its state. This is useful when building forwardprop versions 66 of functions, where an accumulator will trigger function building and then 67 must process captured symbolic tensors while building it. Without pushing and 68 popping, accumulators ignore operations executed as a direct result of their 69 own jvp computations. 70 71 Yields: 72 None (used for its side effect). 73 """ 74 try: 75 pywrap_tfe.TFE_Py_ForwardAccumulatorPushState() 76 yield 77 finally: 78 pywrap_tfe.TFE_Py_ForwardAccumulatorPopState() 79