MCPcopy Create free account
hub / github.com/zai-org/CodeGeeX / create_masked_lm_predictions

Function create_masked_lm_predictions

codegeex/megatron/data/dataset_utils.py:172–393  ·  view source on GitHub ↗

Creates the predictions for the masked LM objective. Note: Tokens here are vocab ids and not text tokens.

(
    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=3,
    do_whole_word_mask=True,
    favor_longer_ngram=False,
    do_permutation=False,
    geometric_dist=False,
    masking_style="bert",
)

Source from the content-addressed store, hash-verified

170
171
172def create_masked_lm_predictions(
173 tokens,
174 vocab_id_list,
175 vocab_id_to_token_dict,
176 masked_lm_prob,
177 cls_id,
178 sep_id,
179 mask_id,
180 max_predictions_per_seq,
181 np_rng,
182 max_ngrams=3,
183 do_whole_word_mask=True,
184 favor_longer_ngram=False,
185 do_permutation=False,
186 geometric_dist=False,
187 masking_style="bert",
188):
189 """Creates the predictions for the masked LM objective.
190 Note: Tokens here are vocab ids and not text tokens."""
191
192 cand_indexes = []
193 # Note(mingdachen): We create a list for recording if the piece is
194 # the starting piece of current token, where 1 means true, so that
195 # on-the-fly whole word masking is possible.
196 token_boundary = [0] * len(tokens)
197
198 for (i, token) in enumerate(tokens):
199 if token == cls_id or token == sep_id:
200 token_boundary[i] = 1
201 continue
202 # Whole Word Masking means that if we mask all of the wordpieces
203 # corresponding to an original word.
204 #
205 # Note that Whole Word Masking does *not* change the training code
206 # at all -- we still predict each WordPiece independently, softmaxed
207 # over the entire vocabulary.
208 if (
209 do_whole_word_mask
210 and len(cand_indexes) >= 1
211 and not is_start_piece(vocab_id_to_token_dict[token])
212 ):
213 cand_indexes[-1].append(i)
214 else:
215 cand_indexes.append([i])
216 if is_start_piece(vocab_id_to_token_dict[token]):
217 token_boundary[i] = 1
218
219 output_tokens = list(tokens)
220
221 masked_lm_positions = []
222 masked_lm_labels = []
223
224 if masked_lm_prob == 0:
225 return (output_tokens, masked_lm_positions, masked_lm_labels, token_boundary)
226
227 num_to_predict = min(
228 max_predictions_per_seq, max(1, int(round(len(tokens) * masked_lm_prob)))
229 )

Callers

nothing calls this directly

Calls 2

is_start_pieceFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected