Skip to content

Commit ba2bf3d

Browse files
kylebgormancopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 956702795
1 parent 9efebb3 commit ba2bf3d

6 files changed

Lines changed: 29 additions & 28 deletions

File tree

mozolm/models/BUILD.bazel

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ cc_library(
3737
":lm_scores_cc_proto",
3838
":model_config_cc_proto",
3939
"//mozolm/stubs:integral_types",
40+
"//third_party/opengrm/sfst",
4041
"@com_google_absl//absl/container:flat_hash_map",
4142
"@com_google_absl//absl/status",
4243
"@com_google_absl//absl/status:statusor",
4344
"@com_google_nisaba//nisaba/port:status_macros",
4445
"@com_google_nisaba//nisaba/port:utf8_util",
4546
"@com_google_protobuf//:protobuf",
46-
"@org_opengrm_ngram//:opengrm-ngram-lib",
4747
],
4848
)
4949

@@ -74,12 +74,12 @@ cc_library(
7474
":lm_scores_cc_proto",
7575
":model_storage_cc_proto",
7676
"//mozolm/stubs:integral_types",
77+
"//third_party/opengrm/sfst",
7778
"@com_google_absl//absl/status",
7879
"@com_google_absl//absl/status:statusor",
7980
"@com_google_absl//absl/strings",
8081
"@com_google_nisaba//nisaba/port:utf8_util",
8182
"@com_google_protobuf//:protobuf",
82-
"@org_opengrm_ngram//:opengrm-ngram-lib",
8383
],
8484
)
8585

@@ -251,6 +251,7 @@ cc_library(
251251
":ngram_fst_model",
252252
":ngram_word_fst_options_cc_proto",
253253
"//mozolm/stubs:integral_types",
254+
"//third_party/opengrm/sfst",
254255
"@com_google_absl//absl/container:flat_hash_map",
255256
"@com_google_absl//absl/memory",
256257
"@com_google_absl//absl/status",
@@ -260,7 +261,6 @@ cc_library(
260261
"@com_google_protobuf//:protobuf",
261262
"@org_openfst//:fst",
262263
"@org_openfst//:symbol-table",
263-
"@org_opengrm_ngram//:opengrm-ngram-lib",
264264
],
265265
)
266266

@@ -334,6 +334,8 @@ cc_library(
334334
":model_storage_cc_proto",
335335
":ppm_as_fst_options_cc_proto",
336336
"//mozolm/stubs:integral_types",
337+
"//third_party/opengrm/sfst",
338+
"//third_party/opengrm/sfst:ngram-count",
337339
"@com_google_absl//absl/container:flat_hash_set",
338340
"@com_google_absl//absl/memory",
339341
"@com_google_absl//absl/status:statusor",
@@ -345,7 +347,6 @@ cc_library(
345347
"@com_google_protobuf//:protobuf",
346348
"@org_openfst//:fst",
347349
"@org_openfst//:symbol-table",
348-
"@org_opengrm_ngram//:opengrm-ngram-lib",
349350
],
350351
)
351352

mozolm/models/language_model.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include "absl/strings/str_cat.h"
2020
#include "nisaba/port/utf8_util.h"
21-
#include "ngram/ngram-model.h"
21+
#include "third_party/opengrm/sfst/sfst.h"
2222

