libdpf/include/grotto/gadgets/activations/hardtanh.hpp

67 lines
2.1 KiB
C++
Raw Normal View History

/// @file grotto/gadgets/activations/hardtanh.hpp
/// @author Ryan Henry <ryan.henry@ucalgary.ca>
/// @brief Cleartext reference for `hardtanh`.
/// @copyright Copyright (c) 2019-2023 Ryan Henry and others
/// @license Released under a GNU General Public v2.0 (GPLv2) license;
/// see [LICENSE.md](@ref license) for details.
#ifndef LIBDPF_INCLUDE_GROTTO_GADGETS_ACTIVATIONS_HARDTANH_HPP__
#define LIBDPF_INCLUDE_GROTTO_GADGETS_ACTIVATIONS_HARDTANH_HPP__
#include <cmath>
#include <array>
#include <limits>
#include "grotto/gadget_hints.hpp"
namespace grotto
{
namespace gadgets
{
HEDLEY_DIAGNOSTIC_PUSH
HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
/// @brief Cleartext `hardtanh`.
/// @see grotto::make_hardtanh_lut
/// @note Straight-line cleartext. Fixed-point evaluation is the symbol in `@see` when that symbol is a LUT or an `eval_*` function.
/// \complexity One evaluation of `operator()` as written in this header. There is no domain scan and no loop. `Θ(1)` time and extra space relative to that expression.
struct HEDLEY_DEPRECATED_FOR(2026, grotto::make_hardtanh_lut) hardtanh
{
/// @brief Evaluate the cleartext map.
/// @tparam T argument and result type
/// @param x the input
/// @return the cleartext image of `x`
/// \complexity The body is straight-line. `Θ(1)` time and extra space.
template <typename T>
T operator()(T x)
{
if (x <= -1) return -1;
if (x >= 1) return 1;
return x;
}
};
template <>
struct gadget_hints<hardtanh>
{
static constexpr double min = std::numeric_limits<double>::min();
static constexpr double max = std::numeric_limits<double>::max();
static constexpr double poles[] = { };
static constexpr double interesting_points[] = { -1, 1 };
static constexpr unsigned degree = 1;
static constexpr bool has_canonical_representation = true;
static constexpr double canonical_bounds[] = { -1, 1 };
static constexpr std::array<double, degree+1> canonical_polys[] = { {-1}, {0,1}, {1} };
};
HEDLEY_DIAGNOSTIC_POP
} // namespace gadgets
} // namespace grotto
#endif // LIBDPF_INCLUDE_GROTTO_GADGETS_ACTIVATIONS_HARDTANH_HPP__