|
34 | 34 |
|
35 | 35 | html_theme = "pytorch_sphinx_theme2"
|
36 | 36 | html_theme_path = [pytorch_sphinx_theme2.get_html_theme_path()]
|
37 |
| -import distutils.file_util |
38 |
| -import gc |
| 37 | + |
| 38 | +import torch |
39 | 39 | import glob
|
40 | 40 | import random
|
41 |
| -import re |
42 | 41 | import shutil
|
43 |
| -from pathlib import Path |
44 |
| - |
45 | 42 | import numpy
|
46 | 43 | import pandocfilters
|
47 | 44 | import plotly.io as pio
|
48 | 45 | import pypandoc
|
49 | 46 | import torch
|
50 |
| - |
| 47 | +import distutils.file_util |
| 48 | +import re |
51 | 49 | 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 |
52 | 54 |
|
53 | 55 | pio.renderers.default = "sphinx_gallery"
|
54 | 56 |
|
| 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 | + |
55 | 97 | try:
|
56 | 98 | import torchvision
|
57 | 99 | except ImportError:
|
|
109 | 151 | # -- Sphinx-gallery configuration --------------------------------------------
|
110 | 152 |
|
111 | 153 |
|
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 |
| - |
127 | 154 | 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'], |
149 | 167 | },
|
150 | 168 | }
|
151 | 169 |
|
|
0 commit comments