2323
namespace mozolm {
2424
namespace models {
@@ -75,7 +75,7 @@ void SoftmaxRenormalize(std::vector<double> *neg_log_probs) {
7575
double kahan_factor = 0.0;
7676
for (int i = 1; i < neg_log_probs->size(); ++i) {
7777
tot_prob =
78-
ngram::NegLogSum(tot_prob, (*neg_log_probs)[i], &kahan_factor);
78+
sfst::NegLogSum(tot_prob, (*neg_log_probs)[i], &kahan_factor);
7979
}
8080
for (int i = 0; i < neg_log_probs->size(); ++i) {
8181
(*neg_log_probs)[i] -= tot_prob;

mozolm/models/language_model_hub.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "google/protobuf/stubs/logging.h"
2121
#include "nisaba/port/utf8_util.h"
22-
#include "ngram/ngram-model.h"
22+
#include "third_party/opengrm/sfst/sfst.h"
2323
#include "nisaba/port/status_macros.h"
2424

2525
namespace mozolm {
@@ -38,7 +38,7 @@ double MixResults(const LMScores& lm_scores, double mix_weight,
3838
const std::string key = lm_scores.symbols(i);
3939
double value = -std::log(lm_scores.probabilities(i)) + mix_weight;
4040
if (mixed_values->contains(key)) {
41-
value = ngram::NegLogSum(value, mixed_values->find(key)->second);
41+
value = sfst::NegLogSum(value, mixed_values->find(key)->second);
4242
}
4343
mixed_values->insert_or_assign(key, value);
4444
}
@@ -56,7 +56,7 @@ void ExtractMixture(
5656
if (idx == 0) {
5757
norm = mixed_value.second;
5858
} else {
59-
norm = ngram::NegLogSum(norm, mixed_value.second);
59+
norm = sfst::NegLogSum(norm, mixed_value.second);
6060
}
6161
values[idx++] = std::make_pair(mixed_value.first, mixed_value.second);
6262
}
@@ -96,7 +96,7 @@ absl::Status LanguageModelHub::InitializeModels(const ModelHubConfig& config) {
9696
// Stores negative log mixing weights for each model.
9797
mixture_weights_[idx] = config.model_config(idx).weight();
9898
normalization = (idx == 0) ? mixture_weights_[idx]
99-
: ngram::NegLogSum(normalization,
99+
: sfst::NegLogSum(normalization,
100100
mixture_weights_[idx]);
101101
}
102102
for (auto idx = 0; idx < mixture_weights_.size(); ++idx) {
@@ -233,7 +233,7 @@ std::vector<double> LanguageModelHub::GetBayesianMixtureWeights(
233233
mixture_weights[idx] += bayesian_history_probs_sum[idx];
234234
normalization = (idx == 0)
235235
? mixture_weights[idx]
236-
: ngram::NegLogSum(normalization, mixture_weights[idx]);
236+
: sfst::NegLogSum(normalization, mixture_weights[idx]);
237237
}
238238
for (auto idx = 0; idx < mixture_weights.size(); ++idx) {
239239
mixture_weights[idx] -= normalization;

mozolm/models/ngram_word_fst_model.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "fst/fst.h"
2727
#include "fst/matcher.h"
2828
#include "fst/symbol-table.h"
29-
#include "ngram/ngram-model.h"
29+
#include "third_party/opengrm/sfst/sfst.h"
3030
#include "nisaba/port/status_macros.h"
3131

3232
using nisaba::utf8::DecodeSingleUnicodeChar;
@@ -51,7 +51,7 @@ double SafeNegLogDiff(double cost1, double cost2) {
5151
if (cost1 >= cost2) {
5252
return StdArc::Weight::Zero().Value();
5353
}
54-
return ngram::NegLogDiff(cost1, cost2);
54+
return sfst::NegLogDiff(cost1, cost2);
5555
}
5656

5757
// Returns the character at index idx in the unicode string.

mozolm/models/ppm_as_fst_model.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
#include "fst/arcsort.h"
2929
#include "fst/symbol-table.h"
3030
#include "fst/vector-fst.h"
31-
#include "ngram/ngram-count.h"
32-
#include "ngram/ngram-model.h"
31+
#include "third_party/opengrm/sfst/ngram-count.h"
32+
#include "third_party/opengrm/sfst/sfst.h"
3333
#include "nisaba/port/status_macros.h"
3434

3535
namespace mozolm {
@@ -230,7 +230,7 @@ int IncrementBackoffArcReturnBackoffState(StdVectorFst* fst,
230230
if (arc.ilabel == 0) {
231231
backoff_state = arc.nextstate;
232232
if (increment_count) {
233-
arc.weight = StdArc::Weight(ngram::NegLogSum(arc.weight.Value(), 0.0));
233+
arc.weight = StdArc::Weight(sfst::NegLogSum(arc.weight.Value(), 0.0));
234234
arc_iterator.SetValue(arc);
235235
}
236236
}
@@ -248,7 +248,7 @@ double GetTotalStateCount(const StdVectorFst& fst, StdArc::StateId s) {
248248
state_count = arc.weight.Value();
249249
return state_count;
250250
}
251-
state_count = ngram::NegLogSum(state_count, arc.weight.Value());
251+
state_count = sfst::NegLogSum(state_count, arc.weight.Value());
252252
}
253253
return state_count;
254254
}
@@ -271,8 +271,8 @@ absl::StatusOr<double> UpdateIndexProb(double count, double neg_log_beta,
271271
if (count >= neg_log_beta) {
272272
return absl::InternalError("Found a count less than \beta.");
273273
}
274-
sym_prob = ngram::NegLogSum(
275-
lower_order_prob, ngram::NegLogDiff(count, neg_log_beta) - denominator);
274+
sym_prob = sfst::NegLogSum(
275+
lower_order_prob, sfst::NegLogDiff(count, neg_log_beta) - denominator);
276276
}
277277
return sym_prob;
278278
}
@@ -356,10 +356,10 @@ absl::Status PpmAsFstModel::AddPriorCounts() {
356356
StdArc arc = arc_iterator.Value();
357357
has_unigram.insert(arc.ilabel);
358358
arc.weight = StdArc::Weight(
359-
ngram::NegLogSum(arc.weight.Value(), 0.0)); // Adds 1 count.
359+
sfst::NegLogSum(arc.weight.Value(), 0.0)); // Adds 1 count.
360360
arc_iterator.SetValue(arc);
361361
}
362-
fst_->SetFinal(unigram_state, StdArc::Weight(ngram::NegLogSum(
362+
fst_->SetFinal(unigram_state, StdArc::Weight(sfst::NegLogSum(
363363
fst_->Final(unigram_state).Value(), 0.0)));
364364
bool syms_added = false;
365365
for (SymbolTableIterator syms_iter(*syms_); !syms_iter.Done();
@@ -474,7 +474,7 @@ absl::Status PpmAsFstModel::Read(const ModelStorage& storage) {
474474
syms_->AddSymbol("<epsilon>");
475475
fst_->SetInputSymbols(syms_.get());
476476
fst_->SetOutputSymbols(syms_.get());
477-
ngram_counter_ = std::make_unique<ngram::NGramCounter<Log64Weight>>(
477+
ngram_counter_ = std::make_unique<sfst::NGramCounter<Log64Weight>>(
478478
/*order=*/max_order_);
479479
if (!storage.model_file().empty()) {
480480
GOOGLE_LOG(INFO) << "Initializing from training data ...";
@@ -598,7 +598,7 @@ std::vector<double> PpmAsFstModel::InitCacheProbs(
598598
--num_continuations;
599599
}
600600
const double gamma =
601-
ngram::NegLogSum(-std::log(num_continuations) - std::log(beta_),
601+
sfst::NegLogSum(-std::log(num_continuations) - std::log(beta_),
602602
-std::log(alpha_)) - denominator;
603603
for (size_t i = 0; i < cache_probs.size(); ++i) {
604604
// Adds in gamma factor to backoff probabilities.
@@ -657,7 +657,7 @@ absl::Status PpmAsFstModel::UpdateCacheAtNonEmptyState(
657657
std::vector<int> destination_states =
658658
InitCacheStates(s, backoff_state, backoff_cache, /*arc_origin=*/false);
659659
const double denominator =
660-
ngram::NegLogSum(impl::GetTotalStateCount(*fst_, s), -std::log(alpha_));
660+
sfst::NegLogSum(impl::GetTotalStateCount(*fst_, s), -std::log(alpha_));
661661
std::vector<double> neg_log_probabilities =
662662
InitCacheProbs(s, backoff_state, backoff_cache, denominator);
663663
update_status = UpdateCacheStatesAndProbs(
@@ -831,7 +831,7 @@ absl::Status PpmAsFstModel::UpdateHighestFoundState(StdArc::StateId curr_state,
831831
int sym_index) {
832832
if (sym_index == 0) {
833833
// Adds one to final cost and sets destination state to start state.
834-
fst_->SetFinal(curr_state, StdArc::Weight(ngram::NegLogSum(
834+
fst_->SetFinal(curr_state, StdArc::Weight(sfst::NegLogSum(
835835
fst_->Final(curr_state).Value(), 0.0)));
836836
} else {
837837
// Arc with sym_index found at current state.
@@ -853,7 +853,7 @@ absl::Status PpmAsFstModel::UpdateHighestFoundState(StdArc::StateId curr_state,
853853
new_next_state = state_orders_.size();
854854
arc.nextstate = new_next_state;
855855
}
856-
arc.weight = StdArc::Weight(ngram::NegLogSum(arc.weight.Value(), 0.0));
856+
arc.weight = StdArc::Weight(sfst::NegLogSum(arc.weight.Value(), 0.0));
857857
arc_iterator.SetValue(arc);
858858
break;
859859
}

mozolm/models/ppm_as_fst_model.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
#include "mozolm/models/ppm_as_fst_options.pb.h"
7373
#include "fst/symbol-table.h"
7474
#include "fst/vector-fst.h"
75-
#include "ngram/ngram-count.h"
75+
#include "third_party/opengrm/sfst/ngram-count.h"
7676

7777
namespace mozolm {
7878
namespace models {
@@ -318,7 +318,7 @@ class PpmAsFstModel : public LanguageModel {
318318
std::vector<int> state_orders_; // Stores the order of each state.
319319
std::unique_ptr<fst::StdVectorFst> fst_; // Model (counts) stored in FST.
320320
// For counting character n-grams if training from text file.
321-
std::unique_ptr<ngram::NGramCounter<fst::Log64Weight>> ngram_counter_;
321+
std::unique_ptr<sfst::NGramCounter<fst::Log64Weight>> ngram_counter_;
322322
std::unique_ptr<fst::SymbolTable> syms_; // Character symbols.
323323

324324
// For caching probabilities and destination states for quick access.

0 commit comments

Comments
 (0)