From a7451a7700722751a486a614472f92984d6874ae Mon Sep 17 00:00:00 2001 From: Nicolas Graves Date: Sat, 30 Aug 2025 19:39:00 +0200 Subject: [PATCH] gnu: tensorflow-lite: Add python output. This output corresponds to the pypi "tflite-runtime" package. * gnu/packages/machine-learning.scm (tensorflow-lite): [outputs]: Add "python". [arguments]<#:imported-modules, #:modules>: Add pyproject modules. <#:phases>: Add 'build-python, 'install-python, 'add-install-to-pythonpath and 'python-sanity-check phases. [inputs]: Replace python by python-wrapper. Add zlib. [propagated-inputs]: Add python-numpy. [native-inputs]: Add pybind11, python-wheel, swig. Signed-off-by: Andreas Enge --- gnu/packages/machine-learning.scm | 49 +++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/gnu/packages/machine-learning.scm b/gnu/packages/machine-learning.scm index 264fde6cfce3e6ac79e20e4858deb66402aa2a6f..409427cb3d95cbe2646b3ccdbddab72d2409e99b 100644 --- a/gnu/packages/machine-learning.scm +++ b/gnu/packages/machine-learning.scm @@ -3206,12 +3206,16 @@ Python.") (base32 "07f4x4g3kwhfjz7iadhqrv97zmw0blacixvca1gdqkqqi7aipxis")))) (build-system cmake-build-system) + (outputs (list "out" "python")) (arguments (list #:build-type "Release" + #:imported-modules (append %cmake-build-system-modules + %pyproject-build-system-modules) #:modules '((ice-9 match) (guix build utils) - (guix build cmake-build-system)) + (guix build cmake-build-system) + ((guix build pyproject-build-system) #:prefix py:)) #:configure-flags #~(list ;; "-DTFLITE_KERNEL_TEST=ON" ; TODO: build tests @@ -3328,17 +3332,46 @@ find_library(ML_DTYPES_LIBRARIES (lambda _ (invoke "cmake" "--build" "." "--target" "benchmark_model" "-j" (number->string (parallel-job-count))))) - + (add-after 'build-benchmark-model 'build-python + (lambda* (#:key configure-flags #:allow-other-keys) + (let ((script (string-append "../lite/tools/pip_package/" + "build_pip_package_with_cmake.sh"))) + (substitute* script + (("\"\\$\\{TENSORFLOW_LITE_DIR\\}\"" all) + (string-append "${CMAKE_ADDITIONAL_CONFIGURE_FLAGS} " + all))) + (setenv "BUILD_NUM_JOBS" (number->string (parallel-job-count))) + (setenv "CMAKE_ADDITIONAL_CONFIGURE_FLAGS" + (string-join configure-flags " ")) + (invoke "sh" script)))) (add-after 'install 'install-extra (lambda _ (install-file "../build/c/libtensorflowlite_c.so" (string-append #$output "/lib")) (install-file "../build/tools/benchmark/benchmark_model" (string-append #$output "/bin")))) + (add-after 'install-extra 'install-python + (lambda* (#:key inputs outputs #:allow-other-keys) + (with-directory-excursion + "../lite/tools/pip_package/gen/tflite_pip/python3" + ((assoc-ref py:%standard-phases 'install) + #:inputs inputs + #:outputs `(("out" . ,#$output:python)))))) (replace 'check (lambda* (#:key tests? #:allow-other-keys) (when tests? - (invoke "ctest" "-L" "plain"))))))) + (invoke "ctest" "-L" "plain")))) + (add-after 'install-python 'add-install-to-pythonpath + (lambda* (#:key inputs #:allow-other-keys) + ((assoc-ref py:%standard-phases 'add-install-to-pythonpath) + #:inputs inputs + #:outputs `(("out" . ,#$output:python))))) + (add-after 'add-install-to-pythonpath 'python-sanity-check + (lambda* (#:key tests? inputs #:allow-other-keys) + ((assoc-ref py:%standard-phases 'sanity-check) + #:inputs `(("sanity-check.py" . ,#$(default-sanity-check.py)) + ,@inputs) + #:outputs `(("out" . ,#$output:python)))))))) (inputs (list abseil-cpp cpuinfo @@ -3353,15 +3386,21 @@ find_library(ML_DTYPES_LIBRARIES opencl-headers opencl-icd-loader pthreadpool - python + python-wrapper python-ml-dtypes ruy re2 xnnpack - vulkan-headers)) + vulkan-headers + zlib)) + (propagated-inputs + (list python-numpy)) (native-inputs `(("pkg-config" ,pkg-config) ("googletest" ,googletest) + ("pybind11" ,pybind11) + ("python-wheel" ,python-wheel) + ("swig" ,swig) ("farmhash-src" ,(let ((commit "816a4ae622e964763ca0862d9dbd19324a1eaf45")) (origin