unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Nicolas Graves via Guix-patches via <guix-patches@gnu.org>
To: 73094@debbugs.gnu.org
Cc: herman@rimm.ee, ngraves@ngraves.fr
Subject: [bug#73094] [PATCH v2 10/10] gnu: Add python-safetensors.
Date: Sun,  8 Sep 2024 16:39:26 +0200	[thread overview]
Message-ID: <20240908143935.22543-10-ngraves@ngraves.fr> (raw)
In-Reply-To: <20240908143935.22543-1-ngraves@ngraves.fr>

* gnu/packages/machine-learning.scm (python-safetensors): New variable.

Change-Id: I90a1684d06756ce87ca0862d745a75be5919f0b2
---
 gnu/packages/machine-learning.scm | 96 +++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)

diff --git a/gnu/packages/machine-learning.scm b/gnu/packages/machine-learning.scm
index a4aeb97be7..5006ba7ca0 100644
--- a/gnu/packages/machine-learning.scm
+++ b/gnu/packages/machine-learning.scm
@@ -1120,6 +1120,102 @@ (define-public rust-safetensors
 @code{PyTorch} counterparts.")
     (license license:asl2.0)))
 
+(define-public python-safetensors
+  (package
+    (name "python-safetensors")
+    (version "0.4.3")
+    (source
+     (origin
+       (method url-fetch)
+       (uri (pypi-uri "safetensors" version))
+       (sha256
+        (base32 "1hhiwy67jarm70l0k26fs1cjhzkgzrh79q14bklj2yp0qi8gr19g"))
+       (modules '((guix build utils)
+                  (ice-9 ftw)))
+       (snippet
+        #~(begin  ;; Only keeping bindings.
+            (for-each (lambda (file)
+                        (unless (member file '("." ".." "bindings" "PKG-INFO"))
+                          (delete-file-recursively file)))
+                      (scandir "."))
+            (for-each (lambda (file)
+                        (unless (member file '("." ".."))
+                          (rename-file (string-append "bindings/python/" file) file)))
+                      (scandir "bindings/python"))))))
+    (build-system cargo-build-system)
+    (arguments
+     (list
+      #:modules '((guix build cargo-build-system)
+                  (guix build utils)
+                  (ice-9 regex)
+                  (ice-9 textual-ports))
+      #:phases
+      #~(modify-phases %standard-phases
+          (add-after 'unpack-rust-crates 'inject-safetensors
+            (lambda _
+              (substitute* "Cargo.toml"
+                (("\\[dependencies\\]")
+                 (format #f "[dependencies]~%safetensors = ~s"
+                         #$(package-version rust-safetensors))))
+              (call-with-input-file "Cargo.toml"
+                (lambda (port)
+                  (let* ((content (get-string-all port))
+                         (top-match (string-match
+                                     "\\[dependencies.safetensors" content)))
+                    (call-with-output-file "Cargo.toml"
+                      (lambda (out)
+                        (format out "~a" (match:prefix top-match)))))))))
+          (add-before 'check 'install-rust-library
+            (lambda _
+              (copy-file "target/release/libsafetensors_rust.so"
+                         "py_src/safetensors/_safetensors_rust.so")))
+          (replace 'check
+            (lambda _
+              (invoke "python3"
+                      "-c" (format #f
+                                   "import sys; sys.path.append(\"~a/py_src\")"
+                                   (getcwd))
+                      "-m" "pytest"
+                      "-n" "auto"
+                      "--dist=loadfile"
+                      "-s" "-v" "./tests/"
+                      ;; Missing jax dependency
+                      "--ignore=./tests/test_flax_comparison.py")))
+          (add-after 'install 'install-python
+            (lambda _
+              (let* ((pversion #$(version-major+minor (package-version python)))
+                     (lib (string-append #$output "/lib/python" pversion
+                                         "/site-packages/"))
+                     (info (string-append lib "safetensors-"
+                                        #$(package-version this-package)
+                                        ".dist-info")))
+                (mkdir-p info)
+                (copy-file "PKG-INFO" (string-append info "/METADATA"))
+                (copy-recursively
+                 "py_src/safetensors"
+                 (string-append lib "safetensors"))))))
+      #:cargo-inputs
+      `(("rust-pyo3" ,rust-pyo3-0.21)
+        ("rust-memmap2" ,rust-memmap2-0.9)
+        ("rust-safetensors" ,rust-safetensors)
+        ("rust-serde-json" ,rust-serde-json-1))))
+    (inputs
+     (list rust-safetensors))
+    (native-inputs
+     (list python-h5py
+           python-minimal
+           python-numpy
+           python-pytest
+           python-pytest-xdist
+           python-pytorch
+           tensorflow))
+    (home-page "https://huggingface.co/docs/safetensors")
+    (synopsis "Simple and safe way to store and distribute tensors")
+    (description "This package provides a fast (zero-copy) and safe
+(dedicated) format for storing tensors safely.  This package builds upon
+@code{rust-safetensors} and provides Python bindings.")
+    (license license:asl2.0)))
+
 (define-public python-sentencepiece
   (package
     (name "python-sentencepiece")
-- 
2.45.2





  parent reply	other threads:[~2024-09-08 14:41 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-07  9:36 [bug#73094] [PATCH 00/10] Add python-safetensors Nicolas Graves via Guix-patches via
2024-09-07 10:08 ` [bug#73094] [PATCH 01/10] gnu: rust-darling-core-0.20: Update to 0.20.8 Nicolas Graves via Guix-patches via
2024-09-07 10:08   ` [bug#73094] [PATCH 02/10] gnu: rust-darling-macro-0.20: " Nicolas Graves via Guix-patches via
2024-09-07 10:08   ` [bug#73094] [PATCH 03/10] gnu: rust-darling-0.20: " Nicolas Graves via Guix-patches via
2024-09-07 10:08   ` [bug#73094] [PATCH 04/10] gnu: Add rust-derive-builder-core-0.20 Nicolas Graves via Guix-patches via
2024-09-07 10:08   ` [bug#73094] [PATCH 05/10] gnu: Add rust-derive-builder-macro-0.20 Nicolas Graves via Guix-patches via
2024-09-07 10:08   ` [bug#73094] [PATCH 06/10] gnu: Add rust-derive-builder-0.20 Nicolas Graves via Guix-patches via
2024-09-07 10:08   ` [bug#73094] [PATCH 07/10] gnu: Add rust-unicode-normalization-alignments-0.1 Nicolas Graves via Guix-patches via
2024-09-07 10:08   ` [bug#73094] [PATCH 08/10] gnu: Add rust-rayon-cond-0.3 Nicolas Graves via Guix-patches via
2024-09-07 10:08   ` [bug#73094] [PATCH 09/10] gnu: Add rust-safetensors Nicolas Graves via Guix-patches via
2024-09-07 10:08   ` [bug#73094] [PATCH 10/10] gnu: Add python-safetensors Nicolas Graves via Guix-patches via
2024-09-08 12:46 ` [bug#73094] [PATCH 00/10] " Herman Rimm via Guix-patches via
2024-09-08 14:39 ` [bug#73094] [PATCH v2 01/10] gnu: rust-darling-core-0.20: Update to 0.20.8 Nicolas Graves via Guix-patches via
2024-09-08 14:39   ` [bug#73094] [PATCH v2 02/10] gnu: rust-darling-macro-0.20: " Nicolas Graves via Guix-patches via
2024-09-08 14:39   ` [bug#73094] [PATCH v2 03/10] gnu: rust-darling-0.20: " Nicolas Graves via Guix-patches via
2024-09-08 14:39   ` [bug#73094] [PATCH v2 04/10] gnu: Add rust-derive-builder-core-0.20 Nicolas Graves via Guix-patches via
2024-09-08 14:39   ` [bug#73094] [PATCH v2 05/10] gnu: Add rust-derive-builder-macro-0.20 Nicolas Graves via Guix-patches via
2024-09-08 14:39   ` [bug#73094] [PATCH v2 06/10] gnu: Add rust-derive-builder-0.20 Nicolas Graves via Guix-patches via
2024-09-08 14:39   ` [bug#73094] [PATCH v2 07/10] gnu: Add rust-unicode-normalization-alignments-0.1 Nicolas Graves via Guix-patches via
2024-09-08 14:39   ` [bug#73094] [PATCH v2 08/10] gnu: Add rust-rayon-cond-0.3 Nicolas Graves via Guix-patches via
2024-09-08 14:39   ` [bug#73094] [PATCH v2 09/10] gnu: Add rust-safetensors Nicolas Graves via Guix-patches via
2024-09-08 14:39   ` Nicolas Graves via Guix-patches via [this message]
2024-09-09 10:07 ` [bug#73094] [PATCH v3 00/10] Add python-safetensors Herman Rimm via Guix-patches via
2024-09-09 10:07   ` [bug#73094] [PATCH v3 01/10] gnu: rust-darling-core-0.20: Update to 0.20.8 Herman Rimm via Guix-patches via
2024-09-09 10:07   ` [bug#73094] [PATCH v3 02/10] gnu: rust-darling-macro-0.20: " Herman Rimm via Guix-patches via
2024-09-09 10:07   ` [bug#73094] [PATCH v3 03/10] gnu: rust-darling-0.20: " Herman Rimm via Guix-patches via
2024-09-09 10:07   ` [bug#73094] [PATCH v3 04/10] gnu: Add rust-derive-builder-core-0.20 Herman Rimm via Guix-patches via
2024-09-09 10:07   ` [bug#73094] [PATCH v3 05/10] gnu: Add rust-derive-builder-macro-0.20 Herman Rimm via Guix-patches via
2024-09-09 10:07   ` [bug#73094] [PATCH v3 06/10] gnu: Add rust-derive-builder-0.20 Herman Rimm via Guix-patches via
2024-09-09 10:08   ` [bug#73094] [PATCH v3 07/10] gnu: Add rust-unicode-normalization-alignments-0.1 Herman Rimm via Guix-patches via
2024-09-09 10:08   ` [bug#73094] [PATCH v3 08/10] gnu: Add rust-rayon-cond-0.3 Herman Rimm via Guix-patches via
2024-09-09 10:08   ` [bug#73094] [PATCH v3 09/10] gnu: Add rust-safetensors Herman Rimm via Guix-patches via
2024-09-09 10:08   ` [bug#73094] [PATCH v3 10/10] gnu: Add python-safetensors Herman Rimm via Guix-patches via

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240908143935.22543-10-ngraves@ngraves.fr \
    --to=guix-patches@gnu.org \
    --cc=73094@debbugs.gnu.org \
    --cc=herman@rimm.ee \
    --cc=ngraves@ngraves.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).