Skip to content

Commit 525f843

Browse files
fix: PEFT parameter statistics only count the first model chunk
Signed-off-by: andrewwhitecdw <andrewwhitecdw@users.noreply.github.com>
1 parent 7b3c40b commit 525f843

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎src/megatron/bridge/training/setup.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -613,14 +613,15 @@ def _apply_peft_transformation(peft, base_model: list[MegatronModule]) -> list[M
613613
peft.set_params_to_save(transformed_model)
614614

615615
# Log PEFT statistics
616-
model_to_analyze = transformed_model[0] if isinstance(transformed_model, list) else transformed_model
616+
model_chunks = transformed_model if isinstance(transformed_model, list) else [transformed_model]
617617
total_params = 0
618618
trainable_params = 0
619-
for param in model_to_analyze.parameters():
620-
param_count = param.numel()
621-
total_params += param_count
622-
if param.requires_grad:
623-
trainable_params += param_count
619+
for model_chunk in model_chunks:
620+
for param in model_chunk.parameters():
621+
param_count = param.numel()
622+
total_params += param_count
623+
if param.requires_grad:
624+
trainable_params += param_count
624625

625626
print_rank_0("PEFT Statistics:")
626627
print_rank_0(f" Total parameters: {total_params:,}")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pytest
2+
import torch
3+
import torch.nn as nn
4+
5+
from megatron.bridge.training.setup import _apply_peft_transformation
6+
7+
8+
class _DummyModelChunk(nn.Module):
9+
def __init__(self, total, trainable):
10+
super().__init__()
11+
self.weight = nn.Parameter(torch.zeros(total), requires_grad=trainable)
12+
13+
14+
class _DummyPEFT:
15+
def __call__(self, base_model, training=True):
16+
return base_model
17+
18+
def set_params_to_save(self, model):
19+
pass
20+
21+
22+
def test_apply_peft_transformation_counts_all_chunks():
23+
"""PEFT parameter stats must account for every model chunk, not only chunk 0."""
24+
chunk0 = _DummyModelChunk(10, False)
25+
chunk1 = _DummyModelChunk(5, True)
26+
peft = _DummyPEFT()
27+
28+
logs = []
29+
30+
def fake_print_rank_0(msg):
31+
logs.append(msg)
32+
33+
import megatron.bridge.training.setup as setup_module
34+
35+
orig_print_rank_0 = setup_module.print_rank_0
36+
setup_module.print_rank_0 = fake_print_rank_0
37+
try:
38+
_apply_peft_transformation(peft, [chunk0, chunk1])
39+
finally:
40+
setup_module.print_rank_0 = orig_print_rank_0
41+
42+
assert any("Total parameters: 15" in msg for msg in logs)
43+
assert any("Trainable parameters: 5" in msg for msg in logs)

0 commit comments

Comments
 (0)