Megatron-LM icon indicating copy to clipboard operation
Megatron-LM copied to clipboard

coupled data sharding across data parallel processes

Open thomas010 opened this issue 3 years ago • 4 comments

the folloinwing code for sampling in data/data_samplers.py seems to be coupled. that is to say, some samples for different data-parallel processes always co-occrurence in different epoch:

` def iter(self): active_total_samples = self.total_samples - self.last_batch_size self.epoch = self.consumed_samples // active_total_samples current_epoch_samples = self.consumed_samples % active_total_samples assert current_epoch_samples % self.micro_batch_times_data_parallel_size == 0

    if isinstance(self.dataset, RandomSeedDataset):
        self.dataset.set_epoch(self.epoch)

    # data sharding and random sampling
    if self.data_sharding:
        bucket_size = (self.total_samples // self.micro_batch_times_data_parallel_size) \
                       * self.micro_batch_size
        bucket_offset = current_epoch_samples // self.data_parallel_size
        start_idx = self.data_parallel_rank * bucket_size
        
        g = torch.Generator()
        g.manual_seed(self.epoch)
        random_idx = torch.randperm(bucket_size, generator=g).tolist()
        idx_range = [start_idx + x for x in random_idx[bucket_offset:]]
    else:
        full_bucket_size = (self.total_samples // self.micro_batch_size) \
                            * self.micro_batch_size
        full_bucket_offset = current_epoch_samples
        g = torch.Generator()
        g.manual_seed(self.epoch)
        idx_range_total = \
            torch.randperm(full_bucket_size, generator=g).tolist()
        idx_range_active = idx_range_total[full_bucket_offset:]
        idx_range = idx_range_active[self.data_parallel_rank::self.data_parallel_size]

`

thomas010 avatar Jun 28 '23 07:06 thomas010

Can you explain more? It looks OK to me. It's true that for a given data parallel rank the same subset of random_idx is always used each epoch, but random_idx comes from a torch.randperm so the actual indexes into the dataset used on a given data parallel rank will be shuffled each epoch I think.

jon-barker avatar Jun 29 '23 21:06 jon-barker

Can you explain more? It looks OK to me. It's true that for a given data parallel rank the same subset of random_idx is always used each epoch, but random_idx comes from a torch.randperm so the actual indexes into the dataset used on a given data parallel rank will be shuffled each epoch I think.

I means that data samples on different data parallel ranks can be coupled. assume there are 2 parallel ranks with totally 8 data samples sharded as follows: rank0: sample0, sample1, sample2, sample3 rank1: sample4, sample5, sample6, sample7

On each epoch, samples 0~3 and samples 4~7 are permuted with the sample random_idx. As a result, sample0 and sample4 always co-occurence in the same batch, the sample with others.

thomas010 avatar Jul 03 '23 16:07 thomas010

Since random_idx is generated from a random permutation that is seeded by the epoch it will contain different values each epoch. So although rank0 and rank1 will sample the same positions inside random_idx each epoch (based on start_idx or self.data_parallel_rank) those positions will contain different values corresponding to different indexes in the actual dataset I believe.

For clarity, are you using self.data_sharding=True?

jon-barker avatar Jul 03 '23 16:07 jon-barker

Marking as stale. No activity in 60 days.

github-actions[bot] avatar Sep 06 '23 18:09 github-actions[bot]