Open
[草稿] [Bug修复] Megatron 数据加载器透传 multiprocessing 配置 #8887
[草稿] [Bug修复] Megatron 数据加载器透传 multiprocessing 配置 #8887
Open
xuxingleiAdd on Oct 28
Total: 2 files changed+88-3
@@ -1,4 +1,4 @@
1-# Copyright 2024 Huawei Technologies Co., Ltd1+# Copyright 2024-2026 Huawei Technologies Co., Ltd
2#2#
3# Licensed under the Apache License, Version 2.0 (the "License");3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.4# you may not use this file except in compliance with the License.
@@ -151,7 +151,8 @@ class MegatronDatasetBuilder:
151 shuffle=shuffle,151 shuffle=shuffle,
152 num_shards=num_shards,152 num_shards=num_shards,
153 shard_id=shard_id,153 shard_id=shard_id,
154- num_parallel_workers=kwargs.get('num_parallel_workers', 1))154+ num_parallel_workers=kwargs.get('num_parallel_workers', 1),
155+ python_multiprocessing=kwargs.get('python_multiprocessing', True))
155 return gen_dataset156 return gen_dataset
156 157 
157 if get_real_group_size() > 1 and get_context('mode') != MODE['PYNATIVE_MODE']:158 if get_real_group_size() > 1 and get_context('mode') != MODE['PYNATIVE_MODE']:
@@ -173,7 +174,8 @@ class MegatronDatasetBuilder:
173 column_names=source.cols(),174 column_names=source.cols(),
174 shuffle=False,175 shuffle=False,
175 num_shards=num_shards,176 num_shards=num_shards,
176- shard_id=shard_id) # keep the same sharding as real dataset177+ shard_id=shard_id,
178+ python_multiprocessing=kwargs.get('python_multiprocessing', True))
177 skip_barrier_controller(times=2)179 skip_barrier_controller(times=2)
178 else:180 else:
179 gen_dataset = build_gpt_dataset()181 gen_dataset = build_gpt_dataset()
@@ -0,0 +1,83 @@
1+# Copyright 2026 Huawei Technologies Co., Ltd
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+"""CPU contracts for GeneratorDataset execution options in the Megatron loader."""
16+ 
17+from types import SimpleNamespace
18+import unittest
19+from unittest.mock import patch
20+ 
21+import numpy as np
22+import pytest
23+import mindspore as ms
24+from mindspore.dataset import GeneratorDataset
25+ 
26+from mindformers.dataset.dataloader import blended_megatron_dataloader as loader
27+ 
28+ 
29+@pytest.fixture(autouse=True)
30+def _cpu_dataset_context():
31+ previous_mode = ms.get_context("mode")
32+ previous_target = ms.get_context("device_target")
33+ ms.set_context(mode=ms.PYNATIVE_MODE, device_target="CPU")
34+ yield
35+ ms.set_context(mode=previous_mode, device_target=previous_target)
36+ 
37+ 
38+class _Integers:
39+ """Small random-access source without external GPT files or native builds."""
40+ 
41+ def __len__(self):
42+ return 10
43+ 
44+ def __getitem__(self, index):
45+ return (np.asarray(index, dtype=np.int32),)
46+ 
47+ 
48+class TestBlendedMegatronKwargs(unittest.TestCase):
49+ """The public loader must preserve explicit options and native defaults."""
50+ 
51+ @staticmethod
52+ def _build(options, dummy=False):
53+ config = SimpleNamespace(sequence_length=8, create_compressed_eod_mask=False,
54+ create_attention_mask=False, mock=True)
55+ with patch.object(loader, "is_compile_runtime", return_value=False), \
56+ patch.object(loader, "get_real_group_size", return_value=2 if dummy else 1), \
57+ patch.object(loader, "get_real_rank", return_value=1), \
58+ patch.object(loader, "get_context", return_value=ms.GRAPH_MODE), \
59+ patch.object(loader.ms, "get_auto_parallel_context", return_value=1), \
60+ patch.object(loader, "get_dp_from_dataset_strategy", return_value=1), \
61+ patch.object(loader, "is_dataset_built_on_rank", return_value=not dummy), \
62+ patch.object(loader, "skip_barrier_controller"), \
63+ patch.object(loader.MegatronDatasetBuilder, "init_gpt_dataset_config", return_value=config), \
64+ patch.object(loader.BlendedMegatronDatasetBuilder, "build", return_value=[_Integers(), None, None]):
65+ return loader.BlendedMegatronDatasetDataLoader(
66+ datasets_type="GPTDataset", sizes=[10, 0, 0], config={},
67+ column_names=["input_ids"], shuffle=False, num_parallel_workers=2, **options,
68+ )
69+ 
70+ def test_explicit_flags_and_default_reach_real_generator(self):
71+ for options, expected in (({"python_multiprocessing": False}, False),
72+ ({"python_multiprocessing": True}, True), ({}, True)):
73+ with self.subTest(options=options):
74+ dataset = self._build(options)
75+ self.assertIsInstance(dataset, GeneratorDataset)
76+ self.assertIs(dataset.python_multiprocessing, expected)
77+ self.assertEqual(dataset.num_parallel_workers, 2)
78+ self.assertFalse(dataset.is_shuffled())
79+ 
80+ def test_dummy_rank_preserves_explicit_false(self):
81+ dataset = self._build({"python_multiprocessing": False}, dummy=True)
82+ self.assertIsInstance(dataset, GeneratorDataset)
83+ self.assertIs(dataset.python_multiprocessing, False)
atomgit-botatomgit-bot

🔵 Low Priority

建议:在两个测试方法上补充与同目录一致的 marker:@pytest.mark.level1、@pytest.mark.platform_x86_cpu、@pytest.mark.env_onecard。

likedislike
Incorrect?

🔵 Low Priority

建议:把 dummy 用例改为对 ({}, True)、({"python_multiprocessing": True}, True)、({"python_multiprocessing": False}, False) 三种组合做 subTest 断言,使第 178 行的缺省值 True 也被锁定。

likedislike
Incorrect?