MCPcopy Index your code
hub / github.com/MotrixLab/MotionDiffuse / DistributedSampler

Class DistributedSampler

text2motion/datasets/dataloader.py:16–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14
15
16class DistributedSampler(_DistributedSampler):
17
18 def __init__(self,
19 dataset,
20 num_replicas=None,
21 rank=None,
22 shuffle=True,
23 round_up=True):
24 super().__init__(dataset, num_replicas=num_replicas, rank=rank)
25 self.shuffle = shuffle
26 self.round_up = round_up
27 if self.round_up:
28 self.total_size = self.num_samples * self.num_replicas
29 else:
30 self.total_size = len(self.dataset)
31
32 def __iter__(self):
33 # deterministically shuffle based on epoch
34 if self.shuffle:
35 g = torch.Generator()
36 g.manual_seed(self.epoch)
37 indices = torch.randperm(len(self.dataset), generator=g).tolist()
38 else:
39 indices = torch.arange(len(self.dataset)).tolist()
40
41 # add extra samples to make it evenly divisible
42 if self.round_up:
43 indices = (
44 indices *
45 int(self.total_size / len(indices) + 1))[:self.total_size]
46 assert len(indices) == self.total_size
47
48 # subsample
49 indices = indices[self.rank:self.total_size:self.num_replicas]
50 if self.round_up:
51 assert len(indices) == self.num_samples
52
53 return iter(indices)
54
55
56def build_dataloader(dataset: Dataset,

Callers 1

build_dataloaderFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected