pixi icon indicating copy to clipboard operation
pixi copied to clipboard

Improve exprience for environments that mix multiple Conda channels and PyPI

Open Yura52 opened this issue 2 years ago • 18 comments

Problem description

(The post is written with pixi v0.24.2)

I was trying to reproduce my real setup that I use in practice with micromamba. I faced a number of issues, and I thought it could be useful if I share the story as a whole. Hopefully, this process will become simpler in future. Below, I provide the story.


In deep-learning-related research, it is very common to have the following list of dependencies:

  • python
  • pytorch::pytorch (note the channel)
  • <some package that depends on torch and is available only on PyPI>
    • For the sake of demonstration, let's use my package delu.
  • Other packages

I want to achieve the following:

  • Install pytorch from the pytorch channel.
  • Install delu from PyPI.
  • Install everything else from conda-forge.

Although I finally managed to achieve that, it turned out to be non-trivial. I performed the following steps:

Step 1: initialize the project, install the basic things:

pixi init pixi-torch
cd pixi-torch
pixi project channel add pytorch
pixi add python pytorch::pytorch

Step 2: add delu and get an unexpected warning:

pixi add delu --pypi            
 WARN These conda-packages will be overridden by pypi: 
	pytorch
...

Step 3: run pixi list and find out that pytorch is installed twice: once as pytorch::pytorch and once as torch from PyPI. While the above warning says that pypi::torch will be used, I was generally confused about what was going on and what was the state of my environment.

Step 4: learn about this issue: https://github.com/prefix-dev/pixi/issues/1230. Add conda-pypi-map = { pytorch = "local_mapping.json" } to the [project] table and the local_mapping.json file with a single line { "pytorch": "torch" }.

Step 5: run pixi install, then pip list and still see multiple packaged from PyPI, including torch. Then read this post and apply the recommended fix (though it seems that pixi update also solves the problem).

Step 6: realise that numpy is still coming from pypi. Run pixi add numpy.

Step 7: realise that delu was silently downgraded from 0.0.23 to the version 0.0.1 after the previous step. It turns out that pixi add fills the version bounds for conda packages, but not for python packages. Run pixi add "numpy>=1.26,<2" and pixi update (💡 idea: perhaps, pixi should warn users when it downgrades direct dependencies, i.e. the ones listed in the manifest file?)

Done.

Yura52 avatar Jun 23 '24 15:06 Yura52

Just with regards to step 3). Is there something we could change in the explanation to make it clearer for first time users to understand whats happening :)?

tdejager avatar Jun 24 '24 07:06 tdejager

I think step 7) is merged into main and will be available in the next release

tdejager avatar Jun 24 '24 07:06 tdejager

Just with regards to step 3). Is there something we could change in the explanation to make it clearer for first time users to understand whats happening :)?

