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

Method forward

demo/HuggingFace/BART/trt.py:393–545  ·  view source on GitHub ↗
(self, input_ids, encoder_hidden_states, *args, **kwargs)

Source from the content-addressed store, hash-verified

391 return reordered_past
392
393 def forward(self, input_ids, encoder_hidden_states, *args, **kwargs):
394 # Get the batch size.
395 bs = input_ids.shape[0] # in beam search mode, bs is batch_size * num_beams
396
397 # Get the maximum sequence length.
398 max_length = self.max_sequence_length
399
400 # Get the vocab size.
401 vocab_size = BARTModelTRTConfig.VOCAB_SIZE[self.variant]
402
403 # Actual sequence length of the input_ids and the output hidden_states.
404 input_length = input_ids.shape[1]
405
406 # The sequence length of the encoder_hidden_states.
407 encoder_length = TRTHFRunner.ENCODER_LENGTH
408
409 # Encoder hidden size
410 encoder_hidden_size = self.encoder_hidden_size
411
412 # KV cache flag
413 use_cache = kwargs.get("use_cache", False)
414
415 # flag for switch between dual engines
416 non_kv_flag = self.use_non_kv_engine or (self.config.use_cache and kwargs.get("past_key_values") is None)
417 # condition 1: during e2e decoding test, based on flag
418 # condition 2: during single-step decoder test, depending on whether past_key_values is empty
419 # note: without --enable-kv-cache arg, this flag should remain False
420
421 # denote as variable to allow switch between non-kv and kv engines in kv cache mode
422 trt_context = self.trt_context_non_kv if non_kv_flag else self.trt_context
423 bindings = self.bindings_non_kv if non_kv_flag else self.bindings
424 inputs = self.inputs_non_kv if non_kv_flag else self.inputs
425 outputs = self.outputs_non_kv if non_kv_flag else self.outputs
426
427 # Check if the input data is on CPU (which usually means the PyTorch does not support current GPU).
428 is_cpu_mode = (input_ids.device == torch.device("cpu")) or (self.return_device == "cpu")
429
430 # We allocate the buffers using max_length, but we only need to first portion of it, so copy the data into the
431 # first portion of the input buffer.
432 # TODO: Could we just reuse input_ids' data_ptr() as the first binding when input_ids is already contiguous to
433 # avoid an additional D2D?
434 if is_cpu_mode:
435 inputs["input_ids"] = input_ids.int().flatten().contiguous().cuda()
436 bindings[0] = inputs["input_ids"].data_ptr()
437 else:
438 inputs["input_ids"][:bs * input_length] = input_ids.flatten()
439
440 # Set the binding shape of input_ids, which should be (bs, input_length).
441 trt_context.set_binding_shape(0, input_ids.shape)
442
443 # If encoder hidden states have not been copied yet, copy the hidden states to the input buffer.
444 if not self.persist_encoder_hidden_states:
445 if is_cpu_mode:
446 inputs["encoder_hidden_states"] = encoder_hidden_states.flatten().contiguous().cuda()
447 bindings[1] = inputs["encoder_hidden_states"].data_ptr()
448 else:
449 inputs["encoder_hidden_states"][:bs * encoder_length * encoder_hidden_size] = encoder_hidden_states.flatten()
450

Callers

nothing calls this directly

Calls 5

deviceMethod · 0.80
viewMethod · 0.80
getMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected