How can I force operator available when statically linking?

daroo: <p>I’m building a program that utilizes libtorch to perform inference using static libs.  Things work alright if I link <code>libtorch_cpu.a</code> using the <code>whole-archive</code> approach, but I need to be able to build without <code>whole-archive</code>.  In the case without <code>whole-archive</code>, the build process succeeds, but I get an error at runtime that <code>aten::mul</code> isn’t available.  Web searches resulted in the same issue, but usually the solution is to use <code>whole-archive</code> which is what I need to avoid.</p>
<p>Given that that is always the solution, I’m sure it will be tedious, but I’d like to just try to manually register the operators that are needed for my particular inference capability.  My thought is to run the executable, see which operator it fails on, then update the code to manually register that operator, then rebuild, run, see which operator it complains about now, modify code to register it, and repeat as needed.</p>
<p>The problem is that I can’t figure out how to do the manual registration.  I’ve found some stuff about registering custom operators, but these aren’t custom ops.</p>
<p>In the pytorch source tree, I found some code that registers a couple of aten operators - I stole it and pasted it into my own code, and it does look it now registers those two operators twice now, so I think the approach might work, but despite a lot of searching, I haven’t been able to find analogous code for the operators I’m looking for.  For what its worth - this is the code I’ve added to my program:</p>
<pre><code class="lang-auto">#include &lt;torch/csrc/jit/runtime/register_ops_utils.h&gt;

namespace torch {
namespace jit {

RegisterOperators reg_ops_directly(
    {Operator(
         "aten::_ncf_unsqueeze(Tensor(a) self, int ndim) -&gt; Tensor(a)",
         [](Stack&amp; stack) {
           const int64_t ndim = pop(stack).toInt();
           auto self = pop(stack).toTensor();
           c10::SmallVector&lt;int64_t, 8&gt; sizes(ndim, 1);
           AT_ASSERT(self.dim() == 1);
           sizes.at(1) = self.size(0);
           push(stack, self.reshape(sizes));
         },
         aliasAnalysisFromSchema()),
     Operator(
         "aten::_ncf_view(Tensor(a) self, int[] input_shape, int normalized_ndim) -&gt; Tensor(a)",
         [](Stack&amp; stack) {
           const int64_t normalized_ndim = pop(stack).toInt();
           auto input_shape = pop(stack).toIntList();
           auto self = pop(stack).toTensor();
           const int64_t input_ndim = input_shape.size();
           c10::SmallVector&lt;int64_t, 8&gt; sizes(input_ndim, 1);
           for (int i = 0; i &lt; input_ndim - normalized_ndim; ++i) {
             sizes.at(i) = input_shape.get(i);
           }
           push(stack, self.reshape(sizes));
         },
         aliasAnalysisFromSchema())});
}
}
</code></pre>
<p>Without that code added, those two operators are listed in <code>torch::jit::getAllOperators</code> once, and with the code added, those two operators are listed twice.</p>
<p>Can I use this approach to manually register the <code>aten::mul</code> operator?  I fully realize there could be a lot more operators after that, which I am ok with - if I just figure out how to get the first one situated, I hope I’ll be able to get through others.</p>
<p>If there’s a different / better way (that doesn’t involve <code>whole-archive</code>), I’d be up for suggestions there too.  One thing I was thinking was to extract out the necessary .o files from <code>libtorch_cpu.a</code> and making a “sub-library” where I link with <code>whole-archive</code> for the (smaller) sub-library, but linked without <code>whole-archive</code> for the rest.  I’ve tried this a bunch, but have not been successful yet in determining which .o files need to be in the sub-library to make it work.  I like the idea of manually registering the ops better because it gives me better control over what is linked in and what is not, but like I said, open to suggestion.</p>
<p>[ Full disclosure: I posted an issue on github a while back for this same issue here: <a href="https://github.com/pytorch/pytorch/issues/111654" class="inline-onebox" rel="noopener nofollow ugc">Static Linking C++, Op not available at runtime · Issue #111654 · pytorch/pytorch · GitHub</a> ]</p>
<p>I really appreciate any guidance - thanks!</p>