Linking static libtorch libraries

username: pizuzadan
cooked: <p>I built static libtorch from sources on windows successfully.<br>
But now when I compile my application, I must explicitly link with onnx, onnx_proto, caffe2_ptoros, mkldnn, mkl, protobuf libraries (my compile options). This is not convenient, but it works. But for fbgemm library this doesn’t work. I get link errors:</p>
<pre><code class="lang-auto">torch_cpu.lib(Context.cpp.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) bool __cdecl fbgemm::fbgemmSupportedCPU(void)" (__imp_?fbgemmSupportedCPU@
fbgemm@@YA_NXZ) referenced in function "public: class std::vector&lt;enum c10::QEngine,class std::allocator&lt;enum c10::QEngine&gt; &gt; __cdecl &lt;lambda_8dd2a8e98f77cf43df1066af8f7a0e
c7&gt;::operator()(void)const " (??R&lt;lambda_8dd2a8e98f77cf43df1066af8f7a0ec7&gt;@@QEBA?AV?$vector@W4QEngine@c10@@V?$allocator@W4QEngine@c10@@@std@@@std@@XZ) [D:\reps\libtorch_tes
t\deploy\example-app.vcxproj]
torch_cpu.lib(Quantizer.cpp.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl fbgemm::fbgemmPartition1D(int,int,int,int &amp;,int &amp;)" (__imp_
?fbgemmPartition1D@fbgemm@@YAXHHHAEAH0@Z) referenced in function "void __cdecl fbgemm::Dequantize&lt;signed char&gt;(signed char const *,float *,int,struct fbgemm::TensorQuantiza
tionParams const &amp;,int,int)" (??$Dequantize@C@fbgemm@@YAXPEBCPEAMHAEBUTensorQuantizationParams@0@HH@Z) [D:\reps\libtorch_test\deploy\example-app.vcxproj]
torch_cpu.lib(Quantizer.cpp.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl fbgemm::Quantize&lt;signed char&gt;(float const *,signed char *,i
nt,struct fbgemm::TensorQuantizationParams const &amp;,int,int)" (__imp_??$Quantize@C@fbgemm@@YAXPEBMPEACHAEBUTensorQuantizationParams@0@HH@Z) referenced in function "void __cd
ecl at::parallel_for$omp$1&lt;class &lt;lambda_11019aac2f2763b0156a5bd7dbaca06d&gt; &gt;(__int64,__int64,__int64,class &lt;lambda_11019aac2f2763b0156a5bd7dbaca06d&gt; const &amp;)" (??$parallel_
for$omp$1@V&lt;lambda_11019aac2f2763b0156a5bd7dbaca06d&gt;@@@at@@YAX_J00AEBV&lt;lambda_11019aac2f2763b0156a5bd7dbaca06d&gt;@@@Z) [D:\reps\libtorch_test\deploy\example-app.vcxproj]
torch_cpu.lib(Quantizer.cpp.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl fbgemm::Quantize&lt;unsigned char&gt;(float const *,unsigned char
 *,int,struct fbgemm::TensorQuantizationParams const &amp;,int,int)" (__imp_??$Quantize@E@fbgemm@@YAXPEBMPEAEHAEBUTensorQuantizationParams@0@HH@Z) referenced in function "void
__cdecl at::parallel_for$omp$1&lt;class &lt;lambda_7b353a87181e064310fa419329247f98&gt; &gt;(__int64,__int64,__int64,class &lt;lambda_7b353a87181e064310fa419329247f98&gt; const &amp;)" (??$paral
lel_for$omp$1@V&lt;lambda_7b353a87181e064310fa419329247f98&gt;@@@at@@YAX_J00AEBV&lt;lambda_7b353a87181e064310fa419329247f98&gt;@@@Z) [D:\reps\libtorch_test\deploy\example-app.vcxproj]
torch_cpu.lib(Quantizer.cpp.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl fbgemm::Quantize&lt;int&gt;(float const *,int *,int,struct fbgemm
::TensorQuantizationParams const &amp;,int,int)" (__imp_??$Quantize@H@fbgemm@@YAXPEBMPEAHHAEBUTensorQuantizationParams@0@HH@Z) referenced in function "void __cdecl at::parallel
_for$omp$1&lt;class &lt;lambda_ac0c20011d29a38651cff09b26e44b16&gt; &gt;(__int64,__int64,__int64,class &lt;lambda_ac0c20011d29a38651cff09b26e44b16&gt; const &amp;)" (??$parallel_for$omp$1@V&lt;lamb
da_ac0c20011d29a38651cff09b26e44b16&gt;@@@at@@YAX_J00AEBV&lt;lambda_ac0c20011d29a38651cff09b26e44b16&gt;@@@Z) [D:\reps\libtorch_test\deploy\example-app.vcxproj]
torch_cpu.lib(THBlas.cpp.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl fbgemm::cblas_gemm_i64_i64acc(enum fbgemm::matrix_op_t,enum fb
gemm::matrix_op_t,int,int,int,__int64 const *,int,__int64 const *,int,bool,__int64 *,int)" (__imp_?cblas_gemm_i64_i64acc@fbgemm@@YAXW4matrix_op_t@1@0HHHPEB_JH1H_NPEA_JH@Z)
referenced in function THLongBlas_gemm [D:\reps\libtorch_test\deploy\example-app.vcxproj]
D:\reps\libtorch_test\deploy\Release\example-app.exe : fatal error LNK1120: 6 unresolved externals [D:\reps\libtorch_test\deploy\example-app.vcxproj]
</code></pre>
<p>This means that the libtorch wants to link to the fbgemm as dynamic library. But fbgemm built as static library without __declspec(dllexport) .</p>

username: peterjc123
cooked: <aside class="onebox githubpullrequest">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/pull/39848" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Pull Request">
    <svg width="60" height="60" class="github-icon" viewBox="0 0 12 16" aria-hidden="true"><path d="M11 11.28V5c-.03-.78-.34-1.47-.94-2.06C9.46 2.35 8.78 2.03 8 2H7V0L4 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 10 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zM4 3c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v6.56A1.993 1.993 0 0 0 2 15a1.993 1.993 0 0 0 1-3.72V4.72c.59-.34 1-.98 1-1.72zm-.8 10c0 .66-.55 1.2-1.2 1.2-.65 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/pull/39848" target="_blank" rel="nofollow noopener">[TEST ONLY] static libtorch builds for Windows</a>
    </h4>

    <div class="branches">
      <code>pytorch:master</code> ← <code>peterjc123:test_static_new</code>
    </div>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-06-11" data-time="08:41:02" data-timezone="UTC">08:41AM - 11 Jun 20 UTC</span>
      </div>

      <div class="user">
        <a href="https://github.com/peterjc123" target="_blank" rel="nofollow noopener">
          <img alt="peterjc123" src="https://avatars3.githubusercontent.com/u/9998726?v=4" class="onebox-avatar-inline" width="20" height="20">
          peterjc123
        </a>
      </div>

      <div class="lines" title="3 commits changed 3 files with 7 additions and 2 deletions">
        <a href="https://github.com/pytorch/pytorch/pull/39848/files" target="_blank" rel="nofollow noopener">
          <span class="added">+7</span>
          <span class="removed">-2</span>
        </a>
      </div>
    </div>

  </div>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
And you’ll need to add <code>-DFBGEMM_STATIC=1</code> to the compiler flags for your project.

username: pizuzadan
cooked: <p>This solved one problem. But now in runtime, when called</p>
<p><code>torch::jit::script::Module module = torch::jit::load(filename);</code></p>
<p>where filename is a traced torchscript model. I get a new error.:</p>
<pre><code class="lang-auto">Unknown builtin op: aten::mul.
Could not find any similar ops to aten::mul. This op may not exist or may not be currently supported in TorchScript.
:
  File "&lt;string&gt;", line 3

def mul(a : float, b : Tensor) -&gt; Tensor:
  return b * a
         ~~~~~ &lt;--- HERE
def add(a : float, b : Tensor) -&gt; Tensor:
  return b + a
'mul' is being compiled since it was called from 'Conv2d.forward'
D:\Soft\Miniconda3\envs\caffe2_to_pytorch\lib\site-packages\torch\nn\modules\conv.py(346): _conv_forward
D:\Soft\Miniconda3\envs\caffe2_to_pytorch\lib\site-packages\torch\nn\modules\conv.py(349): forward
D:\Soft\Miniconda3\envs\caffe2_to_pytorch\lib\site-packages\torch\nn\modules\module.py(534): _slow_forward
D:\Soft\Miniconda3\envs\caffe2_to_pytorch\lib\site-packages\torch\nn\modules\module.py(548): __call__
C:/Users/PycharmProjects/caffe2_to_pytorch/pytorch.py(58): forward
D:\Soft\Miniconda3\envs\caffe2_to_pytorch\lib\site-packages\torch\nn\modules\module.py(534): _slow_forward
D:\Soft\Miniconda3\envs\caffe2_to_pytorch\lib\site-packages\torch\nn\modules\module.py(548): __call__
D:\Soft\Miniconda3\envs\caffe2_to_pytorch\lib\site-packages\torch\jit\__init__.py(1027): trace_module
D:\Soft\Miniconda3\envs\caffe2_to_pytorch\lib\site-packages\torch\jit\__init__.py(875): trace
C:/Users/PycharmProjects/caffe2_to_pytorch/pytorch.py(201): &lt;module&gt;
C:\Program Files\JetBrains\PyCharm Community Edition 2019.2\helpers\pydev\_pydev_imps\_pydev_execfile.py(18): execfile
C:\Program Files\JetBrains\PyCharm Community Edition 2019.2\helpers\pydev\pydevd.py(1412): _exec
C:\Program Files\JetBrains\PyCharm Community Edition 2019.2\helpers\pydev\pydevd.py(1405): run
C:\Program Files\JetBrains\PyCharm Community Edition 2019.2\helpers\pydev\pydevd.py(2054): main
C:\Program Files\JetBrains\PyCharm Community Edition 2019.2\helpers\pydev\pydevd.py(2060): &lt;module&gt;
Serialized   File "code/__torch__/torch/nn/modules/conv.py", line 9
    x: Tensor) -&gt; Tensor:
    _0 = self.bias
    input = torch._convolution(x, self.weight, _0, [1, 1], [0, 0], [1, 1], False, [0, 0], 1, False, False, True)
                                                                                                           ~~~~ &lt;--- HERE
    return input
</code></pre>
<p>or where file is the same scripted model:</p>
<pre><code class="lang-auto">Couldn't find an operator for aten::dropout(Tensor input, float p, bool train) -&gt; Tensor. Do you have to update a set of hardcoded JIT ops? 
(lookupByLiteral at C:\...\torch\csrc\jit\runtime\operator.cpp:72)
</code></pre>
<p>Dynamic build is working fine.</p>

username: pizuzadan
cooked: <p><code> set_target_properties(app PROPERTIES LINK_FLAGS "-WHOLEARCHIVE:torch_cpu.lib")</code></p>
<p>It helped me.</p>