// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // Boundary + random-fuzz tests for the RISC-V RVV activation kernels // (Erf / Tanh / Logistic / Exp / SiLU / GELU). These kernels diverge from // the scalar reference path by (a) replacing libm-style exp with a packed // 2^n + minimax polynomial approximation, (b) clamping inputs before the // exponent-bit reconstruction, and (c) inserting explicit `(±inf) * 0` // guards on SiLU/GELU. The existing test_activation.cpp exercises the // MLAS_ACTIVATION_KIND surface (Identity/Relu/LeakyRelu/Tanh/Logistic/ // Clip/HardSigmoid) but not the MlasComputeUnary* entry points — these // tests fill that gap and pin the kernels' contract under NaN, ±Inf, // large negatives, and EXP_RECON_MIN/MAX boundary inputs. #include "test_util.h" #include #include #include class MlasActivationRvvBoundaryTest : public MlasTestBase { public: static const char* GetTestSuiteName() { static const std::string suite_name("ActivationRvvBoundary"); return suite_name.c_str(); } void ExecuteShort(void) override { TestBoundaryNaN(); TestBoundaryPositiveInf(); TestBoundaryNegativeInf(); TestBoundarySiluGeluLargeNegative(); TestBoundaryExpReconLimits(); TestRandomVsScalar(); } private: // ─── Boundary cases ───────────────────────────────────────────────────── void TestBoundaryNaN() { constexpr size_t N = 8; const float qnan = std::nanf(""); for (auto& kernel : kAllKernels) { float in[N], out[N]; for (size_t i = 0; i < N; ++i) in[i] = qnan; std::memcpy(out, in, sizeof(out)); kernel.fn(out, out, N); for (size_t i = 0; i < N; ++i) { EXPECT_TRUE(std::isnan(out[i])) << kernel.name << " did not propagate NaN at i=" << i; } } } void TestBoundaryPositiveInf() { constexpr size_t N = 4; const float pinf = std::numeric_limits::infinity(); struct Expect { const char* name; void(MLASCALL* fn)(const float*, float*, size_t); float expected; bool allow_nan; } cases[] = { {"Erf+Inf", MlasComputeErf, 1.0f, false}, {"Tanh+Inf", MlasComputeTanh, 1.0f, false}, {"Logistic+Inf", MlasComputeLogistic, 1.0f, false}, // exp(+inf) = +inf; some impls saturate to FLT_MAX, accept either. {"Exp+Inf", MlasComputeExp, pinf, true}, // SiLU(+inf) = +inf * sigmoid(+inf) = +inf * 1 = +inf {"Silu+Inf", MlasComputeSilu, pinf, true}, }; for (auto& c : cases) { float in[N], out[N]; for (size_t i = 0; i < N; ++i) in[i] = pinf; c.fn(in, out, N); for (size_t i = 0; i < N; ++i) { const bool ok = out[i] == c.expected || (c.allow_nan && std::isnan(out[i])) || (c.allow_nan && out[i] >= std::numeric_limits::max() * 0.5f); EXPECT_TRUE(ok) << c.name << " at i=" << i << " got " << out[i] << " expected " << c.expected; } } // GeluErf(+inf) → +inf (or saturated to FLT_MAX) { float in[N], out[N]; for (size_t i = 0; i < N; ++i) in[i] = pinf; MlasComputeGeluErf(in, out, N); for (size_t i = 0; i < N; ++i) { EXPECT_TRUE(out[i] == pinf || out[i] >= std::numeric_limits::max() * 0.5f) << "GeluErf+Inf at i=" << i << " got " << out[i]; } } } void TestBoundaryNegativeInf() { constexpr size_t N = 4; const float ninf = -std::numeric_limits::infinity(); struct Expect { const char* name; void(MLASCALL* fn)(const float*, float*, size_t); float expected; } cases[] = { {"Erf-Inf", MlasComputeErf, -1.0f}, {"Tanh-Inf", MlasComputeTanh, -1.0f}, {"Logistic-Inf", MlasComputeLogistic, 0.0f}, // exp(-inf) = 0 (after clamp inside exp_f32m4) {"Exp-Inf", MlasComputeExp, 0.0f}, }; for (auto& c : cases) { float in[N], out[N]; for (size_t i = 0; i < N; ++i) in[i] = ninf; c.fn(in, out, N); for (size_t i = 0; i < N; ++i) { EXPECT_EQ(out[i], c.expected) << c.name << " at i=" << i << " got " << out[i]; } } } void TestBoundarySiluGeluLargeNegative() { // SiLU(x) = x * sigmoid(x). For very negative x, sigmoid(x) → 0, // so SiLU(x) → 0 (well-defined). The RVV kernel adds a LOGISTIC_CLAMP // and an `(±inf) * 0 → NaN` guard so naive (-inf) * 0 does not leak. // Same idea for GELU(x) ≈ 0 for very negative x via the SMALLEST_NORMAL_F32 // floor. constexpr size_t N = 8; const float large_neg[N] = {-1000.0f, -500.0f, -200.0f, -100.0f, -88.5f, -90.0f, -120.0f, -50.0f}; float out_silu[N], out_gelu[N]; MlasComputeSilu(large_neg, out_silu, N); MlasComputeGeluErf(large_neg, out_gelu, N); for (size_t i = 0; i < N; ++i) { EXPECT_FALSE(std::isnan(out_silu[i])) << "SiLU(" << large_neg[i] << ") produced NaN — inf*0 guard failed"; EXPECT_LE(std::fabs(out_silu[i]), 1e-3f) << "SiLU(" << large_neg[i] << ") should be ~0, got " << out_silu[i]; EXPECT_FALSE(std::isnan(out_gelu[i])) << "GeluErf(" << large_neg[i] << ") produced NaN"; EXPECT_LE(std::fabs(out_gelu[i]), 1e-3f) << "GeluErf(" << large_neg[i] << ") should be ~0, got " << out_gelu[i]; } } void TestBoundaryExpReconLimits() { // exp_f32m4 uses 2^n exponent-bit reconstruction with an internal clamp. // Inputs at the edges of the recon range must produce finite, monotone // results — not Inf, NaN, or sign flips. constexpr size_t N = 8; const float boundary[N] = {-88.5f, -88.0f, -87.5f, -1.0f, 1.0f, 87.0f, 87.5f, 88.0f}; float out[N]; MlasComputeExp(boundary, out, N); float prev = -1.0f; for (size_t i = 0; i < N; ++i) { EXPECT_TRUE(std::isfinite(out[i])) << "Exp boundary[" << i << "]=" << boundary[i] << " produced non-finite " << out[i]; EXPECT_GE(out[i], 0.0f) << "Exp should be non-negative at boundary[" << i << "]"; EXPECT_GE(out[i], prev) << "Exp monotonicity broken between boundary[" << (i - 1) << "]=" << (i > 0 ? boundary[i - 1] : 0.0f) << " and boundary[" << i << "]=" << boundary[i]; prev = out[i]; } } // ─── Random fuzz against scalar reference ─────────────────────────────── void TestRandomVsScalar() { // 1000 random inputs in [-50, 50] — a realistic production range for // activation inputs (Transformer/CNN intermediate features) — each // compared against the scalar libm reference. RVV kernels approximate // with a 6th-order minimax polynomial + range reduction, so we accept // up to ~32 ULP for transcendentals, matching what AVX2 specializations // tolerate in the existing test infrastructure. constexpr size_t N = 1000; std::mt19937 gen(0xC0FFEE); std::uniform_real_distribution dist(-50.0f, 50.0f); std::vector in(N); for (size_t i = 0; i < N; ++i) in[i] = dist(gen); auto CheckRel = [&in](const char* name, const std::vector& fast, const std::vector& ref, float abs_tol, float rel_tol) { ASSERT_EQ(fast.size(), ref.size()) << name; for (size_t i = 0; i < fast.size(); ++i) { const float a = fast[i]; const float b = ref[i]; if (!std::isfinite(b)) continue; // skip overflow points const float diff = std::fabs(a - b); const float scale = std::max(std::fabs(b), 1.0f); EXPECT_LE(diff, abs_tol + rel_tol * scale) << name << " at i=" << i << " in=" << in[i] << " fast=" << a << " ref=" << b << " diff=" << diff; } }; std::vector out(N), ref(N); // Erf MlasComputeErf(in.data(), out.data(), N); for (size_t i = 0; i < N; ++i) ref[i] = std::erf(in[i]); CheckRel("Erf", out, ref, 5e-5f, 1e-5f); // Tanh MlasComputeTanh(in.data(), out.data(), N); for (size_t i = 0; i < N; ++i) ref[i] = std::tanh(in[i]); CheckRel("Tanh", out, ref, 5e-5f, 1e-5f); // Logistic (sigmoid) MlasComputeLogistic(in.data(), out.data(), N); for (size_t i = 0; i < N; ++i) ref[i] = 1.0f / (1.0f + std::exp(-in[i])); CheckRel("Logistic", out, ref, 5e-5f, 1e-5f); // Exp — confine to non-overflowing range { std::uniform_real_distribution exp_dist(-30.0f, 30.0f); std::vector exp_in(N), exp_out(N), exp_ref(N); for (size_t i = 0; i < N; ++i) exp_in[i] = exp_dist(gen); MlasComputeExp(exp_in.data(), exp_out.data(), N); for (size_t i = 0; i < N; ++i) exp_ref[i] = std::exp(exp_in[i]); // Exp grows fast: use a relative-only tolerance for (size_t i = 0; i < N; ++i) { if (!std::isfinite(exp_ref[i])) continue; const float rel = std::fabs(exp_out[i] - exp_ref[i]) / std::max(std::fabs(exp_ref[i]), 1e-12f); EXPECT_LE(rel, 5e-5f) << "Exp rel err at i=" << i << " in=" << exp_in[i] << " fast=" << exp_out[i] << " ref=" << exp_ref[i]; } } // SiLU(x) = x * sigmoid(x) MlasComputeSilu(in.data(), out.data(), N); for (size_t i = 0; i < N; ++i) { ref[i] = in[i] / (1.0f + std::exp(-in[i])); } CheckRel("Silu", out, ref, 1e-4f, 1e-5f); // GeluErf(x) = 0.5 * x * (1 + erf(x / sqrt(2))) MlasComputeGeluErf(in.data(), out.data(), N); for (size_t i = 0; i < N; ++i) { ref[i] = 0.5f * in[i] * (1.0f + std::erf(in[i] * 0.7071067811865476f)); } CheckRel("GeluErf", out, ref, 1e-4f, 1e-5f); } struct KernelEntry { const char* name; void(MLASCALL* fn)(const float*, float*, size_t); }; static constexpr KernelEntry kAllKernels[] = { {"Erf", MlasComputeErf}, {"Tanh", MlasComputeTanh}, {"Logistic", MlasComputeLogistic}, {"Exp", MlasComputeExp}, {"Silu", MlasComputeSilu}, {"GeluErf", MlasComputeGeluErf}, }; }; constexpr MlasActivationRvvBoundaryTest::KernelEntry MlasActivationRvvBoundaryTest::kAllKernels[]; static UNUSED_VARIABLE bool added_rvv_boundary_to_main = AddTestRegister( [](bool is_short_execute) { return is_short_execute ? MlasDirectShortExecuteTests::RegisterShortExecute() : 0; });