Add float32 hardsigmoid tests (#14948)

This commit is contained in:
Yi-Hong Lyu 2023-03-12 10:56:29 -07:00 committed by GitHub
parent 930e009567
commit cce9e0eaad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 28 deletions

View file

@ -154,6 +154,7 @@ enum MLAS_ACTIVATION_KIND {
MlasLogisticActivation,
MlasClipActivation,
MlasHardSigmoidActivation,
MlasActivationKindCount,
};
struct MLAS_ACTIVATION {

View file

@ -509,5 +509,11 @@ Return Value:
MlasActivationKernel<MlasHardSigmoidActivation>(Activation, Buffer, Bias, M, N, ldc);
break;
}
case MlasActivationKindCount:
{
MLAS_THROW_EX(std::runtime_error, "bad mlas activation kind");
break;
}
}
}

View file

@ -17,14 +17,24 @@ Abstract:
#pragma once
#include <mlas.h>
#include <memory.h>
#include <algorithm>
#include <limits>
#include <cmath>
#include <type_traits>
#include <stdexcept>
#include <functional>
#include <limits>
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
#ifdef MLAS_NO_EXCEPTION
#if defined(__ANDROID__)
#include <android/log.h>
#else
#include <iostream>
#endif
#endif // MLAS_NO_EXCEPTION
#include "mlas.h"
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
@ -93,6 +103,38 @@ Abstract:
#define MLAS_UNREFERENCED_PARAMETER(parameter) ((void)(parameter))
#ifdef MLAS_NO_EXCEPTION
MLAS_FORCEINLINE
void
MlasPrintFinalMessage(const std::string& msg)
{
#if defined(__ANDROID__)
__android_log_print(ANDROID_LOG_ERROR, "mlas", "%s", msg.c_str());
#else
// TODO, consider changing the output of the error message from std::cerr to logging when the
// exceptions are disabled, since using std::cerr might increase binary size, and std::cerr
// output might not be easily accesible on some systems such as mobile
// TODO, see if we need to change the output of the error message from std::cerr to NSLog for
// iOS
std::cerr << msg << std::endl;
#endif
}
#define MLAS_THROW_EX(ex, what) \
do { \
std::string msg = #ex; \
msg.append(what); \
MlasPrintFinalMessage(msg); \
abort(); \
} while (false)
#else
#define MLAS_THROW_EX(ex, ...) throw ex(__VA_ARGS__)
#endif // MLAS_NO_EXCEPTION
//
// Select the threading model.
//

View file

