Skip to content

Commit 4365402

Browse files
authored
docs: add usage example for mcore --> hf converter (#807)
Signed-off-by: ashors1 <[email protected]>
1 parent d94c41a commit 4365402

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,17 @@ uv run python examples/converters/convert_dcp_to_hf.py \
412412
--dcp-ckpt-path results/grpo/step_170/policy/weights/ \
413413
--hf-ckpt-path results/grpo/hf
414414
```
415+
416+
If you have a model saved in Megatron format, you can use the following command to convert it to Hugging Face format prior to running evaluation:
417+
418+
```sh
419+
# Example for a GRPO checkpoint at step 170
420+
uv run python examples/converters/convert_megatron_to_hf.py \
421+
--config results/grpo/step_170/config.yaml \
422+
--dcp-ckpt-path results/grpo/step_170/policy/weights/iter_0000000 \
423+
--hf-ckpt-path results/grpo/hf
424+
```
425+
415426
> **Note:** Adjust the paths according to your training output directory structure.
416427
417428
For an in-depth explanation of checkpointing, refer to the [Checkpointing documentation](docs/design-docs/checkpointing.md).

docs/design-docs/checkpointing.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
# Checkpointing with Hugging Face Models
1+
# Exporting Checkpoints to Hugging Face Format
22

33
NeMo RL provides two checkpoint formats for Hugging Face models: Torch distributed and Hugging Face format. Torch distributed is used by default for efficiency, and Hugging Face format is provided for compatibility with Hugging Face's `AutoModel.from_pretrained` API. Note that Hugging Face format checkpoints save only the model weights, ignoring the optimizer states. It is recommended to use Torch distributed format to save intermediate checkpoints and to save a Hugging Face checkpoint only at the end of training.
44

5-
A checkpoint converter is provided to convert a Torch distributed checkpoint checkpoint to Hugging Face format after training:
5+
## Converting Torch Distributed Checkpoints to Hugging Face Format
6+
7+
A checkpoint converter is provided to convert a Torch distributed checkpoint to Hugging Face format after training:
68

79
```sh
810
uv run examples/converters/convert_dcp_to_hf.py --config=<YAML CONFIG USED DURING TRAINING> <ANY CONFIG OVERRIDES USED DURING TRAINING> --dcp-ckpt-path=<PATH TO DIST CHECKPOINT TO CONVERT> --hf-ckpt-path=<WHERE TO SAVE HF CHECKPOINT>
@@ -17,3 +19,13 @@ CKPT_DIR=results/sft/step_10
1719
uv run examples/converters/convert_dcp_to_hf.py --config=$CKPT_DIR/config.yaml --dcp-ckpt-path=$CKPT_DIR/policy/weights --hf-ckpt-path=${CKPT_DIR}-hf
1820
rsync -ahP $CKPT_DIR/policy/tokenizer ${CKPT_DIR}-hf/
1921
```
22+
23+
## Converting Megatron Checkpoints to Hugging Face Format
24+
25+
For models that were originally trained using the Megatron-LM backend, a separate converter is available to convert Megatron checkpoints to Hugging Face format. This script requires Megatron-Core, so make sure to launch the conversion with the `mcore` extra. For example,
26+
27+
```sh
28+
CKPT_DIR=results/sft/step_10
29+
30+
uv run --extra mcore examples/converters/convert_megatron_to_hf.py --config=$CKPT_DIR/config.yaml --megatron-ckpt-path=$CKPT_DIR/policy/weights/iter_0000000/ --hf-ckpt-path=<path_to_save_hf_ckpt>
31+
```

examples/converters/convert_megatron_to_hf.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818

1919
from nemo_rl.models.megatron.community_import import export_model_from_megatron
2020

21+
""" NOTE: this script requires mcore. Make sure to launch with the mcore extra:
22+
uv run --extra mcore python examples/converters/convert_megatron_to_hf.py \
23+
--config <path_to_ckpt>/config.yaml \
24+
--megatron-ckpt-path <path_to_ckpt>/policy/weights/iter_xxxxx \
25+
--hf-ckpt-path <path_to_save_hf_ckpt>
26+
"""
27+
2128

2229
def parse_args():
2330
"""Parse command line arguments."""

0 commit comments

Comments
 (0)