TensorFlowSharp icon indicating copy to clipboard operation
TensorFlowSharp copied to clipboard

Is there a TensorFlow windows gpu package?

Open roeynissim opened this issue 9 years ago • 31 comments

and if so does TensorFlowSharp support that?

roeynissim avatar Aug 02 '17 09:08 roeynissim

TensorFlow have native pip package with gpu support for windows, but +1 about TensorFlowSharp support of gpu.

GreyMS avatar Aug 02 '17 13:08 GreyMS

@Aminullah6264 I see that you created separate issue for your question. I tried to help you with your problem. And this issue is about possibility of using TensorFlowSharp with GPU support, so lets move conversation about image loading from here.

GreyMS avatar Aug 03 '17 11:08 GreyMS

You could download one of the TensorFlow binaries that contains the GPU support and replace the library that comes in the NuGet package. I tried finding prebulit binaries on the CI system, but could not find them:

http://ci.tensorflow.org/

If someone can find them, I can look into integrating them.

There is a potential challenge - how to swap out the library that is used at runtime, and I do not have a good answer for that (yet)

migueldeicaza avatar Aug 05 '17 14:08 migueldeicaza

@migueldeicaza thanks for your answer. I have found this link: http://ci.tensorflow.org/job/tf-master-win-gpu-cmake/ but it contains wheel package, and I don't know if it has exports for c++ api.

I wanted to build the dll by myself, but I don't know what version to build - 1.2.0, 1.2.1, 1.2.2, 1.3.0-rc1, 1.3.0-rc2 or current master?

I built top revesion of branch 1.2 and replaced resulted dll in the nuget package, but I failed to load graph, because it tells "wrong graph def" or smth like that. I saw the issue about similar error for models > 70 mb, but my model successfully loaded with the binaries, which you provided with nuget package, so I suppose the problem is in the dll.

What do you mean "swap out the library that is used in runtime"? I thought that api for gpu and cpu must be identical and TF internally decides where to do computations. Is there some point that I missed?

GreyMS avatar Aug 06 '17 02:08 GreyMS

I do not know if the GPU-aware library will run on non-GPU powered systems or if it will startup if you are missing system dependencies. In that scenario, we would need two libraries (say "cpu/libtensorflow.dll" and "gpu/libtensorflow.dll", and the issue then becomes, how do we make TensorFlowSharp.dll load the right one.

migueldeicaza avatar Aug 07 '17 23:08 migueldeicaza

@migueldeicaza Oh, now I understand. Yes, it is a problem, but as we can see, original Tensorflow has two separate packages for Python - with and without gpu support, so I think it will be normal to have two packages for tensorflowsharp. But the question is - which version of tensorflow must be compiled to be used with TensorFlowSharp? One the one hand - the latest version must be more stable and contain less bugs, but on the other hand TensorFlow changes its api slightly from version to version, so v1.3.0 can fail to work with your lib. I can try to build gpu package and create pull request, so TensorFlowSharp would have two nuget packages, but I need your help. Thanks in advance.

GreyMS avatar Aug 08 '17 09:08 GreyMS

@GreyMS I am also interested in GPU version for Windows, and would like to help. Have you tried to build debug information and see what stops your dll from loading graph?

Also, if you don't mind, can you publish steps you followed to build it?

lostmsu avatar Aug 11 '17 03:08 lostmsu

@lostmsu Unfortunately I built release version of the dll, so I doesn't have any debug information. I'll try to rebuild it and check again.

My steps are:

  1. Clone tensorflow repository.
  2. Check out the revision which is appropriate to my point of view (I tried several, for example head of the branch called 1.2, head of the master, revision with tag 1.2.1 and several more).
  3. Open "tensorflow\contrib\cmake" and create solution for Visual Studio 2015 Community with CMake 3.9.0. 3.1. Check "tensorflow_BUILD_SHARED_LIB" as enabled. 3.2. Check "tensorflow_ENABLE_GPU" as enabled. (I have CUDA 8.0 installed) 3.3. Specify the path to swig.exe 3.4. Add "CUDNN_HOME" to the folder, which contains cudnn 6.0.
  4. Run "vcvars64.bat" from VC 14.0 folder (which corresponds to vs2015). It is important, because TF is so big, that 32 bit tools usually fails with "compiler is out of heap" error. This was the reason my builds failed than I tried to compile from studio. I found some questions at stackoverflow, which tells how to run VS with 64 tools, but I didn't give a try to that method.
  5. Run "MSBuild tensorflow.sln /p:Configuration:Release /p:Platform=x64 /m" to build the solution.
  6. Wait several hours...

Several times I had some problems with compilation, but today I realized that problem is probably in Python 3.6. I will try to rebuild with Python 3.5.2 soon and check if it will work. You can also disable "tensorflow_BUILD_PYTHON_BINDINGS" at all (as I did, than I succeeded to build dll), but it is difficult to understand, if it is problem in the dll, or somewhere in importing process. I hope that python binding lib will help to find out the truth.

I will write again as soon as I examine my guess.

GreyMS avatar Aug 11 '17 14:08 GreyMS

@GreyMS just for the record: with cmake-gui I still had to specify /p:PreferredToolArchitecture=x64 to force MSBuild to use 64-bit compiler (32-bit one fails with heap overflow as you mentioned).

lostmsu avatar Aug 13 '17 01:08 lostmsu

We built a libTensorFlow.dll GPU version AND a CPU version. At startup we copy the GPU version to the expected location and try it. If it doesn't work, we copy the CPU version to the expected location. This is ugly, but happens only once at system startup and seems to give us the desired result of running the GPU version whenever it can be used.

        var targetPath = Path.Combine(binPath, "libtensorflow.dll");
        try
        {
            File.Copy(Path.Combine(netPath, "libtensorflow_gpu.dll"), targetPath, true);
            cachedGraph = new TFGraph();
            UsesGpu = true;
        }
        catch
        {
            File.Copy(Path.Combine(netPath, "libtensorflow_cpu.dll"), targetPath, true);
            cachedGraph = new TFGraph();
            UsesGpu = false;
        }

JohnTenney avatar Aug 13 '17 18:08 JohnTenney

@JohnTenney does your GPU version work on Windows? If so, can you share it with us? I am trying to build one myself, but it fails for various resource-related reasons.

lostmsu avatar Aug 13 '17 18:08 lostmsu

@JohnTenney Absulatelly agree with @lostmsu . I try to build from sources for two weeks, but without any effect. First the problem was in python version, now it seems that nvlabs/cub is broken, since they changed the zip archive and hash changed and so on. Getting dll which works for sure would be really helpfull a lot. Or, it is impossible, at least may be you can tell us any advices or insights about building such lib? What version (or even better - what revision of tensorflow) is built, what version of python, cudnn, windows, visual studio? Any information will be appriciated. Thanks in advance!

GreyMS avatar Aug 13 '17 18:08 GreyMS

@GreyMS for cub you can definitely update the hash. But I also opened an issue in their bug tracker to get url with unchanging binary.

If I get hands on working DLLs I can spend some time nicely packaging them with NuGet.

lostmsu avatar Aug 13 '17 18:08 lostmsu

@GreyMS u can change the hash in tensorflow\tensorflow\tensorflow\contrib\cmake\build\cub\src\cub-stamp\verify-cub.cmake

TheBlubb14 avatar Aug 13 '17 19:08 TheBlubb14

@TheBlubb14 of course I can and I did it. But cub was ruined somehow, they removed block/block_scan.cub file, so my build failed because of it. Today they updated the release again and returned that file, but there is no guaranty they didn't break something else. Also it seems that python package "wheel" is needed, which isn't mentioned anywhere.

GreyMS avatar Aug 13 '17 20:08 GreyMS

@GreyMS do you really need any python packages if you're not building Python binding? I though this whole thread's point is to get rid of that in favor of our beloved .NET languages ;-)

lostmsu avatar Aug 13 '17 21:08 lostmsu

@GreyMS speaking of building. I believe RelWithDebInfo would not build because MS tooling has limit on .PDB size. I experienced .PDB write errors with no reasonable explanation, and found, that it might have this limitation.

lostmsu avatar Aug 13 '17 21:08 lostmsu

@GreyMS all right, I think I've succeeded: https://1drv.ms/u/s!AioR7aFFo_FKypNf4567x8V6WVlabA This is built from 1.2.1 release. When I replace libtensorflow.dll from TensorFlowSharp package with this one, it at very least lets me create an empty graph.

lostmsu avatar Aug 14 '17 02:08 lostmsu

@lostmsu Thank you very much for providing dll! I don't really need python package, but I tried to build it too for testing. For example, I tried your lib, it loads but writes "invalid graphdef" than I try to load my model. I use InceptionV3 and it is rather large, so the problem may be similar to the issues about "can't load model > 70 MB", but python package could help to determine if it is the problem of the lib, or the problem of the C# wrapper. Have you tested your lib with any model?

GreyMS avatar Aug 14 '17 11:08 GreyMS

@GreyMS We (same group as @JohnTenney) can share the tensorflow gpu dll we built but I just tried it with InceptionV3 and got the same "invalid graphdef" exception - but I can add that we've been using this dll without problems on smaller conv architectures (~5-10 MB) that share the same basic components as Inception.

It might be that Inception is too large or maybe that the build isn't fully functional for some of the rarer ops, but I also wasn't able to include the python package. At the very least, I'd suspect that the dll @lostmsu posted is like ours and should work fine on smaller graphs.

If you're interested here's the version of 1.2.1 we built: https://drive.google.com/file/d/0B4jjmYPLkag1YmpRVlN2Zm5TUW8/view?usp=sharing

hansohl27 avatar Aug 14 '17 18:08 hansohl27