Loading...
Searching...
No Matches
weightencoder.h
Go to the documentation of this file.
#ifndef DYSCO_WEIGHT_ENCODER_H
#define DYSCO_WEIGHT_ENCODER_H
#include <cmath>
#include <vector>
namespace dyscostman {
template <typename T>
class WeightEncoder {
public:
typedef T value_t;
typedef unsigned symbol_t;
explicit WeightEncoder(unsigned quantCount) : _quantCount(quantCount) {}
void Encode(value_t &scaleVal, std::vector<symbol_t> &dest,
const std::vector<value_t> &input) const {
// Find max value (implicit assumption input is not empty)
typename std::vector<value_t>::const_iterator i = input.begin();
scaleVal = *i;
++i;
while (i != input.end()) {
if (*i > scaleVal) scaleVal = *i;
++i;
}
i = input.begin();
typename std::vector<symbol_t>::iterator d = dest.begin();
const value_t scaleFact = value_t(_quantCount - 1) / scaleVal;
while (i != input.end()) {
*d = roundf(scaleFact * (*i));
++i;
++d;
}
}
void Decode(std::vector<value_t> &dest, value_t scaleVal,
const std::vector<symbol_t> &input) const {
typename std::vector<symbol_t>::const_iterator i = input.begin();
typename std::vector<value_t>::iterator d = dest.begin();
const value_t scaleFact = scaleVal / value_t(_quantCount - 1);
while (i != input.end()) {
*d = (*i) * scaleFact;
++i;
++d;
}
}
private:
unsigned _quantCount;
};
} // namespace dyscostman
#endif
dyscostman::WeightEncoder::symbol_t
unsigned symbol_t
Value type of the symbols to which the weights are encoded.
Definition weightencoder.h:21
dyscostman::WeightEncoder::value_t
T value_t
Value type of the original weights (a floating point value).
Definition weightencoder.h:19
dyscostman::WeightEncoder::Decode
void Decode(std::vector< value_t > &dest, value_t scaleVal, const std::vector< symbol_t > &input) const
Decode a previously encoded value.
Definition weightencoder.h:66
dyscostman::WeightEncoder::Encode
void Encode(value_t &scaleVal, std::vector< symbol_t > &dest, const std::vector< value_t > &input) const
Encodes a vector of values.
Definition weightencoder.h:37
dyscostman::WeightEncoder::WeightEncoder
WeightEncoder(unsigned quantCount)
Construct a new encoder with the given quantization count.
Definition weightencoder.h:26
dyscostman::WeightEncoder::_quantCount
unsigned _quantCount
Definition weightencoder.h:80
dyscostman
Definition bytepacker.h:7