Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[19.03] tensorflow-tensorboard requires tensorflow python module to be importable #61173

Closed
CMCDragonkai opened this issue May 9, 2019 · 5 comments

Comments

@CMCDragonkai
Copy link
Member

CMCDragonkai commented May 9, 2019

Issue description

In the latest 19.03, the new tensorflow-tensorboard Nix package exports an executable called tensorboard. To run this executable it needs to be able to import tensorflow. Otherwise it comes up with an error:

Traceback (most recent call last):
  File "/nix/store/4hpsjw1a9yvw2skx5kjjc0kp3lqy57ad-python3.6-tensorflow-tensorboard-1.13.0/bin/.tensorboard-wrapped", line 7, in <module>
    from tensorboard.main import run_main
  File "/nix/store/4hpsjw1a9yvw2skx5kjjc0kp3lqy57ad-python3.6-tensorflow-tensorboard-1.13.0/lib/python3.6/site-packages/tensorboard/main.py", line 42, in <module>
    import tensorflow as tf  # pylint: disable=unused-import
ModuleNotFoundError: No module named 'tensorflow'

Now usually I solved this inside nix-shell where I just add tensorflowWithoutCuda as a propagatedBuildInput, and then tensorboard works inside the nix-shell.

However if I'm trying build a custom package that calls tensorboard via subprocess.run in Python, even if my own package has tensorboardWithoutCuda in the propagatedBuildInputs, the tensorflow Python module is not added to the PYTHONPATH for the tensorboard executable. So when your own custom executable calls tensorboard, it results in tensorboard reporting that it cannot find tensorflow module.

Steps to reproduce

{
  pkgs ? import ./pkgs.nix,
  pythonPath ? "python36"
}:
  with pkgs;
  let
    python = lib.getAttrFromPath (lib.splitString "." pythonPath) pkgs;
  in
    python.pkgs.buildPythonApplication rec {
      pname = "tensorboard-dashboard";
      version = "0.0.1";
      src = lib.cleanSourceWith {
        filter = (path: type:
          ! (builtins.any
              (r: (builtins.match r (builtins.baseNameOf path)) != null)
              [
                "pip_packages"
                ".*\.egg-info"
                "\.env"
                "tmp"
                "logs"
              ])
        );
        src = lib.cleanSource ./.;
      };
      nativeBuildInputs = [
        makeWrapper
        python.pkgs.pip
      ];
      propagatedNativeBuildInputs = (with python.pkgs;[
        tensorflow-tensorboard
      ]);
      propagatedBuildInputs = (with python.pkgs; [
        setuptools
        tensorflowWithoutCuda # tensorboard requires tensorflow
      ]);
      # remove this when setuptools becomes compatible with tensorflow's setuptools requirement
      installFlags = [ "--no-deps" ];
    }

If you have a setup.py that registers this executable:

#!/usr/bin/env python3

import subprocess

def main():
    print('Running')
    subprocess.run(['tensorboard', '--help'])
    print('Ran')

if __name__ == '__main__':
    main()
@CMCDragonkai
Copy link
Member Author

CMCDragonkai commented May 9, 2019

Additionally if I try to override the tensorflow-tensorboard derivation, I end up with really different results:

