From 3c0a8e5fd70d017b5e0d70ca79e32dd4134043f8 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 14:13:38 -0700 Subject: [PATCH 01/26] Move back to 2.7 --- .ci/docker/requirements.txt | 6 +++--- .jenkins/build.sh | 7 ++----- .jenkins/validate_tutorials_built.py | 5 ----- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/.ci/docker/requirements.txt b/.ci/docker/requirements.txt index 0e95c62c6b..e6802cb045 100644 --- a/.ci/docker/requirements.txt +++ b/.ci/docker/requirements.txt @@ -14,7 +14,7 @@ tqdm==4.66.1 numpy==1.24.4 matplotlib librosa -torch==2.6 +torch==2.7 torchvision torchdata networkx @@ -67,7 +67,7 @@ iopath pygame==2.6.0 pycocotools semilearn==0.3.2 -torchao==0.5.0 +torchao==0.10.0 segment_anything==1.0 torchrec==1.1.0; platform_system == "Linux" -fbgemm-gpu==1.1.0; platform_system == "Linux" +fbgemm-gpu==1.2.0; platform_system == "Linux" diff --git a/.jenkins/build.sh b/.jenkins/build.sh index 8786859d7d..58483c168b 100755 --- a/.jenkins/build.sh +++ b/.jenkins/build.sh @@ -22,13 +22,10 @@ sudo apt-get install -y pandoc #Install PyTorch Nightly for test. # Nightly - pip install --pre torch torchvision torchaudio -f https://download.pytorch.org/whl/nightly/cu102/torch_nightly.html # Install 2.5 to merge all 2.4 PRs - uncomment to install nightly binaries (update the version as needed). -# sudo pip uninstall -y torch torchvision torchaudio torchtext torchdata -# sudo pip3 install torch==2.6.0 torchvision --no-cache-dir --index-url https://download.pytorch.org/whl/test/cu124 # sudo pip uninstall -y fbgemm-gpu torchrec +# sudo pip uninstall -y torch torchvision torchaudio torchtext torchdata torchrl tensordict # sudo pip3 install fbgemm-gpu==1.1.0 torchrec==1.0.0 --no-cache-dir --index-url https://download.pytorch.org/whl/test/cu124 -sudo pip uninstall -y torch torchvision torchaudio torchtext torchdata torchrl tensordict -pip3 install torch==2.7.0 torchvision torchaudio --no-cache-dir --index-url https://download.pytorch.org/whl/test/cu126 -#sudo pip uninstall -y fbgemm-gpu +# pip3 install torch==2.7.0 torchvision torchaudio --no-cache-dir --index-url https://download.pytorch.org/whl/test/cu126 # Install two language tokenizers for Translation with TorchText tutorial python -m spacy download en_core_web_sm python -m spacy download de_core_news_sm diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index 3ed1e0c028..380f0b458b 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -51,11 +51,6 @@ "intermediate_source/text_to_speech_with_torchaudio", "intermediate_source/tensorboard_profiler_tutorial", # reenable after 2.0 release. "advanced_source/semi_structured_sparse", # reenable after 3303 is fixed. - "intermediate_source/mario_rl_tutorial", # reenable after 3302 is fixed - "intermediate_source/reinforcement_ppo", # reenable after 3302 is fixed - "intermediate_source/pinmem_nonblock", # reenable after 3302 is fixed - "intermediate_source/dqn_with_rnn_tutorial", # reenable after 3302 is fixed - "advanced_source/pendulum", # reenable after 3302 is fixed "advanced_source/coding_ddpg", # reenable after 3302 is fixed "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed "recipes_source/recipes/reasoning_about_shapes" # reenable after 3326 is fixed From a66f05eacfba8e907c39ef3d287a19a18c3ef83f Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 14:31:27 -0700 Subject: [PATCH 02/26] Update --- conf.py | 208 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 122 insertions(+), 86 deletions(-) diff --git a/conf.py b/conf.py index a12a05d21c..4256cfe729 100644 --- a/conf.py +++ b/conf.py @@ -29,34 +29,44 @@ # import os import sys -sys.path.insert(0, os.path.abspath('.')) -sys.path.insert(0, os.path.abspath('./.jenkins')) -import pytorch_sphinx_theme -import torch -import numpy + +sys.path.insert(0, os.path.abspath(".")) +sys.path.insert(0, os.path.abspath("./.jenkins")) +import distutils.file_util import gc import glob import random -import shutil -from custom_directives import IncludeDirective, GalleryItemDirective, CustomGalleryItemDirective, CustomCalloutItemDirective, CustomCardItemDirective -import distutils.file_util import re -from get_sphinx_filenames import SPHINX_SHOULD_RUN +import shutil +from pathlib import Path + +import numpy import pandocfilters -import pypandoc import plotly.io as pio -from pathlib import Path -pio.renderers.default = 'sphinx_gallery' +import pypandoc +import pytorch_sphinx_theme +import torch +from custom_directives import ( + CustomCalloutItemDirective, + CustomCardItemDirective, + CustomGalleryItemDirective, + GalleryItemDirective, + IncludeDirective, +) +from get_sphinx_filenames import SPHINX_SHOULD_RUN + +pio.renderers.default = "sphinx_gallery" try: import torchvision except ImportError: import warnings + warnings.warn('unable to load "torchvision" package') import pytorch_sphinx_theme -rst_epilog =""" +rst_epilog = """ .. |edit| image:: /_static/pencil-16.png :width: 16px :height: 16px @@ -69,21 +79,21 @@ # needs_sphinx = '1.0' html_meta = { - 'description': 'Master PyTorch with our step-by-step tutorials for all skill levels. Start your journey to becoming a PyTorch expert today!', - 'keywords': 'PyTorch, tutorials, Getting Started, deep learning, AI', - 'author': 'PyTorch Contributors' + "description": "Master PyTorch with our step-by-step tutorials for all skill levels. Start your journey to becoming a PyTorch expert today!", + "keywords": "PyTorch, tutorials, Getting Started, deep learning, AI", + "author": "PyTorch Contributors", } # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ - 'sphinxcontrib.katex', - 'sphinx.ext.intersphinx', - 'sphinx_copybutton', - 'sphinx_gallery.gen_gallery', - 'sphinx_design', - 'sphinx_sitemap' + "sphinxcontrib.katex", + "sphinx.ext.intersphinx", + "sphinx_copybutton", + "sphinx_gallery.gen_gallery", + "sphinx_design", + "sphinx_sitemap", ] intersphinx_mapping = { @@ -97,32 +107,45 @@ # -- Sphinx-gallery configuration -------------------------------------------- + def reset_seeds(gallery_conf, fname): torch.cuda.empty_cache() + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + torch._dynamo.reset() torch.manual_seed(42) torch.set_default_device(None) random.seed(10) numpy.random.seed(10) gc.collect() + sphinx_gallery_conf = { - 'examples_dirs': ['beginner_source', 'intermediate_source', - 'advanced_source', 'recipes_source', 'prototype_source'], - 'gallery_dirs': ['beginner', 'intermediate', 'advanced', 'recipes', 'prototype'], - 'filename_pattern': re.compile(SPHINX_SHOULD_RUN), - 'promote_jupyter_magic': True, - 'backreferences_dir': None, - 'first_notebook_cell': ("# For tips on running notebooks in Google Colab, see\n" - "# https://pytorch.org/tutorials/beginner/colab\n" - "%matplotlib inline"), - 'reset_modules': (reset_seeds), - 'ignore_pattern': r'_torch_export_nightly_tutorial.py', - 'pypandoc': {'extra_args': ['--mathjax', '--toc'], - 'filters': ['.jenkins/custom_pandoc_filter.py'], + "examples_dirs": [ + "beginner_source", + "intermediate_source", + "advanced_source", + "recipes_source", + "prototype_source", + ], + "gallery_dirs": ["beginner", "intermediate", "advanced", "recipes", "prototype"], + "filename_pattern": re.compile(SPHINX_SHOULD_RUN), + "promote_jupyter_magic": True, + "backreferences_dir": None, + "first_notebook_cell": ( + "# For tips on running notebooks in Google Colab, see\n" + "# https://pytorch.org/tutorials/beginner/colab\n" + "%matplotlib inline" + ), + "reset_modules": (reset_seeds), + "ignore_pattern": r"_torch_export_nightly_tutorial.py", + "pypandoc": { + "extra_args": ["--mathjax", "--toc"], + "filters": [".jenkins/custom_pandoc_filter.py"], }, } -html_baseurl = 'https://pytorch.org/tutorials/' # needed for sphinx-sitemap +html_baseurl = "https://pytorch.org/tutorials/" # needed for sphinx-sitemap sitemap_locales = [None] sitemap_excludes = [ "search.html", @@ -130,7 +153,7 @@ def reset_seeds(gallery_conf, fname): ] sitemap_url_scheme = "{link}" -if os.getenv('GALLERY_PATTERN'): +if os.getenv("GALLERY_PATTERN"): # GALLERY_PATTERN is to be used when you want to work on a single # tutorial. Previously this was fed into filename_pattern, but # if you do that, you still end up parsing all of the other Python @@ -138,9 +161,11 @@ def reset_seeds(gallery_conf, fname): # ignore_pattern also skips parsing. # See https://github.com/sphinx-gallery/sphinx-gallery/issues/721 # for a more detailed description of the issue. - sphinx_gallery_conf['ignore_pattern'] = r'/(?!' + re.escape(os.getenv('GALLERY_PATTERN')) + r')[^/]+$' + sphinx_gallery_conf["ignore_pattern"] = ( + r"/(?!" + re.escape(os.getenv("GALLERY_PATTERN")) + r")[^/]+$" + ) -for i in range(len(sphinx_gallery_conf['examples_dirs'])): +for i in range(len(sphinx_gallery_conf["examples_dirs"])): gallery_dir = Path(sphinx_gallery_conf["gallery_dirs"][i]) source_dir = Path(sphinx_gallery_conf["examples_dirs"][i]) @@ -152,21 +177,21 @@ def reset_seeds(gallery_conf, fname): distutils.file_util.copy_file(f, gallery_subdir_path, update=True) # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # # source_suffix = ['.rst', '.md'] -source_suffix = '.rst' +source_suffix = ".rst" # The master toctree document. -master_doc = 'index' +master_doc = "index" # General information about the project. -project = 'PyTorch Tutorials' -copyright = '2024, PyTorch' -author = 'PyTorch contributors' +project = "PyTorch Tutorials" +copyright = "2024, PyTorch" +author = "PyTorch contributors" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -182,17 +207,22 @@ def reset_seeds(gallery_conf, fname): # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. -language = 'en' +language = "en" # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This patterns also effect to html_static_path and html_extra_path -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', 'src/pytorch-sphinx-theme/docs*'] -exclude_patterns += sphinx_gallery_conf['examples_dirs'] -exclude_patterns += ['*/index.rst'] +exclude_patterns = [ + "_build", + "Thumbs.db", + ".DS_Store", + "src/pytorch-sphinx-theme/docs*", +] +exclude_patterns += sphinx_gallery_conf["examples_dirs"] +exclude_patterns += ["*/index.rst"] # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = False @@ -220,7 +250,7 @@ def reset_seeds(gallery_conf, fname): # # Add any paths that contain custom static files (such as style sheets) here, # # relative to this directory. They are copied after the builtin static files, # # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +html_static_path = ["_static"] # # Custom sidebar templates, maps document names to template names. # html_sidebars = { @@ -229,23 +259,23 @@ def reset_seeds(gallery_conf, fname): # } -html_theme = 'pytorch_sphinx_theme' +html_theme = "pytorch_sphinx_theme" html_theme_path = [pytorch_sphinx_theme.get_html_theme_path()] -html_logo = '_static/img/pytorch-logo-dark.svg' +html_logo = "_static/img/pytorch-logo-dark.svg" html_theme_options = { - 'pytorch_project': 'tutorials', - 'collapse_navigation': False, - 'display_version': True, - 'navigation_with_keys': True, - 'logo_only': False, - 'analytics_id': 'GTM-T8XT4PS', + "pytorch_project": "tutorials", + "collapse_navigation": False, + "display_version": True, + "navigation_with_keys": True, + "logo_only": False, + "analytics_id": "GTM-T8XT4PS", } # -- Options for HTMLHelp output ------------------------------------------ # Output file base name for HTML help builder. -htmlhelp_basename = 'PyTorchTutorialsdoc' +htmlhelp_basename = "PyTorchTutorialsdoc" # -- Options for LaTeX output --------------------------------------------- @@ -254,15 +284,12 @@ def reset_seeds(gallery_conf, fname): # The paper size ('letterpaper' or 'a4paper'). # # 'papersize': 'letterpaper', - # The font size ('10pt', '11pt' or '12pt'). # # 'pointsize': '10pt', - # Additional stuff for the LaTeX preamble. # # 'preamble': '', - # Latex figure (float) alignment # # 'figure_align': 'htbp', @@ -272,8 +299,13 @@ def reset_seeds(gallery_conf, fname): # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - (master_doc, 'PyTorchTutorials.tex', 'PyTorch Tutorials', - 'Sasank, PyTorch contributors', 'manual'), + ( + master_doc, + "PyTorchTutorials.tex", + "PyTorch Tutorials", + "Sasank, PyTorch contributors", + "manual", + ), ] @@ -281,10 +313,7 @@ def reset_seeds(gallery_conf, fname): # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). -man_pages = [ - (master_doc, 'pytorchtutorials', 'PyTorch Tutorials', - [author], 1) -] +man_pages = [(master_doc, "pytorchtutorials", "PyTorch Tutorials", [author], 1)] # -- Options for Texinfo output ------------------------------------------- @@ -293,40 +322,47 @@ def reset_seeds(gallery_conf, fname): # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - (master_doc, 'PyTorchTutorials', 'PyTorch Tutorials', - author, 'PyTorchTutorials', 'One line description of project.', - 'Miscellaneous'), + ( + master_doc, + "PyTorchTutorials", + "PyTorch Tutorials", + author, + "PyTorchTutorials", + "One line description of project.", + "Miscellaneous", + ), ] html_css_files = [ - 'https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css', - 'css/custom.css', - 'css/custom2.css' - ] + "https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css", + "css/custom.css", + "css/custom2.css", +] html_js_files = [ "js/custom.js", ] + def setup(app): # NOTE: in Sphinx 1.8+ `html_css_files` is an official configuration value # and can be moved outside of this function (and the setup(app) function # can be deleted). - #html_css_files = [ + # html_css_files = [ # 'https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css', # 'css/custom.css' - #] + # ] # In Sphinx 1.8 it was renamed to `add_css_file`, 1.7 and prior it is # `add_stylesheet` (deprecated in 1.8). - #add_css = getattr(app, 'add_css_file', app.add_stylesheet) - #for css_file in html_css_files: + # add_css = getattr(app, 'add_css_file', app.add_stylesheet) + # for css_file in html_css_files: # add_css(css_file) # Custom CSS - #app.add_stylesheet('css/pytorch_theme.css') + # app.add_stylesheet('css/pytorch_theme.css') # app.add_stylesheet('https://fonts.googleapis.com/css?family=Lato') # Custom directives - app.add_directive('includenodoc', IncludeDirective) - app.add_directive('galleryitem', GalleryItemDirective) - app.add_directive('customgalleryitem', CustomGalleryItemDirective) - app.add_directive('customcarditem', CustomCardItemDirective) - app.add_directive('customcalloutitem', CustomCalloutItemDirective) + app.add_directive("includenodoc", IncludeDirective) + app.add_directive("galleryitem", GalleryItemDirective) + app.add_directive("customgalleryitem", CustomGalleryItemDirective) + app.add_directive("customcarditem", CustomCardItemDirective) + app.add_directive("customcalloutitem", CustomCalloutItemDirective) From e00d84e537118699a1d5d9e2dbf2c09d76819f59 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 15:20:57 -0700 Subject: [PATCH 03/26] Update --- conf.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/conf.py b/conf.py index 4256cfe729..8bfe584e9d 100644 --- a/conf.py +++ b/conf.py @@ -114,9 +114,14 @@ def reset_seeds(gallery_conf, fname): torch.backends.cudnn.benchmark = False torch._dynamo.reset() torch.manual_seed(42) + torch.cuda.manual_seed_all(42) torch.set_default_device(None) random.seed(10) numpy.random.seed(10) + torch.set_grad_enabled(True) + torch.set_default_dtype(torch.float32) + torch.set_default_tensor_type(torch.FloatTensor) + logging.getLogger().setLevel(logging.WARNING) gc.collect() From 0c465f28b51ef4cebe1a2c55451ba9f946a6b4b1 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 15:30:46 -0700 Subject: [PATCH 04/26] Update --- conf.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/conf.py b/conf.py index 8bfe584e9d..842aa2b47f 100644 --- a/conf.py +++ b/conf.py @@ -121,6 +121,8 @@ def reset_seeds(gallery_conf, fname): torch.set_grad_enabled(True) torch.set_default_dtype(torch.float32) torch.set_default_tensor_type(torch.FloatTensor) + import logging + logging.getLogger().setLevel(logging.WARNING) gc.collect() From 22302ec265e7fefc0a053945614656a7c0612713 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 15:52:48 -0700 Subject: [PATCH 05/26] Update --- recipes_source/torch_logs.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/recipes_source/torch_logs.py b/recipes_source/torch_logs.py index b5c3f0bd8a..ca66df1374 100644 --- a/recipes_source/torch_logs.py +++ b/recipes_source/torch_logs.py @@ -30,49 +30,48 @@ # change logging settings at the command line. The equivalent environment # variable setting is shown for each example. +torch._dynamo.reset() import torch # exit cleanly if we are on a device that doesn't support torch.compile if torch.cuda.get_device_capability() < (7, 0): print("Skipping because torch.compile is not supported on this device.") else: + @torch.compile() def fn(x, y): z = x + y return z + 2 - inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda")) - -# print separator and reset dynamo -# between each example + # print separator and reset dynamo + # between each example def separator(name): print(f"==================={name}=========================") torch._dynamo.reset() - separator("Dynamo Tracing") -# View dynamo tracing -# TORCH_LOGS="+dynamo" + # View dynamo tracing + # TORCH_LOGS="+dynamo" torch._logging.set_logs(dynamo=logging.DEBUG) fn(*inputs) separator("Traced Graph") -# View traced graph -# TORCH_LOGS="graph" + # View traced graph + # TORCH_LOGS="graph" torch._logging.set_logs(graph=True) fn(*inputs) separator("Fusion Decisions") -# View fusion decisions -# TORCH_LOGS="fusion" + # View fusion decisions + # TORCH_LOGS="fusion" torch._logging.set_logs(fusion=True) fn(*inputs) separator("Output Code") -# View output code generated by inductor -# TORCH_LOGS="output_code" + # View output code generated by inductor + # TORCH_LOGS="output_code" torch._logging.set_logs(output_code=True) fn(*inputs) From d8f60eac22b2a815dccdd5509dc9fcea284674c9 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 16:13:23 -0700 Subject: [PATCH 06/26] Update --- recipes_source/torch_logs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recipes_source/torch_logs.py b/recipes_source/torch_logs.py index ca66df1374..c6ded8530d 100644 --- a/recipes_source/torch_logs.py +++ b/recipes_source/torch_logs.py @@ -30,8 +30,8 @@ # change logging settings at the command line. The equivalent environment # variable setting is shown for each example. -torch._dynamo.reset() import torch +torch._dynamo.reset() # exit cleanly if we are on a device that doesn't support torch.compile if torch.cuda.get_device_capability() < (7, 0): From 6071c565ca74bbf4aa7960f7cc4f36a6f5159b36 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 16:37:05 -0700 Subject: [PATCH 07/26] Update --- .jenkins/validate_tutorials_built.py | 3 ++- recipes_source/torch_logs.py | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index 380f0b458b..b850fb039b 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -53,7 +53,8 @@ "advanced_source/semi_structured_sparse", # reenable after 3303 is fixed. "advanced_source/coding_ddpg", # reenable after 3302 is fixed "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed - "recipes_source/recipes/reasoning_about_shapes" # reenable after 3326 is fixed + "recipes_source/recipes/reasoning_about_shapes", # reenable after 3326 is fixed + "advanced_source/dynamic_quantization_tutorial" ] def tutorial_source_dirs() -> List[Path]: diff --git a/recipes_source/torch_logs.py b/recipes_source/torch_logs.py index c6ded8530d..8b38639f49 100644 --- a/recipes_source/torch_logs.py +++ b/recipes_source/torch_logs.py @@ -31,7 +31,6 @@ # variable setting is shown for each example. import torch -torch._dynamo.reset() # exit cleanly if we are on a device that doesn't support torch.compile if torch.cuda.get_device_capability() < (7, 0): From 65bd04acec1cab9c429f56a4d064b9b2b8a4da2b Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 16:55:13 -0700 Subject: [PATCH 08/26] Update --- recipes_source/torch_logs.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/recipes_source/torch_logs.py b/recipes_source/torch_logs.py index 8b38639f49..14f33853e0 100644 --- a/recipes_source/torch_logs.py +++ b/recipes_source/torch_logs.py @@ -32,6 +32,12 @@ import torch +# Reset torch dynamo and empty CUDA cache before starting +torch._dynamo.reset() +if torch.cuda.is_available(): + torch.cuda.empty_cache() + + # exit cleanly if we are on a device that doesn't support torch.compile if torch.cuda.get_device_capability() < (7, 0): print("Skipping because torch.compile is not supported on this device.") From 9ef0ab88424808428b4d7a209237975f2988e4cc Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 17:01:17 -0700 Subject: [PATCH 09/26] Update --- .jenkins/metadata.json | 3 +++ .jenkins/validate_tutorials_built.py | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.jenkins/metadata.json b/.jenkins/metadata.json index 6e82d054b4..0514266bd6 100644 --- a/.jenkins/metadata.json +++ b/.jenkins/metadata.json @@ -1,4 +1,7 @@ { + "recipes_source/torch_logs.py": { + "duration": 0 + }, "intermediate_source/ax_multiobjective_nas_tutorial.py": { "extra_files": ["intermediate_source/mnist_train_nas.py"], "duration": 2000 diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index b850fb039b..94fc921b8f 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -54,7 +54,6 @@ "advanced_source/coding_ddpg", # reenable after 3302 is fixed "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed "recipes_source/recipes/reasoning_about_shapes", # reenable after 3326 is fixed - "advanced_source/dynamic_quantization_tutorial" ] def tutorial_source_dirs() -> List[Path]: From 9d047493f21ce13364c9a051c907acd54fc34d38 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 17:49:38 -0700 Subject: [PATCH 10/26] Update --- .jenkins/validate_tutorials_built.py | 1 + 1 file changed, 1 insertion(+) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index 94fc921b8f..b850fb039b 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -54,6 +54,7 @@ "advanced_source/coding_ddpg", # reenable after 3302 is fixed "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed "recipes_source/recipes/reasoning_about_shapes", # reenable after 3326 is fixed + "advanced_source/dynamic_quantization_tutorial" ] def tutorial_source_dirs() -> List[Path]: From 536135e009f2b0aa6a79652821c2487b4f8ed2e1 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 17:51:26 -0700 Subject: [PATCH 11/26] Update --- .jenkins/validate_tutorials_built.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index b850fb039b..a023fd6886 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -54,7 +54,8 @@ "advanced_source/coding_ddpg", # reenable after 3302 is fixed "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed "recipes_source/recipes/reasoning_about_shapes", # reenable after 3326 is fixed - "advanced_source/dynamic_quantization_tutorial" + "advanced_source/dynamic_quantization_tutorial", + "intermediate_source/memory_format_tutorial" ] def tutorial_source_dirs() -> List[Path]: From ea13112fc3de36e986fa52fd76a8d56c58a3f02d Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 18:32:13 -0700 Subject: [PATCH 12/26] Update --- .jenkins/validate_tutorials_built.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index a023fd6886..3aa57d6e63 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -55,7 +55,8 @@ "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed "recipes_source/recipes/reasoning_about_shapes", # reenable after 3326 is fixed "advanced_source/dynamic_quantization_tutorial", - "intermediate_source/memory_format_tutorial" + "intermediate_source/memory_format_tutorial", + "intermediate_source/custom_function_conv_bn_tutorial" ] def tutorial_source_dirs() -> List[Path]: From db6dbd53b01e2c08582ed8a2d3865ba273f50d81 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 20:28:59 -0700 Subject: [PATCH 13/26] Update --- .jenkins/validate_tutorials_built.py | 1 - 1 file changed, 1 deletion(-) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index 3aa57d6e63..4069888347 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -55,7 +55,6 @@ "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed "recipes_source/recipes/reasoning_about_shapes", # reenable after 3326 is fixed "advanced_source/dynamic_quantization_tutorial", - "intermediate_source/memory_format_tutorial", "intermediate_source/custom_function_conv_bn_tutorial" ] From 19006b3706513260d54eed8c1b7adb122ad207ff Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Tue, 29 Apr 2025 21:09:37 -0700 Subject: [PATCH 14/26] Update --- .jenkins/validate_tutorials_built.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index 4069888347..a023fd6886 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -55,7 +55,7 @@ "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed "recipes_source/recipes/reasoning_about_shapes", # reenable after 3326 is fixed "advanced_source/dynamic_quantization_tutorial", - "intermediate_source/custom_function_conv_bn_tutorial" + "intermediate_source/memory_format_tutorial" ] def tutorial_source_dirs() -> List[Path]: From 7dcb0fe066e50959854325ec568181bbb40c3911 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Wed, 30 Apr 2025 08:18:17 -0700 Subject: [PATCH 15/26] Update --- .jenkins/validate_tutorials_built.py | 1 - 1 file changed, 1 deletion(-) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index a023fd6886..21aaf9de5b 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -54,7 +54,6 @@ "advanced_source/coding_ddpg", # reenable after 3302 is fixed "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed "recipes_source/recipes/reasoning_about_shapes", # reenable after 3326 is fixed - "advanced_source/dynamic_quantization_tutorial", "intermediate_source/memory_format_tutorial" ] From bfd652e6768a544375ddbf78779b33c57ed82450 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Wed, 30 Apr 2025 08:27:27 -0700 Subject: [PATCH 16/26] Update --- .jenkins/validate_tutorials_built.py | 1 - conf.py | 8 ++++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index 21aaf9de5b..94fc921b8f 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -54,7 +54,6 @@ "advanced_source/coding_ddpg", # reenable after 3302 is fixed "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed "recipes_source/recipes/reasoning_about_shapes", # reenable after 3326 is fixed - "intermediate_source/memory_format_tutorial" ] def tutorial_source_dirs() -> List[Path]: diff --git a/conf.py b/conf.py index 842aa2b47f..fbe5de66eb 100644 --- a/conf.py +++ b/conf.py @@ -121,9 +121,13 @@ def reset_seeds(gallery_conf, fname): torch.set_grad_enabled(True) torch.set_default_dtype(torch.float32) torch.set_default_tensor_type(torch.FloatTensor) - import logging + + # Reset torch modules that might have been modified by tutorials + import importlib + importlib.reload(torch) + if 'torchvision' in sys.modules: + importlib.reload(torchvision) - logging.getLogger().setLevel(logging.WARNING) gc.collect() From 34b91c909c136070ac695fdf783fa28f7b8c2e4e Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Wed, 30 Apr 2025 08:41:06 -0700 Subject: [PATCH 17/26] Update --- conf.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/conf.py b/conf.py index fbe5de66eb..68a1db5929 100644 --- a/conf.py +++ b/conf.py @@ -122,11 +122,10 @@ def reset_seeds(gallery_conf, fname): torch.set_default_dtype(torch.float32) torch.set_default_tensor_type(torch.FloatTensor) - # Reset torch modules that might have been modified by tutorials - import importlib - importlib.reload(torch) - if 'torchvision' in sys.modules: - importlib.reload(torchvision) + # Clean up any global state without reloading + if hasattr(torch, "_C"): + if hasattr(torch._C, "_jit_clear_class_registry"): + torch._C._jit_clear_class_registry() gc.collect() From ffab05c5e290dca448a36a14362c103e1686881d Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Wed, 30 Apr 2025 09:15:12 -0700 Subject: [PATCH 18/26] Update --- conf.py | 219 +++++++++++++++-------------------- recipes_source/torch_logs.py | 30 +++-- 2 files changed, 106 insertions(+), 143 deletions(-) diff --git a/conf.py b/conf.py index 68a1db5929..7ca52a9e50 100644 --- a/conf.py +++ b/conf.py @@ -29,44 +29,34 @@ # import os import sys - -sys.path.insert(0, os.path.abspath(".")) -sys.path.insert(0, os.path.abspath("./.jenkins")) -import distutils.file_util +sys.path.insert(0, os.path.abspath('.')) +sys.path.insert(0, os.path.abspath('./.jenkins')) +import pytorch_sphinx_theme +import torch +import numpy import gc import glob import random -import re import shutil -from pathlib import Path - -import numpy +from custom_directives import IncludeDirective, GalleryItemDirective, CustomGalleryItemDirective, CustomCalloutItemDirective, CustomCardItemDirective +import distutils.file_util +import re +from get_sphinx_filenames import SPHINX_SHOULD_RUN import pandocfilters -import plotly.io as pio import pypandoc -import pytorch_sphinx_theme -import torch -from custom_directives import ( - CustomCalloutItemDirective, - CustomCardItemDirective, - CustomGalleryItemDirective, - GalleryItemDirective, - IncludeDirective, -) -from get_sphinx_filenames import SPHINX_SHOULD_RUN - -pio.renderers.default = "sphinx_gallery" +import plotly.io as pio +from pathlib import Path +pio.renderers.default = 'sphinx_gallery' try: import torchvision except ImportError: import warnings - warnings.warn('unable to load "torchvision" package') import pytorch_sphinx_theme -rst_epilog = """ +rst_epilog =""" .. |edit| image:: /_static/pencil-16.png :width: 16px :height: 16px @@ -79,21 +69,21 @@ # needs_sphinx = '1.0' html_meta = { - "description": "Master PyTorch with our step-by-step tutorials for all skill levels. Start your journey to becoming a PyTorch expert today!", - "keywords": "PyTorch, tutorials, Getting Started, deep learning, AI", - "author": "PyTorch Contributors", + 'description': 'Master PyTorch with our step-by-step tutorials for all skill levels. Start your journey to becoming a PyTorch expert today!', + 'keywords': 'PyTorch, tutorials, Getting Started, deep learning, AI', + 'author': 'PyTorch Contributors' } # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ - "sphinxcontrib.katex", - "sphinx.ext.intersphinx", - "sphinx_copybutton", - "sphinx_gallery.gen_gallery", - "sphinx_design", - "sphinx_sitemap", + 'sphinxcontrib.katex', + 'sphinx.ext.intersphinx', + 'sphinx_copybutton', + 'sphinx_gallery.gen_gallery', + 'sphinx_design', + 'sphinx_sitemap' ] intersphinx_mapping = { @@ -107,55 +97,45 @@ # -- Sphinx-gallery configuration -------------------------------------------- - def reset_seeds(gallery_conf, fname): torch.cuda.empty_cache() torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False torch._dynamo.reset() torch.manual_seed(42) - torch.cuda.manual_seed_all(42) torch.set_default_device(None) random.seed(10) numpy.random.seed(10) torch.set_grad_enabled(True) - torch.set_default_dtype(torch.float32) - torch.set_default_tensor_type(torch.FloatTensor) - # Clean up any global state without reloading - if hasattr(torch, "_C"): - if hasattr(torch._C, "_jit_clear_class_registry"): - torch._C._jit_clear_class_registry() + # Reset any patching from memory_format_tutorial + if hasattr(torch, "old_attrs"): + for (m, attrs) in torch.old_attrs.items(): + for (k, v) in attrs.items(): + setattr(m, k, v) + delattr(torch, "old_attrs") - gc.collect() + gc.collect() sphinx_gallery_conf = { - "examples_dirs": [ - "beginner_source", - "intermediate_source", - "advanced_source", - "recipes_source", - "prototype_source", - ], - "gallery_dirs": ["beginner", "intermediate", "advanced", "recipes", "prototype"], - "filename_pattern": re.compile(SPHINX_SHOULD_RUN), - "promote_jupyter_magic": True, - "backreferences_dir": None, - "first_notebook_cell": ( - "# For tips on running notebooks in Google Colab, see\n" - "# https://pytorch.org/tutorials/beginner/colab\n" - "%matplotlib inline" - ), - "reset_modules": (reset_seeds), - "ignore_pattern": r"_torch_export_nightly_tutorial.py", - "pypandoc": { - "extra_args": ["--mathjax", "--toc"], - "filters": [".jenkins/custom_pandoc_filter.py"], + 'examples_dirs': ['beginner_source', 'intermediate_source', + 'advanced_source', 'recipes_source', 'prototype_source'], + 'gallery_dirs': ['beginner', 'intermediate', 'advanced', 'recipes', 'prototype'], + 'filename_pattern': re.compile(SPHINX_SHOULD_RUN), + 'promote_jupyter_magic': True, + 'backreferences_dir': None, + 'first_notebook_cell': ("# For tips on running notebooks in Google Colab, see\n" + "# https://pytorch.org/tutorials/beginner/colab\n" + "%matplotlib inline"), + 'reset_modules': (reset_seeds), + 'ignore_pattern': r'_torch_export_nightly_tutorial.py', + 'pypandoc': {'extra_args': ['--mathjax', '--toc'], + 'filters': ['.jenkins/custom_pandoc_filter.py'], }, } -html_baseurl = "https://pytorch.org/tutorials/" # needed for sphinx-sitemap +html_baseurl = 'https://pytorch.org/tutorials/' # needed for sphinx-sitemap sitemap_locales = [None] sitemap_excludes = [ "search.html", @@ -163,7 +143,7 @@ def reset_seeds(gallery_conf, fname): ] sitemap_url_scheme = "{link}" -if os.getenv("GALLERY_PATTERN"): +if os.getenv('GALLERY_PATTERN'): # GALLERY_PATTERN is to be used when you want to work on a single # tutorial. Previously this was fed into filename_pattern, but # if you do that, you still end up parsing all of the other Python @@ -171,11 +151,9 @@ def reset_seeds(gallery_conf, fname): # ignore_pattern also skips parsing. # See https://github.com/sphinx-gallery/sphinx-gallery/issues/721 # for a more detailed description of the issue. - sphinx_gallery_conf["ignore_pattern"] = ( - r"/(?!" + re.escape(os.getenv("GALLERY_PATTERN")) + r")[^/]+$" - ) + sphinx_gallery_conf['ignore_pattern'] = r'/(?!' + re.escape(os.getenv('GALLERY_PATTERN')) + r')[^/]+$' -for i in range(len(sphinx_gallery_conf["examples_dirs"])): +for i in range(len(sphinx_gallery_conf['examples_dirs'])): gallery_dir = Path(sphinx_gallery_conf["gallery_dirs"][i]) source_dir = Path(sphinx_gallery_conf["examples_dirs"][i]) @@ -187,21 +165,21 @@ def reset_seeds(gallery_conf, fname): distutils.file_util.copy_file(f, gallery_subdir_path, update=True) # Add any paths that contain templates here, relative to this directory. -templates_path = ["_templates"] +templates_path = ['_templates'] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # # source_suffix = ['.rst', '.md'] -source_suffix = ".rst" +source_suffix = '.rst' # The master toctree document. -master_doc = "index" +master_doc = 'index' # General information about the project. -project = "PyTorch Tutorials" -copyright = "2024, PyTorch" -author = "PyTorch contributors" +project = 'PyTorch Tutorials' +copyright = '2024, PyTorch' +author = 'PyTorch contributors' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -217,22 +195,17 @@ def reset_seeds(gallery_conf, fname): # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. -language = "en" +language = 'en' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This patterns also effect to html_static_path and html_extra_path -exclude_patterns = [ - "_build", - "Thumbs.db", - ".DS_Store", - "src/pytorch-sphinx-theme/docs*", -] -exclude_patterns += sphinx_gallery_conf["examples_dirs"] -exclude_patterns += ["*/index.rst"] +exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', 'src/pytorch-sphinx-theme/docs*'] +exclude_patterns += sphinx_gallery_conf['examples_dirs'] +exclude_patterns += ['*/index.rst'] # The name of the Pygments (syntax highlighting) style to use. -pygments_style = "sphinx" +pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = False @@ -260,7 +233,7 @@ def reset_seeds(gallery_conf, fname): # # Add any paths that contain custom static files (such as style sheets) here, # # relative to this directory. They are copied after the builtin static files, # # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ["_static"] +html_static_path = ['_static'] # # Custom sidebar templates, maps document names to template names. # html_sidebars = { @@ -269,23 +242,23 @@ def reset_seeds(gallery_conf, fname): # } -html_theme = "pytorch_sphinx_theme" +html_theme = 'pytorch_sphinx_theme' html_theme_path = [pytorch_sphinx_theme.get_html_theme_path()] -html_logo = "_static/img/pytorch-logo-dark.svg" +html_logo = '_static/img/pytorch-logo-dark.svg' html_theme_options = { - "pytorch_project": "tutorials", - "collapse_navigation": False, - "display_version": True, - "navigation_with_keys": True, - "logo_only": False, - "analytics_id": "GTM-T8XT4PS", + 'pytorch_project': 'tutorials', + 'collapse_navigation': False, + 'display_version': True, + 'navigation_with_keys': True, + 'logo_only': False, + 'analytics_id': 'GTM-T8XT4PS', } # -- Options for HTMLHelp output ------------------------------------------ # Output file base name for HTML help builder. -htmlhelp_basename = "PyTorchTutorialsdoc" +htmlhelp_basename = 'PyTorchTutorialsdoc' # -- Options for LaTeX output --------------------------------------------- @@ -294,12 +267,15 @@ def reset_seeds(gallery_conf, fname): # The paper size ('letterpaper' or 'a4paper'). # # 'papersize': 'letterpaper', + # The font size ('10pt', '11pt' or '12pt'). # # 'pointsize': '10pt', + # Additional stuff for the LaTeX preamble. # # 'preamble': '', + # Latex figure (float) alignment # # 'figure_align': 'htbp', @@ -309,13 +285,8 @@ def reset_seeds(gallery_conf, fname): # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - ( - master_doc, - "PyTorchTutorials.tex", - "PyTorch Tutorials", - "Sasank, PyTorch contributors", - "manual", - ), + (master_doc, 'PyTorchTutorials.tex', 'PyTorch Tutorials', + 'Sasank, PyTorch contributors', 'manual'), ] @@ -323,7 +294,10 @@ def reset_seeds(gallery_conf, fname): # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). -man_pages = [(master_doc, "pytorchtutorials", "PyTorch Tutorials", [author], 1)] +man_pages = [ + (master_doc, 'pytorchtutorials', 'PyTorch Tutorials', + [author], 1) +] # -- Options for Texinfo output ------------------------------------------- @@ -332,47 +306,40 @@ def reset_seeds(gallery_conf, fname): # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ( - master_doc, - "PyTorchTutorials", - "PyTorch Tutorials", - author, - "PyTorchTutorials", - "One line description of project.", - "Miscellaneous", - ), + (master_doc, 'PyTorchTutorials', 'PyTorch Tutorials', + author, 'PyTorchTutorials', 'One line description of project.', + 'Miscellaneous'), ] html_css_files = [ - "https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css", - "css/custom.css", - "css/custom2.css", -] + 'https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css', + 'css/custom.css', + 'css/custom2.css' + ] html_js_files = [ "js/custom.js", ] - def setup(app): # NOTE: in Sphinx 1.8+ `html_css_files` is an official configuration value # and can be moved outside of this function (and the setup(app) function # can be deleted). - # html_css_files = [ + #html_css_files = [ # 'https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css', # 'css/custom.css' - # ] + #] # In Sphinx 1.8 it was renamed to `add_css_file`, 1.7 and prior it is # `add_stylesheet` (deprecated in 1.8). - # add_css = getattr(app, 'add_css_file', app.add_stylesheet) - # for css_file in html_css_files: + #add_css = getattr(app, 'add_css_file', app.add_stylesheet) + #for css_file in html_css_files: # add_css(css_file) # Custom CSS - # app.add_stylesheet('css/pytorch_theme.css') + #app.add_stylesheet('css/pytorch_theme.css') # app.add_stylesheet('https://fonts.googleapis.com/css?family=Lato') # Custom directives - app.add_directive("includenodoc", IncludeDirective) - app.add_directive("galleryitem", GalleryItemDirective) - app.add_directive("customgalleryitem", CustomGalleryItemDirective) - app.add_directive("customcarditem", CustomCardItemDirective) - app.add_directive("customcalloutitem", CustomCalloutItemDirective) + app.add_directive('includenodoc', IncludeDirective) + app.add_directive('galleryitem', GalleryItemDirective) + app.add_directive('customgalleryitem', CustomGalleryItemDirective) + app.add_directive('customcarditem', CustomCardItemDirective) + app.add_directive('customcalloutitem', CustomCalloutItemDirective) diff --git a/recipes_source/torch_logs.py b/recipes_source/torch_logs.py index 14f33853e0..b5c3f0bd8a 100644 --- a/recipes_source/torch_logs.py +++ b/recipes_source/torch_logs.py @@ -32,51 +32,47 @@ import torch -# Reset torch dynamo and empty CUDA cache before starting -torch._dynamo.reset() -if torch.cuda.is_available(): - torch.cuda.empty_cache() - - # exit cleanly if we are on a device that doesn't support torch.compile if torch.cuda.get_device_capability() < (7, 0): print("Skipping because torch.compile is not supported on this device.") else: - @torch.compile() def fn(x, y): z = x + y return z + 2 + inputs = (torch.ones(2, 2, device="cuda"), torch.zeros(2, 2, device="cuda")) - # print separator and reset dynamo - # between each example + +# print separator and reset dynamo +# between each example def separator(name): print(f"==================={name}=========================") torch._dynamo.reset() + separator("Dynamo Tracing") - # View dynamo tracing - # TORCH_LOGS="+dynamo" +# View dynamo tracing +# TORCH_LOGS="+dynamo" torch._logging.set_logs(dynamo=logging.DEBUG) fn(*inputs) separator("Traced Graph") - # View traced graph - # TORCH_LOGS="graph" +# View traced graph +# TORCH_LOGS="graph" torch._logging.set_logs(graph=True) fn(*inputs) separator("Fusion Decisions") - # View fusion decisions - # TORCH_LOGS="fusion" +# View fusion decisions +# TORCH_LOGS="fusion" torch._logging.set_logs(fusion=True) fn(*inputs) separator("Output Code") - # View output code generated by inductor - # TORCH_LOGS="output_code" +# View output code generated by inductor +# TORCH_LOGS="output_code" torch._logging.set_logs(output_code=True) fn(*inputs) From c7b3aee2be7c4dd6de6b360e9d0f28065eec34a5 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Wed, 30 Apr 2025 09:45:53 -0700 Subject: [PATCH 19/26] Update --- conf.py | 7 ------- intermediate_source/memory_format_tutorial.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/conf.py b/conf.py index 7ca52a9e50..3606f674a8 100644 --- a/conf.py +++ b/conf.py @@ -107,13 +107,6 @@ def reset_seeds(gallery_conf, fname): random.seed(10) numpy.random.seed(10) torch.set_grad_enabled(True) - - # Reset any patching from memory_format_tutorial - if hasattr(torch, "old_attrs"): - for (m, attrs) in torch.old_attrs.items(): - for (k, v) in attrs.items(): - setattr(m, k, v) - delattr(torch, "old_attrs") gc.collect() diff --git a/intermediate_source/memory_format_tutorial.py b/intermediate_source/memory_format_tutorial.py index 26bc5c9d53..4267cd2e21 100644 --- a/intermediate_source/memory_format_tutorial.py +++ b/intermediate_source/memory_format_tutorial.py @@ -376,6 +376,20 @@ def attribute(m): for (k, v) in attrs.items(): setattr(m, k, v) +# Recover Tensor +torch.Tensor = old_attrs[torch.Tensor]["Tensor"] +# Recover nn.functional +torch.nn.functional = old_attrs[torch.nn.functional]["nn.functional"] +# Recover torch +torch.is_cuda = old_attrs[torch]["is_cuda"] +torch.has_names = old_attrs[torch]["has_names"] +torch.numel = old_attrs[torch]["numel"] +torch.stride = old_attrs[torch]["stride"] +torch.is_contiguous = old_attrs[torch]["is_contiguous"] +torch.__class__ = old_attrs[torch]["__class__"] +del old_attrs + + ###################################################################### # Work to do # ---------- From 91b93f1735469ee07cf49340ce332d6b9af4e602 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Wed, 30 Apr 2025 10:18:10 -0700 Subject: [PATCH 20/26] Update --- intermediate_source/memory_format_tutorial.py | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/intermediate_source/memory_format_tutorial.py b/intermediate_source/memory_format_tutorial.py index 4267cd2e21..23c512b62d 100644 --- a/intermediate_source/memory_format_tutorial.py +++ b/intermediate_source/memory_format_tutorial.py @@ -376,19 +376,23 @@ def attribute(m): for (k, v) in attrs.items(): setattr(m, k, v) -# Recover Tensor +import gc +import sys + torch.Tensor = old_attrs[torch.Tensor]["Tensor"] -# Recover nn.functional -torch.nn.functional = old_attrs[torch.nn.functional]["nn.functional"] -# Recover torch -torch.is_cuda = old_attrs[torch]["is_cuda"] -torch.has_names = old_attrs[torch]["has_names"] -torch.numel = old_attrs[torch]["numel"] -torch.stride = old_attrs[torch]["stride"] -torch.is_contiguous = old_attrs[torch]["is_contiguous"] -torch.__class__ = old_attrs[torch]["__class__"] +torch.nn.functional = old_attrs[torch.nn.functional] +for attr_name in ["is_cuda", "has_names", "numel", "stride", "is_contiguous", "__class__"]: + if attr_name in old_attrs[torch]: + setattr(torch, attr_name, old_attrs[torch][attr_name]) + +# Force garbage collection del old_attrs +gc.collect() +# Reset torch modules +torch._dynamo.reset() +if torch.cuda.is_available(): + torch.cuda.empty_cache() ###################################################################### # Work to do From f3bc0ca3df13454ae49487bef3611d6e03766417 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Wed, 30 Apr 2025 11:31:40 -0700 Subject: [PATCH 21/26] Update --- conf.py | 2 +- intermediate_source/memory_format_tutorial.py | 13 +++---------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/conf.py b/conf.py index 3606f674a8..05cfa11ca1 100644 --- a/conf.py +++ b/conf.py @@ -102,13 +102,13 @@ def reset_seeds(gallery_conf, fname): torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False torch._dynamo.reset() + torch._inductor.config.force_disable_caches = True torch.manual_seed(42) torch.set_default_device(None) random.seed(10) numpy.random.seed(10) torch.set_grad_enabled(True) - gc.collect() sphinx_gallery_conf = { diff --git a/intermediate_source/memory_format_tutorial.py b/intermediate_source/memory_format_tutorial.py index 23c512b62d..ab06eeb583 100644 --- a/intermediate_source/memory_format_tutorial.py +++ b/intermediate_source/memory_format_tutorial.py @@ -379,17 +379,10 @@ def attribute(m): import gc import sys -torch.Tensor = old_attrs[torch.Tensor]["Tensor"] -torch.nn.functional = old_attrs[torch.nn.functional] -for attr_name in ["is_cuda", "has_names", "numel", "stride", "is_contiguous", "__class__"]: - if attr_name in old_attrs[torch]: - setattr(torch, attr_name, old_attrs[torch][attr_name]) +if torch.nn.functional in old_attrs: + for k, v in old_attrs[torch.nn.functional].items(): + setattr(torch.nn.functional, k, v) -# Force garbage collection -del old_attrs -gc.collect() - -# Reset torch modules torch._dynamo.reset() if torch.cuda.is_available(): torch.cuda.empty_cache() From fe0bf0035d1c2d94dc843b8ef2a0ebc0747bffd8 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Wed, 30 Apr 2025 12:14:46 -0700 Subject: [PATCH 22/26] Update --- intermediate_source/memory_format_tutorial.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/intermediate_source/memory_format_tutorial.py b/intermediate_source/memory_format_tutorial.py index ab06eeb583..460650200a 100644 --- a/intermediate_source/memory_format_tutorial.py +++ b/intermediate_source/memory_format_tutorial.py @@ -379,14 +379,17 @@ def attribute(m): import gc import sys -if torch.nn.functional in old_attrs: - for k, v in old_attrs[torch.nn.functional].items(): - setattr(torch.nn.functional, k, v) - torch._dynamo.reset() if torch.cuda.is_available(): torch.cuda.empty_cache() +gc.collect() + +if hasattr(torch, "_check_wrapper_patched"): + delattr(torch, "_check_wrapper_patched") + +old_attrs.clear() + ###################################################################### # Work to do # ---------- From 4cb2f59b55b9f6e4ff59a591d9898d99f589f034 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Wed, 30 Apr 2025 12:52:00 -0700 Subject: [PATCH 23/26] Update --- intermediate_source/memory_format_tutorial.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/intermediate_source/memory_format_tutorial.py b/intermediate_source/memory_format_tutorial.py index 460650200a..4ed971b91a 100644 --- a/intermediate_source/memory_format_tutorial.py +++ b/intermediate_source/memory_format_tutorial.py @@ -385,10 +385,12 @@ def attribute(m): gc.collect() -if hasattr(torch, "_check_wrapper_patched"): - delattr(torch, "_check_wrapper_patched") - -old_attrs.clear() +# Clear any references to the wrapper functions +del old_attrs +del contains_cl +del print_inputs +del check_wrapper +del attribute ###################################################################### # Work to do From 27295ea0fadd337c35785fd85164554f4a74ca77 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Wed, 30 Apr 2025 20:22:46 -0700 Subject: [PATCH 24/26] Update --- .jenkins/validate_tutorials_built.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index 94fc921b8f..1a1420f52e 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -50,10 +50,8 @@ "intermediate_source/flask_rest_api_tutorial", "intermediate_source/text_to_speech_with_torchaudio", "intermediate_source/tensorboard_profiler_tutorial", # reenable after 2.0 release. - "advanced_source/semi_structured_sparse", # reenable after 3303 is fixed. - "advanced_source/coding_ddpg", # reenable after 3302 is fixed - "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixed - "recipes_source/recipes/reasoning_about_shapes", # reenable after 3326 is fixed + "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixe + "intermediate_source/memory_format_tutorial" ] def tutorial_source_dirs() -> List[Path]: From 11cc26c1df4ea81fd41de6d9bc96c61b0eb88377 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Thu, 1 May 2025 10:44:42 -0700 Subject: [PATCH 25/26] Update --- .jenkins/validate_tutorials_built.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.jenkins/validate_tutorials_built.py b/.jenkins/validate_tutorials_built.py index 1a1420f52e..f4586c19b3 100644 --- a/.jenkins/validate_tutorials_built.py +++ b/.jenkins/validate_tutorials_built.py @@ -50,8 +50,9 @@ "intermediate_source/flask_rest_api_tutorial", "intermediate_source/text_to_speech_with_torchaudio", "intermediate_source/tensorboard_profiler_tutorial", # reenable after 2.0 release. + "advanced_source/semi_structured_sparse", # reenable after 3303 is fixed. "intermediate_source/torchrec_intro_tutorial", # reenable after 3302 is fixe - "intermediate_source/memory_format_tutorial" + "intermediate_source/memory_format_tutorial", # causes other tutorials like torch_logs fail. "state" issue, reseting dynamo didn't help ] def tutorial_source_dirs() -> List[Path]: From 1f9c4ceb08ecd3d545d881b29ba17cec3e3e86f6 Mon Sep 17 00:00:00 2001 From: Svetlana Karslioglu Date: Thu, 1 May 2025 12:00:18 -0700 Subject: [PATCH 26/26] Update --- .jenkins/metadata.json | 3 --- intermediate_source/memory_format_tutorial.py | 16 ---------------- 2 files changed, 19 deletions(-) diff --git a/.jenkins/metadata.json b/.jenkins/metadata.json index 0514266bd6..6e82d054b4 100644 --- a/.jenkins/metadata.json +++ b/.jenkins/metadata.json @@ -1,7 +1,4 @@ { - "recipes_source/torch_logs.py": { - "duration": 0 - }, "intermediate_source/ax_multiobjective_nas_tutorial.py": { "extra_files": ["intermediate_source/mnist_train_nas.py"], "duration": 2000 diff --git a/intermediate_source/memory_format_tutorial.py b/intermediate_source/memory_format_tutorial.py index 4ed971b91a..26bc5c9d53 100644 --- a/intermediate_source/memory_format_tutorial.py +++ b/intermediate_source/memory_format_tutorial.py @@ -376,22 +376,6 @@ def attribute(m): for (k, v) in attrs.items(): setattr(m, k, v) -import gc -import sys - -torch._dynamo.reset() -if torch.cuda.is_available(): - torch.cuda.empty_cache() - -gc.collect() - -# Clear any references to the wrapper functions -del old_attrs -del contains_cl -del print_inputs -del check_wrapper -del attribute - ###################################################################### # Work to do # ----------