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"""Decorator to overrides the gradient for a function.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21from tensorflow.python.client import pywrap_tf_session 22from tensorflow.python.framework import dtypes 23from tensorflow.python.framework import ops 24 25 26get_resource_handle_data = ops.get_resource_handle_data 27 28 29def copy_handle_data(source_t, target_t): 30 """Copies HandleData for variant and resource type tensors if available. 31 32 The CppShapeInferenceResult::HandleData proto contains information about the 33 shapes and types of the element tensors of resource/variant type tensors. 34 We need to copy this across function boundaries, i.e., when capturing a 35 placeholder or when returning a function tensor as output. If we don't do this 36 the element tensors will have unknown shapes, e.g., if a TensorList variant 37 tensor is captured as a placeholder, elements popped from that list would have 38 unknown shape. 39 40 Args: 41 source_t: The tensor to copy HandleData from. 42 target_t: The tensor to copy HandleData to. 43 """ 44 if (target_t.dtype == dtypes.resource or 45 target_t.dtype == dtypes.variant): 46 if isinstance(source_t, ops.EagerTensor): 47 handle_data = source_t._handle_data # pylint: disable=protected-access 48 else: 49 handle_data = get_resource_handle_data(source_t) 50 if (handle_data is not None 51 and handle_data.is_set 52 and handle_data.shape_and_type): 53 set_handle_data(target_t, handle_data) 54 55 56def set_handle_data(target_t, handle_data): 57 # pylint: disable=protected-access 58 if isinstance(target_t, ops.EagerTensor): 59 target_t._handle_data = handle_data 60 return 61 pywrap_tf_session.SetHandleShapeAndType(target_t.graph._c_graph, 62 target_t._as_tf_output(), 63 handle_data.SerializeToString()) 64 # pylint: enable=protected-access 65