MCPcopy Create free account
hub / github.com/PRIME-RL/PRIME / make_iterator

Method make_iterator

training/verl/protocol.py:349–388  ·  view source on GitHub ↗

Make an iterator from the DataProto. This is built upon that TensorDict can be used as a normal Pytorch dataset. See https://pytorch.org/tensordict/tutorials/data_fashion for more details. Args: mini_batch_size (int): mini-batch size when iterating the dataset. We requir

(self, mini_batch_size, epochs, seed=None, dataloader_kwargs=None)

Source from the content-addressed store, hash-verified

347 return self
348
349 def make_iterator(self, mini_batch_size, epochs, seed=None, dataloader_kwargs=None):
350 """Make an iterator from the DataProto. This is built upon that TensorDict can be used as a normal Pytorch
351 dataset. See https://pytorch.org/tensordict/tutorials/data_fashion for more details.
352
353 Args:
354 mini_batch_size (int): mini-batch size when iterating the dataset. We require that
355 ``batch.batch_size[0] % mini_batch_size == 0``
356 epochs (int): number of epochs when iterating the dataset.
357 dataloader_kwargs: internally, it returns a DataLoader over the batch.
358 The dataloader_kwargs is the kwargs passed to the DataLoader
359
360 Returns:
361 Iterator: an iterator that yields a mini-batch data at a time. The total number of iteration steps is
362 ``self.batch.batch_size * epochs // mini_batch_size``
363 """
364 assert self.batch.batch_size[0] % mini_batch_size == 0, f"{self.batch.batch_size[0]} % {mini_batch_size} != 0"
365 # we can directly create a dataloader from TensorDict
366 if dataloader_kwargs is None:
367 dataloader_kwargs = {}
368
369 if seed is not None:
370 generator = torch.Generator()
371 generator.manual_seed(seed)
372 else:
373 generator = None
374
375 assert isinstance(dataloader_kwargs, Dict)
376 train_dataloader = DataLoader(dataset=self,
377 batch_size=mini_batch_size,
378 collate_fn=collate_fn,
379 generator=generator,
380 **dataloader_kwargs)
381
382 def get_data():
383 for _ in range(epochs):
384 for d in train_dataloader:
385 d.meta_info = self.meta_info
386 yield d
387
388 return iter(get_data())
389
390 def chunk(self, chunks: int) -> List['DataProto']:
391 """Split the batch among dim=0 into chunks. The meta_info is passed to each DataProto after split.

Callers 5

Calls

no outgoing calls

Tested by

no test coverage detected