| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef THIRD_PARTY_GEMMA_CPP_COMPRESSION_TEST_UTIL_H_ |
| #define THIRD_PARTY_GEMMA_CPP_COMPRESSION_TEST_UTIL_H_ |
|
|
| #include <stddef.h> |
| #include <stdint.h> |
|
|
| #include <cmath> |
|
|
| #include "hwy/base.h" |
|
|
| |
| #include "compression/distortion.h" |
| #include "hwy/stats.h" |
| #include "hwy/tests/test_util.h" |
| |
|
|
| namespace gcpp { |
|
|
| |
| |
| |
| HWY_INLINE double RandomGaussian(hwy::RandomState& rng) { |
| uint64_t sum = 0; |
| constexpr int kReps = 40; |
| for (int rep = 0; rep < kReps; ++rep) { |
| sum += hwy::Random32(&rng) & 0xFFFFF; |
| } |
| const double sum_f = |
| static_cast<double>(sum) / static_cast<double>(0xFFFFF * kReps); |
| HWY_ASSERT(0.0 <= sum_f && sum_f <= 1.0); |
| const double plus_minus_1 = 2.0 * sum_f - 1.0; |
| HWY_ASSERT(-1.0 <= plus_minus_1 && plus_minus_1 <= 1.0); |
| |
| return plus_minus_1 * std::sqrt(kReps / 3.0); |
| }; |
|
|
| |
| template <typename T> |
| static inline bool IsInside(T expected_min, T expected_max, T val) { |
| return expected_min <= val && val <= expected_max; |
| } |
|
|
| template <typename T> |
| static inline bool IsNear(T expected, T val, T epsilon = T{1E-6}) { |
| return IsInside(expected - epsilon, expected + epsilon, val); |
| } |
|
|
| HWY_INLINE void VerifyGaussian(hwy::Stats& stats) { |
| |
| HWY_ASSERT(IsNear(-1.0f, stats.Min(), 0.10f)); |
| HWY_ASSERT(IsNear(+1.0f, stats.Max(), 0.10f)); |
| HWY_ASSERT(IsInside(-2E-3, 2E-3, stats.Mean())); |
| HWY_ASSERT(IsInside(-0.15, 0.15, stats.Skewness())); |
| |
| HWY_ASSERT(IsInside(0.30, 0.35, stats.StandardDeviation())); |
| HWY_ASSERT(IsNear(3.0, stats.Kurtosis(), 0.3)); |
| } |
|
|
| } |
|
|
| #endif |
|
|