|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +# How to Add Support for a New Model |
| 6 | + |
| 7 | +To integrate a new model into **ROLL**, you must supply: |
| 8 | + |
| 9 | +1. at least one **inference** implementation, and |
| 10 | +2. at least one **training** implementation. |
| 11 | + |
| 12 | +| Phase | Pick ≥ 1 backend | |
| 13 | +|-----------|-----------------| |
| 14 | +| Inference | `vllm`, `sglang` | |
| 15 | +| Training | `DeepSpeed`, `Megatron` | |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## 1. Inference Strategies |
| 20 | + |
| 21 | +### 1.1 `vllm` |
| 22 | +Follow the official guide: |
| 23 | +https://docs.vllm.ai/en/latest/contributing/model/registration.html#out-of-tree-models |
| 24 | + |
| 25 | +### 1.2 `sglang` |
| 26 | +Follow the official guide: |
| 27 | +https://docs.sglang.ai/supported_models/support_new_models.html |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 2. Training Strategies |
| 32 | + |
| 33 | +### 2.1 `DeepSpeed` |
| 34 | + |
| 35 | +1. Ensure your model can be loaded by |
| 36 | + ```python |
| 37 | + transformers.AutoModelForCausalLM.from_pretrained(...) |
| 38 | + ``` |
| 39 | + If not, add the model implementation directly to the ROLL repository. |
| 40 | +2. Make the model inherit from `transformers.PreTrainedModel`. |
| 41 | +3. Make the model can be loaded in `roll/models/model_providers.py`. |
| 42 | + |
| 43 | +Once these steps are complete, you can: |
| 44 | +- train with the `deepspeed_train` strategy for `actor_train` worker, and |
| 45 | +- with `hf_infer` or `deepspeed_infer` strategy for the `reference` worker. |
| 46 | + |
| 47 | +### 2.2 `Megatron` |
| 48 | + |
| 49 | +To integrate a Hugging Face model with the `Megatron` training strategy, you need to provide a conversion template. This template defines how to map the model's configuration and weights from the Hugging Face format to the Megatron-Core format. |
| 50 | + |
| 51 | +#### 1. For Standard Transformer Models |
| 52 | + |
| 53 | +If your model has a standard Transformer architecture compatible with `mcore.GPTModel`, you only need to register a new conversion template. All templates are located in `mcore_adapter/src/mcore_adapter/models/converter/template.py`. |
| 54 | + |
| 55 | +To add a new template, you'll call the `register_template` function at the end of this file. Here’s a detailed guide on how to construct the arguments for this function. |
| 56 | + |
| 57 | +##### Registering a New Template |
| 58 | + |
| 59 | +The core of the integration is the `register_template` function. Let's break down its main parameters: |
| 60 | + |
| 61 | +```python |
| 62 | +register_template( |
| 63 | + hf_model_type, |
| 64 | + config_hf_to_mca, |
| 65 | + weight_converters, |
| 66 | + hf_layer_prefix, |
| 67 | + constant_mca_config={}, |
| 68 | + hf_invalid_keys=[], |
| 69 | + ... |
| 70 | +) |
| 71 | +``` |
| 72 | + |
| 73 | +**a. `hf_model_type` (str):** |
| 74 | +This is the most crucial parameter. It must exactly match the `model_type` field in the model's Hugging Face `config.json` file. The converter uses this string to look up the correct template. |
| 75 | + |
| 76 | +**b. `hf_layer_prefix` (str):** |
| 77 | +This specifies the prefix for the transformer layers in the Hugging Face model's state dictionary. For most models, this will be something like `"model.layers."`. |
| 78 | + |
| 79 | +**c. `config_hf_to_mca` (Dict[str, str]):** |
| 80 | +This dictionary maps configuration parameter names from the Hugging Face `config.json` to their corresponding names in the Megatron-Core `TransformerConfig`. |
| 81 | + |
| 82 | +**d. `weight_converters` (List[ConverOp]):** |
| 83 | +This is a list of converter operations that define how to transform weights from the HF format to the MCA format. Each operation is an instance of a `ConverOp` subclass. |
| 84 | + |
| 85 | +Common converter operations include: |
| 86 | +- **`RenameConverOp`**: Used for weights that only need to be renamed. |
| 87 | + ```python |
| 88 | + # Renames 'lm_head.weight' in HF to 'output_layer.weight' in MCA |
| 89 | + RenameConverOp(hf_names="lm_head.weight", mca_names="output_layer.weight") |
| 90 | + ``` |
| 91 | +- **`StackConverOp`**: Stacks multiple HF tensors into a single MCA tensor. This is commonly used for the gate and up projections in SwiGLU layers. |
| 92 | + ```python |
| 93 | + # Stacks two HF weights into one MCA weight for the first feed-forward layer |
| 94 | + StackConverOp( |
| 95 | + hf_names=[".mlp.gate_proj.weight", ".mlp.up_proj.weight"], |
| 96 | + mca_names=".mlp.linear_fc1.weight", |
| 97 | + dim=0 |
| 98 | + ) |
| 99 | + ``` |
| 100 | +- **`QKVConverOp`**: Fuses the separate Query, Key, and Value weight tensors from HF into a single, interleaved QKV tensor required by Megatron-Core. |
| 101 | + ```python |
| 102 | + # Fuses Q, K, and V weights into a single QKV weight |
| 103 | + QKVConverOp( |
| 104 | + hf_names=[".self_attn.q_proj.weight", ".self_attn.k_proj.weight", ".self_attn.v_proj.weight"], |
| 105 | + mca_names=".self_attention.linear_qkv.weight", |
| 106 | + ) |
| 107 | + ``` |
| 108 | + |
| 109 | +**e. `constant_mca_config` (Dict[str, Any]):** |
| 110 | +This dictionary defines Megatron-Core configuration values that are constant for the model and are not available in the HF config. |
| 111 | + |
| 112 | + |
| 113 | +#### 2. For Models with Custom Components |
| 114 | + |
| 115 | +If the model includes unique components not found in a standard `mcore.GPTModel` (e.g., Vision Transformer blocks in a multimodal model like Qwen2-VL), you will need to: |
| 116 | +1. Implement a new model class that inherits from `mcore.GPTModel` and adds the custom logic. You can use the implementations for `qwen2-vl` and `qwen2.5-vl` in the repository as a reference. |
| 117 | +2. Register a template for the parts of the model that are standard, as described above. The template can also handle renaming for the custom parts (e.g., `RenameConverOp(hf_names="visual.{}", mca_names="vision_model.{}")`). |
| 118 | + |
| 119 | +After completing these steps, you can: |
| 120 | +- train with the `megatron_train` strategy for the `actor_train` worker, and |
| 121 | +- use the `megatron_infer` strategy for the `reference` worker. |
0 commit comments