diff options
| author | Ricardo Wurmus <rekado@elephly.net> | 2023-10-21 18:13:28 +0200 |
|---|---|---|
| committer | Ricardo Wurmus <rekado@elephly.net> | 2023-10-23 11:26:41 +0200 |
| commit | d96d6d5bd9af064d4c762c45b94c5122d5836217 (patch) | |
| tree | 420c3b8b994a1c8417a4b3b8c2d55770852eca80 | |
| parent | 4571694424273794976f94806913af0ce5193ebf (diff) | |
python: Add tensorflow.
* guix-science/packages/python.scm (tensorflow): New variable.
| -rw-r--r-- | guix-science/packages/python.scm | 331 |
1 files changed, 331 insertions, 0 deletions
diff --git a/guix-science/packages/python.scm b/guix-science/packages/python.scm index 575e79e..8948b73 100644 --- a/guix-science/packages/python.scm +++ b/guix-science/packages/python.scm | |||
| @@ -24,6 +24,7 @@ | |||
| 24 | #:use-module (gnu packages check) | 24 | #:use-module (gnu packages check) |
| 25 | #:use-module (gnu packages compression) | 25 | #:use-module (gnu packages compression) |
| 26 | #:use-module (gnu packages curl) | 26 | #:use-module (gnu packages curl) |
| 27 | #:use-module (gnu packages elf) | ||
| 27 | #:use-module (gnu packages graph) | 28 | #:use-module (gnu packages graph) |
| 28 | #:use-module (gnu packages icu4c) | 29 | #:use-module (gnu packages icu4c) |
| 29 | #:use-module (gnu packages image) | 30 | #:use-module (gnu packages image) |
| @@ -32,6 +33,7 @@ | |||
| 32 | #:use-module (gnu packages maths) | 33 | #:use-module (gnu packages maths) |
| 33 | #:use-module (gnu packages mpi) | 34 | #:use-module (gnu packages mpi) |
| 34 | #:use-module (gnu packages pdf) | 35 | #:use-module (gnu packages pdf) |
| 36 | #:use-module (gnu packages perl) | ||
| 35 | #:use-module (gnu packages protobuf) | 37 | #:use-module (gnu packages protobuf) |
| 36 | #:use-module (gnu packages python) | 38 | #:use-module (gnu packages python) |
| 37 | #:use-module (gnu packages python-build) | 39 | #:use-module (gnu packages python-build) |
| @@ -1322,3 +1324,332 @@ reverse-mode differentiation (a.k.a. backpropagation) via grad as well | |||
| 1322 | as forward-mode differentiation, and the two can be composed | 1324 | as forward-mode differentiation, and the two can be composed |
| 1323 | arbitrarily to any order.") | 1325 | arbitrarily to any order.") |
| 1324 | (license license:asl2.0))) | 1326 | (license license:asl2.0))) |
| 1327 | |||
| 1328 | (define-public tensorflow | ||
| 1329 | (package | ||
| 1330 | (name "tensorflow") | ||
| 1331 | (version "2.13.1") | ||
| 1332 | (source | ||
| 1333 | (origin | ||
| 1334 | (method git-fetch) | ||
| 1335 | (uri (git-reference | ||
| 1336 | (url "https://github.com/tensorflow/tensorflow/") | ||
| 1337 | (commit (string-append "v" version)))) | ||
| 1338 | (file-name (git-file-name name version)) | ||
| 1339 | (sha256 | ||
| 1340 | (base32 "09mfskmpvpbq919wibnw3bnhi1y3hkx3qrzm72gdr0gsivn1yb3w")))) | ||
| 1341 | (build-system gnu-build-system) | ||
| 1342 | (arguments | ||
| 1343 | (list | ||
| 1344 | #:modules | ||
| 1345 | '((guix build gnu-build-system) | ||
| 1346 | (guix build utils) | ||
| 1347 | (srfi srfi-1) | ||
| 1348 | (ice-9 string-fun)) | ||
| 1349 | #:tests? #false ;there are none | ||
| 1350 | #:phases | ||
| 1351 | #~(modify-phases %standard-phases | ||
| 1352 | (replace 'configure | ||
| 1353 | (lambda* (#:key inputs #:allow-other-keys) | ||
| 1354 | (define bazel-out | ||
| 1355 | (string-append (getenv "NIX_BUILD_TOP") "/output")) | ||
| 1356 | (mkdir-p bazel-out) | ||
| 1357 | (with-directory-excursion bazel-out | ||
| 1358 | (invoke "tar" "xf" #$(bazel-vendored-inputs | ||
| 1359 | #:source (package-source this-package) | ||
| 1360 | #:name "tensorflow" | ||
| 1361 | #:version version | ||
| 1362 | #:inputs | ||
| 1363 | (append (standard-packages) | ||
| 1364 | (package-inputs this-package) | ||
| 1365 | (package-propagated-inputs this-package) | ||
| 1366 | (package-native-inputs this-package)) | ||
| 1367 | #:search-paths | ||
| 1368 | (map search-path-specification->sexp | ||
| 1369 | (package-transitive-native-search-paths | ||
| 1370 | this-package)) | ||
| 1371 | #:extra-configuration | ||
| 1372 | ;; TODO: is the union build *really* necessary? | ||
| 1373 | (with-imported-modules (source-module-closure '((guix build utils) | ||
| 1374 | (guix build union) | ||
| 1375 | (guix build gnu-build-system))) | ||
| 1376 | #~(begin | ||
| 1377 | (use-modules (guix build union)) | ||
| 1378 | (union-build (string-append (getenv "NIX_BUILD_TOP") "/site-library") | ||
| 1379 | (parse-path (getenv "GUIX_PYTHONPATH"))) | ||
| 1380 | (setenv "PYTHON_LIB_PATH" (string-append (getenv "NIX_BUILD_TOP") "/site-library")) | ||
| 1381 | (setenv "PYTHON_BIN_PATH" | ||
| 1382 | (string-append | ||
| 1383 | #$(this-package-input "python-wrapper") | ||
| 1384 | "/bin/python")) | ||
| 1385 | (setenv "TF_PYTHON_VERSION" | ||
| 1386 | #$(version-major+minor | ||
| 1387 | (package-version (this-package-input "python-wrapper")))) | ||
| 1388 | (setenv "TF_SYSTEM_LIBS" | ||
| 1389 | (string-join (list | ||
| 1390 | ;;"absl_py" | ||
| 1391 | ;;"boringssl" | ||
| 1392 | "com_github_grpc_grpc" | ||
| 1393 | ;;"com_google_protobuf" | ||
| 1394 | "curl" | ||
| 1395 | "cython" | ||
| 1396 | ;;"dill_archive" | ||
| 1397 | "double_conversion" | ||
| 1398 | "flatbuffers" | ||
| 1399 | ;;"functools32_archive" | ||
| 1400 | ;;"gast_archive" | ||
| 1401 | "gif" | ||
| 1402 | "hwloc" | ||
| 1403 | "icu" | ||
| 1404 | ;;"jsoncpp_git" | ||
| 1405 | "libjpeg_turbo" | ||
| 1406 | "zlib") | ||
| 1407 | ",")))) | ||
| 1408 | #:bazel-targets | ||
| 1409 | (list "//tensorflow/tools/pip_package:build_pip_package" | ||
| 1410 | "//tensorflow/tools/lib_package:libtensorflow") | ||
| 1411 | #:bazel-arguments | ||
| 1412 | #~(list | ||
| 1413 | "--extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain_nonstrict" | ||
| 1414 | "--action_env=PYTHON_LIB_PATH" | ||
| 1415 | "--host_action_env=PYTHON_LIB_PATH" | ||
| 1416 | "--action_env=PYTHON_BIN_PATH" | ||
| 1417 | "--host_action_env=PYTHON_BIN_PATH" | ||
| 1418 | ;; "--action_env=GUIX_PYTHONPATH" | ||
| 1419 | ;; "--action_env=PYTHONPATH" | ||
| 1420 | ;; "--host_action_env=GUIX_PYTHONPATH" | ||
| 1421 | ;; "--host_action_env=PYTHONPATH" | ||
| 1422 | (string-append "--python_path=" | ||
| 1423 | #$(this-package-input "python-wrapper") | ||
| 1424 | "/bin/python")) | ||
| 1425 | #:hash | ||
| 1426 | "0p392v8iwp6y8jxpib86bw46yjxb0lcan8jvbcb30f6938kfqc82"))) | ||
| 1427 | |||
| 1428 | ;; Rewrite dangling links to current build directory | ||
| 1429 | (for-each (lambda (file) | ||
| 1430 | (let ((new-target | ||
| 1431 | (string-replace-substring | ||
| 1432 | (readlink file) | ||
| 1433 | "GUIX_BUILD_TOP" (getenv "NIX_BUILD_TOP")))) | ||
| 1434 | (delete-file file) | ||
| 1435 | (symlink new-target file))) | ||
| 1436 | (find-files bazel-out | ||
| 1437 | (lambda (file-name stat) | ||
| 1438 | (and (eq? (stat:type stat) 'symlink) | ||
| 1439 | (string-contains (readlink file-name) | ||
| 1440 | "GUIX_BUILD_TOP"))) | ||
| 1441 | #:stat lstat)) | ||
| 1442 | (setenv "HOME" (getenv "NIX_BUILD_TOP")) | ||
| 1443 | |||
| 1444 | ;; Bazel aborts when a source file includes a header | ||
| 1445 | ;; that isn't declared. It prints something like this: | ||
| 1446 | ;; | ||
| 1447 | ;; "this rule is missing dependency declarations for | ||
| 1448 | ;; the following files included by..." | ||
| 1449 | ;; | ||
| 1450 | ;; Since we pass through C_INCLUDE_PATH and | ||
| 1451 | ;; CPLUS_INCLUDE_PATH there are many headers that are | ||
| 1452 | ;; visible to the toolchain, but that Bazel refuses to | ||
| 1453 | ;; allow. | ||
| 1454 | ;; | ||
| 1455 | ;; The biggest problem is that the kernel headers are | ||
| 1456 | ;; never declared as dependencies anywhere, so we need | ||
| 1457 | ;; to modify the toolchain declaration to allow headers | ||
| 1458 | ;; from this location. | ||
| 1459 | ;; | ||
| 1460 | ;; There are other headers that cause trouble, though, | ||
| 1461 | ;; such as those for zlib in the | ||
| 1462 | ;; @com_google_protobuf//:protobuf target. There must | ||
| 1463 | ;; be some other mechanism to fix this (e.g. in the | ||
| 1464 | ;; protobuf target itself), but I find it easier to just | ||
| 1465 | ;; allow all locations that appear on these INCLUDE_PATH | ||
| 1466 | ;; variables. | ||
| 1467 | (substitute* (string-append bazel-out "/external/local_config_cc/BUILD") | ||
| 1468 | (("cxx_builtin_include_directories = \\[" m) | ||
| 1469 | (string-append m | ||
| 1470 | (string-join | ||
| 1471 | (map | ||
| 1472 | (lambda (dir) (string-append "\"" dir "\"")) | ||
| 1473 | (append (parse-path (getenv "C_INCLUDE_PATH") '()) | ||
| 1474 | (parse-path (getenv "CPLUS_INCLUDE_PATH") '()))) | ||
| 1475 | "," 'suffix)))) | ||
| 1476 | ;; XXX: Our version of protobuf leads to "File already | ||
| 1477 | ;; exists in database" when loading in Python. | ||
| 1478 | (substitute* (string-append bazel-out "/external/tf_runtime/third_party/systemlibs/protobuf.BUILD") | ||
| 1479 | (("-lprotobuf") "-l:libprotobuf.a") | ||
| 1480 | (("-lprotoc") "-l:libprotoc.a")))) | ||
| 1481 | (replace 'build | ||
| 1482 | (lambda* (#:key parallel-build? #:allow-other-keys) | ||
| 1483 | (define %build-directory (getenv "NIX_BUILD_TOP")) | ||
| 1484 | (define %bazel-out | ||
| 1485 | (string-append %build-directory "/output")) | ||
| 1486 | (define %bazel-user-root | ||
| 1487 | (string-append %build-directory "/tmp")) | ||
| 1488 | (setenv "BAZEL_USE_CPP_ONLY_TOOLCHAIN" "1") | ||
| 1489 | (setenv "USER" "homeless-shelter") | ||
| 1490 | (setenv "TF_SYSTEM_LIBS" | ||
| 1491 | (string-join (list | ||
| 1492 | ;;"absl_py" | ||
| 1493 | ;;"boringssl" | ||
| 1494 | "com_github_grpc_grpc" | ||
| 1495 | ;;"com_google_protobuf" | ||
| 1496 | "curl" | ||
| 1497 | "cython" | ||
| 1498 | ;;"dill_archive" | ||
| 1499 | "double_conversion" | ||
| 1500 | "flatbuffers" | ||
| 1501 | ;;"functools32_archive" | ||
| 1502 | ;;"gast_archive" | ||
| 1503 | "gif" | ||
| 1504 | "hwloc" | ||
| 1505 | "icu" | ||
| 1506 | ;;"jsoncpp_git" | ||
| 1507 | "libjpeg_turbo" | ||
| 1508 | "zlib") | ||
| 1509 | ",")) | ||
| 1510 | (apply invoke "bazel" | ||
| 1511 | "--batch" | ||
| 1512 | (string-append "--output_base=" %bazel-out) | ||
| 1513 | (string-append "--output_user_root=" %bazel-user-root) | ||
| 1514 | "build" | ||
| 1515 | "--nofetch" | ||
| 1516 | "--distinct_host_configuration=false" | ||
| 1517 | "--curses=no" | ||
| 1518 | "--verbose_failures" | ||
| 1519 | "--subcommands" | ||
| 1520 | "--strategy=Genrule=local" | ||
| 1521 | "--toolchain_resolution_debug=\".*\"" | ||
| 1522 | "--local_ram_resources=HOST_RAM*.5" | ||
| 1523 | "--local_cpu_resources=HOST_CPUS*.75" | ||
| 1524 | "--action_env=PATH" | ||
| 1525 | "--action_env=GUIX_PYTHONPATH" | ||
| 1526 | "--action_env=PYTHONPATH" | ||
| 1527 | "--action_env=LIBRARY_PATH" | ||
| 1528 | "--action_env=C_INCLUDE_PATH" | ||
| 1529 | "--action_env=CPLUS_INCLUDE_PATH" | ||
| 1530 | "--action_env=GUIX_LOCPATH" | ||
| 1531 | "--action_env=TF_SYSTEM_LIBS" | ||
| 1532 | "--host_action_env=TF_SYSTEM_LIBS" | ||
| 1533 | "--host_action_env=PATH" | ||
| 1534 | "--host_action_env=GUIX_PYTHONPATH" | ||
| 1535 | "--host_action_env=PYTHONPATH" | ||
| 1536 | "--host_action_env=LIBRARY_PATH" | ||
| 1537 | "--host_action_env=C_INCLUDE_PATH" | ||
| 1538 | "--host_action_env=CPLUS_INCLUDE_PATH" | ||
| 1539 | "--host_action_env=GUIX_LOCPATH" | ||
| 1540 | "-c" "opt" | ||
| 1541 | "--jobs" | ||
| 1542 | (if parallel-build? | ||
| 1543 | (number->string (parallel-job-count)) | ||
| 1544 | "1") | ||
| 1545 | (list "//tensorflow/tools/pip_package:build_pip_package" | ||
| 1546 | "//tensorflow/tools/lib_package:libtensorflow")))) | ||
| 1547 | (replace 'install | ||
| 1548 | (lambda _ | ||
| 1549 | ;; Install library | ||
| 1550 | (mkdir-p #$output) | ||
| 1551 | (invoke "tar" "-xf" | ||
| 1552 | "bazel-bin/tensorflow/tools/lib_package/libtensorflow.tar.gz" | ||
| 1553 | "-C" #$output) | ||
| 1554 | |||
| 1555 | ;; Write pkgconfig file | ||
| 1556 | (mkdir-p (string-append #$output "/lib/pkgconfig")) | ||
| 1557 | (call-with-output-file (string-append #$output "/lib/pkgconfig/tensorflow.pc") | ||
| 1558 | (lambda (port) | ||
| 1559 | (format port "\ | ||
| 1560 | Name: TensorFlow | ||
| 1561 | Version: ~a | ||
| 1562 | Description: Library for computation using data flow graphs for scalable machine learning | ||
| 1563 | Requires: | ||
| 1564 | Libs: -L~a/lib -ltensorflow | ||
| 1565 | Cflags: -I~a/include/tensorflow | ||
| 1566 | " #$version #$output #$output))) | ||
| 1567 | |||
| 1568 | ;; Install python bindings | ||
| 1569 | ;; Build the source code, then copy it to the "python" output. | ||
| 1570 | ;; | ||
| 1571 | ;; TODO: build_pip_package includes symlinks so we must | ||
| 1572 | ;; dereference them. | ||
| 1573 | (let ((here (string-append (getcwd) "/dist"))) | ||
| 1574 | (invoke "bazel-bin/tensorflow/tools/pip_package/build_pip_package" | ||
| 1575 | "--src" here) | ||
| 1576 | (copy-recursively here #$output:python))))))) | ||
| 1577 | (outputs '("out" "python")) | ||
| 1578 | (inputs | ||
| 1579 | (list curl | ||
| 1580 | double-conversion | ||
| 1581 | flatbuffers-next | ||
| 1582 | giflib | ||
| 1583 | grpc | ||
| 1584 | hwloc | ||
| 1585 | icu4c | ||
| 1586 | jsoncpp | ||
| 1587 | libjpeg-turbo | ||
| 1588 | openssl | ||
| 1589 | ;; XXX: With our own version of Protobuf we see a segfault | ||
| 1590 | ;; when building the Python bindings. | ||
| 1591 | pybind11 | ||
| 1592 | python-absl-py | ||
| 1593 | python-cython | ||
| 1594 | python-numpy | ||
| 1595 | python-scipy | ||
| 1596 | python-six | ||
| 1597 | python-wrapper | ||
| 1598 | ;; Wrong version of snappy? | ||
| 1599 | ;; external/tsl/tsl/platform/default/port.cc:328:11: error: | ||
| 1600 | ;; 'RawCompressFromIOVec' is not a member of 'snappy'; did | ||
| 1601 | ;; you mean 'RawUncompressToIOVec'? | ||
| 1602 | ;; snappy | ||
| 1603 | zlib)) | ||
| 1604 | ;; TODO: these inputs probably should not be propagated. They are | ||
| 1605 | ;; only needed for building the Python sources. | ||
| 1606 | (propagated-inputs | ||
| 1607 | (list python-absl-py | ||
| 1608 | python-cachetools | ||
| 1609 | python-certifi | ||
| 1610 | python-charset-normalizer | ||
| 1611 | python-flatbuffers | ||
| 1612 | python-grpcio | ||
| 1613 | python-h5py | ||
| 1614 | python-idna | ||
| 1615 | python-jax | ||
| 1616 | python-markdown | ||
| 1617 | python-markupsafe | ||
| 1618 | python-ml-dtypes | ||
| 1619 | python-numpy | ||
| 1620 | python-oauthlib | ||
| 1621 | python-opt-einsum | ||
| 1622 | python-packaging | ||
| 1623 | python-portpicker | ||
| 1624 | ;; This leads to a segfault when generating the Python bindings. | ||
| 1625 | ;;python-protobuf | ||
| 1626 | python-psutil | ||
| 1627 | python-pyasn1 | ||
| 1628 | python-requests | ||
| 1629 | python-requests-oauthlib | ||
| 1630 | python-rsa | ||
| 1631 | python-scipy | ||
| 1632 | python-six | ||
| 1633 | python-termcolor | ||
| 1634 | python-urllib3 | ||
| 1635 | python-werkzeug)) | ||
| 1636 | ;; patchelf is needed by build_pip_package (in the install phase) | ||
| 1637 | ;; to update references to libraries we had just built earlier for | ||
| 1638 | ;; the Python bindings. The build system features a remarkable | ||
| 1639 | ;; amount of chewing gum and duck tape. | ||
| 1640 | (native-inputs | ||
| 1641 | (list patchelf | ||
| 1642 | perl | ||
| 1643 | python-lit python-pypa-build python-setuptools python-wheel | ||
| 1644 | bazel-6 | ||
| 1645 | which | ||
| 1646 | `(,openjdk11 "jdk"))) ;for bazel | ||
| 1647 | (home-page "https://tensorflow.org") | ||
| 1648 | (synopsis "Machine learning framework") | ||
| 1649 | (description "TensorFlow is a flexible platform for building and | ||
| 1650 | training machine learning models. It provides a library for high | ||
| 1651 | performance numerical computation and includes high level Python APIs, | ||
| 1652 | including both a sequential API for beginners that allows users to | ||
| 1653 | build models quickly by plugging together building blocks and a | ||
| 1654 | subclassing API with an imperative style for advanced research.") | ||
| 1655 | (license license:asl2.0))) | ||
