Skip to content

Commit d01e65d

Browse files
Merge pull request #2049 from SamuelMarks:docgen
PiperOrigin-RevId: 859256825
2 parents f41f61a + 0b38369 commit d01e65d

38 files changed

Lines changed: 480 additions & 397 deletions

.github/workflows/check_docs_build.yml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,28 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515
- name: Checkout repository
16-
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
16+
uses: actions/checkout@v5
1717
with:
1818
persist-credentials: false
1919

20-
- name: Set up Python
21-
uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # v6.0.0
20+
- name: Install uv and set the Python version
21+
uses: astral-sh/setup-uv@v6
2222
with:
2323
python-version: '3.12'
24-
cache: 'pip' # caching pip dependencies
24+
enable-cache: true
25+
26+
- name: Set venv
27+
run: uv venv --python 3.12 $GITHUB_WORKSPACE/venv
2528

2629
- name: Install dependencies
27-
run: pip install -r dependencies/requirements/requirements_docs.txt
30+
run: . $GITHUB_WORKSPACE/venv/bin/activate && uv pip install -r dependencies/requirements/requirements_docs.txt
2831

2932
- name: Build documentation
3033
run: |
34+
. $GITHUB_WORKSPACE/venv/bin/activate
35+
uv pip install -e . --no-deps
36+
uv pip install torch
3137
sphinx-build -W -b html docs docs/_build/html
38+
env:
39+
JAX_PLATFORMS: cpu
40+
CUDA_VISIBLE_DEVICES: ""

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
*__pycache__*
22
tmp/
33
logs/
4-
4+
.venvs
5+
venv*
56
# Byte-compiled / optimized / DLL files
67
__pycache__/
78
*.py[cod]

DOCS.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!--
2+
# Copyright 2023–2025 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
-->
16+
Documentation… documentation!
17+
=============================
18+
19+
## Dependencies
20+
First install the dependencies:
21+
```sh
22+
$ python3 -m pip install -r requirements_docs.txt
23+
```
24+
(or `uv pip install` …)
25+
26+
## Build
27+
```sh
28+
$ sphinx-build -M html docs out
29+
```
30+
31+
## Serve
32+
You can use any static file HTTP server, e.g.:
33+
```sh
34+
$ python3 -m http.server -d 'out/html'
35+
```
36+
37+
## Build & server (watch for changes)
38+
```sh
39+
$ python3 -m pip install sphinx-autobuild
40+
$ sphinx-autobuild docs out
41+
```
42+
43+
## Release to readthedocs
44+
45+
See GitHub Action

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,7 @@ MaxText aims to provide you with the best OSS models, whether as a reference imp
126126
## Get involved
127127

128128
Please join our [Discord Channel](https://discord.com/invite/2H9PhvTcDU) and if you have feedback, you can file a feature request, documentation request, or bug report [here](https://github.com/AI-Hypercomputer/maxtext/issues/new/choose).
129+
130+
## License
131+
132+
[Apache License 2.0](LICENSE)

dependencies/requirements/requirements_docs.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@ myst-parser[linkify]
55
sphinx-book-theme
66
sphinx-design
77
sphinx-copybutton
8+
9+
# for import docs
10+
-r base_requirements/requirements.txt
11+
google-tunix
12+
mlcommons-loadgen
13+
mlperf-logging @ https://github.com/mlcommons/logging/archive/38ab22670527888c8eb7825a4ece176fcc36a95d.zip
14+
tf-keras
15+
torch==2.7.1
16+
trl==0.19.0
17+
vllm

docs/conf.py

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023–2025 Google LLC
1+
# Copyright 2023–2026 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -25,9 +25,23 @@
2525
# -- Project information -----------------------------------------------------
2626
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
2727

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+
2842
project = "MaxText"
2943
# pylint: disable=redefined-builtin
30-
copyright = "2025, Google LLC"
44+
copyright = "2023–2026, Google LLC"
3145
author = "MaxText developers"
3246

3347
# -- General configuration ---------------------------------------------------
@@ -37,11 +51,27 @@
3751
"myst_nb",
3852
"sphinx_design",
3953
"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",
4059
]
4160

4261
templates_path = ["_templates"]
4362
source_suffix = [".rst", ".ipynb", ".md"]
4463

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+
4575
# -- Options for HTML output -------------------------------------------------
4676
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
4777

@@ -60,6 +90,24 @@
6090
]
6191
myst_linkify_fuzzy_links = False
6292

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+
63111
# Theme-specific options
64112
# https://sphinx-book-theme.readthedocs.io/en/stable/reference.html
65113
html_theme_options = {
@@ -77,7 +125,72 @@
77125

78126
# Remove specific documents from ToC
79127
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"),
83135
]
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)

docs/index.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
-->
16+
1617
# MaxText
1718

1819
```{raw} html
1920
:file: index.html
2021
```
2122

23+
:link: reference/api
24+
2225
<div class="doc-body">
2326
<section class="latest-news">
2427

@@ -34,11 +37,11 @@
3437
:maxdepth: 2
3538
:hidden:
3639
37-
install_maxtext.md
38-
tutorials.md
39-
run_maxtext.md
40-
guides.md
41-
reference.md
42-
development.md
43-
release_notes.md
40+
install_maxtext
41+
tutorials
42+
run_maxtext
43+
guides
44+
reference
45+
development
46+
release_notes
4447
```

docs/reference.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
Copyright 2024 Google LLC
2+
Copyright 2023–2025 Google LLC
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -54,8 +54,8 @@ Key concepts including checkpointing strategies, quantization, tiling, and Mixtu
5454
:hidden:
5555
:maxdepth: 1
5656
57-
reference/performance_metrics.md
58-
reference/models.md
59-
reference/architecture.md
60-
reference/core_concepts.md
57+
reference/performance_metrics
58+
reference/models
59+
reference/architecture
60+
reference/core_concepts
6161
```

docs/reference/api.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
..
2+
Copyright 2023–2026 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
API Reference
17+
=============
18+
19+
This section contains the complete API documentation for the ``MaxText`` library, automatically generated from the source code docstrings.
20+
21+
.. toctree::
22+
:maxdepth: 4
23+
:caption: Package Modules
24+
:glob:
25+
26+
api_generated/modules

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies = []
2828
[tool.hatch.metadata.hooks.requirements_txt.optional-dependencies]
2929
tpu = ["dependencies/requirements/generated_requirements/tpu-requirements.txt"]
3030
cuda12 = ["dependencies/requirements/generated_requirements/cuda12-requirements.txt"]
31+
docs = ["dependencies/requirements/requirements_docs.txt"]
3132

3233
[project.urls]
3334
Repository = "https://github.com/AI-Hypercomputer/maxtext.git"

0 commit comments

Comments
 (0)