Program Listing for File service.h

Return to documentation for file (src/translator/service.h)

#ifndef SRC_BERGAMOT_SERVICE_H_
#define SRC_BERGAMOT_SERVICE_H_

#include "batch_translator.h"
#include "data/types.h"
#include "response.h"
#include "response_builder.h"
#include "text_processor.h"
#include "threadsafe_batcher.h"
#include "translator/parser.h"
#include "vocabs.h"

#ifndef WASM_COMPATIBLE_SOURCE
#include <thread>
#endif

#include <queue>
#include <vector>

namespace marian {
namespace bergamot {

class Service {
 public:
  explicit Service(Ptr<Options> options, MemoryBundle memoryBundle = {});

  explicit Service(const std::string &config, MemoryBundle memoryBundle = {})
      : Service(parseOptions(config, /*validate=*/false), std::move(memoryBundle)) {}

  ~Service();

  std::future<Response> translate(std::string &&source, ResponseOptions options = ResponseOptions());

  std::vector<Response> translateMultiple(std::vector<std::string> &&source, ResponseOptions responseOptions);

  bool isAlignmentSupported() const { return options_->hasAndNotEmpty("alignment"); }

 private:
  std::future<Response> queueRequest(std::string &&input, ResponseOptions responseOptions);

  void dispatchTranslate();

  void blockIfWASM();

  size_t numWorkers_;

  Ptr<Options> options_;

  AlignedMemory modelMemory_;  // ORDER DEPENDENCY (translators_)
  AlignedMemory shortlistMemory_;  // ORDER DEPENDENCY (translators_)


  size_t requestId_;
  Vocabs vocabs_;  // ORDER DEPENDENCY (text_processor_)

  TextProcessor text_processor_;  // ORDER DEPENDENCY (vocabs_)

  ThreadsafeBatcher batcher_;

  // The following constructs are available providing full capabilities on a non
  // WASM platform, where one does not have to hide threads.
#ifdef WASM_COMPATIBLE_SOURCE
  BatchTranslator blocking_translator_;  // ORDER DEPENDENCY (modelMemory_, shortlistMemory_)
#else
  std::vector<std::thread> workers_;
#endif  // WASM_COMPATIBLE_SOURCE
};

}  // namespace bergamot
}  // namespace marian

#endif  // SRC_BERGAMOT_SERVICE_H_