Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficiently compute total number of steps #1098

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RicardoDominguez
Copy link
Contributor

@RicardoDominguez RicardoDominguez commented Jan 11, 2024

When calculating the total number of steps, the current implementation required iterating through the entire dataset, which can be very slow for large datasets. Instead, take sampler.num_batches(), which I believe should correspond precisely to the length of the data loader (since when packing, the batch size of the data loader is 1).

@winglian
Copy link
Collaborator

@RicardoDominguez thanks! looks good so far. I need to just do some sanity checks on the validity of this logic. Do you have any ideas about how we might be able to unit test this?

@winglian
Copy link
Collaborator

I went ahead and rebased the PR onto main as there were some linting issues in the commit of main you were on.

@RicardoDominguez
Copy link
Contributor Author

Hmm not sure how to unit test these changes beyond comparing sampler.num_batches() to len(dataloader).

@winglian
Copy link
Collaborator

winglian commented Feb 1, 2024

I also think there is a minor discrepancy because we often pass drop_last=True to the dataloader.

@winglian
Copy link
Collaborator

winglian commented Feb 1, 2024

I think this might not be necessary as checking the length of the sampler is already what happens under the hood.

Also, inspecting the torch DataLoader class, the __len__() method already uses return len(self._index_sampler), where

    @property
    def _index_sampler(self):
        # The actual sampler used for generating indices for `_DatasetFetcher`
        # (see _utils/fetch.py) to read data at each time. This would be
        # `.batch_sampler` if in auto-collation mode, and `.sampler` otherwise.
        # We can't change `.sampler` and `.batch_sampler` attributes for BC
        # reasons.
        if self._auto_collation:
            return self.batch_sampler
        else:
            return self.sampler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants