MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / convert_example_to_features

Function convert_example_to_features

demo/BERT/helpers/data_processing.py:87–184  ·  view source on GitHub ↗

Loads a data file into a list of `InputBatch`s.

(doc_tokens, question_text, tokenizer, max_seq_length,
                                 doc_stride, max_query_length)

Source from the content-addressed store, hash-verified

85
86
87def convert_example_to_features(doc_tokens, question_text, tokenizer, max_seq_length,
88 doc_stride, max_query_length):
89 """Loads a data file into a list of `InputBatch`s."""
90
91 query_tokens = tokenizer.tokenize(question_text)
92
93 if len(query_tokens) > max_query_length:
94 query_tokens = query_tokens[0:max_query_length]
95
96 tok_to_orig_index = []
97 orig_to_tok_index = []
98 all_doc_tokens = []
99 for (i, token) in enumerate(doc_tokens):
100 orig_to_tok_index.append(len(all_doc_tokens))
101 sub_tokens = tokenizer.tokenize(token)
102 for sub_token in sub_tokens:
103 tok_to_orig_index.append(i)
104 all_doc_tokens.append(sub_token)
105
106 # The -3 accounts for [CLS], [SEP] and [SEP]
107 max_tokens_for_doc = max_seq_length - len(query_tokens) - 3
108
109 # We can have documents that are longer than the maximum sequence length.
110 # To deal with this we do a sliding window approach, where we take chunks
111 # of the up to our max length with a stride of `doc_stride`.
112 _DocSpan = collections.namedtuple( # pylint: disable=invalid-name
113 "DocSpan", ["start", "length"])
114 doc_spans = []
115 start_offset = 0
116 while start_offset < len(all_doc_tokens):
117 length = len(all_doc_tokens) - start_offset
118 if length > max_tokens_for_doc:
119 length = max_tokens_for_doc
120 doc_spans.append(_DocSpan(start=start_offset, length=length))
121 if start_offset + length == len(all_doc_tokens):
122 break
123 start_offset += min(length, doc_stride)
124
125 _Feature = collections.namedtuple( # pylint: disable=invalid-name
126 "Feature",
127 ["input_ids", "input_mask", "segment_ids", "tokens", "token_to_orig_map", "token_is_max_context"])
128
129
130 features = []
131 for (doc_span_index, doc_span) in enumerate(doc_spans):
132 tokens = []
133 token_to_orig_map = {}
134 token_is_max_context = {}
135 segment_ids = []
136 tokens.append("[CLS]")
137 segment_ids.append(0)
138 for token in query_tokens:
139 tokens.append(token)
140 segment_ids.append(0)
141 tokens.append("[SEP]")
142 segment_ids.append(0)
143
144 for i in range(doc_span.length):

Callers

nothing calls this directly

Calls 6

minFunction · 0.85
_check_is_max_contextFunction · 0.85
create_int_featureFunction · 0.85
tokenizeMethod · 0.45
appendMethod · 0.45
convert_tokens_to_idsMethod · 0.45

Tested by

no test coverage detected