-
Notifications
You must be signed in to change notification settings - Fork 37
/
bergamot.cpp
213 lines (178 loc) · 8.57 KB
/
bergamot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <pybind11/iostream.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <translator/annotation.h>
#include <translator/parser.h>
#include <translator/project_version.h>
#include <translator/response.h>
#include <translator/response_options.h>
#include <translator/service.h>
#include <translator/translation_model.h>
#include <iostream>
#include <string>
#include <vector>
namespace py = pybind11;
using marian::bergamot::AnnotatedText;
using marian::bergamot::ByteRange;
using marian::bergamot::ConcatStrategy;
using marian::bergamot::Response;
using marian::bergamot::ResponseOptions;
using Service = marian::bergamot::AsyncService;
using _Model = marian::bergamot::TranslationModel;
using Model = std::shared_ptr<_Model>;
using Alignment = std::vector<std::vector<float>>;
using Alignments = std::vector<Alignment>;
PYBIND11_MAKE_OPAQUE(std::vector<Response>);
PYBIND11_MAKE_OPAQUE(std::vector<std::string>);
PYBIND11_MAKE_OPAQUE(Alignments);
class ServicePyAdapter {
public:
ServicePyAdapter(const Service::Config &config) : service_(make_service(config)) {
// Set marian to throw exceptions instead of std::abort()
marian::setThrowExceptionOnAbort(true);
}
std::shared_ptr<_Model> modelFromConfig(const std::string &config) {
auto parsedConfig = marian::bergamot::parseOptionsFromString(config);
return service_.createCompatibleModel(parsedConfig);
}
std::shared_ptr<_Model> modelFromConfigPath(const std::string &configPath) {
auto config = marian::bergamot::parseOptionsFromFilePath(configPath);
return service_.createCompatibleModel(config);
}
std::vector<Response> translate(Model model, std::vector<std::string> &inputs, const ResponseOptions &options) {
py::scoped_ostream_redirect outstream(std::cout, // std::ostream&
py::module_::import("sys").attr("stdout") // Python output
);
py::scoped_ostream_redirect errstream(std::cerr, // std::ostream&
py::module_::import("sys").attr("stderr") // Python output
);
py::call_guard<py::gil_scoped_release> gil_guard;
// Prepare promises, save respective futures. Have callback's in async set
// value to the promises.
std::vector<std::future<Response>> futures;
std::vector<std::promise<Response>> promises;
promises.resize(inputs.size());
for (size_t i = 0; i < inputs.size(); i++) {
auto callback = [&promises, i](Response &&response) { promises[i].set_value(std::move(response)); };
service_.translate(model, std::move(inputs[i]), std::move(callback), options);
futures.push_back(std::move(promises[i].get_future()));
}
// Wait on all futures to be ready.
std::vector<Response> responses;
for (size_t i = 0; i < futures.size(); i++) {
futures[i].wait();
responses.push_back(std::move(futures[i].get()));
}
return responses;
}
std::vector<Response> pivot(Model first, Model second, std::vector<std::string> &inputs,
const ResponseOptions &options) {
py::scoped_ostream_redirect outstream(std::cout, // std::ostream&
py::module_::import("sys").attr("stdout") // Python output
);
py::scoped_ostream_redirect errstream(std::cerr, // std::ostream&
py::module_::import("sys").attr("stderr") // Python output
);
py::call_guard<py::gil_scoped_release> gil_guard;
// Prepare promises, save respective futures. Have callback's in async set
// value to the promises.
std::vector<std::future<Response>> futures;
std::vector<std::promise<Response>> promises;
promises.resize(inputs.size());
for (size_t i = 0; i < inputs.size(); i++) {
auto callback = [&promises, i](Response &&response) { promises[i].set_value(std::move(response)); };
service_.pivot(first, second, std::move(inputs[i]), std::move(callback), options);
futures.push_back(std::move(promises[i].get_future()));
}
// Wait on all futures to be ready.
std::vector<Response> responses;
for (size_t i = 0; i < futures.size(); i++) {
futures[i].wait();
responses.push_back(std::move(futures[i].get()));
}
return responses;
}
private /*functions*/:
static Service make_service(const Service::Config &config) {
py::scoped_ostream_redirect outstream(std::cout, // std::ostream&
py::module_::import("sys").attr("stdout") // Python output
);
py::scoped_ostream_redirect errstream(std::cerr, // std::ostream&
py::module_::import("sys").attr("stderr") // Python output
);
py::call_guard<py::gil_scoped_release> gil_guard;
return Service(config);
}
private /*data*/:
Service service_;
};
PYBIND11_MODULE(_bergamot, m) {
m.doc() = "Bergamot pybind11 bindings";
m.attr("__version__") = marian::bergamot::bergamotBuildVersion();
py::class_<ByteRange>(m, "ByteRange")
.def(py::init<>())
.def_readonly("begin", &ByteRange::begin)
.def_readonly("end", &ByteRange::end)
.def("__repr__", [](const ByteRange &range) {
return "{" + std::to_string(range.begin) + ", " + std::to_string(range.end) + "}";
});
py::class_<AnnotatedText>(m, "AnnotatedText")
.def(py::init<>())
.def("numWords", &AnnotatedText::numWords)
.def("numSentences", &AnnotatedText::numSentences)
.def("word",
[](const AnnotatedText &annotatedText, size_t sentenceIdx, size_t wordIdx) -> std::string {
auto view = annotatedText.word(sentenceIdx, wordIdx);
return std::string(view.data(), view.size());
})
.def("sentence",
[](const AnnotatedText &annotatedText, size_t sentenceIdx) -> std::string {
auto view = annotatedText.sentence(sentenceIdx);
return std::string(view.data(), view.size());
})
.def("wordAsByteRange", &AnnotatedText::wordAsByteRange)
.def("sentenceAsByteRange", &AnnotatedText::sentenceAsByteRange)
.def_readonly("text", &AnnotatedText::text);
py::class_<Response>(m, "Response")
.def(py::init<>())
.def_readonly("source", &Response::source)
.def_readonly("target", &Response::target)
.def_readonly("alignments", &Response::alignments);
py::bind_vector<std::vector<std::string>>(m, "VectorString");
py::bind_vector<std::vector<Response>>(m, "VectorResponse");
py::enum_<ConcatStrategy>(m, "ConcatStrategy")
.value("FAITHFUL", ConcatStrategy::FAITHFUL)
.value("SPACE", ConcatStrategy::SPACE)
.export_values();
py::class_<ResponseOptions>(m, "ResponseOptions")
.def(
py::init<>([](bool qualityScores, bool alignment, bool HTML, bool sentenceMappings, ConcatStrategy strategy) {
return ResponseOptions{qualityScores, alignment, HTML, sentenceMappings, strategy};
}),
py::arg("qualityScores") = true, py::arg("alignment") = false, py::arg("HTML") = false,
py::arg("sentenceMappings") = true, py::arg("concatStrategy") = ConcatStrategy::FAITHFUL)
.def_readwrite("qualityScores", &ResponseOptions::qualityScores)
.def_readwrite("HTML", &ResponseOptions::HTML)
.def_readwrite("alignment", &ResponseOptions::alignment)
.def_readwrite("concatStrategy", &ResponseOptions::concatStrategy)
.def_readwrite("sentenceMappings", &ResponseOptions::sentenceMappings);
py::class_<ServicePyAdapter>(m, "Service")
.def(py::init<const Service::Config &>())
.def("modelFromConfig", &ServicePyAdapter::modelFromConfig)
.def("modelFromConfigPath", &ServicePyAdapter::modelFromConfigPath)
.def("translate", &ServicePyAdapter::translate)
.def("pivot", &ServicePyAdapter::pivot);
py::class_<Service::Config>(m, "ServiceConfig")
.def(py::init<>([](size_t numWorkers, size_t cacheSize, std::string logging) {
Service::Config config;
config.numWorkers = numWorkers;
config.cacheSize = cacheSize;
config.logger.level = logging;
return config;
}),
py::arg("numWorkers") = 1, py::arg("cacheSize") = 0, py::arg("logLevel") = "off")
.def_readwrite("numWorkers", &Service::Config::numWorkers)
.def_readwrite("cacheSize", &Service::Config::cacheSize);
py::class_<_Model, std::shared_ptr<_Model>>(m, "TranslationModel");
}