Torch npu venv setup cpu base

From Fudan-SMI-lab/SEAM · View interactive version

Torch npu venv setup cpu base is an AI agent skill from the GitHub repository Fudan-SMI-lab/SEAM. Set up torch-npu virtual environment with CPU-base torch and +cpu variant torchvision

How to use this skill

Skill file

Set up torch-npu virtual environment with CPU-base torch and +cpu variant torchvision

When to Use

Root Cause

The torch-npu package declares a strict dependency on the CPU-only build of PyTorch (e.g., torch==2.5.1+cpu). pip's resolver initially pulls the standard CUDA build of torch from generic PyPI mirrors, causing a version conflict. Additionally, vanilla torchvision expects CUDA-enabled torch and registers operators with the 'CUDA' dispatch key, which conflicts with the CPU-base torch's 'CPU' dispatch key.

How to Use

  1. 1. Probe the system-wide torch-npu version installed by CANN: python -c 'import torch_npu; print(torch_npu.version)'. Note the major.minor version (e.g., 2.5.1).
  2. 2. Create a virtual environment: python -m venv .venv && source .venv/bin/activate.
  3. 3. Install the matching CPU-base torch from PyTorch's official CPU wheel index: pip install torch==<version>+cpu --extra-index-url https://download.pytorch.org/whl/cpu --trusted-host download.pytorch.org.
  4. 4. Install the matching torch-npu from a domestic mirror: pip install torch-npu==<version> -i https://mirrors.aliyun.com/pypi/simple/.
  5. 5. Install the matching CPU-variant torchvision: pip install torchvision==<derived_version>+cpu --extra-index-url https://download.pytorch.org/whl/cpu --trusted-host download.pytorch.org. Derive torchvision version as: if torch is 2.5.x, torchvision is 0.20.x; if torch is 2.4.x, torchvision is 0.19.x.
  6. 6. Install pyyaml if not already present: pip install pyyaml. (torch_npu requires yaml at import time for its memory visualization module.)
  7. 7. Verify the stack: python -c 'import torch; import torch_npu; import torchvision; print(torch.version); print(torch_npu.version); print(torch.npu.is_available())'. Expected: torch shows '+cpu', torch_npu shows the probed version, npu.is_available() returns True.

Do Not

References

Evidence

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.