Skip to content

Commit

Permalink
Update TranslationModel to boost speed
Browse files Browse the repository at this point in the history
Towards #77.

This updates TranslationModel to use the new
Service::translateMultiple(...) API to efficiently pack batches by
compiling multiple Requests and using only using one translate-dispatch
call for all Requests.

Due to changes in batching/ordering and consequently difference in
approximations after quantization, the outputs are now different from those
earlier (breaking bergamot-translator-app regression-tests) but correct
nonetheless (regression-tests output now need updating).
  • Loading branch information
Jerin Philip committed Apr 7, 2021
1 parent 583fce6 commit 3fb44bd
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/translator/TranslationModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// All local project includes
#include "TranslationModel.h"
#include "translator/parser.h"
#include "translator/response.h"
#include "translator/service.h"

TranslationModel::TranslationModel(const std::string &config,
Expand All @@ -21,31 +22,25 @@ TranslationModel::~TranslationModel() {}
std::vector<TranslationResult>
TranslationModel::translate(std::vector<std::string> &&texts,
TranslationRequest request) {
// Implementing a non-async version first. Unpleasant, but should work.
std::promise<std::vector<TranslationResult>> promise;
auto future = promise.get_future();

// This code, move into async?
std::vector<TranslationResult> translationResults;
for (auto &text : texts) {
// Collect future as marian::bergamot::TranslationResult
auto intermediate = service_.translate(std::move(text));
intermediate.wait();
auto marianResponse(std::move(intermediate.get()));

std::vector<marian::bergamot::Response> responses =
service_.translateMultiple(std::move(texts), request);
for (auto &response : responses) {
TranslationResult::SentenceMappings sentenceMappings;
for (size_t idx = 0; idx < marianResponse.size(); idx++) {
marian::string_view src = marianResponse.source.sentence(idx);
marian::string_view tgt = marianResponse.target.sentence(idx);
for (size_t idx = 0; idx < response.size(); idx++) {
marian::string_view src = response.source.sentence(idx);
marian::string_view tgt = response.target.sentence(idx);
sentenceMappings.emplace_back(std::string_view(src.data(), src.size()),
std::string_view(tgt.data(), tgt.size()));
}

// In place construction.
translationResults.emplace_back(
std::move(marianResponse.source.text), // &&marianResponse.source_
std::move(marianResponse.target.text), // &&marianResponse.translation_
std::move(sentenceMappings) // &&sentenceMappings
std::move(response.source.text), // &&response.source_
std::move(response.target.text), // &&response.translation_
std::move(sentenceMappings) // &&sentenceMappings
);
}

Expand Down

0 comments on commit 3fb44bd

Please sign in to comment.