MCPcopy Create free account
hub / github.com/THUDM/GLM / __getitem__

Method __getitem__

data_utils/datasets.py:623–670  ·  view source on GitHub ↗
(self, idx)

Source from the content-addressed store, hash-verified

621 return self.num_samples
622
623 def __getitem__(self, idx):
624 # init rng
625 rng = random.Random(idx)
626 rng = np.random.RandomState(seed=[rng.randint(0, 2 ** 32 - 1) for _ in range(16)])
627
628 # get possibly weighted random index from dataset
629 tokens, loss_mask = self.get_weighted_samples(rng)
630 # truncate or pad tokens
631 num_tokens = len(tokens)
632 tokens_to_strip = num_tokens - self.max_seq_len + 1
633
634 # randomly choose a position for start
635 if tokens_to_strip > 0:
636 move_count = 0
637 strip_left_tokens = rng.randint(tokens_to_strip)
638 if rng.random() > self.non_sentence_start:
639 if rng.random() < 0.5:
640 while move_count < self.max_seq_len // 2 and strip_left_tokens > 0 and not self.contains_sentence_end(
641 tokens[strip_left_tokens - 1]):
642 strip_left_tokens -= 1
643 move_count += 1
644 else:
645 while move_count < self.max_seq_len // 2 and strip_left_tokens < len(
646 tokens) and not self.contains_sentence_end(tokens[strip_left_tokens - 1]):
647 strip_left_tokens += 1
648 move_count += 1
649 tokens = [self.tokenizer.get_command('ENC').Id] + tokens[strip_left_tokens:]
650 loss_mask = [0] + loss_mask[strip_left_tokens:]
651 if len(tokens) == 2 and tokens[1] == self.tokenizer.get_command('eos').Id:
652 tokens, loss_mask = [], []
653 tokens, loss_mask = self.right_strip_seq(tokens, loss_mask, self.max_seq_len)
654 else:
655 tokens = [self.tokenizer.get_command('ENC').Id] + tokens
656 loss_mask = [0] + loss_mask
657 # Sample multiple documents
658 if self.sample_across_doc:
659 while len(tokens) < self.max_seq_len:
660 new_tokens, new_loss_mask = self.get_weighted_samples(rng)
661 new_tokens = [self.tokenizer.get_command('ENC').Id] + new_tokens
662 new_loss_mask = [0] + new_loss_mask
663 is_last = len(new_tokens) >= self.max_seq_len - len(tokens)
664 new_tokens, new_loss_mask = self.right_strip_seq(new_tokens, new_loss_mask,
665 self.max_seq_len - len(tokens))
666 tokens += new_tokens
667 loss_mask += new_loss_mask
668 if is_last:
669 break
670 return {'text': np.array(tokens), "loss_mask": np.array(loss_mask)}
671
672 def right_strip_seq(self, tokens, loss_mask, seq_length):
673 strip_right_tokens = len(tokens) - seq_length

Callers

nothing calls this directly

Calls 4

get_weighted_samplesMethod · 0.95
contains_sentence_endMethod · 0.95
right_strip_seqMethod · 0.95
get_commandMethod · 0.80

Tested by

no test coverage detected