A component that runs neural machine translation models to process incoming translation requests via RabbitMQ. This application is based on a custom version of FairSeq - a PyTorch-based library for sequence modeling - and is compatible with standard single-direction baseline models as well as modular multilingual models.
For more information, please refer to the model training scripts and the custom FairSeq fork used in this project.
The easiest and recommended way to set up the translation worker, is by using the docker images published alongside this
repository, and it is designed to be used in a CPU environment. Starting from version 1.2.0, we
publish translation-worker images with and without models. For more
information about configuring custom models can be found
in models/README.md).
HF_MODEL(optional) - a HuggingFace model identifyer (tartuNLP/mtee-domain-detectionby default). The model is automatically downloaded from HuggingFace during the build phase.
The running container should be configured using the following parameters:
-
Environment variables:
- Variables that configure the connection to a RabbitMQ message broker:
MQ_USERNAME- RabbitMQ usernameMQ_PASSWORD- RabbitMQ user passwordMQ_HOST- RabbitMQ hostMQ_PORT(optional) - RabbitMQ port (5672by default)MQ_EXCHANGE(optional) - RabbitMQ exchange name (translationby default)MQ_CONNECTION_NAME(optional) - friendly connection name (Translation workerby default)MQ_HEARTBEAT(optional) - heartbeat value (60seconds by default)
- PyTorch-related variables:
MKL_NUM_THREADS(optional) - number of threads used for intra-op parallelism by PyTorch. This defaults to the number of CPU cores which may cause computational overhead when deployed on larger nodes. Alternatively, thedocker runflag--cpuset-cpuscan be used to control this. For more details, refer to the performance and hardware requirements section below.
- Translation-related variables:
WORKER_MAX_INPUT_LENGTH(optional) - the number of characters allowed per request (10000by default). Longer requests will return validation errors with status code400.
- Variables that configure the connection to a RabbitMQ message broker:
-
Optional runtime flags (the
COMMANDoption):--model-config- path to the model config file (models/config.yamlby default). The default file is included in images that already include models. Compatible sample files are included in themodels/directory and the format is described inmodels/README.md).--log-config- path to logging config files (logging/logging.iniby default),logging/debug.inican be used for debug-level logging--port- port of the healthcheck probes (8000by default):
-
Endpoints for healthcheck probes:
/health/startup/health/readiness/health/liveness
When building the image, the model can be built with different targets. BuildKit should be enabled to skip any unused stages of the build.
-
worker-base- the worker code without any models. -
worker-model- a worker with an included model. Requires one of the following build-time arguments:MODEL_IMAGE- the image name where the model is copied from. For example any of thetranslation-modelimages.MODEL_CONFIG_FILE- path to the model configuration file, for examplemodels/general.yaml. The file must contain the otherwise optional keyhuggingfaceto download the model or the build will fail.
-
env- an intermediate build stage with all packages installed, but no code. -
model-dl- images that only contain model files and configuration. The separate stage is used to cache this step and speed up builds because HuggingFace downloads can be very slow compared to copying model files from a build stage. Published attranslation-model. Alternatively, these can be used as init containers to copy models over during startup, but this is quite slow and not recommended. -
model- an alias for the model image, the value ofMODEL_IMAGEormodel-dlby default.
The worker loads the NMT model into memory. The exact RAM usage depends on the model and should always be tested, but a conservative estimate is to have 8 GB of memory available (tested with a modular model with 4 input and 4 output languages).
The performance depends on the available CPU resources, however, this should be finetuned for the deployment
infrastructure. By default, PyTorch will try to utilize all CPU cores to 100% and run as many threads as there are
cores. This can cause major computational overhead if the worker is deployed on large nodes. The number of threads
used should be limited using the MKL_NUM_THREADS environment variable or the docker run flag --cpuset-cpus.
Limiting CPU usage by docker configuration which only limits CPU shares is not sufficient (e.g. docker run flag
--cpus or the CPU limit in K8s, unless the non-default
static CPU Manager policy is used). For
example, on a node with 128 cores, setting the CPU limit at 16.0 results in 128 parallel threads running with each one
utilizing only 1/8 of each core's computational potential. This amplifies the effect of multithreading overhead and can
result in inference speeds up to 20x slower than expected.
Although the optimal number of threads depends on the exact model and infrastructure used, a good starting point is
around 16. With optimal configuration and modern hardware, the worker should be able to process ~7 sentences per
second. For more information, please refer to
PyTorch documentation.
For a manual setup, please refer to the included Dockerfile and the environment specification described in
requirements/requirements.txt.
Additionally, models/README.md describes how
models should be set up correctly.
To initialize the sentence splitting functionality, the following command should be run before starting the application:
python -c "import nltk; nltk.download(\"punkt\")"
RabbitMQ and PyTorch parameters should be configured with environment variables as described above. The worker can be started with:
python main.py [--model-config models/config.yaml] [--log-config logging/logging.ini] [--port 8000]
The worker consumes translation requests from a RabbitMQ message broker and responds with the translated text. The following format is compatible with the translation API.
Requests should be published with the following parameters:
- Exchange name:
translation(exchange type isdirect) - Routing key:
translation.<src>.<tgt>.<domain>.<input_type>where<src>refers to 2-letter ISO language code of the input text,<tgt>is the 2-letter code of the target language,<domain>is the text domain and<input_type>refers to the origin of the text and its format. For exampletranslation.et.en.legal.web. - Message properties:
- Correlation ID - a UID for each request that can be used to correlate requests and responses.
- Reply To - name of the callback queue where the response should be posted.
- Content Type -
application/json - Headers:
RequestIdReturnMessageType
- JSON-formatted message content with the following keys:
text– input text, either a string or a list of strings which are allowed to contain multiple sentences or paragraphs.src– 2-letter ISO language codetgt– 2-letter ISO language codedomain– the text domain, eithergeneral,legal,military,crisis.input_type– input type category that refers to the origin format, eitherplain,document,weborasr
The worker will return a response with the following parameters:
- Exchange name: (empty string)
- Routing key: the Reply To property value from the request
- Message properties:
- Correlation ID - the Correlation ID value of the request
- Content Type -
application/json - Headers:
RequestId- theRequestIdvalue of the requestMT-MessageType- theReturnMessageTypevalue of the request
- JSON-formatted message content with the following keys:
status- a human-readable status message,OKby defaultstatus_code– (integer) a HTTP status code,200by defaulttranslation- string or a list of strings (depending on the input text format) with the translation. May benullin casestatus_code!=200
Known non-OK responses can occur in case the request format was incorrect. Example request and response:
{
"src": "et",
"tgt": "en",
"domain": "general",
"input_type": "plain"
}
{
"status": "1 validation error for Request\ntext\n field required (type=value_error.missing)",
"status_code": 400,
"translation": null
}
The JSON-formatted part of the status field is the
ValidationError message from Pydantic validation.