<p dir="auto">Today doing something like:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@torch.jit.script
def foo(s: Any):
    if isinstance(s, dict):
        print(s.items())"><pre class="notranslate"><code class="notranslate">@torch.jit.script
def foo(s: Any):
    if isinstance(s, dict):
        print(s.items())
</code></pre></div>
<p dir="auto">gives:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="RuntimeError: Unknown type name 'dict':"><pre class="notranslate"><code class="notranslate">RuntimeError: Unknown type name 'dict':
</code></pre></div>
<p dir="auto">And capitalizing "<code class="notranslate">dict</code>" will produce a segfault. Yikes!</p>
<p dir="auto">A related issue is that doing something like:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="isinstance(foo, Dict[str, Tensor])"><pre class="notranslate"><code class="notranslate">isinstance(foo, Dict[str, Tensor])
</code></pre></div>
<p dir="auto">is not actually possible in Python, since the type annotations are mostly static constructs that the interpreter doesn't know anything about.</p>
<p dir="auto">A potential compromise is to have <code class="notranslate">isinstance(foo, dict)</code> refine the type of <code class="notranslate">foo</code> to <code class="notranslate">Dict[Any, Any]</code>. And then the user can further type refine to get an actual things they want. So:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="if isinstance(foo, dict):
    for k, v in foo.items():
        assert isinstance(k, str)
        assert isinstance(v, Tensor)"><pre class="notranslate"><code class="notranslate">if isinstance(foo, dict):
    for k, v in foo.items():
        assert isinstance(k, str)
        assert isinstance(v, Tensor)
</code></pre></div>
<p dir="auto">Another potential solution is to have a <code class="notranslate">torch.jit.isinstance(foo, Dict[str, tensor])</code> that boils down to <code class="notranslate">isinstance(foo, dict)</code> in Python but performs the correct type refinement in TS.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/suo/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/suo">@suo</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/gmagogsfm/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/gmagogsfm">@gmagogsfm</a></p>
<h2 dir="auto">🐛 Bug</h2>
<p dir="auto">PyTorch 1.6.0 is imported with a segmentation fault when built from source on a computer with an old CPU (not supporting AVX/AVX2/SSE4 instructions).</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<ol dir="auto">
<li>Run <code class="notranslate">python setup.py build</code> on a computer with an old CPU (such as AMD Athlon II X4).</li>
<li><code class="notranslate">python -c import torch</code></li>
<li>???</li>
<li>Segmentation fault!</li>
</ol>
<p dir="auto">Log of <code class="notranslate">gdb -ex r --args python -c "import torch"</code>: <a href="https://pastebin.com/RuXGLdLE" rel="nofollow">https://pastebin.com/RuXGLdLE</a><br>
Output of <code class="notranslate">cat /proc/cpuinfo</code>: <a href="https://pastebin.com/EgPSEGS0" rel="nofollow">https://pastebin.com/EgPSEGS0</a></p>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">Successful build and package import.</p>
<h2 dir="auto">Environment</h2>
<h3 dir="auto">When <code class="notranslate">torch</code> is installed</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python collect_env.py"><pre class="notranslate"><code class="notranslate">$ python collect_env.py
</code></pre></div>
<p dir="auto">returns <code class="notranslate">Illegal instruction (core dumped)</code>.</p>
<h3 dir="auto">When <code class="notranslate">torch</code> is not installed</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python collect_env.py"><pre class="notranslate"><code class="notranslate">$ python collect_env.py
</code></pre></div>
<p dir="auto">exits with the error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Collecting environment information...
Traceback (most recent call last):
  File &quot;collect_env.py&quot;, line 429, in &lt;module&gt;
    main()
  File &quot;collect_env.py&quot;, line 424, in main
    output = get_pretty_env_info()
  File &quot;collect_env.py&quot;, line 419, in get_pretty_env_info
    return pretty_str(get_env_info())
  File &quot;collect_env.py&quot;, line 276, in get_env_info
    if torch.version.hip is None:  # cuda version
NameError: name 'torch' is not defined"><pre class="notranslate"><code class="notranslate">Collecting environment information...
Traceback (most recent call last):
  File "collect_env.py", line 429, in &lt;module&gt;
    main()
  File "collect_env.py", line 424, in main
    output = get_pretty_env_info()
  File "collect_env.py", line 419, in get_pretty_env_info
    return pretty_str(get_env_info())
  File "collect_env.py", line 276, in get_env_info
    if torch.version.hip is None:  # cuda version
NameError: name 'torch' is not defined
</code></pre></div>
<ul dir="auto">
<li>PyTorch Version (e.g., 1.0): 1.6</li>
<li>OS (e.g., Linux): Linux 5.8.13-arch1-1 <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="171281708" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/1" data-hovercard-type="issue" data-hovercard-url="/pytorch/pytorch/issues/1/hovercard" href="https://github.com/pytorch/pytorch/issues/1">#1</a> SMP PREEMPT Thu, 01 Oct 2020 20:40:35 +0000 x86_64 GNU/Linux</li>
<li>How you installed PyTorch (<code class="notranslate">conda</code>, <code class="notranslate">pip</code>, source): source</li>
<li>Build command you used (if compiling from source): <code class="notranslate">makepkg -s</code></li>
<li>Python version: 3.8.5</li>
<li>CUDA/cuDNN version: 11.1</li>
<li>GPU models and configuration: <code class="notranslate">01:00.0 VGA compatible controller: NVIDIA Corporation TU117 [GeForce GTX 1650] (rev a1)</code></li>
<li>Any other relevant information:</li>
</ul>
<h2 dir="auto">Additional context</h2>
<p dir="auto">I'm building <code class="notranslate">pytorch</code> using ArchLinux PKGBUILD file from this repo: <a href="https://github.com/archlinux/svntogit-community/tree/packages/python-pytorch/trunk">https://github.com/archlinux/svntogit-community/tree/packages/python-pytorch/trunk</a></p>
<p dir="auto">As can be seen, it contains a section with a x86_64 preconfiguration (meant to turn off any optimizations) and this <a href="https://github.com/archlinux/svntogit-community/blob/packages/python-pytorch/trunk/disable_non_x86_64.patch">patch</a> to disable AVX/AVX2/etc.</p>