|
| 1 | +# MaxText Examples - Setting the Jupyter Lab or Google Colab and connect to the TPU runtime |
| 2 | + |
| 3 | +This guide provides comprehensive instructions for setting up Jupyter Lab on TPU and connecting it to Google Colab for running MaxText examples. |
| 4 | + |
| 5 | +## 📑 Table of Contents |
| 6 | + |
| 7 | +- [Prerequisites](#prerequisites) |
| 8 | +- [Method 1: Google Colab with TPU (Recommended)](#method-1-google-colab-with-tpu-recommended) |
| 9 | +- [Method 2: Local Jupyter Lab with TPU](#method-2-local-jupyter-lab-with-tpu) |
| 10 | +- [Method 3: Colab + Local Jupyter Lab Hybrid](#method-3-colab--local-jupyter-lab-hybrid) |
| 11 | +- [Available Examples](#available-examples) |
| 12 | +- [Common Pitfalls & Debugging](#common-pitfalls--debugging) |
| 13 | +- [Support & Resources](#support--resources) |
| 14 | +- [Contributing](#contributing) |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +Before starting, make sure you have: |
| 19 | + |
| 20 | +- ✅ A Google Cloud Platform (GCP) account with billing enabled |
| 21 | +- ✅ TPU quota available in your region (check under IAM & Admin → Quotas) |
| 22 | +- ✅ Basic familiarity with Jupyter, Python, and Git |
| 23 | +- ✅ gcloud CLI installed locally if you plan to use Method 2 or 3 |
| 24 | +- ✅ Firewall rules open for port 8888 (Jupyter) if accessing directly |
| 25 | + |
| 26 | +## Method 1: Google Colab with TPU (Recommended) |
| 27 | + |
| 28 | +This is the fastest way to run MaxText without managing infrastructure. |
| 29 | + |
| 30 | +### Step 1: Open Google Colab |
| 31 | + |
| 32 | +1. Go to [Google Colab](https://colab.research.google.com/) |
| 33 | +2. Sign in → New Notebook |
| 34 | + |
| 35 | +### Step 2: Enable TPU Runtime |
| 36 | + |
| 37 | +1. **Runtime** → **Change runtime type** |
| 38 | +2. Set **Hardware accelerator** → **TPU** |
| 39 | +3. Select TPU version: |
| 40 | + - **v5e-8** → recommended for most MaxText examples, but it's a paid option |
| 41 | + - **v5e-1** → free tier option (slower, but works for Qwen-0.6B demos) |
| 42 | +4. Click **Save** |
| 43 | + |
| 44 | +### Step 3: Upload & Prepare MaxText |
| 45 | + |
| 46 | +Upload notebooks or mount your GitHub repo |
| 47 | + |
| 48 | +> **Note:** In Colab, the repo root will usually be `/content/maxtext` |
| 49 | +
|
| 50 | +**Example:** |
| 51 | +```python |
| 52 | +!git clone https://github.com/AI-Hypercomputer/maxtext.git |
| 53 | +%cd maxtext |
| 54 | +``` |
| 55 | + |
| 56 | +### Step 4: Run Examples |
| 57 | + |
| 58 | +1. Open `src/MaxText/examples/` |
| 59 | +2. Try: |
| 60 | + - `sft_qwen3_demo.ipynb` |
| 61 | + - `sft_llama3_demo.ipynb` |
| 62 | + - `grpo_llama3_demo.ipynb` |
| 63 | + |
| 64 | + |
| 65 | +> ⚡ **Tip:** If Colab disconnects, re-enable TPU and re-run setup cells. Save checkpoints to GCS or Drive. |
| 66 | +
|
| 67 | +> ⚡ **Tip:** If Colab asks to restart session - do it and continue to run cells |
| 68 | +
|
| 69 | +## Method 2: Local Jupyter Lab with TPU |
| 70 | + |
| 71 | +This method gives you more control and is better for long training runs. |
| 72 | + |
| 73 | +### Step 1: Set Up TPU VM |
| 74 | + |
| 75 | +In Google Cloud Console: |
| 76 | + |
| 77 | +1. **Compute Engine** → **TPU** → **Create TPU Node** |
| 78 | +2. Example config: |
| 79 | + - **Name:** `maxtext-tpu-node` |
| 80 | + - **TPU type:** `v5e-8` (or `v6p-8` for newer hardware) |
| 81 | + - **Runtime Version:** `tpu-ubuntu-alpha-*` (matches your VM image) |
| 82 | + |
| 83 | +### Step 2: Connect to TPU VM |
| 84 | + |
| 85 | +```bash |
| 86 | +gcloud compute tpus tpu-vm ssh maxtext-tpu-node --zone=YOUR_ZONE |
| 87 | +``` |
| 88 | + |
| 89 | +### Step 3: Install Dependencies |
| 90 | + |
| 91 | +```bash |
| 92 | +sudo apt update && sudo apt upgrade -y |
| 93 | +sudo apt install python3-pip python3-dev git -y |
| 94 | +pip3 install jupyterlab |
| 95 | +``` |
| 96 | + |
| 97 | +### Step 4: Start Jupyter Lab |
| 98 | + |
| 99 | +```bash |
| 100 | +jupyter lab --ip=0.0.0.0 --port=8888 --no-browser --allow-root |
| 101 | +``` |
| 102 | + |
| 103 | +Copy the URL with token from terminal |
| 104 | + |
| 105 | +### Step 5: Secure Access |
| 106 | + |
| 107 | +#### Option A: SSH Tunnel (Recommended) |
| 108 | + |
| 109 | +```bash |
| 110 | +gcloud compute tpus tpu-vm ssh maxtext-tpu-node --zone=YOUR_ZONE -- -L 8888:localhost:8888 |
| 111 | +``` |
| 112 | + |
| 113 | +Then open → `http://localhost:8888` |
| 114 | + |
| 115 | + |
| 116 | +## Method 3: Colab + Local Jupyter Lab Hybrid |
| 117 | + |
| 118 | +Set up Jupyter Lab as in step 2. |
| 119 | +Use the link for Jupyter Lab as a link for "Connect to a local runtime" in Collab - at the dropdown where you select the runtime. |
| 120 | + |
| 121 | +## Available Examples |
| 122 | + |
| 123 | +### Supervised Fine-Tuning (SFT) |
| 124 | + |
| 125 | +- **`sft_qwen3_demo.ipynb`** → Qwen3-0.6B with Hugging Face ultrachat_200k dataset |
| 126 | +- **`sft_llama3_demo.ipynb`** → Llama3.1-8B with Hugging Face ultrachat_200k dataset |
| 127 | + |
| 128 | +### GRPO Training |
| 129 | + |
| 130 | +- **`grpo_llama3_demo.ipynb`** → GRPO training on math dataset |
| 131 | + |
| 132 | +## Common Pitfalls & Debugging |
| 133 | + |
| 134 | +| Issue | Solution | |
| 135 | +|-------|----------| |
| 136 | +| ❌ TPU runtime mismatch | Check TPU runtime version matches VM image (`tpu-ubuntu-alpha-*`) | |
| 137 | +| ❌ Colab disconnects | Save checkpoints to GCS or Drive regularly | |
| 138 | +| ❌ "RESOURCE_EXHAUSTED" errors | Use smaller batch size or v5e-8 instead of v5e-1 | |
| 139 | +| ❌ Firewall blocked | Ensure port 8888 open, or always use SSH tunneling | |
| 140 | +| ❌ Path confusion | In Colab use `/content/maxtext`; in TPU VM use `~/maxtext` | |
| 141 | + |
| 142 | +## Support and Resources |
| 143 | + |
| 144 | +- 📘 [MaxText Documentation](https://github.com/AI-Hypercomputer/maxtext) |
| 145 | +- 💻 [Google Colab](https://colab.research.google.com) |
| 146 | +- ⚡ [Cloud TPU Docs](https://cloud.google.com/tpu/docs) |
| 147 | +- 🧩 [Jupyter Lab](https://jupyterlab.readthedocs.io) |
| 148 | + |
| 149 | +## Contributing |
| 150 | + |
| 151 | +If you encounter issues or have improvements for this guide, please: |
| 152 | + |
| 153 | +1. Open an issue on the MaxText repository |
| 154 | +2. Submit a pull request with your improvements |
| 155 | +3. Share your experience in the discussions |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +**Happy Training! 🚀** |
0 commit comments