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 16 #ifndef TENSORFLOW_CORE_FRAMEWORK_BFLOAT16_H_ 17 #define TENSORFLOW_CORE_FRAMEWORK_BFLOAT16_H_ 18 19 #include "tensorflow/core/framework/numeric_types.h" 20 #include "tensorflow/core/platform/byte_order.h" 21 #include "tensorflow/core/platform/types.h" 22 23 #if defined(PLATFORM_WINDOWS) 24 #include "tensorflow/core/platform/windows/cpu_info.h" 25 #endif 26 27 // Compact 16-bit encoding of floating point numbers. This representation uses 28 // 1 bit for the sign, 8 bits for the exponent and 7 bits for the mantissa. It 29 // is assumed that floats are in IEEE 754 format so the representation is just 30 // bits 16-31 of a single precision float. 31 // 32 // NOTE: The IEEE floating point standard defines a float16 format that 33 // is different than this format (it has fewer bits of exponent and more 34 // bits of mantissa). We don't use that format here because conversion 35 // to/from 32-bit floats is more complex for that format, and the 36 // conversion for this format is very simple. 37 // 38 // Because of the existing IEEE float16 type, we do not name our representation 39 // "float16" but just use "uint16". 40 // 41 // <-----our 16bits float-------> 42 // s e e e e e e e e f f f f f f f f f f f f f f f f f f f f f f f 43 // <------------------------------float--------------------------> 44 // 3 3 2 2 1 1 0 45 // 1 0 3 2 5 4 0 46 // 47 // 48 // This type only supports conversion back and forth with float. 49 // 50 // This file must be compilable by nvcc. 51 // 52 // The type is defined in framework/numeric_types.h. 53 54 namespace tensorflow { 55 56 // Conversion routines between an array of float and bfloat16 of 57 // "size". 58 void FloatToBFloat16(const float* src, bfloat16* dst, int64 size); 59 void BFloat16ToFloat(const bfloat16* src, float* dst, int64 size); 60 61 } // namespace tensorflow 62 63 #endif // TENSORFLOW_CORE_FRAMEWORK_BFLOAT16_H_ 64