Registry / ai-ml / ctranslate2

ctranslate2

JSON →
library4.8.1pypypi✓ verified 25d ago

CTranslate2 is a C++ and Python library for efficient inference with Transformer models. It implements a custom runtime with performance optimizations like weights quantization, layers fusion, and batch reordering to accelerate and reduce memory usage of Transformer models on CPUs and GPUs. It currently supports a wide range of encoder-decoder, decoder-only, and encoder-only models from frameworks like OpenNMT, Fairseq, and Hugging Face Transformers. The library is actively maintained with frequent releases, currently at version 4.7.1.

pip install ctranslate2
INSTALL
IMPORT
SIG · CTRANSLATE2
C
ctranslate2
ai-mlpythonv4.8.1
Install
5.1s avg
Import
638ms
Disk
226MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.8.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.915 runs
no_wheel
glibc
py 3.103.915 runs
installs and imports cleanly · install 5.1s · import 0.638s · 221MB
226MB installed
● package 226MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Translator
from ctranslate2 import Translator
import ctranslate2 translator = ctranslate2.Translator(model_path)

This quickstart demonstrates how to load a pre-converted model using `ctranslate2.Translator` and perform a basic text translation. It assumes a model has already been converted (e.g., from Hugging Face Transformers) and a tokenizer is available. For generation tasks, use `ctranslate2.Generator` instead.

# First, convert a model. This example uses a Hugging Face model. # You would run this command in your terminal once: # pip install transformers[torch] # ct2-transformers-converter --model Helsinki-NLP/opus-mt-en-de --output_dir opus-mt-en-de import ctranslate2 import transformers # Path to your converted CTranslate2 model directory model_path = "opus-mt-en-de" try: # Initialize the CTranslate2 Translator translator = ctranslate2.Translator(model_path, device="cpu") # Use device="cuda" for GPU # Initialize the original tokenizer (e.g., from Hugging Face for tokenization) tokenizer = transformers.AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-de") text_to_translate = "Hello world!" # Encode the input text to tokens input_tokens = tokenizer.convert_ids_to_tokens(tokenizer.encode(text_to_translate)) # CTranslate2 expects a batch of inputs, so wrap in a list batch_inputs = [input_tokens] # Perform translation results = translator.translate_batch(batch_inputs) # Decode the output tokens output_tokens = results[0].hypotheses[0] translated_text = tokenizer.decode(tokenizer.convert_tokens_to_ids(output_tokens)) print(f"Original: {text_to_translate}") print(f"Translated: {translated_text}") except Exception as e: print(f"An error occurred: {e}") print("Please ensure you have a model converted and located at 'opus-mt-en-de' ") print("and that 'transformers' library is installed.") print("For example, you can run: `pip install transformers[torch]` and then ") print("`ct2-transformers-converter --model Helsinki-NLP/opus-mt-en-de --output_dir opus-mt-en-de`")
ct2-transformers-converter --version
Debug
Known issues
breakingPython 3.8 support was dropped in CTranslate2 v4.6.0. Users on Python 3.8 or older must upgrade their Python environment to use v4.6.0 or newer.
fix
Upgrade Python to version 3.9 or higher.
affects: >=4.6.0
breakingCTranslate2 v4.5.0 and later require cuDNN 9 and are no longer compatible with cuDNN 8 for NVIDIA GPU acceleration. Users may encounter 'Could not load library libcudnn_ops_infer.so.8' errors.
fix
Upgrade cuDNN to version 9.x if using NVIDIA GPUs, or downgrade CTranslate2 to a version prior to 4.5.0 if cuDNN 8 is strictly required.
affects: >=4.5.0
breakingFlash Attention support was removed from the Python package in CTranslate2 v4.4.0 due to significant package size increase with minimal performance gain. It remains supported in the C++ package with a specific build option.
fix
If Flash Attention is critical, consider using the C++ library with the `WITH_FLASH_ATTN` build option or explore alternative solutions.
affects: >=4.4.0
gotchaCTranslate2 v4.7.0 introduced compatibility with Transformers v5. Older versions of CTranslate2 might have issues when converting or inferring models from `transformers` library versions 5.x.
fix
Upgrade CTranslate2 to version 4.7.0 or higher for full compatibility with Transformers v5.
affects: <4.7.0
gotchaDuring the release of v4.3.0, the PyPI package size exceeded the limit (20GB), leading to incomplete releases for Python 3.8 and 3.9. This was addressed in v4.3.1 and later versions.
fix
Avoid CTranslate2 version 4.3.0. Use 4.3.1 or a later version instead.
affects: 4.3.0
breakingCTranslate2 currently lacks official support and pre-built distributions for Python 3.13, resulting in 'No matching distribution found' errors when attempting installation.
fix
Use a Python environment older than 3.13 (e.g., Python 3.12 or 3.11) or monitor CTranslate2's releases for future Python 3.13 compatibility.
affects: *
Upgrade
Version history
4.8.1latest on PyPI · released Jul 3, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.9 or higher.
sentencepieceoptionalCommonly used for tokenization with CTranslate2 models (e.g., OpenNMT, OPUS-MT).
transformersoptionalRequired for converting models from the Hugging Face Transformers library to CTranslate2 format.
OpenNMT-pyoptionalRequired for converting models trained with OpenNMT-py to CTranslate2 format.
torchoptionalNeeded for AMD GPU support with ROCm, PyTorch 2.1+ required. Also often used for model conversion workflows.
cudaoptionalNVIDIA CUDA Toolkit (12.x recommended) is required for NVIDIA GPU acceleration.
cudnnoptionalNVIDIA cuDNN (8 or 9, depending on CTranslate2 version) is recommended for optimal performance with convolutional layers on NVIDIA GPUs.
rocmoptionalAMD ROCm (6.0+) is required for AMD GPU acceleration.
Agent activity
117 hits · last 30 days
node
108
OpenAI (training)
1
Resources
ctranslate2 — pip install ctranslate2 · libregistry