INetworkDefinition
The INetworkDefinition is a component in the TensorRT workflow that defines the structure and layers of a neural network.
It serves as a high-level representation of the network and allows you to specify the input and output tensors, as well as the various operations and layers that make up the network.
Network Creation
You can create an empty INetworkDefinition using the TensorRT Builder.
The INetworkDefinition is populated either by using a parser (e.g., parsing a pre-trained model) or by manually adding layers and operations using the TensorRT Network API.
Adding Inputs and Outputs
You can add input tensors to the network using the
add_input()
method, specifying the name, data type, and dimensions of the input tensor.Output tensors can be marked using the
mark_output()
method, indicating which tensors should be considered as outputs of the network.
Adding Layers and Operations
INetworkDefinition provides methods to add various layers and operations to the network.
Examples include
add_convolution()
,add_pooling()
,add_activation()
,add_fully_connected()
, etc.Each layer takes input tensors and produces output tensors, forming the network structure.
Tensor Manipulation
INetworkDefinition allows you to manipulate tensors within the network.
You can add operations like concatenation, element-wise operations, reshaping, slicing, etc., to transform and combine tensors.
Network Optimisation
The INetworkDefinition is used by the TensorRT Builder to optimise the network for inference.
The Builder analyses the network structure, applies optimisations, and generates an optimised runtime engine (ICudaEngine).
Debugging and Inspection
INetworkDefinition provides methods to inspect the network structure and debug the network.
You can retrieve information about layers, tensors, and their connections using methods like
get_layer()
,get_input()
,get_output()
, etc.You can also mark tensors as debug tensors using
mark_debug()
to enable additional debugging information.
Relationship with the TensorRT-LLM process
Model Definition
The language model architecture, such as the transformer-based models like BERT or GPT, is defined using the INetworkDefinition.
The layers and operations specific to the language model, such as self-attention, feedforward layers, and embedding layers, are added to the INetworkDefinition.
Input and Output Tensors
The input tensors for the language model, such as the input tokens or token embeddings, are specified using
add_input()
.The output tensors, such as the language model predictions or hidden states, are marked using
mark_output()
.
Optimisation for Inference
The INetworkDefinition representing the language model is passed to the TensorRT Builder for optimisation.
The Builder applies various optimisations techniques, such as layer fusion, precision calibration, and kernel auto-tuning, to generate an optimised runtime engine (ICudaEngine) specifically tailored for the language model.
Inference
The optimised ICudaEngine is used to create an IExecutionContext, which allows for efficient inference on the language model.
The IExecutionContext takes input data, such as text tokens, and produces the language model outputs, such as predicted tokens or language embeddings.
By defining the language model architecture using the INetworkDefinition and leveraging TensorRT's optimisation capabilities, the TensorRT-LLM process enables fast and efficient inference on large language models, making them suitable for real-time applications and resource-constrained environments.
Last updated