| 9 | |
| 10 | |
| 11 | class InferenceSampler(torch.utils.data.sampler.Sampler): |
| 12 | |
| 13 | def __init__(self, size): |
| 14 | self._size = int(size) |
| 15 | assert size > 0 |
| 16 | self._rank = torch.distributed.get_rank() |
| 17 | self._world_size = torch.distributed.get_world_size() |
| 18 | self._local_indices = self._get_local_indices(size, self._world_size, |
| 19 | self._rank) |
| 20 | |
| 21 | @staticmethod |
| 22 | def _get_local_indices(total_size, world_size, rank): |
| 23 | shard_size = total_size // world_size |
| 24 | left = total_size % world_size |
| 25 | shard_sizes = [shard_size + int(r < left) for r in range(world_size)] |
| 26 | |
| 27 | begin = sum(shard_sizes[:rank]) |
| 28 | end = min(sum(shard_sizes[:rank + 1]), total_size) |
| 29 | return range(begin, end) |
| 30 | |
| 31 | def __iter__(self): |
| 32 | yield from self._local_indices |
| 33 | |
| 34 | def __len__(self): |
| 35 | return len(self._local_indices) |
| 36 | |
| 37 | def collate_fn_vqa(batches): |
| 38 | ''' |