Subject: [bug#76999] [PATCH v2] gnu: llama-cpp: Update to 0.0.0-b5013.

From: Morgan Smith <[email-masked]>
Date: Mon, 7 Apr 2025 19:11:08 -0400

* gnu/packages/machine-learning.scm (llama-cpp): Update to 0.0.0-b5013.
[inputs]: Add curl, glslang, and python-gguf.
[native-inputs]: bash -> bash-minimal.
[source, homepage]: Update URL.
[python-scripts]: Rely on upstream to install them.  Delete phase.
[fix-tests]: Fix an additional test.
* gnu/packages/patches/llama-cpp-vulkan-optional.patch: Delete.
* gnu/local.mk: Unregister patch.

Change-Id: Ic297534cd142cb83e3964eae21b4eb807b74e9bc
---
 gnu/local.mk                                  |  1 -
 gnu/packages/machine-learning.scm             | 47 +++++++------------
 .../patches/llama-cpp-vulkan-optional.patch   | 38 ---------------
 3 files changed, 17 insertions(+), 69 deletions(-)
 delete mode 100644 gnu/packages/patches/llama-cpp-vulkan-optional.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 6dc4b4f61b..65c21c2f0d 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1843,7 +1843,6 @@ dist_patch_DATA =						\
   %D%/packages/patches/libmhash-hmac-fix-uaf.patch		\
   %D%/packages/patches/libmodbus-disable-networking-test.patch \
   %D%/packages/patches/lib-tl-for-telegram-memcpy.patch	\
-  %D%/packages/patches/llama-cpp-vulkan-optional.patch	\
   %D%/packages/patches/llhttp-ponyfill-object-fromentries.patch	\
   %D%/packages/patches/lvm2-no-systemd.patch    		\
   %D%/packages/patches/maturin-no-cross-compile.patch		\
diff --git a/gnu/packages/machine-learning.scm b/gnu/packages/machine-learning.scm
index bd7a4fd81b..0b9ee4fa39 100644
--- a/gnu/packages/machine-learning.scm
+++ b/gnu/packages/machine-learning.scm
@@ -84,6 +84,7 @@ (define-module (gnu packages machine-learning)
   #:use-module (gnu packages crates-io)
   #:use-module (gnu packages crates-tls)
   #:use-module (gnu packages crates-web)
+  #:use-module (gnu packages curl)
   #:use-module (gnu packages databases)
   #:use-module (gnu packages dejagnu)
   #:use-module (gnu packages documentation)
@@ -640,7 +641,7 @@ (define-public guile-aiscm-next
   (deprecated-package "guile-aiscm-next" guile-aiscm))
 
 (define-public llama-cpp
-  (let ((tag "b4549"))
+  (let ((tag "b5013"))
     (package
       (name "llama-cpp")
       (version (string-append "0.0.0-" tag))
@@ -648,19 +649,19 @@ (define-public llama-cpp
        (origin
          (method git-fetch)
          (uri (git-reference
-               (url "https://github.com/ggerganov/llama.cpp")
+               (url "https://github.com/ggml-org/llama.cpp")
                (commit tag)))
          (file-name (git-file-name name tag))
          (sha256
-          (base32 "1xf2579q0r8nv06kj8padi6w9cv30w58vdys65nq8yzm3dy452a1"))
-         (patches
-          (search-patches "llama-cpp-vulkan-optional.patch"))))
+          (base32 "0s73dz871x53dr366lkzq19f677bwgma2ri8m5vhbfa9p8yp4p3r"))))
       (build-system cmake-build-system)
       (arguments
        (list
         #:configure-flags
-        #~(list "-DBUILD_SHARED_LIBS=ON"
+        #~(list #$(string-append "-DGGML_BUILD_NUMBER=" tag)
+                "-DBUILD_SHARED_LIBS=ON"
                 "-DGGML_VULKAN=ON"
+                "-DLLAMA_CURL=ON"
                 "-DGGML_BLAS=ON"
                 "-DGGML_BLAS_VENDOR=OpenBLAS"
                 (string-append "-DBLAS_INCLUDE_DIRS="
@@ -690,32 +691,17 @@ (define-public llama-cpp
                 (substitute* "ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp"
                  (("\"/bin/sh\"")
                   (string-append "\"" (search-input-file inputs "/bin/sh") "\"")))))
-            (add-after 'unpack 'disable-unrunable-tests
+            (add-after 'unpack 'fix-tests
               (lambda _
                 ;; test-eval-callback downloads ML model from network, cannot
                 ;; run in Guix build environment
                 (substitute* '("examples/eval-callback/CMakeLists.txt")
                   (("COMMAND llama-eval-callback")
-                   "COMMAND true llama-eval-callback"))))
-            (add-before 'install 'install-python-scripts
-              (lambda _
-                (let ((bin (string-append #$output "/bin/")))
-                  (define (make-script script)
-                    (let ((suffix (if (string-suffix? ".py" script) "" ".py")))
-                      (call-with-input-file
-                          (string-append "../source/" script suffix)
-                        (lambda (input)
-                          (call-with-output-file (string-append bin script)
-                            (lambda (output)
-                              (format output "#!~a/bin/python3\n~a"
-                                      #$(this-package-input "python")
-                                      (get-string-all input))))))
-                      (chmod (string-append bin script) #o555)))
-                  (mkdir-p bin)
-                  (make-script "convert_hf_to_gguf")
-                  (make-script "convert_llama_ggml_to_gguf")
-                  (make-script "convert_hf_to_gguf_update.py"))))
-            (add-after 'install-python-scripts 'wrap-python-scripts
+                   "COMMAND true llama-eval-callback"))
+                ;; Help it find the test files it needs
+                (substitute* "tests/test-chat.cpp"
+                  (("\"\\.\\./\"") "\"../source/\""))))
+            (add-after 'install 'wrap-python-scripts
               (assoc-ref python:%standard-phases 'wrap))
             (add-after 'install 'remove-tests
               (lambda* (#:key outputs #:allow-other-keys)
@@ -723,12 +709,13 @@ (define-public llama-cpp
                                        (string-append (assoc-ref outputs "out")
                                                       "/bin")
                                        "^test-")))))))
-      (inputs (list python vulkan-headers vulkan-loader))
-      (native-inputs (list pkg-config shaderc bash))
+      (inputs (list curl glslang python python-gguf
+                    vulkan-headers vulkan-loader))
+      (native-inputs (list pkg-config shaderc bash-minimal))
       (propagated-inputs
        (list python-numpy python-pytorch python-sentencepiece openblas))
       (properties '((tunable? . #true))) ;use AVX512, FMA, etc. when available
-      (home-page "https://github.com/ggerganov/llama.cpp")
+      (home-page "https://github.com/ggml-org/llama.cpp")
       (synopsis "Port of Facebook's LLaMA model in C/C++")
       (description "This package provides a port to Facebook's LLaMA collection
 of foundation language models.  It requires models parameters to be downloaded
diff --git a/gnu/packages/patches/llama-cpp-vulkan-optional.patch b/gnu/packages/patches/llama-cpp-vulkan-optional.patch
deleted file mode 100644
index 43a49b6a02..0000000000
--- a/gnu/packages/patches/llama-cpp-vulkan-optional.patch
+++ /dev/null
@@ -1,38 +0,0 @@
-Author: Danny Milosavljevic <[email-masked]>
-Date: 2025-01-29
-License: Expat
-Subject: Make Vulkan optional
-
-See also: <https://github.com/ggerganov/llama.cpp/pull/11494>
-
-diff -ru orig/llama.cpp/ggml/include/ggml-vulkan.h llama.cpp/ggml/include/ggml-vulkan.h
---- orig/llama.cpp/ggml/include/ggml-vulkan.h	2025-01-29 10:24:10.894476682 +0100
-+++ llama.cpp/ggml/include/ggml-vulkan.h	2025-02-07 18:28:34.509509638 +0100
-@@ -10,8 +10,6 @@
- #define GGML_VK_NAME "Vulkan"
- #define GGML_VK_MAX_DEVICES 16
- 
--GGML_BACKEND_API void ggml_vk_instance_init(void);
--
- // backend API
- GGML_BACKEND_API ggml_backend_t ggml_backend_vk_init(size_t dev_num);
- 
-diff -ru orig/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp
---- orig/llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp	2025-01-29 10:24:10.922476480 +0100
-+++ llama.cpp/ggml/src/ggml-vulkan/ggml-vulkan.cpp	2025-01-29 22:33:19.955087552 +0100
-@@ -8174,8 +8174,13 @@
-         /* .iface       = */ ggml_backend_vk_reg_i,
-         /* .context     = */ nullptr,
-     };
--
--    return &reg;
-+    try {
-+        ggml_vk_instance_init();
-+        return &reg;
-+    } catch (const vk::SystemError& e) {
-+        VK_LOG_DEBUG("ggml_vk_get_device_count() -> Error: System error: " << e.what());
-+        return nullptr;
-+    }
- }
- 
- // Extension availability

base-commit: 666a6cfd88b3e5106a9180e06ea128db8084be0e
prerequisite-patch-id: 1e2c478cf648ee8c9a3b1af55543e1b96ff24ec7
-- 
2.49.0