diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index a50ed3200..78fd09634 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -10,7 +10,7 @@ on: jobs: build-docs: name: Build Documentation - runs-on: ubuntu-latest + runs-on: linux.g5.4xlarge.nvidia.gpu timeout-minutes: 30 steps: - name: Checkout @@ -24,7 +24,7 @@ jobs: miniconda-version: "latest" activate-environment: test python-version: '3.10' - auto-activate-base: false + auto-activate: false - name: Verify conda environment shell: bash -l {0} run: | @@ -36,7 +36,7 @@ jobs: run: python -m pip install --upgrade pip - name: Install pytorch shell: bash -l {0} - run: python -m pip install torch==2.9.0.dev20250826 --extra-index-url https://download.pytorch.org/whl/nightly/cpu + run: python -m pip install torch==2.9.0 --index-url https://download.pytorch.org/whl/test/cu130 - name: Install monarch shell: bash -l {0} run: python -m pip install monarch-no-torch==0.1.0.dev20250826 --find-links assets/ci @@ -51,61 +51,90 @@ jobs: - name: Build docs shell: bash -l {0} working-directory: docs - run: make html --keep-going SPHINXOPTS='-W' + run: | + set +e # Don't exit on error + make html SPHINXOPTS="-WT --keep-going" || echo "Build completed with warnings/errors" + set -e # Re-enable exit on error for subsequent commands - name: Upload docs artifact uses: actions/upload-artifact@v4 with: name: docs path: docs/build/html/ - # doc-preview: - # runs-on: [ubuntu-latest] - # needs: build-docs - # if: ${{ github.event_name == 'pull_request' }} - # steps: - # - name: Checkout - # uses: actions/checkout@v4 - # - name: Download artifact - # uses: actions/download-artifact@v4 - # with: - # name: docs - # path: docs - # - name: Add noindex to preview docs - # run: | - # echo "Adding noindex meta tag to prevent search engine indexing of preview docs" - # find docs -name "*.html" -print0 | xargs -0 sed -i 's/
/\n /' - # - name: Upload docs preview - # uses: seemethere/upload-artifact-s3@v5 - # if: ${{ github.event_name == 'pull_request' }} - # with: - # retention-days: 14 - # s3-bucket: doc-previews - # if-no-files-found: error - # path: docs - # s3-prefix: meta-pytorch/forge/${{ github.event.pull_request.number }} + doc-preview: + runs-on: linux.large + needs: build-docs + if: ${{ github.event_name == 'pull_request' }} + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Download artifact + uses: actions/download-artifact@v4 + with: + name: docs + path: docs + - name: Add noindex to preview docs + run: | + echo "Adding noindex meta tag to prevent search engine indexing of preview docs" + find docs -name "*.html" -print0 | xargs -0 sed -i 's//\n /' + - name: Upload docs preview + uses: seemethere/upload-artifact-s3@v5 + if: ${{ github.event_name == 'pull_request' }} + with: + retention-days: 14 + s3-bucket: doc-previews + if-no-files-found: error + path: docs + s3-prefix: meta-pytorch/forge/${{ github.event.pull_request.number }} + + upload: + runs-on: ubuntu-latest + permissions: + # Grant write permission here so that the doc can be pushed to gh-pages branch + contents: write + needs: build-docs + if: github.repository == 'meta-pytorch/forge' && github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch') + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + ref: gh-pages + persist-credentials: true + - name: Download artifact + uses: actions/download-artifact@v4 + with: + name: docs + path: docs + #- name: Add no-index tag + # run: | + # REF_NAME=$(echo "${{ github.ref }}") + # echo "Ref name: ${REF_NAME}" + # if [[ "${{ github.ref }}" == 'refs/heads/main' ]]; then + # find docs -name "*.html" -print0 | xargs -0 sed -i '//a \ \ '; + # fi + - name: Move and commit changes + run: | + set -euo pipefail + # Get github.ref for the output doc folder. By default "main" + # If matches a tag like refs/tags/v1.12.0-rc3 or + # refs/tags/v1.12.0 convert to 1.12 + GITHUB_REF=${{ github.ref }} - deploy-docs: - needs: build-docs - if: github.ref == 'refs/heads/main' - permissions: - pages: write - id-token: write - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} - runs-on: ubuntu-latest - steps: - - name: Download build artifact - uses: actions/download-artifact@v4 - with: - name: docs - path: . + # Convert refs/tags/v1.12.0rc3 into 1.12. + # Adopted from https://github.com/pytorch/pytorch/blob/main/.github/workflows/_docs.yml#L150C11-L155C13 + if [[ "${GITHUB_REF}" =~ ^refs/tags/v([0-9]+\.[0-9]+)\.* ]]; then + TARGET_FOLDER="${BASH_REMATCH[1]}" + else + TARGET_FOLDER="main" + fi + echo "Target Folder: ${TARGET_FOLDER}" - - name: Upload Pages artifact - uses: actions/upload-pages-artifact@v3 - with: - path: . + mkdir -p "${TARGET_FOLDER}" + rm -rf "${TARGET_FOLDER}"/* + mv docs/* "${TARGET_FOLDER}" - - name: Deploy to GitHub Pages - id: deployment - uses: actions/deploy-pages@v4 + git config user.name 'pytorchbot' + git config user.email 'soumith+bot@pytorch.org' + git add "${TARGET_FOLDER}" || true + git commit -m "auto-generating sphinx docs" || true + git push -f diff --git a/.gitignore b/.gitignore index 14e5f66e1..3f43562a8 100644 --- a/.gitignore +++ b/.gitignore @@ -151,6 +151,7 @@ docs/source/generated_examples/ docs/source/gen_modules/ docs/source/generated/ docs/source/sg_execution_times.rst +docs/source/tutorials # pytorch-sphinx-theme gets installed here docs/src diff --git a/docs/requirements.txt b/docs/requirements.txt index 87c6f43da..8846bc62e 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,7 +1,8 @@ sphinx==7.2.6 --e git+https://github.com/pytorch/pytorch_sphinx_theme.git@pytorch_sphinx_theme2#egg=pytorch_sphinx_theme2 +pytorch-sphinx-theme2==0.1.0 docutils>=0.18.1,<0.21 sphinx-design==0.6.1 sphinxcontrib-mermaid==1.0.0 +sphinx-gallery==0.19.0 myst-parser #==0.18.1 # if want to contribute in markdown sphinx-sitemap==2.7.1 diff --git a/docs/source/conf.py b/docs/source/conf.py index 69b3c20b5..760a8d714 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -17,10 +17,33 @@ import pytorch_sphinx_theme2 # Add the source directory to Python path so modules can be imported -sys.path.insert(0, os.path.abspath("../../src")) +sys.path.insert(0, os.path.abspath("../../src/forge")) + + +# Determine the version path for deployment +def get_version_path(): + """Get the version path based on environment variables or git context.""" + # Check if we're in CI/CD and get the target folder + github_ref = os.environ.get("GITHUB_REF", "") + + # Convert refs/tags/v1.12.0rc3 into 1.12. + # Matches the logic in .github/workflows/docs.yml + if github_ref.startswith("refs/tags/v"): + import re + + match = re.match(r"^refs/tags/v([0-9]+\.[0-9]+)\..*", github_ref) + if match: + return match.group(1) + "/" + + # Default to main for main branch or local development + return "main/" + + +# Set base URL based on deployment context +version_path = get_version_path() project = "torchforge" -copyright = "2025, PyTorch Contributors" +copyright = "" author = "PyTorch Contributors" release = "0.1" @@ -38,9 +61,12 @@ "sphinx.ext.napoleon", "sphinx.ext.intersphinx", "sphinx.ext.viewcode", + "sphinx_gallery.gen_gallery", ] -html_baseurl = "https://meta-pytorch.org/forge/" # needed for sphinx-sitemap +html_baseurl = ( + f"https://meta-pytorch.org/forge/{version_path}" # needed for sphinx-sitemap +) sitemap_locales = [None] sitemap_excludes = [ "search.html", @@ -52,7 +78,7 @@ "_templates", os.path.join(os.path.dirname(pytorch_sphinx_theme2.__file__), "templates"), ] -exclude_patterns = [] +exclude_patterns = ["tutorials/index.rst"] sys.path.insert(0, os.path.abspath(".")) sys.path.insert(0, os.path.abspath("../../src")) @@ -90,7 +116,7 @@ }, { "name": "PyPi", - "url": "https://pypi.org/project/forge/", + "url": "https://pypi.org/project/torchforge/", "icon": "fa-brands fa-python", }, ], @@ -109,9 +135,21 @@ "github_user": "meta-pytorch", "github_repo": "forge", "feedback_url": "https://github.com/meta-pytorch/forge", + "colab_branch": "gh-pages", "github_version": "main", "doc_path": "docs/source", + "has_sphinx_gallery": True, # Enable tutorial call-to-action links +} + +# For tutorial repository configuration +# Note: github_user and github_repo are combined in the template as "{{ github_user }}/{{ github_repo }}" +# So we keep github_user = "meta-pytorch" and github_repo = "forge" already set above +# and only need to ensure the branch settings are correct +tutorial_repo_config = { + "github_version": "main", # This maps to github_branch in the template + "colab_branch": "gh-pages", } +html_context.update(tutorial_repo_config) myst_enable_extensions = [ "colon_fence", @@ -127,6 +165,17 @@ "exclude-members": "__weakref__", } -# Autosummary settings -autosummary_generate = True -autosummary_imported_members = True + +# -- Sphinx Gallery configuration ------------------------------------------- +sphinx_gallery_conf = { + "examples_dirs": "tutorial_sources", # Path to examples directory + "gallery_dirs": "tutorials", # Path to generate gallery + "filename_pattern": ".*", # Include all files + "download_all_examples": False, + "first_notebook_cell": "%matplotlib inline", + "plot_gallery": "True", + "promote_jupyter_magic": True, + "backreferences_dir": None, + "write_computation_times": True, + "show_signature": False, +} diff --git a/docs/source/tutorial_sources/README.txt b/docs/source/tutorial_sources/README.txt new file mode 100644 index 000000000..1fadb0a08 --- /dev/null +++ b/docs/source/tutorial_sources/README.txt @@ -0,0 +1,5 @@ +Tutorials +========= + +This gallery contains tutorials and examples to help you get started with Forge. +Each tutorial demonstrates specific features and use cases with practical examples. diff --git a/docs/source/tutorial_sources/template_tutorial.py b/docs/source/tutorial_sources/template_tutorial.py new file mode 100644 index 000000000..4018aa1b1 --- /dev/null +++ b/docs/source/tutorial_sources/template_tutorial.py @@ -0,0 +1,91 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +""" +Template Tutorial +================= + +**Author:** `FirstName LastName