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

Function inference

demo/BERT/inference.py:167–221  ·  view source on GitHub ↗
(features, tokens)

Source from the content-addressed store, hash-verified

165 d_output = cuda.mem_alloc(h_output.nbytes)
166
167 def inference(features, tokens):
168 global h_output
169
170 _NetworkOutput = collections.namedtuple( # pylint: disable=invalid-name
171 "NetworkOutput",
172 ["start_logits", "end_logits", "feature_index"])
173 networkOutputs = []
174
175 eval_time_elapsed = 0
176 for feature_index, feature in enumerate(features):
177 # Copy inputs
178 input_ids_batch = np.repeat(np.expand_dims(feature.input_ids, 0), args.batch_size, axis=0)
179 segment_ids_batch = np.repeat(np.expand_dims(feature.segment_ids, 0), args.batch_size, axis=0)
180 input_mask_batch = np.repeat(np.expand_dims(feature.input_mask, 0), args.batch_size, axis=0)
181
182 input_ids = cuda.register_host_memory(np.ascontiguousarray(input_ids_batch.ravel()))
183 segment_ids = cuda.register_host_memory(np.ascontiguousarray(segment_ids_batch.ravel()))
184 input_mask = cuda.register_host_memory(np.ascontiguousarray(input_mask_batch.ravel()))
185
186 eval_start_time = time.time()
187 cuda.memcpy_htod_async(d_inputs[0], input_ids, stream)
188 cuda.memcpy_htod_async(d_inputs[1], segment_ids, stream)
189 cuda.memcpy_htod_async(d_inputs[2], input_mask, stream)
190
191 # Run inference
192 context.execute_async_v2(bindings=[0 for i in range(binding_idx_offset)] + [int(d_inp) for d_inp in d_inputs] + [int(d_output)], stream_handle=stream.handle)
193 # Synchronize the stream
194 stream.synchronize()
195 eval_time_elapsed += (time.time() - eval_start_time)
196
197 # Transfer predictions back from GPU
198 cuda.memcpy_dtoh_async(h_output, d_output, stream)
199 stream.synchronize()
200
201 # Only retrieve and post-process the first batch
202 batch = h_output[0]
203 networkOutputs.append(_NetworkOutput(
204 start_logits = np.array(batch.squeeze()[:, 0]),
205 end_logits = np.array(batch.squeeze()[:, 1]),
206 feature_index = feature_index
207 ))
208
209 eval_time_elapsed /= len(features)
210
211 # Total number of n-best predictions to generate in the nbest_predictions.json output file
212 n_best_size = 20
213
214 # The maximum length of an answer that can be generated. This is needed
215 # because the start and end predictions are not conditioned on one another
216 max_answer_length = 30
217
218 prediction, nbest_json, scores_diff_json = dp.get_predictions(tokens, features,
219 networkOutputs, args.n_best_size, args.max_answer_length)
220
221 return eval_time_elapsed, prediction, nbest_json
222
223 def print_single_query(eval_time_elapsed, prediction, nbest_json):
224 print("------------------------")

Callers 1

inference.pyFile · 0.70

Calls 2

synchronizeMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected