Cuda installation on M3

username: djdoogle123
cooked: <p>So I am trying to install this plug in which is built for Linux/Windows</p>
<aside class="onebox githubrepo" data-onebox-src="https://github.com/Theo-SAMINADIN-td/NukeSamurai">
  <header class="source">

      <a href="https://github.com/Theo-SAMINADIN-td/NukeSamurai" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <div class="github-row" data-github-private-repo="false">
  <img width="690" height="344" src="https://opengraph.githubassets.com/26964ec0b92f55fb9eb3308bb4846d3a/Theo-SAMINADIN-td/NukeSamurai" class="thumbnail">

  <h3><a href="https://github.com/Theo-SAMINADIN-td/NukeSamurai" target="_blank" rel="noopener nofollow ugc">GitHub - Theo-SAMINADIN-td/NukeSamurai: Samurai for Nuke allows you to generate masks from...</a></h3>

    <p><span class="github-repo-description">Samurai for Nuke allows you to generate masks from an image sequence, based on SAMURAI adaptation of SAM2</span></p>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>~ I have managed to get it installed in venv with python 3.10, with torch 2.5.1 , I have the node loaded into to nuke I can create a bounding box and set files, but [create mask] in debugging script I get;</p>
<p>File “/Applications/Nuke15.1v5/Nuke15.1v5.app/Contents/MacOS/plugins/ENV_DIR/venv/lib/python3.10/site-packages/torch/cuda/<strong>init</strong>.py”, line 310, in _lazy_init<br>
raise AssertionError(“Torch not compiled with CUDA enabled”)<br>
AssertionError: Torch not compiled with CUDA enabled</p>
<p>~ Do I need to compile PyTorch myself with CUDA enabled?</p>
<p>~ Can I just swap in the SAM repo folder out in installation for the CPU version posted below.</p>
<aside class="onebox githubrepo" data-onebox-src="https://github.com/SauravMaheshkar/samv2">
  <header class="source">

      <a href="https://github.com/SauravMaheshkar/samv2" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <div class="github-row" data-github-private-repo="false">
  <img width="690" height="344" src="https://opengraph.githubassets.com/7020544075e2e2e80e761e3192cad676/SauravMaheshkar/samv2" class="thumbnail">

  <h3><a href="https://github.com/SauravMaheshkar/samv2" target="_blank" rel="noopener nofollow ugc">GitHub - SauravMaheshkar/samv2: CPU compatible fork of the official SAMv2...</a></h3>

    <p><span class="github-repo-description">CPU compatible fork of the official SAMv2 implementation aimed at more accessible and documented tutorials</span></p>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: ptrblck
cooked: <aside class="quote no-group" data-username="djdoogle123" data-post="1" data-topic="215620">
<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/djdoogle123/48/75207_2.png" class="avatar"> djdoogle123:</div>
<blockquote>
<p>Do I need to compile PyTorch myself with CUDA enabled?</p>
</blockquote>
</aside>
<p>No, CUDA is not supported on Mac anymore.</p>
<aside class="quote no-group" data-username="djdoogle123" data-post="1" data-topic="215620">
<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/djdoogle123/48/75207_2.png" class="avatar"> djdoogle123:</div>
<blockquote>
<p>Can I just swap in the SAM repo folder out in installation for the CPU version posted below</p>
</blockquote>
</aside>
<p>This idea sounds valid.<br>
Instead of installing another repository you could also check if the currently used one could also support CPU-only runs or MPS.</p>