@ -17,34 +17,34 @@ class MlasActivationTest : public MlasTestBase {
};
// N.B. The test data includes values at the edge of Tanh/Logistic boundaries.
// Identity, Relu, LeakyRelu, Tanh, Logistic, Clip,
static const AliasedValue TestData[20][6] = {
{ {0x00000001}, {0x00000001}, {0x00000001}, {0x00000000}, {0x3f000000}, {0x00000001}, }, // positive denormal
{ {0x80000001}, {0x00000000}, {0x80000000}, {0x80000000}, {0x3f000000}, {0x00000000}, }, // negative denormal
{ {0x7ff00002}, {0x7ff00002}, {0x7ff00002}, {0x7ff00002}, {0x7ff00002}, {0x7ff00002}, }, // positive NaN
{ {0xfff00002}, {0xfff00002}, {0xfff00002}, {0xfff00002}, {0xfff00002}, {0xfff00002}, }, // negative NaN
{ {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}, {0x3f000000}, {0x00000000}, }, // 0.0f
{ {0x80000000}, {0x80000000}, {0x80000000}, {0x80000000}, {0x3f000000}, {0x80000000}, }, // -0.0f
{ {0x3e800000}, {0x3e800000}, {0x3e800000}, {0x3e7acbf5}, {0x3f0feacc}, {0x3e800000}, }, // 0.25f
{ {0xbe800000}, {0x00000000}, {0xbd4ccccd}, {0xbe7acbf5}, {0x3ee02a67}, {0x00000000}, }, // -0.25f
{ {0x40800000}, {0x40800000}, {0x40800000}, {0x3f7fd40a}, {0x3f7b6541}, {0x40800000}, }, // 4.0f
{ {0xc0800000}, {0x00000000}, {0xbf4ccccd}, {0xbf7fd40a}, {0x3c9357e0}, {0x00000000}, }, // -4.0f
{ {0x41200000}, {0x41200000}, {0x41200000}, {0x3f800000}, {0x3f7ffd06}, {0x40c00000}, }, // 10.0f
{ {0xc1200000}, {0x00000000}, {0xc0000000}, {0xbf800000}, {0x383e6000}, {0x00000000}, }, // -10.0f
{ {0xc18866eb}, {0x00000000}, {0xc05a3e45}, {0xbf800000}, {0x33000000}, {0x00000000}, }, // -17.0502529144f
{ {0xc18869bb}, {0x00000000}, {0xc05a42c5}, {0xbf800000}, {0x33c00000}, {0x00000000}, }, // -17.0516262054f
{ {0xc18852a8}, {0x00000000}, {0xc05a1dda}, {0xbf800000}, {0x00000000}, {0x00000000}, }, // -17.0403594971f
{ {0xc18844aa}, {0x00000000}, {0xc05a0777}, {0xbf800000}, {0x00000000}, {0x00000000}, }, // -17.0335273743f
{ {0x418866eb}, {0x418866eb}, {0x418866eb}, {0x3f800000}, {0x3f800000}, {0x40c00000}, }, // +17.0502529144f
{ {0x418869bb}, {0x418869bb}, {0x418869bb}, {0x3f800000}, {0x3f7ffffe}, {0x40c00000}, }, // +17.0516262054f
{ {0x418852a8}, {0x418852a8}, {0x418852a8}, {0x3f800000}, {0x3f800000}, {0x40c00000}, }, // +17.0403594971f
{ {0x418844aa}, {0x418844aa}, {0x418844aa}, {0x3f800000}, {0x3f800000}, {0x40c00000}, }, // +17.0335273743f
// Identity, Relu, LeakyRelu, Tanh, Logistic, Clip, HardSigmoid
static const AliasedValue TestData[20][7] = {
{ {0x00000001}, {0x00000001}, {0x00000001}, {0x00000000}, {0x3f000000}, {0x00000001}, {0x3df5c28f}, }, // positive denormal
{ {0x80000001}, {0x00000000}, {0x80000000}, {0x80000000}, {0x3f000000}, {0x00000000}, {0x3df5c28f}, }, // negative denormal
{ {0x7ff00002}, {0x7ff00002}, {0x7ff00002}, {0x7ff00002}, {0x7ff00002}, {0x7ff00002}, {0x7ff00002}, }, // positive NaN
{ {0xfff00002}, {0xfff00002}, {0xfff00002}, {0xfff00002}, {0xfff00002}, {0xfff00002}, {0xfff00002}, }, // negative NaN
{ {0x00000000}, {0x00000000}, {0x00000000}, {0x00000000}, {0x3f000000}, {0x00000000}, {0x3df5c28f}, }, // 0.0f
{ {0x80000000}, {0x80000000}, {0x80000000}, {0x80000000}, {0x3f000000}, {0x80000000}, {0x3df5c28f}, }, // -0.0f
{ {0x3e800000}, {0x3e800000}, {0x3e800000}, {0x3e7acbf5}, {0x3f0feacc}, {0x3e800000}, {0x3e2e147b}, }, // 0.25f
{ {0xbe800000}, {0x00000000}, {0xbd4ccccd}, {0xbe7acbf5}, {0x3ee02a67}, {0x00000000}, {0x3d8f5c28}, }, // -0.25f
{ {0x40800000}, {0x40800000}, {0x40800000}, {0x3f7fd40a}, {0x3f7b6541}, {0x40800000}, {0x3f6b851f}, }, // 4.0f
{ {0xc0800000}, {0x00000000}, {0xbf4ccccd}, {0xbf7fd40a}, {0x3c9357e0}, {0x00000000}, {0x00000000}, }, // -4.0f
{ {0x41200000}, {0x41200000}, {0x41200000}, {0x3f800000}, {0x3f7ffd06}, {0x40c00000}, {0x3f800000}, }, // 10.0f
{ {0xc1200000}, {0x00000000}, {0xc0000000}, {0xbf800000}, {0x383e6000}, {0x00000000}, {0x00000000}, }, // -10.0f
{ {0xc18866eb}, {0x00000000}, {0xc05a3e45}, {0xbf800000}, {0x33000000}, {0x00000000}, {0x00000000}, }, // -17.0502529144f
{ {0xc18869bb}, {0x00000000}, {0xc05a42c5}, {0xbf800000}, {0x33c00000}, {0x00000000}, {0x00000000}, }, // -17.0516262054f
{ {0xc18852a8}, {0x00000000}, {0xc05a1dda}, {0xbf800000}, {0x00000000}, {0x00000000}, {0x00000000}, }, // -17.0403594971f
{ {0xc18844aa}, {0x00000000}, {0xc05a0777}, {0xbf800000}, {0x00000000}, {0x00000000}, {0x00000000}, }, // -17.0335273743f
{ {0x418866eb}, {0x418866eb}, {0x418866eb}, {0x3f800000}, {0x3f800000}, {0x40c00000}, {0x3f800000}, }, // +17.0502529144f
{ {0x418869bb}, {0x418869bb}, {0x418869bb}, {0x3f800000}, {0x3f7ffffe}, {0x40c00000}, {0x3f800000}, }, // +17.0516262054f
{ {0x418852a8}, {0x418852a8}, {0x418852a8}, {0x3f800000}, {0x3f800000}, {0x40c00000}, {0x3f800000}, }, // +17.0403594971f
{ {0x418844aa}, {0x418844aa}, {0x418844aa}, {0x3f800000}, {0x3f800000}, {0x40c00000}, {0x3f800000}, }, // +17.0335273743f
};
MLAS_ACTIVATION Activation;
AliasedValue Buffer[_countof(TestData)];
for (unsigned kind = 0; kind < unsigned(MlasClipActivation); kind++) {
for (unsigned kind = 0; kind < unsigned(MlasActivationKindCount); kind++) {
Activation.ActivationKind = MLAS_ACTIVATION_KIND(kind);
if (Activation.ActivationKind == MlasLeakyReluActivation) {
@ -52,6 +52,9 @@ class MlasActivationTest : public MlasTestBase {
} else if (Activation.ActivationKind == MlasClipActivation) {
Activation.Parameters.Clip.minimum = 0.0f;
Activation.Parameters.Clip.maximum = 6.0f;
} else if (Activation.ActivationKind == MlasHardSigmoidActivation) {
Activation.Parameters.HardSigmoid.alpha = 0.2f;
Activation.Parameters.HardSigmoid.beta = 0.12f;
}
//