Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions tests/L0/run_transformer/test_batch_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ def __getitem__(self, index):
return self.samples[index]


class SplitBatchDataset(Dataset):
def __init__(self, start, end):
super().__init__()
assert end > start, "this example code only works with end >= start"
self.start = start
self.end = end
self.samples = list(range(self.start, self.end))

def __len__(self):
return self.end - self.start

def __iter__(self):
return iter(range(self.start, self.end))

def __getitem__(self, index):
return (torch.tensor([index, index]), torch.tensor([index // 2, index // 2]))


class MegatronPretrainingRandomSampler:

def __init__(self, total_samples, consumed_samples, micro_batch_size,
Expand Down Expand Up @@ -104,25 +122,7 @@ def test_batch_sampler_behavior(self):
self.assertEqual(torch.cat(samples), torch.cat(samples2))

def test_split_batch(self):

class MyIterableDataset(Dataset):
def __init__(self, start, end):
super().__init__()
assert end > start, "this example code only works with end >= start"
self.start = start
self.end = end
self.samples = list(range(self.start, self.end))

def __len__(self):
return self.end - self.start

def __iter__(self):
return iter(range(self.start, self.end))

def __getitem__(self, index):
return (torch.tensor([index, index]), torch.tensor([index // 2, index // 2]))

dataset = MyIterableDataset(0, 100)
dataset = SplitBatchDataset(0, 100)
torch.manual_seed(42)
global_batch_size = 16
loader = DataLoader(dataset, batch_sampler=MegatronPretrainingRandomSampler(100, 0, global_batch_size, 0, 1), num_workers=2)
Expand Down
Loading