mmengine icon indicating copy to clipboard operation
mmengine copied to clipboard

[Fix] Fix IterBased Loop Training with Faster Resume

Open henryzhengr opened this issue 1 year ago • 2 comments

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers. By the way, if you're not familiar with how to use pre-commit to fix lint issues or add unit tests, please refer to Contributing to OpenMMLab.

Motivation

Previous, PR#1471 resolves the issue of IterBased loop resuming at the correct iteration. However, its approach requires processing through data loading and preprocessing, which significantly slows down the resume process. This PR introduces a more efficient solution that minimizes overhead while maintaining correctness. Compared to solutions proposed in PR#1548 and PR#1520, this solution achieves faster resume with fewer code changes.

Modification

This solution leverages itertools.islice to skip iterations in the dataloader without actually triggering the data loading and preprocessing steps, significantly improving the speed of the resume process.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repos? If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDetection or MMPretrain.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

henryzhengr avatar Nov 24 '24 07:11 henryzhengr

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 24 '24 07:11 CLAassistant

Please merge this to fix the extreme slow resume

4o3F avatar Jan 19 '25 06:01 4o3F

It's wrong. The dataloader_iterator is not itered actually. You can verify it from the codes below:

from itertools import islice

class OrderedNumberIterator:
    def __init__(self, start, end, step=1):
        self.current = start
        self.end = end
        self.step = step

    def __iter__(self):
        return self

    def __next__(self):
        if self.current < self.end:
            result = self.current
            self.current += self.step
            return result
        raise StopIteration

iterator = OrderedNumberIterator(1, 10)
islice(iterator, 0, 5)
print(next(iterator))

hujh1994 avatar Apr 07 '25 09:04 hujh1994