nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib
2, stdenv
3, fetchFromGitHub
4, cmake
5, darwin # Accelerate
6, llvmPackages # openmp
7, withMkl ? false, mkl
8, withCUDA ? false
9, withCuDNN ? false
10, cudaPackages
11# Enabling both withOneDNN and withOpenblas is broken
12# https://github.com/OpenNMT/CTranslate2/issues/1294
13, withOneDNN ? false, oneDNN
14, withOpenblas ? true, openblas
15, withRuy ? true
16
17# passthru tests
18, libretranslate
19, wyoming-faster-whisper
20}:
21
22let
23 cmakeBool = b: if b then "ON" else "OFF";
24in
25stdenv.mkDerivation rec {
26 pname = "ctranslate2";
27 version = "4.3.1";
28
29 src = fetchFromGitHub {
30 owner = "OpenNMT";
31 repo = "CTranslate2";
32 rev = "v${version}";
33 hash = "sha256-ApmGto9RzT8t49bsZVwk8aQnIau9sQyFvt9qnWKUGAE=";
34 fetchSubmodules = true;
35 };
36
37 nativeBuildInputs = [
38 cmake
39 ] ++ lib.optionals withCUDA [
40 cudaPackages.cuda_nvcc
41 ];
42
43 cmakeFlags = [
44 # https://opennmt.net/CTranslate2/installation.html#build-options
45 # https://github.com/OpenNMT/CTranslate2/blob/54810350e662ebdb01ecbf8e4a746f02aeff1dd7/python/tools/prepare_build_environment_linux.sh#L53
46 # https://github.com/OpenNMT/CTranslate2/blob/59d223abcc7e636c1c2956e62482bc3299cc7766/python/tools/prepare_build_environment_macos.sh#L12
47 "-DOPENMP_RUNTIME=COMP"
48 "-DWITH_CUDA=${cmakeBool withCUDA}"
49 "-DWITH_CUDNN=${cmakeBool withCuDNN}"
50 "-DWITH_DNNL=${cmakeBool withOneDNN}"
51 "-DWITH_OPENBLAS=${cmakeBool withOpenblas}"
52 "-DWITH_RUY=${cmakeBool withRuy}"
53 "-DWITH_MKL=${cmakeBool withMkl}"
54 ]
55 ++ lib.optional stdenv.isDarwin "-DWITH_ACCELERATE=ON";
56
57 buildInputs = lib.optionals withMkl [
58 mkl
59 ] ++ lib.optionals withCUDA [
60 cudaPackages.cuda_cccl # <nv/target> required by the fp16 headers in cudart
61 cudaPackages.cuda_cudart
62 cudaPackages.libcublas
63 cudaPackages.libcurand
64 ] ++ lib.optionals (withCUDA && withCuDNN) [
65 cudaPackages.cudnn
66 ] ++ lib.optionals withOneDNN [
67 oneDNN
68 ] ++ lib.optionals withOpenblas [
69 openblas
70 ] ++ lib.optionals stdenv.isDarwin [
71 llvmPackages.openmp
72 darwin.apple_sdk.frameworks.Accelerate
73 ] ++ lib.optionals (stdenv.isDarwin && stdenv.isx86_64) [
74 darwin.apple_sdk.frameworks.CoreGraphics
75 darwin.apple_sdk.frameworks.CoreVideo
76 ];
77
78 passthru.tests = {
79 inherit
80 libretranslate
81 wyoming-faster-whisper
82 ;
83 };
84
85 meta = with lib; {
86 description = "Fast inference engine for Transformer models";
87 mainProgram = "ct2-translator";
88 homepage = "https://github.com/OpenNMT/CTranslate2";
89 changelog = "https://github.com/OpenNMT/CTranslate2/blob/${src.rev}/CHANGELOG.md";
90 license = licenses.mit;
91 maintainers = with maintainers; [ hexa misuzu ];
92 broken =
93 (lib.versionOlder cudaPackages.cudaVersion "11.4")
94 || !(withCuDNN -> withCUDA);
95 };
96}