Skip to content

Using GPU with Tensorflow.NET

Rinne edited this page May 17, 2023 · 9 revisions

This article will explains in steps how to install Tensorflow-GPU and setup with Tensorflow.NET in a C# project.

Installing Tensorflow.NET-GPU on Windows

  1. Make sure your projects are targeting x64 as tensorflow does not support x32 architecture.

  2. Uninstall any existing tensorflow redist packages and install the following:

    PM> Install-Package SciSharp.TensorFlow.Redist-Windows-GPU
    PM> Install-Package TensorFlow.NET

    Rebuild your project, ensure that tensorflow.dll is copied to your output folder (e.g. bin/debug/tensorflow.dll or bin/debug/runtimes/win-x64/native/tensorflow.dll) and check its size.
    The .dll should be above 300MB. If it's below 150MB - it is likely to be a CPU version; delete it and rebuild the project.

  3. You need to meet tensorflow-gpu's requirements.

  • Check if your GPU card has with CUDA® Compute Capability 3.5 or higher. See the list of CUDA-enabled GPU cards.
  • Please check the required CUDA and CuDNN version from tensorflow build info. For example, if you'd like to install Redist-GPU v2.10 which is corresponding to tensorflow2.10, it could be found that CUDA 11 + CuDNN 8 is required.
  • Make sure your NVIDIA GPU driver version supports the version of the CUDA you want to install.
  • Install the CUDA, let's take CUDA11.7 as an example. Download the installer from CUDA 11.7 and have the following environment variables set (the directory may vary, verify it exists) after the installation.
    ::Set new environment variables:
    CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7
    CUDA_PATH_V11_7=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7
    ::Append To PATH:
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\extras\CUPTI\libx64
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\include
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin
  • Install cuDNN SDK. Extract the zip and copy the content of the following folders to those of CUDA installation path:
    .\bin\* -> C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin
    .\include\* -> C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\include
    .\lib\* -> C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\lib

If you've done all the above correctly, the library should automatically switch to GPU and console should print a line similar to this:
tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2114 MB memory) -> 
physical GPU (device: 0, name: GeForce GTX 1060 3GB, pci bus id: 0000:04:00.0, compute capability: 6.1)