You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.7 KiB

  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 TPU inference."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import tensorflow as tf
  20. def bfloat16_to_float32(tensor):
  21. """Converts a tensor to tf.float32 only if it is tf.bfloat16."""
  22. if tensor.dtype == tf.bfloat16:
  23. return tf.cast(tensor, dtype=tf.float32)
  24. else:
  25. return tensor
  26. def bfloat16_to_float32_nested(bfloat16_tensor_dict):
  27. """Converts bfloat16 tensors in a nested structure to float32.
  28. Other tensors not of dtype bfloat16 will be left as is.
  29. Args:
  30. bfloat16_tensor_dict: A Python dict, values being Tensor or Python
  31. list/tuple of Tensor.
  32. Returns:
  33. A Python dict with the same structure as `bfloat16_tensor_dict`,
  34. with all bfloat16 tensors converted to float32.
  35. """
  36. float32_tensor_dict = {}
  37. for k, v in bfloat16_tensor_dict.items():
  38. if isinstance(v, tf.Tensor):
  39. float32_tensor_dict[k] = bfloat16_to_float32(v)
  40. elif isinstance(v, (list, tuple)):
  41. float32_tensor_dict[k] = [bfloat16_to_float32(t) for t in v]
  42. return float32_tensor_dict