Skip to content

Commit fe5032a

Browse files
SurbhiJainUSCGoogle-ML-Automation
authored andcommitted
Refactor RL tutorials
PiperOrigin-RevId: 948616994
1 parent 593891d commit fe5032a

9 files changed

Lines changed: 76 additions & 247 deletions

File tree

docs/guides/checkpointing_solutions/convert_checkpoint.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ You can find your converted checkpoint files under `${BASE_OUTPUT_DIRECTORY}/0/i
8686
- `--hf_model_path` (Optional): Specifies a customized remote directory or local directory containing the model weights. If unspecified, we use the [default Hugging Face repository ID](https://github.com/AI-Hypercomputer/maxtext/blob/main/src/maxtext/utils/globals.py) (e.g., openai/gpt-oss-20b). This is necessary for locally dequantized models like GPT-OSS or DeepSeek.
8787
- `--save_dtype` (Optional): Specifies the data type of saved model weights. Default to `bfloat16` to save memory.
8888

89+
(maxtext-to-hf)=
90+
8991
## MaxText to Hugging Face
9092

9193
Use the `to_huggingface.py` script to convert a MaxText checkpoint into the Hugging Face format. This is useful for sharing your models or integrating them with the Hugging Face ecosystem.

docs/tutorials/posttraining/rl_gptoss_20b.md

Lines changed: 15 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# Reinforcement Learning with GPT-OSS 20B on Multi-Host TPUs
1818

1919
This tutorial provides step-by-step instructions for setting up the environment
20-
and training the GPT-OSS 20B model on the [GSM8K dataset](https://huggingface.co/datasets/openai/gsm8k) on an Viperfish GKE cluster with `v5p-64` nodes.
20+
and training the GPT-OSS 20B model on the [GSM8K dataset](https://huggingface.co/datasets/openai/gsm8k) on a GKE cluster with `v5p-64` nodes.
2121

2222
## Prerequisites
2323

@@ -30,10 +30,6 @@ Before starting, ensure you have:
3030
- A Pathways-ready GKE cluster (see [create GKE cluster](https://docs.cloud.google.com/ai-hypercomputer/docs/workloads/pathways-on-cloud/create-gke-cluster)).
3131
- **Docker** installed and configured for sudoless use. Follow the steps to [configure sudoless Docker](https://docs.docker.com/engine/install/linux-postinstall/).
3232

33-
## Build and Upload MaxText Docker Image
34-
35-
For instructions on building and uploading the MaxText Docker image with post-training dependencies, please refer to the [official documentation](../../build_maxtext.md).
36-
3733
## Setup Environment Variables
3834

3935
Set up the following environment variables to configure your training run. Replace
@@ -53,19 +49,6 @@ export ZONE=<ZONE> # e.g., 'us-central1' or 'us-central1-a'
5349

5450
# Use a GCS bucket you own to store logs and checkpoints.
5551
export BASE_OUTPUT_DIRECTORY=<GCS_BUCKET> # e.g., gs://my-bucket/maxtext-runs
56-
57-
# The Docker image you pushed in the previous step
58-
export CLOUD_IMAGE_NAME=<IMAGE_NAME>
59-
export DOCKER_IMAGE="gcr.io/${PROJECT_ID?}/${CLOUD_IMAGE_NAME?}"
60-
```
61-
62-
## Clone MaxText Repository
63-
64-
If you haven't already, clone the MaxText repository to your local machine:
65-
66-
```bash
67-
git clone https://github.com/AI-Hypercomputer/maxtext.git
68-
cd maxtext
6952
```
7053

7154
## Authenticate with Hugging Face
@@ -89,51 +72,29 @@ export MAXTEXT_CKPT_PATH=<CKPT_PATH> # e.g., gs://my-bucket/my-model-checkpoint/
8972

9073
### Option 2: Converting from a Hugging Face checkpoint
9174

92-
> **Note:** Converting the 20B model requires approximately 40 GB of free disk space to download its safetensors. Please verify you have sufficient space before running the conversion.
75+
Refer to [Hugging Face to MaxText](hf-to-maxtext) to convert a Hugging Face checkpoint to MaxText format. After conversion finishes, set `MAXTEXT_CKPT_PATH` to the converted MaxText checkpoint path.
9376

9477
```bash
95-
# Create and activate a virtual environment
96-
uv venv --python 3.12 --seed tpu_venv
97-
source tpu_venv/bin/activate
98-
uv pip install -e .[tpu] --resolution=lowest
99-
100-
# Install torch for conversion
101-
python3 -m pip install torch --index-url https://download.pytorch.org/whl/cpu
102-
103-
# Download Hugging Face weights to a local directory
104-
huggingface-cli download openai/gpt-oss-20b --local-dir ./gpt-oss-20b-hf
105-
106-
# Set up the MaxText checkpoint path environment variable where the converted checkpoint will be saved
107-
export MAXTEXT_CKPT_PATH="${BASE_OUTPUT_DIRECTORY}/checkpoints/gpt-oss-20b-converted/0/items"
108-
109-
# Run the conversion script to convert the Hugging Face checkpoint to MaxText format
110-
python3 -m maxtext.checkpoint_conversion.standalone_scripts.convert_gpt_oss_ckpt \
111-
--base-model-path ./gpt-oss-20b-hf \
112-
--maxtext-model-path ${MAXTEXT_CKPT_PATH} \
113-
--model-size gpt-oss-20b \
114-
--use-ocdbt=False --use-zarr3=False
115-
116-
# Deactivate the virtual environment
117-
deactivate
118-
rm -rf tpu_venv
78+
export MAXTEXT_CKPT_PATH=<CKPT_PATH> # e.g., gs://my-bucket/my-model-checkpoint/0/items
11979
```
12080

81+
> **Note:** Converting the 20B model requires approximately 40 GB of free disk space to download its safetensors. Please verify you have sufficient space before running the conversion.
82+
12183
## Run RL Workload
12284

85+
### Build and Upload MaxText Docker Image
86+
87+
For instructions on building and uploading the MaxText Docker image with post-training dependencies, please refer to the [official documentation](../../build_maxtext.md).
88+
12389
### Submit your workload
12490

12591
```bash
126-
# Create and activate a virtual environment
127-
uv venv --python 3.12 --seed runner_venv
128-
source runner_venv/bin/activate
129-
uv pip install -e .[runner] --resolution=lowest
92+
# The Docker image you pushed in the previous step
93+
export CLOUD_IMAGE_NAME=<IMAGE_NAME>
94+
export DOCKER_IMAGE="gcr.io/${PROJECT_ID?}/${CLOUD_IMAGE_NAME?}"
13095

13196
# Run the RL training script on your cluster
132-
bash scripts/run_gptoss_20b_rl.sh
133-
134-
# Deactivate the virtual environment
135-
deactivate
136-
rm -rf runner_venv
97+
run_tutorial maxtext/trainers/post_train/rl/scripts/run_gptoss_20b_rl.sh
13798
```
13899

139100
### Monitor your workload
@@ -156,7 +117,7 @@ Alternatively, after running the bash script, you will also get a link to the Go
156117

157118
During RL training, you can monitor key metrics to track model convergence, reward trends, and hardware performance.
158119

159-
To enable Tunix-managed metrics measurement, set `enable_tunix_perf_metrics` to `true` in `src/maxtext/configs/post_train/rl.yml`. Note that this flag is already set to `True` by default in the [scripts/run_gptoss_20b_rl.sh](../../../scripts/run_gptoss_20b_rl.sh) script for this tutorial workload. When enabled, Tunix automatically collects and uploads these metrics to TensorBoard.
120+
To enable Tunix-managed metrics measurement, set `enable_tunix_perf_metrics` to `true` in RL configurations. Note that this flag is already set to `True` by default for this tutorial workload. When enabled, Tunix automatically collects and uploads these metrics to TensorBoard.
160121

