Automatic quantization deployment method and system for AI chips supporting operator-level operations
By generating the NHNN framework and weights, disassembling the AI model to the operator level, and constructing a multi-core parallel scheduling strategy, it solves the problem that existing technology cannot simplify the development and deployment of HC3080 chips, and achieves efficient and low-cost AI model deployment and operation. .
Patent Information
- Application Number
- CN202411075653.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-07
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2044-08-07
Smart Images

Figure CN118605890B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of model quantization deployment, and in particular, to an automatic quantization deployment method and system for an AI chip that supports operator-level operations. Background Art
[0002] AI model quantization deployment is the process of quantizing the trained deep learning model and deploying it on various hardware platforms. Quantization is a model compression technology that can reduce the model's memory usage and computing requirements through quantization deployment, thereby improving the model's operating efficiency on the hardware platform and enabling it to run on resource-constrained devices, such as mobile devices, embedded systems, etc.
[0003] At present, the mainstream AI model quantization deployment tools on the market include NVIDIA's trtexec, Rockchip's RKNN-Toolkit, and SenseTime's openppl. Their common feature is that they will package the quantized model as a whole into a model structure under a certain framework. This means that general AI chips that support the model by disassembling it to the operator level, such as HC3080, cannot use these existing quantization deployment tools to simplify the development process and reduce deployment costs. Summary of the invention
[0004] The purpose of the present invention is to address the above-mentioned problems and propose an automatic quantization deployment method and system for AI chips that support operator-level operations. The main purpose is to propose an automatic quantization deployment process suitable for AI chips such as HC3080 that support operator-level operations, so as to simplify the development of such chips and reduce the deployment cost.
[0005] An automatic quantization deployment method for an AI chip supporting operator-level operations, the method comprising automatically quantizing and deploying a model to be deployed in an intermediate format:
[0006] Generate an NHNN model including an NHNN framework and NHNN weights;
[0007] The NHNN framework records the information required for quantization and the information required by the target chip, as well as the model operation information of the model to be deployed, and records the information of each operation according to the order in which different operators are run when the model is running;
[0008] The NHNN weights are obtained by quantizing the weights of the models to be deployed according to the information recorded by the NHNN framework, splitting them into operations and storing them in binary files. Model operations are operators, that is, the generated NHNN model breaks down the models to be deployed into various operations at the operator level.
[0009] According to the information recorded in the NHNN framework, a model that can be recognized by the embedded system of the target chip is constructed, and the quantized NHNN weights are imported into the target board to complete the model deployment. The target board contains the target chip and some peripheral devices, such as memory.
[0010] This solution can convert the AI model under any other framework into an intermediate format in a general way in advance, and then generate the NHNN model of the model to be deployed based on the intermediate format. In this way, it is only necessary to build a model that can be recognized by the target chip according to the NHNN framework, and then import the NHNN weights to complete the deployment. This can simplify the cost of developers deploying AI models to chips such as HC3080 and simplify the development process.
[0011] In the above-mentioned automatic quantization deployment method for AI chips that support operator-level operations, model deployment specifically includes:
[0012] Call the corresponding API function (provided by the developer) according to the operation sequence recorded in the NHNN framework, and call the corresponding NHNN weights at the same time to generate the source files required for deployment;
[0013] The cross-compilation chain supported by the target chip is called to compile the source file expressed in C language into a dynamic library file and an executable file that can be recognized by the target chip, and the quantized NHNN weight and model dynamic library file and executable file are imported into the target board.
[0014] The above deployment process is for the HC3080 chip, that is, generally, a model that can be recognized by the embedded system of the target chip is constructed according to the information recorded in the NHNN framework, and the quantized NHNN weights and the dynamic library files and executable files of the aforementioned constructed model are imported. For the HC3080 chip, the specific process is as follows.
[0015] In the above-mentioned automatic quantization deployment method for AI chips supporting operator-level operations, the method further includes:
[0016] Rearrange the priorities of various operations under the NHNN framework;
[0017] Prioritize operations that can run simultaneously.
[0018] According to the priority arrangement, a multi-core parallel scheduling strategy is generated.
[0019] In the above-mentioned automatic quantization deployment method for AI chips supporting operator-level operations, the multi-core parallel scheduling strategy generation method includes sequentially retrieving the NHNN framework, taking the priority number of the first operation as 1, and the priority number of subsequent operations as the priority number of the maximum parent node of the current node + 1 for statistics, and finally generating a multi-core parallel scheduling strategy file;
[0020] When the target chip runs the deployment model, it runs from low to high according to the priority number of the scheduling strategy file. When it is detected that there are multiple operations with a certain priority number, the multiple operations are distributed on multiple cores to run together.
[0021] If the number of operations with the same priority number exceeds the number of cores supported or idle on the target chip, and if the number of idle cores is less than the number of cores on the target chip, the number of idle cores is taken, and multiple operations with the same number of cores are randomly selected and distributed on each core to run simultaneously, and the remaining operations continue to run from low to high priority numbers.
[0022] The above scheme uses the operation information recorded by the NHNN framework to reorder the operations through the child-parent node method, and the sequence numbers arranged in this way enable non-conflicting nodes to be at the same priority, thereby generating a multi-core parallel scheduling strategy file. The model can automatically realize multi-core parallelism on the chip through the above deployment, without the need for developers to write core scheduling plans.
[0023] In the above-mentioned automatic quantization deployment method for AI chips that support operator-level operations, the model operation information includes the name of the current layer operation, the dimension in which the operation is located, whether the operation involves dimension transformation, the output feature size, the node number, the data source, the output layer number, the current layer number, and the input feature size;
[0024] The information needed for quantification is obtained from the dataset used when training the model;
[0025] The information required by the target chip is obtained through calculation;
[0026] In the model operation information, the data source, the output layer number, and the layer number of the current layer are obtained through the sequential running order of different operations of the model to be deployed during runtime; the remaining information is obtained from the intermediate format.
[0027] In the above-mentioned automatic quantization deployment method for an AI chip supporting operator-level operations, the target chip is an AI chip supporting fixed-point quantization;
[0028] And the information required for quantization includes input FL, weight FL, bias FL, and output FL, where FL represents the number of decimal places involved in the fixed-point quantization process;
[0029] The information required by the target chip includes three truncation parameters: trunc_mac, trunc_add, and trunc_bias.
[0030] In the above-mentioned automatic quantization deployment method for AI chips supporting operator-level operations, the three truncation parameters are obtained as follows:
[0031] (1)
[0032] (2)
[0033] trunc_mac and trunc_add are obtained by formula (1), and trunc_bias is obtained by formula (2);
[0034] a represents the integer number of bits of input data, b represents the integer number of bits of output data, c represents the integer number of bits of weight data, and d represents the integer number of bits of bias data;
[0035] N=data bit width of target chip - 1.
[0036] In the above-mentioned automatic quantization deployment method for AI chips supporting operator-level operations, the target chip is the HC3080 chip, and a 16-bit data width is used during operation;
[0037] The three truncation parameters are obtained as follows:
[0038] (1)
[0039] (2).
[0040] In the above-mentioned automatic quantization deployment method for the AI chip supporting operator-level operations, if the model to be deployed is not in an intermediate format, the model to be deployed is converted into an intermediate format;
[0041] The intermediate format is the Onnx format.
[0042] An automatic quantization deployment system for AI chips supporting operator-level operations, including an NHNN model generation module and an automatic deployment module;
[0043] An NHNN model generation module is used to generate an NHNN model including an NHNN framework and NHNN weights according to the model to be deployed in the intermediate format;
[0044] The NHNN framework records the information required for quantization and the information required by the target chip, as well as the model operation information of the model to be deployed;
[0045] The NHNN weights are obtained by quantizing the weights of the model to be deployed according to the information recorded by the NHNN framework, splitting them into operations and storing them in the form of binary files;
[0046] The automatic deployment module is used to build a model that can be recognized by the embedded system of the target chip based on the information recorded in the NHNN framework, and import the quantized NHNN weights and the aforementioned model dynamic library files and executable files into the target board containing the target chip, so as to restore the model to be deployed on the target chip and complete the model deployment.
[0047] The advantages of the present invention are:
[0048] This solution simplifies the model quantization and deployment process for specific AI chips by designing a new model structure. It is applicable to the HC3080 chip, enriches the ecosystem of such chips, saves developers' R&D costs for such chips, and fills the market gap.
[0049] Automatically implement multi-core parallel scheduling strategies during the quantization and deployment process, fully utilize the multi-core characteristics of chips such as HC3080, improve the operating efficiency of the deployed model, and do not require developers to do additional work. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] Figure 1 This is a schematic diagram of the neural network computing cluster structure of the HC3080 chip according to an embodiment of the present invention;
[0051] Figure 2 A flowchart of model quantization in an embodiment of the present invention;
[0052] Figure 3 The algorithm principle of multi-core parallel optimization deployment in the embodiment of the present invention;
[0053] Figure 4 This is a flowchart of the algorithm for multi-core parallel optimization deployment according to an embodiment of the present invention. DETAILED DESCRIPTION
[0054] The present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0055] In the model deployment process, the first step is usually to convert the model under a specific framework, such as pytorch, tensorflow, etc., into an intermediate format, such as ONNX. This process involves exporting the model's structure, weights, and computational graphs into a file so that it can be read and understood by the quantization tool, thereby performing quantization and deployment.
[0056] The purpose of this solution is to help users quickly and easily deploy AI models in frameworks such as pytorch and tensorflow to AI chips that support operator-level operations such as HC3080 by converting AI models in frameworks such as pytorch and tensorflow to NHNN framework models, thereby reducing the deployment cost of AI models for such chips. Like the general quantitative deployment process, this solution also converts AI models in frameworks such as pytorch and tensorflow to OnnxSim format first. Unlike the general quantitative deployment process, this solution uses the method of this solution to generate an NHNN framework model based on the model in OnnxSim format. The NHNN framework model is aimed at general AI chips that support the decomposition of the model to the operator level, especially for the HC3080 chip. It is a new model framework created to simplify the development process of general chips such as HC3080 and reduce the cost of model deployment. It is also the key to this solution. The meaning of NHNN is Nanhu Neural Network. The NHNN framework model includes the NHNN framework and NHNN weights. The NHNN framework records the information required for quantization, the information required for the target chip, and the model operation information of the model to be deployed according to the target chips such as HC3080; the NHNN weights are obtained by quantizing the weights of the model to be deployed according to the information recorded by the NHNN framework, splitting them into units of operation, and storing them in the form of binary files. Then, when the user deploys the model on the chip side, he only needs to build the model that can be recognized in the chip's embedded system according to the guidance of the information recorded by the NHNN framework (for the HC3080 chip, the model is built by calling the API function provided by the chip developer in C language based on the NHNN framework information), and directly import the quantized NHNN weights and the dynamic library files and executable files of the aforementioned model construction to complete the deployment.
[0057] The following takes HC3080 as an example to explain this solution in detail:
[0058] Each MU unit (multiplication unit) in the neural network computing cluster of the HC3080 chip includes 16-bit fixed-point multiplication and addition, which is used to support a large number of multiplication and accumulation operations in machine learning, especially the multiplication and accumulation operations of the CNN convolution layer and the matrix or vector multiplication operations in the CNN fully connected layer and LSTM algorithm. The multiplier in the MU unit completes the multiplication of the 16-bit fixed-point source data SRC and the weight w. The result is sent to the accumulator after being truncated bit_cut_mult. The accumulated result is output as 32 bits to the ADD unit (addition unit) in the cluster. The 32-bit accumulated result is added to the bias value Bias and then output. The structure is as follows: Figure 1 shown.
[0059] In this embodiment, the quantization method of the model adopts fixed-point quantization. For example, the full-precision 32-bit floating-point data can be quantized into a 16-bit integer. The first bit of the binary number corresponding to the 16-bit integer is the sign bit, and the remaining 15 bits have IL integer bits and FL decimal bits. The calculation principle of IL and FL of each layer of data in the model is as follows
[0060]
[0061] This formula counts a quantization range, taking the maximum value of the input data matrix (inputs is not a value, but a multidimensional data matrix) as the standard, ensuring that each element in the input data matrix can be quantized according to a unified standard.
[0062] Inputs represents input data;
[0063] abs() means taking the absolute value;
[0064] max() means taking the maximum value, which is used to define the dynamic range;
[0065] bin() means binary processing;
[0066] Len() calculates the length.
[0067] The quantization principle of the model is to replace the original full-precision floating-point number with a number that can be represented by a 16-bit signed binary number without losing precision as much as possible, and then shift the number left by FL bits to get an integer data. The quantization flow chart of the model is as follows Figure 2 As shown, q represents the integer part of the quantized data, e is a constant (1.0), and i represents the decimal part of the quantized data. The core idea of the quantization process is to replace the input data with the closest number that can be represented in hexadecimal.
[0068] Similar to the general quantization process, this embodiment also performs precision calibration on the quantized model. After all the parameters and input data of the model are quantized, the cosine similarity between the vectors output by each layer before and after the quantization of the model is calculated to indicate the degree of precision loss. The cosine similarity calculation formula is as follows:
[0069]
[0070] The above model quantization and model accuracy calibration of this embodiment adopts conventional fixed-point quantization and accuracy calibration methods, and a more detailed description is not repeated here. The core of this solution lies in the generation of the NHNN model:
[0071] The NHNN model includes the NHNN framework and the corresponding NHNN weights, where the NHNN framework records the information required for quantization, various information required by the HC3080 chip, and the model operations of the model to be deployed.
[0072] The model operation information includes the name of the current layer operation, dimension (the dimension where the operation is located, one dimension is the value, and multiple dimensions are lists), perm (whether the operation involves dimension transformation, whether the matrix dimension needs to be exchanged, and the value is a list), output feature size, node number, data source (list, if it is [] (indicating empty), it means that the input is an image), output layer number (list, if it is [] (indicating empty), it means that the output is a result), current layer number, input feature size (unless otherwise specified, if a value does not exist in the current operation, it is filled with 0).
[0073] The information required for quantization includes input FL, weight FL, bias FL, and output FL;
[0074] The information required by the HC3080 chip includes three truncation parameters: trunc_mac, trunc_add, and trunc_bias.
[0075] In order for the HC3080 chip to correctly interpret the converted 16-bit integer data in the multiplication and addition operation, three truncation parameters (trunc_mac (truncation applied in the multiplication and accumulation operation), trunc_add (truncation after the addition operation), trunc_bias (truncation of the bias value)) are used to indicate the number of decimal places FL and the number of integer places IL in the 16-bit integer. The principle of fixed-point quantization is as follows:
[0076] Assume that there is a layer of convolution, the input data range is [-2^a, 2^a], the output data range is [-2^b, 2^b], the weight data range is [-2^c, 2^c], and the bias data range is [-2^d, 2^d]. Then, according to the INT16 format, the input data contains 1 sign bit, a integer bit, and 15-a decimal bits. Similarly, the weight data contains 1 sign bit, c integer bit, and 15-c decimal bits. The result of multiplying the source data and the weight is a 32-bit fixed-point number. The multiplication result contains 1 sign bit, a+c+1 integer bit, and 30-ac decimal bits. The multiplication result is truncated twice by trunc_mac and trunc_bias to obtain the output result:
[0077]
[0078] In order to meet the data accuracy as much as possible, first assign the truncation configuration to trunc_mac and then assign the truncation configuration to trunc_add when the multiplication result does not overflow. The offset data is aligned with the multiplication result:
[0079]
[0080] The first formula gives priority to satisfying the rules of trunc_mac, obtaining trunc_mac and trunc_add, and then uses the second formula to obtain trunc_bias.
[0081] Ultimately, the NHNN framework will contain the following information:
[0082] The operation name of the current layer, input FL, weight FL, bias FL, output FL, trunc_mac, trunc_add, trunc_bias, convolution kernel size, convolution step size, padding parameter, number of groups for grouped convolution, dimension, perm, output feature size, node number, data source, output layer number, current layer number, input feature size.
[0083] The FL value in the NHNN framework is obtained from the data set used in model training. The three trunc values are calculated using the above formulas. The data source, output layer number, and current layer number are obtained by the order in which different operators are run during model operation. Other model information is obtained from the simplified onnx model. The order of operations in the NHNN framework is arranged according to the order in which each operation is run when the model is running on a computer or other device.
[0084] NHNN weights are the parameters required when the model runs on the chip. Specifically, the weights of the original pytorch model are quantized by the algorithm in this solution, split into layers and stored in the form of binary files. The algorithm for generating binary files is as follows:
[0085] First, the quantized parameters are expanded into one dimension. The expansion method of the input and output data of the convolution layer is based on the length × width × number of channels of the data matrix and supplemented with 0 until the result is a multiple of 16. The expansion method of the weight file is based on the length × width × number of channels + bias of the convolution kernel and supplemented with 0 until the result is a multiple of 16. The expansion method of the input and output data of the fully connected layer is directly expanded according to the number of channels of the data vector and supplemented with 0 until the result is a multiple of 16. The expansion method of the weight file is (input vector dimension + zero padding) × output vector dimension. Since the bias file of the fully connected layer is 1-dimensional, it can be directly expanded. After the data is expanded, the data is represented and stored as a signed 16-bit binary number, which is the weight file of NHNN.
[0086] Furthermore, conventional model quantization and deployment only use a single core of the target chip to run, which is a waste of resources. If multiple cores in the target chip are to be used together, developers need to provide a corresponding core scheduling solution, which is relatively cumbersome to implement. This solution provides a multi-core parallel scheduling strategy, which is explained here using the HC3080 chip as an example.
[0087] The basic requirement of multi-core parallelism is that there should be no data interaction between the operations running on each core to prevent data request deadlock or requested data errors. To solve this problem, this method cuts the entire model and uses the information recorded by the NHNN framework to rearrange the priorities of each operation of the model, putting the operations that can run simultaneously at the same priority level to generate a multi-core parallel scheduling strategy, such as Figure 3 and Figure 4 As shown, the principle is as follows:
[0088] Retrieve the NHNN framework in order, with the priority number of the first operation being 1, and the priority number of subsequent operations being the priority number of the current node's maximum parent node + 1, and finally generate a multi-core parallel strategy file. When the HC3080 chip runs the deployment code, it runs from low to high according to the priority number of the parallel strategy file. When it detects that there are multiple operations with a certain priority number, the multiple operations are distributed on multiple cores and run together. Of course, if the number of operations with the same priority number exceeds the number of cores supported by the HC3080 chip or the number of cores currently idle on the HC3080 chip, then any multiple operations with the same number of cores supported or idle by the HC3080 chip are distributed on each core and run simultaneously, and the remaining operations continue to run from low to high according to the priority number.
[0089] Finally, according to the order of operations in the NHNN framework, the corresponding API functions are called and the corresponding NHNN weight files are called at the same time to generate the source files required for deployment. Then, the cross-compilation chain supported by the chip is called to compile the source files expressed in C language into dynamic library files and executable files recognized by the HC3080 chip, and import them into the board containing the HC3080 chip. The model can be restored in the HC3080 chip. Each operation has a priority number for multi-core parallel operation based on the priority number.
[0090] The specific embodiments described in this embodiment are merely examples of the spirit of the present invention. Those skilled in the art may make various modifications or additions to the specific embodiments described or replace them in similar ways, but they will not deviate from the spirit of the present invention or exceed the scope defined by the attached claims.
Claims
1. An automatic quantization deployment method for an AI chip supporting operator-level operations, characterized in that: This method includes automatic quantization and deployment of the model to be deployed in an intermediate format: Generate an NHNN model including an NHNN framework and NHNN weights; The NHNN framework records the information required for quantization and the information required by the target chip, as well as the model operation information of the model to be deployed; The NHNN weights are obtained by quantizing the weights of the model to be deployed according to the information recorded by the NHNN framework, splitting them into operations and storing them in the form of binary files; A model that can be recognized by the embedded system of the target chip is constructed according to the information recorded in the NHNN framework, and the quantized NHNN weights are imported into the target board containing the target chip to complete the model deployment.
2. The automatic quantization deployment method for an AI chip supporting operator-level operations according to claim 1, characterized in that: Model deployment specifically includes: Call the corresponding API functions according to the operation sequence recorded in the NHNN framework, and call the corresponding NHNN weights at the same time to generate the source files required for deployment; The cross-compilation chain supported by the target chip is called to compile the source file into a dynamic library file and an executable file that can be recognized by the target chip, and the quantized NHNN weight and model dynamic library file and executable file are imported into the target board.
3. The automatic quantization deployment method for an AI chip supporting operator-level operations according to claim 1, characterized in that: The method further comprises: Rearrange the priorities of various operations under the NHNN framework; Prioritize operations that can run simultaneously. According to the priority arrangement, a multi-core parallel scheduling strategy is generated.
4. The automatic quantization deployment method for an AI chip supporting operator-level operations according to claim 3, characterized in that: The method for generating a multi-core parallel scheduling strategy includes searching the NHNN framework in order, taking the priority number of the first operation as 1, and the priority number of subsequent operations as the priority number of the maximum parent node of the current node + 1 for statistics, and finally generating a multi-core parallel scheduling strategy file; When the target chip runs the deployment model, it runs from low to high according to the priority number of the scheduling strategy file. When it is detected that there are multiple operations with a certain priority number, the multiple operations are distributed on multiple cores to run together. If the number of operations with the same priority number exceeds the number of cores supported or idle on the target chip, multiple operations with the same number of cores are randomly selected and distributed to each core to run simultaneously, and the remaining operations continue to run from low to high priority numbers.
5. The automatic quantization deployment method for an AI chip supporting operator-level operations according to claim 1, characterized in that: Model operation information includes the name of the current layer operation, the dimension in which the operation is located, whether the operation involves dimensional transformation, output feature size, node number, data source, output layer number, current layer number, and input feature size; The information required for quantification is obtained from the dataset; The information required by the target chip is obtained through calculation; In the model operation information, the data source, output layer number, and current layer number are obtained by the order in which different operations of the model to be deployed are run; The remaining information is obtained from the intermediate format described.
6. The automatic quantization deployment method for an AI chip supporting operator-level operations according to claim 5, characterized in that: The target chip is an AI chip that supports fixed-point quantization; And the information required for quantization includes input FL, weight FL, bias FL, and output FL, where FL represents the number of decimal places involved in the fixed-point quantization process; The information required by the target chip includes three truncation parameters: trunc_mac, trunc_add, and trunc_bias.
7. The automatic quantization deployment method for an AI chip supporting operator-level operations according to claim 6, characterized in that: The three truncation parameters are obtained as follows: (1) (2) trunc_mac and trunc_add are obtained by formula (1), and trunc_bias is obtained by formula (2); a represents the integer number of bits of input data, b represents the integer number of bits of output data, c represents the integer number of bits of weight data, and d represents the integer number of bits of bias data; N = data bit width of target chip - 1.
8. The method for automatic quantization deployment of an AI chip supporting operator-level operations according to claim 7, characterized in that: The target chip is HC3080 chip, and 16-bit data width is used during operation; The three truncation parameters are obtained as follows: (1) (2)。 9. The method for automatic quantization deployment of an AI chip supporting operator-level operations according to claim 1, characterized in that: If the model to be deployed is not in the intermediate format, convert the model to be deployed into the intermediate format; The intermediate format is the Onnx format.
10. An automatic quantization deployment system for AI chips supporting operator-level operations, characterized in that: Includes NHNN model generation module and automatic deployment module; An NHNN model generation module is used to generate an NHNN model including an NHNN framework and NHNN weights according to the model to be deployed in the intermediate format; The NHNN framework records the information required for quantization and the information required by the target chip, as well as the model operation information of the model to be deployed; The NHNN weights are obtained by quantizing the weights of the model to be deployed according to the information recorded by the NHNN framework, splitting them into operations and storing them in the form of binary files; The automatic deployment module is used to build a model that can be recognized by the embedded system of the target chip based on the information recorded in the NHNN framework, and import the quantized NHNN weights and model dynamic library files and executable files into the target board containing the target chip to restore the model to be deployed on the target chip and complete the model deployment.
Citation Information
Cited By
A method and system for automatically generating inference code during model deployment.
CN120297418B
