Skip to content

Commit

Permalink
Safe transfer of bindings through typedefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerin Philip committed Apr 29, 2021
1 parent de0abfd commit fcc285e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
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
10 changes: 8 additions & 2 deletions wasm/bindings/TranslationModelBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

#include <emscripten/bind.h>

#include "TranslationModel.h"
#include "response.h"
#include "service.h"

using namespace emscripten;

typedef marian::bergamot::Service TranslationModel;
typedef marian::bergamot::Response TranslationResult;

val getByteArrayView(marian::bergamot::AlignedMemory& alignedMemory) {
return val(typed_memory_view(alignedMemory.size(), alignedMemory.as<char>()));
}
Expand All @@ -31,9 +35,11 @@ TranslationModel* TranslationModelFactory(const std::string &config,
EMSCRIPTEN_BINDINGS(translation_model) {
class_<TranslationModel>("TranslationModel")
.constructor(&TranslationModelFactory, allow_raw_pointers())
.function("translate", &TranslationModel::translate)
.function("translate", &TranslationModel::translateMultiple)
.function("isAlignmentSupported", &TranslationModel::isAlignmentSupported)
;
// ^ We redirect Service::translateMultiple to WASMBound::translate instead. Sane API is
// translate. If and when async comes, we can be done with this inconsistency.

register_vector<std::string>("VectorString");
register_vector<TranslationResult>("VectorTranslationResult");
Expand Down
12 changes: 7 additions & 5 deletions wasm/bindings/TranslationResultBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
#include <emscripten/bind.h>
#include <vector>

#include "TranslationResult.h"
// #include "TranslationResult.h"
#include "response.h"

typedef marian::bergamot::Response TranslationResult;

using namespace emscripten;

// Binding code
EMSCRIPTEN_BINDINGS(translation_result) {
class_<TranslationResult>("TranslationResult")
.constructor<std::string, std::string, TranslationResult::SentenceMappings>()
.function("getOriginalText", &TranslationResult::getOriginalText)
.function("getTranslatedText", &TranslationResult::getTranslatedText)
;
.constructor<>()
.function("getOriginalText", &TranslationResult::getOriginalText)
.function("getTranslatedText", &TranslationResult::getTranslatedText);
}

0 comments on commit fcc285e

Please sign in to comment.