-
Notifications
You must be signed in to change notification settings - Fork 279
Support mbridge distillation for any_model #904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielkorzekwa
wants to merge
6
commits into
dkorzekwa/any_model
Choose a base branch
from
dkorzekwa/any_model_mbridge_distillation
base: dkorzekwa/any_model
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+935
−14
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7a7ed1e
Add MBridge distillation support for AnyModel checkpoints
danielkorzekwa 562f46b
Add missing files from modelopt/torch/puzzletron/export/mbridge
danielkorzekwa d244ca7
A tutorial on mbridge distillation for puzzletron/any_model
danielkorzekwa 018b208
Update distillation readme
danielkorzekwa 5f30fa9
Improve mbridge tutorial for anymodel
danielkorzekwa 5f73765
Fixing distillation for heterogenous models (call self.teacher.finali…
danielkorzekwa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # Knowledge Distillation with Megatron-Bridge | ||
|
|
||
| This guide shows how to perform knowledge distillation on Puzzletron-compressed AnyModel checkpoints using Megatron-Bridge. | ||
|
|
||
| ## Overview | ||
|
|
||
| 1. Set up the environment with Megatron-Bridge | ||
| 2. Convert AnyModel checkpoints (student and teacher) to Megatron-Bridge format | ||
| 3. Run knowledge distillation training | ||
|
|
||
| ## Setup | ||
|
|
||
| > **Temporary Setup:** The NeMo docker container includes Megatron-Bridge (main branch), but Puzzletron requires a specific version/branch of Megatron-Bridge that is not included by default. This manual setup is required to use the Puzzletron-compatible version. Once the container includes the required version, this setup step will no longer be necessary. | ||
|
|
||
| **Note:** Set `$WORKSPACE` to your project root directory before running these commands: | ||
|
|
||
| ```bash | ||
| export WORKSPACE=/path/to/your/project | ||
| ``` | ||
|
|
||
| 1. **Clone Megatron-Bridge:** | ||
|
|
||
| Clone [Megatron-Bridge](https://github.com/NVIDIA-NeMo/Megatron-Bridge) and checkout the specific commit required for Puzzletron: | ||
|
|
||
| ```bash | ||
| cd $WORKSPACE | ||
| git clone https://github.com/NVIDIA-NeMo/Megatron-Bridge.git | ||
| cd Megatron-Bridge | ||
| git checkout 960a718cb8989676b258e107d538642717e22e39 | ||
| ``` | ||
|
|
||
| 2. **Initialize Megatron-Bridge submodules:** | ||
|
|
||
| ```bash | ||
| cd $WORKSPACE/Megatron-Bridge | ||
| git submodule init | ||
| git submodule update | ||
| ``` | ||
|
|
||
| 3. **Start Docker container with mounts:** | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If these same steps work for 26.02, can we use that instead? |
||
|
|
||
| Use the [NeMo 25.11 container](https://catalog.ngc.nvidia.com/orgs/nvidia/containers/nemo?version=25.11): | ||
|
|
||
| ```bash | ||
| docker run --gpus all -it --rm \ | ||
| -v $WORKSPACE:/workspace \ | ||
| -v $WORKSPACE/Megatron-Bridge/3rdparty/Megatron-LM:/opt/megatron-lm \ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fyi, for 26.02, Megatron-LM is at |
||
| nvcr.io/nvidia/nemo:25.11 \ | ||
| /bin/bash | ||
| ``` | ||
|
|
||
| **Note:** The mount `/opt/megatron-lm` is required because Megatron-Bridge depends on the Megatron-LM submodule. | ||
|
|
||
| 4. **Set up the environment inside the container:** | ||
|
|
||
| ```bash | ||
| export PYTHONPATH="/workspace/Megatron-Bridge/src:/workspace/Model-Optimizer:${PYTHONPATH}" | ||
| ``` | ||
|
|
||
| ## Dataset Preparation | ||
|
|
||
| This section describes how to prepare datasets for knowledge distillation. We provide examples using a toy dataset (WikiText-103) for illustration purposes, and note how to adapt the process for production datasets like Nemotron-Post-Training-Dataset-v2. | ||
|
|
||
| > **Note:** For actual knowledge distillation, use a larger, more representative dataset like [Nemotron-Post-Training-Dataset-v2](https://huggingface.co/datasets/nvidia/Nemotron-Post-Training-Dataset-v2). | ||
|
|
||
| ### Step 1: Download Dataset | ||
|
|
||
| First, download the dataset and save it in JSONL format. For WikiText-103, you can use the following script: | ||
|
|
||
| ```python | ||
| # download_hf_wikitext_dataset.py | ||
| import json | ||
| import os | ||
| from datasets import load_dataset | ||
|
|
||
| DATA_PATH = "path/to/hf_datasets/wikitext-103-v1" | ||
| # Load the WikiText-103 dataset | ||
| dataset = load_dataset("wikitext", "wikitext-103-v1", split="train") | ||
|
|
||
| # Define the destination folder | ||
| os.makedirs(DATA_PATH, exist_ok=True) | ||
|
|
||
| # Save splits to JSONL files | ||
| with open(f"{DATA_PATH}/wikitext-train.jsonl", "w") as file: | ||
| file.writelines(json.dumps(item) + "\n" for item in dataset) | ||
|
|
||
| print(f"Raw dataset saved to {DATA_PATH}/wikitext-train.jsonl") | ||
| ``` | ||
|
|
||
| ### Step 2: Tokenize Dataset | ||
|
|
||
| Next, tokenize the JSONL dataset using the tokenizer from your model. This converts the text data into token IDs that can be used for training: | ||
|
|
||
| ```python | ||
| # tokenize_wikitext_dataset.py | ||
| from modelopt.torch.utils.plugins import megatron_preprocess_data | ||
|
|
||
| DATA_PATH = "path/to/hf_datasets/wikitext-103-v1" | ||
| HF_MODEL_NAME_OR_PATH = "path/to/your/model/checkpoint" | ||
|
|
||
| megatron_preprocess_data( | ||
| input_path=f"{DATA_PATH}/wikitext-train.jsonl", | ||
| output_dir=DATA_PATH, | ||
| tokenizer_name_or_path=HF_MODEL_NAME_OR_PATH, | ||
| json_keys=["text"], | ||
| workers=32, | ||
| log_interval=100000, | ||
| ) | ||
| ``` | ||
|
|
||
| ## Step 1: Convert Checkpoints to Megatron-Bridge Format | ||
|
|
||
| Convert both student and teacher checkpoints: | ||
|
|
||
| ```bash | ||
| # Convert student checkpoint | ||
| torchrun --nproc_per_node=1 examples/puzzletron/mbridge_distillation/import_anymodel_to_mbridge.py \ | ||
| --input-ckpt-path /path/to/student/anymodel/checkpoint \ | ||
| --output-ckpt-path /path/to/student/mbridge/checkpoint | ||
|
|
||
| # Convert teacher checkpoint | ||
| torchrun --nproc_per_node=1 examples/puzzletron/mbridge_distillation/import_anymodel_to_mbridge.py \ | ||
| --input-ckpt-path /path/to/teacher/anymodel/checkpoint \ | ||
| --output-ckpt-path /path/to/teacher/mbridge/checkpoint | ||
| ``` | ||
|
|
||
| ## Step 2: Run Knowledge Distillation | ||
|
|
||
| Run distillation with tokenized dataset: | ||
|
|
||
| ```bash | ||
| torchrun --nproc_per_node=8 examples/puzzletron/mbridge_distillation/distill_anymodel.py \ | ||
| --student-mbridge-ckpt /path/to/student/mbridge/checkpoint/iter_0000000 \ | ||
| --teacher-mbridge-ckpt /path/to/teacher/mbridge/checkpoint/iter_0000000 \ | ||
| --data-path /path/to/tokenized/dataset \ | ||
| --output-dir ./distilled_output \ | ||
| dataset.sequence_length=8192 \ | ||
| model.tensor_model_parallel_size=8 \ | ||
| model.teacher.tensor_model_parallel_size=8 \ | ||
| train.global_batch_size=4 \ | ||
| train.micro_batch_size=1 \ | ||
| train.train_iters=5000 \ | ||
| logger.log_interval=1 | ||
| ``` | ||
|
|
||
| The distilled checkpoint will be saved to `--output-dir`. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Megatron-Bridge is already cloned in the container at
/opt/Megatron-Bridge. Why dont we just do following inside the container:cd /opt/Megatron-Bridge && git checkout 960a718cb8989676b258e107d538642717e22e39?