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