|
1 | 1 | # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
2 | 2 |
|
3 | 3 | import importlib.util |
| 4 | +import json |
4 | 5 | import pathlib |
5 | 6 | import sys |
6 | 7 | from types import SimpleNamespace |
7 | 8 |
|
8 | 9 | import pytest |
9 | 10 |
|
10 | | -from megatron.bridge.data.builders import GPTSFTDatasetConfig, HFDatasetSourceConfig |
| 11 | +import megatron.bridge.data.builders.gpt_sft as gpt_sft_builder |
| 12 | +from megatron.bridge.data.builders import ( |
| 13 | + GPTSFTDatasetConfig, |
| 14 | + HFDatasetSourceConfig, |
| 15 | + PromptCompletionSFTPreprocessingConfig, |
| 16 | +) |
11 | 17 |
|
12 | 18 |
|
13 | 19 | pytestmark = pytest.mark.unit |
@@ -47,3 +53,45 @@ def test_data_path_replaces_named_source_with_custom_json_source(): |
47 | 53 | assert config.dataset.hf_validation_proportion == 0.1 |
48 | 54 | finally: |
49 | 55 | sys.modules.pop(name, None) |
| 56 | + |
| 57 | + |
| 58 | +def test_data_path_materializes_advertised_messages_schema(monkeypatch, tmp_path): |
| 59 | + name = "qwen3_next_finetune_messages_data" |
| 60 | + try: |
| 61 | + module = _load_example_module(name) |
| 62 | + config = SimpleNamespace( |
| 63 | + dataset=GPTSFTDatasetConfig( |
| 64 | + seq_length=2048, |
| 65 | + hf_dataset=HFDatasetSourceConfig(dataset_name="squad"), |
| 66 | + hf_validation_proportion=0.1, |
| 67 | + do_test=False, |
| 68 | + preprocessing=PromptCompletionSFTPreprocessingConfig( |
| 69 | + prompt_column="input", |
| 70 | + completion_column="output", |
| 71 | + separator=" ", |
| 72 | + ), |
| 73 | + ) |
| 74 | + ) |
| 75 | + messages_rows = [ |
| 76 | + { |
| 77 | + "id": index, |
| 78 | + "messages": [ |
| 79 | + {"role": "user", "content": f"Question {index}"}, |
| 80 | + {"role": "assistant", "content": f"Answer {index}"}, |
| 81 | + ], |
| 82 | + } |
| 83 | + for index in range(10) |
| 84 | + ] |
| 85 | + monkeypatch.setattr(gpt_sft_builder, "load_and_adapt_hf_dataset", lambda _: messages_rows) |
| 86 | + |
| 87 | + module._replace_with_custom_data_path(config, "/data/custom.jsonl") |
| 88 | + gpt_sft_builder.materialize_hf_dataset(config.dataset, tmp_path) |
| 89 | + |
| 90 | + materialized_rows = [] |
| 91 | + for split in ("training", "validation"): |
| 92 | + with (tmp_path / f"{split}.jsonl").open(encoding="utf-8") as input_file: |
| 93 | + materialized_rows.extend(json.loads(line) for line in input_file) |
| 94 | + assert len(materialized_rows) == len(messages_rows) |
| 95 | + assert all("conversation" in row and "messages" not in row for row in materialized_rows) |
| 96 | + finally: |
| 97 | + sys.modules.pop(name, None) |
0 commit comments