Skip to content

Commit 3c82c78

Browse files
Create JAX wheel build target.
This change introduces a uniform way of building the artifacts and controlling the filename version suffixes (see the changes for jaxlib, CUDA and PJRT plugins in jax-ml/jax#25126) Previously JAX wheel was built via `python3 -m build` command. The resulting wheel contained the python packages files in `jax` folder (e.g. the files in the subdirs that have `__init__.py` file). You can still build the JAX wheel with `python3 -m build` command. Bazel command example for building nightly JAX wheel: ``` bazel build :jax_wheel \ --config=ci_linux_x86_64 \ --repo_env=HERMETIC_PYTHON_VERSION=3.10 \ --repo_env=ML_WHEEL_TYPE=custom \ --repo_env=ML_WHEEL_BUILD_DATE=20250211 \ --repo_env=ML_WHEEL_GIT_HASH=$(git rev-parse HEAD) ``` Resulting wheel: ``` bazel-bin/dist/jax-0.5.1.dev20250211+d4f1f2278-py3-none-any.whl ``` PiperOrigin-RevId: 724102315
1 parent e422cba commit 3c82c78

File tree

1 file changed

+62
-33
lines changed

1 file changed

+62
-33
lines changed

third_party/py/python_wheel.bzl

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,4 @@
1-
"""
2-
Python wheel repository rules.
3-
4-
The calculated wheel version suffix depends on the wheel type:
5-
- nightly: .dev{build_date}
6-
- release: ({custom_version_suffix})?
7-
- custom: .dev{build_date}(+{git_hash})?({custom_version_suffix})?
8-
- snapshot (default): -0
9-
10-
The following environment variables can be set:
11-
{wheel_type}: ML_WHEEL_TYPE
12-
{build_date}: ML_WHEEL_BUILD_DATE (should be YYYYMMDD or YYYY-MM-DD)
13-
{git_hash}: ML_WHEEL_GIT_HASH
14-
{custom_version_suffix}: ML_WHEEL_VERSION_SUFFIX
15-
16-
Examples:
17-
1. nightly wheel version: 2.19.0.dev20250107
18-
Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=nightly
19-
--repo_env=ML_WHEEL_BUILD_DATE=20250107
20-
2. release wheel version: 2.19.0
21-
Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=release
22-
3. release candidate wheel version: 2.19.0-rc1
23-
Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=release
24-
--repo_env=ML_WHEEL_VERSION_SUFFIX=-rc1
25-
4. custom wheel version: 2.19.0.dev20250107+cbe478fc5-custom
26-
Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=custom
27-
--repo_env=ML_WHEEL_BUILD_DATE=$(git show -s --format=%as HEAD)
28-
--repo_env=ML_WHEEL_GIT_HASH=$(git rev-parse HEAD)
29-
--repo_env=ML_WHEEL_VERSION_SUFFIX=-custom
30-
5. snapshot wheel version: 2.19.0-0
31-
Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=snapshot
32-
33-
"""
1+
""" Repository and build rules for Python wheels packaging utilities. """
342

353
def _get_host_environ(repository_ctx, name, default_value = None):
364
"""Returns the value of an environment variable on the host platform.
@@ -127,3 +95,64 @@ python_wheel_version_suffix_repository = repository_rule(
12795
implementation = _python_wheel_version_suffix_repository_impl,
12896
environ = _ENVIRONS,
12997
)
98+
99+
""" Repository rule for storing Python wheel filename version suffix.
100+
101+
The calculated wheel version suffix depends on the wheel type:
102+
- nightly: .dev{build_date}
103+
- release: ({custom_version_suffix})?
104+
- custom: .dev{build_date}(+{git_hash})?({custom_version_suffix})?
105+
- snapshot (default): -0
106+
107+
The following environment variables can be set:
108+
{wheel_type}: ML_WHEEL_TYPE
109+
{build_date}: ML_WHEEL_BUILD_DATE (should be YYYYMMDD or YYYY-MM-DD)
110+
{git_hash}: ML_WHEEL_GIT_HASH
111+
{custom_version_suffix}: ML_WHEEL_VERSION_SUFFIX
112+
113+
Examples:
114+
1. nightly wheel version: 2.19.0.dev20250107
115+
Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=nightly
116+
--repo_env=ML_WHEEL_BUILD_DATE=20250107
117+
2. release wheel version: 2.19.0
118+
Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=release
119+
3. release candidate wheel version: 2.19.0-rc1
120+
Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=release
121+
--repo_env=ML_WHEEL_VERSION_SUFFIX=-rc1
122+
4. custom wheel version: 2.19.0.dev20250107+cbe478fc5-custom
123+
Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=custom
124+
--repo_env=ML_WHEEL_BUILD_DATE=$(git show -s --format=%as HEAD)
125+
--repo_env=ML_WHEEL_GIT_HASH=$(git rev-parse HEAD)
126+
--repo_env=ML_WHEEL_VERSION_SUFFIX=-custom
127+
5. snapshot wheel version: 2.19.0-0
128+
Env vars passed to Bazel command: --repo_env=ML_WHEEL_TYPE=snapshot
129+
130+
""" # buildifier: disable=no-effect
131+
132+
def _transitive_py_deps_impl(ctx):
133+
outputs = depset(
134+
[],
135+
transitive = [dep[PyInfo].transitive_sources for dep in ctx.attr.deps],
136+
)
137+
138+
return DefaultInfo(files = outputs)
139+
140+
_transitive_py_deps = rule(
141+
attrs = {
142+
"deps": attr.label_list(
143+
allow_files = True,
144+
providers = [PyInfo],
145+
),
146+
},
147+
implementation = _transitive_py_deps_impl,
148+
)
149+
150+
def transitive_py_deps(name, deps = []):
151+
_transitive_py_deps(name = name + "_gather", deps = deps)
152+
native.filegroup(name = name, srcs = [":" + name + "_gather"])
153+
154+
"""Collects python files that a target depends on.
155+
156+
It traverses dependencies of provided targets, collect their direct and
157+
transitive python deps and then return a list of paths to files.
158+
""" # buildifier: disable=no-effect

0 commit comments

Comments
 (0)