-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable load_from_bytes of model and ExtScorer #3331
Open
bernardohenz
wants to merge
9
commits into
mozilla:master
Choose a base branch
from
bernardohenz:loading_from_bytes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
06c23fe
Loading model from both path or array of bytes
danielefernandes dc8553b
Loading ExtScorer from array of bytes
bernardohenz d6a4e37
removing debug info
bernardohenz 9671041
Exposing methods DS_CreateModelFromBuffer and DS_EnableExternalScorer…
bernardohenz dd73ec8
Loading tflite buffer from buffer working
bernardohenz 9faeb51
Fixing API functions exposition
bernardohenz 71e19bc
load_trie with default value for load_from_bytes
bernardohenz 3b9d2b0
Seting a default value for load_from_bytes in load_lm
bernardohenz df40e22
-Change string to char* in the API
bernardohenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,22 +29,24 @@ static const int32_t FILE_VERSION = 6; | |
|
||
int | ||
Scorer::init(const std::string& lm_path, | ||
bool load_from_bytes, | ||
const Alphabet& alphabet) | ||
{ | ||
set_alphabet(alphabet); | ||
return load_lm(lm_path); | ||
return load_lm(lm_path, load_from_bytes); | ||
} | ||
|
||
int | ||
Scorer::init(const std::string& lm_path, | ||
bool load_from_bytes, | ||
const std::string& alphabet_config_path) | ||
{ | ||
int err = alphabet_.init(alphabet_config_path.c_str()); | ||
int err = alphabet_.init(alphabet_config_path.c_str()); // Do we need to make this initiable from bytes? | ||
if (err != 0) { | ||
return err; | ||
} | ||
setup_char_map(); | ||
return load_lm(lm_path); | ||
return load_lm(lm_path, load_from_bytes); | ||
} | ||
|
||
void | ||
|
@@ -69,45 +71,60 @@ void Scorer::setup_char_map() | |
} | ||
} | ||
|
||
int Scorer::load_lm(const std::string& lm_path) | ||
int Scorer::load_lm(const std::string& lm_string, bool load_from_bytes) | ||
{ | ||
// Check if file is readable to avoid KenLM throwing an exception | ||
const char* filename = lm_path.c_str(); | ||
if (access(filename, R_OK) != 0) { | ||
return DS_ERR_SCORER_UNREADABLE; | ||
} | ||
|
||
// Check if the file format is valid to avoid KenLM throwing an exception | ||
lm::ngram::ModelType model_type; | ||
if (!lm::ngram::RecognizeBinary(filename, model_type)) { | ||
return DS_ERR_SCORER_INVALID_LM; | ||
if (!load_from_bytes){ | ||
// Check if file is readable to avoid KenLM throwing an exception | ||
const char* filename = lm_string.c_str(); | ||
if (access(filename, R_OK) != 0) { | ||
return DS_ERR_SCORER_UNREADABLE; | ||
} | ||
|
||
// Check if the file format is valid to avoid KenLM throwing an exception | ||
lm::ngram::ModelType model_type; | ||
if (!lm::ngram::RecognizeBinary(filename, model_type)) { | ||
return DS_ERR_SCORER_INVALID_LM; | ||
} | ||
} | ||
|
||
// Load the LM | ||
lm::ngram::Config config; | ||
config.load_method = util::LoadMethod::LAZY; | ||
language_model_.reset(lm::ngram::LoadVirtual(filename, config)); | ||
max_order_ = language_model_->Order(); | ||
|
||
uint64_t package_size; | ||
{ | ||
util::scoped_fd fd(util::OpenReadOrThrow(filename)); | ||
package_size = util::SizeFile(fd.get()); | ||
if (load_from_bytes){ | ||
language_model_.reset(lm::ngram::LoadVirtual(lm_string.c_str(), lm_string.size(), config)); | ||
} else { | ||
language_model_.reset(lm::ngram::LoadVirtual(lm_string.c_str(), config)); | ||
} | ||
|
||
max_order_ = language_model_->Order(); | ||
std::stringstream stst; | ||
uint64_t trie_offset = language_model_->GetEndOfSearchOffset(); | ||
if (package_size <= trie_offset) { | ||
// File ends without a trie structure | ||
return DS_ERR_SCORER_NO_TRIE; | ||
|
||
if (!load_from_bytes){ | ||
uint64_t package_size; | ||
{ | ||
util::scoped_fd fd(util::OpenReadOrThrow(lm_string.c_str())); | ||
package_size = util::SizeFile(fd.get()); | ||
} | ||
|
||
if (package_size <= trie_offset) { | ||
// File ends without a trie structure | ||
return DS_ERR_SCORER_NO_TRIE; | ||
} | ||
// Read metadata and trie from file | ||
std::ifstream fin(lm_string.c_str(), std::ios::binary); | ||
stst<<fin.rdbuf(); | ||
} else { | ||
stst = std::stringstream(lm_string); | ||
} | ||
|
||
// Read metadata and trie from file | ||
std::ifstream fin(lm_path, std::ios::binary); | ||
fin.seekg(trie_offset); | ||
return load_trie(fin, lm_path); | ||
stst.seekg(trie_offset); | ||
return load_trie(stst, lm_string, load_from_bytes); | ||
} | ||
|
||
int Scorer::load_trie(std::ifstream& fin, const std::string& file_path) | ||
int Scorer::load_trie(std::stringstream& fin, const std::string& file_path, bool load_from_bytes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I tested yes |
||
{ | ||
|
||
int magic; | ||
fin.read(reinterpret_cast<char*>(&magic), sizeof(magic)); | ||
if (magic != MAGIC) { | ||
|
@@ -140,9 +157,13 @@ int Scorer::load_trie(std::ifstream& fin, const std::string& file_path) | |
reset_params(alpha, beta); | ||
|
||
fst::FstReadOptions opt; | ||
opt.mode = fst::FstReadOptions::MAP; | ||
opt.source = file_path; | ||
dictionary.reset(FstType::Read(fin, opt)); | ||
if (load_from_bytes) { | ||
dictionary.reset(fst::ConstFst<fst::StdArc>::Read(fin, opt)); | ||
} else { | ||
opt.mode = fst::FstReadOptions::MAP; | ||
opt.source = file_path; | ||
dictionary.reset(FstType::Read(fin, opt)); | ||
} | ||
return DS_ERR_OK; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either provide a default value, split the implementation in two
load_lm
or update all call sites: this PR does not build because ofload_lm
calls ingenerate_scorer_package.cpp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've provided a default value (false) for the boolean