Skip to content
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

Implement poolside input_format for spm_decode #21

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/spm_decode_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,34 @@
#include <string>
#include <vector>

#include "absl/flags/flag.h"
#include "absl/strings/str_split.h"
#include "common.h"
#include "filesystem.h"
#include "init.h"
#include "sentencepiece.pb.h"
#include "sentencepiece_processor.h"
#include "absl/flags/flag.h"
#include "absl/strings/str_split.h"
#include "util.h"

ABSL_FLAG(std::string, model, "", "model file name");
ABSL_FLAG(std::string, input, "", "input filename");
ABSL_FLAG(std::string, output, "", "output filename");
ABSL_FLAG(std::string, input_format, "piece", "choose from piece or id");
ABSL_FLAG(std::string, input_format, "piece",
"choose from piece, id, poolside or poolside_no_toc");
ABSL_FLAG(std::string, output_format, "string", "choose from string or proto");
ABSL_FLAG(std::string, extra_options, "",
"':' separated encoder extra options, e.g., \"reverse:bos:eos\"");

std::vector<int> read_uint32(absl::string_view binary_data) {
std::vector<int> result;
for (size_t i = 0; i < binary_data.size(); i += sizeof(uint32_t)) {
uint32_t value =
*reinterpret_cast<const uint32_t *>(binary_data.data() + i);
result.push_back(value);
}
return result;
}

int main(int argc, char *argv[]) {
sentencepiece::ScopedResourceDestructor cleaner;
sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true);
Expand Down Expand Up @@ -101,6 +112,31 @@ int main(int argc, char *argv[]) {
LOG(FATAL) << "Unknown output format: "
<< absl::GetFlag(FLAGS_output_format);
}
} else if (absl::GetFlag(FLAGS_input_format) == "poolside" ||
absl::GetFlag(FLAGS_input_format) == "poolside_no_toc") {
for (const auto &filename : rest_args) {
auto input = sentencepiece::filesystem::NewReadableFile(filename, false);
CHECK_OK(input->status());
input->ReadAll(&line);
{
auto tokens = read_uint32(line);
if (absl::GetFlag(FLAGS_input_format) == "poolside") {
// the last 8 bytes of the file contain encode the number of documents
// encoded as a little endian uint64_t which means the last 2 uint32_t
// of the tokens array contain the length;
uint64_t num_docs = (uint64_t)tokens.back() << 32;
tokens.pop_back();
num_docs |= tokens.back();
// the trailier of the file contains the length of each document
// encoded as an uint32_t
tokens.resize(tokens.size() - num_docs);
}

CHECK_OK(sp.Decode(tokens, &detok));
output->Write(detok);
}
}
return 0;
} else {
LOG(FATAL) << "Unknown input format: " << absl::GetFlag(FLAGS_input_format);
}
Expand Down