Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UV tries to build from source during package resolution #7299

Closed
jdumas opened this issue Sep 11, 2024 · 16 comments
Closed

UV tries to build from source during package resolution #7299

jdumas opened this issue Sep 11, 2024 · 16 comments
Assignees
Labels
question Asking for clarification or support

Comments

@jdumas
Copy link

jdumas commented Sep 11, 2024

Hi. I'm trying to install the setup the following project:

[project]
name = "myproject"
version = "0.1.0"
description = "My project"
requires-python = ">=3.9"
dependencies = [
    "numpy>=1.23",
    "colorama>=0.4.5",
    "cuda-python",
    "torch>=2.4.1,<2.5",
    "torchvision",
    "plyfile",
    "tqdm",
    "opencv-python",
    "imageio",
    "pycolmap",
]

[project.optional-dependencies]
build = ["setuptools"]
compile = [
    "diff-surfel-rasterization@git+https://github.com/hbb1/diff-surfel-rasterization.git@7bdbd51"
]

[tool.uv.pip]
extra-index-url = ["https://download.pytorch.org/whl/cu124"]

[tool.uv]
no-build-isolation-package = ["diff-surfel-rasterization"]

I've tried the various approaches documented in the build isolation page:

uv venv
uv pip install setuptools
uv sync --extra build --extra build

But they all failed to locate setuptools during resolve:

❯ uv sync --extra build --extra compile
 Updated https://github.com/hbb1/diff-surfel-rasterization.git (7bdbd51)
error: Failed to download and build: `diff-surfel-rasterization @ git+https://github.com/hbb1/diff-surfel-rasterization.git@7bdbd51`
  Caused by: Build backend failed to determine metadata through `prepare_metadata_for_build_wheel` with exit code: 1
--- stdout:

--- stderr:
Traceback (most recent call last):
  File "<string>", line 8, in <module>
ModuleNotFoundError: No module named 'setuptools'
---

Note that if I manually comment out the compile extra in the pyproject.toml to install all the dependencies I need, and then later on uncomment it, it then fails with a different error message (which is probably a separate issue):

❯ uv sync --extra build --extra compile
 Updated https://github.com/hbb1/diff-surfel-rasterization.git (7bdbd51)
error: Failed to download and build: `diff-surfel-rasterization @ git+https://github.com/hbb1/diff-surfel-rasterization.git@7bdbd51`
  Caused by: Build backend failed to determine metadata through `prepare_metadata_for_build_wheel` with exit code: 1
--- stdout:

