mirror of
https://github.com/browsermt/bergamot-translator.git
synced 2024-09-11 05:35:33 +03:00
Bundle AlignedMemory inputs with MemoryBundle (#147)
This commit is contained in:
parent
6c063c607e
commit
6c7e6156ab
@ -16,19 +16,15 @@ int main(int argc, char *argv[]) {
|
|||||||
auto cp = marian::bergamot::createConfigParser();
|
auto cp = marian::bergamot::createConfigParser();
|
||||||
auto options = cp.parseOptions(argc, argv, true);
|
auto options = cp.parseOptions(argc, argv, true);
|
||||||
|
|
||||||
// Prepare memories for model and shortlist
|
// Prepare memories for bytearrays (including model, shortlist and vocabs)
|
||||||
marian::bergamot::AlignedMemory modelBytes, shortlistBytes;
|
marian::bergamot::MemoryBundle memoryBundle;
|
||||||
std::vector<std::shared_ptr<marian::bergamot::AlignedMemory>> vocabsBytes;
|
|
||||||
|
|
||||||
if (options->get<bool>("check-bytearray")) {
|
if (options->get<bool>("check-bytearray")) {
|
||||||
// Load legit values into bytearrays.
|
// Load legit values into bytearrays.
|
||||||
modelBytes = marian::bergamot::getModelMemoryFromConfig(options);
|
memoryBundle = marian::bergamot::getMemoryBundleFromConfig(options);
|
||||||
shortlistBytes = marian::bergamot::getShortlistMemoryFromConfig(options);
|
|
||||||
marian::bergamot::getVocabsMemoryFromConfig(options, vocabsBytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
marian::bergamot::Service service(options, std::move(modelBytes),
|
marian::bergamot::Service service(options, std::move(memoryBundle));
|
||||||
std::move(shortlistBytes), std::move(vocabsBytes));
|
|
||||||
|
|
||||||
// Read a large input text blob from stdin
|
// Read a large input text blob from stdin
|
||||||
std::ostringstream std_input;
|
std::ostringstream std_input;
|
||||||
|
@ -117,5 +117,13 @@ void getVocabsMemoryFromConfig(marian::Ptr<marian::Options> options,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MemoryBundle getMemoryBundleFromConfig(marian::Ptr<marian::Options> options){
|
||||||
|
MemoryBundle memoryBundle;
|
||||||
|
memoryBundle.model = getModelMemoryFromConfig(options);
|
||||||
|
memoryBundle.shortlist = getShortlistMemoryFromConfig(options);
|
||||||
|
getVocabsMemoryFromConfig(options, memoryBundle.vocabs);
|
||||||
|
return memoryBundle;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace bergamot
|
} // namespace bergamot
|
||||||
} // namespace marian
|
} // namespace marian
|
||||||
|
@ -10,5 +10,6 @@ AlignedMemory getShortlistMemoryFromConfig(marian::Ptr<marian::Options> options)
|
|||||||
void getVocabsMemoryFromConfig(marian::Ptr<marian::Options> options,
|
void getVocabsMemoryFromConfig(marian::Ptr<marian::Options> options,
|
||||||
std::vector<std::shared_ptr<AlignedMemory>>& vocabMemories);
|
std::vector<std::shared_ptr<AlignedMemory>>& vocabMemories);
|
||||||
bool validateBinaryModel(const AlignedMemory& model, uint64_t fileSize);
|
bool validateBinaryModel(const AlignedMemory& model, uint64_t fileSize);
|
||||||
|
MemoryBundle getMemoryBundleFromConfig(marian::Ptr<marian::Options> options);
|
||||||
} // namespace bergamot
|
} // namespace bergamot
|
||||||
} // namespace marian
|
} // namespace marian
|
||||||
|
@ -15,6 +15,42 @@ typedef std::vector<Segment> Segments;
|
|||||||
/// Shortcut to AlignedVector<char> for byte arrays
|
/// Shortcut to AlignedVector<char> for byte arrays
|
||||||
typedef AlignedVector<char> AlignedMemory;
|
typedef AlignedVector<char> AlignedMemory;
|
||||||
|
|
||||||
|
/// Memory bundle for all byte-arrays.
|
||||||
|
/// Can be a set/subset of model, shortlist, vocabs and ssplitPrefixFile bytes.
|
||||||
|
struct MemoryBundle {
|
||||||
|
AlignedMemory model; ///< Byte-array of model (aligned to 256)
|
||||||
|
AlignedMemory shortlist; ///< Byte-array of shortlist (aligned to 64)
|
||||||
|
|
||||||
|
/// Vector of vocabulary memories (aligned to 64).
|
||||||
|
/// If two vocabularies are the same (based on the filenames), two entries (shared
|
||||||
|
/// pointers) will be generated which share the same AlignedMemory object.
|
||||||
|
std::vector<std::shared_ptr<AlignedMemory>> vocabs;
|
||||||
|
|
||||||
|
/// @todo Not implemented yet
|
||||||
|
AlignedMemory ssplitPrefixFile;
|
||||||
|
|
||||||
|
MemoryBundle() = default;
|
||||||
|
|
||||||
|
MemoryBundle(MemoryBundle &&from){
|
||||||
|
model = std::move(from.model);
|
||||||
|
shortlist = std::move(from.shortlist);
|
||||||
|
vocabs = std::move(vocabs);
|
||||||
|
ssplitPrefixFile = std::move(from.ssplitPrefixFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
MemoryBundle &operator=(MemoryBundle &&from) {
|
||||||
|
model = std::move(from.model);
|
||||||
|
shortlist = std::move(from.shortlist);
|
||||||
|
vocabs = std::move(vocabs);
|
||||||
|
ssplitPrefixFile = std::move(from.ssplitPrefixFile);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete copy constructors
|
||||||
|
MemoryBundle(const MemoryBundle&) = delete;
|
||||||
|
MemoryBundle& operator=(const MemoryBundle&) = delete;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace bergamot
|
} // namespace bergamot
|
||||||
} // namespace marian
|
} // namespace marian
|
||||||
|
|
||||||
|
@ -41,14 +41,13 @@ loadVocabularies(marian::Ptr<marian::Options> options,
|
|||||||
namespace marian {
|
namespace marian {
|
||||||
namespace bergamot {
|
namespace bergamot {
|
||||||
|
|
||||||
Service::Service(Ptr<Options> options, AlignedMemory modelMemory, AlignedMemory shortlistMemory,
|
Service::Service(Ptr<Options> options, MemoryBundle memoryBundle)
|
||||||
std::vector<std::shared_ptr<AlignedMemory>> vocabMemories)
|
|
||||||
: requestId_(0), options_(options),
|
: requestId_(0), options_(options),
|
||||||
vocabs_(std::move(loadVocabularies(options, std::move(vocabMemories)))),
|
vocabs_(std::move(loadVocabularies(options, std::move(memoryBundle.vocabs)))),
|
||||||
text_processor_(vocabs_, options), batcher_(options),
|
text_processor_(vocabs_, options), batcher_(options),
|
||||||
numWorkers_(options->get<int>("cpu-threads")),
|
numWorkers_(options->get<int>("cpu-threads")),
|
||||||
modelMemory_(std::move(modelMemory)),
|
modelMemory_(std::move(memoryBundle.model)),
|
||||||
shortlistMemory_(std::move(shortlistMemory))
|
shortlistMemory_(std::move(memoryBundle.shortlist))
|
||||||
#ifndef WASM_COMPATIBLE_SOURCE
|
#ifndef WASM_COMPATIBLE_SOURCE
|
||||||
// 0 elements in PCQueue is illegal and can lead to failures. Adding a
|
// 0 elements in PCQueue is illegal and can lead to failures. Adding a
|
||||||
// guard to have at least one entry allocated. In the single-threaded
|
// guard to have at least one entry allocated. In the single-threaded
|
||||||
|
@ -55,53 +55,29 @@ namespace bergamot {
|
|||||||
/// // Do things with response.
|
/// // Do things with response.
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Optionally Service can be initialized by also passing model memory for
|
/// Optionally Service can be initialized by also passing bytearray memories
|
||||||
/// purposes of efficiency (which defaults to nullpointer and then reads from
|
/// for purposes of efficiency (which defaults to empty and then reads from
|
||||||
/// file supplied through config).
|
/// file supplied through config).
|
||||||
///
|
///
|
||||||
class Service {
|
class Service {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/// Construct Service from Marian options. If memoryBundle is empty, Service is
|
||||||
|
/// initialized from file-based loading. Otherwise, Service is initialized from
|
||||||
|
/// the given bytearray memories.
|
||||||
/// @param options Marian options object
|
/// @param options Marian options object
|
||||||
/// @param modelMemory byte array (aligned to 256!!!) that contains the bytes
|
/// @param memoryBundle holds all byte-array memories. Can be a set/subset of
|
||||||
/// of a model.bin.
|
/// model, shortlist, vocabs and ssplitPrefixFile bytes. Optional.
|
||||||
/// @param shortlistMemory byte array of shortlist (aligned to 64)
|
explicit Service(Ptr<Options> options, MemoryBundle memoryBundle={});
|
||||||
/// @param vocabMemories vector of vocabulary memories (aligned to 64)
|
|
||||||
explicit Service(Ptr<Options> options, AlignedMemory modelMemory,
|
|
||||||
AlignedMemory shortlistMemory,
|
|
||||||
std::vector<std::shared_ptr<AlignedMemory>> vocabMemories);
|
|
||||||
|
|
||||||
/// Construct Service purely from Options. This expects options which
|
/// Construct Service from a string configuration. If memoryBundle is empty, Service is
|
||||||
/// marian-decoder expects to be set for loading model shortlist and
|
/// initialized from file-based loading. Otherwise, Service is initialized from
|
||||||
/// vocabularies from files in addition to parameters that set unset desired
|
/// the given bytearray memories.
|
||||||
/// features (e.g: alignments, quality-scores).
|
/// @param [in] config string parsable as YAML expected to adhere with marian config
|
||||||
///
|
/// @param [in] memoryBundle holds all byte-array memories. Can be a set/subset of
|
||||||
/// This is equivalent to a call to:
|
/// model, shortlist, vocabs and ssplitPrefixFile bytes. Optional.
|
||||||
/// ```cpp
|
explicit Service(const std::string &config, MemoryBundle memoryBundle={})
|
||||||
/// Service(options, AlignedMemory(), AlignedMemory(), {})
|
: Service(parseOptions(config, /*validate=*/false), std::move(memoryBundle)) {}
|
||||||
/// ```
|
|
||||||
/// wherein empty memory is passed and internal flow defaults to file-based
|
|
||||||
/// model, shortlist loading. AlignedMemory() corresponds to empty memory
|
|
||||||
explicit Service(Ptr<Options> options)
|
|
||||||
: Service(options, AlignedMemory(), AlignedMemory(), {}) {}
|
|
||||||
|
|
||||||
/// Construct Service from a string configuration.
|
|
||||||
/// @param [in] config string parsable as YAML expected to adhere with marian
|
|
||||||
/// config
|
|
||||||
/// @param [in] modelMemory byte array (aligned to 256!!!) that contains the
|
|
||||||
/// bytes of a model.bin. Optional. AlignedMemory() corresponds to empty memory
|
|
||||||
/// @param [in] shortlistMemory byte array of shortlist (aligned to 64). Optional.
|
|
||||||
/// @param [in] vocabMemories vector of vocabulary memories (aligned to 64). Optional.
|
|
||||||
/// If two vocabularies are the same (based on the filenames), two entries (shared
|
|
||||||
/// pointers) will be generated which share the same AlignedMemory object.
|
|
||||||
explicit Service(const std::string &config,
|
|
||||||
AlignedMemory modelMemory = AlignedMemory(),
|
|
||||||
AlignedMemory shortlistMemory = AlignedMemory(),
|
|
||||||
std::vector<std::shared_ptr<AlignedMemory>> vocabsMemories = {})
|
|
||||||
: Service(parseOptions(config, /*validate=*/false),
|
|
||||||
std::move(modelMemory),
|
|
||||||
std::move(shortlistMemory),
|
|
||||||
std::move(vocabsMemories)) {}
|
|
||||||
|
|
||||||
/// Explicit destructor to clean up after any threads initialized in
|
/// Explicit destructor to clean up after any threads initialized in
|
||||||
/// asynchronous operation mode.
|
/// asynchronous operation mode.
|
||||||
|
@ -48,14 +48,22 @@ std::vector<std::shared_ptr<AlignedMemory>> prepareVocabsSmartMemories(std::vect
|
|||||||
return vocabsSmartMemories;
|
return vocabsSmartMemories;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
marian::bergamot::MemoryBundle prepareMemoryBundle(AlignedMemory* modelMemory,
|
||||||
|
AlignedMemory* shortlistMemory,
|
||||||
|
std::vector<AlignedMemory*> uniqueVocabsMemories){
|
||||||
|
marian::bergamot::MemoryBundle memoryBundle;
|
||||||
|
memoryBundle.model = std::move(*modelMemory);
|
||||||
|
memoryBundle.shortlist = std::move(*shortlistMemory);
|
||||||
|
memoryBundle.vocabs = std::move(prepareVocabsSmartMemories(uniqueVocabsMemories));
|
||||||
|
|
||||||
|
return memoryBundle;
|
||||||
|
}
|
||||||
|
|
||||||
TranslationModel* TranslationModelFactory(const std::string &config,
|
TranslationModel* TranslationModelFactory(const std::string &config,
|
||||||
AlignedMemory* modelMemory,
|
AlignedMemory* modelMemory,
|
||||||
AlignedMemory* shortlistMemory,
|
AlignedMemory* shortlistMemory,
|
||||||
std::vector<AlignedMemory*> uniqueVocabsMemories) {
|
std::vector<AlignedMemory*> uniqueVocabsMemories) {
|
||||||
return new TranslationModel(config,
|
return new TranslationModel(config, std::move(prepareMemoryBundle(modelMemory, shortlistMemory, uniqueVocabsMemories)));
|
||||||
std::move(*modelMemory),
|
|
||||||
std::move(*shortlistMemory),
|
|
||||||
std::move(prepareVocabsSmartMemories(uniqueVocabsMemories)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EMSCRIPTEN_BINDINGS(translation_model) {
|
EMSCRIPTEN_BINDINGS(translation_model) {
|
||||||
|
Loading…
Reference in New Issue
Block a user