|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "[](https://colab.research.google.com/github/AI-Hypercomputer/maxtext/blob/main/src/MaxText/examples/multimodal_gemma3_demo.ipynb)\n", |
| 8 | + "\n", |
| 9 | + "# Gemma3 Multimodal Inference/Training Demo" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "## Overview\n", |
| 17 | + "\n", |
| 18 | + "This notebook demonstrates MaxText's multimodal features, using Gemma3-4B as an example:\n", |
| 19 | + "- Convert an orbax checkpoint from HuggingFace.\n", |
| 20 | + "- Apply decoding on a single image input.\n", |
| 21 | + "- Apply SFT to the converted checkpoint on ChartQA dataset.\n", |
| 22 | + "\n", |
| 23 | + "Given the relative small size of Gemma3-4B, you can run this colab on a v4-8, v5p-8 or v6e-4 TPU VM. However, we recommend using [XPK](https://github.com/AI-Hypercomputer/maxtext/blob/64d6d9b425e78dde94c37a82bb13ba5606e74b1b/docs/guides/run_maxtext_via_xpk.md) to schedule a training workload on a TPU cluster for better performance." |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "### Get Your Hugging Face Token\n", |
| 31 | + "\n", |
| 32 | + "To access model checkpoint from the Hugging Face Hub, you need to authenticate with a personal access token.\n", |
| 33 | + "\n", |
| 34 | + "**Follow these steps to get your token:**\n", |
| 35 | + "\n", |
| 36 | + "1. **Navigate to the Access Tokens page** in your Hugging Face account settings. You can go there directly by visiting this URL:\n", |
| 37 | + " * [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)\n", |
| 38 | + "\n", |
| 39 | + "2. **Create a new token** by clicking the **\"+ Create new token\"** button.\n", |
| 40 | + "\n", |
| 41 | + "3. **Give your token a name** and assign it a **`read` role**. The `read` role is sufficient for downloading models.\n", |
| 42 | + "\n", |
| 43 | + "4. **Copy the generated token**. You will need to paste it in `HF_TOKEN`." |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "metadata": { |
| 50 | + "id": "5KPyOE8e9WbO" |
| 51 | + }, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "#Install maxtext and dependencies\n", |
| 55 | + "# 1. Install uv, a fast Python package installer\n", |
| 56 | + "!pip install uv\n", |
| 57 | + "\n", |
| 58 | + "# 2. Install MaxText and its dependencies\n", |
| 59 | + "!uv pip install maxtext --resolution=lowest\n", |
| 60 | + "!install_maxtext_github_deps" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": null, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "import os\n", |
| 70 | + "import MaxText\n", |
| 71 | + "\n", |
| 72 | + "# Get the root directory of the MaxText\n", |
| 73 | + "MAXTEXT_REPO_ROOT=os.path.dirname(MaxText.__file__)\n", |
| 74 | + "\n", |
| 75 | + "# Define model name\n", |
| 76 | + "MODEL_NAME=\"gemma3-4b\"\n", |
| 77 | + "\n", |
| 78 | + "# Use either a GCS path or a local path for the model checkpoint\n", |
| 79 | + "MODEL_CHECKPOINT_PATH = f\"gs://your-gcs-bucket/{MODEL_NAME}\"\n", |
| 80 | + "\n", |
| 81 | + "# Replace with your actual Hugging Face token\n", |
| 82 | + "HF_TOKEN = \"your_huggingface_token_here\"" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "markdown", |
| 87 | + "metadata": {}, |
| 88 | + "source": [ |
| 89 | + "## Convert Checkpoint from HuggingFace" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": null, |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [], |
| 97 | + "source": [ |
| 98 | + "!python3 -m MaxText.utils.ckpt_conversion.to_maxtext \\\n", |
| 99 | + " $MAXTEXT_REPO_ROOT/configs/base.yml \\\n", |
| 100 | + " model_name=$MODEL_NAME \\\n", |
| 101 | + " hf_access_token=$HF_TOKEN \\\n", |
| 102 | + " base_output_directory=$MODEL_CHECKPOINT_PATH \\\n", |
| 103 | + " use_multimodal=true \\\n", |
| 104 | + " scan_layers=false" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "markdown", |
| 109 | + "metadata": {}, |
| 110 | + "source": [ |
| 111 | + "## Decode on One Image" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": null, |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [ |
| 120 | + "!python -m MaxText.decode \\\n", |
| 121 | + " $MAXTEXT_REPO_ROOT/configs/base.yml \\\n", |
| 122 | + " model_name=$MODEL_NAME \\\n", |
| 123 | + " tokenizer_path=assets/tokenizer.gemma3 \\\n", |
| 124 | + " load_parameters_path=$MODEL_CHECKPOINT_PATH/0/items \\\n", |
| 125 | + " per_device_batch_size=1 \\\n", |
| 126 | + " run_name=ht_test max_prefill_predict_length=272 \\\n", |
| 127 | + " max_target_length=300 \\\n", |
| 128 | + " steps=1 \\\n", |
| 129 | + " async_checkpointing=false \\\n", |
| 130 | + " scan_layers=false \\\n", |
| 131 | + " use_multimodal=true \\\n", |
| 132 | + " prompt='Describe image <start_of_image>' \\\n", |
| 133 | + " image_path=$MAXTEXT_REPO_ROOT/test_assets/test_image.jpg \\\n", |
| 134 | + " attention='dot_product'" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "markdown", |
| 139 | + "metadata": {}, |
| 140 | + "source": [ |
| 141 | + "## Supervised Finetuning (SFT)" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "markdown", |
| 146 | + "metadata": {}, |
| 147 | + "source": [ |
| 148 | + "Running the cell below will trigger a 10-step SFT on your TPU VM (v4-8, v5p-8, or v6e-4). However, we recommend using [XPK](https://github.com/AI-Hypercomputer/maxtext/blob/64d6d9b425e78dde94c37a82bb13ba5606e74b1b/docs/guides/run_maxtext_via_xpk.md) to schedule a training workload on a TPU cluster for better performance. After the SFT, the result checkpoint will be saved to `BASE_OUTPUT_DIRECTORY`." |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "code", |
| 153 | + "execution_count": null, |
| 154 | + "metadata": {}, |
| 155 | + "outputs": [], |
| 156 | + "source": [ |
| 157 | + "# Define SFT output directory\n", |
| 158 | + "BASE_OUTPUT_DIRECTORY=f\"gs://your-gcs-bucket/{MODEL_NAME}-sft\"\n", |
| 159 | + "PRE_TRAINED_MODEL_TOKENIZER=\"google/gemma-3-4b-it\"\n", |
| 160 | + "WORKLOAD_NAME=f\"{MODEL_NAME}-chartqa-sft\"\n", |
| 161 | + "STEPS=10\n", |
| 162 | + "PER_DEVICE_BATCH_SIZE=1\n", |
| 163 | + "\n", |
| 164 | + "!python -m MaxText.sft_trainer \\\n", |
| 165 | + " $MAXTEXT_REPO_ROOT/configs/sft-vision-chartqa.yml \\\n", |
| 166 | + " run_name=$WORKLOAD_NAME \\\n", |
| 167 | + " model_name=$MODEL_NAME \\\n", |
| 168 | + " tokenizer_path=$PRE_TRAINED_MODEL_TOKENIZER \\\n", |
| 169 | + " hf_access_token=$HF_TOKEN \\\n", |
| 170 | + " load_parameters_path=$MODEL_CHECKPOINT_PATH/0/items \\\n", |
| 171 | + " base_output_directory=$BASE_OUTPUT_DIRECTORY \\\n", |
| 172 | + " per_device_batch_size=$PER_DEVICE_BATCH_SIZE \\\n", |
| 173 | + " steps=$STEPS \\\n", |
| 174 | + " max_prefill_predict_length=1024 \\\n", |
| 175 | + " max_target_length=2048 \\\n", |
| 176 | + " checkpoint_period=1000 \\\n", |
| 177 | + " scan_layers=False \\\n", |
| 178 | + " async_checkpointing=True \\\n", |
| 179 | + " enable_checkpointing=True \\\n", |
| 180 | + " attention=dot_product \\\n", |
| 181 | + " max_num_images_per_example=1 \\\n", |
| 182 | + " dataset_type=hf profiler=xplane" |
| 183 | + ] |
| 184 | + } |
| 185 | + ], |
| 186 | + "metadata": { |
| 187 | + "accelerator": "TPU", |
| 188 | + "colab": { |
| 189 | + "gpuType": "V5E1", |
| 190 | + "provenance": [] |
| 191 | + }, |
| 192 | + "kernelspec": { |
| 193 | + "display_name": "python3.12", |
| 194 | + "language": "python", |
| 195 | + "name": "python3" |
| 196 | + }, |
| 197 | + "language_info": { |
| 198 | + "codemirror_mode": { |
| 199 | + "name": "ipython", |
| 200 | + "version": 3 |
| 201 | + }, |
| 202 | + "file_extension": ".py", |
| 203 | + "mimetype": "text/x-python", |
| 204 | + "name": "python", |
| 205 | + "nbconvert_exporter": "python", |
| 206 | + "pygments_lexer": "ipython3", |
| 207 | + "version": "3.12.7" |
| 208 | + } |
| 209 | + }, |
| 210 | + "nbformat": 4, |
| 211 | + "nbformat_minor": 0 |
| 212 | +} |
0 commit comments