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
+87 -7
View File
@@ -18,11 +18,12 @@
#include <armadillo>
#include "catch.hpp"
#include "utils.hpp"
using namespace arma;
TEST_CASE("fn_dot_1")
TEST_CASE("fn_dot_1", "[dot]")
{
mat A =
"\
@@ -51,7 +52,7 @@ TEST_CASE("fn_dot_1")
TEST_CASE("fn_dot_2")
TEST_CASE("fn_dot_2", "[dot]")
{
mat A =
"\
@@ -77,7 +78,7 @@ TEST_CASE("fn_dot_2")
TEST_CASE("fn_dot_sp_mat_mat")
TEST_CASE("fn_dot_sp_mat_mat", "[dot]")
{
// Make matrices.
SpMat<double> a("3.0 0.0 0.0; 1.0 2.0 2.0; 0.0 0.0 1.0");
@@ -89,7 +90,7 @@ TEST_CASE("fn_dot_sp_mat_mat")
TEST_CASE("fn_dot_sp_col_col")
TEST_CASE("fn_dot_sp_col_col", "[dot]")
{
SpCol<unsigned int> a("3; 4; 0; 0; 0; 2; 0; 0");
Col<unsigned int> b("1 6 1 2 3 7 1 2");
@@ -100,7 +101,7 @@ TEST_CASE("fn_dot_sp_col_col")
TEST_CASE("fn_dot_sp_mat_sp_mat")
TEST_CASE("fn_dot_sp_mat_sp_mat", "[dot]")
{
SpMat<double> a("3.0 0.0 0.0; 1.0 2.0 2.0; 0.0 0.0 1.0");
SpMat<double> b("3.0 0.0 0.0; 1.0 2.0 2.0; 0.0 0.0 1.0");
@@ -111,7 +112,7 @@ TEST_CASE("fn_dot_sp_mat_sp_mat")
TEST_CASE("fn_dot_sp_col_sp_col")
TEST_CASE("fn_dot_sp_col_sp_col", "[dot]")
{
SpCol<unsigned int> a("3; 4; 0; 0; 0; 2; 0; 0");
SpCol<unsigned int> b("0; 8; 0; 1; 1; 0; 0; 0");
@@ -122,4 +123,83 @@ TEST_CASE("fn_dot_sp_col_sp_col")
// TODO: norm_dot
TEMPLATE_TEST_CASE("fn_dot_fp_randu", "[dot]", TEST_FLOAT_TYPES)
{
typedef TestType eT;
Col<eT> x1 = randu<Col<eT>>(100);
Col<eT> x2 = randu<Col<eT>>(100);
vec x1_ref = conv_to<vec>::from(x1);
vec x2_ref = conv_to<vec>::from(x2);
eT d = dot(x1, x2);
double d_ref = dot(x1_ref, x2_ref);
constexpr eT eps = is_blas_real<eT>::value ? eT(0.001) : eT(0.1);
REQUIRE( double(d) == Approx(d_ref).epsilon(eps) );
}
//TEMPLATE_TEST_CASE("fn_sp_dot_fp_randu", "[dot]", TEST_FLOAT_TYPES)
// {
// typedef TestType eT;
//
// SpCol<eT> x1, x2;
// x1.sprandu(1000, 1, 0.3);
// x2.sprandu(1000, 1, 0.3);
//
// sp_mat x1_ref = conv_to<sp_mat>::from(x1);
// sp_mat x2_ref = conv_to<sp_mat>::from(x2);
//
// eT d = dot(x1, x2);
// double d_ref = dot(x1_ref, x2_ref);
//
// constexpr eT eps = is_blas_real<eT>::value ? eT(0.001) : eT(0.1);
//
// REQUIRE( double(d) == Approx(d_ref).epsilon(eps) );
// }
TEMPLATE_TEST_CASE("fn_cdot_fp_randu", "[dot]", TEST_CX_FLOAT_TYPES)
{
typedef TestType eT;
Col<eT> x1 = randu<Col<eT>>(100);
Col<eT> x2 = randu<Col<eT>>(100);
cx_vec x1_ref = conv_to<cx_vec>::from(x1);
cx_vec x2_ref = conv_to<cx_vec>::from(x2);
eT d = cdot(x1, x2);
std::complex<double> d_ref = cdot(x1_ref, x2_ref);
typedef typename get_pod_type<eT>::result epsT;
constexpr epsT eps = is_blas_real<eT>::value ? epsT(0.001) : epsT(0.1);
REQUIRE( double(std::real(d)) == Approx(std::real(d_ref)).epsilon(eps) );
REQUIRE( double(std::imag(d)) == Approx(std::imag(d_ref)).epsilon(eps) );
}
TEMPLATE_TEST_CASE("fn_norm_dot_fp_randu", "[dot]", TEST_FLOAT_TYPES)
{
typedef TestType eT;
Col<eT> x1 = randu<Col<eT>>(10);
Col<eT> x2 = randu<Col<eT>>(10);
const eT d_unnorm = dot(x1, x2);
const eT norm1 = norm(x1);
const eT norm2 = norm(x2);
const eT d_norm = norm_dot(x1, x2);
constexpr eT eps = is_blas_real<eT>::value ? eT(0.001) : eT(0.1);
REQUIRE( d_norm == Approx(d_unnorm / (norm1 * norm2)).epsilon(eps) );
}