def build_training_sample( self, sample, target_seq_length, np_rng, ): """Build training sample. Arguments: sample: A list of sentences in which each sentence is a list token ids. target_seq_length: Desired sequence length. max_seq_length: Maximum length of the sequence. All values are padded to this length. vocab_id_list: List of vocabulary ids. Used to pick a random id. vocab_id_to_token_dict: A dictionary from vocab ids to text tokens. cls_id: Start of example id. sep_id: Separator id. mask_id: Mask token id. pad_id: Padding token id. masked_lm_prob: Probability to mask tokens. np_rng: Random number genenrator. Note that this rng state should be numpy and not python since python randint is inclusive for the opper bound whereas the numpy one is exclusive. bos_id: start of decoder example id eos_id: end of generation id sentinel_tokens: unique value to be substituted for every replaced span tokenizer_type: wordpiece (BERT-style) or sentencepiece tokenizer. Used for whole word masking logic. max_ngram_size: maximum size of ngrams to be masked. mean_ngram_size: mean size of ngrams to be masked (only used if geometric_dist=True). geometric_dist: Uses a geometric distribution to sample ngram size. permutation: Permutes the ngrams. whole_word_masking: Always masks entire words instead of individual sub-word tokens. favor_long_ngrams: Favor longer ngrams over shorter ones. """ assert target_seq_length <= self.max_seq_length # flatten sentences into one list tokens = [token for sentence in sample for token in sentence] # Truncate to `target_sequence_length`. max_num_tokens = target_seq_length truncated = len(tokens) > max_num_tokens tokens = tokens[:max_num_tokens] # Masking. max_predictions_per_seq = self.masked_lm_prob * max_num_tokens lm_pred = create_masked_lm_predictions( tokens=tokens, vocab_id_list=self.vocab_id_list, vocab_id_to_token_dict=self.vocab_id_to_token_dict, masked_lm_prob=self.masked_lm_prob, cls_id=self.cls_id, sep_id=self.sep_id, mask_id=self.mask_id, max_predictions_per_seq=max_predictions_per_seq, np_rng=np_rng, max_ngram_size=self.max_ngram_size, whole_word_masking=self.whole_word_masking, favor_long_ngrams=self.favor_long_ngrams, mean_ngram_size=self.mean_ngram_size, permutation=self.permutation, geometric_dist=self.geometric_dist, masking_style="t5", tokenizer_type=self.tokenizer_type, ) if self.masked_lm_prob == 0: (output_tokens, masked_positions, masked_labels, _) = lm_pred masked_spans = None else: (output_tokens, masked_positions, masked_labels, _, masked_spans) = lm_pred # Padding. tokens_enc, tokens_dec_in, labels, enc_mask, dec_mask, loss_mask = self.pad_and_convert_to_numpy( tokens=tokens, output_tokens=output_tokens, masked_positions=masked_positions, masked_labels=masked_labels, masked_spans=masked_spans, np_rng=np_rng, ) train_sample = { 'text_enc': tokens_enc, 'text_dec': tokens_dec_in, 'labels': labels, 'loss_mask': loss_mask, 'truncated': int(truncated), 'enc_mask': enc_mask, 'dec_mask': dec_mask, } return train_sample
def build_training_sample( sample, target_seq_length, max_seq_length, max_seq_length_dec, vocab_id_list, vocab_id_to_token_dict, cls_id, sep_id, mask_id, pad_id, masked_lm_prob, np_rng, bos_id=None, eos_id=None, sentinel_tokens=None, ): """Build training sample. Arguments: sample: A list of sentences in which each sentence is a list token ids. target_seq_length: Desired sequence length. max_seq_length: Maximum length of the sequence. All values are padded to this length. vocab_id_list: List of vocabulary ids. Used to pick a random id. vocab_id_to_token_dict: A dictionary from vocab ids to text tokens. cls_id: Start of example id. sep_id: Separator id. mask_id: Mask token id. pad_id: Padding token id. masked_lm_prob: Probability to mask tokens. np_rng: Random number genenrator. Note that this rng state should be numpy and not python since python randint is inclusive for the opper bound whereas the numpy one is exclusive. bos_id: start of decoder example id eos_id: end of generation id sentinel_tokens: unique value to be substituted for every replaced span """ assert target_seq_length <= max_seq_length # flatten sentences into one list tokens = [token for sentence in sample for token in sentence] # Truncate to `target_sequence_length`. max_num_tokens = target_seq_length truncated = len(tokens) > max_num_tokens tokens = tokens[:max_num_tokens] # Masking. max_predictions_per_seq = masked_lm_prob * max_num_tokens (tokens, masked_positions, masked_labels, _, masked_spans) = create_masked_lm_predictions( tokens, vocab_id_list, vocab_id_to_token_dict, masked_lm_prob, cls_id, sep_id, mask_id, max_predictions_per_seq, np_rng, max_ngrams=10, geometric_dist=True, masking_style="t5", ) # Padding. tokens_enc, tokens_dec_in, labels, enc_mask, dec_mask, enc_dec_mask, loss_mask = pad_and_convert_to_numpy( tokens, masked_positions, masked_labels, pad_id, max_seq_length, max_seq_length_dec, masked_spans, bos_id, eos_id, sentinel_tokens, ) train_sample = { 'text_enc': tokens_enc, 'text_dec': tokens_dec_in, 'labels': labels, 'loss_mask': loss_mask, 'truncated': int(truncated), 'enc_mask': enc_mask, 'dec_mask': dec_mask, 'enc_dec_mask': enc_dec_mask, } return train_sample
def build_training_sample( sample, target_seq_length, max_seq_length, vocab_id_list, vocab_id_to_token_dict, cls_id, sep_id, mask_id, pad_id, masked_lm_prob, np_rng, binary_head, ): """Biuld training sample. Arguments: sample: A list of sentences in which each sentence is a list token ids. target_seq_length: Desired sequence length. max_seq_length: Maximum length of the sequence. All values are padded to this length. vocab_id_list: List of vocabulary ids. Used to pick a random id. vocab_id_to_token_dict: A dictionary from vocab ids to text tokens. cls_id: Start of example id. sep_id: Separator id. mask_id: Mask token id. pad_id: Padding token id. masked_lm_prob: Probability to mask tokens. np_rng: Random number genenrator. Note that this rng state should be numpy and not python since python randint is inclusive for the opper bound whereas the numpy one is exclusive. """ if binary_head: # We assume that we have at least two sentences in the sample assert len(sample) > 1 assert target_seq_length <= max_seq_length # Divide sample into two segments (A and B). if binary_head: tokens_a, tokens_b, is_next_random = get_a_and_b_segments( sample, np_rng) else: tokens_a = [] for j in range(len(sample)): tokens_a.extend(sample[j]) tokens_b = [] is_next_random = False # Truncate to `target_sequence_length`. max_num_tokens = target_seq_length truncated = truncate_segments(tokens_a, tokens_b, len(tokens_a), len(tokens_b), max_num_tokens, np_rng) # Build tokens and toketypes. tokens, tokentypes = create_tokens_and_tokentypes(tokens_a, tokens_b, cls_id, sep_id) # Masking. max_predictions_per_seq = masked_lm_prob * max_num_tokens (tokens, masked_positions, masked_labels, _, _) = create_masked_lm_predictions( tokens, vocab_id_list, vocab_id_to_token_dict, masked_lm_prob, cls_id, sep_id, mask_id, max_predictions_per_seq, np_rng, ) # Padding. tokens_np, tokentypes_np, labels_np, padding_mask_np, loss_mask_np = pad_and_convert_to_numpy( tokens, tokentypes, masked_positions, masked_labels, pad_id, max_seq_length) train_sample = { 'text': tokens_np, 'types': tokentypes_np, 'labels': labels_np, 'is_random': int(is_next_random), 'loss_mask': loss_mask_np, 'padding_mask': padding_mask_np, 'truncated': int(truncated), } return train_sample