Skip to content

Commit 8e88f71

Browse files
committed
[data] fix: make CORD target selection deterministic
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
1 parent 4456135 commit 8e88f71

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/megatron/bridge/data/sources/hf_adapters.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import io
1818
import json
19-
import random
2019
import re
2120
from collections.abc import Callable, Iterable, Mapping
2221
from pathlib import Path
@@ -164,7 +163,9 @@ def _cord_v2_adapter(example: Mapping[str, Any], kwargs: Mapping[str, Any]) -> d
164163
gt_jsons = ground_truth["gt_parses"]
165164
else:
166165
gt_jsons = [ground_truth["gt_parse"]]
167-
text = json2token(random.choice(gt_jsons), sort_json_key=True)
166+
# Dataset adaptation runs independently on pipeline stages and after RNG
167+
# restoration, so target selection must not depend on process-global state.
168+
text = json2token(gt_jsons[0], sort_json_key=True)
168169
prompt = str(kwargs.get("prompt", "Describe this image."))
169170
return {
170171
"conversation": [

tests/unit_tests/data/sources/test_hf_adapters.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
22

33
import json
4+
import random
45
from types import SimpleNamespace
56

67
import pytest
@@ -77,6 +78,20 @@ def test_cord_adapter_supports_plural_and_singular_ground_truth(ground_truth):
7778
assert adapted[0]["conversation"][1]["role"] == "assistant"
7879

7980

81+
def test_cord_adapter_multiple_ground_truths_is_independent_of_global_rng():
82+
row = {
83+
"image": SimpleNamespace(),
84+
"ground_truth": json.dumps({"gt_parses": [{"name": "first"}, {"name": "second"}]}),
85+
}
86+
87+
random.seed(42)
88+
first_adaptation = adapt_hf_dataset([row], adapter_name="cord_v2")
89+
random.seed(142)
90+
second_adaptation = adapt_hf_dataset([row], adapter_name="cord_v2")
91+
92+
assert first_adaptation == second_adaptation
93+
94+
8095
@pytest.mark.parametrize(
8196
("adapter_name", "row"),
8297
[

0 commit comments

Comments
 (0)