forked from pisa-engine/pisa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_quantizer.cpp
More file actions
45 lines (38 loc) · 1.26 KB
/
Copy pathlinear_quantizer.cpp
File metadata and controls
45 lines (38 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cassert>
#include <cmath>
#include <stdexcept>
#include "fmt/core.h"
#include "linear_quantizer.hpp"
namespace pisa {
[[nodiscard]] auto all_ones(std::uint32_t bits) -> std::uint32_t {
if (bits > 32 or bits < 2) {
throw std::runtime_error(
fmt::format(
"Linear quantizer must take a number of bits between 2 and 32 but {} passed", bits
)
);
}
auto half = std::uint32_t(1) << (bits - 1);
return half - 1 + half;
}
LinearQuantizer::LinearQuantizer(float max, std::uint8_t bits)
: m_range(all_ones(bits)), m_max(max) {
if (max <= 0.0) {
throw std::runtime_error(
fmt::format("Max score for linear quantizer must be positive but {} passed", max)
);
}
}
auto LinearQuantizer::operator()(float value) const -> std::uint32_t {
if (value < 0 || value > m_max) {
throw std::invalid_argument(
fmt::format("quantized value must be between 0 and {} but {} given", m_max, value)
);
}
auto normalized_value = static_cast<double>(value / m_max);
return static_cast<std::uint32_t>(normalized_value * (m_range - 1)) + 1;
}
auto LinearQuantizer::range() const noexcept -> std::uint32_t {
return m_range;
}
} // namespace pisa