00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "lm/quantize.hh"
00010
00011 #include "lm/binary_format.hh"
00012 #include "lm/lm_exception.hh"
00013 #include "util/file.hh"
00014
00015 #include <algorithm>
00016 #include <numeric>
00017
00018 namespace lm {
00019 namespace ngram {
00020
00021 namespace {
00022
00023 void MakeBins(std::vector<float> &values, float *centers, uint32_t bins) {
00024 std::sort(values.begin(), values.end());
00025 std::vector<float>::const_iterator start = values.begin(), finish;
00026 for (uint32_t i = 0; i < bins; ++i, ++centers, start = finish) {
00027 finish = values.begin() + ((values.size() * static_cast<uint64_t>(i + 1)) / bins);
00028 if (finish == start) {
00029
00030 *centers = i ? *(centers - 1) : -std::numeric_limits<float>::infinity();
00031 } else {
00032 *centers = std::accumulate(start, finish, 0.0) / static_cast<float>(finish - start);
00033 }
00034 }
00035 }
00036
00037 const char kSeparatelyQuantizeVersion = 2;
00038
00039 }
00040
00041 void SeparatelyQuantize::UpdateConfigFromBinary(const BinaryFormat &file, uint64_t offset, Config &config) {
00042 unsigned char buffer[3];
00043 file.ReadForConfig(buffer, 3, offset);
00044 char version = buffer[0];
00045 config.prob_bits = buffer[1];
00046 config.backoff_bits = buffer[2];
00047 if (version != kSeparatelyQuantizeVersion) UTIL_THROW(FormatLoadException, "This file has quantization version " << (unsigned)version << " but the code expects version " << (unsigned)kSeparatelyQuantizeVersion);
00048 }
00049
00050 void SeparatelyQuantize::SetupMemory(void *base, unsigned char order, const Config &config) {
00051 prob_bits_ = config.prob_bits;
00052 backoff_bits_ = config.backoff_bits;
00053
00054 if (config.prob_bits == 0) UTIL_THROW(ConfigException, "You can't quantize probability to zero");
00055 if (config.backoff_bits == 0) UTIL_THROW(ConfigException, "You can't quantize backoff to zero");
00056 if (config.prob_bits > 25) UTIL_THROW(ConfigException, "For efficiency reasons, quantizing probability supports at most 25 bits. Currently you have requested " << static_cast<unsigned>(config.prob_bits) << " bits.");
00057 if (config.backoff_bits > 25) UTIL_THROW(ConfigException, "For efficiency reasons, quantizing backoff supports at most 25 bits. Currently you have requested " << static_cast<unsigned>(config.backoff_bits) << " bits.");
00058
00059 actual_base_ = static_cast<uint8_t*>(base);
00060 float *start = reinterpret_cast<float*>(actual_base_ + 8);
00061 for (unsigned char i = 0; i < order - 2; ++i) {
00062 tables_[i][0] = Bins(prob_bits_, start);
00063 start += (1ULL << prob_bits_);
00064 tables_[i][1] = Bins(backoff_bits_, start);
00065 start += (1ULL << backoff_bits_);
00066 }
00067 longest_ = tables_[order - 2][0] = Bins(prob_bits_, start);
00068 }
00069
00070 void SeparatelyQuantize::Train(uint8_t order, std::vector<float> &prob, std::vector<float> &backoff) {
00071 TrainProb(order, prob);
00072
00073
00074 float *centers = tables_[order - 2][1].Populate();
00075 *(centers++) = kNoExtensionBackoff;
00076 *(centers++) = kExtensionBackoff;
00077 MakeBins(backoff, centers, (1ULL << backoff_bits_) - 2);
00078 }
00079
00080 void SeparatelyQuantize::TrainProb(uint8_t order, std::vector<float> &prob) {
00081 float *centers = tables_[order - 2][0].Populate();
00082 MakeBins(prob, centers, (1ULL << prob_bits_));
00083 }
00084
00085 void SeparatelyQuantize::FinishedLoading(const Config &config) {
00086 uint8_t *actual_base = actual_base_;
00087 *(actual_base++) = kSeparatelyQuantizeVersion;
00088 *(actual_base++) = config.prob_bits;
00089 *(actual_base++) = config.backoff_bits;
00090 }
00091
00092 }
00093 }