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
2 changes: 1 addition & 1 deletion .github/workflows/pr-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ jobs:
strategy:
fail-fast: false
matrix:
info: [{"num_gpus": 0, "test_file": "test_megatron_argument_validation.py"}, {"num_gpus": 0, "test_file": "test_rollout_validation.py"}, {"num_gpus": 0, "test_file": "plugin_contracts/test_plugin_rollout_contracts.py"}, {"num_gpus": 0, "test_file": "plugin_contracts/test_plugin_runtime_hook_contracts.py"}, {"num_gpus": 0, "test_file": "plugin_contracts/test_plugin_path_loading_contracts.py"}, {"num_gpus": 0, "test_file": "plugin_contracts/test_plugin_generate_contracts.py"}, {"num_gpus": 0, "test_file": "test_rm_deepscaler.py"}, {"num_gpus": 0, "test_file": "test_rm_f1.py"}, {"num_gpus": 0, "test_file": "test_rm_gpqa.py"}, {"num_gpus": 0, "test_file": "test_rm_math.py"}, {"num_gpus": 0, "test_file": "test_rm_math_dapo.py"}, {"num_gpus": 0, "test_file": "test_dp_schedule.py"}, {"num_gpus": 0, "test_file": "test_cp_utils.py"}, {"num_gpus": 0, "test_file": "test_metric_report.py"}, {"num_gpus": 0, "test_file": "test_metric_report_dist.py"}, {"num_gpus": 0, "test_file": "test_loss_cp_invariance.py"}, {"num_gpus": 0, "test_file": "test_sample.py"}, {"num_gpus": 0, "test_file": "utils/test_hf_checkpoint_saver.py"}]
info: [{"num_gpus": 0, "test_file": "test_megatron_argument_validation.py"}, {"num_gpus": 0, "test_file": "test_value_temperature.py"}, {"num_gpus": 0, "test_file": "test_rollout_validation.py"}, {"num_gpus": 0, "test_file": "plugin_contracts/test_plugin_rollout_contracts.py"}, {"num_gpus": 0, "test_file": "plugin_contracts/test_plugin_runtime_hook_contracts.py"}, {"num_gpus": 0, "test_file": "plugin_contracts/test_plugin_path_loading_contracts.py"}, {"num_gpus": 0, "test_file": "plugin_contracts/test_plugin_generate_contracts.py"}, {"num_gpus": 0, "test_file": "test_rm_deepscaler.py"}, {"num_gpus": 0, "test_file": "test_rm_f1.py"}, {"num_gpus": 0, "test_file": "test_rm_gpqa.py"}, {"num_gpus": 0, "test_file": "test_rm_math.py"}, {"num_gpus": 0, "test_file": "test_rm_math_dapo.py"}, {"num_gpus": 0, "test_file": "test_dp_schedule.py"}, {"num_gpus": 0, "test_file": "test_cp_utils.py"}, {"num_gpus": 0, "test_file": "test_metric_report.py"}, {"num_gpus": 0, "test_file": "test_metric_report_dist.py"}, {"num_gpus": 0, "test_file": "test_loss_cp_invariance.py"}, {"num_gpus": 0, "test_file": "test_sample.py"}, {"num_gpus": 0, "test_file": "utils/test_hf_checkpoint_saver.py"}]
defaults:
run:
working-directory: ${{ github.workspace }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pr-test.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
'cpu': True,
'tests': [
{'test_file': 'test_megatron_argument_validation.py', 'num_gpus': 0},
{'test_file': 'test_value_temperature.py', 'num_gpus': 0},
{'test_file': 'test_rollout_validation.py', 'num_gpus': 0},
{'test_file': 'plugin_contracts/test_plugin_rollout_contracts.py', 'num_gpus': 0},
{'test_file': 'plugin_contracts/test_plugin_runtime_hook_contracts.py', 'num_gpus': 0},
Expand Down
54 changes: 54 additions & 0 deletions tests/test_value_temperature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
import types
from argparse import Namespace

import pytest
import torch


NUM_GPUS = 0


def test_get_values_does_not_apply_rollout_temperature(monkeypatch):
previous_loss = sys.modules.pop("vime.backends.megatron_utils.loss", None)
previous_cp_utils = sys.modules.pop("vime.backends.megatron_utils.cp_utils", None)

mpu_stub = types.SimpleNamespace(
get_context_parallel_world_size=lambda: 1,
get_context_parallel_rank=lambda: 0,
)
megatron_mod = types.ModuleType("megatron")
core_mod = types.ModuleType("megatron.core")
core_mod.mpu = mpu_stub
monkeypatch.setitem(sys.modules, "megatron", megatron_mod)
monkeypatch.setitem(sys.modules, "megatron.core", core_mod)

try:
from vime.backends.megatron_utils.loss import get_values

args = Namespace(qkv_format="thd", rollout_temperature=0.5, allgather_cp=False)
logits = torch.tensor([[[1.0], [2.0], [3.0], [4.0]]], dtype=torch.float32)
tokens = [torch.tensor([10, 11, 12, 13], dtype=torch.long)]

_, result = get_values(
logits,
args=args,
unconcat_tokens=tokens,
total_lengths=[4],
response_lengths=[2],
)

torch.testing.assert_close(result["values"][0], torch.tensor([2.0, 3.0]))
finally:
if previous_loss is None:
sys.modules.pop("vime.backends.megatron_utils.loss", None)
else:
sys.modules["vime.backends.megatron_utils.loss"] = previous_loss
if previous_cp_utils is None:
sys.modules.pop("vime.backends.megatron_utils.cp_utils", None)
else:
sys.modules["vime.backends.megatron_utils.cp_utils"] = previous_cp_utils
Comment on lines +13 to +50

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of manually popping modules from sys.modules and restoring them in a try...finally block, you can use pytest's monkeypatch.delitem with raising=False. This is more idiomatic, less error-prone, and automatically handles the cleanup/restoration of sys.modules after the test completes.

    monkeypatch.delitem(sys.modules, "vime.backends.megatron_utils.loss", raising=False)
    monkeypatch.delitem(sys.modules, "vime.backends.megatron_utils.cp_utils", raising=False)

    mpu_stub = types.SimpleNamespace(
        get_context_parallel_world_size=lambda: 1,
        get_context_parallel_rank=lambda: 0,
    )
    megatron_mod = types.ModuleType("megatron")
    core_mod = types.ModuleType("megatron.core")
    core_mod.mpu = mpu_stub
    monkeypatch.setitem(sys.modules, "megatron", megatron_mod)
    monkeypatch.setitem(sys.modules, "megatron.core", core_mod)

    from vime.backends.megatron_utils.loss import get_values

    args = Namespace(qkv_format="thd", rollout_temperature=0.5, allgather_cp=False)
    logits = torch.tensor([[[1.0], [2.0], [3.0], [4.0]]], dtype=torch.float32)
    tokens = [torch.tensor([10, 11, 12, 13], dtype=torch.long)]

    _, result = get_values(
        logits,
        args=args,
        unconcat_tokens=tokens,
        total_lengths=[4],
        response_lengths=[2],
    )

    torch.testing.assert_close(result["values"][0], torch.tensor([2.0, 3.0]))



if __name__ == "__main__":
raise SystemExit(pytest.main([__file__]))
Loading