|
1 | | -# Copyright 2023–2025 Google LLC |
| 1 | +# Copyright 2023–2026 Google LLC |
2 | 2 | # |
3 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | # you may not use this file except in compliance with the License. |
|
25 | 25 | # -- Project information ----------------------------------------------------- |
26 | 26 | # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information |
27 | 27 |
|
| 28 | +import os |
| 29 | +import os.path |
| 30 | +import sys |
| 31 | +import warnings |
| 32 | + |
| 33 | +# Prevent JAX/Torch/TF from trying to access TPU/GPU during doc build |
| 34 | +os.environ["JAX_PLATFORMS"] = "cpu" |
| 35 | +os.environ["CUDA_VISIBLE_DEVICES"] = "" |
| 36 | +MAXTEXT_REPO_ROOT = os.environ.get("MAXTEXT_REPO_ROOT", os.path.join(os.path.dirname(os.path.dirname(__file__)))) |
| 37 | +sys.path.insert(0, os.path.abspath(os.path.join(MAXTEXT_REPO_ROOT, "test"))) |
| 38 | +sys.path.insert(0, os.path.abspath(os.path.join(MAXTEXT_REPO_ROOT, "src"))) |
| 39 | + |
| 40 | +warnings.filterwarnings("ignore", category=FutureWarning, module="keras.src.export.tf2onnx_lib") |
| 41 | + |
28 | 42 | project = "MaxText" |
29 | 43 | # pylint: disable=redefined-builtin |
30 | | -copyright = "2025, Google LLC" |
| 44 | +copyright = "2023–2026, Google LLC" |
31 | 45 | author = "MaxText developers" |
32 | 46 |
|
33 | 47 | # -- General configuration --------------------------------------------------- |
|
37 | 51 | "myst_nb", |
38 | 52 | "sphinx_design", |
39 | 53 | "sphinx_copybutton", |
| 54 | + "sphinx.ext.napoleon", |
| 55 | + # This needs to be before autodoc^ |
| 56 | + "sphinx.ext.autodoc", |
| 57 | + "sphinx.ext.autosummary", |
| 58 | + "sphinx.ext.viewcode", |
40 | 59 | ] |
41 | 60 |
|
42 | 61 | templates_path = ["_templates"] |
43 | 62 | source_suffix = [".rst", ".ipynb", ".md"] |
44 | 63 |
|
| 64 | +# Suppress specific warnings |
| 65 | +suppress_warnings = [ |
| 66 | + "app.add_node", |
| 67 | + "ref.python", |
| 68 | + "myst.xref_ambiguous", |
| 69 | + "docutils", |
| 70 | + "autodoc", |
| 71 | + "autodoc.duplicate_object_description", |
| 72 | + "toc.not_included", |
| 73 | +] |
| 74 | + |
45 | 75 | # -- Options for HTML output ------------------------------------------------- |
46 | 76 | # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output |
47 | 77 |
|
|
60 | 90 | ] |
61 | 91 | myst_linkify_fuzzy_links = False |
62 | 92 |
|
| 93 | +# -- Options for autodoc ---------------------------------------------------- |
| 94 | +autodoc_member_order = "bysource" |
| 95 | +autodoc_typehints = "description" |
| 96 | +autodoc_mock_imports = [ |
| 97 | + "cloud_tpu_diagnostics", |
| 98 | + "google_cloud_mldiagnostics", |
| 99 | + "jetstream", |
| 100 | + "ml_goodput_measurement", |
| 101 | + "pathwaysutils", |
| 102 | + "safetensors", |
| 103 | + "tensorflow_datasets", |
| 104 | + "torch", |
| 105 | + "tpu_inference", |
| 106 | + "transformer_engine", |
| 107 | + "vllm", |
| 108 | +] |
| 109 | +autosummary_generate = True |
| 110 | + |
63 | 111 | # Theme-specific options |
64 | 112 | # https://sphinx-book-theme.readthedocs.io/en/stable/reference.html |
65 | 113 | html_theme_options = { |
|
77 | 125 |
|
78 | 126 | # Remove specific documents from ToC |
79 | 127 | exclude_patterns = [ |
80 | | - "run_maxtext/run_maxtext_via_multihost_job.md", |
81 | | - "run_maxtext/run_maxtext_via_multihost_runner.md", |
82 | | - "reference/core_concepts/llm_calculator.ipynb", |
| 128 | + os.path.join("guides", "run_maxtext", "run_maxtext_via_multihost_job.md"), |
| 129 | + os.path.join("guides", "run_maxtext", "run_maxtext_via_multihost_runner.md"), |
| 130 | + os.path.join("explanations", "llm_calculator.ipynb"), |
| 131 | + os.path.join("reference", "api.rst"), |
| 132 | + os.path.join("run_maxtext", "run_maxtext_via_multihost_job.md"), |
| 133 | + os.path.join("run_maxtext", "run_maxtext_via_multihost_runner.md"), |
| 134 | + os.path.join("reference", "core_concepts", "llm_calculator.ipynb"), |
83 | 135 | ] |
| 136 | + |
| 137 | + |
| 138 | +# -- Autogenerate API documentation ------------------------------------------ |
| 139 | +def run_apidoc(_): |
| 140 | + """Runs sphinx-apidoc to generate API documentation. |
| 141 | +
|
| 142 | + This function is connected to the Sphinx build process and is triggered to |
| 143 | + automatically generate the reStructuredText (RST) files for the API |
| 144 | + documentation from the docstrings in the MaxText source code. |
| 145 | +
|
| 146 | + Args: |
| 147 | + _: The Sphinx application object. Not used. |
| 148 | + """ |
| 149 | + # directly within the Sphinx process, especially on macOS, as it avoids |
| 150 | + # potential multiprocessing/forking issues like the "mutex lock failed" error. |
| 151 | + # pylint: disable=import-outside-toplevel |
| 152 | + import subprocess |
| 153 | + |
| 154 | + os.environ["OBJC_DISABLE_INITIALIZE_FORK_SAFETY"] = "1" |
| 155 | + |
| 156 | + assert os.path.isfile(os.path.join(MAXTEXT_REPO_ROOT, "pyproject.toml")) |
| 157 | + |
| 158 | + # The path where the generated RST files will be stored |
| 159 | + output_path = os.path.join(MAXTEXT_REPO_ROOT, "docs", "reference", "api_generated") |
| 160 | + |
| 161 | + # Command to run sphinx-apidoc |
| 162 | + # Note: We use `sys.executable -m sphinx.ext.apidoc` to ensure we're using |
| 163 | + # the apidoc from the same Python environment as Sphinx. |
| 164 | + command = [ |
| 165 | + sys.executable, |
| 166 | + "-m", |
| 167 | + "sphinx.ext.apidoc", |
| 168 | + "--module-first", |
| 169 | + "--force", |
| 170 | + "--separate", |
| 171 | + "--output-dir", |
| 172 | + output_path, |
| 173 | + os.path.join(MAXTEXT_REPO_ROOT, "src"), |
| 174 | + # Paths to exclude |
| 175 | + os.path.join(MAXTEXT_REPO_ROOT, "tests"), |
| 176 | + os.path.join(MAXTEXT_REPO_ROOT, "src", "MaxText", "experimental"), |
| 177 | + os.path.join(MAXTEXT_REPO_ROOT, "src", "MaxText", "inference_mlperf"), |
| 178 | + os.path.join(MAXTEXT_REPO_ROOT, "src", "MaxText", "scratch_code"), |
| 179 | + os.path.join(MAXTEXT_REPO_ROOT, "src", "MaxText", "utils", "ckpt_conversion"), |
| 180 | + os.path.join(MAXTEXT_REPO_ROOT, "src", "MaxText", "rl"), |
| 181 | + ] |
| 182 | + |
| 183 | + # Run the command and check for errors |
| 184 | + try: |
| 185 | + print("Running sphinx-apidoc...") |
| 186 | + subprocess.check_call(command, env={**os.environ, **{"OBJC_DISABLE_INITIALIZE_FORK_SAFETY": "1"}}) |
| 187 | + except subprocess.CalledProcessError as e: |
| 188 | + print(f"sphinx-apidoc failed with error: {e}", file=sys.stderr) |
| 189 | + sys.exit(1) |
| 190 | + |
| 191 | + |
| 192 | +# Connect the apidoc generation to the Sphinx build process |
| 193 | +def setup(app): |
| 194 | + run_apidoc(None) |
| 195 | + print("running:", app) |
| 196 | + # app.connect("builder-inited", run_apidoc) |
0 commit comments