Half precision (float16) is a binary floating-point format that occupies 16 bits in memory. float16 is half the size of traditional 32-bit single precision format (float) and has lower precision and smaller range.
When high precision computation is not required, using float16 data type could potentially
- reduce storage space, memory bandwidth, and power usages;
- increase the chance of data fitting into a smaller cache of lower latency;
- provide arithmetic speed up if supported by hardware.
## Survey of current float16 support
A brief survey of float16 support on different compilers, hardwares, and libraries can be found below. Interested readers can refer to [link1](https://github.com/PaddlePaddle/Paddle/issues/4853) and [link2](https://github.com/Xreki/Xreki.github.io/blob/master/multi_data_types_in_dl_framework/ppt/float16_and_quantized_type.md) for more info.
The goal of float16 is to serve as a key for the executor to find and run the correct version of compute method specialized for float16 in operator kernel. It should be compatible with various natively supported float16 implementations including `__half` for cuda, `float16_t` for ARM, and `Eigen::half` for Eigen to make writing customized float16 kernels easier.
### Compiler
- nvcc supports `__half` data type after CUDA 7.5.
- `__fp16` or `float16_t` is supported as storage type for gcc >= 6.1 and clang >= 3.4.
- `__fp16` or `float16_t` is supported as arithmetic type for gcc >= 7.1 and clang >= 3.9.
### Hardware
- `__half` is supported on GPU with compute capability >= 5.3.
- `__fp16` is supported as storage type for ARMv7-A, ARMv8-A, and above.
- `__fp16` is supported as arithmetic type after ARMv8.2-A (currently, the only microarchitecture implementing ARMv8.2-A is ARM Cortex-A75, which is announced in May 2017. There seems to be no application processors currently available on market that adopts this architecture. It is reported that Qualcomm Snapdragon 845 uses Cortex-A75 design and will be available in mobile devices in early 2018).
### Libraries
- [Eigen](https://github.com/RLovelett/eigen) >= 3.3 supports float16 calculation on both GPU and CPU using the `Eigen::half` class. It is mostly useful for Nvidia GPUs because of the overloaded arithmetic operators using cuda intrinsics. It falls back to using software emulation on CPU for calculation and there is no special treatment to ARM processors.
The float16 class holds a 16-bit `uint16_t` data internally.
```
struct float16 {
uint16_t x;
};
```
float16 supports the following features:
- constructors / assignment operators that take input from primitive data types including bool, integers of various length, float, and double.
- constructors / assignment operators that take input from `__half` on cuda, `float16_t` on ARM, and `Eigen::half` on Eigen.
- conversion operators to primitive data types and half precision data types on cuda, ARM and Eigen.
- overloaded arithmetic operators for cuda, arm, and non-arm cpu, respectively. These operators will take advantage of the cuda and ARM intrinsics on the corresponding hardware.
To support the above features, two fundamental conversion functions are provided:
```
float16 float_to_half_rn(float f); // convert to half precision in round-to-nearest-even mode
float half_to_float(float16 h);
```
which provides one-to-one conversion between float32 and float16. These twos functions will do different conversion routines based on the current hardware. CUDA/ARM instrinsics will be used when the corresonding hardware is available. If the hardware or compiler level does not support float32 to float16 conversion, software emulation will be performed to do the conversion.
## To do
After float16 class is available, some of the future items are below:
- Update pybind/tensor_py.h to bind c++ float16 with numpy float16.
- Modify `IndicateDataType()` method in `framework/operator.h` to make it compatible with float16.
- Create a type-casting operator that can convert the data type in tensor between float16 and other types.
<liclass="toctree-l2"><aclass="reference internal"href="../getstarted/build_and_install/index_en.html">Install and Build</a><ul>
<liclass="toctree-l3"><aclass="reference internal"href="../getstarted/build_and_install/docker_install_en.html">PaddlePaddle in Docker Containers</a></li>
<liclass="toctree-l3"><aclass="reference internal"href="../getstarted/build_and_install/build_from_source_en.html">Installing from Sources</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/usage/k8s/k8s_en.html">Paddle On Kubernetes</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/usage/k8s/k8s_aws_en.html">Distributed PaddlePaddle Training on AWS with Kubernetes</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/dev/build_en.html">Build PaddlePaddle from Source Code and Run Unit Test</a></li>
<liclass="toctree-l2"><aclass="reference internal"href="../howto/dev/new_layer_en.html">Write New Layers</a></li>
<spanid="design-doc-float16"></span><h1>Design Doc: float16<aclass="headerlink"href="#design-doc-float16"title="Permalink to this headline">¶</a></h1>
<divclass="section"id="why-float16">
<spanid="why-float16"></span><h2>Why float16<aclass="headerlink"href="#why-float16"title="Permalink to this headline">¶</a></h2>
<p>Half precision (float16) is a binary floating-point format that occupies 16 bits in memory. float16 is half the size of traditional 32-bit single precision format (float) and has lower precision and smaller range.</p>
<p>When high precision computation is not required, using float16 data type could potentially</p>
<ulclass="simple">
<li>reduce storage space, memory bandwidth, and power usages;</li>
<li>increase the chance of data fitting into a smaller cache of lower latency;</li>
<li>provide arithmetic speed up if supported by hardware.</li>
<spanid="survey-of-current-float16-support"></span><h2>Survey of current float16 support<aclass="headerlink"href="#survey-of-current-float16-support"title="Permalink to this headline">¶</a></h2>
<p>A brief survey of float16 support on different compilers, hardwares, and libraries can be found below. Interested readers can refer to <aclass="reference external"href="https://github.com/PaddlePaddle/Paddle/issues/4853">link1</a> and <aclass="reference external"href="https://github.com/Xreki/Xreki.github.io/blob/master/multi_data_types_in_dl_framework/ppt/float16_and_quantized_type.md">link2</a> for more info.</p>
<p>The goal of float16 is to serve as a key for the executor to find and run the correct version of compute method specialized for float16 in operator kernel. It should be compatible with various natively supported float16 implementations including <codeclass="docutils literal"><spanclass="pre">__half</span></code> for cuda, <codeclass="docutils literal"><spanclass="pre">float16_t</span></code> for ARM, and <codeclass="docutils literal"><spanclass="pre">Eigen::half</span></code> for Eigen to make writing customized float16 kernels easier.</p>
<divclass="section"id="compiler">
<spanid="compiler"></span><h3>Compiler<aclass="headerlink"href="#compiler"title="Permalink to this headline">¶</a></h3>
<ulclass="simple">
<li>nvcc supports <codeclass="docutils literal"><spanclass="pre">__half</span></code> data type after CUDA 7.5.</li>
<li><codeclass="docutils literal"><spanclass="pre">__fp16</span></code> or <codeclass="docutils literal"><spanclass="pre">float16_t</span></code> is supported as storage type for gcc >= 6.1 and clang >= 3.4.</li>
<li><codeclass="docutils literal"><spanclass="pre">__fp16</span></code> or <codeclass="docutils literal"><spanclass="pre">float16_t</span></code> is supported as arithmetic type for gcc >= 7.1 and clang >= 3.9.</li>
</ul>
</div>
<divclass="section"id="hardware">
<spanid="hardware"></span><h3>Hardware<aclass="headerlink"href="#hardware"title="Permalink to this headline">¶</a></h3>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">__half</span></code> is supported on GPU with compute capability >= 5.3.</li>
<li><codeclass="docutils literal"><spanclass="pre">__fp16</span></code> is supported as storage type for ARMv7-A, ARMv8-A, and above.</li>
<li><codeclass="docutils literal"><spanclass="pre">__fp16</span></code> is supported as arithmetic type after ARMv8.2-A (currently, the only microarchitecture implementing ARMv8.2-A is ARM Cortex-A75, which is announced in May 2017. There seems to be no application processors currently available on market that adopts this architecture. It is reported that Qualcomm Snapdragon 845 uses Cortex-A75 design and will be available in mobile devices in early 2018).</li>
</ul>
</div>
<divclass="section"id="libraries">
<spanid="libraries"></span><h3>Libraries<aclass="headerlink"href="#libraries"title="Permalink to this headline">¶</a></h3>
<ulclass="simple">
<li><aclass="reference external"href="https://github.com/RLovelett/eigen">Eigen</a>>= 3.3 supports float16 calculation on both GPU and CPU using the <codeclass="docutils literal"><spanclass="pre">Eigen::half</span></code> class. It is mostly useful for Nvidia GPUs because of the overloaded arithmetic operators using cuda intrinsics. It falls back to using software emulation on CPU for calculation and there is no special treatment to ARM processors.</li>
<li>constructors / assignment operators that take input from primitive data types including bool, integers of various length, float, and double.</li>
<li>constructors / assignment operators that take input from <codeclass="docutils literal"><spanclass="pre">__half</span></code> on cuda, <codeclass="docutils literal"><spanclass="pre">float16_t</span></code> on ARM, and <codeclass="docutils literal"><spanclass="pre">Eigen::half</span></code> on Eigen.</li>
<li>conversion operators to primitive data types and half precision data types on cuda, ARM and Eigen.</li>
<li>overloaded arithmetic operators for cuda, arm, and non-arm cpu, respectively. These operators will take advantage of the cuda and ARM intrinsics on the corresponding hardware.</li>
</ul>
<p>To support the above features, two fundamental conversion functions are provided:</p>
<p>which provides one-to-one conversion between float32 and float16. These twos functions will do different conversion routines based on the current hardware. CUDA/ARM instrinsics will be used when the corresonding hardware is available. If the hardware or compiler level does not support float32 to float16 conversion, software emulation will be performed to do the conversion.</p>
</div>
<divclass="section"id="to-do">
<spanid="to-do"></span><h2>To do<aclass="headerlink"href="#to-do"title="Permalink to this headline">¶</a></h2>
<p>After float16 class is available, some of the future items are below:</p>
<ulclass="simple">
<li>Update pybind/tensor_py.h to bind c++ float16 with numpy float16.</li>
<li>Modify <codeclass="docutils literal"><spanclass="pre">IndicateDataType()</span></code> method in <codeclass="docutils literal"><spanclass="pre">framework/operator.h</span></code> to make it compatible with float16.</li>
<li>Create a type-casting operator that can convert the data type in tensor between float16 and other types.</li>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.
Half precision (float16) is a binary floating-point format that occupies 16 bits in memory. float16 is half the size of traditional 32-bit single precision format (float) and has lower precision and smaller range.
When high precision computation is not required, using float16 data type could potentially
- reduce storage space, memory bandwidth, and power usages;
- increase the chance of data fitting into a smaller cache of lower latency;
- provide arithmetic speed up if supported by hardware.
## Survey of current float16 support
A brief survey of float16 support on different compilers, hardwares, and libraries can be found below. Interested readers can refer to [link1](https://github.com/PaddlePaddle/Paddle/issues/4853) and [link2](https://github.com/Xreki/Xreki.github.io/blob/master/multi_data_types_in_dl_framework/ppt/float16_and_quantized_type.md) for more info.
The goal of float16 is to serve as a key for the executor to find and run the correct version of compute method specialized for float16 in operator kernel. It should be compatible with various natively supported float16 implementations including `__half` for cuda, `float16_t` for ARM, and `Eigen::half` for Eigen to make writing customized float16 kernels easier.
### Compiler
- nvcc supports `__half` data type after CUDA 7.5.
- `__fp16` or `float16_t` is supported as storage type for gcc >= 6.1 and clang >= 3.4.
- `__fp16` or `float16_t` is supported as arithmetic type for gcc >= 7.1 and clang >= 3.9.
### Hardware
- `__half` is supported on GPU with compute capability >= 5.3.
- `__fp16` is supported as storage type for ARMv7-A, ARMv8-A, and above.
- `__fp16` is supported as arithmetic type after ARMv8.2-A (currently, the only microarchitecture implementing ARMv8.2-A is ARM Cortex-A75, which is announced in May 2017. There seems to be no application processors currently available on market that adopts this architecture. It is reported that Qualcomm Snapdragon 845 uses Cortex-A75 design and will be available in mobile devices in early 2018).
### Libraries
- [Eigen](https://github.com/RLovelett/eigen) >= 3.3 supports float16 calculation on both GPU and CPU using the `Eigen::half` class. It is mostly useful for Nvidia GPUs because of the overloaded arithmetic operators using cuda intrinsics. It falls back to using software emulation on CPU for calculation and there is no special treatment to ARM processors.
The float16 class holds a 16-bit `uint16_t` data internally.
```
struct float16 {
uint16_t x;
};
```
float16 supports the following features:
- constructors / assignment operators that take input from primitive data types including bool, integers of various length, float, and double.
- constructors / assignment operators that take input from `__half` on cuda, `float16_t` on ARM, and `Eigen::half` on Eigen.
- conversion operators to primitive data types and half precision data types on cuda, ARM and Eigen.
- overloaded arithmetic operators for cuda, arm, and non-arm cpu, respectively. These operators will take advantage of the cuda and ARM intrinsics on the corresponding hardware.
To support the above features, two fundamental conversion functions are provided:
```
float16 float_to_half_rn(float f); // convert to half precision in round-to-nearest-even mode
float half_to_float(float16 h);
```
which provides one-to-one conversion between float32 and float16. These twos functions will do different conversion routines based on the current hardware. CUDA/ARM instrinsics will be used when the corresonding hardware is available. If the hardware or compiler level does not support float32 to float16 conversion, software emulation will be performed to do the conversion.
## To do
After float16 class is available, some of the future items are below:
- Update pybind/tensor_py.h to bind c++ float16 with numpy float16.
- Modify `IndicateDataType()` method in `framework/operator.h` to make it compatible with float16.
- Create a type-casting operator that can convert the data type in tensor between float16 and other types.
<p>Half precision (float16) is a binary floating-point format that occupies 16 bits in memory. float16 is half the size of traditional 32-bit single precision format (float) and has lower precision and smaller range.</p>
<p>When high precision computation is not required, using float16 data type could potentially</p>
<ulclass="simple">
<li>reduce storage space, memory bandwidth, and power usages;</li>
<li>increase the chance of data fitting into a smaller cache of lower latency;</li>
<li>provide arithmetic speed up if supported by hardware.</li>
<spanid="survey-of-current-float16-support"></span><h2>Survey of current float16 support<aclass="headerlink"href="#survey-of-current-float16-support"title="永久链接至标题">¶</a></h2>
<p>A brief survey of float16 support on different compilers, hardwares, and libraries can be found below. Interested readers can refer to <aclass="reference external"href="https://github.com/PaddlePaddle/Paddle/issues/4853">link1</a> and <aclass="reference external"href="https://github.com/Xreki/Xreki.github.io/blob/master/multi_data_types_in_dl_framework/ppt/float16_and_quantized_type.md">link2</a> for more info.</p>
<p>The goal of float16 is to serve as a key for the executor to find and run the correct version of compute method specialized for float16 in operator kernel. It should be compatible with various natively supported float16 implementations including <codeclass="docutils literal"><spanclass="pre">__half</span></code> for cuda, <codeclass="docutils literal"><spanclass="pre">float16_t</span></code> for ARM, and <codeclass="docutils literal"><spanclass="pre">Eigen::half</span></code> for Eigen to make writing customized float16 kernels easier.</p>
<li>nvcc supports <codeclass="docutils literal"><spanclass="pre">__half</span></code> data type after CUDA 7.5.</li>
<li><codeclass="docutils literal"><spanclass="pre">__fp16</span></code> or <codeclass="docutils literal"><spanclass="pre">float16_t</span></code> is supported as storage type for gcc >= 6.1 and clang >= 3.4.</li>
<li><codeclass="docutils literal"><spanclass="pre">__fp16</span></code> or <codeclass="docutils literal"><spanclass="pre">float16_t</span></code> is supported as arithmetic type for gcc >= 7.1 and clang >= 3.9.</li>
<li><codeclass="docutils literal"><spanclass="pre">__half</span></code> is supported on GPU with compute capability >= 5.3.</li>
<li><codeclass="docutils literal"><spanclass="pre">__fp16</span></code> is supported as storage type for ARMv7-A, ARMv8-A, and above.</li>
<li><codeclass="docutils literal"><spanclass="pre">__fp16</span></code> is supported as arithmetic type after ARMv8.2-A (currently, the only microarchitecture implementing ARMv8.2-A is ARM Cortex-A75, which is announced in May 2017. There seems to be no application processors currently available on market that adopts this architecture. It is reported that Qualcomm Snapdragon 845 uses Cortex-A75 design and will be available in mobile devices in early 2018).</li>
<li><aclass="reference external"href="https://github.com/RLovelett/eigen">Eigen</a>>= 3.3 supports float16 calculation on both GPU and CPU using the <codeclass="docutils literal"><spanclass="pre">Eigen::half</span></code> class. It is mostly useful for Nvidia GPUs because of the overloaded arithmetic operators using cuda intrinsics. It falls back to using software emulation on CPU for calculation and there is no special treatment to ARM processors.</li>
<li>constructors / assignment operators that take input from primitive data types including bool, integers of various length, float, and double.</li>
<li>constructors / assignment operators that take input from <codeclass="docutils literal"><spanclass="pre">__half</span></code> on cuda, <codeclass="docutils literal"><spanclass="pre">float16_t</span></code> on ARM, and <codeclass="docutils literal"><spanclass="pre">Eigen::half</span></code> on Eigen.</li>
<li>conversion operators to primitive data types and half precision data types on cuda, ARM and Eigen.</li>
<li>overloaded arithmetic operators for cuda, arm, and non-arm cpu, respectively. These operators will take advantage of the cuda and ARM intrinsics on the corresponding hardware.</li>
</ul>
<p>To support the above features, two fundamental conversion functions are provided:</p>
<p>which provides one-to-one conversion between float32 and float16. These twos functions will do different conversion routines based on the current hardware. CUDA/ARM instrinsics will be used when the corresonding hardware is available. If the hardware or compiler level does not support float32 to float16 conversion, software emulation will be performed to do the conversion.</p>
<p>After float16 class is available, some of the future items are below:</p>
<ulclass="simple">
<li>Update pybind/tensor_py.h to bind c++ float16 with numpy float16.</li>
<li>Modify <codeclass="docutils literal"><spanclass="pre">IndicateDataType()</span></code> method in <codeclass="docutils literal"><spanclass="pre">framework/operator.h</span></code> to make it compatible with float16.</li>
<li>Create a type-casting operator that can convert the data type in tensor between float16 and other types.</li>
Built with <ahref="http://sphinx-doc.org/">Sphinx</a> using a <ahref="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <ahref="https://readthedocs.org">Read the Docs</a>.