MCPcopy Create free account
hub / github.com/LargeWorldModel/LWM / __iter__

Method __iter__

lwm/data.py:434–508  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

432 yield example
433
434 def __iter__(self):
435 global_chunk_size = self.config.batch_size * self.config.seq_length
436 if self.config.use_data_sharded_loader:
437 local_batch_size = self.config.batch_size // self._node_info['dp_node_size']
438 else:
439 local_batch_size = self.config.batch_size
440 chunk_size = local_batch_size * self.config.seq_length
441
442 token_buffer = []
443 loss_mask_buffer = []
444
445 last_time = 0.0
446 step_times = []
447 start_time = time.time()
448 start_tokens = self._total_tokens
449 for tokens, loss_masks, loc, index in self.parallel_example_iterator():
450 self._file_loc = loc
451 self._index = index
452 if self.config.pad:
453 tokens = tokens[:self.config.seq_length + 1]
454 tokens.extend([self._tokenizer.bos_token_id] * (self.config.seq_length + 1 - len(tokens)))
455 loss_masks = loss_masks[:self.config.seq_length + 1]
456 loss_masks.extend([0.0] * (self.config.seq_length + 1 - len(loss_masks)))
457 token_buffer.extend(tokens)
458 loss_mask_buffer.extend(loss_masks)
459 while len(token_buffer) > chunk_size + 1:
460 self._total_tokens += global_chunk_size
461 step_times.append(time.time() - last_time)
462 last_time = time.time()
463 if len(step_times) > self.config.throughput_average_window_size:
464 step_times = step_times[-self.config.throughput_average_window_size:]
465 average_throughput = global_chunk_size / np.mean(step_times)
466 accumulated_throughput = (
467 (self._total_tokens - start_tokens) / (time.time() - start_time)
468 )
469 metrics = {
470 'dataset_file_loc': loc,
471 'dataset_example_index': index,
472 'dataset_total_tokens': self._total_tokens,
473 'dataset_accumulated_tps': accumulated_throughput,
474 'dataset_average_tps': average_throughput,
475 }
476 batch = {
477 'input_tokens': np.array(token_buffer[:chunk_size], dtype=np.int32).reshape(
478 local_batch_size, -1
479 ),
480 'target_tokens': np.array(token_buffer[1:chunk_size + 1], dtype=np.int32).reshape(
481 local_batch_size, -1
482 ),
483 'loss_masks': np.array(loss_mask_buffer[1:chunk_size + 1], dtype=np.float32).reshape(
484 local_batch_size, -1
485 ),
486 }
487 batch.update({
488 'input_vision_masks': np.zeros(batch['input_tokens'].shape, dtype=bool),
489 'target_vision_masks': np.zeros(batch['input_tokens'].shape, dtype=bool),
490 })
491 if self.config.always_start_with_bos:

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected