|
| 1 | +# GKD |
| 2 | + |
| 3 | +**Version Requirement**: ms-swift >= 3.12 |
| 4 | + |
| 5 | +If you are new to GKD, please refer to the [GKD Documentation](../Instruction/GKD.md) first. |
| 6 | + |
| 7 | +GKD (Generalized Knowledge Distillation) is a training method that transfers knowledge from a teacher model to a student model by computing the Jensen-Shannon Divergence (JSD) loss between their output distributions. |
| 8 | + |
| 9 | +## Feature Support |
| 10 | + |
| 11 | +Megatron GKD currently supports the following features: |
| 12 | + |
| 13 | +- **Training Modes**: Full parameter training and LoRA fine-tuning |
| 14 | +- **Parallelism Strategies**: Context Parallel (CP), Pipeline Parallel (PP), Tensor Parallel (TP), and Expert Parallel (EP) |
| 15 | +- **Model Support**: Compatible with LLMs and MLLMs in Megatron-SWIFT |
| 16 | +- **Teacher Offload**: Supports offloading teacher model to CPU to save GPU memory |
| 17 | +- **Online Generation**: Supports on-policy generation using vLLM for student model |
| 18 | + |
| 19 | +### Current Limitations |
| 20 | + |
| 21 | +- **Teacher Model Online Generation** (`seq_kd=True`): Teacher model generation in Sequential KD mode is not yet supported |
| 22 | +- **Non-vLLM Generation**: On-policy generation currently only supports vLLM |
| 23 | +- **Teacher model with different parallel parameters**: Will be supported in future versions |
| 24 | + |
| 25 | +⚠️ Notes: |
| 26 | +- **On-policy Generation**: Requires vLLM (`--use_vllm true --vllm_mode colocate/server`) |
| 27 | +- When `lmbda > 0` but vLLM is not enabled, it will automatically fall back to off-policy mode (using dataset responses) |
| 28 | +- When `seq_kd=True`, since teacher generation is not yet supported, it will automatically fall back to off-policy mode. If needed, please use [swift infer](../Instruction/Inference-and-deployment.md) to pre-generate responses for the dataset |
| 29 | + |
| 30 | +## Parameters |
| 31 | + |
| 32 | +### GKD-specific Parameters |
| 33 | + |
| 34 | +| Parameter | Type | Default | Description | |
| 35 | +|-----------|------|---------|-------------| |
| 36 | +| `--teacher_model` | str | Required | Path or model ID of the teacher model | |
| 37 | +| `--beta` | float | 0.5 | JSD divergence interpolation coefficient:<br>• 0.0: Forward KL<br>• 0.5: Symmetric JSD<br>• 1.0: Reverse KL | |
| 38 | +| `--lmbda` | float | 0.5 | On-Policy learning probability:<br>• 0.0: Pure Off-Policy<br>• 1.0: Pure On-Policy | |
| 39 | +| `--seq_kd` | bool | False | Use teacher-generated responses (not yet supported) | |
| 40 | +| `--temperature` | float | 0.9 | Temperature for sampling and loss computation | |
| 41 | +| `--sft_alpha` | float | 0 | Mix in a proportion of SFT loss; applied to non-student-generated completions | |
| 42 | +| `--max_completion_length` | int | 512 | Maximum tokens for generation | |
| 43 | + |
| 44 | +### Batch-related Parameters |
| 45 | + |
| 46 | +Same as Megatron SFT, use the following parameters to control batch size: |
| 47 | + |
| 48 | +| Parameter | Description | |
| 49 | +|-----------|-------------| |
| 50 | +| `--micro_batch_size` | Training batch size per GPU | |
| 51 | +| `--global_batch_size` | Global batch size: `micro_batch_size × dp_size × gradient_accumulation_steps` | |
| 52 | + |
| 53 | +## Three Training Modes |
| 54 | + |
| 55 | +GKD supports three training modes, controlled by `lmbda` and `seq_kd` parameters: |
| 56 | + |
| 57 | +### Mode 1: On-Policy Learning |
| 58 | +- Trigger: `random() < lmbda` and `use_vllm=True` |
| 59 | +- Data source: Responses generated by the student model |
| 60 | + |
| 61 | +### Mode 2: Sequential KD (Not Yet Supported) |
| 62 | +- Trigger: `random() >= lmbda` and `seq_kd=True` |
| 63 | +- Data source: Responses generated by the teacher model |
| 64 | + |
| 65 | +### Mode 3: Off-Policy Learning |
| 66 | +- Trigger: Other cases |
| 67 | +- Data source: Labeled responses from the dataset |
| 68 | + |
| 69 | +## Reference |
| 70 | + |
| 71 | +For more parameters, please refer to [Command-line Parameters](./Command-line-parameters.md) |
| 72 | + |
| 73 | +For training scripts, please refer to [Megatron GKD Scripts](https://github.com/modelscope/ms-swift/blob/main/examples/megatron/rlhf/gkd) |
0 commit comments