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

WASM Bindings collapse #87

Merged
merged 8 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 7 additions & 6 deletions app/bergamot-translator-app-bytearray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include <iostream>

#include "TranslationModel.h"
#include "translator/parser.h"
#include "translator/byte_array_util.h"
#include "translator/parser.h"
#include "translator/service.h"

int main(int argc, char **argv) {

Expand All @@ -20,19 +20,20 @@ int main(int argc, char **argv) {
std::string config = options->asYamlString();

// Route the config string to construct marian model through TranslationModel
TranslationModel model(config, marian::bergamot::getModelMemoryFromConfig(options));
marian::bergamot::Service model(
config, marian::bergamot::getModelMemoryFromConfig(options));
qianqianzhu marked this conversation as resolved.
Show resolved Hide resolved

TranslationRequest translationRequest;
std::vector<std::string> texts;

for (std::string line; std::getline(std::cin, line);) {
texts.emplace_back(line);
texts.emplace_back(line);
}

auto results = model.translate(std::move(texts), translationRequest);
auto results = model.translateMultiple(std::move(texts), translationRequest);

// Resolve the future and get the actual result
//std::vector<TranslationResult> results = futureResults.get();
// std::vector<TranslationResult> results = futureResults.get();
jerinphilip marked this conversation as resolved.
Show resolved Hide resolved

for (auto &result : results) {
std::cout << result.getTranslatedText() << std::endl;
Expand Down
15 changes: 8 additions & 7 deletions app/bergamot-translator-app.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/*
* main.cpp
*
* An application which accepts line separated texts in stdin and returns translated ones in stdout.
* It is convenient for batch processing and can be used with tools like SacreBLEU.
* An application which accepts line separated texts in stdin and returns
* translated ones in stdout. It is convenient for batch processing and can be
* used with tools like SacreBLEU.
*
*/

#include <iostream>
#include <string>

#include "TranslationModel.h"
#include "translator/parser.h"
#include "translator/service.h"

int main(int argc, char **argv) {

Expand All @@ -21,19 +22,19 @@ int main(int argc, char **argv) {
std::string config = options->asYamlString();

// Route the config string to construct marian model through TranslationModel
TranslationModel model(config);
marian::bergamot::Service model(config);

TranslationRequest translationRequest;
std::vector<std::string> texts;

for (std::string line; std::getline(std::cin, line);) {
texts.emplace_back(line);
texts.emplace_back(line);
}

auto results = model.translate(std::move(texts), translationRequest);
auto results = model.translateMultiple(std::move(texts), translationRequest);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saying. This change will require a change in the extension code otherwise extension code will not work out of box.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't I aliased translate -> Service::translateMultiple in bindings? I'm hoping this is the change you're talking about.

Build is failing in Extension's CircleCI (https://app.circleci.com/pipelines/github/mozilla-extensions/bergamot-browser-extension/204/workflows/ab129537-ad34-403b-b31d-40c626c19707/jobs/206/steps). But I'm guessing these are expected due to the issues discussed in mozilla-extensions/firefox-translations#83.

Can you confirm?

Copy link
Contributor Author

@jerinphilip jerinphilip Apr 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well manually downloaded XPI, seems working. Edit: I take that back, didn't overwrite old extension, oops. ^_^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerinphilip We can test it with the wasm test page because that is using the latest byte based API. If it works with that then we are good to go 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

I have no clue what is happening, but I am assuming bindings transfer is intact, given I/O works?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerinphilip We can test it with the wasm test page because that is using the latest byte based API. If it works with that then we are good to go 👍

@jerinphilip Let me help you with testing. I will test it at my end and confirm 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerinphilip I tested your change with the fix in #125 and it works 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pulled #125 into a test build, still translates to "schwanger". I guess the transfer is intact from your response and this is something to resolve at my end maybe.


// Resolve the future and get the actual result
//std::vector<TranslationResult> results = futureResults.get();
// std::vector<TranslationResult> results = futureResults.get();
jerinphilip marked this conversation as resolved.
Show resolved Hide resolved

for (auto &result : results) {
std::cout << result.getTranslatedText() << std::endl;
Expand Down
80 changes: 0 additions & 80 deletions src/TranslationModel.h

This file was deleted.

108 changes: 0 additions & 108 deletions src/TranslationResult.h

This file was deleted.

1 change: 0 additions & 1 deletion src/translator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
add_library(bergamot-translator STATIC
TranslationModel.cpp
byte_array_util.cpp
text_processor.cpp
sentence_splitter.cpp
Expand Down
50 changes: 0 additions & 50 deletions src/translator/TranslationModel.cpp

This file was deleted.

4 changes: 4 additions & 0 deletions src/translator/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ struct Response {
/// sparse matrix representation with indices corresponding
/// to (sub-)words accessible through Annotation.
std::vector<Alignment> alignments;

const std::string &getOriginalText() const { return source.text; }

const std::string &getTranslatedText() const { return target.text; }
};
} // namespace bergamot
} // namespace marian
Expand Down
4 changes: 2 additions & 2 deletions src/translator/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ loadVocabularies(marian::Ptr<marian::Options> options) {
namespace marian {
namespace bergamot {

Service::Service(Ptr<Options> options, AlignedMemory modelMemory, AlignedMemory shortlistMemory)
: requestId_(0), vocabs_(std::move(loadVocabularies(options))),
Service::Service(Ptr<Options> options, AlignedMemory modelMemory, AlignedMemory shortlistMemory)
: requestId_(0), options_(options), vocabs_(std::move(loadVocabularies(options))),
text_processor_(vocabs_, options), batcher_(options),
numWorkers_(options->get<int>("cpu-threads")),
modelMemory_(std::move(modelMemory)), shortlistMemory_(std::move(shortlistMemory))
Expand Down
10 changes: 10 additions & 0 deletions src/translator/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ class Service {
translateMultiple(std::vector<std::string> &&source,
TranslationRequest translationRequest);

/// Returns if model is alignment capable or not. Exists for not breaking
/// things at the extension.
bool isAlignmentSupported() const {
return options_->hasAndNotEmpty("alignment");
}

private:
/// Queue an input for translation.
std::future<Response> queueRequest(std::string &&input,
Expand All @@ -149,6 +155,10 @@ class Service {

/// Number of workers to launch.
size_t numWorkers_; // ORDER DEPENDENCY (pcqueue_)

/// Options object holding the options Service was instantiated with.
Ptr<Options> options_;

/// Model memory to load model passed as bytes.
AlignedMemory modelMemory_; // ORDER DEPENDENCY (translators_)
/// Shortlist memory passed as bytes.
Expand Down
Loading