Initial FP16 support

This commit is contained in:
Ryan Curtin
2025-07-07 04:00:20 +00:00
committed by conradsnicta
parent 252d43ae35
commit f07af5a111
99 changed files with 2589 additions and 646 deletions
+126 -1
View File
@@ -562,9 +562,10 @@ Conrad Sanderson and Ryan Curtin.
<tr><td><a href="#output_streams">output&nbsp;streams</a></td><td>&nbsp;</td><td>streams for printing warnings and errors</td></tr>
<tr><td><a href="#uword">uword&nbsp;/&nbsp;sword</a></td><td>&nbsp;</td><td>shorthand for unsigned and signed integers</td></tr>
<tr><td><a href="#cx_double">cx_double&nbsp;/&nbsp;cx_float</a></td><td>&nbsp;</td><td>shorthand for std::complex&lt;double&gt; and std::complex&lt;float&gt;</td></tr>
<tr style="background-color: #F5F5F5;"><td><a href="#lowprec">fp16</a></td><td>&nbsp;</td><td>shorthand for low-precision types, if supported</td></tr>
<tr style="background-color: #F5F5F5;"><td><a href="#syntax">Matlab/Armadillo&nbsp;syntax&nbsp;differences</a></td><td>&nbsp;</td><td>examples of Matlab syntax and conceptually corresponding Armadillo syntax</td></tr>
<tr style="background-color: #F5F5F5;"><td><a href="#example_prog">example&nbsp;program</a></td><td>&nbsp;</td><td>short example program</td></tr>
<tr style="background-color: #F5F5F5;"><td><a href="#config_hpp">config.hpp</a></td><td>&nbsp;</td><td>configuration options</td></tr>
<tr><td><a href="#config_hpp">config.hpp</a></td><td>&nbsp;</td><td>configuration options</td></tr>
<tr><td><a href="#changelog">API&nbsp;additions</a></td><td>&nbsp;</td><td>API stability and list of API additions</td></tr>
<!--<tr><td><a href="#log_add">log_add</a></td><td>&nbsp;</td><td>TODO</td></tr>-->
<!--<tr><td><a href="#catching_exceptions">catching exceptions</a></td><td>&nbsp;</td><td>TODO</td></tr>-->
@@ -604,6 +605,10 @@ The root matrix class is <b>Mat&lt;</b><i>type</i><b>&gt;</b>, where <i>type</i>
</li>
</ul>
</li>
<li>
When compiling with C++23 support, <i>type</i> can also be the low-precision type <i>std::float16_t</i>, if hardware support is detected;
see the <a href="#lowprec">low-precision support</a> section for more details
</li>
<br>
<li>
For convenience the following typedefs have been defined:
@@ -698,6 +703,17 @@ For convenience the following typedefs have been defined:
<code>Mat&lt;<a href="#uword">sword</a>&gt;</code>
</td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;">
<code>fp16_mat</code>
</td>
<td style="vertical-align: top;">
&nbsp;=&nbsp;
</td>
<td style="vertical-align: top;">
<code>Mat&lt;<a href="#lowprec">fp16</a>&gt;</code><font style="font-size: 75%"><i> (only if supported by compiler and hardware)</i></font></font>
</td>
</tr>
</tbody>
</table>
</ul>
@@ -1060,6 +1076,23 @@ For convenience the following typedefs have been defined:
<code>Col&lt;<a href="#uword">sword</a>&gt;</code>
</td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;">
<code>fp16_vec</code>
</td>
<td style="vertical-align: top;">
&nbsp;=&nbsp;
</td>
<td style="vertical-align: top; text-align: right;">
<code>fp16_colvec</code>
</td>
<td style="vertical-align: top;">
&nbsp;=&nbsp;
</td>
<td style="vertical-align: top;">
<code>Col&lt;<a href="#lowprec">fp16</a>&gt;</code>
</td>
</tr>
</tbody>
</table>
</ul>
@@ -1311,6 +1344,17 @@ For convenience the following typedefs have been defined:
<code>Row&lt;<a href="#uword">sword</a>&gt;</code>
</td>
</tr>
<tr>
<td style="vertical-align: top; text-align: right;">
<code>fp16_rowvec</code>
</td>
<td style="vertical-align: top;">
&nbsp;=&nbsp;
</td>
<td style="vertical-align: top;">
<code>Row&lt;<a href="#lowprec">fp16</a>&gt;</code><font style="font-size: 75%"><i> (only if supported by compiler and hardware)</i></font>
</td>
</tr>
</tbody>
</table>
</ul>
@@ -18394,6 +18438,86 @@ cx_double val = X(2,3);
<br>
</ul>
<div class="pagebreak"></div><div class="noprint"><hr class="greyline"><br></div>
<a name="lowprec"></a>
<b>fp16</b>
<ul>
<li>
Convenience short form (typedef) for low-precision element types; <b>compiler and hardware support is required</b>
</li>
<br>
<br>
<li>
The low-precision <i>fp16</i> type can be used with all Armadillo functions that do not require LAPACK (e.g., almost any operation that is not a decomposition like <code>svd()</code> or similar)
</li>
<br>
<li>
To enable automatic support for low-precision elements, <b>both</b> of the following are necessary:
<br>
<ul>
<li>compiler C++23 support (e.g. compile with <code>-std=c++23</code> or similar)</li>
<br>
<li>hardware support for native FP16 operation (not emulated):
<ul>
<li>ARM devices require the <code>+fp16</code> extension (typically found on ARMv8.2-A+ devices)</li>
<li>x86_64 devices require the AVX512-FP16 extension</li>
</ul>
</li>
<br>
<li>
If no compiler or hardware support is detected, <i>fp16</i> will <b>not</b> be available
</li>
<br>
<li>
The C++ low-precision types <code>std::float16_t</code> is preferentially used to define the <i>fp16</i> type,
but in certain situations other underlying intrinsic types such as <code>_Float16</code>, <code>__fp16</code>, or similar will be used
</li>
<br>
<li>
<b>NOTE:</b>
Low-precision types can have numerical stability issues,
and are not suitable for all applications:
<i>fp16</i> only has a range from roughly -65536 to 65536!
</li>
<br>
<li>
The <code>ARMA_FORCE_USE_FP16</code> macro can be defined before including Armadillo
to force emulated support even when native hardware support is not detected
<ul>
<li>Emulated FP16 support can be <b>very slow!</b></li>
<li>Manually forcing support is only recommended when Armadillo's auto-detection is failing;</li>
<li>in which case, please send an email to the developers to improve the auto-detection!</li>
</ul>
</li>
<br>
<li>
Example:
<ul>
<pre>
fp16_mat X(5, 5, fill::randu);
X(1,2) = fp16(1.5);
fp16 val = X(2,3);
</pre>
</ul>
</li>
<li>See also:
<ul>
<li><a href="https://en.wikipedia.org/wiki/Half-precision_floating-point_format">half-precision floating point format on Wikipedia</a></li>
<li><a href="https://gcc.gnu.org/onlinedocs/gcc/Half-Precision.html">gcc half-precision documentation</a></li>
<li><a href="https://clang.llvm.org/docs/LanguageExtensions.html#half-precision-floating-point">clang half-precision documentation</a></li>
<li><a href="https://en.cppreference.com/w/cpp/header/stdfloat.html">std::float16_t reference</a></li>
<li><a href="https://cplusplus.com/doc/tutorial/other_data_types/">explanation of <i>typedef</i></a></li>
<li><a href="#Mat">fp16_mat</a> matrix type
<li><a href="#Col">fp16_vec</a> vector type
</ul>
</li>
<br>
</ul>
<div class="pagebreak"></div><div class="noprint"><hr class="greyline"><br></div>
<a name="syntax"></a>
<b>Examples of Matlab/Octave syntax and conceptually corresponding Armadillo syntax</b>
@@ -20287,6 +20411,7 @@ List of additions and changes for each version:
<li>Version 15.0:
<ul>
<li>(under development)</li>
<li>added support for <a href="#lowprec">low-precision fp16 type</a> when native hardware support is detected</li>
<li>...</li>
</ul>
</li>