Skip to content

Commit 140a828

Browse files
authored
[Docs] Enable doc builds and tutorial runs and see what happens (#3335)
1 parent d79e651 commit 140a828

80 files changed

Lines changed: 764 additions & 384 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/docs.yml

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,18 @@ jobs:
3434
repository: pytorch/rl
3535
upload-artifact: docs
3636
timeout: 120
37+
# Use PyTorch image with newer GCC for GLIBCXX_3.4.30 support (needed by AOTInductor in export.py)
38+
docker-image: pytorch/pytorch:2.5.1-cuda12.4-cudnn9-devel
3739
script: |
3840
set -e
3941
set -v
40-
yum makecache
41-
# Install Mesa and OpenGL Libraries:
42-
yum install -y glfw mesa-libGL mesa-libGL-devel egl-utils freeglut mesa-libGLU mesa-libEGL python39-pip
43-
yum install epel-release -y # for missing librhash0 when installing tensordict
44-
yum install rhash -y # Provides librhash.so.0 needed by cmake
45-
# Install DRI Drivers:
46-
yum install -y mesa-dri-drivers
47-
# Install Xvfb for Headless Environments:
48-
yum install -y xorg-x11-server-Xvfb
49-
# xhost +local:docker
50-
# Xvfb :1 -screen 0 1024x768x24 &
51-
52-
root_dir="$(pwd)"
53-
conda_dir="${root_dir}/conda"
54-
env_dir="${root_dir}/env"
55-
os=Linux
5642
57-
# 1. Install conda at ./conda
58-
printf "* Installing conda\n"
59-
wget -O miniconda.sh "http://repo.continuum.io/miniconda/Miniconda3-latest-${os}-x86_64.sh"
60-
bash ./miniconda.sh -b -f -p "${conda_dir}"
61-
eval "$(${conda_dir}/bin/conda shell.bash hook)"
62-
printf "* Creating a test environment\n"
63-
conda create --prefix "${env_dir}" -y python=3.12
64-
printf "* Activating\n"
65-
conda activate "${env_dir}"
66-
43+
# Install system dependencies (Ubuntu-based PyTorch image)
44+
apt-get update
45+
apt-get install -y libglfw3 libglfw3-dev libgl1-mesa-glx libgl1-mesa-dev libegl1-mesa-dev \
46+
freeglut3-dev libglu1-mesa libegl1 mesa-utils xvfb wget git cmake
47+
6748
# 2. upgrade pip, ninja and packaging
68-
conda install anaconda::cmake -y
6949
python -m pip install --upgrade pip
7050
python -m pip install setuptools ninja packaging "pybind11[global]" -U
7151
@@ -76,19 +56,26 @@ jobs:
7656
git version
7757
7858
# 5. Install PyTorch
59+
# Uninstall pre-installed packages from Docker image to avoid conflicts
60+
# (gym conflicts with gymnasium, torch/torchaudio versions conflict with nightly)
61+
python -m pip uninstall -y torch torchvision torchaudio gym || true
62+
7963
if [[ ${{ github.event_name }} == push && (${{ github.ref_type }} == tag || (${{ github.ref_type }} == branch && ${{ github.ref_name }} == release/*)) ]]; then
80-
python -m pip install torch torchvision
81-
python -m pip install tensordict
64+
# Use stable PyTorch for releases (tags and release/* branches)
65+
python -m pip install torch torchvision --quiet --root-user-action=ignore
66+
python -m pip install tensordict --quiet --root-user-action=ignore
8267
else
68+
# Use nightly PyTorch for main, nightly, and PRs
8369
python -m pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cpu -U --quiet --root-user-action=ignore
8470
python -m pip install git+https://github.com/pytorch/tensordict.git --quiet --root-user-action=ignore
8571
fi
8672
73+
# 6. Install doc requirements BEFORE TorchRL to ensure gymnasium is available
74+
# (TorchRL's gym backend detection happens at import time)
75+
python -m pip install -r docs/requirements.txt --quiet --root-user-action=ignore
76+
8777
# 7. Install TorchRL
8878
python -m pip install -e . --no-build-isolation
89-
90-
# 8. Install requirements
91-
python -m pip install -r docs/requirements.txt --quiet --root-user-action=ignore
9279
9380
# 9. Set sanitize version
9481
if [[ ${{ github.event_name }} == push && (${{ github.ref_type }} == tag || (${{ github.ref_type }} == branch && ${{ github.ref_name }} == release/*)) ]]; then

docs/build_local.sh

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
#!/bin/bash
2+
# Build TorchRL documentation locally using uv with a temporary virtual environment.
3+
#
4+
# Usage:
5+
# ./build_local.sh # Build docs (runs tutorials)
6+
# ./build_local.sh --no-run # Build docs without running tutorials (faster)
7+
#
8+
# The script creates a temporary virtual environment, installs dependencies,
9+
# builds the documentation, and cleans up on failure.
10+
11+
set -e
12+
13+
# Configuration
14+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
15+
ROOT_DIR="$(dirname "$SCRIPT_DIR")"
16+
VENV_DIR="${ROOT_DIR}/.doc-venv"
17+
BUILD_DIR="${SCRIPT_DIR}/_local_build"
18+
PYTHON_VERSION="3.12"
19+
20+
# Parse arguments
21+
RUN_TUTORIALS=true
22+
for arg in "$@"; do
23+
case $arg in
24+
--no-run)
25+
RUN_TUTORIALS=false
26+
shift
27+
;;
28+
--help|-h)
29+
echo "Usage: $0 [--no-run]"
30+
echo ""
31+
echo "Options:"
32+
echo " --no-run Skip running tutorials (faster build)"
33+
echo " --help, -h Show this help message"
34+
exit 0
35+
;;
36+
esac
37+
done
38+
39+
# Cleanup function - always runs on exit
40+
cleanup() {
41+
local exit_code=$?
42+
43+
# Restore conf.py if we modified it (do this regardless of success/failure)
44+
if [ -f "$SCRIPT_DIR/source/conf.py.bak" ]; then
45+
mv "$SCRIPT_DIR/source/conf.py.bak" "$SCRIPT_DIR/source/conf.py"
46+
echo "Restored conf.py from backup"
47+
fi
48+
49+
if [ $exit_code -ne 0 ]; then
50+
echo ""
51+
echo "============================================"
52+
echo "Build failed with exit code $exit_code"
53+
echo "Cleaning up virtual environment..."
54+
echo "============================================"
55+
rm -rf "$VENV_DIR"
56+
echo "Virtual environment removed: $VENV_DIR"
57+
fi
58+
exit $exit_code
59+
}
60+
trap cleanup EXIT INT TERM
61+
62+
echo "============================================"
63+
echo "TorchRL Documentation Build Script"
64+
echo "============================================"
65+
echo ""
66+
echo "Root directory: $ROOT_DIR"
67+
echo "Virtual env: $VENV_DIR"
68+
echo "Build output: $BUILD_DIR"
69+
echo "Run tutorials: $RUN_TUTORIALS"
70+
echo ""
71+
72+
# Check for uv
73+
if ! command -v uv &> /dev/null; then
74+
echo "Error: 'uv' is not installed."
75+
echo "Install it with: curl -LsSf https://astral.sh/uv/install.sh | sh"
76+
exit 1
77+
fi
78+
79+
# Remove existing venv if it exists
80+
if [ -d "$VENV_DIR" ]; then
81+
echo "Removing existing virtual environment..."
82+
rm -rf "$VENV_DIR"
83+
fi
84+
85+
# Create virtual environment
86+
echo "Creating virtual environment with Python $PYTHON_VERSION..."
87+
uv venv "$VENV_DIR" --python "$PYTHON_VERSION"
88+
89+
# Activate virtual environment
90+
echo "Activating virtual environment..."
91+
source "$VENV_DIR/bin/activate"
92+
93+
# Upgrade pip and install build tools
94+
echo "Installing build tools..."
95+
uv pip install --upgrade pip setuptools ninja packaging "pybind11[global]" cmake
96+
97+
# Install PyTorch (nightly for latest features)
98+
echo "Installing PyTorch..."
99+
uv pip install --pre torch torchvision --index-url https://download.pytorch.org/whl/nightly/cpu
100+
101+
# Install tensordict from git
102+
echo "Installing tensordict..."
103+
uv pip install git+https://github.com/pytorch/tensordict.git
104+
105+
# Install torchrl in editable mode
106+
echo "Installing torchrl..."
107+
cd "$ROOT_DIR"
108+
uv pip install -e . --no-build-isolation
109+
110+
# Install documentation requirements
111+
echo "Installing documentation requirements..."
112+
# First install pytorch_sphinx_theme separately (editable git not supported by uv -r)
113+
uv pip install git+https://github.com/pytorch/pytorch_sphinx_theme.git
114+
# Install remaining requirements (skip the editable line)
115+
grep -v "pytorch_sphinx_theme" "$SCRIPT_DIR/requirements.txt" | uv pip install -r -
116+
117+
# Verify installation
118+
echo "Verifying torchrl installation..."
119+
python -c "import torchrl; print(f'TorchRL version: {torchrl.__version__}')"
120+
121+
# Set up environment for building
122+
export MAX_IDLE_COUNT=180
123+
export BATCHED_PIPE_TIMEOUT=180
124+
export TORCHRL_CONSOLE_STREAM=stdout
125+
126+
# Clear old MuJoCo environment variables that might interfere with MuJoCo 3.x
127+
unset MUJOCO_PATH
128+
unset MUJOCO_PY_MUJOCO_PATH
129+
unset LD_LIBRARY_PATH # Reset to avoid old MuJoCo libs
130+
131+
# Set plot_gallery based on mode
132+
# Create backup of conf.py for cleanup to restore
133+
cp "$SCRIPT_DIR/source/conf.py" "$SCRIPT_DIR/source/conf.py.bak"
134+
135+
if [ "$RUN_TUTORIALS" = true ]; then
136+
echo ""
137+
echo "Note: Tutorials WILL be executed"
138+
# Enable plot_gallery (replace both True and "False" variants)
139+
sed -i '' 's/"plot_gallery": "False"/"plot_gallery": True/' "$SCRIPT_DIR/source/conf.py"
140+
sed -i '' 's/"plot_gallery": False/"plot_gallery": True/' "$SCRIPT_DIR/source/conf.py"
141+
else
142+
echo ""
143+
echo "Note: Tutorials will NOT be executed (--no-run mode)"
144+
# Disable plot_gallery
145+
sed -i '' 's/"plot_gallery": True/"plot_gallery": "False"/' "$SCRIPT_DIR/source/conf.py"
146+
fi
147+
148+
# Clean up stale generated files
149+
echo "Cleaning up stale generated tutorials..."
150+
rm -rf "$SCRIPT_DIR/source/reference/generated/tutorials" | true
151+
152+
# Build documentation
153+
echo ""
154+
echo "============================================"
155+
echo "Building documentation..."
156+
echo "============================================"
157+
cd "$SCRIPT_DIR"
158+
159+
# Suppress multiprocessing resource tracker warnings (they're harmless but noisy)
160+
# These occur when child processes are killed before cleaning up shared memory
161+
export PYTHONWARNINGS="ignore::UserWarning"
162+
163+
# Ignore SIGPIPE to prevent crashes from broken pipes during multiprocessing cleanup
164+
# This is a common issue on macOS when child processes are terminated
165+
trap '' PIPE 2>/dev/null || true
166+
167+
# Use -j 1 to avoid conflicts with tutorials that use multiprocessing
168+
sphinx-build ./source "$BUILD_DIR" -v -j 1
169+
BUILD_EXIT_CODE=$?
170+
171+
# On macOS, multiprocessing cleanup can cause SIGABRT (exit code 134) even when
172+
# the build succeeded. Check if HTML output exists to determine true success.
173+
if [ $BUILD_EXIT_CODE -eq 134 ] && [ -f "$BUILD_DIR/index.html" ]; then
174+
echo ""
175+
echo "============================================"
176+
echo "Note: Build process received SIGABRT during cleanup (exit 134)"
177+
echo "This is a known macOS multiprocessing issue and is harmless."
178+
echo "Documentation was generated successfully."
179+
echo "============================================"
180+
elif [ $BUILD_EXIT_CODE -ne 0 ]; then
181+
echo ""
182+
echo "============================================"
183+
echo "Build failed with exit code $BUILD_EXIT_CODE"
184+
echo "============================================"
185+
exit $BUILD_EXIT_CODE
186+
fi
187+
188+
echo ""
189+
echo "============================================"
190+
echo "Documentation built successfully!"
191+
echo "============================================"
192+
echo ""
193+
echo "Output: $BUILD_DIR"
194+
echo ""
195+
echo "To view the documentation, run:"
196+
echo " python -m http.server 8000 -d $BUILD_DIR"
197+
echo " # Then open http://localhost:8000 in your browser"
198+
echo ""
199+
echo "To clean up the virtual environment, run:"
200+
echo " rm -rf $VENV_DIR"

docs/requirements.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@ matplotlib
22
numpy
33
sphinx-copybutton
44
sphinx-gallery
5-
sphinx===5.0.0
5+
sphinx>=5.0.0,<8.0.0
66
Jinja2==3.1.4
77
sphinx-autodoc-typehints
88
sphinx-serve==1.0.1
99
git+https://github.com/vmoens/aafig@4319769eae88fff8e3464858f3cf8c277f35335d
1010
sphinxcontrib-htmlhelp
11-
-e git+https://github.com/pytorch/pytorch_sphinx_theme.git#egg=pytorch_sphinx_theme
11+
pytorch-sphinx-theme
1212
myst-parser
1313
docutils
1414
sphinx_design
1515

1616
torchvision
17-
dm_control
18-
mujoco<3.3.6
19-
gymnasium[classic_control,atari]
17+
dm_control>=1.0.0
18+
mujoco>=3.0.0,<3.3.6
19+
gymnasium[classic_control,atari,mujoco]
2020
pygame
2121
tqdm
2222
ipython
2323
imageio[ffmpeg,pyav]
2424
memory_profiler
25-
pyrender
2625
pytest
2726
vmas
2827
onnxscript
@@ -32,3 +31,8 @@ psutil
3231
hydra-core>=1.1
3332
omegaconf
3433
hydra-submitit-launcher
34+
transformers
35+
accelerate
36+
sentencepiece
37+
playwright
38+
ale-py>=0.9.0

0 commit comments

Comments
 (0)