|
| 1 | +<!-- |
| 2 | + Copyright 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 | + |
| 17 | +# Adding a New Model for LoRA Fine-Tuning |
| 18 | + |
| 19 | +This guide explains how to add Low-Rank Adaptation (LoRA) support for a new model architecture in MaxText. |
| 20 | + |
| 21 | +MaxText leverages [Tunix](https://github.com/google/tunix) and [Qwix](https://github.com/google/qwix) to support Parameter-Efficient Fine-Tuning (PEFT) on JAX/NNX model definitions. Since the architecture uses modular APIs, adding LoRA support for a new model is highly streamlined. |
| 22 | + |
| 23 | +______________________________________________________________________ |
| 24 | + |
| 25 | +## 1. Step-by-Step Bring-up Guide for NNX LoRA |
| 26 | + |
| 27 | +To enable LoRA support for a new model, follow these two simple steps: |
| 28 | + |
| 29 | +### Step 1.1: Verify Base Model Support |
| 30 | + |
| 31 | +The target model architecture must already be implemented and supported as a base model in MaxText. |
| 32 | + |
| 33 | +- The JAX/NNX model definition should be located under `src/maxtext/models/` (e.g., \[gemma3.py\](file:///home/jackyf_google_com/maxtext/src/maxtext/models/gemma3.py)). |
| 34 | +- The model configurations must be registered and runnable for baseline pre-training or full fine-tuning. |
| 35 | + |
| 36 | +### Step 1.2: Define Trainable LoRA Target Modules |
| 37 | + |
| 38 | +Add a recommended target pattern for your model architecture prefix in \[src/maxtext/configs/post_train/lora_module_path.yml\](file:///home/jackyf_google_com/maxtext/src/maxtext/configs/post_train/lora_module_path.yml): |
| 39 | + |
| 40 | +```yaml |
| 41 | +your_model_prefix: "decoder/layers/.*(self_attention/(query|key|value|out)|mlp/(wi_0|wi_1|wo))" |
| 42 | +``` |
| 43 | +
|
| 44 | +> [!NOTE] |
| 45 | +> MaxText's `_get_lora_module_path` in `lora_utils.py` automatically handles both **scanned** (e.g., `layers/0/self_attention/...`) and **unscanned** (e.g., `layers/self_attention/...`) layer formats by injecting an optional layer index regex. You only need to define standard, unscanned paths. |
| 46 | + |
| 47 | +If no prefix matches your model name, MaxText falls back to the `default` pattern: |
| 48 | + |
| 49 | +```yaml |
| 50 | +default: "decoder/layers/.*(self_attention/(query|key|value|out)|mlp/(wi_0|wi_1|wo))" |
| 51 | +``` |
| 52 | + |
| 53 | +______________________________________________________________________ |
| 54 | + |
| 55 | +## 2. Integrating Custom Weight Mappings (When is it needed?) |
| 56 | + |
| 57 | +Determining whether you need to implement custom weight mappings depends entirely on your downstream workflow: |
| 58 | + |
| 59 | +### Scenario A: SFT Training & Conversion to PEFT (No Mapping Needed) |
| 60 | + |
| 61 | +If you only need to run SFT fine-tuning with LoRA and then export the adapter back to Hugging Face format using `to_huggingface.py`, **you do not need to write any custom weight mappings.** |
| 62 | + |
| 63 | +- The conversion utility automatically maps, scales, and formats the LoRA adapter parameters back into standard Hugging Face PEFT format based on the base model's existing weight mapping. |
| 64 | + |
| 65 | +### Scenario B: Decoding with the MaxText vLLM Adapter (Mapping is Required) |
| 66 | + |
| 67 | +If you want to perform decoding or run high-performance serving on your adapted model using the **MaxText vLLM adapter** (e.g., via `vllm_decode`), **you must define and register a custom weight mapping.** This allows the vLLM JAX wrapper to dynamically map and feed weights to the vLLM engine. |
| 68 | + |
| 69 | +To add weight mapping for vLLM decode: |
| 70 | + |
| 71 | +1. **Create a Weight Mapping Config**: |
| 72 | + Create a new file in \[src/maxtext/integration/tunix/weight_mapping/\](file:///home/jackyf_google_com/maxtext/src/maxtext/integration/tunix/weight_mapping/) (e.g., `your_model.py`) defining a mapping dataclass. You can refer to \[gemma3.py\](file:///home/jackyf_google_com/maxtext/src/maxtext/integration/tunix/weight_mapping/gemma3.py) or \[llama3.py\](file:///home/jackyf_google_com/maxtext/src/maxtext/integration/tunix/weight_mapping/llama3.py) as templates. |
| 73 | + |
| 74 | + Your class should specify: |
| 75 | + |
| 76 | + - `to_hf_mapping()`: Maps MaxText base parameters to Hugging Face parameters and specifies their sharding axes. |
| 77 | + - `to_hf_hook_fns()`: Custom hook functions for complex weight transformations (e.g., RoPE reordering or query scaling). |
| 78 | + - `lora_to_hf_mappings()`: Custom mapping for LoRA weights if they require different handling. |
| 79 | + |
| 80 | +2. **Register the Mapping**: |
| 81 | + Register your new class in \[src/maxtext/integration/tunix/weight_mapping/__init__.py\](file:///home/jackyf_google_com/maxtext/src/maxtext/integration/tunix/weight_mapping/__init__.py) inside the `StandaloneVllmWeightMapping` class: |
| 82 | + |
| 83 | + ```python |
| 84 | + # Inside StandaloneVllmWeightMapping |
| 85 | + if name.startswith("your_model_name"): |
| 86 | + return YOUR_MODEL_VLLM_MAPPING |
| 87 | + ``` |
0 commit comments