Skip to content

Commit 4320360

Browse files
committed
Add multi-platform torch example
This has been a useful starting point for me locally to repro pypi downloader issues we see in our project.
1 parent 077eec6 commit 4320360

8 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel-*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
load("@pip//:requirements.bzl", "requirement")
2+
load("@rules_python//python:defs.bzl", "py_test")
3+
load("@rules_uv//uv:pip.bzl", "pip_compile")
4+
5+
[
6+
pip_compile(
7+
name = "generate_{}_requirements".format(arch),
8+
args = [
9+
"--emit-index-url",
10+
"--index-strategy=unsafe-best-match", # NOTE: required because torch's index contains requests and that is preferred over pypi
11+
],
12+
python_platform = arch,
13+
requirements_in = "requirements.in",
14+
requirements_txt = "{}-requirements.txt".format(arch),
15+
)
16+
for arch in [
17+
"aarch64-unknown-linux-gnu",
18+
"x86_64-unknown-linux-gnu",
19+
"aarch64-apple-darwin",
20+
]
21+
]
22+
23+
py_test(
24+
name = "test",
25+
srcs = ["test_load.py"],
26+
main = "test_load.py",
27+
deps = [
28+
requirement("torch"),
29+
],
30+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module(
2+
name = "platform_specific_deps",
3+
version = "0.0.0",
4+
compatibility_level = 1,
5+
)
6+
7+
bazel_dep(name = "bazel_skylib", version = "1.7.1")
8+
bazel_dep(name = "rules_python", version = "0.0.0")
9+
# TODO: Replace with builtin uv support if it supports platform specific requirements output
10+
bazel_dep(name = "rules_uv", version = "0.42.0")
11+
12+
local_path_override(
13+
module_name = "rules_python",
14+
path = "../..",
15+
)
16+
17+
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
18+
python.toolchain(
19+
python_version = "3.11",
20+
)
21+
22+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
23+
pip.parse(
24+
hub_name = "pip",
25+
python_version = "3.11",
26+
requirements_by_platform = {
27+
"//:x86_64-unknown-linux-gnu-requirements.txt": "linux_x86_64",
28+
"//:aarch64-unknown-linux-gnu-requirements.txt": "linux_aarch64",
29+
},
30+
requirements_lock = "aarch64-apple-darwin-requirements.txt",
31+
)
32+
use_repo(pip, "pip")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file was autogenerated by uv via the following command:
2+
# bazel run @@//:generate_aarch64-apple-darwin_requirements
3+
--index-url https://pypi.org/simple
4+
--extra-index-url https://download.pytorch.org/whl/cpu
5+
6+
filelock==3.16.1
7+
# via torch
8+
fsspec==2024.10.0
9+
# via torch
10+
jinja2==3.1.4
11+
# via torch
12+
markupsafe==3.0.1
13+
# via jinja2
14+
mpmath==1.3.0
15+
# via sympy
16+
networkx==3.4.2
17+
# via torch
18+
sympy==1.13.3
19+
# via torch
20+
torch==2.4.1
21+
# via -r requirements.in
22+
typing-extensions==4.12.2
23+
# via torch
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file was autogenerated by uv via the following command:
2+
# bazel run @@//:generate_aarch64-unknown-linux-gnu_requirements
3+
--index-url https://pypi.org/simple
4+
--extra-index-url https://download.pytorch.org/whl/cpu
5+
6+
filelock==3.16.1
7+
# via torch
8+
fsspec==2024.10.0
9+
# via torch
10+
jinja2==3.1.4
11+
# via torch
12+
markupsafe==3.0.1
13+
# via jinja2
14+
mpmath==1.3.0
15+
# via sympy
16+
networkx==3.4.2
17+
# via torch
18+
sympy==1.13.3
19+
# via torch
20+
torch==2.4.1
21+
# via -r requirements.in
22+
typing-extensions==4.12.2
23+
# via torch
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--extra-index-url https://download.pytorch.org/whl/cpu
2+
torch==2.4.1; platform_machine != "x86_64"
3+
torch==2.4.1+cpu; platform_machine == "x86_64"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import torch # This verifies the deps were loaded as expected for the current platform
2+
3+
print("worked!")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file was autogenerated by uv via the following command:
2+
# bazel run @@//:generate_x86_64-unknown-linux-gnu_requirements
3+
--index-url https://pypi.org/simple
4+
--extra-index-url https://download.pytorch.org/whl/cpu
5+
6+
filelock==3.16.1
7+
# via torch
8+
fsspec==2024.10.0
9+
# via torch
10+
jinja2==3.1.4
11+
# via torch
12+
markupsafe==3.0.1
13+
# via jinja2
14+
mpmath==1.3.0
15+
# via sympy
16+
networkx==3.4.2
17+
# via torch
18+
sympy==1.13.3
19+
# via torch
20+
torch==2.4.1+cpu
21+
# via -r requirements.in
22+
typing-extensions==4.12.2
23+
# via torch

0 commit comments

Comments
 (0)