Transmission Optimization
Reducing the amount of input and output data transferred between the X86 side and the board side can improve the tool's performance. The tool can provide transmission optimization support for the following three usage scenarios.
-
The model input is fixed or updated periodically. If the input tensor of a multi-frame inference model remains unchanged, the input tensor does not need to be transmitted repeatedly; it only needs to be stored on the board side, and subsequent inferences can directly reuse the tensor on the board.
-
Model output filtering: Unused model outputs do not need to be transmitted back to the X86 side.
-
In multi-model scenarios, the output of a preceding model can be directly used as the input for a subsequent model. In this case, the output tensor of the preceding model can be stored on the board side without being transmitted back, and the corresponding input for the subsequent model also does not need to be transmitted, as it can use the tensor stored on the board.
Classes, Interfaces, and Parameters
-
Class HTensor
HTensor is used as the input and output tensor in transmission optimization scenarios. It is a wrapper class for tensors on the X86 side or the board side, designed to provide a unified data interface and restrict modifications to certain attributes to ensure data consistency.
1). HTensor Member Method:
__init__Initialize an HTensor object.
- Parameters
2).
dataAttribute of HTensorRetrieves or assigns the tensor data. When assigning new data, if the existing data is not None, the data type must match that of the original.
3).
deviceAttribute of HTensorObtains the storage device information of the tensor. This attribute is immutable once the object is constructed.
4).
keyAttribute of HTensorObtains the tensor’s unique identification key on the server. This attribute is immutable once the object is constructed.
5).
shapeAttribute of HTensorObtains the shape of the tensor. Manual modifications are prohibited, as it is maintained automatically by the tool.
-
output_configParameter ofHbmRpcSession.__call__This parameter is used to configure the transmission behavior of the output tensor after the current inference frame ends. Its type is
Dict[str, Dict[str, Any]], where the first-level keys are the model output names, and the second-level keys must include"device"or"key"(optional). The meanings and constraints of the values corresponding to"device"or"key"are consistent with thedeviceorkeyparameters in the HTensor constructor.When a certain output name of the model is correctly configured in
output_config, the corresponding output tensor in the inference results returned for the current frame will be of type HTensor. Outputs that are not configured will be returned as regular types (numpy.ndarrayortorch.Tensor).
Examples of Transmission Optimization Application
-
Periodic Input Update
In this example, it is assumed that the model has an input named
img, which is updated every10frames during inference. On the first frame after theimgupdate, the tool will transfer it to the board side, while no input data will be transferred in the remaining frames. -
Output Filtering
In this example, the model has three outputs:
output_0,output_1, andoutput_2. Among them,output_2is unused and filtered out, so onlyoutput_0andoutput_1are returned to the X86 side. -
Model Chaining
This example assumes that the input HBM file contains two models: model0 and model1. The output of model0 named
output_0will be directly used as the input namedinput_0for model1. In this process, the output of model0 does not need to be transmitted back to the X86 side, and the input of model1 does not need to be transmitted to the board side. -
Comprehensive Application
The following inference pipeline covers the three scenarios of periodic input update, output filtering, and model chaining. The flowchart is as follows:
The reference code is as follows:
