Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/conversion/hf_megatron_roundtrip_multi_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@
# MiniMax-M2: QK norms stored as bf16 in HF, loaded as fp32 by Megatron config.params_dtype
"q_norm.weight",
"k_norm.weight",
# MiniMax-M2: router gate stored as fp32 in HF, loaded as bf16 via autocast_dtype
# MoE router gate stored as fp32 in Megatron, may be bf16 in HF
"block_sparse_moe.gate.weight",
"mlp.gate.weight",
"moe.gate.weight",
]

# FP8 dtypes whose dequantisation is inherently lossy — allclose is meaningless.
Expand Down
71 changes: 71 additions & 0 deletions examples/models/vlm/ernie_vl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# ERNIE 4.5 VL Examples

This directory contains example scripts for ERNIE 4.5 Vision-Language (VL) MoE models.

## Supported Models

| Model | Parameters | Active Parameters | Type |
|-------|-----------|-------------------|------|
| ERNIE-4.5-VL-28B-A3B-Instruct | 28B | 3B | VL MoE |
| ERNIE-4.5-VL-28B-A3B-Thinking | 28B | 3B | VL MoE |

## Prerequisites

- `--trust-remote-code` is required for the custom HuggingFace model class.
- All scripts use a `WORKSPACE` environment variable for checkpoints. Default: `/workspace`.

```bash
export WORKSPACE=/your/custom/path
```

## Checkpoint Conversion

### Import HF → Megatron

```bash
uv run python examples/conversion/convert_checkpoints.py import \
--hf-model baidu/ERNIE-4.5-VL-28B-A3B-Instruct \
--megatron-path ${WORKSPACE}/ERNIE-4.5-VL-28B-A3B-Instruct \
--torch-dtype bfloat16 \
--trust-remote-code
```

### Export Megatron → HF

```bash
uv run python examples/conversion/convert_checkpoints.py export \
--hf-model baidu/ERNIE-4.5-VL-28B-A3B-Instruct \
--megatron-path ${WORKSPACE}/ERNIE-4.5-VL-28B-A3B-Instruct/iter_0000000 \
--hf-path ${WORKSPACE}/ERNIE-4.5-VL-28B-A3B-Instruct-hf-export \
--trust-remote-code
```

See [conversion.sh](conversion.sh) for the full pipeline including multi-GPU round-trip validation.

## Inference

ERNIE 4.5 VL uses a processor API that differs from other VLMs (e.g., Qwen), so a
dedicated inference script is provided instead of the generic `hf_to_megatron_generate_vlm.py`.

### Run Inference from HF Checkpoint

```bash
uv run python -m torch.distributed.run --nproc_per_node=8 \
examples/models/vlm/ernie_vl/hf_to_megatron_generate_ernie_vl.py \
--hf_model_path baidu/ERNIE-4.5-VL-28B-A3B-Instruct \
--image_path "https://huggingface.co/nvidia/NVIDIA-Nemotron-Nano-12B-v2-VL-BF16/resolve/main/images/table.png" \
--prompt "Describe this image." \
--max_new_tokens 100 \
--tp 2 --pp 1 --ep 4 \
--trust_remote_code
```

See [inference.sh](inference.sh) for a ready-to-use launch script.

## Architecture Notes

ERNIE 4.5 VL uses a **dual-pool MoE** architecture:
- Text and vision experts reside in separate pools within each MoE layer.
- Each pool has its own router and routes tokens independently.
- This design uses `SequentialMLP` (per-expert execution) rather than `GroupedMLP`
(batched GEMM), since the two pools cannot be merged into a single expert group.
53 changes: 53 additions & 0 deletions examples/models/vlm/ernie_vl/conversion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e

# Workspace directory for checkpoints and results
WORKSPACE=${WORKSPACE:-/workspace}
# Supported model variants:
# ERNIE-4.5-VL-28B-A3B-Instruct, ERNIE-4.5-VL-28B-A3B-Thinking
MODEL_NAME=ERNIE-4.5-VL-28B-A3B-Instruct

EP=4
TP=2
PP=1

# Import HF -> Megatron
uv run python examples/conversion/convert_checkpoints.py import \
--hf-model baidu/${MODEL_NAME} \
--megatron-path ${WORKSPACE}/${MODEL_NAME} \
--torch-dtype bfloat16 \
--trust-remote-code

# HF and Megatron models logits comparison validation
uv run python -m torch.distributed.run --nproc_per_node=8 examples/conversion/compare_hf_and_megatron/compare.py \
--hf_model_path baidu/${MODEL_NAME} \
--megatron_model_path ${WORKSPACE}/${MODEL_NAME} \
--model_class "Ernie4_5_VLMoeForConditionalGeneration" \
--image_path "https://huggingface.co/nvidia/NVIDIA-Nemotron-Nano-12B-v2-VL-BF16/resolve/main/images/table.png" \
--prompt "Describe this image." \
--tp ${TP} --pp ${PP} --ep ${EP} \
--trust-remote-code

# Export Megatron -> HF
uv run python examples/conversion/convert_checkpoints.py export \
--hf-model baidu/${MODEL_NAME} \
--megatron-path ${WORKSPACE}/${MODEL_NAME}/iter_0000000 \
--hf-path ${WORKSPACE}/${MODEL_NAME}-hf-export \
--trust-remote-code

# Round-trip validation
uv run python -m torch.distributed.run --nproc_per_node=8 examples/conversion/hf_megatron_roundtrip_multi_gpu.py \
--hf-model-id baidu/${MODEL_NAME} --tp ${TP} --pp ${PP} --ep ${EP} --trust-remote-code
Loading
Loading