Skip to content

Commit 1a619a2

Browse files
committed
fix(megatron): narrow the output_layer tiedness fallback to expected exceptions
Only two exceptions are anticipated when reading Megatron-LM's untie_embeddings_and_output_weights: ImportError when megatron.training is absent, and AssertionError when get_args() runs before initialize_megatron. Anything else now propagates rather than silently downgrading to "tied", which is the path that exports an unquantized output_layer. Adds coverage for the uninitialized case, which had none. Signed-off-by: James Shen <yueshen@nvidia.com>
1 parent 8264fcb commit 1a619a2

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

modelopt/torch/quantization/plugins/megatron.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ def _output_layer_untied(config) -> bool:
309309
from megatron.training import get_args as _mlm_get_args
310310

311311
return bool(getattr(_mlm_get_args(), "untie_embeddings_and_output_weights", False))
312-
except Exception as e:
312+
except (ImportError, AssertionError) as e:
313+
# ImportError: no megatron.training. AssertionError: get_args() before initialize_megatron.
313314
# Warn once per config rather than on every save and every load.
314315
if not getattr(config, "_modelopt_warned_output_layer_untied", False):
315316
warn_rank_0(

tests/gpu_megatron/torch/quantization/plugins/test_megatron.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,28 @@ class _Config:
18621862
assert warn.call_count == 1
18631863

18641864

1865+
def test_output_layer_untied_warns_when_args_uninitialized():
1866+
"""Megatron-LM importable but not initialized: treated as tied, warned once."""
1867+
1868+
class _Config:
1869+
pass
1870+
1871+
def _uninitialized():
1872+
raise AssertionError("args is not initialized.")
1873+
1874+
fake_training = types.ModuleType("megatron.training")
1875+
fake_training.get_args = _uninitialized
1876+
1877+
config = _Config()
1878+
with (
1879+
patch.dict(sys.modules, {"megatron.training": fake_training}),
1880+
patch("modelopt.torch.quantization.plugins.megatron.warn_rank_0") as warn,
1881+
):
1882+
assert _output_layer_untied(config) is False
1883+
assert _output_layer_untied(config) is False
1884+
assert warn.call_count == 1
1885+
1886+
18651887
def test_output_layer_untied_not_stamped_onto_teacher_config():
18661888
"""A distillation teacher keeps its own tiedness; the student's answer must not leak in."""
18671889

0 commit comments

Comments
 (0)