From 5bf29c1b01b09b9245338b10bfed0f0173470167 Mon Sep 17 00:00:00 2001 From: Jerin Philip Date: Sat, 3 Apr 2021 13:46:16 +0000 Subject: [PATCH] We accept TranslationRequest now, and vector Towards https://github.com/browsermt/bergamot-translator/issues/77. - What was earlier translateWithOptions is now renamed to translate, making it a method overload based on arguments. - A new translateMultiple is provided, which is blocking. This allows the browser to sent separate vector which is queued as a whole, but each text maps to a Request each for which there is a Response. This is closer to what exists in TranslationModel as of now, except there are speed gains from properly batching. translateMultiple accepts TranslationRequest as well, although it does nothing with it. --- app/service-cli.cpp | 2 +- src/translator/service.cpp | 45 +++++++++++++++++++++++++++++++++----- src/translator/service.h | 27 +++++++++++++++++++++-- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/app/service-cli.cpp b/app/service-cli.cpp index 526da7f7d..f8b07e88f 100644 --- a/app/service-cli.cpp +++ b/app/service-cli.cpp @@ -28,7 +28,7 @@ int main(int argc, char *argv[]) { // Wait on future until Response is complete std::future responseFuture = - service.translateWithOptions(std::move(input), responseOptions); + service.translate(std::move(input), responseOptions); responseFuture.wait(); Response response = responseFuture.get(); diff --git a/src/translator/service.cpp b/src/translator/service.cpp index e678f2c2d..62c19893f 100644 --- a/src/translator/service.cpp +++ b/src/translator/service.cpp @@ -119,12 +119,36 @@ std::future Service::translate(std::string &&input) { New(); // TODO(jerinphilip): Take this in as argument // Hardcode responseOptions for now - return translateWithOptions(std::move(input), responseOptions); + return translate(std::move(input), responseOptions); } -std::future -Service::translateWithOptions(std::string &&input, - ResponseOptions responseOptions) { +std::vector +Service::translateMultiple(std::vector &&inputs, + TranslationRequest translationRequest) { + ResponseOptions responseOptions = New(); + + // TODO(jerinphilip) Set options based on TranslationRequest, if and when it + // becomes non-dummy. + + std::vector> responseFutures; + for (auto &input : inputs) { + std::future inputResponse = + queueRequest(std::move(input), responseOptions); + responseFutures.push_back(std::move(inputResponse)); + } + + dispatchTranslate(); + + std::vector responses; + for (auto &future : responseFutures) { + future.wait(); + responses.push_back(std::move(future.get())); + } + return responses; +} + +std::future Service::queueRequest(std::string &&input, + ResponseOptions responseOptions) { Segments segments; AnnotatedText source(std::move(input)); text_processor_.process(source, segments); @@ -138,12 +162,23 @@ Service::translateWithOptions(std::string &&input, std::move(responseBuilder)); batcher_.addWholeRequest(request); + return future; +} + +std::future Service::translate(std::string &&input, + ResponseOptions responseOptions) { + std::future future = + queueRequest(std::move(input), responseOptions); + dispatchTranslate(); + return future; +} + +void Service::dispatchTranslate() { if (numWorkers_ == 0) { blocking_translate(); } else { async_translate(); } - return future; } Service::~Service() { diff --git a/src/translator/service.h b/src/translator/service.h index 6244f9a0a..3e62b28f9 100644 --- a/src/translator/service.h +++ b/src/translator/service.h @@ -1,6 +1,7 @@ #ifndef SRC_BERGAMOT_SERVICE_H_ #define SRC_BERGAMOT_SERVICE_H_ +#include "TranslationRequest.h" #include "batch_translator.h" #include "batcher.h" #include "data/types.h" @@ -104,10 +105,32 @@ class Service { /// @param [in] responseOptions: Options indicating whether or not to include /// some member in the Response, also specify any additional configurable /// parameters. - std::future translateWithOptions(std::string &&source, - ResponseOptions options); + std::future translate(std::string &&source, + ResponseOptions options); + + /// Translate an input, providing TranslationRequest across all texts to + /// construct Response. Provides the browser with the ability to break texts + /// into multiple Request keeping gains from efficiently batching internally. + /// Also useful when one has to set/unset alignments or quality in the + /// Response to save compute spent in constructing these objects. + + /// @param [in] source: rvalue reference of the string to be translated / + /// @param [in] translationRequest: TranslationRequest (Unified API) + /// indicating whether or not to include some member in the Response, also + /// specify any / additional configurable parameters. + + std::vector + translateMultiple(std::vector &&source, + TranslationRequest translationRequest); private: + /// Queue an input for translation. + std::future queueRequest(std::string &&input, + ResponseOptions responseOptions); + + /// Dispatch call to translate after inserting in queue + void dispatchTranslate(); + /// Build numTranslators number of translators with options from options void build_translators(Ptr options, size_t numTranslators); /// Initializes a blocking translator without using std::thread