Slow torch import on macos

leoentersthevoid: <p>I’m having a hard time understanding the slow import of torch in later versions, including the most recent 2.0 release. I strongly believe this might be a cache issue, but I can’t put the nail in the coffin. I also ruled out is only on my end, my buddy’s macos is facing a similar issue. We’re also having different distributions, me Ventura, and he Big Sur.</p>
<p>Did anybody else face a similar issue? My current import time is taking on an average of 3.5s.</p>
<p>Thanks</p>

leoentersthevoid: <p>Digging deeper, I ran a profiler on <code>torch</code>’s <code>__init__.py</code> and got the following statistics:</p>
<pre data-code-wrap="shell"><code class="lang-shell">   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1287    1.824    0.001    1.824    0.001 {built-in method io.open_code}         &lt;--------------
    45/25    1.045    0.023    1.579    0.063 {built-in method _imp.create_dynamic}  &lt;--------------
     1287    0.136    0.000    0.136    0.000 {built-in method marshal.loads}
     5408    0.064    0.000    0.064    0.000 {built-in method posix.stat}
2841/2785    0.050    0.000    0.240    0.000 {built-in method builtins.__build_class__}
      424    0.047    0.000    0.080    0.000 assumptions.py:596(__init__)
   1635/1    0.042    0.000    4.315    4.315 {built-in method builtins.exec}
        2    0.040    0.020    0.040    0.020 {built-in method _ctypes.dlopen}
    51906    0.026    0.000    0.062    0.000 {built-in method builtins.getattr}
     1298    0.026    0.000    0.026    0.000 {method '__exit__' of '_io._IOBase' objects}
      164    0.023    0.000    0.023    0.000 {built-in method posix.listdir}
     1769    0.020    0.000    0.131    0.000 &lt;frozen importlib._bootstrap_external&gt;:1604(find_spec)
     1287    0.019    0.000    0.019    0.000 {method 'read' of '_io.BufferedReader' objects}
</code></pre>
<p>The <code>_imp.create_dynamic</code> is part of <code>importlib.machinery.ModuleSpec</code> and is called once inside <code>package_importer.py</code> here:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/b/4b283960e33a7d2f0c3be1a9dfc699fda3b74805.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/4b283960e33a7d2f0c3be1a9dfc699fda3b74805" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b283960e33a7d2f0c3be1a9dfc699fda3b74805_2_690x389.jpeg" alt="image" data-base62-sha1="aIRZnVhHVz9sNJWN99u5bS4nD1z" width="690" height="389" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b283960e33a7d2f0c3be1a9dfc699fda3b74805_2_690x389.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b283960e33a7d2f0c3be1a9dfc699fda3b74805_2_1035x583.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/b/4b283960e33a7d2f0c3be1a9dfc699fda3b74805_2_1380x778.jpeg 2x" data-dominant-color="2B293B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1494×844 166 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I’m really confused.</p>