Skip to content

Commit

Permalink
Constructs to make Response members optional
Browse files Browse the repository at this point in the history
Towards #77.

Brings in the concept of ResponseOptions, which should be the intended
outcome of TranslationRequest, providing additional options
along with the source string describing how to construct Response.

It's currently typedef-ed to Ptr<marian::Options>, and subject to change
if and when UnifiedAPI can bring it's own proper equivalent.

Through options, ResponseBuilder now has if-guards to construct
QualityScores or Alignments making it a no-op if the options are unset,
bringing in the desired feature, albeit with a reinvented wheel
structure which is not improper.
  • Loading branch information
Jerin Philip committed Apr 2, 2021
1 parent 42e1ddc commit 8f2e84e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/translator/response_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ void ResponseBuilder::buildAlignments(Histories &histories,
// mean WASM bindings for a structure deep within marian source.
auto hyp = std::get<1>(result);
auto softAlignment = hyp->tracebackAlignment();
auto hardAlignment = data::ConvertSoftAlignToHardAlign(
softAlignment, /*threshold=*/0.2f); // TODO(jerinphilip): Make this
// a configurable parameter.

auto threshold = responseOptions_->get<float>("alignment-threshold", 0.2f);
auto hardAlignment =
data::ConvertSoftAlignToHardAlign(softAlignment, threshold);
Alignment unified_alignment;
for (auto &p : hardAlignment) {
unified_alignment.emplace_back((Point){p.srcPos, p.tgtPos, p.prob});
Expand Down
31 changes: 19 additions & 12 deletions src/translator/response_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// For now we will work with this, to avoid complaints another structure is hard
// to operate with.

typedef marian::Ptr<marian::Options> RequestParams;
// typedef TranslationRequest RequestParams;
typedef marian::Ptr<marian::Options> ResponseOptions;
// typedef TranslationRequest ResponseOptions;

namespace marian {
namespace bergamot {
Expand All @@ -21,23 +21,24 @@ namespace bergamot {

class ResponseBuilder {
public:
/// @param [in] params: RequestParams, also an alias for TranslationRequest
/// @param [in] responseOptions: ResponseOptions, indicating what to include
/// or not in the response and any additional configurable parameters.
/// @param [in] vocabs: marian vocab object (used in decoding)
// @param [in] promise: promise to set with the constructed Response
ResponseBuilder(RequestParams params, AnnotatedText &&source,
/// @param [in] promise: promise to set with the constructed Response.
ResponseBuilder(ResponseOptions responseOptions, AnnotatedText &&source,
std::vector<Ptr<Vocab const>> &vocabs,
std::promise<Response> &&promise)
: params_(params), source_(std::move(source)), vocabs_(&vocabs),
promise_(std::move(promise)) {}
: responseOptions_(responseOptions), source_(std::move(source)),
vocabs_(&vocabs), promise_(std::move(promise)) {}

/// Constructs and sets the promise of a Response object from obtained
/// histories after translating.
/// @param [in] histories: Histories obtained after translating the Request
/// from which this functor is called.
void operator()(Histories &&histories) {
// TODO(jerinphilip) load RequestParams into options and turn build
// TODO(jerinphilip) load ResponseOptions into options and turn build
// functions on or off.
// params_ is unused, but we can try something here.
// responseOptions_ is unused, but we can try something here.
ABORT_IF(source_.numSentences() != histories.size(),
"Mismatch in source and translated sentences");
Response response;
Expand All @@ -49,9 +50,15 @@ class ResponseBuilder {
buildTranslatedText(histories, response);

// Should always be after buildTranslatedText
buildQualityScores(histories, response);
bool quality = responseOptions_->get<bool>("quality");
if (quality) {
buildQualityScores(histories, response);
}

buildAlignments(histories, response);
bool alignment = responseOptions_->get<bool>("alignment");
if (alignment) {
buildAlignments(histories, response);
}

// Once complete, set promise.
promise_.set_value(std::move(response));
Expand All @@ -77,7 +84,7 @@ class ResponseBuilder {

// Data members are context/curried args for the functor.

RequestParams params_;
ResponseOptions responseOptions_;
std::vector<Ptr<Vocab const>> *vocabs_; // vocabs are required for decoding
// and any source validation checks.
std::promise<Response> promise_; // To be set when callback triggered and
Expand Down
16 changes: 14 additions & 2 deletions src/translator/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,27 @@ void Service::async_translate() {
#endif // WASM_COMPATIBLE_SOURCE

std::future<Response> Service::translate(std::string &&input) {
ResponseOptions responseOptions =
New<Options>(); // TODO(jerinphilip): Take this in as argument

// Hardcode responseOptions for now
responseOptions->set<bool>("quality", true);
responseOptions->set<bool>("alignment", true);
responseOptions->set<float>("alignment-threshold", 0.2f);
return translateWithOptions(std::move(input), responseOptions);
}

std::future<Response>
Service::translateWithOptions(std::string &&input,
ResponseOptions responseOptions) {
Segments segments;
AnnotatedText source(std::move(input));
text_processor_.process(source, segments);

std::promise<Response> responsePromise;
auto future = responsePromise.get_future();

RequestParams requestParams; // TODO(jerinphilip): Take this in as argument
ResponseBuilder responseBuilder(requestParams, std::move(source), vocabs_,
ResponseBuilder responseBuilder(responseOptions, std::move(source), vocabs_,
std::move(responsePromise));
Ptr<Request> request = New<Request>(requestId_++, std::move(segments),
std::move(responseBuilder));
Expand Down
15 changes: 13 additions & 2 deletions src/translator/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,19 @@ class Service {
/// To stay efficient and to refer to the string for alignments, expects
/// ownership be moved through std::move(..)
///
/// @param [in] rvalue reference of string to be translated.
std::future<Response> translate(std::string &&input);
/// @param [in] source: rvalue reference of string to be translated.
std::future<Response> translate(std::string &&source);

/// Translate an input, providing Options to construct Response. This is
/// 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] responseOptions: Options indicating whether or not to include
/// in the Response

std::future<Response> translateWithOptions(std::string &&source,
ResponseOptions options);

private:
/// Build numTranslators number of translators with options from options
Expand Down

0 comments on commit 8f2e84e

Please sign in to comment.