How to install Tensorflow GPU with CUDA 10.0 for python on Windows

Step 10: Install Dependencies

pip3 install -U pip six numpy wheel mock
pip3 install -U keras_applications==1.0.5 --no-deps
pip3 install -U keras_preprocessing==1.0.3 --no-deps

Step 11: Configure Tensorflow from source:

Preparing shell for build Exit all running programms, shells, then go to run [Win+R] and enter follwings:
"C:\msys64\msys2_shell.cmd" -use-full-path
Add bazel to path
export PATH=/c/bazel:$PATH
Start the process of building TensorFlow by downloading latest tensorflow 1.12 .
cd /c/

mkdir tensorflow

cd tensorflow/
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow/
git checkout r1.12
python configure.py
Give python path in
Please specify the location of python. [Default is C:\Users\ACER\AppData\Local\Programs\Python\Python36\python.exe]:
Hit enter for default path or use your own python path but install step 10 to it. Press enter two times
Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: Y

Do you wish to build TensorFlow with XLA JIT support? [y/N]: N

Do you wish to build TensorFlow with ROCm support? [y/N]: N

Do you wish to build TensorFlow with CUDA support? [y/N]: Y

Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 10.0
Please specify the location where CUDA 10.0 toolkit is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0]: {press enter}
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7.3.1
Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0]: {press enter}
Now we need compute capability which we have noted at step 1 eg. 5.0
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 3.5, 7.0]: 5.0
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]: {press enter}
Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]: Y
Configuration finished

Step 12: Build Tensorflow using bazel

The next step in the process to install tensorflow GPU version will be to build tensorflow using bazel. This process takes a fairly long time. To build a pip package for TensorFlow you would typically invoke the following command:
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
Note:-
 add "--config=mkl" if you want Intel MKL support for newer intel cpu for faster training on cpu
 add "--config=monolithic" if you want static monolithic build (try this if build failed)
 add "--local_resources 2048,.5,1.0" if your PC has low ram causing Segmentation fault or other related errors
This process will take a lot of time. It may take 3- 4 hours or maybe even more. Also if you see no cpu usages or build failed then restart pc and try again once only. The bazel build command builds a script named build_pip_package. Running this script as follows will build a .whl file within the tensorflow_pkg directory: To build whl file issue following command:
bazel-bin/tensorflow/tools/pip_package/build_pip_package tensorflow_pkg
To install tensorflow with pip:
cd tensorflow_pkg
pip3 install tensorflow*.whl
or use pip instead of pip3 Note : if you got error like unsupported platform then make sure you are running correct pip command associated with the python you used while configuring tensorflow build. You can check pip version and associated python by following command
pip -V

Step 13: Verify Tensorflow installation

Run in terminal
python

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
If the system outputs the following, then you are ready to begin writing TensorFlow programs: Success! You have now successfully installed tensorflow 1.12 on your machine. If you are on Ubuntu OS, you might want to check out our other post here, How to install Tensorflow GPU with CUDA 10.0 for python on Ubuntu. Cheers!! For prebuilt wheels go to this link .

