Skip to content

Commit c6b5368

Browse files
authored
[examples] docs: add sarvam-30b conversion and inference examples (#2963)
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com> Signed-off-by: Yu Yao <yaoyu.094@gmail.com>
1 parent 8d2b0ea commit c6b5368

3 files changed

Lines changed: 153 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 total, 3B active |
10+
| Sarvam 105B | `sarvamai/sarvam-105b` | MoE (128 experts, top-8) | 105B total, 10.3B active |
11+
12+
## Workspace Configuration
13+
14+
All scripts use a `WORKSPACE` environment variable for the base directory. Default: `/workspace`.
15+
16+
```bash
17+
export WORKSPACE=/your/custom/path
18+
```
19+
20+
Directory structure:
21+
- `${WORKSPACE}/models/` - Converted checkpoints
22+
- `${WORKSPACE}/results/` - Training outputs
23+
24+
## Checkpoint Conversion
25+
26+
See [conversion.sh](conversion.sh) for checkpoint conversion examples.
27+
28+
### Import HF → Megatron
29+
30+
```bash
31+
python examples/conversion/convert_checkpoints.py import \
32+
--hf-model sarvamai/sarvam-30b \
33+
--megatron-path ${WORKSPACE}/models/sarvam-30b \
34+
--trust-remote-code
35+
```
36+
37+
### Export Megatron → HF
38+
39+
```bash
40+
python examples/conversion/convert_checkpoints.py export \
41+
--hf-model sarvamai/sarvam-30b \
42+
--megatron-path ${WORKSPACE}/models/sarvam-30b/iter_0000000 \
43+
--hf-path ${WORKSPACE}/models/sarvam-30b-hf-export
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 --ep 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: 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+
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+
36+
# Round-trip validation
37+
uv run python -m torch.distributed.run --nproc_per_node=8 \
38+
examples/conversion/hf_megatron_roundtrip_multi_gpu.py \
39+
--hf-model-id "$HF_MODEL_ID" \
40+
--megatron-load-path "${WORKSPACE}/models/${MODEL_NAME}/iter_0000000" \
41+
--tp 2 --pp 2 --ep 2 \
42+
--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)