username: djdoogle123
cooked: <p>Thanks for your reply <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>~ I checked and it does, and this is the repo I have installed.</p>
<p>You can pull the latest code and reinstall via</p>
<h1><a name="p-463649-run-the-line-below-inside-the-sam-2-repo-git-pull-pip-uninstall-y-sam-2-rm-f-sam2so-pip-install-e-demo-1" class="anchor" href="#p-463649-run-the-line-below-inside-the-sam-2-repo-git-pull-pip-uninstall-y-sam-2-rm-f-sam2so-pip-install-e-demo-1"></a>run the line below inside the SAM 2 repo git pull; pip uninstall -y SAM-2; rm -f sam2/*.so; pip install -e “.[demo]”</h1>
<p>which allows using SAM 2 without CUDA extension (the results should stay the same in most cases, see <a href="https://github.com/facebookresearch/segment-anything-2/blob/main/INSTALL.md" rel="noopener nofollow ugc"><code>INSTALL.md</code></a> for details).</p>
<p>~ but that doesn’t explain why I am getting this error?<br>
Could I be using the wrong PyTorch?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="djdoogle123" data-post="3" data-topic="215620">
<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/djdoogle123/48/75207_2.png" class="avatar"> djdoogle123:</div>
<blockquote>
<p>but that doesn’t explain why I am getting this error?</p>
</blockquote>
</aside>
<aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="215620">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>No, CUDA is not supported on Mac anymore.</p>
</blockquote>
</aside>
<p>You cannot run CUDA code on a platform that does not support NVIDIA devices and the CUDA toolkit.</p>

username: djdoogle123
cooked: <p>but in this post it says I can?</p>
<aside class="onebox githubissue" data-onebox-src="https://github.com/facebookresearch/sam2/issues/122">
  <header class="source">

      <a href="https://github.com/facebookresearch/sam2/issues/122" target="_blank" rel="noopener nofollow ugc">github.com/facebookresearch/sam2</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue" data-github-private-repo="false">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/facebookresearch/sam2/issues/122" target="_blank" rel="noopener nofollow ugc">I want to install SAM 2 on M1 Macbook, but with CUDA error</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2024-08-02" data-time="23:39:52" data-timezone="UTC">11:39PM - 02 Aug 24 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2024-08-12" data-time="19:27:02" data-timezone="UTC">07:27PM - 12 Aug 24 UTC</span>
        </div>

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

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          installation
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">Does that mean I can't do it right now?

Thanks.</p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: ptrblck
cooked: <blockquote>
<p>we have recently made the CUDA extension step optional (in <a href="https://github.com/facebookresearch/sam2/pull/155">#155</a>) as a workaround to this problem. […]  which allows using SAM 2 without CUDA extension</p>
</blockquote>
<p>No, you cannot run the CUDA extension on your M3 and would need to disable it.</p>

username: djdoogle123
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>But if I was to follow this tutorial?</p>
<div class="youtube-onebox lazy-video-container" data-video-id="53PjsHUd46E" data-video-title="Code with me: Machine learning on a Macbook GPU (works for all M1, M2, M3) for a 10x speedup" data-video-start-time="" data-provider-name="youtube">
  <a href="https://www.youtube.com/watch?v=53PjsHUd46E" target="_blank" class="video-thumbnail" rel="noopener nofollow ugc">
    <img class="youtube-thumbnail" src="https://img.youtube.com/vi/53PjsHUd46E/maxresdefault.jpg" title="Code with me: Machine learning on a Macbook GPU (works for all M1, M2, M3) for a 10x speedup" width="690" height="388">
  </a>
</div>


username: djdoogle123
cooked: <p>Also <a class="mention" href="/u/ptrblck">@ptrblck</a> I think your missing the part where it says <em>without</em> Cuda extension</p>

username: ptrblck
cooked: <p>Your error shows you are trying to initialize CUDA on an unsupported platform:</p>
<pre data-code-wrap="python"><code class="lang-python">File “/Applications/Nuke15.1v5/Nuke15.1v5.app/Contents/MacOS/plugins/ENV_DIR/venv/lib/python3.10/site-packages/torch/cuda/init.py”, line 310, in _lazy_init
raise AssertionError(“Torch not compiled with CUDA enabled”)
AssertionError: Torch not compiled with CUDA enabled
</code></pre>
<p>Could you explain where this call comes from and which part of your code tries to initialize CUDA, which is unsupported on Mac M3?</p>

username: djdoogle123
cooked: <p>from within nuke it is;</p>
<p>gc.collect()<br>
torch.cuda.empty_cache()</p>
<p>in train.py ;</p>
<p>Initialize torch.distributed and set the CUDA device.<br>
Expects environment variables to be set as per<br>
<a href="https://pytorch.org/docs/stable/distributed.html#environment-variable-initialization" class="inline-onebox" rel="noopener nofollow ugc">Distributed communication package - torch.distributed — PyTorch 2.5 documentation</a><br>
along with the environ variable “LOCAL_RANK” which is used to set the CUDA device.</p>
<p>device = “cuda” if cpu_group is None else “cpu”</p>
<p>orig_device = “cpu” if not tensor.is_cuda else “gpu”</p>
<p>def _setup_torch_dist_and_backend(self, cuda_conf, distributed_conf) → None:<br>
if torch.cuda.is_available():</p>

username: ptrblck
cooked: <p>Great, in this case you would need to disable the CUDA code from your script or the repository as mentioned in the linked issue.</p>