def create_instances_from_document(<EOL>all_documents, document_index, max_seq_length, short_seq_prob,<EOL>masked_lm_prob, max_predictions_per_seq, vocab_words, rng):
document = all_documents[document_index]<EOL>max_num_tokens = max_seq_length - <NUM_LIT:3><EOL>target_seq_length = max_num_tokens<EOL>if rng.random() < short_seq_prob:<EOL><INDENT>target_seq_length = rng.randint(<NUM_LIT:2>, max_num_tokens)<EOL><DEDENT>instances = []<EOL>current_chunk = []<EOL>current_length = <NUM_LIT:0><EOL>i = <NUM_LIT:0><EOL>while i < len(document):  <EOL><INDENT>segment = document[i]<EOL>current_chunk.append(segment)<EOL>current_length += len(segment)<EOL>if i == len(document) - <NUM_LIT:1> or current_length >= target_seq_length:<EOL><INDENT>if current_chunk:<EOL><INDENT>a_end = <NUM_LIT:1><EOL>if len(current_chunk) >= <NUM_LIT:2>:<EOL><INDENT>a_end = rng.randint(<NUM_LIT:1>, len(current_chunk) - <NUM_LIT:1>)<EOL><DEDENT>tokens_a = []<EOL>for j in range(a_end):<EOL><INDENT>tokens_a.extend(current_chunk[j])<EOL><DEDENT>tokens_b = []<EOL>is_random_next = False<EOL>if len(current_chunk) == <NUM_LIT:1> or rng.random() < <NUM_LIT:0.5>:<EOL><INDENT>is_random_next = True<EOL>target_b_length = target_seq_length - len(tokens_a)<EOL>for _ in range(<NUM_LIT:10>):<EOL><INDENT>random_document_index = rng.randint(<EOL><NUM_LIT:0>,<EOL>len(all_documents) - <NUM_LIT:1>)<EOL>if random_document_index != document_index:<EOL><INDENT>break<EOL><DEDENT><DEDENT>random_document = all_documents[random_document_index]<EOL>random_start = rng.randint(<NUM_LIT:0>, len(random_document) - <NUM_LIT:1>)<EOL>for j in range(random_start, len(random_document)):<EOL><INDENT>tokens_b.extend(random_document[j])<EOL>if len(tokens_b) >= target_b_length:<EOL><INDENT>break<EOL><DEDENT><DEDENT>num_unused_segments = len(current_chunk) - a_end<EOL>i -= num_unused_segments<EOL><DEDENT>else:<EOL><INDENT>is_random_next = False<EOL>for j in range(a_end, len(current_chunk)):<EOL><INDENT>tokens_b.extend(current_chunk[j])<EOL><DEDENT><DEDENT>truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng)<EOL>assert len(tokens_a) >= <NUM_LIT:1><EOL>assert len(tokens_b) >= <NUM_LIT:1><EOL>tokens = []<EOL>segment_ids = []<EOL>tokens.append('<STR_LIT>')<EOL>segment_ids.append(<NUM_LIT:0>)<EOL>for token in tokens_a:<EOL><INDENT>tokens.append(token)<EOL>segment_ids.append(<NUM_LIT:0>)<EOL><DEDENT>tokens.append('<STR_LIT>')<EOL>segment_ids.append(<NUM_LIT:0>)<EOL>segment_a_lengths = len(segment_ids)<EOL>for token in tokens_b:<EOL><INDENT>tokens.append(token)<EOL>segment_ids.append(<NUM_LIT:1>)<EOL><DEDENT>tokens.append('<STR_LIT>')<EOL>segment_ids.append(<NUM_LIT:1>)<EOL>segment_b_lengths = len(segment_ids) - segment_a_lengths<EOL>(tokens, masked_lm_positions,<EOL>masked_lm_labels) = create_masked_lm_predictions(<EOL>tokens, masked_lm_prob, max_predictions_per_seq,<EOL>vocab_words, rng)<EOL>instance = TrainingInstance(<EOL>tokens=tokens,<EOL>segment_ids=segment_ids,<EOL>is_random_next=is_random_next,<EOL>masked_lm_positions=masked_lm_positions,<EOL>masked_lm_labels=masked_lm_labels,<EOL>segment_a_lengths=segment_a_lengths,<EOL>segment_b_lengths=segment_b_lengths)<EOL>instances.append(instance)<EOL><DEDENT>current_chunk = []<EOL>current_length = <NUM_LIT:0><EOL><DEDENT>i += <NUM_LIT:1><EOL><DEDENT>return instances<EOL>
Creates `TrainingInstance`s for a single document.