summaryrefslogtreecommitdiff
path: root/pkgs/development/python-modules/llama-cpp-python/default.nix
blob: 4c0391858e4aa206a8a646555e6a007da18b7900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{
  lib,
  stdenv,
  buildPythonPackage,
  cmake,
  fetchFromGitHub,
  gitUpdater,
  ninja,
  pathspec,
  pyproject-metadata,
  pytestCheckHook,
  pythonOlder,
  scikit-build-core,
  llama-cpp-python,

  config,
  cudaSupport ? config.cudaSupport,
  cudaPackages ? { },

  diskcache,
  jinja2,
  numpy,
  typing-extensions,
  scipy,
  huggingface-hub,
}:
let
  version = "0.3.1";
in
buildPythonPackage {
  pname = "llama-cpp-python";
  inherit version;
  pyproject = true;

  disabled = pythonOlder "3.7";

  stdenv = if cudaSupport then cudaPackages.backendStdenv else stdenv;

  src = fetchFromGitHub {
    owner = "abetlen";
    repo = "llama-cpp-python";
    rev = "refs/tags/v${version}";
    hash = "sha256-eO1zvNJZBE5BCnbgbh00tFIRWBCWor1lIsrLXs/HFds=";
    fetchSubmodules = true;
  };

  dontUseCmakeConfigure = true;
  SKBUILD_CMAKE_ARGS = lib.strings.concatStringsSep ";" (
    lib.optionals cudaSupport [
      "-DGGML_CUDA=on"
      "-DCUDAToolkit_ROOT=${lib.getDev cudaPackages.cuda_nvcc}"
      "-DCMAKE_CUDA_COMPILER=${lib.getExe cudaPackages.cuda_nvcc}"
    ]
  );

  nativeBuildInputs = [
    cmake
    ninja
    pathspec
    pyproject-metadata
    scikit-build-core
  ];

  buildInputs = lib.optionals cudaSupport (
    with cudaPackages;
    [
      cuda_cudart # cuda_runtime.h
      cuda_cccl # <thrust/*>
      libcublas # cublas_v2.h
    ]
  );

  propagatedBuildInputs = [
    diskcache
    jinja2
    numpy
    typing-extensions
  ];

  nativeCheckInputs = [
    pytestCheckHook
    scipy
    huggingface-hub
  ];

  disabledTests = [
    # tries to download model from huggingface-hub
    "test_real_model"
    "test_real_llama"
  ];

  pythonImportsCheck = [ "llama_cpp" ];

  passthru.updateScript = gitUpdater { rev-prefix = "v"; };
  passthru.tests.llama-cpp-python = llama-cpp-python.override { cudaSupport = true; };

  meta = {
    description = "Python bindings for llama.cpp";
    homepage = "https://github.com/abetlen/llama-cpp-python";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [ kirillrdy ];
  };
}