RuntimeError: attribute 'name' has the wrong type in Pytorch v2.1 with libc++

FatemeH: <p>Hi,<br>
I built the latest pytorch(v2.1.0) with libc++. Now whenever I run torch._C._jit_pass_onnx_function_substitution(graph) function, I get this error:</p>
<blockquote>
<p>RuntimeError: required keyword attribute ‘name’ has the wrong type</p>
</blockquote>
<p>Here is an example code that I run:</p>
<pre><code class="lang-auto">import torch

class MyModel(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.conv = torch.nn.Conv2d(in_channels=3, out_channels=10, kernel_size=3, padding=1)
    def forward(self, x):
        return self.conv(x)
    
model = MyModel()
inputs = torch.randn(1,3,224,224)

traced_model = torch.jit.trace(model, inputs)

graph = traced_model.graph
torch._C._jit_pass_onnx_function_substitution(graph)

</code></pre>
<p>I traced the error using debugger and it took me to line 133 of functionCallSubstitution function in <a href="https://github.com/pytorch/pytorch/blob/3fcc5ff0d670747a267ad0645f4b2b64ce29234d/torch/csrc/jit/passes/onnx/function_substitution.cpp#L133" rel="noopener nofollow ugc">https://github.com/pytorch/pytorch/blob/3fcc5ff0d670747a267ad0645f4b2b64ce29234d/torch/csrc/jit/passes/onnx/function_substitution.cpp#L133</a></p>
<pre><code class="lang-auto">const std::string&amp; name = cur-&gt;s(attr::name);
</code></pre>
<p>which in turn took me to</p>
<pre><code class="lang-auto">CREATE_ACCESSOR(String, s)
</code></pre>
<p>and to this getAttr function from there:</p>
<pre><code class="lang-auto"> template &lt;typename T&gt;
 typename T::ValueType&amp; getAttr(Symbol name) const {
   AT_ASSERT(name.is_attr());
   auto it = findAttr(name, true);
   auto* child = dynamic_cast&lt;T*&gt;(it-&gt;get());
   if (child == nullptr) {
     throw IRAttributeError(name, true);
   }
   return child-&gt;value();
 }
</code></pre>
<p>The “it” variable is a nullpointer.</p>
<p>The error only occurs if I build torch with libc++. This is the command I used to build pytorch with libc++:</p>
<pre><code class="lang-auto">CC=clang CXX=clang++ CXXFLAGS="-stdlib=libc++"  CMAKE_PREFIX_PATH=/local/mnt/workspace/fh/Software/pytorch-install/lib CMAKE_CXX_FLAGS=-stdlib=libc++ CMAKE_EXE_LINKER_FLAGS=-stdlib=libc++ python3 setup.py develop
</code></pre>
<p>I’d really appreciate it if anyone could provide some insight on this question.</p>
<p>Thank you</p>

ptrblck: <p>I don’t know if <code>libc++</code> is supported or if e.g. <code>libstdc++</code> would be needed, but you might want to create a GitHub issues so that the code owners could also take a look at it and chime in.<br>
Also CC <a class="mention" href="/u/malfet">@malfet</a> in case you are aware of the current toolchain limitations.</p>

FatemeH: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a>. I need to install torch with libcpp because the libstdcpp version throws pybind11 exception when used in conjunction with tvm but I could not find a definite answer to whether pytorch is supposed to work.with libc++… I’ll open a github issue for my issue.</p>

blrm: <p>You need to enable dynamic_fallback in libcxxabi. You can also rebuild clang tools by defining it in libcxxabi/src/private_typeinfo.cpp (<span class="hashtag">#define</span> _LIBCXX_DYNAMIC_FALLBACK)</p>

FatemeH: <p>Thank you very much <a class="mention" href="/u/blrm">@blrm</a>. You are right, I did not built my libcxxabi. So, would these steps do the work instead of the <span class="hashtag-raw">#define</span> process that you mentioned (I’m not sure I fully understand it)?</p>
<ol>
<li>Rebuild my llvm with clang by adding these flags:</li>
</ol>
<blockquote>
<p>-DLLVM_ENABLE_RUNTIMES=‘libcxx;libcxxabi;libunwind’<br>
-DLIBCXXABI_USE_LLVM_UNWINDER=ON</p>
</blockquote>
<ol start="2">
<li>pass</li>
</ol>
<blockquote>
<p>DLIBCXXABI_USE_LLVM_UNWINDER=ON</p>
</blockquote>
<p>to my pytorch build command?</p>

blrm: <aside class="quote no-group" data-username="blrm" data-post="4" data-topic="177175">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/blrm/48/42881_2.png" class="avatar"> blrm:</div>
<blockquote>
<p>LIBCXX_DYNAMIC_FALLBACK</p>
</blockquote>
</aside>
<p>You need to do both. You can see <a href="https://github.com/pytorch/pytorch/issues/65397" class="inline-onebox" rel="noopener nofollow ugc">Vague linkage when using libcxxabi built torch in torch_glow · Issue #65397 · pytorch/pytorch · GitHub</a> for more details.</p>