|
| 1 | +# Copyright (c) 2026, 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 | +from pathlib import Path |
| 16 | +from unittest.mock import MagicMock |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from megatron.bridge.data.builders.finetuning_dataset import FinetuningDatasetBuilder |
| 21 | +from megatron.bridge.data.datasets.packed_sequence import PackedSequenceSpecs |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize("mkdir_error", [FileExistsError, FileNotFoundError]) |
| 25 | +def test_default_pack_path_ignores_shared_fs_mkdir_race(tmp_path, monkeypatch, mkdir_error): |
| 26 | + """Network filesystems can leak mkdir races even with exist_ok=True.""" |
| 27 | + builder = FinetuningDatasetBuilder( |
| 28 | + dataset_root=tmp_path, |
| 29 | + tokenizer=MagicMock(), |
| 30 | + packed_sequence_specs=PackedSequenceSpecs( |
| 31 | + packed_sequence_size=128, |
| 32 | + tokenizer_model_name="mock-tokenizer", |
| 33 | + pad_seq_to_mult=8, |
| 34 | + ), |
| 35 | + ) |
| 36 | + expected_path = tmp_path / "packed" / "mock-tokenizer_pad_seq_to_mult8" |
| 37 | + |
| 38 | + monkeypatch.setattr(Path, "exists", lambda _: False) |
| 39 | + |
| 40 | + def raise_mkdir(self, parents=False, exist_ok=False): |
| 41 | + assert self == expected_path |
| 42 | + assert parents is True |
| 43 | + assert exist_ok is True |
| 44 | + raise mkdir_error("stale shared filesystem state") |
| 45 | + |
| 46 | + monkeypatch.setattr(Path, "mkdir", raise_mkdir) |
| 47 | + |
| 48 | + assert builder.default_pack_path == expected_path |
0 commit comments