161122
For a complete list of collected metrics, see the [Tunix Metrics Documentation](https://tunix.readthedocs.io/en/latest/metrics.html). Key metrics to monitor include:
162123

@@ -172,30 +133,4 @@ For a complete list of collected metrics, see the [Tunix Metrics Documentation](
172133

173134
## Convert Checkpoint to Hugging Face Format
174135

175-
After training, you may want to convert your MaxText checkpoint back to Hugging Face format. Use the following command to perform the conversion:
176-
177-
```bash
178-
# Create and activate a virtual environment
179-
uv venv --python 3.12 --seed tpu_venv
180-
source tpu_venv/bin/activate
181-
uv pip install -e .[tpu] --resolution=lowest
182-
183-
# Install torch for conversion
184-
python3 -m pip install torch --index-url https://download.pytorch.org/whl/cpu
185-
186-
# Define the output path for the converted checkpoint
187-
export HF_CKPT_OUTPUT_DIR="${BASE_OUTPUT_DIRECTORY}/checkpoints/gpt-oss-20b-hf-converted"
188-
189-
# Run the conversion script to convert the MaxText checkpoint back to Hugging Face format
190-
python3 -m maxtext.checkpoint_conversion.to_huggingface \
191-
src/maxtext/configs/base.yml \
192-
model_name=gpt-oss-20b \
193-
load_parameters_path="${MAXTEXT_CKPT_PATH}" \
194-
base_output_directory="${HF_CKPT_OUTPUT_DIR}" \
195-
scan_layers=True \
196-
weight_dtype=bfloat16 hardware=cpu skip_jax_distributed_system=True
197-
198-
# Deactivate the virtual environment
199-
deactivate
200-
rm -rf tpu_venv
201-
```
136+
Refer to [MaxText to Hugging Face](maxtext-to-hf) to convert a MaxText checkpoint back to Hugging Face format.

docs/tutorials/posttraining/rl_qwen3_30b.md

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ Before starting, ensure you have:
3030
- A Pathways-ready GKE cluster (see [create GKE cluster](https://docs.cloud.google.com/ai-hypercomputer/docs/workloads/pathways-on-cloud/create-gke-cluster)).
3131
- **Docker** installed and configured for sudoless use. Follow the steps to [configure sudoless Docker](https://docs.docker.com/engine/install/linux-postinstall/).
3232

33-
## Build and Upload MaxText Docker Image
34-
35-
For instructions on building and uploading the MaxText Docker image with post-training dependencies, please refer to the [official documentation](../../build_maxtext.md).
36-
3733
## Setup Environment Variables
3834

3935
Set up the following environment variables to configure your training run. Replace
@@ -53,19 +49,6 @@ export ZONE=<ZONE> # e.g., 'us-central1' or 'us-central1-a'
5349

5450
# Use a GCS bucket you own to store logs and checkpoints.
5551
export BASE_OUTPUT_DIRECTORY=<GCS_BUCKET> # e.g., gs://my-bucket/maxtext-runs
56-
57-
# The Docker image you pushed in the previous step
58-
export CLOUD_IMAGE_NAME=<IMAGE_NAME>
59-
export DOCKER_IMAGE="gcr.io/${PROJECT_ID?}/${CLOUD_IMAGE_NAME?}"
60-
```
61-
62-
## Clone MaxText Repository
63-
64-
If you haven't already, clone the MaxText repository to your local machine:
65-
66-
```bash
67-
git clone https://github.com/AI-Hypercomputer/maxtext.git
68-
cd maxtext
6952
```
7053

7154
## Authenticate with Hugging Face
@@ -89,42 +72,29 @@ export MAXTEXT_CKPT_PATH=<CKPT_PATH> # e.g., gs://my-bucket/my-model-checkpoint/
8972

9073
### Option 2: Converting from a Hugging Face checkpoint
9174

92-
> **Note:** Converting the 30B model requires approximately 62 GB of free disk space to download its safetensors. Please verify you have sufficient space before running the conversion script.
75+
Refer to [Hugging Face to MaxText](hf-to-maxtext) to convert a Hugging Face checkpoint to MaxText format. You can find an example script to convert `qwen3-30b-a3b-base`model to MaxText format [here](https://github.com/AI-Hypercomputer/maxtext/blob/main/tests/end_to_end/tpu/qwen3/30b/test_qwen3_to_mt.sh). After conversion finishes, set `MAXTEXT_CKPT_PATH` to the converted MaxText checkpoint path.
9376

9477
```bash
95-
# Optional: If you run out of disk space when downloading Hugging Face safetensors,
96-
# customize your "HF_HOME" to redirect the cache to a larger or mounted disk (e.g., on a TPU VM).
97-
# export HF_HOME="/dev/shm/huggingface_tmp"
78+
export MAXTEXT_CKPT_PATH=<CKPT_PATH> # e.g., gs://my-bucket/my-model-checkpoint/0/items
79+
```
9880

99-
# Create and activate a virtual environment
100-
uv venv --python 3.12 --seed tpu_venv
101-
source tpu_venv/bin/activate
102-
uv pip install -e .[tpu] --resolution=lowest
81+
> **Note:** Converting the 30B model requires approximately 62 GB of free disk space to download its safetensors. Please verify you have sufficient space before running the conversion script.
10382
104-
# Run the conversion script to convert the Hugging Face checkpoint to MaxText format
105-
bash scripts/run_qwen3_30b_hf_to_maxtext.sh
83+
## Run RL Workload
10684

107-
# Deactivate the virtual environment
108-
deactivate
109-
rm -rf tpu_venv
110-
```
85+
### Build and Upload MaxText Docker Image
11186

112-
## Run RL Workload
87+
For instructions on building and uploading the MaxText Docker image with post-training dependencies, please refer to the [official documentation](../../build_maxtext.md).
11388

11489
### Submit your workload
11590

11691
```bash
117-
# Create and activate a virtual environment
118-
uv venv --python 3.12 --seed runner_venv
119-
source runner_venv/bin/activate
120-
uv pip install -e .[runner] --resolution=lowest
92+
# The Docker image you pushed in the previous step
93+
export CLOUD_IMAGE_NAME=<IMAGE_NAME>
94+
export DOCKER_IMAGE="gcr.io/${PROJECT_ID?}/${CLOUD_IMAGE_NAME?}"
12195

12296
# Run the RL training script on your cluster
123-
bash scripts/run_qwen3_30b_rl.sh
124-
125-
# Deactivate the virtual environment
126-
deactivate
127-
rm -rf runner_venv
97+
run_tutorial maxtext/trainers/post_train/rl/scripts/run_qwen3_30b_rl.sh
12898
```
12999

130100
### Monitor your workload
@@ -147,7 +117,7 @@ Alternatively, after running the bash script, you will also get a link to the Go
147117

148118
During RL training, you can monitor key metrics to track model convergence, reward trends, and hardware performance.
149119

150-
To enable Tunix-managed metrics measurement, set `enable_tunix_perf_metrics` to `true` in `src/maxtext/configs/post_train/rl.yml`. Note that this flag is already set to `True` by default in the [scripts/run_qwen3_30b_rl.sh](../../../scripts/run_qwen3_30b_rl.sh) script for this tutorial workload. When enabled, Tunix automatically collects and uploads these metrics to TensorBoard.
120+
To enable Tunix-managed metrics measurement, set `enable_tunix_perf_metrics` to `true` in RL configurations. Note that this flag is already set to `True` by default for this tutorial workload. When enabled, Tunix automatically collects and uploads these metrics to TensorBoard.
151121

152122
For a complete list of collected metrics, see the [Tunix Metrics Documentation](https://tunix.readthedocs.io/en/latest/metrics.html). Key metrics to monitor include:
153123

@@ -163,18 +133,6 @@ For a complete list of collected metrics, see the [Tunix Metrics Documentation](
163133

164134
## Convert Checkpoint to Hugging Face Format
165135

166-
After training, you may want to convert your MaxText checkpoint back to Hugging Face format. Use the following script to perform the conversion:
167-
168-
```bash
169-
# Create and activate a virtual environment
170-
uv venv --python 3.12 --seed tpu_venv
171-
source tpu_venv/bin/activate
172-
uv pip install -e .[tpu] --resolution=lowest
173-
174-
# Run the conversion script to convert the MaxText checkpoint back to Hugging Face format
175-
bash scripts/run_qwen3_30b_maxtext_to_hf.sh
136+
## Convert Checkpoint to Hugging Face Format
176137

177-
# Deactivate the virtual environment
178-
deactivate
179-
rm -rf tpu_venv
180-
```
138+
Refer to [MaxText to Hugging Face](maxtext-to-hf) to convert a MaxText checkpoint back to Hugging Face format. You can find an example script to convert `qwen3-30b-a3b-base`model to Hugging Face format [here](https://github.com/AI-Hypercomputer/maxtext/blob/main/tests/end_to_end/tpu/qwen3/30b/test_qwen3_to_hf.sh).

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ allow-direct-references = true
4242
[tool.hatch.build.targets.wheel]
4343
packages = ["src/maxtext", "src/dependencies"]
4444

45-
46-
4745
[project.scripts]
4846
install_tpu_pre_train_extra_deps = "dependencies.scripts.install_pre_train_extra_deps:main"
4947
install_cuda12_pre_train_extra_deps = "dependencies.scripts.install_pre_train_extra_deps:main"
5048
install_tpu_post_train_extra_deps = "dependencies.scripts.install_post_train_extra_deps:main"
5149
build_maxtext_docker_image = "dependencies.scripts.build_maxtext_docker_image:main"
5250
upload_maxtext_docker_image = "dependencies.scripts.upload_maxtext_docker_image:main"
51+
run_tutorial = "dependencies.scripts.run_tutorial:main"

scripts/run_qwen3_30b_hf_to_maxtext.sh

Lines changed: 0 additions & 45 deletions
This file was deleted.

scripts/run_qwen3_30b_maxtext_to_hf.sh

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)