You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
14 KiB

  1. (define-module (gn packages tensorflow)
  2. #:use-module ((guix licenses) #:prefix license:)
  3. #:use-module (guix utils)
  4. #:use-module (guix packages)
  5. #:use-module (guix download)
  6. #:use-module (guix git-download)
  7. #:use-module (guix build-system gnu)
  8. #:use-module (gn packages bazel)
  9. #:use-module (gnu packages assembly)
  10. #:use-module (gnu packages base)
  11. #:use-module (gnu packages commencement)
  12. #:use-module (gnu packages compression)
  13. #:use-module (gnu packages curl)
  14. #:use-module (gnu packages databases)
  15. #:use-module (gnu packages gcc)
  16. #:use-module (gnu packages icu4c)
  17. #:use-module (gnu packages image)
  18. #:use-module (gnu packages maths)
  19. #:use-module (gnu packages mpi)
  20. #:use-module (gnu packages pcre)
  21. #:use-module (gnu packages protobuf)
  22. #:use-module (gnu packages python)
  23. #:use-module (gnu packages python-xyz)
  24. #:use-module (gnu packages rpc)
  25. #:use-module (gnu packages regex)
  26. #:use-module (gnu packages serialization)
  27. #:use-module (gnu packages sqlite)
  28. #:use-module (gnu packages version-control))
  29. (define-public tensorflow
  30. (package
  31. (name "tensorflow")
  32. (version "2.4.0")
  33. (source (origin
  34. (method git-fetch)
  35. (uri (git-reference
  36. (url "https://github.com/tensorflow/tensorflow")
  37. (commit (string-append "v" version))))
  38. (file-name (git-file-name name version))
  39. (sha256
  40. (base32
  41. "0yl06aypfxrcs35828xf04mkidz1x0j89v0q5h4d2xps1cb5rv3f"))))
  42. (build-system gnu-build-system)
  43. (arguments
  44. `(
  45. #:tests? #f ; Worry about this later.
  46. #:phases
  47. (modify-phases %standard-phases
  48. (delete 'configure)
  49. (add-before 'build 'pre-build
  50. (lambda* (#:key inputs outputs #:allow-other-keys)
  51. (let* ((out (assoc-ref outputs "out"))
  52. (jdk (assoc-ref inputs "jdk"))
  53. (HOME (string-append (getcwd) "/" ,name))
  54. (bazelrc (string-append HOME "/bazelrc"))
  55. (srcdeps (string-append HOME "/srcdeps"))
  56. )
  57. (setenv "HOME" HOME)
  58. (with-output-to-file bazelrc
  59. (lambda _
  60. (format #t
  61. "startup --server_javabase=~a~@
  62. startup --batch~@
  63. build --define=-copt=\"-Wl,-rpath -Wl,~a/lib\"~@
  64. build --color=yes~@
  65. build --action_env=PATH=~a~@
  66. build --action_env=PYTHON_LIB_PATH=~a~@
  67. build --define=PREFIX=~a~@
  68. build --host_javabase='@local_jdk//:jdk'~%"
  69. jdk (assoc-ref inputs "gcc:lib") (getenv "PATH") (getenv "PYTHONPATH") out)))
  70. (newline)
  71. (invoke "cat" bazelrc)
  72. (newline)
  73. (substitute* ".bazelrc"
  74. (("PREFIX=/usr") (string-append "PREFIX=" out)))
  75. ;; Use the pre-downloaded files
  76. (substitute* "WORKSPACE"
  77. (("\\\".*github.com/bazelbuild/rules_closure/.*.tar.gz")
  78. (string-append "\"file://" (assoc-ref inputs "io_bazel_rules_closure")))
  79. )
  80. (substitute* "third_party/toolchains/preconfig/generate/archives.bzl"
  81. (("\\\".*github.com/bazelbuild/bazel-toolchains/.*.tar.gz")
  82. (string-append "\"file://" (assoc-ref inputs "bazel_toolchains")))
  83. )
  84. (substitute* "tensorflow/workspace.bzl"
  85. (("\\\".*github.com/bazelbuild/rules_swift/.*.tar.gz")
  86. (string-append "\"file://" (assoc-ref inputs "build_bazel_rules_swift")))
  87. (("\\\".*github.com/googleapis/googleapis/.*.zip")
  88. (string-append "\"file://" (assoc-ref inputs "com_google_googleapis")))
  89. (("\\\".*github.com/bazelbuild/bazel-skylib/.*.tar.gz")
  90. (string-append "\"file://" (assoc-ref inputs "bazel_skylib")))
  91. (("\\\".*github.com/bazelbuild/rules_cc/.*.zip")
  92. (string-append "\"file://" (assoc-ref inputs "rules_cc")))
  93. (("\\\".*github.com/bazelbuild/rules_android/.*.zip")
  94. (string-append "\"file://" (assoc-ref inputs "build_bazel_rules_android")))
  95. )
  96. (substitute* "configure.py"
  97. (("/usr/bin/ldd") (which "ldd")))
  98. ;; Lets fix some hardcoded /usr/bins
  99. ;(substitute* "third_party/toolchains/preconfig/generate/generate.bzl"
  100. ; (("/usr/bin/python")
  101. ; (string-append (dirname (or (which "python")
  102. ; (which "python3")))
  103. ; "/python"))
  104. ; )
  105. ;; cpu detection fails, hardcode Linux as OS.
  106. ;(substitute* "third_party/gpus/cuda_configure.bzl"
  107. ; ((".*Linux.*") " if True:\n"))
  108. ;(substitute* "third_party/remote_config/common.bzl"
  109. ; (("uname") (which "uname")))
  110. ;(substitute* "third_party/remote_config/common.bzl"
  111. ; ;(("\\\"which\\\"") (string-append "\"" (which "which") "\""))
  112. ; (("python_bin_path =.*") (string-append "python_bin_path = \"" (which "python") "\"\n"))
  113. ; )
  114. ;; Copy sources to one directory
  115. ;(mkdir-p srcdeps)
  116. ;(for-each
  117. ; (lambda (file)
  118. ; (copy-recursively file (string-append srcdeps "/" (strip-store-file-name file))))
  119. ; (list (assoc-ref inputs "io_bazel_rules_closure")))
  120. #t)))
  121. (replace 'build
  122. (lambda* (#:key inputs #:allow-other-keys)
  123. (let* ((jdk (assoc-ref inputs "jdk"))
  124. (HOME (string-append (getcwd) "/" ,name))
  125. (bazelrc (string-append HOME "/bazelrc"))
  126. (srcdeps (string-append HOME "/srcdeps"))
  127. )
  128. (setenv "TF_SYSTEM_LIBS" (string-append "absl_py,"
  129. "astor_archive,"
  130. "astunparse_archive,"
  131. ;"boringssl,"
  132. ;"com_github_googleapis_googleapis,"
  133. ;"com_github_googlecloudplatform_google_cloud_cpp,"
  134. "com_github_grpc_grpc,"
  135. "com_google_protobuf,"
  136. "com_googlesource_code_re2,"
  137. "curl,"
  138. "cython,"
  139. "dill_archive,"
  140. "double_conversion,"
  141. ;"enum34_archive," ; python2 only?
  142. "flatbuffers,"
  143. ;"functools32_archive," ; python2 only?
  144. "gast_archive,"
  145. "gif,"
  146. "hwloc,"
  147. "icu,"
  148. "jsoncpp_git,"
  149. "libjpeg_turbo,"
  150. "lmdb,"
  151. "nasm,"
  152. ;"nsync,"
  153. ;"opt_einsum_archive,"
  154. "org_sqlite,"
  155. ;"pasta,"
  156. "pcre,"
  157. "png,"
  158. "pybind11,"
  159. "six_archive,"
  160. "snappy,"
  161. "tblib_archive,"
  162. "termcolor_archive,"
  163. "typing_extensions_archive,"
  164. "wrapt,"
  165. "zlib"))
  166. (setenv "PYTHON_BIN_PATH" (which "python"))
  167. (setenv "GCC_HOST_COMPILER_PATH" (string-append (assoc-ref inputs "gcc-toolchain") "/bin/gcc"))
  168. (setenv "GCC_HOST_COMPILER_PREFIX" (string-append (assoc-ref inputs "gcc-toolchain") "/bin"))
  169. ;(setenv "LD_LIBRARY_PATH" (string-append (assoc-ref inputs "gcc:lib") "/lib"))
  170. (setenv "LD_LIBRARY_PATH" (string-append (assoc-ref inputs "gcc:lib")))
  171. ;(invoke "python3" "configure.py")
  172. (invoke "./configure")
  173. ;(substitute* ".tf_configure.bazelrc"
  174. ; (("PYTHON_LIB_PATH.*") (string-append "PYTHON_LIB_PATH=\"" (getenv "PYTHONPATH") "\"\n")))
  175. (invoke "bazel"
  176. (string-append "--bazelrc=" bazelrc)
  177. "build"
  178. ;"--config=opt"
  179. "--config=avx_linux"
  180. ;"//tensorflow:libtensorflow_cc.so"
  181. "//tensorflow/tools/pip_package:build_pip_package"
  182. ;"//tensorflow/tools/lib_package:libtensorflow"
  183. )
  184. )
  185. ))
  186. )
  187. ))
  188. (native-inputs
  189. `(
  190. ("bazel" ,bazel-bootstrap)
  191. ("gcc:lib" ,gcc "lib")
  192. ("gcc-toolchain" ,gcc-toolchain)
  193. ("git" ,git)
  194. ("python" ,python)
  195. ("which" ,which)
  196. ;; https://github.com/tensorflow/tensorflow/blob/master/third_party/systemlibs/syslibs_configure.bzl
  197. ("absl_py" ,python-absl-py)
  198. ("astor_archive" ,python-astor)
  199. ("astunparse_archive" ,python-astunparse)
  200. ("com_googlesource_code_re2" ,re2)
  201. ("com_google_protobuf" ,protobuf)
  202. ("com_github_grpc_grpc" ,grpc)
  203. ("curl" ,curl)
  204. ("cython" ,python-cython)
  205. ("dill_archive" ,python-dill)
  206. ("double-conversion" ,double-conversion)
  207. ("flatbuffers" ,flatbuffers)
  208. ("gast_archive" ,python-gast)
  209. ("gif" ,giflib)
  210. ("hwloc" ,hwloc)
  211. ("icu" ,icu4c)
  212. ("jsoncpp_git" ,jsoncpp)
  213. ("libjpeg_turbo" ,libjpeg-turbo)
  214. ("lmdb" ,lmdb)
  215. ("nasm" ,nasm)
  216. ("org_sqlite" ,sqlite)
  217. ("pcre" ,pcre2)
  218. ("png" ,libpng)
  219. ("pybind11" ,pybind11)
  220. ("six_archive" ,python-six)
  221. ("snappy" ,snappy)
  222. ("tblib_archive" ,python-tblib)
  223. ("termcolor_archive" ,python-termcolor)
  224. ("typing_extensions_archive" ,python-typing-extensions)
  225. ("wrapt" ,python-wrapt)
  226. ("zlib" ,zlib)
  227. ("io_bazel_rules_closure"
  228. ,(origin
  229. (method url-fetch)
  230. (uri "https://github.com/bazelbuild/rules_closure/archive/308b05b2419edb5c8ee0471b67a40403df940149.tar.gz")
  231. (file-name "io_bazel_rules_closure.tar.gz")
  232. (sha256
  233. (base32
  234. "1abbgk05f9flpv35zzw9fjjdvd3g1d8b0vbk0f2z4wfx10ykh02v"))))
  235. ("bazel_toolchains"
  236. ,(origin
  237. (method url-fetch)
  238. (uri "https://github.com/bazelbuild/bazel-toolchains/archive/92dd8a7a518a2fb7ba992d47c8b38299fe0be825.tar.gz")
  239. (file-name "bazel_toolchains.tar.gz")
  240. (sha256
  241. (base32
  242. "1xgyalc8pvy76rghkby28vz5y9ng1qhrqhn817vfwa1ny3wiis48"))))
  243. ("build_bazel_rules_swift"
  244. ,(origin
  245. (method url-fetch)
  246. (uri "https://github.com/bazelbuild/rules_swift/archive/3eeeb53cebda55b349d64c9fc144e18c5f7c0eb8.tar.gz")
  247. (file-name "build_bazel_rules_swift.tar.gz")
  248. (sha256
  249. (base32
  250. "11n8fmb5mf2zwgn202p8nmh82c8i1hm90pvajdks65yqvb33p0yh"))))
  251. ("com_google_googleapis"
  252. ,(origin
  253. (method url-fetch)
  254. (uri "https://github.com/googleapis/googleapis/archive/541b1ded4abadcc38e8178680b0677f65594ea6f.zip")
  255. (file-name "com_google_googleapis.zip")
  256. (sha256
  257. (base32
  258. "0059835rzfr9mb9x5m6ij7pi1y372qzdqls4a6vg8mf50qdv1fky"))))
  259. ("bazel_skylib"
  260. ,(origin
  261. (method url-fetch)
  262. (uri "https://github.com/bazelbuild/bazel-skylib/releases/download/0.9.0/bazel_skylib-0.9.0.tar.gz")
  263. (file-name "bazel_skylib-0.9.0.tar.gz")
  264. (sha256
  265. (base32
  266. "183w7h2cyzlq7q5q86yivzdwafyxvxc5dqii0yvksnhjj5a3dphx"))))
  267. ("rules_cc"
  268. ,(origin
  269. (method url-fetch)
  270. (uri "https://github.com/bazelbuild/rules_cc/archive/01d4a48911d5e7591ecb1c06d3b8af47fe872371.zip")
  271. (file-name "rules_cc.zip")
  272. (sha256
  273. (base32
  274. "1ybcjfwjykk7qii17dr640jivxssy1hb3x0hbd65bh461jlpcfyg"))))
  275. ("build_bazel_rules_android"
  276. ,(origin
  277. (method url-fetch)
  278. (uri "https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip")
  279. (file-name "build_bazel_rules_android.zip")
  280. (sha256
  281. (base32
  282. "01kqga5xrkchw4kmr60r4m5xmy90zhxh1yb59mp94ndvv1fx21nd"))))
  283. ))
  284. (home-page "https://www.tensorflow.org/")
  285. (synopsis "Machine learning framework")
  286. (description "TensorFlow is a flexible platform for building and training
  287. machine learning models. It provides a library for high performance numerical
  288. computation and includes high level Python APIs, including both a sequential API
  289. for beginners that allows users to build models quickly by plugging together
  290. building blocks and a subclassing API with an imperative style for advanced
  291. research.")
  292. (license license:asl2.0)))