Skip to content

Commit 5a230d7

Browse files
authored
Merge branch 'main' into add-new-theme
2 parents da018d8 + 78933b1 commit 5a230d7

File tree

2 files changed

+60
-44
lines changed

2 files changed

+60
-44
lines changed

.jenkins/validate_tutorials_built.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353
"intermediate_source/tensorboard_profiler_tutorial", # reenable after 2.0 release.
5454
"advanced_source/semi_structured_sparse", # reenable after 3303 is fixed.
5555
"intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixe
56-
"intermediate_source/memory_format_tutorial", # causes other tutorials like torch_logs fail. "state" issue, reseting dynamo didn't help
57-
"intermediate_source/ax_multiobjective_nas_tutorial"
5856
]
5957

6058
def tutorial_source_dirs() -> List[Path]:

conf.py

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,66 @@
3434

3535
html_theme = "pytorch_sphinx_theme2"
3636
html_theme_path = [pytorch_sphinx_theme2.get_html_theme_path()]
37-
import distutils.file_util
38-
import gc
37+
38+
import torch
3939
import glob
4040
import random
41-
import re
4241
import shutil
43-
from pathlib import Path
44-
4542
import numpy
4643
import pandocfilters
4744
import plotly.io as pio
4845
import pypandoc
4946
import torch
50-
47+
import distutils.file_util
48+
import re
5149
from get_sphinx_filenames import SPHINX_SHOULD_RUN
50+
import pandocfilters
51+
import pypandoc
52+
import plotly.io as pio
53+
from pathlib import Path
5254

5355
pio.renderers.default = "sphinx_gallery"
5456

57+
import sphinx_gallery.gen_rst
58+
import multiprocessing
59+
60+
# Monkey patch sphinx gallery to run each example in an isolated process so that
61+
# we don't need to worry about examples changing global state.
62+
#
63+
# Alt option 1: Parallelism was added to sphinx gallery (a later version that we
64+
# are not using yet) using joblib, but it seems to result in errors for us, and
65+
# it has no effect if you set parallel = 1 (it will not put each file run into
66+
# its own process and run singly) so you need parallel >= 2, and there may be
67+
# tutorials that cannot be run in parallel.
68+
#
69+
# Alt option 2: Run sphinx gallery once per file (similar to how we shard in CI
70+
# but with shard sizes of 1), but running sphinx gallery for each file has a
71+
# ~5min overhead, resulting in the entire suite taking ~2x time
72+
def call_fn(func, args, kwargs, result_queue):
73+
try:
74+
result = func(*args, **kwargs)
75+
result_queue.put((True, result))
76+
except Exception as e:
77+
result_queue.put((False, str(e)))
78+
79+
def call_in_subprocess(func):
80+
def wrapper(*args, **kwargs):
81+
result_queue = multiprocessing.Queue()
82+
p = multiprocessing.Process(
83+
target=call_fn,
84+
args=(func, args, kwargs, result_queue)
85+
)
86+
p.start()
87+
p.join()
88+
success, result = result_queue.get()
89+
if success:
90+
return result
91+
else:
92+
raise RuntimeError(f"Error in subprocess: {result}")
93+
return wrapper
94+
95+
sphinx_gallery.gen_rst.generate_file_rst = call_in_subprocess(sphinx_gallery.gen_rst.generate_file_rst)
96+
5597
try:
5698
import torchvision
5799
except ImportError:
@@ -109,43 +151,19 @@
109151
# -- Sphinx-gallery configuration --------------------------------------------
110152

111153

112-
def reset_seeds(gallery_conf, fname):
113-
torch.cuda.empty_cache()
114-
torch.backends.cudnn.deterministic = True
115-
torch.backends.cudnn.benchmark = False
116-
torch._dynamo.reset()
117-
torch._inductor.config.force_disable_caches = True
118-
torch.manual_seed(42)
119-
torch.set_default_device(None)
120-
random.seed(10)
121-
numpy.random.seed(10)
122-
torch.set_grad_enabled(True)
123-
124-
gc.collect()
125-
126-
127154
sphinx_gallery_conf = {
128-
"examples_dirs": [
129-
"beginner_source",
130-
"intermediate_source",
131-
"advanced_source",
132-
"recipes_source",
133-
"prototype_source",
134-
],
135-
"gallery_dirs": ["beginner", "intermediate", "advanced", "recipes", "prototype"],
136-
"filename_pattern": re.compile(SPHINX_SHOULD_RUN),
137-
"promote_jupyter_magic": True,
138-
"backreferences_dir": None,
139-
"first_notebook_cell": (
140-
"# For tips on running notebooks in Google Colab, see\n"
141-
"# https://pytorch.org/tutorials/beginner/colab\n"
142-
"%matplotlib inline"
143-
),
144-
"reset_modules": (reset_seeds),
145-
"ignore_pattern": r"_torch_export_nightly_tutorial.py",
146-
"pypandoc": {
147-
"extra_args": ["--mathjax", "--toc"],
148-
"filters": [".jenkins/custom_pandoc_filter.py"],
155+
'examples_dirs': ['beginner_source', 'intermediate_source',
156+
'advanced_source', 'recipes_source', 'prototype_source'],
157+
'gallery_dirs': ['beginner', 'intermediate', 'advanced', 'recipes', 'prototype'],
158+
'filename_pattern': re.compile(SPHINX_SHOULD_RUN),
159+
'promote_jupyter_magic': True,
160+
'backreferences_dir': None,
161+
'first_notebook_cell': ("# For tips on running notebooks in Google Colab, see\n"
162+
"# https://pytorch.org/tutorials/beginner/colab\n"
163+
"%matplotlib inline"),
164+
'ignore_pattern': r'_torch_export_nightly_tutorial.py',
165+
'pypandoc': {'extra_args': ['--mathjax', '--toc'],
166+
'filters': ['.jenkins/custom_pandoc_filter.py'],
149167
},
150168
}
151169

0 commit comments

Comments
 (0)