.. _program_listing_file_src_translator_service.h: Program Listing for File service.h ================================== |exhale_lsh| :ref:`Return to documentation for file ` (``src/translator/service.h``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #ifndef SRC_BERGAMOT_SERVICE_H_ #define SRC_BERGAMOT_SERVICE_H_ #include #include #include #include "cache.h" #include "data/types.h" #include "logging.h" #include "quality_estimator.h" #include "response.h" #include "response_builder.h" #include "text_processor.h" #include "threadsafe_batching_pool.h" #include "translation_model.h" #include "translator/parser.h" #include "vocabs.h" namespace marian { namespace bergamot { class BlockingService; class AsyncService; class BlockingService { public: struct Config { size_t cacheSize{0}; Logger::Config logger; template static void addOptions(App &app, Config &config) { // Options will come here. app.add_option("--cache-size", config.cacheSize, "Number of entries to store in cache."); Logger::Config::addOptions(app, config.logger); } }; BlockingService(const BlockingService::Config &config); std::vector translateMultiple(std::shared_ptr translationModel, std::vector &&source, const std::vector &responseOptions); std::vector pivotMultiple(std::shared_ptr first, std::shared_ptr second, std::vector &&sources, const std::vector &responseOptions); TranslationCache::Stats cacheStats() { return cache_ ? cache_->stats() : TranslationCache::Stats(); } private: std::vector translateMultipleRaw(std::shared_ptr translationModel, std::vector &&source, const std::vector &responseOptions); size_t requestId_; AggregateBatchingPool batchingPool_; Config config_; // Logger which shuts down cleanly with service. Logger logger_; std::optional cache_; }; class AsyncService { public: struct Config { size_t numWorkers{1}; size_t cacheSize{0}; Logger::Config logger; // Configurations for logging template static void addOptions(App &app, Config &config) { app.add_option("--cpu-threads", config.numWorkers, "Workers to form translation backend"); app.add_option("--cache-size", config.cacheSize, "Number of entries to store in cache."); Logger::Config::addOptions(app, config.logger); } }; AsyncService(const AsyncService::Config &config); Ptr createCompatibleModel(const TranslationModel::Config &config) { // @TODO: Remove this remove this dependency/coupling. return New(config, /*replicas=*/config_.numWorkers); } void translate(std::shared_ptr translationModel, std::string &&source, CallbackType callback, const ResponseOptions &options = ResponseOptions()); void pivot(std::shared_ptr first, std::shared_ptr second, std::string &&source, CallbackType clientCallback, const ResponseOptions &options = ResponseOptions()); void clear(); ~AsyncService(); TranslationCache::Stats cacheStats() { return cache_ ? cache_->stats() : TranslationCache::Stats(); } private: void translateRaw(std::shared_ptr translationModel, std::string &&source, CallbackType callback, const ResponseOptions &options = ResponseOptions()); AsyncService::Config config_; std::vector workers_; size_t requestId_; ThreadsafeBatchingPool safeBatchingPool_; // Logger which shuts down cleanly with service. Logger logger_; std::optional cache_; }; } // namespace bergamot } // namespace marian #endif // SRC_BERGAMOT_SERVICE_H_