Skip to content

Commit f1c42e0

Browse files
committed
[Doc] Fix (and deactivate) tutorials
ghstack-source-id: 56c7757c36a2d609688ce0777a49d54763d3e691 Pull Request resolved: #2785
1 parent f5445a4 commit f1c42e0

11 files changed

+147
-29
lines changed

.github/workflows/docs.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ jobs:
2626
build-docs:
2727
strategy:
2828
matrix:
29-
python_version: ["3.9"]
30-
cuda_arch_version: ["12.4"]
29+
python_version: [ "3.9" ]
30+
cuda_arch_version: [ "12.4" ]
3131
uses: pytorch/test-infra/.github/workflows/linux_job_v2.yml@main
3232
with:
33+
runner: linux.g5.4xlarge.nvidia.gpu
3334
repository: pytorch/rl
3435
upload-artifact: docs
3536
timeout: 120
@@ -38,7 +39,6 @@ jobs:
3839
set -v
3940
# apt-get update && apt-get install -y -f git wget gcc g++ dialog apt-utils
4041
yum makecache
41-
# yum install -y glfw glew mesa-libGL mesa-libGL-devel mesa-libOSMesa-devel egl-utils freeglut
4242
# Install Mesa and OpenGL Libraries:
4343
yum install -y glfw mesa-libGL mesa-libGL-devel egl-utils freeglut mesa-libGLU mesa-libEGL
4444
# Install DRI Drivers:
@@ -112,7 +112,7 @@ jobs:
112112
cd ./docs
113113
# timeout 7m bash -ic "MUJOCO_GL=egl sphinx-build ./source _local_build" || code=$?; if [[ $code -ne 124 && $code -ne 0 ]]; then exit $code; fi
114114
# bash -ic "PYOPENGL_PLATFORM=egl MUJOCO_GL=egl sphinx-build ./source _local_build" || code=$?; if [[ $code -ne 124 && $code -ne 0 ]]; then exit $code; fi
115-
PYOPENGL_PLATFORM=egl MUJOCO_GL=egl TORCHRL_CONSOLE_STREAM=stdout sphinx-build ./source _local_build -v
115+
PYOPENGL_PLATFORM=egl MUJOCO_GL=egl TORCHRL_CONSOLE_STREAM=stdout sphinx-build ./source _local_build -v -j 4
116116
cd ..
117117
118118
cp -r docs/_local_build/* "${RUNNER_ARTIFACT_DIR}"
@@ -123,8 +123,8 @@ jobs:
123123
124124
upload:
125125
needs: build-docs
126-
if: github.repository == 'pytorch/rl' && github.event_name == 'push' &&
127-
((github.ref_type == 'branch' && github.ref_name == 'main') || github.ref_type == 'tag')
126+
if: github.repository == 'pytorch/rl' && github.event_name == 'push' &&
127+
((github.ref_type == 'branch' && github.ref_name == 'main') || github.ref_type == 'tag')
128128
permissions:
129129
contents: write
130130
uses: pytorch/test-infra/.github/workflows/linux_job.yml@main

docs/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ vmas
2828
onnxscript
2929
onnxruntime
3030
onnx
31+
psutil

docs/source/conf.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
import pytorch_sphinx_theme
2929
import torchrl
3030

31-
# Suppress warnings - TODO
32-
# suppress_warnings = [ 'misc.highlighting_failure' ]
31+
# Suppress warnings
3332
warnings.filterwarnings("ignore", category=UserWarning)
3433

3534
project = "torchrl"
@@ -86,6 +85,21 @@
8685
"torchvision": ("https://pytorch.org/vision/stable/", None),
8786
}
8887

88+
89+
def kill_procs(gallery_conf, fname):
90+
import os
91+
92+
import psutil
93+
94+
# Get the current process
95+
current_proc = psutil.Process(os.getpid())
96+
# Iterate over all child processes
97+
for child in current_proc.children(recursive=True):
98+
# Kill the child process
99+
child.terminate()
100+
print(f"Killed child process with PID {child.pid}") # noqa: T201
101+
102+
89103
sphinx_gallery_conf = {
90104
"examples_dirs": "reference/generated/tutorials/", # path to your example scripts
91105
"gallery_dirs": "tutorials", # path to where to save gallery generated output
@@ -95,9 +109,12 @@
95109
"notebook_images": "reference/generated/tutorials/media/", # images to parse
96110
"download_all_examples": True,
97111
"abort_on_example_error": True,
98-
"show_memory": True,
112+
# "show_memory": True,
113+
"plot_gallery": "False",
99114
"capture_repr": ("_repr_html_", "__repr__"), # capture representations
100115
"write_computation_times": True,
116+
# "compress_images": ("images", "thumbnails"),
117+
"reset_modules": (kill_procs, "matplotlib", "seaborn"),
101118
}
102119

103120
napoleon_use_ivar = True

docs/source/content_generation.py

-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ def generate_tutorial_references(tutorial_path: str, file_type: str) -> None:
8383
for f in os.listdir(tutorial_path)
8484
if f.endswith((".py", ".rst", ".png"))
8585
]
86-
# Make rb_tutorial.py the first one
87-
file_paths = [p for p in file_paths if p.endswith("rb_tutorial.py")] + [
88-
p for p in file_paths if not p.endswith("rb_tutorial.py")
89-
]
9086

9187
for file_path in file_paths:
9288
shutil.copyfile(file_path, os.path.join(target_path, Path(file_path).name))

tutorials/sphinx-tutorials/coding_ddpg.py

+15
Original file line numberDiff line numberDiff line change
@@ -1234,3 +1234,18 @@ def ceil_div(x, y):
12341234
# - Using `@dispatch` (see `[Feature] Distpatch IQL loss module <https://github.com/pytorch/rl/pull/1230>`_.)
12351235
# - Allowing flexible TensorDict keys.
12361236
#
1237+
1238+
# sphinx_gallery_start_ignore
1239+
1240+
# Remove scratch dir
1241+
try:
1242+
import shutil
1243+
1244+
# Use shutil.rmtree() to delete the directory and all its contents
1245+
shutil.rmtree(buffer_scratch_dir)
1246+
print(f"Directory '{buffer_scratch_dir}' deleted successfully.")
1247+
except FileNotFoundError:
1248+
print(f"Directory '{buffer_scratch_dir}' not found.")
1249+
except Exception as e:
1250+
print(f"Error deleting directory: {e}")
1251+
# sphinx_gallery_end_ignore

tutorials/sphinx-tutorials/coding_dqn.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,13 @@ def make_model(dummy_env):
380380
# time must always have the same shape.
381381

382382

383+
buffer_scratch_dir = tempfile.TemporaryDirectory().name
384+
385+
383386
def get_replay_buffer(buffer_size, n_optim, batch_size, device):
384387
replay_buffer = TensorDictReplayBuffer(
385388
batch_size=batch_size,
386-
storage=LazyMemmapStorage(buffer_size),
389+
storage=LazyMemmapStorage(buffer_size, scratch_dir=buffer_scratch_dir),
387390
prefetch=n_optim,
388391
transform=lambda td: td.to(device),
389392
)
@@ -777,3 +780,19 @@ def print_csv_files_in_folder(folder_path):
777780
# - A distributional loss (see :class:`~torchrl.objectives.DistributionalDQNLoss`
778781
# for more information).
779782
# - More fancy exploration techniques, such as :class:`~torchrl.modules.NoisyLinear` layers and such.
783+
784+
785+
# sphinx_gallery_start_ignore
786+
787+
# Remove scratch dir
788+
try:
789+
import shutil
790+
791+
# Use shutil.rmtree() to delete the directory and all its contents
792+
shutil.rmtree(buffer_scratch_dir)
793+
print(f"Directory '{buffer_scratch_dir}' deleted successfully.")
794+
except FileNotFoundError:
795+
print(f"Directory '{buffer_scratch_dir}' not found.")
796+
except Exception as e:
797+
print(f"Error deleting directory: {e}")
798+
# sphinx_gallery_end_ignore

tutorials/sphinx-tutorials/dqn_with_rnn.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* tqdm
2121
"""
2222

23+
import tempfile
24+
2325
#########################################################################
2426
# Overview
2527
# --------
@@ -393,9 +395,13 @@
393395
# For the sake of efficiency, we're only running a few thousands iterations
394396
# here. In a real setting, the total number of frames should be set to 1M.
395397
#
398+
buffer_scratch_dir = tempfile.TemporaryDirectory().name
399+
396400
collector = SyncDataCollector(env, stoch_policy, frames_per_batch=50, total_frames=200)
397401
rb = TensorDictReplayBuffer(
398-
storage=LazyMemmapStorage(20_000), batch_size=4, prefetch=10
402+
storage=LazyMemmapStorage(20_000, scratch_dir=buffer_scratch_dir),
403+
batch_size=4,
404+
prefetch=10,
399405
)
400406

401407
######################################################################
@@ -470,3 +476,19 @@
470476
# ---------------
471477
#
472478
# - The TorchRL documentation can be found `here <https://pytorch.org/rl/>`_.
479+
480+
481+
# sphinx_gallery_start_ignore
482+
483+
# Remove scratch dir
484+
try:
485+
import shutil
486+
487+
# Use shutil.rmtree() to delete the directory and all its contents
488+
shutil.rmtree(buffer_scratch_dir)
489+
print(f"Directory '{buffer_scratch_dir}' deleted successfully.")
490+
except FileNotFoundError:
491+
print(f"Directory '{buffer_scratch_dir}' not found.")
492+
except Exception as e:
493+
print(f"Error deleting directory: {e}")
494+
# sphinx_gallery_end_ignore

tutorials/sphinx-tutorials/getting-started-3.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
1717
"""
1818

19+
import tempfile
20+
1921
#################################
2022
#
2123
# There is no learning without data. In supervised learning, users are
@@ -56,12 +58,12 @@
5658

5759
import torch
5860

59-
torch.manual_seed(0)
60-
6161
from torchrl.collectors import SyncDataCollector
6262
from torchrl.envs import GymEnv
6363
from torchrl.envs.utils import RandomPolicy
6464

65+
torch.manual_seed(0)
66+
6567
env = GymEnv("CartPole-v1")
6668
env.set_seed(0)
6769

@@ -141,7 +143,11 @@
141143

142144
from torchrl.data.replay_buffers import LazyMemmapStorage, ReplayBuffer
143145

144-
buffer = ReplayBuffer(storage=LazyMemmapStorage(max_size=1000))
146+
buffer_scratch_dir = tempfile.TemporaryDirectory().name
147+
148+
buffer = ReplayBuffer(
149+
storage=LazyMemmapStorage(max_size=1000, scratch_dir=buffer_scratch_dir)
150+
)
145151

146152
#################################
147153
# Populating the buffer can be done via the
@@ -190,3 +196,18 @@
190196
# batch-size in the constructor, then try to iterate over it. This is
191197
# equivalent to calling ``rb.sample()`` within a loop!
192198
#
199+
200+
# sphinx_gallery_start_ignore
201+
202+
# Remove scratch dir
203+
try:
204+
import shutil
205+
206+
# Use shutil.rmtree() to delete the directory and all its contents
207+
shutil.rmtree(buffer_scratch_dir)
208+
print(f"Directory '{buffer_scratch_dir}' deleted successfully.")
209+
except FileNotFoundError:
210+
print(f"Directory '{buffer_scratch_dir}' not found.")
211+
except Exception as e:
212+
print(f"Error deleting directory: {e}")
213+
# sphinx_gallery_end_ignore

tutorials/sphinx-tutorials/multiagent_competitive_ddpg.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,16 @@
661661
# This will result in faster sampling but is subject to the memory constraints of the selected device.
662662
#
663663

664+
664665
replay_buffers = {}
666+
scratch_dirs = []
665667
for group, _agents in env.group_map.items():
668+
scratch_dir = tempfile.TemporaryDirectory().name
669+
scratch_dirs.append(scratch_dir)
666670
replay_buffer = ReplayBuffer(
667671
storage=LazyMemmapStorage(
668-
memory_size
672+
memory_size,
673+
scratch_dir=scratch_dir,
669674
), # We will store up to memory_size multi-agent transitions
670675
sampler=RandomSampler(),
671676
batch_size=train_batch_size, # We will sample batches of this size
@@ -948,3 +953,19 @@ def process_batch(batch: TensorDictBase) -> TensorDictBase:
948953
#
949954
# Scenarios available in `VMAS <https://github.com/proroklab/VectorizedMultiAgentSimulator>`__
950955
#
956+
957+
# sphinx_gallery_start_ignore
958+
959+
# Remove scratch dir
960+
try:
961+
import shutil
962+
963+
for scratch_dir in scratch_dirs:
964+
# Use shutil.rmtree() to delete the directory and all its contents
965+
shutil.rmtree(scratch_dir)
966+
print(f"Directory '{scratch_dir}' deleted successfully.")
967+
except FileNotFoundError:
968+
print(f"Directory '{scratch_dir}' not found.")
969+
except Exception as e:
970+
print(f"Error deleting directory: {e}")
971+
# sphinx_gallery_end_ignore

tutorials/sphinx-tutorials/pretrained_models.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
=======================
44
This tutorial explains how to use pretrained models in TorchRL.
55
"""
6+
import tempfile
7+
68
##############################################################################
79
# At the end of this tutorial, you will be capable of using pretrained models
810
# for efficient image representation, and fine-tune them.
@@ -66,7 +68,7 @@
6668
r3m = R3MTransform(
6769
"resnet50",
6870
in_keys=["pixels"],
69-
download=True,
71+
download=False, # Turn to true for real-life testing
7072
)
7173
env_transformed = TransformedEnv(base_env, r3m)
7274
net = nn.Sequential(
@@ -114,7 +116,8 @@
114116
#
115117
from torchrl.data import LazyMemmapStorage, ReplayBuffer
116118

117-
storage = LazyMemmapStorage(1000)
119+
buffer_scratch_dir = tempfile.TemporaryDirectory().name
120+
storage = LazyMemmapStorage(1000, scratch_dir=buffer_scratch_dir)
118121
rb = ReplayBuffer(storage=storage, transform=Compose(lambda td: td.to(device), r3m))
119122

120123
##############################################################################
@@ -140,7 +143,15 @@
140143
print("data after sampling:", batch)
141144

142145
# sphinx_gallery_start_ignore
143-
import time
144-
145-
time.sleep(10)
146+
# Remove scratch dir
147+
try:
148+
import shutil
149+
150+
# Use shutil.rmtree() to delete the directory and all its contents
151+
shutil.rmtree(buffer_scratch_dir)
152+
print(f"Directory '{buffer_scratch_dir}' deleted successfully.")
153+
except FileNotFoundError:
154+
print(f"Directory '{buffer_scratch_dir}' not found.")
155+
except Exception as e:
156+
print(f"Error deleting directory: {e}")
146157
# sphinx_gallery_end_ignore

tutorials/sphinx-tutorials/rb_tutorial.py

-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@
5656
# example:
5757
#
5858

59-
######################################################################
60-
# .. warning:: This tutorial build is temporarily disabled.
61-
62-
exit(0)
63-
6459
# sphinx_gallery_start_ignore
6560
import gc
6661

0 commit comments

Comments
 (0)