0

I'm trying to get a tensorflow C++ build (or tensorflow lite) for Windows that runs on GPU (WITHOUT using CUDA, it should work on AMD). I decided to opt in for tensorflow lite with the -DTFLITE_ENABLE_GPU=ON flag to enable OpenCL.

The steps I followed:

  1. Clone tensorflow github repo to tensorflow_src and checout to r2.14
  2. Create a folder called tflite-opencl next to the cloned repo
  3. Go to tflite-opencl and run cmake C:/Users/Asus/Desktop/tensorflow/tensorflow/lite -DTFLITE_ENABLE_GPU=ON
  4. Run cmake --build . -j

When I try to run cmake --build . -j I get the following error:

C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\selectors\operation_selector.cc(313,20): error
 C2039: 'any_cast': is not a member of 'std' [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]

...
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\selectors\operation_selector.cc(313,1): error
C2065: 'any_cast': undeclared identifier [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\selectors\operation_selector.cc(313,29): error
 C2275: 'tflite::gpu::ElementwiseAttributesBase<tflite::gpu::DataType::BOOL,T>': expected an expression instead of a
 type [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]
          with
          [
              T=bool
          ]
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\selectors\operation_selector.cc(317,1): error
C3536: 'attr': cannot be used before it is initialized [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\selectors\operation_selector.cc(317,17): error
 C2672: 'CreateElementwiseWithBroadcast': no matching overloaded function found [C:\Users\Asus\Desktop\tflite-opencl
\tensorflow-lite.vcxproj]

...


C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\selectors\operation_selector.cc(321,17): error
 C2672: 'CreateElementwise': no matching overloaded function found [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-l
ite.vcxproj]

...

C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\selectors\operation_selector.cc(313,29): error
 C2275: 'tflite::gpu::ElementwiseAttributesBase<tflite::gpu::DataType::INT32,T>': expected an expression instead of
a type [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]
          with
          [
              T=int32_t
          ]
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\selectors\operation_selector.cc(313,29): error
 C2275: 'tflite::gpu::ElementwiseAttributesBase<tflite::gpu::DataType::FLOAT32,float>': expected an expression inste
ad of a type [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]

...

C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\tasks\special\conv_pointwise.cc(129,12): error
 C2039: 'any_cast': is not a member of 'std' [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\include\variant(30,1): message : s
ee declaration of 'std' [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\tasks\special\conv_pointwise.cc(129,20): error
 C2065: 'any_cast': undeclared identifier [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\tasks\special\conv_pointwise.cc(129,21): error
 C2275: 'tflite::gpu::ReduceAttributes': expected an expression instead of a type [C:\Users\Asus\Desktop\tflite-open
cl\tensorflow-lite.vcxproj]
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\delegates\gpu\common\tasks\special\conv_pointwise.cc(130): error C3
536: 'reduce_attr': cannot be used before it is initialized [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcx
proj]

But despite these errors it kept on compiling and eventually ended on:

...
 tensorflow_profiler_logger_shim.cc
  tflite_with_xnnpack_optional.cc
  minimal_logging_default.cc
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\minimal_logging_default.cc(37,9): warning C4068: unknown pragma 'cl
ang' [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\minimal_logging_default.cc(38,9): warning C4068: unknown pragma 'cl
ang' [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]
C:\Users\Asus\Desktop\tensorflow\tensorflow\lite\minimal_logging_default.cc(40,9): warning C4068: unknown pragma 'cl
ang' [C:\Users\Asus\Desktop\tflite-opencl\tensorflow-lite.vcxproj]
  platform_profiler.cc
  root_profiler.cc
  profiler.cc
  sparsity_format_converter.cc
  schema_utils.cc
  Generating Code...

C:\Users\Asus\Desktop\tflite-opencl>

I think I'm getting the any_cast is not a member of std because my C++ standard version is below 14. But I've been coding in windows for a while and I'm pretty sure I'm above 17 as I use many modern features. I've updated g++ from Visual Studio Installer but I'm not sure how to properly update my C++ version on Windows.

I've tried adding SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")

to cmake_install.cmake under tflite-opencl but it still generates the same error.

I'm sure this build has failed, but regardless I searched for the dll.

This failed build gave me a Visual Studio solution inside tflite-opencl. I need to import tflite into a huge project so I need either the dll files or the lib files. I tried looking under tflite-opencl/Release, tflite-opencl/Debug and tflite-opencl/x64 but found nothing. I'm also adviced to look under \bazel-bin\tensorflow\lite\kernels on my previous question, which I can find under tensorflow_src and I can't find any dll's in it.

How can I fix that error and change my C++ standard on Windows? How can I get these dll or lib files to use under my project? If I should build INSTALL how am I supposed to do it? I developed mainly on Linux environments so I don't know how to use Visual Studio propperly.

Update: Apparently operation_selector.cc does not include #include <any> as it should, causing the aforementioned 'any_cast': is not a member of 'std' error. After including and building it, I get a .lib file under tflite-opencl/Debug (no .dll's found).

However, when I include it and try to use it on a project, I get many unresolved external symbol errors. Am I doing something wrong? Were there supposed to be a .dll file?

Turgut
  • 711
  • 3
  • 25
  • Instead of appending the CMAKE_CXX_FLAGS with /std:c++17, could you try setting the variable CMAKE_CXX_STANDARD using `set(CMAKE_CXX_STANDARD 17)`. On a side note, to be sure your installation supports modern C++, just create a very small example with a C++17 example and try to compile it – Martin Aug 28 '23 at 10:40
  • @Martin I did try to create a project from scratch and use any_cast, and it works just fine as long as I set it above c++ 14+ in properties. I made sure the project uses c++ 20 but still the same error, I don't think it's about c++ standard anymore... – Turgut Aug 28 '23 at 10:43

1 Answers1

0

Apperently tensorflow do not include #include <any> under the files where it uses any_cast, causing the aforementioned problem. Adding the includes fixes the problems, however after getting a build with this approach I'm now getting unresolved external symbol errors, which is more detailed on this question: Tensorflow lite C++ undefined symbol on windows with GPU support

Turgut
  • 711
  • 3
  • 25