Build shared lib fail at linking stage

julien-ys: <p>Hello there,</p>
<p>I am trying to build the lite version in Release on Windows. The goal is to get a smaller shared lib that focus on the inference part of Pytorch.<br>
After trying multiple times, both following README instructions from the repository and using the builder available on Git, I’m reaching a point where everything seems to build correctly but the lib generation fails at linking stage.<br>
Does anyone know what’s happening here ?<br>
Thanks for reading me !</p>
<p>I’m providing the logs after removing a few lines of unresolved external symbols for clarity :</p>
<pre><code class="lang-auto">[6837/7346] Linking CXX shared library bin\torch_cpu.dll
FAILED: bin/torch_cpu.dll lib/torch_cpu.lib 
C:\Windows\system32\cmd.exe /C "cd . &amp;&amp; "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_dll --intdir=caffe2\CMakeFiles\torch_cpu.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\mt.exe --manifests  -- C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1437~1.328\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\torch_cpu.rsp  /out:bin\torch_cpu.dll /implib:lib\torch_cpu.lib /pdb:bin\torch_cpu.pdb /dll /version:0.0 /machine:x64 /ignore:4049 /ignore:4217 /ignore:4099 /INCREMENTAL:NO  -WHOLEARCHIVE:C:/Users/juliena/Desktop/builder/windows/pytorch/build/lib/caffe2_protos.lib -WHOLEARCHIVE:C:/Users/juliena/Desktop/builder/windows/pytorch/build/lib/onnx.lib  &amp;&amp; cd ."
LINK: command "C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1437~1.328\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\torch_cpu.rsp /out:bin\torch_cpu.dll /implib:lib\torch_cpu.lib /pdb:bin\torch_cpu.pdb /dll /version:0.0 /machine:x64 /ignore:4049 /ignore:4217 /ignore:4099 /INCREMENTAL:NO -WHOLEARCHIVE:C:/Users/juliena/Desktop/builder/windows/pytorch/build/lib/caffe2_protos.lib -WHOLEARCHIVE:C:/Users/juliena/Desktop/builder/windows/pytorch/build/lib/onnx.lib /MANIFEST:EMBED,ID=2" failed (exit code 1120) with the following output:
   Creating library lib\torch_cpu.lib and object lib\torch_cpu.exp
AdaptiveAvgPoolKernel.cpp.AVX2.cpp.obj : error LNK2001: unresolved external symbol "void __cdecl at::share_memory_(class at::TensorBase &amp;)" (?share_memory_@at@@YAXAEAVTensorBase@1@@Z)
Activation.cpp.AVX2.cpp.obj : error LNK2001: unresolved external symbol "void __cdecl at::share_memory_(class at::TensorBase &amp;)" (?share_memory_@at@@YAXAEAVTensorBase@1@@Z)
BinaryOpsKernel.cpp.AVX2.cpp.obj : error LNK2001: unresolved external symbol "void __cdecl at::share_memory_(class at::TensorBase &amp;)" (?share_memory_@at@@YAXAEAVTensorBase@1@@Z)

... (a lot of unresolved external sympbols here)

sgd.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl torch::nn::Module::load(class torch::serialize::InputArchive &amp;)" (?load@Module@nn@torch@@UEAAXAEAVInputArchive@serialize@3@@Z)
sgd.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl torch::nn::Module::pretty_print(class std::basic_ostream&lt;char,struct std::char_traits&lt;char&gt; &gt; &amp;)const " (?pretty_print@Module@nn@torch@@UEBAXAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@@Z)
sgd.cpp.obj : error LNK2001: unresolved external symbol "public: virtual bool __cdecl torch::nn::Module::is_serializable(void)const " (?is_serializable@Module@nn@torch@@UEBA_NXZ)

sgd.cpp.obj : error LNK2001: unresolved external symbol "private: virtual void __cdecl torch::nn::Module::clone_(class torch::nn::Module &amp;,class std::optional&lt;struct c10::Device&gt; const &amp;)" (?clone_@Module@nn@torch@@EEAAXAEAV123@AEBV?$optional@UDevice@c10@@@std@@@Z)

bin\torch_cpu.dll : fatal error LNK1120: 80 unresolved externals
</code></pre>

julien-ys: <p>Is there anyone that can help me please ?<br>
I can provide more logs if necessary… I really need to compile a lite version and I can’t make it using the provided scripts</p>
<p>Thank you again <img src="https://discuss.pytorch.org/images/emoji/apple/pray.png?v=12" title=":pray:" class="emoji" alt=":pray:" loading="lazy" width="20" height="20"></p>

konan009: <p>I have exact same error, were you able to solve this ?</p>

julien-ys: <p>I was indeed<br>
A bit sad noone answered that one actually. The fact is, using the BUILD_LITE_INTERPRETER flag is working for mobile only and not for Windows. You simply can’t get inference only builds which is kind of sad really… Still working on trying to reduce the size of the output dll which grows quite fast when including a couple GPU architectures</p>

konan009: <p>Completely fine on my side. I haven’t tried rebuilding on windows yet but I was able to build pytorch on <em>WSL Ubuntu 22.04</em>. Looking at my last settings, I already have it :</p>
<pre><code class="lang-auto">export USE_CUDA=1 USE_CUDNN=1 USE_MKLDNN=0 USE_DISTRIBUTED=0 DEBUG=1  BUILD_SHARED_LIBS=ON
export USE_NINJA=0 USE_OPENMP=0 MAX_JOBS=3 USE_FLASH_ATTENTION=0  USE_ITT=0 USE_OBSERVERS=0 VERBOSE=1
export USE_MEM_EFF_ATTENTION=0 BUILD_TEST=0 USE_GLOO=0 USE_XNNPACK=0 USE_MIMALLOC=0 USE_NCCL=0
export USE_FBGEMM=0 USE_PYTORCH_QNNPACK=0 USE_VULKAN=0 BUILD_LITE_INTERPRETER=0 USE_KINETO=0
</code></pre>
<p>I think I’m going to give it a try again and will give here an update : Maybe, my build problem is diffrent to yours — but yeah my reply might take a while since im currently working on different problem. Thanks for the update.</p>

julien-ys: <p>Thank you for the update. Indeed you are not trying to build the lite interpreter so this is not the same issue. My advice would be to take a peak in .ci/pytorch and look at the different scripts to see what you might have missed in the environment setup and more importantly to start with default values of the flags as defined in CMakeLists.txt and try to change the flags one by one to see which one is causing your issue.</p>
<p>Good luck !</p>