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

Cleanup API: Refactor request on-complete transition #80

Merged
merged 96 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 87 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
138a032
Removes vocabs and propogates fixes for breaks
Mar 31, 2021
457fc69
Merge branch 'main' into jp/marian-decoder-new-output-collector
Mar 31, 2021
28eb098
Prettify diff: Undoing comment shuffles due to merge conflict edits
Mar 31, 2021
2a6b5b2
20% of time actual work, 80% prettifying diff
Mar 31, 2021
63da9bd
Histories members -> poof!
Mar 31, 2021
f6fbc81
First draft of ResponseBuilder
Mar 31, 2021
4fe562f
Draft implementation builds
Mar 31, 2021
208757c
Duplicating functions for surgery at Service
Mar 31, 2021
e5422c4
Cut Service, Request open to move ResponseBuilder in
Mar 31, 2021
38f3439
Surgery works! No more broken promises!
Mar 31, 2021
ebe9839
Probably overdoing appendSentence on annotation
Mar 31, 2021
0201efc
Transplant: ResponseBuilder new flow in
Mar 31, 2021
5d70aa2
Applies output collector removal as a single commit
Mar 31, 2021
ba03154
Changing Annotation to adhere to [begin, end)
Apr 1, 2021
a315dc0
Stronger unit tests on sentences + num words, num sentences
Apr 1, 2021
0239a5b
Hotfix with empty string view from EOS
Apr 1, 2021
4970a62
No more absolving empty-sentence; Added tests now defined behaviour
Apr 1, 2021
7e2b664
Uncommenting important section in unit test
Apr 1, 2021
d8d89f3
Ensure empty string view default, beginning at end so marker points
Apr 1, 2021
f10d6d2
Further strengthen and comment unit-tests, mark exactly where empty s…
Apr 1, 2021
b86db66
Changing Annotation to adhere to [begin, end)
Apr 1, 2021
095fde7
Stronger unit tests on sentences + num words, num sentences
Apr 1, 2021
ff5d61e
Hotfix with empty string view from EOS
Apr 1, 2021
f43324a
No more absolving empty-sentence; Added tests now defined behaviour
Apr 1, 2021
da43a85
Uncommenting important section in unit test
Apr 1, 2021
325b2cc
Ensure empty string view default, beginning at end so marker points
Apr 1, 2021
6947dc4
Further strengthen and comment unit-tests, mark exactly where empty s…
Apr 1, 2021
83e3290
Merge branch 'main' of github.com:browsermt/bergamot-translator into …
Apr 1, 2021
82b87bb
Merge branch 'jp/strengthen-annotation' into jp/response-builder
Apr 2, 2021
2125106
Merge branch 'jp/marian-decoder-new-output-collector' into jp/respons…
Apr 2, 2021
cb48c79
Remove marian entities from Response
Apr 2, 2021
62d022c
Sanitize Request constructor and document properly
Apr 2, 2021
512cc3d
See the struggle turning autoformat off and prettifying diff ugghhh
Apr 2, 2021
b63334b
Improve Response documentation
Apr 2, 2021
14881dd
Fixes to documentation
Apr 2, 2021
e388963
Further improves docs, makes docs more doxygen compatible
Apr 2, 2021
26ed203
Improve docs for Service external API
Apr 2, 2021
42e1ddc
Doxygen prettify Service and Request documentation
Apr 2, 2021
8f2e84e
Constructs to make Response members optional
Apr 2, 2021
41a27c5
Documentation adjustments
Apr 2, 2021
b83026d
Set default options with sane values
Apr 2, 2021
b8c1932
Reduce redundancy; default is thin Response
Apr 2, 2021
6476519
Service: Minor doc adjust
Apr 2, 2021
a5501da
We accept TranslationRequest now, and vector<text>
Apr 3, 2021
487bfe7
Adding comments to flow, some doc improvements
Apr 3, 2021
32285aa
Update TranslationModel to boost speed
Apr 3, 2021
c053d2e
First draft of ResponseBuilder
Mar 31, 2021
62cbd5e
Draft implementation builds
Mar 31, 2021
f1a3f79
Duplicating functions for surgery at Service
Mar 31, 2021
e7cb458
Cut Service, Request open to move ResponseBuilder in
Mar 31, 2021
99a4667
Surgery works! No more broken promises!
Mar 31, 2021
773f721
Probably overdoing appendSentence on annotation
Mar 31, 2021
828c6da
Transplant: ResponseBuilder new flow in
Mar 31, 2021
a4be691
Remove marian entities from Response
Apr 2, 2021
e7197a9
Sanitize Request constructor and document properly
Apr 2, 2021
ed9b83f
See the struggle turning autoformat off and prettifying diff ugghhh
Apr 2, 2021
583c343
Improve Response documentation
Apr 2, 2021
4a43662
Fixes to documentation
Apr 2, 2021
2932d53
Further improves docs, makes docs more doxygen compatible
Apr 2, 2021
b79c1bc
Improve docs for Service external API
Apr 2, 2021
c884e51
Doxygen prettify Service and Request documentation
Apr 2, 2021
57d1d26
Constructs to make Response members optional
Apr 2, 2021
f15b717
Documentation adjustments
Apr 2, 2021
fafce99
Set default options with sane values
Apr 2, 2021
fd9d2c4
Reduce redundancy; default is thin Response
Apr 2, 2021
c7400f6
Service: Minor doc adjust
Apr 2, 2021
5bf29c1
We accept TranslationRequest now, and vector<text>
Apr 3, 2021
583fce6
Adding comments to flow, some doc improvements
Apr 3, 2021
3fb44bd
Update TranslationModel to boost speed
Apr 3, 2021
d0a907e
Updating marian-dev, looks like submodules are a pain
Apr 7, 2021
52e0095
Merge branch 'main' into jp/response-builder
Apr 15, 2021
2929de6
Merge branch 'main' into jp/response-builder
Apr 16, 2021
b5363b8
Merge branch 'main' into jp/response-builder
Apr 21, 2021
dfde262
Merge branch 'jp/response-builder' of github.com:browsermt/bergamot-t…
Apr 21, 2021
0dc0cae
Printer: Consolidate printing Response utils at one place
Apr 21, 2021
666b15d
Adding print_utils files
Apr 21, 2021
dbad655
cleaning a few unnecessary functions
Apr 21, 2021
f7c82e3
Removing now empty response.cpp
Apr 22, 2021
c2f8790
Merge branch 'main' into jp/response-builder
Apr 22, 2021
01aeaa0
Revert "cleaning a few unnecessary functions"
Apr 22, 2021
6abad1c
Revert "Adding print_utils files"
Apr 22, 2021
dcf8491
Revert "Printer: Consolidate printing Response utils at one place"
Apr 22, 2021
7b1f18a
Merge branch 'jp/response-builder' of github.com:browsermt/bergamot-t…
Apr 22, 2021
4a83104
Merge branch 'main' into jp/response-builder
Apr 22, 2021
ca48e9b
Adding ResponseOptions to service-cli-bytearray
Apr 22, 2021
ca9e6c5
Undoing service-cli autoformat
Apr 23, 2021
6dd5f29
undoing style stuff on service.cpp
Apr 23, 2021
ee398d8
ResponseOptions is now a struct with default construction
Apr 26, 2021
959be6c
Adding responseOptions.h file
Apr 26, 2021
bd0e20f
Adding documentation
Apr 26, 2021
d000d8a
Elaborating alignmentThreshold documentation
Apr 26, 2021
e33a22e
Remove move constructor; Using default
Apr 26, 2021
7c8157b
Remove constructors from Response
Apr 26, 2021
7a5acbb
Merge branch 'main' into jp/response-builder
Apr 26, 2021
8a61d79
C++ style initializer list for struct
Apr 26, 2021
b8b1b19
Manually fixing merge submodule weirdness
Apr 26, 2021
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
8 changes: 7 additions & 1 deletion app/service-cli-bytearray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ int main(int argc, char *argv[]) {
std::string input = std_input.str();
using marian::bergamot::Response;

marian::Ptr<marian::Options> responseOptions = marian::New<marian::Options>();
jerinphilip marked this conversation as resolved.
Show resolved Hide resolved
responseOptions->set<bool>("quality", true);
responseOptions->set<bool>("alignment", true);
responseOptions->set<float>("alignment-threshold", 0.2f);

// Wait on future until Response is complete
std::future<Response> responseFuture = service.translate(std::move(input));
std::future<Response> responseFuture =
jerinphilip marked this conversation as resolved.
Show resolved Hide resolved
service.translate(std::move(input), responseOptions);
responseFuture.wait();
Response response = responseFuture.get();

Expand Down
8 changes: 7 additions & 1 deletion app/service-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ int main(int argc, char *argv[]) {
std::string input = std_input.str();
using marian::bergamot::Response;

marian::Ptr<marian::Options> responseOptions = marian::New<marian::Options>();
jerinphilip marked this conversation as resolved.
Show resolved Hide resolved
responseOptions->set<bool>("quality", true);
responseOptions->set<bool>("alignment", true);
responseOptions->set<float>("alignment-threshold", 0.2f);

// Wait on future until Response is complete
std::future<Response> responseFuture = service.translate(std::move(input));
std::future<Response> responseFuture =
service.translate(std::move(input), responseOptions);
responseFuture.wait();
Response response = responseFuture.get();

Expand Down
2 changes: 1 addition & 1 deletion src/translator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ add_library(bergamot-translator STATIC
batch_translator.cpp
request.cpp
batcher.cpp
response.cpp
response_builder.cpp
batch.cpp
sentence_ranges.cpp
service.cpp
Expand Down
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
5 changes: 4 additions & 1 deletion src/translator/batch_translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ void BatchTranslator::translate(Batch &batch) {
std::vector<data::SentenceTuple> batchVector;

auto &sentences = batch.sentences();
size_t batchSequenceNumber{0};
for (auto &sentence : sentences) {
data::SentenceTuple sentence_tuple(sentence.lineNumber());
data::SentenceTuple sentence_tuple(batchSequenceNumber);
Segment segment = sentence.getUnderlyingSegment();
sentence_tuple.push_back(segment);
batchVector.push_back(sentence_tuple);

++batchSequenceNumber;
}

size_t batchSize = batchVector.size();
Expand Down
26 changes: 7 additions & 19 deletions src/translator/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ namespace marian {
namespace bergamot {

// -----------------------------------------------------------------
Request::Request(size_t Id, size_t lineNumberBegin,
std::vector<Ptr<Vocab const>> &vocabs, AnnotatedText &&source,
Segments &&segments, std::promise<Response> responsePromise)
: Id_(Id), lineNumberBegin_(lineNumberBegin), vocabs_(&vocabs),
source_(std::move(source)), segments_(std::move(segments)),
response_(std::move(responsePromise)) {
Request::Request(size_t Id, Segments &&segments,
ResponseBuilder &&responseBuilder)
: Id_(Id), segments_(std::move(segments)),
responseBuilder_(std::move(responseBuilder))

{

counter_ = segments_.size();
histories_.resize(segments_.size(), nullptr);
}

size_t Request::lineNumberBegin() const { return lineNumberBegin_; }
size_t Request::numSegments() const { return segments_.size(); }

size_t Request::segmentTokens(size_t index) const {
Expand All @@ -39,17 +38,10 @@ void Request::processHistory(size_t index, Ptr<History> history) {
// In case this is last request in, completeRequest is called, which sets the
// value of the promise.
if (--counter_ == 0) {
completeRequest();
responseBuilder_(std::move(histories_));
}
}

void Request::completeRequest() {
// Request no longer needs to hold the content, can transfer it to
// Response.
Response response(std::move(source_), std::move(histories_), *vocabs_);
response_.set_value(std::move(response));
}

bool Request::operator<(const Request &b) const {
// Among Requests, only sequence id is used for obtaining priority.
return Id_ < b.Id_;
Expand All @@ -64,10 +56,6 @@ size_t RequestSentence::numTokens() const {
return (request_->segmentTokens(index_));
}

size_t RequestSentence::lineNumber() const {
return (request_->lineNumberBegin() + index_);
}

void RequestSentence::completeSentence(Ptr<History> history) {
// Relays completeSentence into request's processHistory, using index
// information.
Expand Down
121 changes: 61 additions & 60 deletions src/translator/request.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
//
// Defines:
//
// Request: holds the input text of a text, Segments (vector<Words>) which are
// to go to the batching mechanism and alignments between the processed
// segments and the input text (sourceTokenRanges). In addition, Request takes
// care of the barrier which fires when all the Segments in a request are done
// translating by the workers (BatchTranslator).
// TODO(jerinphilip): Extend Request with notions of Priority (sequence,
// user-given).
//
// RequestSentence: is a tuple of (index, Ptr<Request>). This provides the
// batching mechanism access to the segment within the request. The backref to
// Request allows event triggering the barrier upon completion of the last
// sentence by a worker.

#ifndef SRC_BERGAMOT_REQUEST_H_
#define SRC_BERGAMOT_REQUEST_H_

#include "definitions.h"
#include "response.h"
#include "response_builder.h"
#include "sentence_ranges.h"

#include "common/logging.h"
Expand All @@ -33,80 +18,96 @@
namespace marian {
namespace bergamot {

/// A Request is an internal representation used to represent a request after
/// processed by TextProcessor into sentences constituted by marian::Words.
///
/// The batching mechanism (Batcher) draws from multiple Requests and compiles
/// sentences into a batch. When a batch completes translation (at
/// BatchTranslator, intended in a different thread), backward propogation
/// happens through:
///
/// ```cpp
/// Batch::completeBatch(...)
/// -> RequestSentence::completeSentence(..)
/// -> Request::processHistory(...)
/// ```
///
/// When all sentences in a Request are completed, responseBuilder is
/// triggered with the compiled Histories, to construct the Response
/// corresponding to the Request and set value of the promise which triggers the
/// future at client.
class Request {
public:
Request(size_t Id, size_t lineNumberBegin,
std::vector<Ptr<Vocab const>> &vocabs_, AnnotatedText &&source,
Segments &&segments, std::promise<Response> responsePromise);

// Obtain the count of tokens in the segment correponding to index. Used to
// insert sentence from multiple requests into the corresponding size bucket.
/// Constructs an internal representation of the Request identified by Id,
/// processed Segments and accepts a callback (ResponseBuilder) which builds
/// the Response upon completion of the Request.
///
///
/// @param [in] Id: Identifier assigned to Request by Service.
/// @param [in] segments: Each segment is a unit to be translated.
/// @param [in] responseBuilder: Callback function (of ResponseBuilder type)
/// to be triggered upon the completion of translation of all units in a
/// Request.
Request(size_t Id, Segments &&segments, ResponseBuilder &&responseBuilder);

/// Obtain the count of tokens in the segment correponding to index. Used to
/// insert sentence from multiple requests into the corresponding size bucket.
size_t segmentTokens(size_t index) const;

// Obtain number of segments in a request.
/// Obtain number of segments in a request.
size_t numSegments() const;
size_t lineNumberBegin() const;

// Obtains segment corresponding to index to create a batch of segments among
// several requests.
/// Obtains segment corresponding to index to create a batch of segments
/// among several requests.
Segment getSegment(size_t index) const;

// For notions of priority among requests, used to enable std::set in
// Batcher.
/// For notions of priority among requests, used to enable std::set in
/// Batcher.
bool operator<(const Request &request) const;

// Processes a history obtained after translating in a heterogenous batch
// compiled from requests.
/// Processes a history obtained after translating in a heterogenous batch
/// compiled from requests.
void processHistory(size_t index, Ptr<History> history);

// On completion of last segment, sets value of the promise.
void completeRequest();

private:
size_t Id_;
size_t lineNumberBegin_;

// Multiple translation-workers can concurrently access the same Request. The
// following atomic atomically operates on the variable holding sentences
// remaining to be translated.
/// Multiple translation-workers can concurrently access the same Request. The
/// following atomic atomically operates on the variable holding sentences
/// remaining to be translated.
std::atomic<int> counter_;

// source_ holds the source string to be translated. segments_ hold the
// sentences generated from source_ in vector<Words>. sourceRanges_ are
// string_views of the text corresponding to these words, pointing to
// sequences in source_. histories_ is a buffer which eventually stores the
// translations of each segment in the corresponding index.
AnnotatedText source_;
/// segments_ hold the sentences processed into Words which generated from
/// input string.
Segments segments_;
std::vector<Ptr<History>> histories_;

// Members above are moved into newly constructed Response on completion
// of translation of all segments. The promise below is set to this Response
// value. future to this promise is made available to the user through
// Service.
std::promise<Response> response_;
/// histories_ is a buffer which eventually stores the translations of each
/// segment in the corresponding index.
std::vector<Ptr<History>> histories_;

// Constructing Response requires the vocabs_ used to generate Request.
std::vector<Ptr<Vocab const>> *vocabs_;
/// Constructing Response requires the vocabs_ used to generate Request.
/// std::vector<Ptr<Vocab const>> *vocabs_;
ResponseBuilder responseBuilder_;
};

/// A RequestSentence provides a view to a sentence within a Request. Existence
/// of this class allows the sentences and associated information to be kept
/// within Request, while batching mechanism (Batcher) compiles Batch from
/// RequestSentence-s coming from different Requests.
class RequestSentence {
// A RequestSentence provides a view to a sentence within a Request. Existence
// of this class allows the sentences and associated information to be kept
// within Request.

public:
RequestSentence(size_t, Ptr<Request>);
size_t numTokens() const;

// lineNumber in Request, used for matching marian-decoder. SentenceTuple
// requires lineNumber to be set for Corpus based batches.
size_t lineNumber() const;
/// Number of tokens in the segment this RequestSentence represents. Used to
/// order by length in batching.
size_t numTokens() const;

// Accessor to the segment represented by the RequestSentence.
/// Accessor to the segment represented by the RequestSentence.
Segment getUnderlyingSegment() const;

// Forwards call to Request, checking for completion.
/// Forwards history to Request to set history corresponding to this
/// RequestSentence.
void completeSentence(Ptr<History> history);

friend bool operator<(const RequestSentence &a, const RequestSentence &b);
Expand Down
Loading