brash-glass-61350
11/21/2023, 2:12 PMpython_source(
name="lib",
source="lib.py",
dependencies=[
...
"torch_cpu",
...
],
)
python_source(
name="lib_gpu",
source="lib.py",
dependencies=[
...
"torch_gpu",
...
],
)
When importing these libraries, we get a [WARN] Pants cannot infer owners for the following imports in the target $TARGET_NAME message, even though we clearly specify in the dependencies which one (ie, lib or lib_gpu) we are importing. Is there a way to fix this, other than suppressing the warning?gorgeous-winter-99296
11/21/2023, 4:19 PMtorch_gpu and torch_cpu?gorgeous-winter-99296
11/21/2023, 4:19 PMbrash-glass-61350
11/21/2023, 4:21 PMrequirements.txt file, to lock the packages used by all libraries. We also have a requirements_gpu.txt for the GPU packages. Everything runs absolutely fine, so pants is able to get the correct packages. But it still gives the warning.
Regarding you second question, no. We are locking all our packages directly in the requirements.txt files. What do we get by using resolves/lockfiles?gorgeous-winter-99296
11/21/2023, 4:24 PMgorgeous-winter-99296
11/21/2023, 4:29 PM"!torch_cpu" on the GPU target to exclude those "invalid" dependencies to prevent dependency inference from pulling them in at all. And vice versa for the CPU ones. Look at pants peek output to see what it pulls in for lib and lib_gpu, and adjust accordingly with ! for direct, and !! if you need a transitive exclude.
https://www.pantsbuild.org/docs/targets#dependencies-fieldbrash-glass-61350
11/21/2023, 4:32 PMsys_platform == "linux"), which is why we're doing it this waybrash-glass-61350
11/21/2023, 4:33 PM! and `!!`: running pants peek and pants dependencies, the dependencies show up correctly. For lib_gpu only torch_gpu shows up, and for lib only torch shows up. Everything works fine, but we get that damn warning šgorgeous-winter-99296
11/21/2023, 4:36 PMbase, which is torch==1.12,!=1.12+cpu,!=1.12+cu116
⢠gpu, which is torch==1.12+cu116
⢠cpu, which is torch==1.12+cpu
Yes, all those constraints are necessary as python-repos config is global unfortunately.brash-glass-61350
11/21/2023, 4:37 PMgorgeous-winter-99296
11/21/2023, 4:44 PM# pants: no-infer-dep on the import line. You can also set this globally with https://www.pantsbuild.org/docs/reference-python-infer#ignored_unowned_imports.gorgeous-winter-99296
11/21/2023, 4:45 PMbrash-glass-61350
11/30/2023, 11:30 AMlinux is the default resolve. I then added a resolve=parametrize("linux", "mac", "gpu") option to every library importing torch.
Everything works fine on linux (cpu). However, the other two resolves don't work: NoCompatibleResolveException: The target path/to/library:lib@resolve=gpu uses the 'resolve' 'gpu', but some of its dependencies are not compatible with that resolve
It looks like I need to add the resolve=parametrize(...) line to every BUILD rule. Is there an alternative?
Also, how do you select which version to run/test? Doing pants run path/to/stuff:name@resolve=cpu is very annoying. Plus we want to run all tests using the linux resolve for CI. While I might want to run all tests using the mac resolve on my laptop
Can you provide some more details on how you dealt with this?gorgeous-winter-99296
11/30/2023, 12:03 PM__defaults__ mechanism in a few select places in top-level BUILD-files:
__defaults__({
python_sources: dict(resolve=parametrize(...)),
python_tests: dict(resolve="cpu"),
})
This sets the resolve field for all "nested" targets from the view of that BUILD file. So pretty much every Python file that is used in our RL project has that resolve parametrization, but we only specify it in a few locations to apply recursively.
Regarding the target selection when running etc, we have a /cmd/ directory where we place entrypoint files to avoid specifying the full path. There we don't use python_sources, instead we use one `python_source`/`pex_binary` per file and set the resolve to whatever we want. We've played around with --python-default-resolve based workflows for those as well but it hasn't seen huge adoption so I'm unsure how well it works in practice.brash-glass-61350
11/30/2023, 2:08 PMpants run path/to/target:bin --resolve=cpu. And I also expected that if I didn't say anything it would default to the default resolve (i.e., pants run path/to/target:bin should use the default resolve, rather than forcing me to pick with the @resolve=name thing).
What is the --python-default-resolve flag?brash-glass-61350
11/30/2023, 5:55 PM