|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import json |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from nemo_automodel.components.datasets.llm.column_mapped_text_instruction_iterable_dataset import ( |
| 21 | + ColumnMappedTextInstructionIterableDataset, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +class _DummyTokenizer: # noqa: D401 |
| 26 | + """Minimal tokenizer stub sufficient for dataset tokenization paths.""" |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + self.pad_token = "<pad>" |
| 30 | + self.pad_token_id = 0 |
| 31 | + self.eos_token_id = 1 |
| 32 | + self.bos_token_id = 2 |
| 33 | + self._counter = 3 |
| 34 | + |
| 35 | + def __call__( |
| 36 | + self, |
| 37 | + text: str, |
| 38 | + add_special_tokens: bool = True, |
| 39 | + padding=None, |
| 40 | + truncation=None, |
| 41 | + max_length=None, |
| 42 | + ): |
| 43 | + tokens = text.split() |
| 44 | + input_ids = list(range(self._counter, self._counter + len(tokens))) |
| 45 | + if add_special_tokens: |
| 46 | + input_ids = [self.bos_token_id] + input_ids + [self.eos_token_id] |
| 47 | + # Advance counter so successive calls yield distinct id ranges |
| 48 | + self._counter += len(tokens) + (2 if add_special_tokens else 0) |
| 49 | + return {"input_ids": input_ids} |
| 50 | + |
| 51 | + |
| 52 | +def _write_jsonl(path: Path, rows): |
| 53 | + with path.open("w", encoding="utf-8") as fp: |
| 54 | + for row in rows: |
| 55 | + fp.write(json.dumps(row) + "\n") |
| 56 | + |
| 57 | +def test_iterable_dataset_shard_and_shuffle_smoke(monkeypatch, tmp_path: Path): |
| 58 | + class _StubHFIterable: |
| 59 | + def __init__(self, rows): |
| 60 | + self._rows = rows |
| 61 | + self._shard = None |
| 62 | + self._shuffled = False |
| 63 | + |
| 64 | + def __iter__(self): |
| 65 | + it = self._rows |
| 66 | + if self._shard is not None: |
| 67 | + n, idx = self._shard |
| 68 | + it = [r for i, r in enumerate(it) if i % n == idx] |
| 69 | + if self._shuffled: |
| 70 | + it = list(reversed(it)) |
| 71 | + for r in it: |
| 72 | + yield r |
| 73 | + |
| 74 | + def shard(self, num_shards, index): |
| 75 | + self._shard = (num_shards, index) |
| 76 | + return self |
| 77 | + |
| 78 | + def shuffle(self, buffer_size, seed): |
| 79 | + self._shuffled = True |
| 80 | + return self |
| 81 | + |
| 82 | + rows = [ |
| 83 | + {"q": "Q0?", "a": "A0"}, |
| 84 | + {"q": "Q1?", "a": "A1"}, |
| 85 | + {"q": "Q2?", "a": "A2"}, |
| 86 | + ] |
| 87 | + |
| 88 | + def _fake_load_dataset(*args, **kwargs): |
| 89 | + return _StubHFIterable(rows) |
| 90 | + |
| 91 | + monkeypatch.setattr( |
| 92 | + "nemo_automodel.components.datasets.llm.column_mapped_text_instruction_iterable_dataset._load_dataset", |
| 93 | + _fake_load_dataset, |
| 94 | + ) |
| 95 | + |
| 96 | + ds = ColumnMappedTextInstructionIterableDataset( |
| 97 | + path_or_dataset_id="ignored.jsonl", |
| 98 | + column_mapping={"question": "q", "answer": "a"}, |
| 99 | + tokenizer=_DummyTokenizer(), |
| 100 | + answer_only_loss_mask=False, |
| 101 | + repeat_on_exhaustion=False, |
| 102 | + ).shard(2, 1).shuffle(buffer_size=2, seed=0) |
| 103 | + |
| 104 | + first = next(iter(ds)) |
| 105 | + assert {"input_ids", "attention_mask", "labels"}.issubset(first.keys()) |
| 106 | + |
| 107 | + |
| 108 | +def test_iterable_dataset_pad_token_fallback_with_eos(tmp_path: Path): |
| 109 | + class _TokNoPadWithEos: |
| 110 | + eos_token = "</s>" |
| 111 | + pad_token = None |
| 112 | + |
| 113 | + rows = [{"q": "Q?", "a": "A"}] |
| 114 | + jsonl_path = tmp_path / "toy_pad_eos.jsonl" |
| 115 | + _write_jsonl(jsonl_path, rows) |
| 116 | + |
| 117 | + tok = _TokNoPadWithEos() |
| 118 | + _ = ColumnMappedTextInstructionIterableDataset( |
| 119 | + path_or_dataset_id=str(jsonl_path), |
| 120 | + column_mapping={"question": "q", "answer": "a"}, |
| 121 | + tokenizer=tok, |
| 122 | + answer_only_loss_mask=False, |
| 123 | + repeat_on_exhaustion=False, |
| 124 | + ) |
| 125 | + assert tok.pad_token == tok.eos_token |
| 126 | + |
| 127 | + |
| 128 | +def test_iterable_dataset_pad_token_fallback_without_eos(tmp_path: Path): |
| 129 | + class _TokNoPadNoEos: |
| 130 | + pad_token = None |
| 131 | + |
| 132 | + rows = [{"q": "Q?", "a": "A"}] |
| 133 | + jsonl_path = tmp_path / "toy_pad_noeos.jsonl" |
| 134 | + _write_jsonl(jsonl_path, rows) |
| 135 | + |
| 136 | + tok = _TokNoPadNoEos() |
| 137 | + _ = ColumnMappedTextInstructionIterableDataset( |
| 138 | + path_or_dataset_id=str(jsonl_path), |
| 139 | + column_mapping={"question": "q", "answer": "a"}, |
| 140 | + tokenizer=tok, |
| 141 | + answer_only_loss_mask=False, |
| 142 | + repeat_on_exhaustion=False, |
| 143 | + ) |
| 144 | + assert tok.pad_token == " " |
| 145 | + |
| 146 | + |
| 147 | +def test_iterable_dataset_mapping_checks_missing_answer(tmp_path: Path): |
| 148 | + rows = [{"q": "Q?", "a": "A"}] |
| 149 | + jsonl_path = tmp_path / "toy_missing_answer.jsonl" |
| 150 | + _write_jsonl(jsonl_path, rows) |
| 151 | + |
| 152 | + with pytest.raises(AssertionError): |
| 153 | + _ = ColumnMappedTextInstructionIterableDataset( |
| 154 | + path_or_dataset_id=str(jsonl_path), |
| 155 | + column_mapping={"question": "q"}, # missing answer |
| 156 | + tokenizer=_DummyTokenizer(), |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +def test_iterable_dataset_mapping_checks_two_keys_missing_both_context_and_question(tmp_path: Path): |
| 161 | + rows = [{"q": "Q?", "a": "A"}] |
| 162 | + jsonl_path = tmp_path / "toy_two_keys_invalid.jsonl" |
| 163 | + _write_jsonl(jsonl_path, rows) |
| 164 | + |
| 165 | + with pytest.raises(AssertionError, match="Expected context or question"): |
| 166 | + _ = ColumnMappedTextInstructionIterableDataset( |
| 167 | + path_or_dataset_id=str(jsonl_path), |
| 168 | + column_mapping={"answer": "a", "foo": "bar"}, |
| 169 | + tokenizer=_DummyTokenizer(), |
| 170 | + ) |
| 171 | + |
| 172 | + |
| 173 | +def test_iterable_dataset_mapping_checks_invalid_num_columns(tmp_path: Path): |
| 174 | + rows = [{"q": "Q?", "a": "A"}] |
| 175 | + jsonl_path = tmp_path / "toy_invalid_cols.jsonl" |
| 176 | + _write_jsonl(jsonl_path, rows) |
| 177 | + |
| 178 | + with pytest.raises(ValueError, match="Expected 2 or 3 columns"): |
| 179 | + _ = ColumnMappedTextInstructionIterableDataset( |
| 180 | + path_or_dataset_id=str(jsonl_path), |
| 181 | + column_mapping={"answer": "a"}, # only 1 key |
| 182 | + tokenizer=_DummyTokenizer(), |
| 183 | + ) |
| 184 | + |
| 185 | + |
0 commit comments