16 Comments on How to install Tensorflow GPU with CUDA 10.0 for python on Windows

  1. I am following the guide but I am getting this error. See if anyone can help.
    p_packageuild –config=opt –config=cuda //tensorflow/tools/pip_package:build_pip
    Starting local Bazel server and connecting to it…
    WARNING: The following configs were expanded more than once: [cuda]. For repeatable flags, repeats are counted twice and may lead to unexpected behavior.
    WARNING: Option ‘experimental_shortened_obj_file_path’ is deprecated
    Loading:
    Loading: 0 packages loaded
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (1 packages loaded)
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (41 packages loaded)
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (95 packages loaded)
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (159 packages loaded)
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (223 packages loaded)
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (241 packages loaded)
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (268 packages loaded)
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (272 packages loaded)
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (293 packages loaded)
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (293 packages loaded)
    Analyzing: target //tensorflow/tools/pip_package:build_pip_package (294 packages loaded)
    ERROR: Analysis of target ‘//tensorflow/tools/pip_package:build_pip_package’ failed; build aborted: no such package ‘@png_archive//’: Traceback (most recent call last):
    File “C:/tensorflow/third_party/repo.bzl”, line 106
    _apply_patch(ctx, ctx.attr.patch_file)
    File “C:/tensorflow/third_party/repo.bzl”, line 73, in _apply_patch
    _execute_and_check_ret_code(ctx, cmd)
    File “C:/tensorflow/third_party/repo.bzl”, line 52, in _execute_and_check_ret_code
    fail(“Non-zero return code({1}) when …))
    Non-zero return code(256) when executing ‘C:\msys64\usr\bin\bash.exe -l -c “patch” “-p1” “-d” “C:/users/kailok/_bazel_kailok/xv6zejqw/external/png_archive” “-i” “C:/tensorflow/third_party/png_fix_rpi.patch”‘:
    Stdout:
    Stderr: Timed out
    INFO: Elapsed time: 29.694s
    INFO: 0 processes.
    FAILED: Build did NOT complete successfully (296 packages loaded)
    FAILED: Build did NOT complete successfully (296 packages loaded)

    • vim third_party/repo.bzl
      Press “a” on your keyboard. It will go in Insert mode look around def _wrap_bash_cmd(ctx, cmd):
      In that function you have to comment a line by adding # at first (may in line 29). See below
      Before:
      def _wrap_bash_cmd(ctx, cmd):
      if _is_windows(ctx):
      bazel_sh = _get_env_var(ctx, “BAZEL_SH”)
      if not bazel_sh:
      fail(“BAZEL_SH environment variable is not set”)
      cmd = [bazel_sh, “-l”, “-c”, ” “.join(cmd)]
      return cmd
      After:

      def _wrap_bash_cmd(ctx, cmd):
      if _is_windows(ctx):
      bazel_sh = _get_env_var(ctx, “BAZEL_SH”)
      if not bazel_sh:
      fail(“BAZEL_SH environment variable is not set”)
      # cmd = [bazel_sh, “-l”, “-c”, ” “.join(cmd)]
      return cmd
      Press ESE after edit then type
      :wq
      And press ENTER

  2. I tried that.
    It gave me error:
    Solution copy NUL WORKSPACE
    I tried again.
    It gave me error: no member sort in __init__ of some package.
    I found solution.
    I tried again: now I get
    WARNING: The following configs were expanded more than once: [cuda]. For repeatable flags, repeats are counted twice and may lead to unexpected behavior.
    WARNING: Option ‘experimental_shortened_obj_file_path’ is deprecated
    Loading:
    Loading: 0 packages loaded
    ERROR: Skipping ‘//tensorflow/tools/pip_package:build_pip_package’: error loading package ‘tensorflow/tools/pip_package’: Extension file not found. Unable to load package for ‘@local_config_rocm//rocm:build_defs.bzl’: The repository could not be resolved
    WARNING: Target pattern parsing failed.
    ERROR: error loading package ‘tensorflow/tools/pip_package’: Extension file not found. Unable to load package for ‘@local_config_rocm//rocm:build_defs.bzl’: The repository could not be resolved
    INFO: Elapsed time: 2.914s
    INFO: 0 processes.
    FAILED: Build did NOT complete successfully (0 packages loaded)
    FAILED: Build did NOT complete successfully (0 packages loaded)
    To what stage should I return?
    Thanks

  3. If you have an error with eigen (something like execroot\org_tensorflow\external\eigen_archive\eigen\src/Core/arch/CUDA/Half.h(212)), which may occurs because you have Compute capability above 5.3, do the following steps before step 12:

    1. download https://github.com/amsokol/tensorflow-windows-build-tutorial
    2. copy eigen_half.patch to tensorflow/third_party
    3. add Add patch_file = clean_dep(β€œ//third_party:eigen_half.patch”), line to eigen_archive section to tensorflow/tensorflow/workspace.bzl

    it should looks like:
    tf_http_archive(
    name = “eigen_archive”,
    build_file = clean_dep(“//third_party:eigen.BUILD”),
    patch_file = clean_dep(“//third_party:eigen_half.patch”),
    sha256 = “d956415d784fa4e42b6a2a45c32556d6aec9d0a3d8ef48baee2522ab762556a9”,
    strip_prefix = “eigen-eigen-fd6845384b86”,
    urls = [
    “https://mirror.bazel.build/bitbucket.org/eigen/eigen/get/fd6845384b86.tar.gz”,
    “https://bitbucket.org/eigen/eigen/get/fd6845384b86.tar.gz”,
    ],
    )

    This bug is reported at https://github.com/tensorflow/tensorflow/issues/22715

  4. The result of this building is original tensorflow but the one I needed is tensorflow with gpu support, is there any fix to the instructions?

      • After the installation, I open up CMD and type in “pip list” but there isn’t any sign of tensorflow-gpu but the regular tensorflow is there waiting for me to use it, is there any instructions that differentiate the results as tensorflow or tensorflow-gpu?

        • Run the following to check tf gpu:

          python -c "import tensorflow as tf; print(tf.contrib.eager.num_gpus())"

          • It shows the gpu is there but when I use tensorflow for python it does not show any usage on the gpu…

  5. Windows 10 Home Edition user doesn’t have gpedit, do we need to spend $200 on it to use tensorflow? πŸ˜€

2 Trackbacks & Pingbacks

  1. How to install Tensorflow GPU on Windows | Python 3.6
  2. How to install Tensorflow 1.7.0 using official pip package | Python 3.6

Leave a Reply

Your email address will not be published.




This site uses Akismet to reduce spam. Learn how your comment data is processed.