• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 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"""Shared utilities related to backprop."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21from tensorflow.core.framework import types_pb2
22from tensorflow.python.framework import dtypes
23from tensorflow.python.framework import ops
24from tensorflow.python.framework import tensor_util
25from tensorflow.python.ops import handle_data_util
26
27
28def _DTypeFromTensor(tensor):
29  """Extract either `tensor.dtype` or the unanimous sub-type of a variant."""
30  dtype = tensor.dtype
31  if dtype.base_dtype == dtypes.variant:
32    # If we know statically that the data a variant points to is non-trainable
33    # then the variant itself is non-trainable.
34    if isinstance(tensor, ops.EagerTensor):
35      handle_data = tensor._handle_data  # pylint: disable=protected-access
36    else:
37      handle_data = handle_data_util.get_resource_handle_data(tensor)
38    if (handle_data is not None
39        and handle_data.is_set
40        and handle_data.shape_and_type):
41      first_type = handle_data.shape_and_type[0].dtype
42      # Some variants have statically unknown dtypes; we can't make inferences
43      # about trainability, so we conservatively assume they're trainable
44      # (which may waste memory passing zeros around, but will be correct).
45      if (first_type != types_pb2.DT_INVALID
46          and all(shape_and_type.dtype == first_type
47                  for shape_and_type in handle_data.shape_and_type)):
48        return first_type
49  return dtype
50
51
52def IsTrainable(tensor_or_dtype):
53  """Determines whether a tensor or dtype supports infinitesimal changes."""
54  if tensor_util.is_tf_type(tensor_or_dtype):
55    dtype = _DTypeFromTensor(tensor_or_dtype)
56  else:
57    dtype = tensor_or_dtype
58  dtype = dtypes.as_dtype(dtype)
59  return dtype.base_dtype in (dtypes.float16, dtypes.float32, dtypes.float64,
60                              dtypes.complex64, dtypes.complex128,
61                              dtypes.resource, dtypes.variant, dtypes.bfloat16)
62