| | |
| | | import numpy as np |
| | | import logging |
| | | import math |
| | | import random |
| | | import torch.distributed as dist |
| | | from torch.utils.data import DistributedSampler |
| | | from torch.utils.data import BatchSampler, Sampler |
| | |
| | | batch_type="token", |
| | | num_replicas=None, |
| | | rank=None, |
| | | rank_split=False, |
| | | shuffle=True, |
| | | drop_last=False, |
| | | is_training: bool = True, |
| | |
| | | except: |
| | | rank = 0 |
| | | num_replicas = 1 |
| | | |
| | | if rank_split: |
| | | logging.info(f"Warning, rank_split: {rank_split}, batch and shuffle data in local rank") |
| | | rank = 0 |
| | | num_replicas = 1 |
| | | |
| | | self.rank = rank |
| | | self.num_replicas = num_replicas |
| | | self.dataset = dataset |
| | |
| | | self.drop_last = drop_last |
| | | |
| | | self.total_size = len(self.dataset) |
| | | # self.num_samples = int(math.ceil(self.total_size / self.num_replicas)) |
| | | self.num_samples = int(math.ceil(self.total_size / self.num_replicas)) |
| | | self.epoch = 0 |
| | | self.sort_size = sort_size * num_replicas |
| | | self.max_token_length = kwargs.get("max_token_length", 2048) |
| | | self.length_scale_source = kwargs.get("length_scale_source", 1.0) |
| | | super().__init__(dataset, num_replicas=num_replicas, rank=rank, |
| | | shuffle=shuffle, drop_last=drop_last) |
| | | |
| | | def __iter__(self): |
| | | if self.shuffle: |
| | | g = torch.Generator() |
| | | g.manual_seed(self.epoch) |
| | | random.seed(self.epoch) |
| | | |
| | | indices = torch.randperm(len(self.dataset), generator=g).tolist() |
| | | else: |
| | | indices = list(range(len(self.dataset))) |
| | |
| | | max_len_in_batch = 0 |
| | | for idx in buffer: |
| | | original_sample_length = self.dataset.get_source_len(idx) |
| | | if original_sample_length > self.max_sample_length: |
| | | if original_sample_length > self.max_token_length: |
| | | continue |
| | | sample_length = 1 if self.batch_type == "example" else original_sample_length |
| | | potential_batch_length = max(max_len_in_batch, sample_length) * (len(batch) + 1) |
| | |
| | | # Ensure each rank gets the same number of batches, duplicate data if needed |
| | | batches_per_rank = math.ceil(len(buffer_batches) / self.num_replicas) |
| | | total_batches_needed = batches_per_rank * self.num_replicas |
| | | buffer_batches.extend(buffer_batches[:total_batches_needed - len(buffer_batches)]) |
| | | |
| | | |
| | | extra_batches = total_batches_needed - len(buffer_batches) |
| | | buffer_batches += random.choices(buffer_batches, k=extra_batches) |
| | | |
| | | # Evenly distribute batches from buffer_batches to each rank |
| | | rank_batches = [[] for _ in range(self.num_replicas)] |
| | | for i, batch in enumerate(buffer_batches): |