MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / StreamSampler

Class StreamSampler

imperative/python/megengine/data/sampler.py:148–175  ·  view source on GitHub ↗

r"""Sampler for stream dataset. Warning: In the case of multiple machines, sampler should ensure that each worker gets different data. But this class cannot do it yet, please build your own dataset and sampler to achieve this goal. Usually, :meth:`~.StreamDataset.__

Source from the content-addressed store, hash-verified

146
147
148class StreamSampler(Sampler):
149 r"""Sampler for stream dataset.
150
151 Warning:
152 In the case of multiple machines, sampler should ensure that each worker gets
153 different data. But this class cannot do it yet, please build your own
154 dataset and sampler to achieve this goal.
155
156 Usually, :meth:`~.StreamDataset.__iter__` can return different iterator by
157 ``rank = dist.get_rank()``. So that they will get different data.
158 """
159
160 def __init__(self, batch_size=1):
161 self.batch_size = batch_size
162
163 def __iter__(self):
164 return self.batch()
165
166 def batch(self):
167 batch = []
168 for idx in self.sample():
169 batch.append(idx)
170 if len(batch) == self.batch_size:
171 yield batch
172 batch = []
173
174 def sample(self):
175 return count(start=0)
176
177
178class SequentialSampler(MapSampler):

Calls

no outgoing calls