1{
2 lib,
3 stdenv,
4 buildPythonPackage,
5 fetchFromGitHub,
6
7 # nativeBuildInputs
8 cmake,
9 ninja,
10
11 # build-system
12 pathspec,
13 pyproject-metadata,
14 scikit-build-core,
15
16 # buildInputs
17 apple-sdk_11,
18
19 # dependencies
20 diskcache,
21 jinja2,
22 numpy,
23 typing-extensions,
24
25 # tests
26 scipy,
27 huggingface-hub,
28
29 # passthru
30 gitUpdater,
31 pytestCheckHook,
32 llama-cpp-python,
33
34 config,
35 cudaSupport ? config.cudaSupport,
36 cudaPackages ? { },
37
38}:
39buildPythonPackage rec {
40 pname = "llama-cpp-python";
41 version = "0.3.5";
42 pyproject = true;
43
44 src = fetchFromGitHub {
45 owner = "abetlen";
46 repo = "llama-cpp-python";
47 tag = "v${version}";
48 hash = "sha256-+LBq+rCqOsvGnhTL1UoCcAwvDt8Zo9hlaa4KibFFDag=";
49 fetchSubmodules = true;
50 };
51 # src = /home/gaetan/llama-cpp-python;
52
53 dontUseCmakeConfigure = true;
54 SKBUILD_CMAKE_ARGS = lib.strings.concatStringsSep ";" (
55 lib.optionals cudaSupport [
56 "-DGGML_CUDA=on"
57 "-DCUDAToolkit_ROOT=${lib.getDev cudaPackages.cuda_nvcc}"
58 "-DCMAKE_CUDA_COMPILER=${lib.getExe cudaPackages.cuda_nvcc}"
59 ]
60 );
61
62 nativeBuildInputs = [
63 cmake
64 ninja
65 ];
66
67 build-system = [
68 pathspec
69 pyproject-metadata
70 scikit-build-core
71 ];
72
73 buildInputs =
74 lib.optionals cudaSupport (
75 with cudaPackages;
76 [
77 cuda_cudart # cuda_runtime.h
78 cuda_cccl # <thrust/*>
79 libcublas # cublas_v2.h
80 ]
81 )
82 ++ lib.optionals stdenv.hostPlatform.isDarwin [
83 apple-sdk_11
84 ];
85
86 dependencies = [
87 diskcache
88 jinja2
89 numpy
90 typing-extensions
91 ];
92
93 nativeCheckInputs = [
94 pytestCheckHook
95 scipy
96 huggingface-hub
97 ];
98
99 disabledTests = [
100 # tries to download model from huggingface-hub
101 "test_real_model"
102 "test_real_llama"
103 ];
104
105 pythonImportsCheck = [ "llama_cpp" ];
106
107 passthru = {
108 updateScript = gitUpdater { rev-prefix = "v"; };
109 tests.llama-cpp-python = llama-cpp-python.override { cudaSupport = true; };
110 };
111
112 meta = {
113 description = "Python bindings for llama.cpp";
114 homepage = "https://github.com/abetlen/llama-cpp-python";
115 changelog = "https://github.com/abetlen/llama-cpp-python/blob/v${version}/CHANGELOG.md";
116 license = lib.licenses.mit;
117 maintainers = with lib.maintainers; [ kirillrdy ];
118 badPlatforms = [
119 # Segfaults during tests:
120 # tests/test_llama.py .Fatal Python error: Segmentation fault
121 # Current thread 0x00000001f3decf40 (most recent call first):
122 # File "/private/tmp/nix-build-python3.12-llama-cpp-python-0.3.2.drv-0/source/llama_cpp/_internals.py", line 51 in __init__
123 lib.systems.inspect.patterns.isDarwin
124 ];
125 };
126}