/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ // Utilities for dealing with XLA primitive types. #ifndef TENSORFLOW_COMPILER_XLA_PRIMITIVE_UTIL_H_ #define TENSORFLOW_COMPILER_XLA_PRIMITIVE_UTIL_H_ #include #include "tensorflow/compiler/xla/types.h" #include "tensorflow/compiler/xla/xla_data.pb.h" namespace xla { namespace primitive_util { // The number of exponent bits in a BF16 value. const int kBFloat16ExponentBits = 8; // The number of mantissa bits in a BF16 value. There is an implicit leading // 1, so there is an implicit additional bit of precision. const int kBFloat16MantissaBits = 7; // Returns the XLA primitive type (eg, F32) corresponding to the given // template parameter native type (eg, float). template PrimitiveType NativeToPrimitiveType() { // Make the expression depend on the template parameter NativeT so // that this compile-time error only apperas if this function is // instantiated with some concrete type that is not specialized // below. static_assert(!std::is_same::value, "Cannot map native type to primitive type."); return PRIMITIVE_TYPE_INVALID; } // Declarations of specializations for each native type which correspond to a // XLA primitive type. As an optimization, these are declared inline in the // header. template <> inline PrimitiveType NativeToPrimitiveType() { return PRED; } // Unsigned integer template <> inline PrimitiveType NativeToPrimitiveType() { return U8; } template <> inline PrimitiveType NativeToPrimitiveType() { return U16; } template <> inline PrimitiveType NativeToPrimitiveType() { return U32; } template <> inline PrimitiveType NativeToPrimitiveType() { return U64; } // Signed integer template <> inline PrimitiveType NativeToPrimitiveType() { return S8; } template <> inline PrimitiveType NativeToPrimitiveType() { return S16; } template <> inline PrimitiveType NativeToPrimitiveType() { return S32; } template <> inline PrimitiveType NativeToPrimitiveType() { return S64; } // Floating point template <> inline PrimitiveType NativeToPrimitiveType() { return F32; } template <> inline PrimitiveType NativeToPrimitiveType() { return F64; } template <> inline PrimitiveType NativeToPrimitiveType() { return F16; } template <> inline PrimitiveType NativeToPrimitiveType() { return BF16; } // Complex template <> inline PrimitiveType NativeToPrimitiveType() { return C64; } bool IsFloatingPointType(PrimitiveType type); bool IsComplexType(PrimitiveType type); bool IsSignedIntegralType(PrimitiveType type); bool IsUnsignedIntegralType(PrimitiveType type); bool IsIntegralType(PrimitiveType type); // Returns true if values of the given primitive type are held in array shapes. bool IsArrayType(PrimitiveType primitive_type); // Returns the number of bits in the representation for a given type. int BitWidth(PrimitiveType type); // Returns the real, imag component type underlying the given complex type. // LOG(FATAL)'s if complex_type is not complex. PrimitiveType ComplexComponentType(PrimitiveType complex_type); // Returns the native type (eg, float) corresponding to the given template // parameter XLA primitive type (eg, F32). template struct PrimitiveTypeToNative; // Declarations of specializations for each native type which correspond to a // XLA primitive type. template <> struct PrimitiveTypeToNative { using type = bool; }; // Unsigned integer template <> struct PrimitiveTypeToNative { using type = uint8; }; template <> struct PrimitiveTypeToNative { using type = uint16; }; template <> struct PrimitiveTypeToNative { using type = uint32; }; template <> struct PrimitiveTypeToNative { using type = uint64; }; // Signed integer template <> struct PrimitiveTypeToNative { using type = int8; }; template <> struct PrimitiveTypeToNative { using type = int16; }; template <> struct PrimitiveTypeToNative { using type = int32; }; template <> struct PrimitiveTypeToNative { using type = int64; }; // Floating point template <> struct PrimitiveTypeToNative { using type = float; }; template <> struct PrimitiveTypeToNative { using type = double; }; template <> struct PrimitiveTypeToNative { using type = half; }; template <> struct PrimitiveTypeToNative { using type = bfloat16; }; // Complex template <> struct PrimitiveTypeToNative { using type = complex64; }; } // namespace primitive_util } // namespace xla #endif // TENSORFLOW_COMPILER_XLA_PRIMITIVE_UTIL_H_