{
  pkgs ? import (fetchTarball https://github.com/NixOS/nixpkgs-channels/archive/cf3e277dd0bd710af0df667e9364f4bd80c72713.tar.gz) {},
  pythonPath ? "python36"
}:
  with pkgs;
  let
    python = lib.getAttrFromPath (lib.splitString "." pythonPath) pkgs;
  in
    {
      old = python.pkgs.tensorflow-tensorboard;
      new = python.pkgs.tensorflow-tensorboard.overridePythonAttrs (attrs: {
        propagatedBuildInputs = [
          python.pkgs.tensorflowWithoutCuda
        ] ++ attrs.propagatedBuildInputs;
      });
    }

Building the old produces:

/nix/store/4hpsjw1a9yvw2skx5kjjc0kp3lqy57ad-python3.6-tensorflow-tensorboard-1.13.0
├── bin
├── lib
└── nix-support

3 directories, 0 files

Building the new produces:

/nix/store/r8gg7wf3li6kh3nblvd2bvjnb3lgfhbr-python3.6-tensorflow-tensorboard-1.13.0
├── lib
└── nix-support

2 directories, 0 files

How can overriding the propagatedBuildInputs end up changing whether the bin directory exists or not?

Using nix-diff on both derivations only shows this:

> nix-diff /nix/store/5gyq5v9h6pqms0f79kjazqrnawlkif6k-python3.6-tensorflow-tensorboard-1.13.0.drv /nix/store/11nnqlpw3sns85rrsrillnib81484gqj-python3.6-tensorflow-tensorboard-1.13.0.drv

- /nix/store/5gyq5v9h6pqms0f79kjazqrnawlkif6k-python3.6-tensorflow-tensorboard-1.13.0.drv:{out}
+ /nix/store/11nnqlpw3sns85rrsrillnib81484gqj-python3.6-tensorflow-tensorboard-1.13.0.drv:{out}
• The set of input names do not match:
    + python3.6-tensorflow-1.13.1
• The environments do not match:
    propagatedBuildInputs=''
        /nix/store/ylx6i7ing1p53bnnciq6s0fyrhlh19c0-python3.6-tensorflow-1.13.1 /nix/store/1vjr8ywyvczgvf7wzzi149j1nzz6zh2l-python3.6-numpy-1.16.1 /nix/store/5rm6v6dfx6r62jzkl8sc7x70fs4pfhi9-python3.6-Werkzeug-0.14.1 /nix/store/2xj01vi4kbimkzaxf5s86y15wy0fzfh3-python3.6-protobuf-3.6.1 /nix/store/wsq8l0wq8vgmgjbdpd47i4zzl4rc8wcx-python3.6-Markdown-2.6.10 /nix/store/fzl93dl6576b7xhbdxqfz46q77xv1crr-python3.6-grpcio-1.18.0 /nix/store/arlf0ydxbkkd7cs4gzjkdi6yfgsip5g3-python3.6-absl-py-0.7.0 /nix/store/lwwk80ls3sb1dfljfsrrx6wlkjkd8imn-python3-3.6.8 /nix/store/kgiwzwf4q9107lv8cvpdqn79rmv5k54j-python3.6-setuptools-40.8.0

@CMCDragonkai
Copy link
Member Author

CMCDragonkai commented May 9, 2019

It is possible to get tensorboard CLI executable executed by a custom script as long as you use propagatedBuildInputs with python.pkgs.tensorflow. However if you are able to run it, the tensorboard script ends up reporting with:

Traceback (most recent call last):
  File "/nix/store/4hpsjw1a9yvw2skx5kjjc0kp3lqy57ad-python3.6-tensorflow-tensorboard-1.13.0/bin/.tensorboard-wrapped", line 7, in <module>
    from tensorboard.main import run_main
  File "/nix/store/4hpsjw1a9yvw2skx5kjjc0kp3lqy57ad-python3.6-tensorflow-tensorboard-1.13.0/lib/python3.6/site-packages/tensorboard/main.py", line 42, in <module>
    import tensorflow as tf  # pylint: disable=unused-import
ModuleNotFoundError: No module named 'tensorflow'

This looks like its a bug in the tensorflow-tensorboard expression itself. It is a dependency of tensorflow expression, but when used transitively, it is not able to find the tensorflow Python module.


Logically it makes sense that tensorboard should be dependent on tensorflow, so that way if I depend on tensorboard, then tensorboard CLI call should succeed. Right now however, it's made that tensorboard is sort of a "peer" dependency on tensorflow. In fact, by itself pythonPackages.tensorflow-tensorboard doesn't work at all. You have to instead get pythonPackages.tensorflow and that brings in pythonPackages.tensorflow-tensorboard.

But this ends up failing because when we try to use the tensorboard CLI transitively at runtime by relying on the pythonPackages.tensorboard package, it just fails to find the module.

I suggest that tensorboard-tensorflow should actually depend on the tensorflow package. And it should be an independent package from tensorflow. I'm not sure whether this would create a cyclic dependency issue. It can be a good idea to separate these anyway. So tensorflow does not automatically bring in tensorflow-tensorboard, and instead tensorflow-tensorboard is an "extension package" you have to explicitly bring in.

@stites
Copy link
Member

stites commented Oct 24, 2019

Edit: Tensorboard-2.0 requires wheel (with only protobuf>=3.6) and will run without tensorflow. This ticket is tagged with 19.03 -- will bumping tensorboard to 2.0 close this issue?

$  nix-shell -I nixpkgs=$(pwd) -p 'pkgs.python2.withPackages(ps: [ps.tensorflow-tensorboard])' 
$ tensorboard
TensorFlow installation not found - running with reduced feature set.
Error: A logdir or db must be specified. For example `tensorboard --logdir mylogdir` or `tensorboard --db sqlite:~/.tensorboard.db`. Run `tensorboard --helpfull` for details and examples. 

Identical output with python3. Please test in #71890


~I'm currently investigating bumping tensorboard (which is backed-up by bumping protobuf and failing protobuf test suites for anything over 3.x, see #71889) and am not sure this is an issue for more recent versions of tensorboard.

Of note, tensorboard has a compat module which is explicitly created to break circular dependencies. Tensorflow does seem to be required, however, for almost all plugins. which are bundled with tensorboard.~

I'm fairly invested in how this ticket will turn out. In truth, I'm trying to fix the test suite for pytorch-1.3.0 now that #65041 is merged. Pytorch-1.3.0 has added a tensorboard test suite which I believe requires tensorboard-2.0. Pulling in a transitive tensorflow dependency would be unfortunate overhead.

@stale
Copy link

stale bot commented Jun 1, 2020

Thank you for your contributions.
This has been automatically marked as stale because it has had no activity for 180 days.
If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.
Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the
    related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse. 3. Ask on the #nixos channel on
    irc.freenode.net.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 1, 2020
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Feb 25, 2022
@uri-canva
Copy link
Contributor

I believe this was fixed in tensorboard 2 added in #83518 and tensorboard 1 was removed in #108696, please comment or reopen if this is not the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants