machinelearning icon indicating copy to clipboard operation
machinelearning copied to clipboard

ML.Net is not able to load Tensorflow GPU DLL...

Open zeahmed opened this issue 8 years ago • 24 comments

I am trying work with TF GPU native dll so that I can speed up the scoring process. I replaced the tensorflow.dll in microsoft.ml.tensorflow.redist\0.5.0\runtimes\win-x64\native with gpu version but ML.Net throws DLL not found exception.

I did the same with TensorflowSharp and it loads the GPU version perfectly fine.

SOLVED

Updating To TensorFlow GPU DLL

If you want to use TF GPU dll during scoring follow the steps below

  • In your ML.Net nuget, replace the tensorflow.dll in microsoft.ml.tensorflow.redist\<ML.Net-Version>\runtimes\win-x64\native with gpu version (please keep the name same).
  • You can grap the prebuilt gpu dll from https://github.com/fo40225/tensorflow-windows-wheel.
  • Update the graphics card drivers, CUDA and CuDNN according the Tensorflow gpu version you are using.
  • TensorFlowScorer will automatically pick up the TF GPU version.

zeahmed avatar Sep 10 '18 02:09 zeahmed

Can you please paste the full error message here?

markusweimer avatar Sep 10 '18 08:09 markusweimer

@zeahmed let me know more about what you tried?

Did you get the GPU build of TF and just try a 1:1 replacement? (same name and all) If so, can you share where you got this so I can try it out? That error sounds like an issue with either the DLL name or dependencies, rather than any issue with the bindings in ML.NET.

TF# has some API around listing devices then setting devices. I left out the API that lets you SetDevice at the moment because we weren't using it, but can bring it back.

ericstj avatar Sep 10 '18 17:09 ericstj

Here is the gpu dll I am using: https://github.com/fo40225/tensorflow-windows-wheel/blob/master/1.9.0/cpp/libtensorflow-gpu-windows-x86_64-1.9.0-avx2cuda92cudnn71.7z

Yes, I did the 1:1 replacement both in TF# and ML.Net (same name and same location).

Now I got it working. Actually, the problem was graphics card driver. I updated it through windows driver update and it worked for sometime with TF# but after restarting windows it showed me the same problem as ML.Net.

I updated my driver to latest from Nvidia site and also updated the CUDA to 9.2 and CuDNN to 7.1 as a requirement to the above dll. It's being loaded correctly now in ML.Net though I am hitting some system specific issues on my end.

@markusweimer and @ericstj, I have added notes in the original issue to elaborate how to work with Tensorflow gpu dll.

zeahmed avatar Sep 10 '18 20:09 zeahmed

Taking a look at your steps: I wouldn't recommend that folks update the nuget package. The Nuget package cache is shared between all projects. Lots of parts of the build treat it as immutable.

Instead, to use the GPU binary, you can exclude the native assets from the Microsoft.ML.Tensorflow package reference

<PackageReference Include="Microsoft.ML.TensorFlow" ExcludeAssets="native" />

This will tell NuGet not to include the native tensorflow binaries (it applies transitively to the dependencies, and will thus apply to TensorFlow.Redist package).

Then they can "bring their own" copy of tensorflow however they choose. Simply referencing it as content is one way. If we want to officially support this scenario we could consider a packaging story for the GPU build (as well as an official way for someone to choose to use this).

ericstj avatar Sep 10 '18 21:09 ericstj

@ericstj, totally agree! This was just a work around for my case.

For now, ExcludeAssets="native" option sounds good me. Otherwise, there are lots dependencies for GPU dll based on how it was built e.g. CUDA version, CuDNN version, driver version etc.

zeahmed avatar Sep 11 '18 01:09 zeahmed

Tensorflow windows GPU dll is now officially available at https://www.tensorflow.org/install/lang_c

zeahmed avatar Jan 31 '19 19:01 zeahmed

@zeahmed, can this issue be closed?

yaeldekel avatar Feb 21 '19 18:02 yaeldekel

Yes, it can be closed as there is no work item for this right now.

zeahmed avatar Feb 21 '19 20:02 zeahmed

Hi - could you please clarify what now needs doing to enable GPU tensorflow with ML now that

Tensorflow windows GPU dll is now officially available at https://www.tensorflow.org/install/lang_c

I have replaced the tensorflow.dll with the one in the download direct from tensorflow.org, but am getting:

Unhandled exception. System.FormatException: Tensorflow exception triggered while loading model. ---> System.DllNotFoundException: Unable to load DLL 'tensorflow' or one of its dependencies: The specified module could not be found. (0x8007007E) at Tensorflow.c_api.TF_NewGraph() at Tensorflow.Graph..ctor() at Microsoft.ML.Transforms.Dnn.DnnUtils.LoadTFSessionByModelFilePath(IExceptionContext ectx, String modelFile, Boolean metaGraph) --- End of inner exception stack trace --- at Microsoft.ML.Transforms.Dnn.DnnUtils.LoadTFSessionByModelFilePath(IExceptionContext ectx, String modelFile, Boolean metaGraph) at Microsoft.ML.Transforms.Dnn.DnnUtils.GetSession(IHostEnvironment env, String modelPath, Boolean metaGraph) at Microsoft.ML.Transforms.TensorFlow.TensorFlowUtils.LoadTensorFlowModel(IHostEnvironment env, String modelPath) at Microsoft.ML.TensorflowCatalog.LoadTensorFlowModel(ModelOperationsCatalog catalog, String modelLocation) at Classify.Program.GenerateModel(MLContext mlContext) in C:\Users\a\source\repos\Classify\Classify\Program.cs:line 144 at Classify.Program.RunPred() in C:\Users\a\source\repos\Classify\Classify\Program.cs:line 90 at Classify.Program.Main(String[] args) in C:\Users\a\source\repos\Classify\Classify\Program.cs:line 33

rossbuggins avatar Oct 28 '19 14:10 rossbuggins

@rossbuggins - Can you follow this procedure?

https://github.com/dotnet/machinelearning/blob/master/docs/api-reference/tensorflow-usage.md#gpu-support

CESARDELATORRE avatar Oct 28 '19 16:10 CESARDELATORRE

UPDATE: I was using CUDA 10.1 not 10.0, so it wasn't working. Moving back to 10.0 and the tensor flow GPU dll loads ok.

However, I'm not seeing any GPU usage. I'm testing demo code based on https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/image-classification

Thanks

rossbuggins avatar Oct 28 '19 16:10 rossbuggins

@CESARDELATORRE - just seen you've sent that link through, I'll add the nuget to SciSharp.TensorFlow.Redist-Windows-GPU and recompile and let you know :-)

rossbuggins avatar Oct 28 '19 16:10 rossbuggins

you will need nvidia’s gpu monitor to see the usage ... some versions of windows will show in task manager if you have right driver version

codemzs avatar Oct 28 '19 16:10 codemzs

Sorry, stupid question, out of all the nvidia tools that get installed, whats best one to easily see the GPU useage please? Thanks

rossbuggins avatar Oct 28 '19 17:10 rossbuggins

In Windows, while training, go to TaskManager-->Performance-->GPU, then, if using the GPU you'll see activity in there. :) Please, confirm when it is working for you, ok? 👍

CESARDELATORRE avatar Oct 28 '19 17:10 CESARDELATORRE

I'm not seeing any GPU activity, only CPU maxed out. At the start of the console app I see:

2019-10-28 18:15:37.816641: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2019-10-28 18:15:37.831001: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library nvcuda.dll 2019-10-28 18:15:37.886686: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties: name: GeForce GTX 1050 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.62 pciBusID: 0000:01:00.0 2019-10-28 18:15:37.895463: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check. 2019-10-28 18:15:37.901922: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0 2019-10-28 18:15:38.565714: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix: 2019-10-28 18:15:38.571459: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187] 0 2019-10-28 18:15:38.576291: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0: N 2019-10-28 18:15:38.581043: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3000 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)

rossbuggins avatar Oct 28 '19 18:10 rossbuggins

What should the result of adding SciSharp.TensorFlow.Redist-Windows-GPU be? Can I check a version of something in the output folder to confirm its definitely utilising this?

rossbuggins avatar Oct 28 '19 18:10 rossbuggins

What API or sample code are you using for training?

CESARDELATORRE avatar Oct 28 '19 19:10 CESARDELATORRE

https://docs.microsoft.com/en-us/dotnet/machine-learning/tutorials/image-classification

rossbuggins avatar Oct 28 '19 21:10 rossbuggins

That sample is a "model composition" which uses TensorFlow only to featurize the images but then trains with an ML.NET trainer (mlContext.MulticlassClassification.Trainers.LbfgsMaximumEntropy) which is a regular ML.NET MulticlassClassification Trainer which doesn't use GPU.

For training with GPU you need to use this sample which trains with TensorFlow, internally within the DNN and uses the new PREVIEW with the ImageClassification high level API:

https://github.com/dotnet/machinelearning-samples/tree/master/samples/csharp/getting-started/DeepLearning_ImageClassification_Training

However, with the current PREVIEW version there's an issue if you want to use GPU and needs a configuration workaround.

I'd recommend you to wait a few days when we'll release 1.4 GA where you won't need this workaround and the sample above will be able to directly run it as CPU or GPU.

If you still want to try GPU wit the current Preview-2, you need to follow this workaround:

Workaround for PREVIEW-2 to use GPU

Tested work-around, and confirmed we can run with GPU TF for now steps for work-around:

1. Add the TensorFlow.NET GPU redist dependency to your project just as would be required originally.
2. Locate your Nuget package cache it should be something like C:\Users\<your_user_name>\.nuget\packages
3. Copy <package_cache>\scisharp.tensorflow.redist\1.14.0\runtimes\win-x64\native\tensorflow.dll to your desktop or somewhere for safe keeping.
4. Overwrite the previously mentioned DLL by copying the GPU version from <package_cache>\scisharp.tensorflow.redist-windows-gpu\1.14.0\runtimes\win-x64\native\tensorflow.dll to <package_cache>\scisharp.tensorflow.redist\1.14.0\runtimes\win-x64\native\tensorflow.dll
5. You can now remove the dependency to the TensorFlow.NET GPU package as it is not really used by your project when using this Preview.

Optional: If you want to reset back to the CPU version move the DLL you had saved to the desktop back to the original location as specified in step 3

In 1.4 GA you will simply need to reference the TensorFlow.NET GPU or CPU redist package from your project.

CESARDELATORRE avatar Oct 28 '19 22:10 CESARDELATORRE