Skip to content

Add pyproject.toml so isolated builds can import setup.py - #41

Open
u7-u7 wants to merge 1 commit into
MoonshotAI:masterfrom
u7-u7:fix/add-pyproject-build-system
Open

u7-u7 wants to merge 1 commit into
MoonshotAI:masterfrom
u7-u7:fix/add-pyproject-build-system

Conversation

@u7-u7

@u7-u7 u7-u7 commented Oct 26, 2026

Copy link
Copy Markdown

Refs #5.

Problem

With no pyproject.toml, a PEP 517 frontend cannot read this project's metadata at all in a clean environment — the failure happens before anything CUDA-related is even reached:

$ python -m pip install --no-deps --dry-run .        # pristine master checkout, clean venv
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'error'
        File "<string>", line 18, in <module>
      ModuleNotFoundError: No module named 'torch'
      [end of output]
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

Root cause

setup.py:18 imports torch.utils.cpp_extension at module scope to describe the CUDA extension, so torch must be present in the build environment, not only in the runtime environment. Without pyproject.toml pip falls back to the PEP 517 defaults: a build-isolated environment containing only setuptools + wheel. The backend therefore cannot import setup.py even to generate metadata. The only workaround today is --no-build-isolation, which nothing in the repository tells a user to pass (README's Build & Test section just says pip install -e .).

Fix

One new file, no existing file touched:

[build-system]
requires = ["setuptools>=64", "wheel", "torch"]
build-backend = "setuptools.build_meta"

setuptools>=64 is what provides the PEP 660 editable-install path; torch is the import setup.py actually needs (wheel is kept alongside for older builders).

Deliberately not included:

  • No [project] table. pyproject.toml metadata takes precedence over the equivalent setup() arguments, so duplicating name/version/install_requires here would create two sources of truth (a version bump would have to be made twice). They stay in setup.py.
  • No dependency changes. The nvidia-cutlass-dsl==4.6.2 pin and the absence of a runtime torch pin are untouched, and the build requirement carries no invented version floor either.

Verification

Machine: macOS arm64, no NVIDIA GPU and no nvcc, so the compile step itself cannot run here. The verification is therefore split in two: the real repo before/after, plus a packaging-only experiment that removes CUDA from the equation entirely.

Environment for every command below: python -m venv on CPython 3.12.12, pip 25.0.1, fresh venv without torch.

1. Real repo, before (pristine master checkout)

$ python -m pip install --no-deps --dry-run .
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: finished with status 'error'
        File "<string>", line 18, in <module>
      ModuleNotFoundError: No module named 'torch'

2. Real repo, after (this branch)

$ python -m pip install --no-deps --dry-run .
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
        File "<string>", line 51, in <module>
        File ".../torch/utils/cpp_extension.py", line 1573, in CUDAExtension
          library_dirs += library_paths(device_type="cuda")
      OSError: CUDA_HOME environment variable is not set. Please set it to your CUDA install root.

torch is now resolved into the isolated build environment, import torch succeeds, and the only remaining failure is the pre-existing CUDA toolchain prerequisite (CUDA_HOME/nvcc), which is a property of this GPU-less machine, not of the packaging.

3. Packaging-only experiment (isolates packaging from the CUDA toolchain)

To show the pyproject.toml is sufficient rather than a partial improvement, I copied the repo into two throwaway directories (/tmp, no repository file modified), stubbed ext_modules to [] in both copies so nothing CUDA is ever constructed, and ran the same command on each — one copy without pyproject.toml, one with this branch's file:

BEFORE (setup.py only, ext_modules stubbed):
  Getting requirements to build wheel: finished with status 'error'
      ModuleNotFoundError: No module named 'torch'

AFTER  (same setup.py + pyproject.toml, ext_modules stubbed):
  Getting requirements to build wheel: finished with status 'done'
Would install moonep-0.0.1

Same setup.py, same interpreter, same network: the single added file is what turns a hard metadata failure into a working build/environment resolution.

4. Existing test suite

tests/ requires multiple GPUs with NVLink (see README, Build & Test), so none of it can run on this machine. This patch adds a packaging file and touches no Python or kernel code path, so those results are unaffected — but I did not run them and am not claiming to have.

Risk / compatibility

  • Additive: 1 new file, 11 lines, nothing existing modified.
  • On a CUDA machine pip install . / pip install -e . now work under build isolation by default (previously they could not). A contributor who would rather build the extension against the torch already installed in their environment — e.g. to match the runtime ABI exactly — can keep doing that with pip install --no-build-isolation -e .; that path is unchanged.
  • No change to the compiled extension, the kernels, the runtime requirements, or the version.

AI assistance: this patch was prepared with AI assistance; I reproduced the bug and verified the fix with the commands above.

setup.py imports torch.utils.cpp_extension at module scope, so torch has to be in the build environment too. Without a pyproject.toml pip falls back to the PEP 517 defaults and isolates the build with only setuptools + wheel, so 'getting requirements to build wheel' dies with ModuleNotFoundError: No module named 'torch'.

Declare setuptools>=64, wheel and torch as build requirements and use the setuptools.build_meta backend. Package metadata stays in setup.py as the single source of truth so no version or dependency pin changes.

Signed-off-by: u7-u7 <116776533+u7-u7@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant