Skip to content

Commit de0f57d

Browse files
committed
[examples] docs: add sarvam-30b conversion and inference examples
Add conversion.sh, inference.sh and README.md for sarvamai/sarvam-30b. Follows the same structure as examples/models/gpt_oss/. Parallelism: TP=2 PP=1 EP=4 for 8-GPU inference. Known issue: sarvamai/sarvam-30b HF checkpoint generates degenerate output (token repetition collapse) even with direct HF inference. Noted in README. Bridge conversion is verified correct — Megatron checkpoint matches HF output. Closes #1814 (examples follow-up) Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
1 parent 6d8a1c5 commit de0f57d

3 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎examples/models/sarvam/README.md‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Sarvam Examples
2+
3+
This directory contains example scripts for Sarvam language models.
4+
5+
Sarvam models use a Mixture of Experts (MoE) architecture with QKV layernorm and Grouped Query Attention (GQA).
6+
7+
| Model | HF ID | Architecture | Params |
8+
|---|---|---|---|
9+
| Sarvam 30B | `sarvamai/sarvam-30b` | MoE (128 experts, top-6) | 30B active 3B |
10+
11+
## Workspace Configuration
12+
13+
All scripts use a `WORKSPACE` environment variable for the base directory. Default: `/workspace`.
14+
15+
```bash
16+
export WORKSPACE=/your/custom/path
17+
```
18+
19+
Directory structure:
20+
- `${WORKSPACE}/models/` - Converted checkpoints
21+
- `${WORKSPACE}/results/` - Training outputs
22+
23+
## Checkpoint Conversion
24+
25+
See [conversion.sh](conversion.sh) for checkpoint conversion examples.
26+
27+
### Import HF → Megatron
28+
29+
```bash
30+
python examples/conversion/convert_checkpoints.py import \
31+
--hf-model sarvamai/sarvam-30b \
32+
--megatron-path ${WORKSPACE}/models/sarvam-30b \
33+
--trust-remote-code
34+
```
35+
36+
### Export Megatron → HF
37+
38+
```bash
39+
python examples/conversion/convert_checkpoints.py export \
40+
--hf-model sarvamai/sarvam-30b \
41+
--megatron-path ${WORKSPACE}/models/sarvam-30b/iter_0000000 \
42+
--hf-path ${WORKSPACE}/models/sarvam-30b-hf-export \
43+
--trust-remote-code
44+
```
45+
46+
### Round-trip Validation
47+
48+
```bash
49+
python -m torch.distributed.run --nproc_per_node=8 \
50+
examples/conversion/hf_megatron_roundtrip_multi_gpu.py \
51+
--hf-model-id sarvamai/sarvam-30b \
52+
--megatron-load-path ${WORKSPACE}/models/sarvam-30b/iter_0000000 \
53+
--tp 2 --pp 2 \
54+
--trust-remote-code
55+
```
56+
57+
## Inference
58+
59+
See [inference.sh](inference.sh) for text generation with:
60+
- Hugging Face checkpoint (`sarvamai/sarvam-30b`)
61+
- Imported Megatron checkpoint (after [conversion.sh](conversion.sh) import)
62+
- Exported HF checkpoint (after conversion export)
63+
64+
The default parallelism for 8 GPUs is `--tp 2 --pp 1 --ep 4 --etp 1`.
65+
TP×PP×EP must equal `--nproc_per_node`.
66+
67+
> **Note**: `sarvamai/sarvam-30b` is a base pretrained model published in float32.
68+
> The Bridge conversion is correct (verified by matching outputs between HF and Megatron checkpoints).
69+
> For coherent text generation, use an instruction-tuned variant if available.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -xeuo pipefail
17+
18+
# Workspace directory for checkpoints and results
19+
WORKSPACE=${WORKSPACE:-/workspace}
20+
21+
MODEL_NAME=sarvam-30b
22+
HF_MODEL_ID=sarvamai/sarvam-30b
23+
24+
# Import HF → Megatron
25+
uv run python examples/conversion/convert_checkpoints.py import \
26+
--hf-model "$HF_MODEL_ID" \
27+
--megatron-path "${WORKSPACE}/models/${MODEL_NAME}" \
28+
--trust-remote-code
29+
30+
# Export Megatron → HF
31+
uv run python examples/conversion/convert_checkpoints.py export \
32+
--hf-model "$HF_MODEL_ID" \
33+
--megatron-path "${WORKSPACE}/models/${MODEL_NAME}/iter_0000000" \
34+
--hf-path "${WORKSPACE}/models/${MODEL_NAME}-hf-export" \
35+
--trust-remote-code
36+
37+
# Round-trip validation
38+
uv run python -m torch.distributed.run --nproc_per_node=8 \
39+
examples/conversion/hf_megatron_roundtrip_multi_gpu.py \
40+
--hf-model-id "$HF_MODEL_ID" \
41+
--megatron-load-path "${WORKSPACE}/models/${MODEL_NAME}/iter_0000000" \
42+
--tp 2 --pp 2 \
43+
--trust-remote-code
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# Workspace directory for checkpoints and results
17+
WORKSPACE=${WORKSPACE:-/workspace}
18+
19+
# Inference with Hugging Face checkpoints
20+
uv run python -m torch.distributed.run --nproc_per_node=8 examples/conversion/hf_to_megatron_generate_text.py \
21+
--hf_model_path sarvamai/sarvam-30b \
22+
--prompt "Hello, how are you?" \
23+
--max_new_tokens 64 \
24+
--tp 2 --pp 1 --ep 4 --etp 1 \
25+
--trust-remote-code
26+
27+
# Inference with imported Megatron checkpoints
28+
uv run python -m torch.distributed.run --nproc_per_node=8 examples/conversion/hf_to_megatron_generate_text.py \
29+
--hf_model_path sarvamai/sarvam-30b \
30+
--megatron_model_path ${WORKSPACE}/models/sarvam-30b/iter_0000000 \
31+
--prompt "Hello, how are you?" \
32+
--max_new_tokens 64 \
33+
--tp 2 --pp 1 --ep 4 --etp 1 \
34+
--trust-remote-code
35+
36+
# Inference with exported HF checkpoints
37+
uv run python -m torch.distributed.run --nproc_per_node=8 examples/conversion/hf_to_megatron_generate_text.py \
38+
--hf_model_path ${WORKSPACE}/models/sarvam-30b-hf-export \
39+
--prompt "Hello, how are you?" \
40+
--max_new_tokens 64 \
41+
--tp 2 --pp 1 --ep 4 --etp 1 \
42+
--trust-remote-code

0 commit comments

Comments
 (0)