--- stderr:
Traceback (most recent call last):
  File "<string>", line 14, in <module>
  File "C:\devel\myproject\.venv\Lib\site-packages\setuptools\build_meta.py", line 373, in prepare_metadata_for_build_wheel
    self.run_setup()
  File "C:\devel\myproject\.venv\Lib\site-packages\setuptools\build_meta.py", line 503, in run_setup
    super().run_setup(setup_script=setup_script)
  File "C:\devel\myproject\.venv\Lib\site-packages\setuptools\build_meta.py", line 318, in run_setup
    exec(code, locals())
  File "<string>", line 22, in <module>
  File "C:\devel\myproject\.venv\Lib\site-packages\torch\utils\cpp_extension.py", line 1076, in CUDAExtension
    library_dirs += library_paths(cuda=True)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\devel\myproject\.venv\Lib\site-packages\torch\utils\cpp_extension.py", line 1214, in library_paths
    paths.append(_join_cuda_home(lib_dir))
                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\devel\myproject\.venv\Lib\site-packages\torch\utils\cpp_extension.py", line 2416, in _join_cuda_home
    raise OSError('CUDA_HOME environment variable is not set. '
OSError: CUDA_HOME environment variable is not set. Please set it to your CUDA install root.
---

Would appreciate any guidance on the matter. Thanks!

  • Platform: Windows
  • UV version: 0.4.9
@charliermarsh
Copy link
Member

Interesting... On my machine, I get:

❯ uv sync --extra build --extra build
 Updated https://github.com/hbb1/diff-surfel-rasterization.git (7bdbd51)
error: Failed to download and build: `diff-surfel-rasterization @ git+https://github.com/hbb1/diff-surfel-rasterization.git@7bdbd51`
  Caused by: Build backend failed to determine metadata through `prepare_metadata_for_build_wheel` with exit status: 1
--- stdout:

--- stderr:
Traceback (most recent call last):
  File "<string>", line 14, in <module>
  File "/Users/crmarsh/workspace/puffin/bar/.venv/lib/python3.12/site-packages/setuptools/build_meta.py", line 366, in prepare_metadata_for_build_wheel
    self.run_setup()
  File "/Users/crmarsh/workspace/puffin/bar/.venv/lib/python3.12/site-packages/setuptools/build_meta.py", line 487, in run_setup
    super().run_setup(setup_script=setup_script)
  File "/Users/crmarsh/workspace/puffin/bar/.venv/lib/python3.12/site-packages/setuptools/build_meta.py", line 311, in run_setup
    exec(code, locals())
  File "<string>", line 13, in <module>
ModuleNotFoundError: No module named 'torch'

Which I assume is the expected result?

@jdumas
Copy link
Author

jdumas commented Sep 11, 2024

Hmm right, maybe I had the other packages installed in the venv and for me it was only missing setuptools which I've added later. But it's essentially the same problem (other packages should be installed first before trying to build the repo from source).

@charliermarsh
Copy link
Member

charliermarsh commented Sep 11, 2024

You would need to run uv sync before uv sync --extra compile, because you need torch to be installed prior to uv sync --extra compile.

@jdumas
Copy link
Author

jdumas commented Sep 11, 2024

uv sync without --extra compile still tries to build diff-surfel-rasterization from source at the resolve stage, and would fail due to missing torch or setuptool package.

@charliermarsh
Copy link
Member

It looks like diff-surfel-rasterization doesn't ship with any static metadata. So we have to build the package even just to resolve its dependencies. There's a chicken-and-egg problem -- in that case, you can't use uv sync and uv lock on their own. You have to pre-build the package with uv venv && uv pip install torch setuptools && uv pip install diff-surfel-rasterization (shorthand) so that either (1) diff-surfel-rasterization is already in the environment and/or (2) diff-surfel-rasterization is in the uv cache, so that it doesn't need to build it to resolve and install.

@charliermarsh charliermarsh added the question Asking for clarification or support label Sep 13, 2024
@charliermarsh charliermarsh self-assigned this Sep 13, 2024
@jdumas
Copy link
Author

jdumas commented Sep 13, 2024

I see, thanks. Is this because the diff-surfel-rasterization doesn't have a pyproject.toml, and is using a straight up setup.py that you need to run before being able to resolve anything? Is there any way to workaround this? I can't expect to fix all the imperfectly packaged repo around, although maybe that would be the easiest solution in this case.

Finally, even if I'm able to resolve it correctly, I still have the issue of the unset CUDA_HOME variable. Let me know if I should create a separate issue for it.

@charliermarsh
Copy link
Member

Yeah, that's correct. We have to run setup.py to get the dependencies for the package. If it had a pyproject.toml that included a static dependencies field under [project], it wouldn't be necessary.

@jdumas
Copy link
Author

jdumas commented Sep 13, 2024

Thanks. After adding a simple pyproject.toml, I am now stuck due to the missing CUDA_HOME env variable:

[project]
name = "myproject"
version = "0.1.0"
description = "My project"
requires-python = ">=3.10"
dependencies = [
    "numpy>=1.23",
    "colorama>=0.4.5",
    "cuda-python",
    "torch>=2.4.1,<2.5",
    "torchvision",
    "plyfile",
    "tqdm",
    "opencv-python",
    "imageio",
    "pycolmap",
    "diff-surfel-rasterization@git+https://github.com/jdumas/diff-surfel-rasterization.git@155efa9",
]

[tool.uv.pip]
extra-index-url = ["https://download.pytorch.org/whl/cu124"]

Yields:

❯ uv sync
 Updated https://github.com/jdumas/diff-surfel-rasterization.git (155efa9)
Resolved 34 packages in 299ms
error: Failed to prepare distributions
  Caused by: Failed to fetch wheel: diff-surfel-rasterization @ git+https://github.com/jdumas/diff-surfel-rasterization.git@155efa9590ac73ff5535ca84eb08df1b00934236
  Caused by: Build backend failed to determine extra requires with `build_wheel()` with exit code: 1
--- stdout:

--- stderr:
C:\Users\jedumas\AppData\Local\uv\cache\builds-v0\.tmpbVUHKB\Lib\site-packages\torch\_subclasses\functional_tensor.py:258: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\utils\tensor_numpy.cpp:84.)
  cpu = _conversion_method_template(device=torch.device("cpu"))
