| @@ -1767,7 +1767,9 @@ class Trainer: | |||
| 1767 | except StopIteration: | 1767 | except StopIteration: |
| 1768 | if hasattr(self._dataset_iter, 'stop'): | 1768 | if hasattr(self._dataset_iter, 'stop'): |
| 1769 | self._dataset_iter.stop() | 1769 | self._dataset_iter.stop() |
| 1770 | - if self._resumed and self.dynamic_batch_enabled: | 1770 | + # The saved offset selects only the initial resumed epoch. Later |
| 1771 | + # ordinary epochs start at zero for both static and dynamic batches. | ||
| 1772 | + if self._resumed: | ||
| 1771 | if self._balance_source is None: | 1773 | if self._balance_source is None: |
| 1772 | self.train_dataset.set_init_step(0) | 1774 | self.train_dataset.set_init_step(0) |
| 1773 | self._dataset_iter = self._create_dataset_iterator(self.train_dataset) | 1775 | self._dataset_iter = self._create_dataset_iterator(self.train_dataset) |
| @@ -0,0 +1,93 @@ | |||
| 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 regression for the lifetime of an ordinary dataset resume offset.""" | ||
| 16 | + | ||
| 17 | +from types import SimpleNamespace | ||
| 18 | +import unittest | ||
| 19 | + | ||
| 20 | +import numpy as np | ||
| 21 | +import pytest | ||
| 22 | +import mindspore as ms | ||
| 23 | +import mindspore.dataset as ds | ||
| 24 | + | ||
| 25 | +from mindformers.pynative.trainer.trainer import Trainer | ||
| 26 | + | ||
| 27 | + | ||
| 28 | +@pytest.fixture(autouse=True) | ||
| 29 | +def _cpu_dataset_context(): | ||
| 30 | + """Isolate the data-only test without changing other tests' runtime 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 | + """Deterministic random-access data, without model weights or external files.""" | ||
| 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 _DataOnlyTrainer: | ||
| 49 | + """Exercise the real Trainer data methods without initializing a Trainer.""" | ||
| 50 | + | ||
| 51 | + _uses_data_balance = Trainer._uses_data_balance | ||
| 52 | + _create_dataset_iterator = Trainer._create_dataset_iterator | ||
| 53 | + _get_batch_naive = Trainer._get_batch_naive | ||
| 54 | + get_batch = Trainer.get_batch | ||
| 55 | + _next_batch_unchecked = Trainer._next_batch_unchecked | ||
| 56 | + | ||
| 57 | + def __init__(self, resumed, dynamic): | ||
| 58 | + self.train_dataset = ds.GeneratorDataset( | ||
| 59 | + _Integers(), column_names=["input_ids"], shuffle=False, | ||
| 60 | + num_parallel_workers=1, python_multiprocessing=False, | ||
| 61 | + ).batch(batch_size=2, drop_remainder=True) | ||
| 62 | + if resumed: | ||
| 63 | + self.train_dataset.set_init_step(3) | ||
| 64 | + self.train_num_epochs = 4 | ||
| 65 | + self.config = SimpleNamespace( | ||
| 66 | + train_dataset=SimpleNamespace(balance_enabled=False), | ||
| 67 | + parallelism=SimpleNamespace(data_parallel=1), use_remove_redundant_dataset=False, | ||
| 68 | + ) | ||
| 69 | + self.model = [] | ||
| 70 | + self.enable_data_broadcast = False | ||
| 71 | + self._resumed = resumed | ||
| 72 | + self.dynamic_batch_enabled = dynamic | ||
| 73 | + self._balance_source = None | ||
| 74 | + self._dataset_iter = self._create_dataset_iterator(self.train_dataset) | ||
| 75 | + | ||
| 76 | + def read(self, count): | ||
| 77 | + try: | ||
| 78 | + return [self._next_batch_unchecked()["input_ids"].asnumpy().tolist() for _ in range(count)] | ||
| 79 | + finally: | ||
| 80 | + self._dataset_iter.stop() | ||
| 81 | + | ||
| 82 | + | ||
| 83 | +class TestIteratorResume(unittest.TestCase): | ||
🟡 Medium Priority 变更内容:新增文件 tests/st/test_ut/test_pynative/test_trainer/test_iterator_resume.py 中,测试类 TestIteratorResume(第 83 行)及其两个测试方法(第 86、91 行)没有任何 pytest 标记,模块级也没有 pytestmark。 证据链:同目录及同一测试树下的既有用例全部显式携带 失败模式:MindFormers 的 CI 通过 建议:在第 83 行类定义之前加一行模块级标记,使其与同目录既有用例一致,确保 CI 标记过滤能选中本回归: pytestmark = [pytest.mark.level0, pytest.mark.platform_x86_cpu, pytest.mark.env_onecard] (若该用例不需要单卡环境,可只保留 level0 + platform_x86_cpu,与 test_utils.py 中部分用例的写法保持一致。) ![]() ![]() Incorrect? | |||
| 84 | + """Epoch wrap must not replay the offset belonging to checkpoint restore.""" | ||
| 85 | + | ||
| 86 | + def test_static_and_dynamic_resume_continue_at_epoch_start(self): | ||
| 87 | + for dynamic in (False, True): | ||
| 88 | + with self.subTest(dynamic=dynamic): | ||
| 89 | + self.assertEqual(_DataOnlyTrainer(True, dynamic).read(4), [[6, 7], [8, 9], [0, 1], [2, 3]]) | ||
| 90 | + | ||
| 91 | + def test_fresh_epoch_wrap_is_unchanged(self): | ||
| 92 | + self.assertEqual(_DataOnlyTrainer(False, False).read(7), | ||
| 93 | + [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [0, 1], [2, 3]]) | ||


🔵 Low Priority
变更行(trainer.py:1770-1774)把 epoch wrap 时的清零条件从
self._resumed and self.dynamic_batch_enabled改为self._resumed,使普通静态 batch 恢复也在首次 wrap 时执行set_init_step(0)。但同文件未修改的初始化注释(trainer.py:283-288)仍写着:"On epoch-wrap iterator recreate, a resumed DYNAMIC run resets the dataset offset to 0 (set_init_step(0)) so the new epoch starts at item 0 — otherwise the persisted resume offset makes the recreated iterator restart at the checkpoint position",即把该行为限定为 dynamic 场景,并暗示静态路径必须依赖持久化偏移(即本次修复所纠正的错误语义)。建议:同步修改 trainer.py:283-288 的
_resumed字段注释,把"a resumed DYNAMIC run resets…"改为"静态与动态 batch 的恢复训练在 epoch-wrap 时都会重置为 0(_balance_source非空的 balanced source 路径除外)",与 1770-1774 新增的说明保持一致,避免后续维护者重新引入dynamic_batch_enabled条件。