I think the explanation is not a problem as such. To me, the problem was that I lost understanding of the state of my environment. I did not understand (and, I should admit, I still don't understand):

  • how it was possible to have pytorch::pytorch and pypi::torch to be installed at the same time
  • how I could actually go and check that indeed pypi::torch was used when I did import torch

Overall, I now realise that sometimes I implicitly expect that PyPI behaves just like another Conda channel. For example:

  • I would be suprised if one package was installed simultaneously from two conda channels (but this is what effectively happened at Steps 2-3)
  • For conda channels, if I install a package from a non-default channel (here, PyPI), I would expect that:
    • if a dependency is presented only in the default channel (conda-forge), take it from there.
    • if a dependecy is already explicitly provided in the manifest file with a channel (in my example, it is pytorch), follow the manifest file.
    • if a dependecy is presented in multiple channels, make user explicitly choose the channel (at least this is the current behavior as described here: https://github.com/prefix-dev/pixi/issues/1532#issuecomment-2184913566)

Yura52 avatar Jun 24 '24 09:06 Yura52

You are right that ideally we do not install the same pypi and conda package. However, the case being that because of different naming conda <-> pipy it can be hard to know which packages are equivalent. Pandoc being an example of having the same name but being a different package. There are also examples where the name of the package is x but the wheel in installing in namespace xy. A lot of intricacies.

That's why we create the parselmout mapping: https://github.com/prefix-dev/parselmouth. The problem being here that we do not have a mapping for the PyTorch channel (right @nichmor?), so we rely on a name to name mapping which fails.

The solution is that we should create a mapping for these channels as well. However, even when we have the mapping errors like these sometimes happen, that's why we have the clobber warning :)


W.r.t to your last point, specifically in the case of pypi, we now always prefer conda packages over pypi packages, like in your case its should've automatically picked the .conda version. We do this because wheels can vendor in whatever they like, while with conda the link to (system-level) packages like gcc or sdl2 is a lot more explicit.

tdejager avatar Jun 24 '24 11:06 tdejager

Thank you for the detailed reply. Could you clarify, when I have both pytorch::pytorch and pypi::torch installed, what can I do to understand who is the provider of the torch package in .pixi/envs/envname/lib/python3.11/site-packages (i.e. what version: pypi:: or pytorch:: of torch I use when I do import torch)?

Yura52 avatar Jun 24 '24 11:06 Yura52

So it isn't foolproof but you could check the INSTALLER file for the .dist-info of the package in the site-packages folder. It's conda when it is installed as a conda package and pixi-uv I think when it's pypi installed.

tdejager avatar Jun 25 '24 07:06 tdejager

Just in case, sharing my experience: for pypi::torch there is indeed the .dist-info thing and INSTALLER says uv-pixi. But for pytorch::pytorch there is .egg-info instead of .dist-info, seemingly without any information about the package provider:

$ ls -1 .pixi/envs/default/lib/python3.11/site-packages/torch-2.3.1-py3.11.egg-info
PKG-INFO
SOURCES.txt
dependency_links.txt
entry_points.txt
requires.txt
top_level.txt

Yura52 avatar Jun 25 '24 09:06 Yura52

You are right, checking out: https://anaconda.org/pytorch/pytorch/2.3.1/download/osx-arm64/pytorch-2.3.1-py3.11_0.tar.bz2 seems to be packaged as a source distribution. Only wheels contain a .dist-info

tdejager avatar Jun 25 '24 11:06 tdejager

I'm having very similar issues to those described here and #1230 . I'm currently working with segment-anything-2 and have the following pyproject.toml file

[build-system]
requires = ["setuptools>=61.0", "torch>=2.3.1"]
build-backend = "setuptools.build_meta"

[tool.pixi.project]
name = "segment-anything-2"
channels = [
    # "https://prefix.dev/ai-demos",
    "nvidia",
    "nvidia/label/cuda-12.1.0",
    "conda-forge",
    "pytorch",
]
platforms = ["linux-64"]

[tool.pixi.pypi-dependencies]
sam-2 = { path = ".", editable = true, extras = ["demo"] }

[tool.pixi.dependencies]
python = "3.11.*"
pytorch = { version = "2.3.1", channel = "pytorch" }
torchvision = { version = "0.18.1", channel = "pytorch" }
cuda = { version = "*", channel = "nvidia/label/cuda-12.1.0" }
pytorch-cuda = { version = "12.1.*", channel = "pytorch" }

[tool.pixi.tasks]

There's also a setup.py file that has

# Required dependencies
REQUIRED_PACKAGES = [
    "torch>=2.3.1",
    "torchvision>=0.18.1",
    "numpy>=1.24.4",
    "tqdm>=4.66.1",
    "hydra-core>=1.3.2",
    "iopath>=0.1.10",
    "pillow>=9.4.0",
]

EXTRA_PACKAGES = {
    "demo": ["matplotlib>=3.9.1", "jupyter>=1.0.0", "opencv-python>=4.7.0"],
    "dev": ["black==24.2.0", "usort==1.0.2", "ufmt==2.0.0b2"],
}

after running pixi install I get the following message

pablo@pablo-lambda-vector:~/0Dev/repos/segment-anything-2$ pixi install
 WARN These conda-packages will be overridden by pypi: 
        torchtriton, pytorch, torchvision
✔ The default environment has been installed.

and with pixi list I also get multiple installs from both pypi and conda. I'm trying to understand the solution.

So I need to create a custom mapping that includes all of packages https://github.com/prefix-dev/pixi/issues/1230#issuecomment-2136706130 ? and then for every package that has pytorch in their channel, include the pytorch channel arg?

conda-pypi-map = { "pytorch" = "local_mapping.json" }

[tasks]

[dependencies]
captum = {version="*", channel="pytorch"}
boltons = {version = "*"}

[pypi-dependencies]
captum = { version = "*"}

is this not already included in parselmouth, im still slightly confused as to exactly how things are getting mapped between the different names

pablovela5620 avatar Aug 13 '24 21:08 pablovela5620

TLDR

Currently there are some funny gimmicks related to CUDA, Tensorflow, Pytorch and conda/pypi mapping. Looking at Github issues, it seems like those packages are installed often with Pixi and users often fail to make them work. It's clear that there is some bugfixing required, but with busy schedule it might take Prefix Devs some time. So It would be helpful for everyone to at least share some reference envs that just work. I'd rather have some overcomplicated environment example in docs than to have to dig down in Github Issues or tests.

My attempt

Hi @pablovela5620 I successfully resolved my issue and got pytorch, torchvision, torchaudio and tensorflow to work both on CPU and GPU setup. without the issues you specified.

I have used parselmouth mapping for local_mapping.json starter as referenced by @nichmor and simply added just "torchaudio":"torchaudio" in the beginning, as everything else was successfully resolved in my case. You can already see "pytorch": "torch" in this mapping, so it maps pytorch package from conda to torch in pypi. Same for torchvision

In your case I see even regular packages fail, but maybe it's related to this issue https://github.com/prefix-dev/pixi/issues/1840, where solving pypi and conda might not work too well. Maybe downgrading to lower version could help. Or maybe this mapping will work for you. Or maybe you should add "torchtriton" to this mapping to make this work in the end.

My pixi.toml for reference

I can solve some complicated env as a referance for you and possibly someone else. I could cut it down to be minimal example for your case, but I'm now short on time, so sorry for that. Would be best if I helped with your specific case, but it's not possible now for me.

[project]
name = "speaker-recognition-research"
channels = ["conda-forge"]
platforms = ["linux-64"]
conda-pypi-map = {"pytorch" = "local_mapping.json" }

[dependencies]
python = "~=3.11.0"
pandas = "*"
pyyaml = "*"
tqdm = "*"
ffmpeg = "*"
ruff = "*"
pre-commit = "*"
srt = "*"
python-dotenv = "*"
numpy = "~=1.0"


[pypi-dependencies]
ffmpeg-python = {git= "https://github.com/charlienewey/ffmpeg-python.git", branch = "bugfix/782", rev="37dd98ca210fb8132575bedd719c6f8f9bfd1021"}
tensorflow ="~=2.15.0"
tensorflow-addons = "*"
tensorflow-io = {version="*", extras=["tensorflow"]}
tensorflow-datasets = "*"
keras-cv = "*"
pytest = "*"
pytest-cov = "*"
clearml = "*"


[feature.train]
system-requirements = {cuda = "12.2"}
channels = ["nvidia"]

[feature.train.tasks]
setup_precommit = "pre-commit install"
setup_jupyter = "python -m ipykernel install --user --name 'speaker-recognition-pixi'"
test_gpu = {cmd = ["python3", "-c", "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'));"], depends_on=["setup_precommit", "setup_jupyter"]}

[feature.train.dependencies]
cuda = "~=12.2.0"
ipykernel = "*"
matplotlib = "*"
pandas = "*"
umap-learn = "*"
bokeh = "*"
pre-commit = "*"
dask = "*"
ffmpeg = "*"

[feature.train.pypi-dependencies]
ray = {version = "*", extras=["data","train","tune","serve"]}
nvidia-cudnn-cu12 = "~=8.9.0"
audiomentations = "*"
joblib = "*"

[feature.speaker-recognition-demo.pypi-dependencies]
vtt_to_srt3 = "*"
yt_dlp = "*"
pyannote-core = "*"
pyannote-database = "*"
webvtt-py = "*"

[feature.speaker-recognition-demo.tasks]
speaker_recognition_demo = {cmd = ["python", "-m", "speaker_recognition_demo.demo"]}
regression_tests = {cmd = ["python", "-m", "pytest", "speaker_recognition_demo/tests/"]}

[feature.diarization-demo]
channels = ["conda-forge", "pytorch"]

[feature.diarization-demo.tasks]
diarization_demo = {cmd = ["python", "-m", "diarization_demo.demo"]}

[feature.diarization-demo.dependencies]
pytorch = {version="*", channel="pytorch"}
torchvision = {version="*", channel="pytorch"}
torchaudio = {version="*", channel="pytorch"}

[feature.diarization-demo.pypi-dependencies]
vtt_to_srt3 = "*"
yt_dlp = "*"
pyannote-audio = "*"
spectralcluster = "*"
speechbrain = "*"
webvtt-py = "*"

[feature.diarization-demo-cpu.dependencies]
cpuonly = {version="*", channel="pytorch"}

[feature.diarization-demo-gpu]
channels = ["conda-forge", "pytorch", "nvidia"]
system-requirements = {cuda = "12.4"}

[feature.diarization-demo-gpu.dependencies]
pytorch-cuda = {version="12.4", channel="pytorch"}


[environments]
train = ["train"]
speaker-recognition-demo = ["speaker-recognition-demo"]
diarization-demo-cpu = ["diarization-demo-cpu", "diarization-demo"]
diarization-demo-gpu = ["diarization-demo-gpu", "diarization-demo"]

You can try to reference diarization environments, as I define there conda dependencies on pytorch packages and also use pyannote.audio which uses pypi dependencies on those packages.

Please note Tensorflow won't work on GPU in this (diarization) env, as I only use it here for tflite inference. It will only work on train. I might try to make it work after my holidays. It was easier before, when pytorch didn't depend on it's own distributed conda cuda packages and I could use same cuda package sucessfully for both pytorch and tensorflow. Now it would install some of those packages twice if I remember correctly. Also would be nice to see someone installing pytorch and tensorflow in one pixi env with success for GPU :)

Also please note if system-requirements worked correctly, we wouldn't need extra env diarization-demo-cpu, default would be sufficient. There was a bug that should be fixed with next release, as described here. And yeah, passing branch for pypi git dependency won't work yet, it needs rev.

niemiaszek avatar Aug 20 '24 15:08 niemiaszek

This is helpful! Thank you @niemiaszek, I'll post an minimally working pytorch + opencv toml that I've gotten working for me to hopefully help others later!

pablovela5620 avatar Aug 26 '24 22:08 pablovela5620

I should say that with pixi==0.29.0 (or maybe after some prior releases) things got better, thanks! Does this mean that the core reason behind the issue is gone, and in such pytorch-related scenarios users don't need to know about conda-pypi-map anymore? Or I just got lucky somehow? :)

Also, there are some smaller issues remaining. My setup:

pixi init torch-demo
cd torch-demo
pixi project channel add pytorch
pixi add python pytorch::pytorch

The good news is that the following command (installing a simple library with torch and numpy as the only dependencies) does not bring pypi::torch anymore:

pixi add delu --pypi

However, it does bring pypi::numpy (according to pixi list). To fix that, I run:

pixi add numpy

Which leads to an error (which is rather specific to my example). Should I always explicitly specify version bounds when the most recent version cannot be installed?

Error message × failed to solve the pypi requirements of 'default' 'osx-arm64' ├─▶ failed to resolve pypi dependencies ╰─▶ Because delu==0.0.25 depends on numpy>=1.18,<2 and numpy==2.1.1, we can conclude that delu==0.0.25 cannot be used. And because only delu<=0.0.25 is available and you require delu>=0.0.25, we can conclude that your requirements are unsatisfiable.

Then I run:

pixi add "numpy<2"

And the environment is ready!

Yura52 avatar Sep 04 '24 21:09 Yura52

Which leads to an error (which is rather specific to my example). Should I always explicitly specify version bounds when the most recent version cannot be installed?

That is currently the case, yes. Ideally, we would solve conda and PyPI dependencies together, but that is not yet possible.

baszalmstra avatar Sep 05 '24 06:09 baszalmstra

hey, community!

starting with pixi 0.29 we improved a lot our mapping experience and we also added pytorch mapping, so now you can conda-pypi-map if you had it specifically for this situation.

You will also need to run pixi clean cache --mapping so it can pick up new hashes of mapping.

I'm closing this issue, but feel free to re-open it or create new other issues if you face other problems

nichtmacho avatar Sep 13 '24 08:09 nichtmacho

Things seem to be working much better! Incase anyone it helps anyone I made a small gist of an example pixi.toml that requires compiling some cuda using no-build isolation and pypi

https://gist.github.com/pablovela5620/2c505b5300f97a297497f068c664c482

this is working great for me with 0.29 and the updated mapping

pablovela5620 avatar Sep 15 '24 13:09 pablovela5620

Things seem to be working much better! Incase anyone it helps anyone I made a small gist of an example pixi.toml that requires compiling some cuda using no-build isolation and pypi

https://gist.github.com/pablovela5620/2c505b5300f97a297497f068c664c482

this is working great for me with 0.29 and the updated mapping

thanks a lot for your sharing @pablovela5620 ! I think it's a good candidate for one of the pixi examples! wdyt?

nichtmacho avatar Sep 15 '24 18:09 nichtmacho

Sure, though this particular pixi.toml is in context to the gsplat library https://github.com/nerfstudio-project/gsplat/tree/main/examples so this works in conjunction with the code laid out there. I don't know if that's self-contained enough to make for a great example but I'd be happy to contribute it if you think others would find it helpful

pablovela5620 avatar Sep 15 '24 18:09 pablovela5620

I'm a bit low on time, but if there's not an example yet with PyTorch I think that was a common one that was problematic when I was looking into different options (for projects like ComfyUI). So if that is sufficient for a more common and minimal example, it might be more suitable?

With PyTorch I recall the PyPi package wasn't suitable when you needed to get PyTorch for CPU or a specific CUDA release? But there was a resolver issue (at least for UV) due to the non-standard way PyTorch was managing the deps. IIRC on amd64 systems the PyPi packages defaulted to nvidia, while on arm64 they defaulted to CPU, but the PyTorch repo/registry where they published their own packages had a similar issue where the uv resolver struggled... something to do with the semver version needing to be exact due to a + identifier suffix, arm64 again had a difference there (+cpu for amd64 but none for arm64).

I think that is why I subscribed to this issue with interest in Pixi. Not sure when I'll have time to come back to that but the linked gist implies it'll be a much smoother experience 👍 (I was interested in preparing a multi-arch Docker image without the management issues I've seen in some projects with Python deps, especially in the PyTorch related projects)

Ideally Pixi will handle simplifying support for amd64 + arm64 with the CPU / CUDA variants of PyTorch. I think that's why I subscribed here as it seemed the Conda channel support would be better than PyPi issues with PyTorch, but I still wanted PyPi for other deps.

I think it's a common problem, other users subscribed here can probably chime in to clarify (or it may have already been mentioned above in the existing discussion 😅 )

Some references of projects that come to mind:


Looks like @pablovela5620 had a more minimal example earlier https://github.com/prefix-dev/pixi/issues/1537#issuecomment-2287154332

That could be adapted as an example?

I just recall various other alternatives to pixi that I had seen prior did not handle the PyTorch concern well, so anything that demonstrates that advantage with pixi is probably a win.

polarathene avatar Sep 15 '24 22:09 polarathene