Traceback (most recent call last):
  File "<string>", line 14, in <module>
  File "C:\Users\jedumas\AppData\Local\uv\cache\builds-v0\.tmpbVUHKB\Lib\site-packages\setuptools\build_meta.py", line 332, in get_requires_for_build_wheel
    return self._get_build_requires(config_settings, requirements=[])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jedumas\AppData\Local\uv\cache\builds-v0\.tmpbVUHKB\Lib\site-packages\setuptools\build_meta.py", line 302, in _get_build_requires
    self.run_setup()
  File "C:\Users\jedumas\AppData\Local\uv\cache\builds-v0\.tmpbVUHKB\Lib\site-packages\setuptools\build_meta.py", line 318, in run_setup
    exec(code, locals())
  File "<string>", line 22, in <module>
  File "C:\Users\jedumas\AppData\Local\uv\cache\builds-v0\.tmpbVUHKB\Lib\site-packages\torch\utils\cpp_extension.py", line 1076, in CUDAExtension
    library_dirs += library_paths(cuda=True)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jedumas\AppData\Local\uv\cache\builds-v0\.tmpbVUHKB\Lib\site-packages\torch\utils\cpp_extension.py", line 1214, in library_paths
    paths.append(_join_cuda_home(lib_dir))
                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jedumas\AppData\Local\uv\cache\builds-v0\.tmpbVUHKB\Lib\site-packages\torch\utils\cpp_extension.py", line 2416, in _join_cuda_home
    raise OSError('CUDA_HOME environment variable is not set. '
OSError: CUDA_HOME environment variable is not set. Please set it to your CUDA install root.
---

@charliermarsh
Copy link
Member

Dumb question, but is CUDA_HOME set?

@jdumas
Copy link
Author

jdumas commented Sep 13, 2024

It's not. But who should set it? I've added the cuda-python pip package as a dependency to the pyproject.toml. Is there a way to have UV specify environment variables for building packages?

@charliermarsh
Copy link
Member

I think you're responsible for setting it, outside of uv. I'm not familiar with building that specific package, but it's not uncommon to need to provide some configuration if you're building packages from source.

@jdumas
Copy link
Author

jdumas commented Sep 14, 2024

Ok but the problem is that CUDA_HOME should point to where cuda-python gets installed by uv. How can I know that in advance? Building Cuda kernel is kinda ubiquitous in ML Python code. However I have very little experience with it and I'm struggling to find build instructions that work.

EDIT: If I don't try to build the package (i.e. just install the deps in the pyproject.toml), and then run:

uv run python -c "import torch; print(torch.cuda.is_available())"

It replies False. So at this point I'm not quite sure what's wrong with my setup.

EDIT2: After further investigation it looks like I had the name of a table wrong in my pyproject.toml. If I replace [tool.uv.pip] with [tool.uv] in the pyproject.toml, it does reply True. So Cuda is properly setup via pip, but I still don't know who is supposed to set CUDA_HOME and how, etc. Very frustrating :(

Conda has some way to define env vars in their .yml. Is there something similar supported by UV or pyproject.toml?

@matejpekar
Copy link

I am running into a similar problem when trying to install detectron2:

[project]
name = "test"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["torch>=2.3.1", "detectron2>=0.6"]

[tool.uv]
no-build-isolation-package = ["detectron2"]

[tool.uv.sources]
detectron2 = { git = "https://github.com/facebookresearch/detectron2.git" }

When running uv lock with a clean cache (uv cache clean) I get this error:

Using CPython 3.12.6 interpreter at: /opt/homebrew/opt/python@3.12/bin/python3.12
 Updated https://github.com/facebookresearch/detectron2.git (ebe8b45)
error: Failed to download and build: `detectron2 @ git+https://github.com/facebookresearch/detectron2.git`
  Caused by: Build backend failed to determine metadata through `prepare_metadata_for_build_wheel` with exit status: 1
--- stdout:

--- stderr:
Traceback (most recent call last):
  File "<string>", line 8, in <module>
ModuleNotFoundError: No module named 'setuptools'
---

The workaround is to add detectron2 after installing all other dependencies, however this is very inconvenient when working on multiple devices.

@charliermarsh
Copy link
Member

I'm going to close this for now because I think the CUDA_HOME piece isn't specific to uv.

@jdumas
Copy link
Author

jdumas commented Oct 11, 2024

What's a good platform to discuss this issue then? While the CUDA_HOME issue might be specific to PyTorch, I feel that being able to define environment variables, and being able to have some form of introspection (i.e. refer to the active uv environment) is a useful feature to have.

charliermarsh added a commit that referenced this issue Oct 23, 2024
## Summary

This is part of making
#7299 (comment)
better. You can now use `tool.uv.dependency-metadata` for direct URL
requirements. Unfortunately, you _must_ include a version, since we need
one to perform resolution.
@kabouzeid
Copy link
Contributor

I think you're responsible for setting it, outside of uv. I'm not familiar with building that specific package, but it's not uncommon to need to provide some configuration if you're building packages from source.

This is correct. On Linux, you should download CUDA runfiles from NVIDIA’s official site (they have downloads for all versions), or use the module system of your compute cluster. For Windows, consider reading a tutorial on best practices for installation. Either way, you need to set up the CUDA environment yourself, not uv.

If you want a quick fix, you can set up a Conda environment just for CUDA and run uv in it. But honestly, I wouldn’t recommend it—because, well, Conda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for clarification or support
Projects
None yet
Development

No branches or pull requests

4 participants