Skip to content

Commit 0c05bba

Browse files
authored
[Versioning] Better usage of pyproject.toml (#3089)
1 parent 3f10cb1 commit 0c05bba

File tree

12 files changed

+400
-318
lines changed

12 files changed

+400
-318
lines changed

.github/workflows/nightly_build.yml

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ jobs:
6161
export PATH="/opt/python/${{ matrix.python_version[1] }}/bin:$PATH"
6262
python3 -mpip install --pre torch --index-url https://download.pytorch.org/whl/nightly/${{ matrix.cuda_support[1] }}
6363
- name: Build TorchRL Nightly
64+
env:
65+
TORCHRL_NIGHTLY: 1
6466
run: |
6567
rm -r dist || true
6668
export PATH="/opt/python/${{ matrix.python_version[1] }}/bin:$PATH"
67-
python3 -mpip install wheel
68-
python3 setup.py bdist_wheel \
69-
--package_name torchrl-nightly \
70-
--python-tag=${{ matrix.python-tag }}
71-
find dist -name '*whl' -exec bash -c ' mv $0 ${0/linux/manylinux1}' {} \;
69+
python3 -mpip install build wheel
70+
./build_nightly.sh
71+
find dist -name '*whl' -exec bash -c ' mv $0 ${0/linux/manylinux1}' {} \;
7272
# pytorch/pytorch binaries are also manylinux_2_17 compliant but they
7373
# pretend that they're manylinux1 compliant so we do the same.
7474
- name: Upload wheel for the test-wheel job
@@ -112,14 +112,8 @@ jobs:
112112
python3 -mpip install --upgrade pip
113113
- name: Install tensordict
114114
run: |
115-
if [[ "$OSTYPE" == "darwin"* ]]; then
116-
brew install cmake
117-
else
118-
sudo apt-get update
119-
sudo apt-get install -y cmake
120-
fi
121-
python3 -mpip install "pybind11[global]"
122-
python3 -mpip install git+https://github.com/pytorch/tensordict.git
115+
export PATH="/opt/python/${{ matrix.python_version[1] }}/bin:$PATH"
116+
python3 -mpip install tensordict-nightly
123117
- name: Install test dependencies
124118
run: |
125119
export PATH="/opt/python/${{ matrix.python_version[1] }}/bin:$PATH"
@@ -141,6 +135,11 @@ jobs:
141135
rm -rf torchrl/
142136
export PATH="/opt/python/${{ matrix.python_version[1] }}/bin:$PATH"
143137
python3 -c "import torchrl; print(torchrl.__version__)"
138+
- name: Verify nightly version format
139+
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
140+
run: |
141+
export PATH="/opt/python/${{ matrix.python_version[1] }}/bin:$PATH"
142+
python3 scripts/verify_nightly_version.py
144143
- name: Run tests
145144
run: |
146145
set -e
@@ -213,7 +212,7 @@ jobs:
213212
]
214213
steps:
215214
- name: Setup Python
216-
uses: actions/setup-python@v2
215+
uses: actions/setup-python@v5
217216
with:
218217
python-version: ${{ matrix.python_version[1] }}
219218
- name: Checkout torchrl
@@ -223,13 +222,13 @@ jobs:
223222
run: |
224223
python3 -mpip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cpu -U
225224
- name: Build TorchRL nightly
225+
env:
226+
TORCHRL_NIGHTLY: 1
226227
shell: bash
227228
run: |
228229
rm -r dist || true
229-
python3 -mpip install wheel
230-
python3 setup.py bdist_wheel \
231-
--package_name torchrl-nightly \
232-
--python-tag=${{ matrix.python-tag }}
230+
python3 -mpip install build wheel
231+
./build_nightly.sh
233232
- name: Upload wheel for the test-wheel job
234233
uses: actions/upload-artifact@v4
235234
with:
@@ -273,15 +272,7 @@ jobs:
273272
- name: Install tensordict
274273
shell: bash
275274
run: |
276-
if ! choco -v &> /dev/null; then
277-
powershell -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
278-
fi
279-
# Install cmake using Chocolatey
280-
choco install cmake -y
281-
# Install necessary Python packages
282-
python -m pip install --upgrade pip
283-
python -m pip install "pybind11[global]"
284-
python3 -mpip install git+https://github.com/pytorch/tensordict.git
275+
python3 -mpip install tensordict-nightly
285276
- name: Download built wheels
286277
uses: actions/download-artifact@v4
287278
with:
@@ -297,6 +288,11 @@ jobs:
297288
# Avoid ambiguity of "import torchrl" by deleting the source files.
298289
rm -rf torchrl/
299290
python3 -c "import torchrl; print(torchrl.__version__)"
291+
- name: Verify nightly version format
292+
if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
293+
shell: bash
294+
run: |
295+
python3 scripts/verify_nightly_version.py
300296
- name: Run tests
301297
shell: bash
302298
run: |

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include torchrl/version.py

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,8 @@ such as `apt`, `apt-get`, `conda` or `yum` but NOT `pip`, as well as `pip instal
972972

973973
One can also build the wheels to distribute to co-workers using
974974
```bash
975-
python setup.py bdist_wheel
975+
pip install build
976+
python -m build --wheel
976977
```
977978
Your wheels will be stored there `./dist/torchrl<name>.whl` and installable via
978979
```bash

benchmarks/test_compressed_storage_benchmark.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99
import torch
1010

11+
1112
try:
1213
from safetensors.torch import save
1314
except ImportError:

build_nightly.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Check if we're in a nightly build
5+
if [ "$TORCHRL_NIGHTLY" != "1" ]; then
6+
echo "Not a nightly build, exiting"
7+
exit 0
8+
fi
9+
10+
echo "Starting nightly build process..."
11+
12+
# Create backups of original files
13+
cp pyproject.toml pyproject.toml.backup
14+
cp version.txt version.txt.backup
15+
16+
# Function to restore original files
17+
restore_files() {
18+
echo "Restoring original files..."
19+
mv pyproject.toml.backup pyproject.toml
20+
mv version.txt.backup version.txt
21+
}
22+
23+
# Set up trap to restore files on exit (success or failure)
24+
trap restore_files EXIT
25+
26+
# Modify pyproject.toml for nightly build
27+
echo "Modifying pyproject.toml for nightly build..."
28+
sed -i.bak 's/name = "torchrl"/name = "torchrl-nightly"/' pyproject.toml
29+
30+
# Replace tensordict dependency with tensordict-nightly
31+
echo "Replacing tensordict with tensordict-nightly..."
32+
sed -i.bak 's/"tensordict[^"]*"/"tensordict-nightly"/g' pyproject.toml
33+
34+
# Clean up sed backup files
35+
rm -f pyproject.toml.bak
36+
37+
# Set nightly version (YYYY.MM.DD format)
38+
echo "Setting nightly version..."
39+
echo "$(date +%Y.%m.%d)" > version.txt
40+
41+
# Build the package
42+
echo "Building nightly package..."
43+
echo "Python version: $(python --version)"
44+
echo "Platform: $(python -c 'import sys; print(sys.platform)')"
45+
46+
# Install build dependencies
47+
echo "Installing build dependencies..."
48+
# Use setuptools 65.3.0 for Python <= 3.10 (fixes compatibility issues)
49+
# Use latest setuptools for Python 3.11+
50+
#
51+
# Rationale:
52+
# - Python 3.9/3.10: setuptools 65.3.0 is more stable and fixes version detection issues
53+
# that cause "0.0.0+unknown" version strings in nightly builds
54+
# - Python 3.11+: Newer setuptools versions are required for proper functionality
55+
# with the latest Python features and build system changes
56+
PYTHON_VERSION=$(python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
57+
if python -c "import sys; exit(0 if sys.version_info < (3, 11) else 1)"; then
58+
echo "Using setuptools 65.3.0 for Python $PYTHON_VERSION (compatibility mode)"
59+
python -m pip install wheel setuptools==65.3.0
60+
else
61+
echo "Using latest setuptools for Python $PYTHON_VERSION (modern mode)"
62+
python -m pip install wheel setuptools
63+
fi
64+
65+
python setup.py bdist_wheel
66+
67+
echo "Nightly build completed successfully!"

packaging/build_wheels.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
77
export BUILD_TYPE=wheel
88
setup_env
99
setup_wheel_python
10-
pip_install numpy pyyaml future ninja "pybind11[global]"
10+
pip_install numpy pyyaml future ninja "pybind11[global]" build
1111
pip_install --upgrade setuptools
1212
setup_pip_pytorch_version
13-
python setup.py clean
1413

1514
# Copy binaries to be included in the wheel distribution
1615
if [[ "$(uname)" == Darwin || "$OSTYPE" == "msys" ]]; then
@@ -29,9 +28,9 @@ else
2928
fi
3029

3130
if [[ "$OSTYPE" == "msys" ]]; then
32-
IS_WHEEL=1 "$script_dir/windows/internal/vc_env_helper.bat" python setup.py bdist_wheel
31+
IS_WHEEL=1 "$script_dir/windows/internal/vc_env_helper.bat" python -m build --wheel
3332
else
34-
python setup.py bdist_wheel
33+
python -m build --wheel
3534
if [[ "$(uname)" != Darwin ]]; then
3635
rename "linux_x86_64" "manylinux1_x86_64" dist/*.whl
3736
fi

packaging/prepare_nightly_build.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
import logging
3+
import os
4+
import re
5+
from pathlib import Path
6+
7+
logger = logging.getLogger(__name__)
8+
9+
10+
def prepare_nightly_build():
11+
"""Prepare pyproject.toml for nightly builds by modifying dependencies."""
12+
is_nightly = os.getenv("TORCHRL_NIGHTLY") == "1"
13+
14+
if not is_nightly:
15+
logger.info("Not a nightly build, skipping pyproject.toml modification")
16+
return
17+
18+
pyproject_path = Path("pyproject.toml")
19+
if not pyproject_path.exists():
20+
logger.info("pyproject.toml not found")
21+
return
22+
23+
# Read the current pyproject.toml
24+
with open(pyproject_path) as f:
25+
content = f.read()
26+
27+
# Replace tensordict dependency with tensordict-nightly using regex
28+
# This pattern matches "tensordict" followed by any version constraints
29+
tensordict_pattern = r"tensordict[^,\]]*"
30+
if re.search(tensordict_pattern, content):
31+
content = re.sub(tensordict_pattern, "tensordict-nightly", content)
32+
logger.info("Replaced tensordict with tensordict-nightly in pyproject.toml")
33+
else:
34+
logger.info("tensordict dependency not found in pyproject.toml")
35+
36+
# Write the modified content back
37+
with open(pyproject_path, "w") as f:
38+
f.write(content)
39+
40+
logger.info("pyproject.toml prepared for nightly build")
41+
42+
43+
if __name__ == "__main__":
44+
prepare_nightly_build()

packaging/verify_nightly_version.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
import re
3+
import sys
4+
from datetime import date
5+
6+
# Try to import torchrl with better error handling
7+
try:
8+
import torchrl
9+
10+
print(f"Successfully imported torchrl from: {torchrl.__file__}")
11+
except ImportError as e:
12+
print(f"Failed to import torchrl: {e}")
13+
sys.exit(1)
14+
15+
# Check if __version__ attribute exists
16+
if not hasattr(torchrl, "__version__"):
17+
print(
18+
f'Available attributes in torchrl: {[attr for attr in dir(torchrl) if not attr.startswith("_")]}'
19+
)
20+
raise AttributeError("torchrl module has no __version__ attribute")
21+
22+
version = torchrl.__version__
23+
print(f"Checking version: {version}")
24+
25+
# Check that version is not the major version (0.9.0)
26+
if re.match(r"^\d+\.\d+\.\d+$", version):
27+
raise ValueError(f"Version should not be the major version: {version}")
28+
29+
# Check that version matches date format (YYYY.M.D)
30+
date_pattern = r"^\d{4}\.\d{1,2}\.\d{1,2}$"
31+
if not re.match(date_pattern, version):
32+
raise ValueError(f"Version should match date format YYYY.M.D, got: {version}")
33+
34+
# Verify it's today's date
35+
today = date.today()
36+
expected_version = f"{today.year}.{today.month}.{today.day}"
37+
if version != expected_version:
38+
raise ValueError(f"Version should be today date {expected_version}, got: {version}")
39+
40+
print(f"✓ Version {version} is correctly formatted as nightly date")
41+
42+
# Check that tensordict-nightly is installed (not stable tensordict)
43+
try:
44+
import tensordict
45+
46+
# Check if it's the nightly version by looking at the version
47+
tensordict_version = tensordict.__version__
48+
print(f"Checking tensordict version: {tensordict_version}")
49+
50+
# Check if it's a nightly version (either date format or contains 'd' followed by date)
51+
if re.match(date_pattern, tensordict_version) or "d2025" in tensordict_version:
52+
print(f"✓ tensordict version {tensordict_version} appears to be nightly")
53+
else:
54+
# Check if it's a stable version that should not be used in nightly builds
55+
if tensordict_version.startswith("0.9."):
56+
raise ValueError(
57+
f"tensordict should be nightly, not stable version: {tensordict_version}"
58+
)
59+
print(
60+
f"⚠ tensordict version {tensordict_version} - please verify this is nightly"
61+
)
62+
63+
except ImportError:
64+
raise ValueError(
65+
"tensordict is not installed - nightly builds should include tensordict-nightly"
66+
)

0 commit comments

Comments
 (0)