Skip to content

python3Packages.tensorflow: 2.4.2 -> 2.6.0 - #137069

Closed
samuela wants to merge 20 commits into
NixOS:masterfrom
samuela:samuela/tensorflow
Closed

samuela wants to merge 20 commits into
NixOS:masterfrom
samuela:samuela/tensorflow

Conversation

@samuela

@samuela samuela commented Sep 8, 2021 •

Copy link
Copy Markdown
Member
Motivation for this change

Upgrade tensorflow to the latest version.

Upgrading tensorflow required a number of changes to related libraries and dependencies. For some reason tensorflow 2.6.0 introduces a dependency on clang, the Python package, so we must package that as well. In addition, keras and tensorflow-estimator must be kept up to date with the same version as tensorflow, hence those commits as well.

The main blocker right now is that tensorflowWithCuda is broken. It fails with the message patchelf: maximum file size exceeded in the final install phase. This has been fixed. This appears to be related to

TODO:

  • The default tensorflow builds and passes tests (though it takes 24 CPU hours)
  • Fix tensorflowWithCuda

Optionally,

  • Should we also upgrade the -bin derivation?
Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • 21.11 Release Notes (or backporting 21.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

@github-actions github-actions Bot added the 6.topic: python Python is a high-level, general-purpose programming language. label Sep 8, 2021
@samuela

samuela commented Sep 8, 2021

Copy link
Copy Markdown
Member Author

It turns out that gast ~= 0.4 requirement cannot be safely relaxed to gast >= 0.4 as I discovered while packaging dm-sonnet. Therefore I packaged both gast_0_4 and gast_0_5 in 44b4708, with the gast alias pointing to gast_0_5 for backwards compatibility.

On another note, I'm thoroughly stumped as to why CI is failing with the following error,

...
checking Nixpkgs on aarch64-darwin
error: undefined variable 'flatbuffers_2' at /nix/store/qyciq1vg1yzk2l8gc117b5yp6v1ly3p5-source/pkgs/top-level/python-packages.nix:2704:17

It builds without issue for me locally, and I cannot find any flaw with the changes made to python-packages.nix. Does anyone have any ideas?

@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-to-work-around-patchelf-maximum-file-size-exceeded-error/14920/1

@samuela

samuela commented Sep 8, 2021

Copy link
Copy Markdown
Member Author

Resolved the flatbuffers issue in 94f32d8.

@ofborg ofborg Bot added the 8.has: package (new) This PR adds a new package label Sep 8, 2021
@ofborg
ofborg Bot requested review from abbradar and cpcloud September 8, 2021 23:28
@ofborg ofborg Bot added 10.rebuild-darwin: 11-100 This PR causes between 11 and 100 packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux. labels Sep 8, 2021
@cpcloud

cpcloud commented Sep 8, 2021

Copy link
Copy Markdown
Contributor

Heroic effort! The gast changes LGTM!

@samuela

samuela commented Sep 9, 2021

Copy link
Copy Markdown
Member Author

Ok, I managed to fix the tensorflowWithCuda build in samuela@d4d40f6. The trickiness here is that there's a patchelf bug we have to workaround. See NixOS/patchelf#256 for more info.

Now that everything is building this PR is ready for review!

@samuela
samuela marked this pull request as ready for review September 9, 2021 01:49
@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/584

Comment thread pkgs/top-level/python-packages.nix Resolved

ckcc-protocol = callPackage ../development/python-modules/ckcc-protocol { };

inherit (import ../development/python-modules/clang self)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we want to get rid of multiple versions as soon as possible I don't think we should convert this into a common package definition. Just copy the current file and adjust it for the older version or only use overrideAttr if possible.

Also I am not sure if we even can/want to introduce multiple version of the same package.

/cc @dotlambda

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better or worse, tensorflow 2.6.0 depends on clang v5. The currently packaged version is v11, so I didn't want to break compatibility for people already using v11.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to package it any way that you feel is most appropriate as long as tensorflow can get access to clang_5.

@mschwaig

Copy link
Copy Markdown
Member

Using amd64-linux build machines I can build python38Package.tensorflow for both amd64-linux and also for aarch64-linux using emulation and a command like

nix build .#defaultPackage.aarch64-linux

as part of a flake with nixpkgs from this PR (c29885e).

Unfortunately I found that aarch64-linux build under emulation unreliable. It went through only on the second try one system and it's always failing on the other system I tried it on (CI). The error I get is always the same though, even including the wrong hash I get.

[...]
tensorflow> INFO: Analyzed 2 targets (419 packages loaded, 20828 targets configured).
tensorflow> INFO: Found 2 targets...
tensorflow> 
tensorflow> INFO: Elapsed time: 564.706s
tensorflow> INFO: 0 processes.
tensorflow> INFO: Build completed successfully, 0 total actions
tensorflow> INFO: Build completed successfully, 0 total actions
tensorflow> @nix { "action": "setPhase", "phase": "installPhase" }
tensorflow> installing
error: hash mismatch in fixed-output derivation '/nix/store/ddg68niqgh1zimwb9qqi2hisncr1q3fv-tensorflow-2.6.0-deps.tar.gz.drv':
         specified: sha256-uluy/WfgttsYqoUb+YhYMkMBOKblKeR643pcHWyxNwk=
            got:    sha256-PryC8pnWd4jf97dusb0rnPjcbXWiI4btXlJK4T6x2IM=
error: 1 dependencies of derivation '/nix/store/h61vdfpn08bz4l9fxhvfld3alhv8l8lz-tensorflow-2.6.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/0fryb5p9hijc2lp6a5hqf9ay65ydhmxk-python3.8-tensorflow-2.6.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/aw1fvr3pdclf4gdrnz2krhp9fgq4bjnj-python3-3.8.11-env.drv' failed to build

Maybe someone knows what's going on here. I hope it's OK I bring this up here.

I'm really happy about @samuela's efforts to move tensorflow forward in nixpkgs. As far as I can tell the TensorFlow version currently available in nixpkgs is completely unusable because the currently packaged versions of keras and tensorflow are incompatible.

@samuela

samuela commented Sep 28, 2021

Copy link
Copy Markdown
Member Author

Hmm, odd. I haven't tested the ARM build. That seems like an odd error considering it happens on one system and not the other...

@mschwaig

Copy link
Copy Markdown
Member

It did happen on the other system as well, but only on the first try (I kept the output because it looked like a bug). Second attempt to do the same thing went through. The one where it's always failing is a CI Pipeline, so parts of the system get thrown away after each build.

Maybe I'll have to look up where that tensorflow-2.6.0-deps.tar.gz file comes from.

@dotlambda dotlambda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely not the way to go. Please check whether we can relax tensorflow's version constraints rather than introducing two new versioned attributes in python-packages.nix.

@ofborg ofborg Bot added 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 501-1000 This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 1001-2500 This PR causes many rebuilds on Linux and should target the staging branches. and removed 10.rebuild-darwin: 11-100 This PR causes between 11 and 100 packages to rebuild on Darwin. 10.rebuild-linux: 11-100 This PR causes between 11 and 100 packages to rebuild on Linux. labels Sep 30, 2021
@samuela

samuela commented Sep 30, 2021

Copy link
Copy Markdown
Member Author

This is definitely not the way to go. Please check whether we can relax tensorflow's version constraints rather than introducing two new versioned attributes in python-packages.nix.

For better or worse, tensorflow is quite good about dependency version hygiene. For example, the gast version is capped for a reason (https://github.com/tensorflow/tensorflow/blob/v2.6.0/tensorflow/tools/pip_package/setup.py#L97-L99). And generally speaking, major clang versions have significant incompatibilities.

@mschwaig

mschwaig commented Sep 30, 2021 •

Copy link
Copy Markdown
Member

I had a look at what's wrong with that broken tensorflow-2.6.0-deps.tar.gz file.
On the machine where I got it to build for aarch64-linux on the second try I found files with both the good and the bad hash in the store:

mschwaig@hydralisk /n/store > ls -alh | grep tensorflow-2.6.0-deps.tar.gz
-r--r--r--     1 root     root    184M Jan  1  1970 36jx274amppq7vndys5kw1r3qhvxnsy6-tensorflow-2.6.0-deps.tar.gz
-r--r--r--     2 root     root    193M Jan  1  1970 955jl5k0f9h1gah3hr2g4lzypi1wz62h-tensorflow-2.6.0-deps.tar.gz
mschwaig@hydralisk ~> nix hash file /nix/store/36jx274amppq7vndys5kw1r3qhvxnsy6-tensorflow-2.6.0-deps.tar.gz
sha256-PryC8pnWd4jf97dusb0rnPjcbXWiI4btXlJK4T6x2IM=
mschwaig@hydralisk ~ > nix hash file /nix/store/955jl5k0f9h1gah3hr2g4lzypi1wz62h-tensorflow-2.6.0-deps.tar.gz
sha256-uluy/WfgttsYqoUb+YhYMkMBOKblKeR643pcHWyxNwk=

I then compared the two files using diffoscope.

mkl_dnn_v1 is missing in the archive that breaks the build. (Everything inside the external/mkl_dnn_v1/ folder as well as the file external/@mkl_dnn_v1.marker file is missing in the broken one, otherwise the two archives' contents are identical.)

Looking up MKL it seems to be an Intel libarary, so I would guess Bazel configures it out for everything but x86_64 builds.

I also found a change (#126034) which made it so that buildBazelPackage would only fetch what's actually configured for the current build.

Maybe the aarch64-linux build only goes through if you build for amd64-linux first so that buildBazelPackage can pick up a file with the expected hash by chance. If that's the case I think we could disable fetchConfigured or configure different expected hashes for builds with and without MKL to fix the issue.

@dotlambda

Copy link
Copy Markdown
Member

This is definitely not the way to go. Please check whether we can relax tensorflow's version constraints rather than introducing two new versioned attributes in python-packages.nix.

For better or worse, tensorflow is quite good about dependency version hygiene. For example, the gast version is capped for a reason (https://github.com/tensorflow/tensorflow/blob/v2.6.0/tensorflow/tools/pip_package/setup.py#L97-L99). And generally speaking, major clang versions have significant incompatibilities.

"newer versions are incompatible with the rest of the ecosystem" is a very unspecific reason. I'm just asking you to try to unpin these dependencies and see if anything breaks. If unpinning clang does break things, we should only package the version of clang that tensorflow needs. If unpinning gast breaks things, then we should think about downgrading that version. However, it seems like we successfully unpinned gast in the past.

};

checkInputs = [ nose ];
# The pypi distribution does not have any tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use fetchFromGitHub then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the comment is not required. It is very standard to do this, so much standard that there are no questions asked in review if you fetch from GitHub/etc instead of pypi.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no convenient GitHub repo for this package to fetch from unfortunately.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the thing we're using right now "is a non-official packaging directly from the debian packages for the purpose of pypi package." We should really just build Python bindings at the same time as clang.

@samuela samuela Oct 5, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, there's been some discussion on this (#89214). But for the moment that discussion seems to have stalled, so I propose that we move forward with sub-optimal pypi bindings and then switch to nice nix-built ones once they're ready.

@dpaetzel

Copy link
Copy Markdown
Contributor

Thank you for working on this! Is there any chance this gets merged soon? 🙂

@samuela

samuela commented Feb 22, 2022

Copy link
Copy Markdown
Member Author

tensorflow has been upgraded to 2.7 already so this can be closed now

@samuela samuela closed this Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: python Python is a high-level, general-purpose programming language. 8.has: package (new) This PR adds a new package 10.rebuild-darwin: 501-1000 This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 1001-2500 This PR causes many rebuilds on Linux and should target the staging branches.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants