Repository: pytorch
Issue: Build errors with USE_VULKAN=ON when cross-compiling for Android
Labels: module: build,triaged,module: android

## 🐛 Bug

I met several errors while generating and building pytorch from source for Android on Windows with Vulkan backend.

First error occurred while cmake generating (I've added message with unwrapped variables in `execute_process` command):
```
execute_process(
          COMMAND D:/local/android-ndk-r20b/ndk-build
          NDK_PROJECT_PATH=D:/local/android-ndk-r20b/sources/third_party/shaderc
          APP_BUILD_SCRIPT=D:/local/android-ndk-r20b/sources/third_party/shaderc/Android.mk
          APP_PLATFORM=android-29
          APP_STL=c++_static
          APP_ABI=arm64-v8a
          libshaderc_combined -j8
          WORKING_DIRECTORY D:/local/android-ndk-r20b/sources/third_party/shaderc
          RESULT_VARIABLE error_code)
CMake Error at cmake/VulkanDependencies.cmake:78 (message):
  Failed to build ANDROID_NDK shaderc error_code:The system cannot find the
  file specified
Call Stack (most recent call first):
  cmake/Dependencies.cmake:559 (include)
  CMakeLists.txt:564 (include)
```
This error I solved by executing this command manually.

When building the pytorch with the Vulkan backend there occur a lot of errors such as:
```
pytorch/aten/src/ATen/native/vulkan/VulkanOps.cpp:61:7: error: use of undeclared identifier 'upsample_nearest2d_glsl'
      GLSL_SPV(upsample_nearest2d), descriptorSetLayout, workGroupSize);
      ^
pytorch/aten/src\ATen/native/vulkan/Vulkan.h:17:31: note: expanded from macro 'GLSL_SPV'
#define GLSL_SPV(name) #name, name##_glsl
                              ^
<scratch space>:160:1: note: expanded from here
upsample_nearest2d_glsl
```
and
```
pytorch/aten/src/ATen/native/vulkan/ops/Mean.cpp:81:21: error: use of undeclared identifier 'mean_glsl'
          keepdim ? VK_KERNEL(mean) : VK_KERNEL(mean2d),
                    ^
pytorch/aten/src\ATen/native/vulkan/api/Common.h:11:5: note: expanded from macro 'VK_KERNEL'
    name##_glsl,                                 \
    ^
<scratch space>:14:1: note: expanded from here
mean_glsl
```
When `USE_VULKAN_SHADERC_RUNTIME` is enabled, these errors can be pretty easy fixed by the next patch:
```
diff --git a/aten/src/ATen/native/vulkan/Vulkan.h b/aten/src/ATen/native/vulkan/Vulkan.h
index c2b1775e8f..396ba9e57e 100644
--- a/aten/src/ATen/native/vulkan/Vulkan.h
+++ b/aten/src/ATen/native/vulkan/Vulkan.h
@@ -14,7 +14,7 @@
  #ifdef USE_VULKAN_SHADERC_RUNTIME
 #include <ATen/native/vulkan/glsl.h>
-#define GLSL_SPV(name) #name, name##_glsl
+#define GLSL_SPV(name) #name, #name "_glsl"
 #else
 #include <ATen/native/vulkan/spv.h>
 #define GLSL_SPV(name) #name, name##_spv, name##_spv_len
diff --git a/aten/src/ATen/native/vulkan/api/Common.h b/aten/src/ATen/native/vulkan/api/Common.h
index 90e05fe993..6d2e3e5b13 100644
--- a/aten/src/ATen/native/vulkan/api/Common.h
+++ b/aten/src/ATen/native/vulkan/api/Common.h
@@ -8,7 +8,7 @@
 #include <ATen/native/vulkan/glsl.h>
 #define VK_KERNEL(name)                          \
   ::at::native::vulkan::api::Shader::Descriptor{ \
-    name##_glsl,                                 \
+    #name "_glsl",                                 \
   }
 #else
 #include <ATen/native/vulkan/spv.h>
```
But for disabled `USE_VULKAN_SHADERC_RUNTIME` I didn't find a solution yet. There are same errors, but also there are errors caused by `name##_spv_len`.

And the last error after fixes above is
```
pytorch/aten/src/ATen/native/vulkan/api/Shader.cpp:74:13: error: no member named 'SetNanClamp' in 'shaderc::CompileOptions'
    options.SetNanClamp(/*enable =*/ true);
    ~~~~~~~ ^
1 error generated.
```
This error I "fixed" by commenting this line, but I am not sure that it is a correct way.

## To Reproduce

Steps to reproduce: see build command.

## Expected behavior

Generating and building without errors

## Environment

 - PyTorch Version 1.8.1
 - Host OS: Windows 10 20H2
 - Target OS: Android 10
 - Installing from source
 - Python version: 3.8.5
 - Androind NDK: r20b
 - Arch: arm64-v8a
 - Build command (from powershell script):
  ```
    git clone --recurse-submodules -j8 -b v1.8.1 https://github.com/pytorch/pytorch.git "$source_dir"
    $python_exec = (python -c "import sys; print(sys.executable)")
    $python_dir = $python_exec | Split-Path -Parent
    $env:PYTHONHOME="$source_dir;$python_dir"
    $env:PYTHONPATH="$python_dir/lib;$python_dir/lib/site-packages;$source_dir"
    cmake -S"$source_dir" -B"$build_dir" `
       "-GNinja" `
        "-DCMAKE_SYSTEM_NAME=Android" `
        "-DCMAKE_CROSSCOMPILING=ON" `
        "-DANDROID_ABI=arm64-v8a" `
        "-DANDROID_NDK=$ndk_root".Replace('\', '/') `
        "-DCMAKE_BUILD_TYPE=Release" `
        "-DCMAKE_TOOLCHAIN_FILE=$ndk_root/build/cmake/android.toolchain.cmake".Replace('\', '/') `
        "-DANDROID_PLATFORM=29" `
        "-DANDROID_TOOLCHAIN=clang" `
        "-DCMAKE_PREFIX_PATH=$install_dir;" `
        "-DCMAKE_FIND_ROOT_PATH=$install_dir" `
        "-DCMAKE_INSTALL_PREFIX=$install_dir" 
        "-DCMAKE_C_FLAGS=-Wno-everything" `
        "-DCMAKE_CXX_FLAGS=-Wno-everything" `
        "-DBUILD_SHARED_LIBS=ON" `
        "-DBUILD_TEST=OFF" `
        "-DBUILD_BINARY=OFF" `
        "-DBUILD_SHARED_LIBS=ON" `
        "-DUSE_VULKAN=ON" `
        "-DUSE_VULKAN_SHADERC_RUNTIME=ON" `
        "-DUSE_OPENMP=OFF" `
        "-DPYTHON_EXECUTABLE=$python_exec"
  cmake --build "$build_dir" -j $env:NUMBER_OF_PROCESSORS
  ```
 - cmake summary:
 ```
-- ******** Summary ********
-- General:
--   CMake version         : 3.19.2
--   CMake command         : D:/Program Files/CMake/bin/cmake.exe
--   System                : Android
--   C++ compiler          : D:/local/android-ndk-r20b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe
--   C++ compiler id       : Clang
--   C++ compiler version  : 8.0
--   CXX flags             : -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security 
 -Wno-everything -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DUSE_VULKAN -DUSE_VULKAN_API -DUSE_VULKAN_SHADERC_RUNTIME -DUSE_VULKAN_WRAPPER -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -Wno-invalid-partial-specialization -Wno-typedef-redefinition -Wno-unknown-warning-option -Wno-unused-private-field -Wno-inconsistent-missing-override -Wno-aligned-allocation-unavailable -Wno-c++14-extensions -Wno-constexpr-not-const -Wno-missing-braces -Qunused-arguments -fcolor-diagnostics -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Werror=cast-function-type -g0
--   Build type            : Release
--   Compile definitions   :
--   CMAKE_PREFIX_PATH     : D:/install/arm64-v8a/;
--   CMAKE_INSTALL_PREFIX  : D:/install/arm64-v8a
--
--   TORCH_VERSION         : 1.8.0
--   CAFFE2_VERSION        : 1.8.0
--   BUILD_CAFFE2          : ON
--   BUILD_CAFFE2_OPS      : OFF
--   BUILD_CAFFE2_MOBILE   : OFF
--   BUILD_STATIC_RUNTIME_BENCHMARK: OFF
--   BUILD_TENSOREXPR_BENCHMARK: OFF
--   BUILD_BINARY          : OFF
--   BUILD_CUSTOM_PROTOBUF : ON
--     Link local protobuf : ON
--   BUILD_DOCS            : OFF
--   BUILD_PYTHON          : OFF
--   BUILD_SHARED_LIBS     : ON
--   CAFFE2_USE_MSVC_STATIC_RUNTIME     : OFF
--   BUILD_TEST            : OFF
--   BUILD_JNI             : OFF
--   BUILD_MOBILE_AUTOGRAD : OFF
--   INTERN_BUILD_MOBILE   : ON
--   USE_BLAS              : 1
--     BLAS                :
--   USE_LAPACK            :
--   USE_ASAN              : OFF
--   USE_CPP_CODE_COVERAGE : OFF
--   USE_CUDA              : OFF
--   USE_ROCM              : OFF
--   USE_EIGEN_FOR_BLAS    : ON
--   USE_FBGEMM            : OFF
--     USE_FAKELOWP          : OFF
--   USE_KINETO            : OFF
--   USE_FFMPEG            : OFF
--   USE_GFLAGS            : OFF
--   USE_GLOG              : OFF
--   USE_LEVELDB           : OFF
--   USE_LITE_PROTO        : OFF
--   USE_LMDB              : OFF
--   USE_METAL             : OFF
--   USE_PYTORCH_METAL     : OFF
--   USE_FFTW              : OFF
--   USE_MKL               :
--   USE_MKLDNN            : OFF
--   USE_NCCL              : OFF
--   USE_NNPACK            : ON
--   USE_NUMPY             : ON
--   USE_OBSERVERS         : OFF
--   USE_OPENCL            : OFF
--   USE_OPENCV            : OFF
--   USE_OPENMP            : OFF
--   USE_TBB               : OFF
--   USE_VULKAN            : ON
--     USE_VULKAN_FP16_INFERENCE    : OFF
--     USE_VULKAN_RELAXED_PRECISION : OFF
--     USE_VULKAN_SHADERC_RUNTIME   : ON
--     USE_VULKAN_WRAPPER           : ON
--   USE_PROF              : OFF
--   USE_QNNPACK           : OFF
--   USE_PYTORCH_QNNPACK   : ON
--   USE_REDIS             : OFF
--   USE_ROCKSDB           : OFF
--   USE_ZMQ               : OFF
--   USE_DISTRIBUTED       : OFF
--   USE_DEPLOY           : OFF
--   Public Dependencies  : Threads::Threads
--   Private Dependencies : eigen_blas;pthreadpool;cpuinfo;pytorch_qnnpack;nnpack;XNNPACK;VulkanWrapper;D:/local/android-ndk-r20b/sources/third_party/shaderc/libs/c++_static/arm64-v8a/libshaderc.a;fp16;log;fmt::fmt-header-only;dl
 ```

cc @malfet @seemethere @walterddr