WO2021036412A1 - 数据处理方法、装置、计算机设备和存储介质 - Google Patents

数据处理方法、装置、计算机设备和存储介质 Download PDF

Info

Publication number
WO2021036412A1
WO2021036412A1 PCT/CN2020/095673 CN2020095673W WO2021036412A1 WO 2021036412 A1 WO2021036412 A1 WO 2021036412A1 CN 2020095673 W CN2020095673 W CN 2020095673W WO 2021036412 A1 WO2021036412 A1 WO 2021036412A1
Authority
WO
WIPO (PCT)
Prior art keywords
data
quantized
iteration
target
bit width
Prior art date
Application number
PCT/CN2020/095673
Other languages
English (en)
French (fr)
Inventor
刘少礼
周诗怡
张曦珊
曾洪博
孟小甫
郭家明
Original Assignee
上海寒武纪信息科技有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Priority claimed from CN201910888552.3A external-priority patent/CN112085151A/zh
Priority claimed from CN201910888449.9A external-priority patent/CN112085176B/zh
Priority claimed from CN201910889202.9A external-priority patent/CN112085187A/zh
Application filed by 上海寒武纪信息科技有限公司 filed Critical 上海寒武纪信息科技有限公司
Publication of WO2021036412A1 publication Critical patent/WO2021036412A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/04Architecture, e.g. interconnection topology

Definitions

  • the present disclosure relates to the field of computer technology, in particular to a neural network data quantification method, device, computer equipment and storage medium.
  • Neural network is a mathematical model or calculation model that imitates the structure and function of biological neural networks. Through the training of sample data, the neural network continuously corrects the network weights and thresholds so that the error function decreases along the negative gradient direction and approximates the expected output. It is a widely used recognition and classification model, which is mostly used for function approximation, model recognition and classification, data compression and time series forecasting. Neural networks are used in image recognition, speech recognition, natural language processing and other fields. However, as the complexity of neural networks increases, the amount of data and data dimensions are increasing, and the increasing amount of data is equal to The data processing efficiency of the device, the storage capacity of the storage device, and the memory access efficiency pose greater challenges.
  • a fixed bit width is used to quantify the operation data of the neural network, that is, the floating-point operation data is converted into the fixed-point operation data, so as to realize the compression of the operation data of the neural network.
  • the same quantization scheme is used for the entire neural network, but there may be large differences between different operation data of the neural network, which often leads to lower accuracy and affects the data operation result.
  • a neural network quantification method which is applied to a terminal, and the terminal is used to run a neural network to perform a target task, and the method includes:
  • multiple data to be quantified are determined from the target data, each of the data to be quantified is a subset of the target data, and the target data is the nerve Any kind of data to be quantified in the to-be-quantized layer of the network, where the data to be calculated includes at least one of input neurons, weights, biases, and gradients;
  • a neural network quantification device which is applied to a terminal, the terminal is used to run a neural network to perform a target task, and the device includes:
  • the data determining module determines a plurality of data to be quantified from the target data according to the target task and/or the accuracy requirements of the terminal, each of the data to be quantified is a subset of the target data, and the target data Is any kind of data to be quantified to be calculated in the layer to be quantized of the neural network, and the data to be calculated includes at least one of input neurons, weights, biases, and gradients;
  • a data quantization module to quantize each of the data to be quantized according to the corresponding quantization parameter to obtain quantized data corresponding to each of the data to be quantized;
  • the result determining module obtains the quantization result of the target data according to the quantization data corresponding to each of the data to be quantized, so that the layer to be quantized performs operations according to the quantization result of the target data.
  • an artificial intelligence chip characterized in that the chip includes the above-mentioned neural network quantification device.
  • an electronic device including the above artificial intelligence chip.
  • a board card comprising: a storage device, an interface device, a control device, and the above-mentioned artificial intelligence chip;
  • the artificial intelligence chip is connected to the storage device, the control device, and the interface device respectively;
  • the storage device is used to store data
  • the interface device is used to implement data transmission between the artificial intelligence chip and external equipment
  • the control device is used to monitor the state of the artificial intelligence chip.
  • a non-volatile computer-readable storage medium having computer program instructions stored thereon, and when the computer program instructions are executed by a processor, the above neural network quantization method is realized.
  • the neural network quantification method, device, computer equipment, and storage medium include: determining from the target data of the layer to be quantized according to the target task and/or the accuracy requirements of the terminal A plurality of data to be quantized, each of the data to be quantized is a subset of the target data, the target data is any data to be quantized in the layer to be quantized of the neural network, and the data to be calculated
  • the data includes at least one of input neurons, weights, offsets, and gradients; each of the data to be quantized is quantized according to the corresponding quantization parameter to obtain the quantized data corresponding to each of the data to be quantized;
  • the quantized data corresponding to each of the data to be quantized obtain a quantization result of the target data, so that the layer to be quantized performs operations according to the quantization result of the target data.
  • the neural network quantification method, device, computer equipment, and storage medium determine multiple data to be quantized from the target data according to the accuracy requirements of the target task and/or terminal, and use the corresponding quantization parameters to perform the quantization of the data to be quantified. Quantification, while ensuring accuracy, reduces the storage space occupied by stored data, ensures the accuracy and reliability of the calculation results, and can improve the efficiency of calculations. Quantization also reduces the size of the neural network model and reduces The performance requirements of the terminal running the neural network model.
  • a neural network quantization device includes a control module and a processing module, the processing module includes a first operation sub-module, and the first operation sub-module includes a main operation sub-module and From the operator module,
  • the control module is configured to determine a plurality of data to be quantized from the target data of the neural network, and obtain the quantization result of the target data according to the quantization data corresponding to each of the data to be quantized, each of the data to be quantized
  • the quantized data of is obtained by quantizing the corresponding quantization parameter, and the quantization parameter includes the point position;
  • the first operation sub-module is used to perform operations related to the quantization result to obtain the operation result
  • the main operation sub-module is configured to send first data to the slave operation sub-module, and the first data includes data of the first type obtained by quantization according to the point position in the quantization result;
  • the slave operation submodule is configured to perform a multiplication operation on the received first data to obtain an intermediate result
  • the main operation sub-module is also used to perform operations on data other than the first data in the intermediate result and the quantization result to obtain an operation result.
  • a neural network quantization method is provided, the method is applied to a neural network quantization device, the device includes a control module and a processing module, the processing module includes a first arithmetic sub-module, the The first operation sub-module includes a main operation sub-module and a slave operation sub-module, and the method includes:
  • the control module is used to determine a plurality of data to be quantized from the target data of the neural network, and the quantization result of the target data is obtained according to the quantization data corresponding to each of the data to be quantized.
  • the data is obtained by quantization using corresponding quantization parameters, and the quantization parameters include point positions;
  • using the first operation submodule to perform operations related to the quantization result to obtain the operation result includes:
  • the main operation sub-module is used to perform operations on the intermediate result and the quantized result data other than the first data to obtain an operation result.
  • an artificial intelligence chip characterized in that the chip includes the above-mentioned neural network quantification device.
  • an electronic device including the above artificial intelligence chip.
  • a board card comprising: a storage device, an interface device, a control device, and the above-mentioned artificial intelligence chip;
  • the artificial intelligence chip is connected to the storage device, the control device, and the interface device respectively;
  • the storage device is used to store data
  • the interface device is used to implement data transmission between the artificial intelligence chip and external equipment
  • the control device is used to monitor the state of the artificial intelligence chip.
  • a non-volatile computer-readable storage medium having computer program instructions stored thereon, and when the computer program instructions are executed by a processor, the above neural network quantization method is realized.
  • the neural network quantization method, device, computer equipment, and storage medium provided by the embodiments of the present disclosure include a control module and a processing module.
  • the processing module includes a first arithmetic sub-module, and the first arithmetic sub-module includes a main arithmetic sub-module and a slave Operation sub-module, control module, used to determine multiple data to be quantized from the target data of the neural network, and obtain the quantization result of the target data according to the quantized data corresponding to each data to be quantized.
  • the quantized data of each data to be quantized is It is obtained by quantization using the corresponding quantization parameter, the quantization parameter includes the position of the point; the first operation sub-module is used to perform operations related to the quantization result to obtain the operation result, and the main operation sub-module is used to send the first data To the slave operation sub-module, the first data includes the first type of data quantized according to the point position in the quantization result; the slave operation sub-module is used to multiply the received first data to obtain an intermediate result; the main operator The module is also used to perform operations on data other than the first data in the intermediate result and the quantized result to obtain the operation result.
  • the neural network quantization method, device, computer equipment, and storage medium use corresponding quantization parameters to quantize multiple data to be quantized in the target data, and perform and quantize the data through the first operation submodule.
  • Results-related calculations while ensuring accuracy, reduce the storage space occupied by stored data, ensure the accuracy and reliability of the calculation results, and can improve the efficiency of calculations, and quantization also reduces the size of the neural network model , Which reduces the performance requirements of the terminal running the neural network model.
  • a neural network quantization method For any layer to be quantized in the neural network, the method includes:
  • multiple data to be quantized are determined from the target data of the layer to be quantized.
  • the target data is any batch of data to be calculated in the layer to be quantized.
  • the data to be calculated includes at least one of input neurons and/or gradients;
  • each data to be quantized is quantized according to the corresponding quantization parameter in turn to obtain the quantized data corresponding to each data to be quantized;
  • a neural network quantization device For any layer to be quantized in the neural network, the device includes:
  • the data determination module determines a plurality of data to be quantized from the target data of the layer to be quantized according to the storage capacity of the terminal running the neural network, and the target data is any batch of data to be calculated in the layer to be quantized Data, the data to be calculated includes at least one of input neurons and/or gradients;
  • the data quantization module adopts a cyclic processing method to sequentially quantize each data to be quantized according to the corresponding quantization parameter to obtain the quantized data corresponding to each data to be quantized;
  • the result determining module obtains the quantization result of the target data according to the quantization data corresponding to each of the data to be quantized, so that the layer to be quantized performs operations according to the quantization result of the target data.
  • an artificial intelligence chip characterized in that the chip includes the above-mentioned neural network quantification device.
  • an electronic device including the above artificial intelligence chip.
  • a board card comprising: a storage device, an interface device, a control device, and the above-mentioned artificial intelligence chip;
  • the artificial intelligence chip is connected to the storage device, the control device, and the interface device respectively;
  • the storage device is used to store data
  • the interface device is used to implement data transmission between the artificial intelligence chip and external equipment
  • the control device is used to monitor the state of the artificial intelligence chip.
  • a non-volatile computer-readable storage medium having computer program instructions stored thereon, and when the computer program instructions are executed by a processor, the above neural network quantization method is realized.
  • the neural network quantification method, device, computer equipment, and storage medium provided by the embodiments of the present disclosure include: determining a plurality of target data from the target data of the layer to be quantized according to the storage capacity of the terminal running the neural network Quantitative data, the target data is any batch of data to be calculated in the layer to be quantized, and the data to be calculated includes at least one of input neurons and/or gradients; a cyclic processing method is adopted, according to each successively Each piece of data to be quantized is quantized according to the corresponding quantization parameter to obtain the quantized data corresponding to each data to be quantized; the quantization result of the target data is obtained according to the quantized data corresponding to each of the data to be quantized, so that the The quantization layer performs operations according to the quantization result of the target data.
  • the neural network quantization method, device, computer equipment and storage medium determine multiple data to be quantized from the target data of the layer to be quantized according to the storage capacity of the terminal running the neural network, and use the corresponding quantization parameters.
  • Each data to be quantized is quantized separately, which reduces the storage space occupied by the stored data while ensuring accuracy, ensures the accuracy and reliability of the calculation results, and can improve the efficiency of calculations, and the quantization also reduces the neural network
  • the size of the model reduces the performance requirements of the terminal running the neural network model.
  • Fig. 1 shows a schematic diagram of a processor according to an embodiment of the present disclosure.
  • Figure 2-1 shows a flowchart of a neural network quantification method according to an embodiment of the present disclosure.
  • Fig. 2-2 shows a schematic diagram of a symmetric fixed-point number representation according to an embodiment of the present disclosure.
  • Figures 2-3 show a schematic diagram of a fixed-point number representation with an offset introduced according to an embodiment of the present disclosure.
  • Figures 2-4 show a flowchart of a neural network quantification method according to an embodiment of the present disclosure.
  • FIGS. 2-5 show a block diagram of a neural network quantization device according to an embodiment of the present disclosure.
  • Figure 3-1 shows a block diagram of a neural network quantization device according to an embodiment of the present disclosure.
  • Fig. 3-2 shows a schematic diagram of a symmetric fixed-point number representation according to an embodiment of the present disclosure.
  • Fig. 3-3 shows a schematic diagram of a fixed-point number representation with an offset introduced according to an embodiment of the present disclosure.
  • Figures 3-4 show a flowchart of a neural network quantization method according to an embodiment of the present disclosure.
  • Figure 4-1 shows a flowchart of a neural network quantification method according to an embodiment of the present disclosure.
  • Fig. 4-2 shows a schematic diagram of a symmetric fixed-point number representation according to an embodiment of the present disclosure.
  • Fig. 4-3 shows a schematic diagram of a fixed-point number representation with an offset introduced according to an embodiment of the present disclosure.
  • Figure 4-4 shows a flowchart of a neural network quantification method according to an embodiment of the present disclosure.
  • Figures 4-5 show a block diagram of a neural network quantization device according to an embodiment of the present disclosure.
  • Fig. 5 shows a structural block diagram of a board according to an embodiment of the present disclosure.
  • the term “if” can be interpreted as “when” or “once” or “in response to determination” or “in response to detection” depending on the context.
  • the phrase “if determined” or “if detected [described condition or event]” can be interpreted as meaning “once determined” or “in response to determination” or “once detected [described condition or event]” depending on the context ]” or “in response to detection of [condition or event described]”.
  • Fig. 1 shows a schematic diagram of a processor according to an embodiment of the present disclosure.
  • the processor 100 can execute the following method.
  • the processor 100 includes multiple processing units 101 and a storage unit 102.
  • the multiple processing units 101 are used to execute instruction sequences, and the storage unit 102 is used to store data. Including random access memory (RAM, Random Access Memory) and register file.
  • RAM random access memory
  • the multiple processing units 101 in the processor 100 can not only share part of the storage space, for example, share part of the RAM storage space and the register file, but also have their own storage space at the same time.
  • the data to be calculated in the neural network is usually in the floating-point data format or the fixed-point data format with higher precision.
  • the floating-point data format or the fixed-point data format with higher precision is various to be calculated Data, which leads to a large amount of computation and memory access overhead for neural network operation.
  • the neural network quantization method, device, computer equipment, and storage medium provided by the embodiments of the present disclosure can perform local quantization of the data to be calculated in the neural network according to different types of data to be calculated.
  • the quantized data format is usually It is a fixed-point data format with shorter bit width and lower precision. The use of lower-precision quantized data to perform neural network operations can reduce the amount of calculation and memory access.
  • the quantized data format can be a fixed-point data format with a shorter bit width.
  • the to-be-calculated data in the floating-point data format can be quantized into the to-be-calculated data in the fixed-point data format, and the to-be-calculated data in the fixed-point format with higher precision can also be quantized into the data to be calculated in the fixed-point format with lower precision.
  • multiple data to be quantized are determined from the target data, and the corresponding quantization parameters are used to quantify the data to be quantized. While ensuring accuracy, the storage space occupied by the stored data is reduced.
  • the quantization accuracy refers to the size of the error between the quantized data and the pre-quantized data.
  • the quantization accuracy can affect the accuracy of the neural network operation result. The higher the quantization accuracy, the higher the accuracy of the calculation result, but the greater the amount of calculation and the greater the memory access overhead.
  • the quantized data with a longer bit width has a higher quantization accuracy, and is also more accurate when used to perform neural network operations.
  • the quantization with a longer bit width has a larger amount of data calculation, a larger memory access overhead, and lower calculation efficiency.
  • the quantized data obtained by using different quantization parameters will have different quantization precisions, which will produce different quantization results, and will also have different effects on the calculation efficiency and accuracy of the calculation results.
  • the neural network is quantized to balance the calculation efficiency and the accuracy of the calculation results, and the quantized data bit width and quantization parameters that are more in line with the data characteristics of the data to be calculated can be used.
  • the data to be calculated in the neural network may include at least one of weights, neurons, biases, and gradients.
  • the data to be calculated is a matrix containing multiple elements.
  • traditional neural network quantization the whole data to be calculated is usually quantified and then operated.
  • a part of the overall quantized data to be calculated is usually used to perform calculations.
  • the convolutional layer when the overall quantized input neuron is used for convolution operation, according to the dimensionality and step size of the convolution kernel, the dimensionality equivalent to the convolution kernel is extracted from the overall quantized input neuron.
  • the quantized neuron performs convolution operation.
  • the overall quantized input neuron when used to perform the matrix multiplication operation, the quantized neurons are extracted row by row from the overall quantized input neuron to perform the matrix multiplication operation. Therefore, in the traditional neural network quantization method, the entire data to be calculated is quantized and then calculated according to the partially quantized data, and the overall calculation efficiency is low. In addition, the overall quantified data to be calculated before performing the calculation requires the storage of the overall quantized data to be calculated, which occupies a large storage space, which cannot meet the accuracy requirements of different terminals, and cannot meet the needs of terminals to perform different tasks using neural networks. Mission requirements.
  • the neural network quantification method can be applied to a processor, and the processor can be a general-purpose processor, such as a CPU (Central Processing Unit, central processing unit), or an artificial intelligence for performing artificial intelligence operations.
  • processor processor
  • Artificial intelligence operations can include machine learning operations, brain-like operations, and so on. Among them, machine learning operations include neural network operations, k-means operations, support vector machine operations, and so on.
  • the artificial intelligence processor may, for example, include GPU (Graphics Processing Unit), NPU (Neural-Network Processing Unit, neural network processing unit), DSP (Digital Signal Process, digital signal processing unit), field programmable gate array (Field-Programmable Gate Array, FPGA) One or a combination of chips.
  • GPU Graphics Processing Unit
  • NPU Neuro-Network Processing Unit
  • DSP Digital Signal Process, digital signal processing unit
  • field programmable gate array Field-Programmable Gate Array
  • FPGA Field-Programmable Gate Array
  • the processor mentioned in the present disclosure may include multiple processing units, and each processing unit can independently run various tasks assigned to it, such as convolution computing tasks and pooling tasks. Or fully connected tasks, etc.
  • the present disclosure does not limit the processing unit and the tasks run by the processing unit.
  • FIG 2-1 shows a flowchart of a neural network quantification method according to an embodiment of the present disclosure.
  • the method includes steps S4-11 to S4-13.
  • This method can be applied to a terminal including the processor 100 shown in FIG. 1, and the terminal is used to run a neural network to perform target tasks.
  • the processing unit 101 is configured to execute step S4-11 to step S4-13.
  • the storage unit 102 is used to store data related to the processing from step S4-11 to step S4-13, such as the data to be quantized, quantization parameters, data bit width, and the like.
  • step S4-11 a plurality of data to be quantized is determined from the target data according to the target task and/or the accuracy requirements of the terminal, and each of the data to be quantified is a subset of the target data, so
  • the target data is any kind of data to be quantified in the layer to be quantized of the neural network, and the data to be calculated includes at least one of input neurons, weights, offsets, and gradients.
  • the amount of data to be calculated, the data volume of each data to be calculated, and the accuracy requirements determined based on the calculation accuracy of the terminal, the current processing capacity, and the storage capacity, the data to be calculated The types of operations involved, etc., determine the way to determine multiple data to be quantified from the target data.
  • the layer to be quantified in the neural network can be any layer in the neural network. Some or all of the layers in the neural network can be determined as the layers to be quantified according to requirements. When the neural network includes multiple layers to be quantized, each layer to be quantized may be continuous or discontinuous. According to different neural networks, the types of layers to be quantized may also be different. For example, the layers to be quantized may be convolutional layers, fully connected layers, etc. The present disclosure does not limit the number and types of quantized layers.
  • the data to be calculated includes at least one of neurons, weights, biases, and gradients. At least one of neurons, weights, biases, and gradients in the layer to be quantified can be quantified according to requirements.
  • the target data is any kind of data to be calculated to be quantified.
  • the data to be calculated are neurons, weights, and biases. If the neurons and weights need to be quantified, the neuron is target data 1, and the weight is target data 2.
  • the quantization method in the present disclosure can be used to quantify each target data to obtain the quantized data corresponding to each target data, and then use the quantized data and non-target data of various target data.
  • the to-be-calculated data that needs to be quantized performs the calculation of the to-be-quantized layer.
  • the reasoning stage of the neural network operation may include a stage in which the trained neural network is subjected to forward operation to complete the set task.
  • the inference stage of the neural network at least one of neurons, weights, biases, and gradients can be used as the data to be quantified. After quantization is performed according to the method in the embodiment of the present disclosure, the quantized data is used to complete the layer to be quantized. Operation.
  • the fine-tuning stage of the neural network operation may include: performing forward and backward operations of a preset number of iterations on the trained neural network, and fine-tuning the parameters to adapt to the stage of the set task.
  • at least one of neurons, weights, biases, and gradients can be quantified according to the method in the embodiment of the present disclosure, and then the quantized data is used to complete the forward direction of the layer to be quantized. Operation or reverse operation.
  • the training phase of the neural network operation may include: iterative training of the initialized neural network to obtain a trained neural network, and the trained neural network can perform specific tasks.
  • the training phase of the neural network at least one of neurons, weights, biases, and gradients can be quantized according to the method in the embodiment of the present disclosure, and then the quantized data is used to complete the forward operation of the layer to be quantized Or reverse operation.
  • a subset of the target data can be used as the data to be quantified, the target data can be divided into multiple subsets in different ways, and each subset can be regarded as the data to be quantified.
  • the target data can be divided into multiple data to be quantified according to the type of operation to be performed on the target data. For example, when the target data needs to be subjected to a convolution operation, the target data can be divided into multiple data to be quantized corresponding to the convolution kernel according to the height and width of the convolution kernel.
  • the target data is a left matrix that requires a matrix multiplication operation, the target data can be divided into multiple data to be quantized by rows.
  • the target data can be divided into multiple data to be quantized at one time, or the target data can be divided into multiple data to be quantized in sequence according to the order of operations.
  • the target data into multiple data may be quantified according to a preset data division method.
  • the preset data division method may be: division according to a fixed data size, or division according to a fixed data shape.
  • each data to be quantized can be quantized separately, and operations can be performed based on the quantized data of each data to be quantized.
  • the quantization time required for a data to be quantized is shorter than the overall quantization time of the target data.
  • the quantized data can be used to perform subsequent operations without waiting for all the target data. After the quantized data are all quantized, the calculation is performed. Therefore, the quantization method of target data in the present disclosure can improve the calculation efficiency of target data.
  • step S4-12 each of the data to be quantized is respectively quantized according to the corresponding quantization parameter to obtain quantized data corresponding to each of the data to be quantized.
  • the quantization parameter corresponding to the data to be quantized may be one quantization parameter or multiple quantization parameters.
  • the quantization parameters may include parameters used for quantizing the data to be quantized, such as point positions.
  • the point position can be used to determine the position of the decimal point in the quantized data.
  • the quantization parameters can also include scaling factors, offsets, and so on.
  • the method of determining the quantization parameter corresponding to the data to be quantized may include a method of determining the quantization parameter corresponding to the target data as the quantization parameter of the data to be quantized after determining the quantization parameter corresponding to the target data.
  • each target data may have a corresponding quantization parameter, and the quantization parameter corresponding to each target data may be different or the same, which is not limited in the present disclosure.
  • the quantization parameter corresponding to the target data can be determined as the quantization parameter corresponding to each data to be quantized. At this time, the quantization parameter corresponding to each data to be quantized is the same.
  • the method of determining the quantization parameter corresponding to the data to be quantized may also include a method of directly determining the quantization parameter corresponding to each data to be quantized.
  • the target data may not have a corresponding quantization parameter, or the target data may have a corresponding quantization parameter but the data to be quantized is not used.
  • the corresponding quantization parameter can be directly set for each data to be quantized.
  • the corresponding quantization parameter can also be calculated according to the data to be quantized. At this time, the quantization parameters corresponding to the data to be quantized may be the same or different.
  • the weight can be divided into multiple weight data to be quantized according to channels, and the weight data to be quantized in different channels can correspond to different quantization parameters.
  • the quantization parameter corresponding to each data to be quantized is different, after each data to be quantized is quantized using the corresponding quantization parameter, the obtained quantization result does not need to affect the calculation of the target data.
  • the method of determining the quantization parameter corresponding to the target data may include: looking up a preset quantization parameter to directly determine the quantization parameter, looking up a correspondence relationship to determine the quantization parameter, Or the way to calculate the quantization parameter based on the data to be quantified.
  • the method of determining the quantization parameter corresponding to the data to be quantized is taken as an example for description:
  • the quantization parameter corresponding to the data to be quantized can be directly set.
  • the set quantization parameters can be stored in the set storage space.
  • the set storage space can be on-chip or off-chip storage space.
  • the set quantization parameter can be stored in the set storage space.
  • each data to be quantized is quantized, it can be quantized after the corresponding quantization parameter is extracted in the set storage space.
  • the quantization parameter corresponding to each type of data to be quantized can be set according to empirical values.
  • the stored quantization parameters corresponding to each type of data to be quantized can also be updated according to requirements.
  • the quantization parameter can be determined by searching the correspondence relationship between the data feature and the quantization parameter according to the data feature of each data to be quantized. For example, when the data distribution of the data to be quantized is sparse and dense, it can correspond to different quantization parameters respectively.
  • the quantization parameter corresponding to the data distribution of the data to be quantized can be determined by searching for the corresponding relationship.
  • the quantization parameter corresponding to each layer to be quantized can be calculated by using the set quantization parameter calculation method according to the data to be quantized.
  • the point position in the quantization parameter can be calculated by using a rounding algorithm according to the maximum value of the absolute value of the data to be quantized and the preset data bit width.
  • step S4-13 the quantization result of the target data is obtained according to the quantization data corresponding to each of the data to be quantized, so that the layer to be quantized performs operations according to the quantization result of the target data.
  • the set quantization algorithm can be used to quantize the data to be quantized according to the quantization parameter to obtain the quantized data.
  • a rounding algorithm can be used as the quantization algorithm, and the quantized data can be rounded and quantized according to the data bit width and point position to obtain the quantized data.
  • the rounding algorithm may include rounding up, rounding down, rounding to zero, and rounding to five. The present disclosure does not limit the specific implementation of the quantization algorithm.
  • Each data to be quantized can be quantized using corresponding quantization parameters. Since the quantization parameter corresponding to each data to be quantized is more suitable for the characteristics of each data to be quantized, the quantization accuracy of each type of quantized data in each layer to be quantized is more in line with the calculation requirements of the target data, and also more in line with the layer to be quantized. Computing needs. On the premise of ensuring the accuracy of the calculation result of the layer to be quantized, the calculation efficiency of the layer to be quantized can be improved, and a balance between the calculation efficiency of the layer to be quantized and the accuracy of the calculation result can be achieved. Further, the target data is divided into multiple data to be quantized and quantized separately.
  • the quantization of the second data to be quantized can be performed while performing calculations based on the quantization result obtained by the quantization.
  • the overall improvement of the computing efficiency of the target data also increases the computing efficiency of the layer to be quantified.
  • the quantized data of the data to be quantized can be combined to obtain the quantized result of the target data. It is also possible to perform a set operation on the quantized data of each data to be quantized to obtain the quantized result of the target data. For example, the quantized data of each data to be quantized can be weighted according to the set weight to obtain the quantized result of the target data. This disclosure does not limit this.
  • the data to be quantified can be quantified offline or online.
  • offline quantization may be to use quantization parameters to perform offline processing on the data to be quantized.
  • Online quantization can be the online processing of the data to be quantized using quantization parameters.
  • a neural network running on an artificial intelligence chip can send the data to be quantified and quantified parameters to an arithmetic device outside the artificial intelligence chip for offline quantification, or use an arithmetic device other than the artificial intelligence chip to analyze the data to be quantified in advance.
  • quantization parameters for offline quantization In the process of the artificial intelligence chip running the neural network, the artificial intelligence chip can use the quantitative parameters to quantify the quantified data online. In the present disclosure, there is no limitation on whether the quantization process of each data to be quantified is online or offline.
  • the neural network quantification method provided in this embodiment is applied to a terminal, and the terminal is used to run a neural network to perform a target task.
  • the method includes: according to the target task and/or the accuracy requirements of the terminal, from A plurality of data to be quantized is determined in the target data of the layer to be quantized. Each data to be quantized is a subset of the target data.
  • the target data is any data to be quantized in the layer to be quantized.
  • the data to be calculated includes input neurons.
  • At least one of, weight, bias, and gradient quantize each data to be quantized according to the corresponding quantization parameter to obtain quantized data corresponding to each data to be quantized; obtain quantized data corresponding to each data to be quantized
  • the quantized data of the target data so that the layer to be quantized performs operations based on the quantized data of the target data.
  • the quantization process and calculation process of each data to be quantized can be executed in parallel, which can improve the quantization efficiency and computing efficiency of the target data, and can also improve the layer to be quantized until the entire neural network is improved. Quantify efficiency and computing efficiency.
  • step S4-11 Five examples of implementation manners of step S4-11 are given below, that is, manner 1 to manner 5, so as to better understand the implementation process of step S4-11.
  • the target data in one or more layers to be quantized is determined as a piece of data to be quantized.
  • the quantitative data volume of the data quantized by the terminal each time can be determined according to the target task and the accuracy requirements of the terminal, and then according to the data volume of the target data and the quantitative data volume in different quantization layers, Determine the target data in one or more to-be-quantized layers as one to-be-quantized data.
  • the input neuron in a layer to be quantized is determined as a piece of data to be quantized.
  • Manner 2 According to the target task and/or the accuracy requirements of the terminal, the same type of data to be calculated in one or more layers to be quantized is determined as one data to be quantized.
  • the quantitative data volume of the data quantized by the terminal each time can be determined according to the target task and the accuracy requirements of the terminal, and then according to the data volume of the target data and the quantitative data volume in different quantization layers, A certain type of target data in one or more to-be-quantized layers is determined as one to-be-quantized data.
  • the input neurons in all the layers to be quantized are determined as one piece of data to be quantized.
  • Manner 3 According to the target task and/or the accuracy requirements of the terminal, the data in one or more channels in the target data corresponding to the layer to be quantized is determined as one data to be quantized.
  • the layer to be quantized When the layer to be quantized is a convolutional layer, the layer to be quantized contains channels. According to the channel and the quantized data volume of the data quantized by the terminal determined according to the accuracy requirements of the target task and the terminal, one or more The data in the channel is determined as a data to be quantized. For example, for a certain convolutional layer, the target data in 2 channels can be determined as one data to be quantized. Or, the target data in each channel can be determined as a piece of data to be quantized.
  • Manner 4 According to the target task and/or the accuracy requirements of the terminal, one or more batches of data in the target data corresponding to the layer to be quantified are determined as one data to be quantified;
  • the dimensions of the input neuron in the convolutional layer can include batch number (batch, B), channel (channel, C), height (height, H), and width (width, W) .
  • each batch of input neurons can be regarded as three-dimensional data with dimensions of channel, height, and width.
  • Each batch of input neurons can correspond to multiple convolution kernels, and the number of channels of each batch of input neurons is consistent with the number of channels of each corresponding convolution kernel.
  • any batch number of input neurons and for any one of the multiple convolution kernels corresponding to the batch number of input neurons, it can be based on the quantitative data volume and the batch number of input neuron data
  • the amount of data (subset) corresponding to the batch of input neurons and the convolution kernel is determined as a plurality of data to be quantified corresponding to the batch of input neurons and the convolution kernel. For example, assuming that the target data B1 has three batches of data, if one batch of data in the target data is determined as one to-be-quantized data, the target data B can be divided into three to-be-quantized data.
  • the quantization process is performed in parallel for each data to be quantized. Since the amount of data to be quantified is smaller than the input neuron, the calculation amount for quantifying a piece of data to be quantified is smaller than the calculation amount for the overall quantization of the input neuron. Therefore, the quantization method in this embodiment can improve the input neuron’s performance. Quantization speed, improve quantization efficiency. It is also possible to divide the input neuron according to the dimension and step size of the convolution kernel, and after each data to be quantized is obtained in turn, the obtained data to be quantized are respectively subjected to convolution operation with the convolution kernel. The quantization process and the convolution operation process of each data to be quantized can be executed in parallel, and the quantization method in this embodiment can improve the quantization efficiency and operation efficiency of the input neuron.
  • Manner 5 Determine the division size of the divided data according to the target task and/or the accuracy requirements of the terminal, and divide the target data in the corresponding layer to be quantized into one or more data to be quantized according to the division size.
  • the real-time processing capability of the terminal can be determined according to the target task and the precision requirements of the terminal, which can include: the speed at which the terminal quantizes the target data, the speed at which the quantized data is calculated, and what the terminal can handle when quantizing and calculating the target data
  • the amount of data represents information related to the processing capability of the terminal to process the target data.
  • the size of the data to be quantized can be determined according to the speed at which the target data is quantized and the speed at which the quantized data is operated, so that the time to quantize the data to be quantized is the same as the speed at which the quantized data is operated. Quantization and calculation are performed simultaneously, which can improve the calculation efficiency of the target data. The stronger the real-time processing capability of the terminal, the larger the size of the data to be quantified.
  • the method of determining the data to be quantified can be set as required.
  • the data to be quantified can include a kind of data to be calculated, such as input neuron (it can also be weight, bias, gradient, and input neuron below.
  • the data to be calculated can be part or all of the input neurons in a certain layer to be quantized, or it can be in multiple layers to be quantized, including all or part of the input neurons in each layer to be quantized. yuan.
  • the data to be quantized may also be all or part of the input neurons corresponding to a certain channel of the layer to be quantized, or all input neurons corresponding to several channels of the layer to be quantized.
  • the data to be quantified can also be part or all of a certain input neuron, and so on. That is to say, the target data can be divided according to any manner, and the present disclosure does not limit this.
  • a quantization parameter corresponding to the target data may be used for quantization in the process of quantizing the target data. After dividing the target data into multiple data to be quantized, the quantization parameter corresponding to each data to be quantized can be used for quantization.
  • the quantization parameter corresponding to each data to be quantized can be preset or calculated according to the data to be quantized. No matter what method is used to determine the quantization parameter corresponding to each data to be quantized, the quantization of the data to be quantized can be achieved.
  • the parameters are more in line with the quantitative requirements of the data to be quantified. For example, when the corresponding quantization parameter is calculated according to the target data, the quantization parameter can be calculated by using the maximum value and the minimum value of each element in the target data.
  • the maximum and minimum values of each element in the data to be quantized can be used to calculate the quantization parameter.
  • the quantization parameter of the data to be quantized can be more suitable for the target data than the quantization parameter of the target data.
  • the data characteristics of the quantized data can make the quantization result of the data to be quantized more accurate and the quantization precision higher.
  • the method may further include:
  • the corresponding quantization parameter is calculated according to each of the data to be quantized and the corresponding data bit width.
  • the reference data bit width or the data bit width reference range that meets the requirements can be determined according to the target task and the accuracy requirements of the terminal, and then the data bit width corresponding to the data to be quantized can be determined based on the characteristics of the data to be quantized.
  • the quantization parameter may include one or more of point position, scaling factor, and offset.
  • calculating the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width may include:
  • the first-type point position of each data to be quantized is obtained according to the maximum absolute value Z 1 in each data to be quantized and the corresponding data bit width.
  • the maximum absolute value Z 1 is the maximum value obtained by taking the absolute value of the data in the data to be quantized.
  • the quantization parameter may not include the offset, assuming that Z 1 is the maximum value of the absolute value of the element in the data to be quantized, and the data corresponding to the data to be quantized
  • the bit width is n
  • a 1 is the maximum value that can be represented by the quantized data after the quantized data is quantized with the data bit width n
  • a 1 is A 1 needs to include Z 1
  • Z 1 must be greater than Therefore
  • the processor may calculate the position s 1 of the first type point according to the maximum absolute value Z 1 and the data bit width n in the data to be quantized.
  • the following formula (4-2) can be used to calculate the first-type point position s 1 corresponding to the data to be quantified:
  • ceil is rounded up
  • Z 1 is the maximum absolute value in the data to be quantized
  • s 1 is the position of the first type of point
  • n is the data bit width.
  • calculating the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width may include:
  • the second-type point position s 2 of each data to be quantized is obtained according to the maximum value, the minimum value and the corresponding data bit width in each of the data to be quantized.
  • the maximum value Z max and the minimum value Z min in the data to be quantized can be obtained first, and then the following formula (4-3) is used for calculation according to the maximum value Z max and the minimum value Z min,
  • the absolute maximum value is obtained directly based on the maximum value and minimum value of the saved data to be quantized. Consume more resources to determine the absolute value of the quantitative data, saving the time to determine the statistical results.
  • calculating the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width includes:
  • the first-type scaling factor f'of each data to be quantized is obtained.
  • the first type of scaling factor f' may include a first scaling factor f 1 and a second scaling factor f 2 .
  • the first scaling factor f 1 can be calculated in the following manner (4-5):
  • the second scaling factor f 2 can be calculated according to the following formula (4-6):
  • calculating the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width may include:
  • the offset of each data to be quantized is obtained.
  • Fig. 2-2 shows a schematic diagram of a symmetric fixed-point number representation according to an embodiment of the present disclosure.
  • the number field of the data to be quantized is distributed with "0" as the symmetric center.
  • Z 1 is the maximum absolute value of all floating-point numbers in the number domain of the data to be quantized.
  • a 1 is the maximum value of the floating-point number that can be represented by an n-bit fixed-point number.
  • the conversion of a floating-point number A 1 to a fixed-point number is ( 2 n-1 -1).
  • a 1 needs to include Z 1 .
  • FIGs 2-3 show a schematic diagram of a fixed-point number representation with an offset introduced according to an embodiment of the present disclosure. As shown in Figure 2-3.
  • the number field of the data to be quantized is not symmetrically distributed with "0" as the center.
  • Z min is the minimum value of all floating-point numbers in the number field of the data to be quantized
  • Z max is the maximum value of all floating-point numbers in the number field of the data to be quantized.
  • a 2 is the maximum value of the translated floating-point number represented by n-bit fixed-point number
  • a 2 is P is the center point between Z min ⁇ Z max , which shifts the number field of the data to be quantized as a whole, so that the number field of the data to be quantized after translation is distributed with "0" as the symmetric center to avoid data "overflow” .
  • the maximum absolute value in the number field of the data to be quantized after translation is Z 2 . It can be seen from Figure 2-3 that the offset is the horizontal distance from point "0" to point "P”, and this distance is called offset o.
  • the offset can be calculated according to the minimum value Z min and maximum value Z max according to the following formula (4-7):
  • o represents the offset
  • Z min represents the minimum value among all the elements of the data to be quantized
  • Z max represents the maximum value among all the elements of the data to be quantized.
  • calculating the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width may include:
  • the quantization parameter includes an offset
  • the second type of scaling factor f" of each of the data to be quantized is obtained.
  • the second type of scaling factor f" may include the first The third scaling factor f 3 and the fourth scaling factor f 4 .
  • a 2 is the maximum value that can be represented by the quantized data after quantizing the shifted data to be quantized with the data bit width n
  • a 2 is The maximum absolute value Z 2 in the number domain of the translated data to be quantized can be calculated according to Z max and the minimum value Z min in the data to be quantized, and then the third scaling factor f can be calculated according to the following formula (4-8) 3 :
  • the fourth scaling factor f 4 can be calculated according to the following formula (4-9):
  • the quantization parameters used are different, and the data used for quantization is different.
  • the quantization parameter may include the first-type point position s 1 .
  • the following formula (4-10) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • I x is the quantized data
  • F x is the data to be quantized
  • round is the rounding operation performed by rounding.
  • the quantized data of the target data can be dequantized according to formula (4-11) to obtain the dequantized data of the target data
  • the quantization parameter may include a first-type point position and a first scaling factor.
  • the following formula (4-12) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • the quantized data of the target data can be inversely quantized according to formula (4-13) to obtain the inverse quantized data of the target data
  • the quantization parameter may include a second scaling factor.
  • the following formula (4-14) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • the quantization data of the target data can be inversely quantized according to formula (4-15) to obtain the inverse quantization data of the target data
  • the quantization parameter may include an offset.
  • the following formula (4-16) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • the quantized data of the target data can be inversely quantized according to formula (4-17) to obtain the inverse quantized data of the target data
  • the quantization parameter may include the position and offset of the second type of point.
  • the following formula (4-18) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • the quantized data of the target data can be inversely quantized according to formula (4-19) to obtain the inverse quantized data of the target data
  • the quantization parameter may include the second-type scaling factor f" and the offset o.
  • the following formula (4-20) may be used to quantize the quantized data to obtain the quantized data I x :
  • the quantized data of the target data can be inversely quantized according to formula (4-21) to obtain the inverse quantized data of the target data
  • the quantization parameter may include the position of the second-type point, the second-type scaling factor, and the offset.
  • the following formula (4-22) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • the quantized data of the target data can be dequantized according to formula (4-23) to obtain the dequantized data of the target data
  • rounding operations can also be used, for example, rounding operations such as rounding up, rounding down, and rounding to zero may be used to replace the rounding operation round in the above formula. It is understandable that, in the case of a certain data bit width, in the quantized data obtained by quantization according to the point position, the more digits after the decimal point, the greater the quantization accuracy of the quantized data.
  • step S4-11 may include:
  • the quantization parameter corresponding to each type of data to be quantized in the layer to be quantized is determined by searching for the corresponding relationship between the data to be quantized and the quantization parameter.
  • the quantization parameter corresponding to each type of data to be quantized in each to-be-quantized layer may be a saved preset value.
  • a corresponding relationship between the data to be quantized and the quantization parameter can be established for the neural network.
  • the corresponding relationship can include the corresponding relationship between each type of data to be quantized and the quantization parameter in each layer to be quantized, and the corresponding relationship can be saved in each layer for shared access Storage space. It is also possible to establish multiple correspondences between the data to be quantized and the quantization parameters for the neural network, and each layer to be quantized corresponds to one of the correspondences.
  • the corresponding relationship of each layer can be stored in the storage space exclusive to this layer, or the corresponding relationship of each layer can be stored in the storage space that can be shared and accessed by each layer.
  • the correspondence between the data to be quantized and the quantization parameter may include correspondences between multiple data to be quantized and multiple quantization parameters corresponding thereto.
  • the corresponding relationship A between the data to be quantized and the quantization parameter may include two data to be quantized: neuron and weight of layer 1 to be quantized, and three quantization parameters of neuron corresponding point position 1, scaling factor 1, and offset 1.
  • the weight corresponds to the two quantization parameters of point position 2 and offset 2.
  • the present disclosure does not limit the specific format of the corresponding relationship between the quantized data and the quantized parameter.
  • the quantization parameter corresponding to each type of data to be quantized in the layer to be quantized can be determined by searching for the corresponding relationship between the data to be quantized and the quantization parameter.
  • Corresponding quantization parameters can be preset for each layer to be quantized, and the corresponding quantization parameters are stored through the corresponding relationship, and then used after searching for the layer to be quantized.
  • the method of acquiring the quantization parameter in this embodiment is simple and convenient.
  • Figures 2-4 show a flowchart of a neural network quantification method according to an embodiment of the present disclosure.
  • the method may further include step S4-14 to step S4-16.
  • step S4-14 the quantization error corresponding to each data to be quantized is determined according to each data to be quantized and the quantized data corresponding to each data to be quantized.
  • the quantization error of the data to be quantized can be determined according to the error between the quantized data corresponding to the data to be quantized and the data to be quantized. You can use the set error calculation method, such as the standard deviation calculation method, the root mean square error calculation method, etc., to calculate the quantization error of the data to be quantified.
  • F i is the corresponding floating point value to be quantized
  • i is the subscript of the data in the data to be quantized.
  • I is the dequantized data corresponding to the floating point value.
  • the quantization error diff bit can also be determined according to the formula (4-25) according to the quantization interval, the number of quantized data, and the corresponding data before quantization.
  • C is the corresponding quantization interval during quantization
  • m is the number of quantized data obtained after quantization
  • F i is the floating point value corresponding to the to-be-quantized data
  • i is the subscript of the data in the to-be-quantized data.
  • F i is the corresponding floating point value to be quantized
  • i is the subscript of the data in the data set to be quantized.
  • I is the dequantized data corresponding to the floating point value.
  • step S4-15 the data bit width corresponding to each data to be quantized is adjusted according to the quantization error and error threshold corresponding to each data to be quantized to obtain the adjustment bit width corresponding to each data to be quantized.
  • the error threshold can be determined based on empirical values, and the error threshold can be used to indicate the expected value of the quantization error.
  • the data bit width corresponding to the number to be quantized can be adjusted to obtain the adjustment bit width corresponding to the data to be quantized.
  • the data bit width can be adjusted to a longer bit width or a shorter bit width to increase or decrease the quantization accuracy.
  • the error threshold can be determined according to the maximum acceptable error. When the quantization error is greater than the error threshold, it means that the quantization accuracy cannot meet expectations, and the data bit width needs to be adjusted to a longer bit width. A smaller error threshold can also be determined according to the higher quantization accuracy. When the quantization error is less than the error threshold, it means that the quantization accuracy is higher, and the operating efficiency of the neural network will be affected. The data bit width can be appropriately adjusted to be shorter The bit width of, in order to appropriately reduce the quantization accuracy and improve the operating efficiency of the neural network.
  • the data bit width can be adjusted according to a fixed number of bits, or the data bit width can be adjusted according to a variable adjustment step according to the difference between the quantization error and the error threshold. This disclosure does not limit this.
  • step S4-16 the data bit width corresponding to each of the data to be quantized is updated to the corresponding adjustment bit width, and the corresponding adjustment quantization parameter is calculated according to each of the data to be quantized and the corresponding adjustment bit width, so that Each of the data to be quantized is quantized according to the corresponding adjusted quantization parameter.
  • the data bit width corresponding to the data to be quantized can be updated to the adjustment bit width. For example, if the data bit width before the update of the data to be quantized is 8 bits, and the adjustment bit width is 12 bits, the data bit width corresponding to the data to be quantized after the update is 12 bits.
  • the adjusted quantization parameter corresponding to the data to be quantized can be calculated according to the adjustment bit width and the data to be quantized.
  • the data to be quantized can be re-quantized according to the adjusted quantization parameters corresponding to the data to be quantized to obtain quantized data with higher or lower quantization accuracy, so that the layer to be quantized can achieve a balance between quantization accuracy and processing efficiency.
  • the data to be quantified between the layers can be considered to have a certain relevance.
  • the adjusted quantization parameter of the layer to be quantized may be used as the adjusted quantization parameter of one or more subsequent layers for quantizing the data to be quantized in one or more subsequent layers of the to-be-quantized layer.
  • the adjusted quantization parameters obtained in the current iteration of the layer to be quantized can also be used to quantize the layer to be quantized in subsequent iterations.
  • the method further includes:
  • the quantization parameter of the layer to be quantized is used in one or more layers after the layer to be quantized.
  • the neural network performs quantization according to the adjusted quantization parameter, which may include re-quantizing the to-be-quantized data using the adjusted quantization parameter only in the to-be-quantized layer, and use the re-obtained quantized data in the calculation of the to-be-quantized layer. It can also include not using the adjusted quantization parameter to re-quantize the data to be quantized in the layer to be quantized, and using the adjusted quantization parameter in one or more subsequent layers of the layer to be quantized for quantization, and/or use the quantized parameter in the layer to be quantized in subsequent iterations Adjust the quantization parameter for quantization.
  • the adjusted quantization parameter may include re-quantizing the to-be-quantized data using the adjusted quantization parameter only in the to-be-quantized layer, and use the re-obtained quantized data in the calculation of the to-be-quantized layer. It can also include not using the adjusted quantization parameter to re-quantize the data to be quantized in the layer to be quantized, and using
  • It may also include using the adjusted quantization parameter to re-quantize in the layer to be quantized, and use the retrieved quantized data in the operation of the layer to be quantized, and use the adjusted quantization parameter to perform quantization in one or more subsequent layers of the layer to be quantized, and /Or in subsequent iterations, quantization is performed by adjusting the quantization parameter in the layer to be quantized.
  • This disclosure does not limit this.
  • the data bit width is adjusted according to the error between the data to be quantized and the quantized data corresponding to the data to be quantized, and the adjusted quantization parameter is calculated according to the adjusted data bit width.
  • different adjusted quantization parameters can be obtained to achieve different quantization requirements such as improving quantization accuracy or improving operation efficiency.
  • the adjusted quantization parameters calculated according to the data to be quantified and the quantized data of the data to be quantified can also be more in line with the data characteristics of the data to be quantized, and achieve a quantization result that more in line with the needs of the data to be quantized. It is between quantization accuracy and processing efficiency. Achieve a better balance.
  • step S4-15 may include:
  • the corresponding data bit width is increased to obtain the corresponding adjustment bit width.
  • the first error threshold may be determined according to the maximum quantization error that can be accepted.
  • the quantization error can be compared with the first error threshold. When the quantization error is greater than the first error threshold, it can be considered that the quantization error is already unacceptable.
  • the quantization accuracy of the data to be quantized can be improved by increasing the data bit width corresponding to the data to be quantized.
  • the data bit width corresponding to the data to be quantized can be increased according to a fixed adjustment step to obtain the adjustment bit width.
  • the fixed adjustment step size can be N bits, and N is a positive integer. Each time you adjust the data bit width, you can increase N bits.
  • the data bit width after each increase the original data bit width + N bits.
  • the data bit width corresponding to the data to be quantized can be increased according to the variable adjustment step length to obtain the adjustment bit width. For example, when the difference between the quantization error and the error threshold is greater than the first threshold, the data bit width can be adjusted according to the adjustment step M1, and when the difference between the quantization error and the error threshold is less than the first threshold, the data can be adjusted according to the adjustment step M1.
  • the step size M2 adjusts the data bit width, where the first threshold is greater than the second threshold, and M1 is greater than M2.
  • the variable adjustment step length can be determined according to requirements. The present disclosure does not limit the adjustment step length of the data bit width and whether the adjustment step length is variable.
  • the adjusted quantization parameter can be obtained by calculating the data to be quantized according to the adjusted bit width.
  • the quantized data obtained by using the adjusted quantization parameter to requantize the data to be quantized has higher quantization accuracy than the quantized data obtained by using the quantized parameter before adjustment.
  • the method may further include:
  • the bit width When increasing the data bit width corresponding to the data to be quantized according to the quantization error, adjust the bit width once to obtain the adjusted bit width, calculate the adjusted quantization parameter according to the adjusted bit width, and quantize the data to be quantized according to the adjusted quantization parameter to obtain the adjusted
  • the quantized data is calculated based on the adjusted quantized data and the to-be-quantized data to obtain the adjusted quantization error of the to-be-quantized data.
  • the adjusted quantization error may still be greater than the first error threshold, that is, the data bit width adjusted once may not meet the adjustment purpose.
  • the adjusted quantization error is still greater than the first error threshold, you can continue to adjust the adjusted data bit width, that is, increase the data bit width corresponding to the data to be quantized multiple times, until the adjusted bit width and the to-be quantized data are finally obtained.
  • the adjusted quantization error obtained from the data is less than the first error threshold.
  • the adjustment step length that is increased multiple times may be a fixed adjustment step length or a variable adjustment step length.
  • the final data bit width original data bit width+B*N bits, where N is the fixed adjustment step length for each increase, and B is the number of times the data bit width is increased.
  • the final data bit width original data bit width+M1+M2+...+Mm, where M1, M2...Mm are the variable adjustment step lengths that are increased each time.
  • the data bit width corresponding to the data to be quantized is increased to obtain the adjustment bit width corresponding to the data to be quantized.
  • the data bit width can be increased by setting the first error threshold and adjusting the step size, so that the adjusted data bit width can meet the quantization requirement.
  • the data bit width can also be adjusted multiple times. The setting of the first error threshold and the adjustment step length enables the quantization parameters to be flexibly adjusted according to the quantization requirements to meet different quantization requirements, so that the quantization accuracy can be adaptively adjusted according to its own data characteristics.
  • step S4-15 may include:
  • the corresponding data bit width is increased to obtain the corresponding adjustment bit width, and the second error threshold is less than the first error threshold
  • the second error threshold can be determined according to the acceptable quantization error and the expected operating efficiency of the neural network.
  • the quantization error can be compared with a second error threshold. When the quantization error is less than the second error threshold, it can be considered that the quantization error exceeds expectations, but the operating efficiency is too low to be acceptable.
  • the quantization accuracy can be reduced to improve the operating efficiency of the neural network, and the quantization accuracy of the data to be quantized can be reduced by reducing the data bit width corresponding to the data to be quantized.
  • the data bit width corresponding to the data to be quantized can be reduced according to a fixed adjustment step length to obtain the adjustment bit width.
  • the data bit width corresponding to the data to be quantized can be reduced according to the variable adjustment step length to obtain the adjustment bit width. For example, when the difference between the quantization error and the error threshold is greater than the first threshold, the data bit width can be adjusted according to the adjustment step M1, and when the difference between the quantization error and the error threshold is less than the first threshold, the data can be adjusted according to the adjustment step M1.
  • the step size M2 adjusts the data bit width, where the first threshold is greater than the second threshold, and M1 is greater than M2.
  • the variable adjustment step length can be determined according to requirements. The present disclosure does not limit the adjustment step length of the data bit width and whether the adjustment step length is variable.
  • the adjusted quantization parameter can be calculated from the data to be quantized according to the adjustment bit width, and the quantized data obtained after re-quantization of the quantized data using the adjusted quantization parameter is higher than the quantization accuracy of the quantized data obtained by using the quantized parameter before adjustment. Lower.
  • the method may further include:
  • the bit width When increasing the data bit width corresponding to the data to be quantized according to the quantization error, adjust the bit width once to obtain the adjusted bit width, calculate the adjusted quantization parameter according to the adjusted bit width, and quantize the data to be quantized according to the adjusted quantization parameter to obtain the adjusted
  • the quantized data is calculated based on the adjusted quantized data and the to-be-quantized data to obtain the adjusted quantization error of the to-be-quantized data.
  • the adjusted quantization error may still be less than the second error threshold, that is, the data bit width adjusted once may not meet the adjustment purpose.
  • the adjusted quantization error is still less than the second error threshold, you can continue to adjust the adjusted data bit width, that is, reduce the data bit width corresponding to the data to be quantized multiple times, until the adjusted bit width and the to-be quantized data are finally obtained.
  • the adjusted quantization error obtained from the data is greater than the second error threshold.
  • the adjustment step length reduced multiple times may be a fixed adjustment step length or a variable adjustment step length.
  • the final data bit width original data bit width-B*N bits, where N is the fixed adjustment step length for each increase, and B is the number of times the data bit width is increased.
  • the final data bit width the original data bit width-M1-M2-...-Mm, where M1, M2...Mm are the variable adjustment steps that are reduced each time.
  • the data bit width corresponding to the data to be quantized is reduced to obtain the adjustment bit width corresponding to the data to be quantized.
  • the data bit width can be reduced by setting the second error threshold and adjusting the step size, so that the adjusted data bit width can meet the quantization requirement.
  • the data bit width can also be adjusted multiple times. The setting of the second error threshold and the adjustment step length enables the quantization parameters to be flexibly and adaptively adjusted according to the quantization requirements to meet different quantization requirements, so that the quantization accuracy is adjustable, and a balance is achieved between the quantization accuracy and the operating efficiency of the neural network .
  • the method further includes:
  • the data bit width corresponding to the data to be quantized is increased, and when the quantization error is less than the second error threshold, the data bit width corresponding to the data to be quantized is reduced to obtain The adjustment bit width corresponding to the data to be quantized.
  • the first error threshold is used to indicate that the quantization accuracy is too low, which can increase the number of bits in the data bit width
  • the second error threshold is used to indicate that the quantization accuracy is too high, which can reduce the data bit width. Digits.
  • the first error threshold is greater than the second error threshold.
  • the quantization error of the data to be quantized can be compared with the two error thresholds at the same time. When the quantization error is greater than the first error threshold, the number of bits of the data bit width is increased. When the quantization error is less than the first error threshold, For the second error threshold, reduce the number of digits of the data bit width. When the quantization error is between the first error threshold and the second error threshold, the data bit width can remain unchanged.
  • the data bit width can be increased or decreased according to the comparison result, and the first error threshold and the second error threshold can be used for more flexible adjustment.
  • Data bit width This makes the adjustment result of the data bit width more in line with quantitative requirements.
  • the method may further include:
  • the target iteration interval corresponding to the data to be quantized is determined, so that the layer to be quantized updates the quantization parameter of the data to be quantized according to the target iteration interval, and the target iteration The interval includes at least one iteration.
  • the fine-tuning phase and/or training phase of the neural network operation includes multiple iterations.
  • Each layer to be quantized in the neural network performs one forward operation and one reverse operation, and after updating the weight of the quantized layer, an iteration is completed.
  • the data to be quantized in the layer to be quantized and/or the quantized data corresponding to the quantized data can be used to measure whether the data to be quantized and/or the quantized data in different iterations can be the same.
  • the quantization parameter is quantized. If the data change range of the data to be quantified in the current iteration and the historical iteration is small, for example, less than the set range change threshold, the same quantization parameter can be used in multiple iterations with smaller data change range.
  • the quantization parameter corresponding to the data to be quantized can be determined by extracting the pre-stored quantization parameter.
  • the quantization parameter can also be calculated according to the data to be quantized and the data bit width. When quantizing the data to be quantized in different iterations, it is necessary to calculate the quantization parameters separately in each iteration. If the data to be quantized in multiple iterations and/or the quantized data corresponding to the data to be quantized has a small data variation range, the same quantization parameter can be used in multiple iterations with a small data variation range, and each iteration can be directly Use the quantization parameter calculated in the first iteration instead of calculating the quantization parameter in each iteration.
  • the weight value between each iteration is constantly updated. If the data change range of the weight value of multiple iterations is small, or the quantified data corresponding to the weight value of multiple iterations The data change range of is small, and the same quantization parameter can be used to quantify the weight in multiple iterations.
  • the target iteration interval can be determined according to the data variation range of the data to be quantified.
  • the target iteration interval includes at least one iteration.
  • the same quantization parameters can be used in each iteration within the target iteration interval, that is, the iteration within the target iteration interval will no longer be updated.
  • the quantization parameter of the quantized data The neural network updates the quantization parameters of the data to be quantized according to the target iteration interval, including the iterations within the target iteration interval, and does not obtain the preset quantization parameters or calculate the quantization parameters, that is, the iterations within the target iteration interval do not update the quantization parameters.
  • the preset quantization parameter is obtained or the quantization parameter is calculated, that is, the quantization parameter is updated in the iteration outside the target iteration interval.
  • the calculated data fluctuation range the corresponding relationship between the preset data fluctuation range and the iteration interval can be searched, and the target iteration interval corresponding to the calculated data fluctuation range can be determined.
  • the corresponding relationship between the data change range and the iteration interval can be preset according to requirements. You can also calculate the target iteration interval by using the set calculation method according to the calculated data variation range.
  • the present disclosure does not limit the calculation method of the data variation range and the acquisition method of the target iteration interval.
  • the data change range of the data to be quantized in the current iteration and historical iteration is obtained, and the data change range of the data to be quantized is determined according to the data change range of the data to be quantized.
  • the target iteration interval can be determined according to the data change range of the to-be-quantized data or the quantized data corresponding to the to-be-quantized data in multiple iterations.
  • the neural network can determine whether to update the quantization parameter according to the target iteration interval.
  • the quantization parameters are not updated in the iterations within the target iteration interval to ensure the quantization accuracy.
  • multiple iterations within the target iteration interval do not update the quantization parameters, which can reduce the number of extractions or calculations of the quantization parameters, thereby improving the computational efficiency of the neural network.
  • the method further includes:
  • the data bit width corresponding to the iteration of the data to be quantized in the target iteration interval is determined, so that the neural network is based on the data to be quantized.
  • the data bit width corresponding to the iteration within the target iteration interval determines the quantization parameter.
  • the quantization parameter of the data to be quantized can be preset or calculated according to the data bit width corresponding to the data to be quantized.
  • the data bit width corresponding to the data to be quantized in different layers to be quantized, or the data bit width corresponding to the data to be quantized in the same layer to be quantized in different iterations, can be adaptively adjusted according to the method in the above-mentioned embodiment of the present disclosure.
  • the iterative correspondence of the data to be quantized in the target iteration interval can be determined according to the preset data bit width of the data to be quantized in the current iteration The data bit width.
  • Each iteration within the target iteration interval may not use its own preset value.
  • the data bit width corresponding to the iteration of the data to be quantized within the target iteration interval can be determined according to the data bit width corresponding to the current iteration of the data to be quantized.
  • the data bit width can be adjusted adaptively, the data bit width can be adjusted once or adjusted multiple times.
  • the data bit width after adaptive adjustment of the data to be quantized in the current iteration can be used as the data bit width corresponding to each iteration within the target iteration interval, and the data bit width is no longer adaptively adjusted for each iteration within the target iteration interval. (Update).
  • the data bit width after adaptive adjustment can be used, or the data bit width before adaptive adjustment can be used, which is not limited in the present disclosure.
  • the data bit width can be adaptively adjusted according to the above-mentioned method of the present disclosure to obtain the data of the data to be quantized that is more in line with the current iteration.
  • the bit width can also use the calculation method of the target iteration interval in the present disclosure to calculate and use a new target iteration interval, thereby ensuring the quantization accuracy of iterations outside the target iteration interval and improving the operating efficiency of the neural network.
  • the data bit width of each iteration within the target iteration interval is the same, and each iteration can calculate the corresponding quantization parameter separately according to the same data bit width.
  • the quantization parameter may include at least one of a point position, a scaling factor, and an offset amount.
  • the quantization parameters can be calculated separately for each iteration within the target iteration interval based on the same data bit width.
  • the quantization parameters include the point position (including the first type of point position and the second type of point position), the zoom factor (including the first type of zoom factor and the second type of zoom factor) and the offset, each iteration within the target iteration interval .
  • the same data bit width can be used to calculate the respective corresponding point positions, scaling factors and offsets.
  • the corresponding quantization parameter of each iteration within the target iteration interval can be determined according to the quantization parameter of the current iteration.
  • the quantization parameters of each iteration within the target iteration interval are not calculated again based on the same data bit width, which can further improve the computational efficiency of the neural network.
  • the corresponding quantization parameter of each iteration within the target iteration interval can be determined according to all quantization parameters or part of the quantization parameters of the current iteration.
  • the quantization parameter includes the position of the second-type point, the second-type scaling factor, and the offset.
  • the data bit width of each iteration and the position of the second type point in the target iteration interval can be determined according to the data bit width of the current iteration and the position of the second type point.
  • the second-type scaling factor and offset of each iteration within the target iteration interval need to be calculated based on the same data bit width. It is also possible to determine the data bit width of each iteration within the target iteration interval, the position of the second type of point, the second type of zoom factor and the offset according to the data bit width of the current iteration, the position of the second type of point, the second type of zoom factor and the offset. Offset, the quantization parameters of each iteration within the target iteration interval do not need to be calculated.
  • the data bit width corresponding to the iteration of the data to be quantized in the target iteration interval is determined, so that the neural network is based on the data to be quantized in the target iteration interval. Iterate the corresponding data bit width to determine the quantization parameter.
  • the data bit width of each iteration in the target iteration interval is determined according to the data bit width of the current iteration. Since the data change range of the data to be quantized in each iteration within the target iteration interval meets the set conditions, the same data bit width is used for calculation
  • the obtained quantization parameters can ensure the quantization accuracy of each iteration within the target iteration interval.
  • Each iteration within the target iteration interval uses the same data bit width, which can also improve the computational efficiency of the neural network. After quantifying the neural network, a balance is reached between the accuracy of the calculation result and the calculation efficiency of the neural network.
  • the method may further include: determining the point position corresponding to the iteration of the to-be-quantized data within the target iteration interval according to the point position corresponding to the current iteration of the to-be-quantized data ,
  • the point position includes a first type of point position and/or a second type of point position.
  • the position of the first-type point corresponding to the current iteration of the data to be quantized is determined.
  • the position of the second-type point corresponding to the current iteration of the data to be quantized is determined.
  • the point position corresponding to the iteration within the target iteration interval can be determined according to the point position corresponding to the current iteration of the data to be quantified.
  • the point position of the data to be quantized in the current iteration can be set as the point position corresponding to each iteration of the data to be quantized in the target iteration interval, or the data to be quantized can be set according to the current iteration.
  • the point position calculated from the preset data bit width is used as the point position corresponding to each iteration of the data to be quantified in the target iteration interval.
  • the point position of the data to be quantized adjusted in the current iteration can be used as the point position of the data to be quantized in each iteration within the target iteration interval.
  • the point position corresponding to the iteration of the data to be quantized in the target iteration interval is determined, and at the same time, the scaling corresponding to the data to be quantized in the current iteration is also possible.
  • Coefficient, determining the scaling factor corresponding to the iteration of the data to be quantized in the target iteration interval, and/or determining the data to be quantized in the target iteration interval according to the offset corresponding to the data to be quantized in the current iteration The offset corresponding to the iteration within.
  • the point position corresponding to the iteration of the data to be quantized in the target iteration interval is determined, and the data to be quantized in the current iteration may also be determined according to the data to be quantized in the current iteration.
  • the bit width determines the data bit width corresponding to the iteration of the data to be quantized in the target iteration interval, where the data bit width corresponding to the current iteration of the data to be quantized can be the preset data bit width of the current iteration or self Adapt to the adjusted data bit width.
  • the point position corresponding to the iteration of the data to be quantized in the target iteration interval is determined.
  • the point position of each iteration within the target iteration interval is determined according to the point position of the current iteration. Since the data change range of the data to be quantified in each iteration within the target iteration interval meets the set conditions, the same point position can be used to ensure the target The quantization accuracy of each iteration within the iteration interval.
  • Using the same point position for each iteration within the target iteration interval can also improve the computational efficiency of the neural network. After quantifying the neural network, a balance is reached between the accuracy of the calculation result and the calculation efficiency of the neural network.
  • obtaining the data change range of the data to be quantified in the current iteration and the historical iteration may include:
  • the moving average of the point positions of the data to be quantified corresponding to each iteration interval is calculated.
  • the location includes the first-category point location and/or the second-category point location;
  • determining the target iteration interval corresponding to the to-be-quantized data according to the data variation range of the to-be-quantized data so that the neural network updates the quantization parameter of the to-be-quantized data according to the target iteration interval may include:
  • the first type of the data to be quantified corresponding to each iteration interval is calculated
  • the historical iteration corresponding to the current iteration determined according to the historical iteration interval may be a historical iteration for calculating the target iteration interval.
  • the correspondence between the current iteration and the corresponding target iteration interval may include:
  • the target iteration interval can be counted from the current iteration, and the target iteration interval can be recalculated at the next iteration after the end of the target iteration interval corresponding to the current iteration.
  • the current iteration is the 100th generation
  • the target iteration interval is 3
  • the iterations within the target iteration interval include: the 100th, 101st, and 102nd generations.
  • the historical iteration corresponding to the current iteration determined according to the historical iteration interval is 100 generations.
  • the target iteration interval can be counted from the next iteration of the current iteration, and the target iteration interval can be recalculated from the last iteration within the target iteration interval.
  • the current iteration is the 100th generation
  • the target iteration interval is 3
  • the iterations within the target iteration interval include: the 101st, 102nd, and 103rd generations.
  • And use 104 as the new calculation to get the first iteration within the target iteration interval.
  • the historical iteration corresponding to the current iteration determined according to the historical iteration interval is 100 generations.
  • the target iteration interval can be counted from the next iteration of the current iteration, and the target iteration interval can be recalculated at the next iteration after the end of the target iteration interval.
  • the current iteration is the 100th generation
  • the target iteration interval is 3
  • the iterations within the target iteration interval include: the 101st, 102nd, and 103rd generations.
  • the target iteration interval corresponding to the 104th generation can be calculated in the 104th generation.
  • And use 105 as the new calculation to get the first iteration within the target iteration interval.
  • the historical iteration corresponding to the current iteration determined according to the historical iteration interval is 100 generations.
  • the target iteration interval may be counted from the Nth iteration after the current iteration, and N is greater than 1, which is not limited in the present disclosure.
  • the calculated moving average of the point positions of the data to be quantized corresponding to each iteration interval includes the first moving average of the point positions of the data to be quantized in the current iteration, and corresponds to the data to be quantized in the previous iteration interval.
  • Formula (4-27) can be used to calculate the first moving average m (t) of the corresponding point position of the current iteration:
  • t is the current iteration
  • t-1 is the historical iteration determined according to the previous iteration interval
  • m (t-1) is the second moving average of the historical iteration determined according to the previous iteration interval
  • s (t) is the point position of the current iteration, which can be the first type point position or the second type point position.
  • is the first parameter.
  • the first parameter may be a hyperparameter.
  • the sliding of the point position of the data to be quantified corresponding to each iteration interval is calculated.
  • Average According to the first moving average of the point position of the current iteration of the data to be quantified, and the second moving average of the point position of the corresponding iteration in the previous iteration interval, the first data variation range is obtained. Determine the target iteration interval corresponding to the data to be quantized according to the first data variation range, so that the neural network updates the quantization parameter of the data to be quantized according to the target iteration interval.
  • the target iteration interval can follow the change trend of the data point to be quantified, and the calculated target iteration interval can be based on the data point to be quantified.
  • the changing trend of the location changes. Since the quantization parameter is determined according to the target iteration interval, the quantization data obtained by quantization according to the quantization parameter can be more in line with the changing trend of the point position of the data to be quantized, and the operation efficiency of the neural network is improved while ensuring the quantization accuracy.
  • a data change range which can include:
  • the absolute value of the difference is determined as the first data variation range.
  • the first data change range diff update1 can be calculated using formula (4-28):
  • the target iteration interval corresponding to the data to be quantized may be determined according to the first data variation range, so that the neural network updates the quantization parameter of the data to be quantized according to the target iteration interval.
  • the target iteration interval I can be calculated according to formula (4-29):
  • is the second parameter
  • is the third parameter
  • the second parameter and the third parameter may be hyperparameters.
  • the first data change range can be used to measure the change trend of the point position.
  • the larger the first data change range it means that the numerical range of the quantized data changes drastically.
  • a shorter target iteration interval is required when updating the quantized parameters. I.
  • the difference between the first moving average value and the second moving average value is calculated; the absolute value of the difference value is determined as the first data variation range. According to the difference between the moving average values, an accurate first data variation range can be obtained.
  • the method may further include: obtaining the second data variation range according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration;
  • determining the target iteration interval corresponding to the to-be-quantized data according to the data variation range of the to-be-quantized data so that the neural network updates the quantization parameter of the to-be-quantized data according to the target iteration interval may include:
  • the second data variation range may be obtained according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration. It is also possible to obtain the second data variation range according to the data to be quantized and the inverse quantization data corresponding to the data to be quantized in the current iteration.
  • the second data variation amplitude diff bit between the data to be quantized and the dequantized data corresponding to the data to be quantized in the current iteration can be calculated according to formula (4-30). It is also possible to use other error calculation methods to calculate the second data variation range diff bit between the data to be quantized and the dequantized data. This disclosure does not limit this.
  • z i is the data to be quantized
  • z i (n) is the inverse quantized data corresponding to the data to be quantized.
  • the second data change range can be used to measure the change trend of the data bit width corresponding to the data to be quantified. The larger the second data change range is, the more likely the data to be quantized need to update the corresponding data bit width, and the interval is required. The shorter the iteration is to update, the greater the second data change range, and the smaller the target iteration interval is required.
  • the second data variation range is obtained according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration.
  • the quantization parameter of the data is obtained according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration.
  • the second data change range can be used to measure the change demand of the data bit width
  • the target iteration interval calculated according to the first data change range and the second data change range can track the changes of the point position and the data bit width at the same time, and the target iteration The interval can also be more in line with the data quantification requirements of the data to be quantified.
  • obtaining the second data variation range according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration may include:
  • the square of the error is determined as the second data variation range.
  • the second data change range diff update2 can be calculated using formula (4-31):
  • is the fourth parameter
  • the fourth parameter may be a hyperparameter
  • different quantization parameters can be obtained by using different data bit widths, and then different quantized data can be obtained, and different second data variation ranges can be generated.
  • the second data change range can be used to measure the change trend of the data bit width. The larger the second data change range, the shorter the target iteration interval is needed to update the data bit width more frequently, that is, the target iteration interval needs to be smaller.
  • determining the target iteration interval corresponding to the data to be quantized according to the first data variation amplitude and the second data variation amplitude of the data to be quantized may include:
  • the target iteration interval can be calculated according to formula (4-32):
  • is the second parameter
  • is the third parameter
  • the second parameter and the third parameter may be hyperparameters.
  • the target iteration interval obtained by using the first data change range and the second data change range can simultaneously measure the change trend of the data bit width and the point position.
  • the change trend of one of the two is larger, Then the target iteration interval can be changed accordingly.
  • the target iteration interval can simultaneously track changes in data bit width and point position and make corresponding adjustments.
  • the quantization parameter updated according to the target iteration interval can be more in line with the change trend of the target data, and finally the quantized data obtained according to the quantization parameter can be more in line with the quantization requirement.
  • obtaining the data change range of the data to be quantified in the current iteration and the historical iteration may include:
  • the data change range of the data to be quantified in the current iteration and the historical iteration is obtained, and the update period includes at least one iteration.
  • the data to be quantified varies greatly. If the target iteration interval is calculated in multiple iterations at the beginning of training or fine-tuning, the calculated target iteration interval may lose its usefulness. According to the preset update period, the target iteration interval is not calculated for each iteration within the update period, and the target iteration interval is not applicable so that multiple iterations use the same data bit width or point position.
  • the iteration progresses beyond the update period, that is, when the current iteration is outside the update period, obtain the data change range of the data to be quantified in the current iteration and historical iterations, and determine the data change range of the data to be quantified according to the data change range of the data to be quantified.
  • the target iteration interval corresponding to the data, so that the neural network updates the quantization parameter of the data to be quantized according to the target iteration interval. For example, if the preset update period is 100 generations, the target iteration interval is not calculated from the first generation to the 100th generation.
  • the iteration progresses to the 101st generation, that is, when the current iteration is the 101st generation, the current iteration is outside the update cycle.
  • the target iteration interval corresponding to the data to be quantized in the 101st generation, and the calculated target iteration interval is used in the 101st generation or a preset number of iterations with the 101st interval.
  • the update cycle can be counted from a preset generation number, for example, multiple iterations in the update cycle can be counted from the first generation, or multiple iterations in the update cycle can be counted from the Nth generation, which is not limited in the present disclosure.
  • the target iteration interval is calculated and used when the iteration progresses beyond the update period. It can avoid the problem that the target iteration interval is of little significance due to the large fluctuation of the data to be quantified in the initial stage of the training process or fine-tuning process of the neural network operation.
  • the neural network can be further improved when the target iteration interval is used. Operating efficiency.
  • the method may further include:
  • the point position of the data to be quantized in the current iteration is determined.
  • each cycle can include multiple iterations.
  • the data used for neural network calculations is completely calculated once into a cycle.
  • the weight changes of the neural network tend to stabilize.
  • the neurons, weights, bias, and gradient waiting for quantified data tend to stabilize.
  • the data to be quantized stabilizes, the data bit width and quantization parameters of the data to be quantized also stabilize.
  • the fine-tuning process after the fine-tuning is stable, the data bit width and quantization parameters of the data to be quantized also tend to be stable.
  • the preset period can be determined according to the period of training stability or fine-tuning stability.
  • the period after the period where the training is stable or the fine-tuning stable period can be determined as the preset period.
  • the stable training period is the Mth period, and the periods after the Mth period can be used as the preset period.
  • a target iteration interval can be calculated every interval, and the data bit width or quantization parameter can be adjusted once according to the calculated target iteration interval to reduce the update times of the data bit width or quantization parameter and improve the performance of the neural network. operating efficiency.
  • the preset period is the period after the Mth period.
  • the target iteration interval calculated according to the Pth iteration in the Mth cycle ends at the Qth iteration in the M+1th cycle.
  • the target iteration interval I m+1 corresponding to it is calculated according to the Q m+ 1th iteration in the M+1th cycle.
  • a period interval can be set. After the training or fine-tuning of the neural network operation reaches a stable level, the quantization parameters such as the data bit width or the point position are updated every period according to the period interval.
  • the period interval can reduce the update times of the data bit width or point position after the training is stable or the fine-tuning is stable, while ensuring the quantization accuracy, it can improve the operating efficiency of the neural network.
  • steps in the flowcharts of FIGS. 2-1 and 2-4 are displayed in sequence as indicated by the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless there is a clear description in this article, there is no strict order for the execution of these steps, and these steps can be executed in other orders. Moreover, at least some of the steps in Figures 2-1 and 2-4 may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. The order of execution of these sub-steps or stages is not necessarily performed sequentially, but may be performed alternately or alternately with other steps or at least a part of the sub-steps or stages of other steps.
  • the embodiments of the present disclosure also provide a non-volatile computer-readable storage medium on which computer program instructions are stored.
  • the computer program instructions are executed by a processor, the above-mentioned neural network data quantization processing method is realized.
  • FIG. 2-5 show a block diagram of a neural network quantization device according to an embodiment of the present disclosure.
  • the device is applied to a terminal including the processor 100 as shown in Figure 1.
  • the terminal is used to run a neural network to perform target tasks.
  • the device includes a data determination module 4-61 and a data quantization module 4. -62 and result determination module 4-63.
  • a certain processing unit 101 is provided with a data determination module 4-61, a data quantization module 4-62, and a result determination module 4-63.
  • the data determination module 4-61, the data quantization module 4-62, and the result determination module 4-63 are respectively provided in different processing units 101.
  • the storage unit 102 is used to store data related to the operation of the data determining module 4-61, the data quantizing module 4-62, and the result determining module 4-63, such as the data to be quantized, the quantization parameter, and the data bit width.
  • the data determining module 4-61 determines a plurality of data to be quantified from the target data according to the target task and/or the accuracy requirements of the terminal, and each of the data to be quantified is a subset of the target data, so
  • the target data is any data to be quantified to be calculated in the layer to be quantized of the neural network, and the data to be calculated includes at least one of input neurons, weights, biases, and gradients;
  • the data quantization module 4-62 quantizes each of the data to be quantized according to the corresponding quantization parameter to obtain the quantized data corresponding to each of the data to be quantized;
  • the result determining module 4-63 obtains the quantization result of the target data according to the quantization data corresponding to each of the data to be quantized, so that the layer to be quantized performs operations according to the quantization result of the target data.
  • the data determining module includes at least one of the following sub-modules:
  • the first determining sub-module determines the target data in one or more layers to be quantized as a piece of data to be quantized according to the target task and/or the accuracy requirements of the terminal;
  • the second determining sub-module determines the same type of data to be calculated in one or more layers to be quantized as one data to be quantized according to the target task and/or the accuracy requirements of the terminal;
  • the third determining sub-module determines the data in one or more channels in the target data corresponding to the layer to be quantized as one data to be quantized according to the target task and/or the accuracy requirements of the terminal;
  • a fourth determining sub-module determine one or more batches of data corresponding to the target data of the layer to be quantized as one data to be quantized;
  • the fifth determining sub-module determines the division size of the divided data according to the target task and/or the accuracy requirements of the terminal, and divides the target data in the corresponding layer to be quantized into one or more data to be quantized according to the division size .
  • the device further includes:
  • the data bit width determination module determines the data bit width corresponding to the data to be quantified according to the target task and/or the accuracy requirements of the terminal;
  • the parameter determination module calculates the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width.
  • the parameter determination module includes:
  • the first point position determination sub-module when the quantization parameter does not include the offset, obtain the first value of each of the data to be quantized according to the maximum value of the absolute value in each of the data to be quantized and the corresponding data bit width. Class point location.
  • the parameter determination module includes:
  • the first maximum value determination sub-module when the quantization parameter does not include an offset, obtain the maximum value of the quantized data according to each of the data to be quantized and the corresponding data bit width;
  • the first scaling factor determining sub-module obtains the first type scaling factor of each data to be quantized according to the maximum absolute value of each data to be quantized and the maximum value of the quantized data.
  • the parameter determination module includes:
  • the second point position determination sub-module when the quantization parameter includes an offset, obtains the second value of each of the data to be quantized according to the maximum value, the minimum value and the corresponding data bit width in each of the data to be quantized Class point location.
  • the parameter determination module includes:
  • the second maximum value determining sub-module when the quantization parameter includes an offset, obtain the maximum value of the quantized data according to each of the data to be quantized and the corresponding data bit width;
  • the first scaling factor determining sub-module obtains the second type scaling factor of each of the data to be quantized according to the maximum value, the minimum value and the maximum value of the quantized data in each of the data to be quantized.
  • the parameter determination module includes:
  • the offset determining sub-module obtains the offset of each data to be quantized according to the maximum value and the minimum value in each of the data to be quantized.
  • the device further includes:
  • the first quantization error determination module determines the quantization error corresponding to each of the data to be quantized according to each of the data to be quantized and the quantized data corresponding to each of the data to be quantized;
  • the adjustment bit width determination module adjusts the data bit width corresponding to each data to be quantized according to the quantization error and error threshold corresponding to each data to be quantized to obtain the adjustment bit width corresponding to each data to be quantized;
  • the adjustment quantization parameter determination module updates the data bit width corresponding to each of the data to be quantized to the corresponding adjustment bit width, and calculates the corresponding adjustment quantization parameter according to each of the data to be quantized and the corresponding adjustment bit width, so that each The data to be quantized is quantized according to the corresponding adjusted quantization parameter.
  • the adjustment bit width determination module includes:
  • the first adjustment bit width determination sub-module when the quantization error is greater than a first error threshold, increases the corresponding data bit width to obtain the corresponding adjustment bit width.
  • the device further includes:
  • the first adjusted quantization error module calculates the adjusted quantization error of each of the data to be quantized according to each of the data to be quantized and the corresponding adjustment bit width;
  • the first adjustment bit width cyclic determination module continues to increase the corresponding adjustment bit width according to the adjusted quantization error and the first error threshold, until the adjusted quantization error is less than or equal to the first error Threshold.
  • the adjustment bit width determination module includes:
  • the second adjustment bit width determination sub-module when the quantization error is less than a second error threshold, increase the corresponding data bit width to obtain the corresponding adjustment bit width, and the second error threshold is less than the first error Threshold.
  • the device further includes:
  • a second adjusted quantization error module which calculates the adjusted quantization error of the to-be-quantized data according to the adjustment bit width and the to-be-quantized data;
  • the second adjustment bit width cyclic determination module continues to reduce the adjustment bit width according to the adjusted quantization error and the second error threshold, until the adjusted quantization calculated according to the adjustment bit width and the data to be quantized The error is greater than or equal to the second error threshold.
  • the device in the fine-tuning phase and/or training phase of the neural network operation, the device further includes:
  • the first data change range determination module which obtains the data change range of the data to be quantified in the current iteration and the historical iteration, where the historical iteration is the iteration before the current iteration;
  • the target iteration interval determination module determines the target iteration interval corresponding to the to-be-quantized data according to the data change range of the to-be-quantized data, so that the to-be-quantized layer updates the quantization of the to-be-quantized data according to the target iteration interval.
  • the target iteration interval includes at least one iteration.
  • the device further includes:
  • the first target iteration interval application module determines, according to the data bit width of the data to be quantized in the current iteration, the data bit width corresponding to the iteration of the data to be quantized in the target iteration interval, so that the nerve
  • the network determines the quantization parameter according to the data bit width corresponding to the iteration of the data to be quantized within the target iteration interval.
  • the device further includes:
  • the second target iteration interval application module determines the point position corresponding to the iteration of the to-be-quantized data within the target iteration interval according to the point position corresponding to the current iteration of the to-be-quantized data, where the point position includes the first Point position of the first type and/or point position of the second type.
  • the first data variation range determination module includes:
  • the sliding average calculation sub-module calculates the point position of the data to be quantified corresponding to each iteration interval according to the point position of the data to be quantified in the current iteration and the point position of the historical iteration corresponding to the current iteration determined according to the historical iteration interval. Sliding average value, where the point position includes a first-type point position and/or a second-type point position;
  • the first data variation range determination sub-module obtains the first data according to the first moving average value of the point position of the current iteration of the data to be quantified and the second moving average value of the point position corresponding to the iteration in the previous iteration interval Range of change
  • the target iteration interval determination module includes:
  • the first target iteration interval determination sub-module determines the target iteration interval corresponding to the data to be quantized according to the amplitude of the first data change, so that the neural network updates the quantization of the data to be quantized according to the target iteration interval parameter.
  • the first data variation range determination submodule includes:
  • the first amplitude determination sub-module calculates the difference between the first sliding average and the second sliding average; and determines the absolute value of the difference as the first data variation amplitude.
  • the device further includes:
  • the second data variation range determination module obtains the second data variation range according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration;
  • the target iteration interval determination module includes:
  • the second target iteration interval determination sub-module determines the target iteration interval corresponding to the to-be-quantized data according to the first data change range and the second data change range of the to-be-quantized data, so that the neural network is based on the
  • the target iteration interval updates the quantization parameter of the data to be quantized.
  • the second data variation range determination module includes:
  • the second amplitude determination sub-module calculates the error between the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration; and determines the square of the error as the second data variation amplitude.
  • the second target iteration interval determination submodule includes:
  • the interval determination sub-module determines the target iteration interval corresponding to the data to be quantified according to the maximum value of the first data change range and the second data change range.
  • the first data variation range determination module includes:
  • the second data change range determination sub-module obtains the data change range of the data to be quantified in the current iteration and the historical iteration when the current iteration is outside the update period, and the update period includes at least one iteration.
  • the device further includes:
  • the cycle interval determination module when the current iteration is within a preset cycle, determines the cycle interval according to the current iteration, the iteration corresponding to the current iteration in the next cycle of the preset cycle, and the iteration interval corresponding to the current iteration;
  • the first period interval application module determines the data bit width of the data to be quantized in the iteration within the period interval according to the data bit width corresponding to the data to be quantized in the current iteration; or
  • the second cycle interval application module determines the point position of the data to be quantized in the iteration within the cycle interval according to the point position corresponding to the data to be quantized in the current iteration.
  • the neural network quantization device determines multiple data to be quantized from the target data according to the accuracy requirements of the target task and/or terminal, and uses the corresponding quantization parameters to perform the respective operations on the multiple data to be quantized in the target data. Quantification, while ensuring accuracy, reduces the storage space occupied by stored data, ensures the accuracy and reliability of the calculation results, and can improve the efficiency of calculations. Quantization also reduces the size of the neural network model and reduces The performance requirements of the terminal running the neural network model enable the neural network model to be applied to terminals such as mobile phones with relatively limited computing power, size, and power consumption.
  • a non-volatile computer-readable storage medium on which computer program instructions are stored, and when the computer program instructions are executed by a processor, the above neural network quantization method is realized.
  • a neural network quantification method applied to a terminal, the terminal is used to run a neural network to perform a target task, the method includes:
  • multiple data to be quantified are determined from the target data, each of the data to be quantified is a subset of the target data, and the target data is the nerve Any kind of data to be quantified in the to-be-quantized layer of the network, where the data to be calculated includes at least one of input neurons, weights, biases, and gradients;
  • the division size of the divided data is determined according to the target task and/or the accuracy requirements of the terminal, and the target data corresponding to the layer to be quantized is divided into one or more data to be quantized according to the division size.
  • Clause D3 The method according to Clause D1 or Clause D2, the method further comprising:
  • the corresponding quantization parameter is calculated according to each of the data to be quantized and the corresponding data bit width.
  • Clause D4 The method according to clause D3, wherein the calculation of the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width includes:
  • the first-type point position of each data to be quantized is obtained according to the maximum absolute value of each data to be quantized and the corresponding data bit width.
  • Clause D5. The method according to clause D3, wherein the calculation of the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width includes:
  • the first-type scaling coefficient of each data to be quantized is obtained.
  • Clause D6 The method according to clause D3, wherein the calculation of the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width includes:
  • the position of the second type point of each data to be quantized is obtained according to the maximum value, the minimum value and the corresponding data bit width in each of the data to be quantized.
  • the quantization parameter includes an offset
  • the second-type scaling coefficient of each data to be quantized is obtained.
  • Clause D8 The method according to clause D3, wherein the calculation of the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width includes:
  • the offset of each data to be quantized is obtained.
  • Clause D9 The method according to any one of Clauses D1 to D8, the method further comprising:
  • the data bit width corresponding to each of the data to be quantized is updated to the corresponding adjustment bit width, and the corresponding adjusted quantization parameter is calculated according to each of the data to be quantized and the corresponding adjustment bit width, so that each of the data to be quantized is based on The corresponding adjusted quantization parameter is quantized.
  • the corresponding data bit width is increased to obtain the corresponding adjustment bit width.
  • Clause D11 According to Clause D9 or Clause D 10, the method further includes:
  • the data bit width corresponding to each of the data to be quantized is adjusted according to the quantization error and error threshold corresponding to each of the data to be quantized to obtain each of the data to be quantized.
  • the adjustment bit width corresponding to the quantized data includes:
  • the corresponding data bit width is increased to obtain the corresponding adjustment bit width, and the second error threshold is less than the first error threshold.
  • Clause D13 The method according to Clause D12, the method further comprising:
  • Clause D14 The method according to any one of clauses D1 to D13, during the fine-tuning phase and/or training phase of the neural network operation, the method further includes:
  • the target iteration interval corresponding to the data to be quantized is determined, so that the layer to be quantized updates the quantization parameter of the data to be quantized according to the target iteration interval, and the target iteration The interval includes at least one iteration.
  • Clause D15 The method according to Clause D14, the method further comprising:
  • the data bit width corresponding to the iteration of the data to be quantized in the target iteration interval is determined, so that the neural network is based on the data to be quantized.
  • the data bit width corresponding to the iteration within the target iteration interval determines the quantization parameter.
  • Clause D16 The method according to Clause D15, the method further comprising:
  • the point position corresponding to the current iteration of the data to be quantized is determined, and the point position includes the first-type point position and/or the first type point position.
  • the second-class point location is determined.
  • the obtaining the data change range of the data to be quantified in the current iteration and the historical iteration includes:
  • the moving average of the point positions of the data to be quantified corresponding to each iteration interval is calculated.
  • the location includes the first-category point location and/or the second-category point location;
  • the determining the target iteration interval corresponding to the to-be-quantized data according to the data change range of the to-be-quantized data so that the neural network updates the quantization parameter of the to-be-quantized data according to the target iteration interval includes:
  • Clause D18 The method according to Clause D17, according to the first moving average of the point position of the current iteration of the data to be quantified, and the second moving average of the point position of the corresponding iteration in the previous iteration interval, Obtain the first data change range, including:
  • the absolute value of the difference is determined as the first data variation range.
  • Clause D19 The method according to Clause D18, the method further comprising:
  • the determining the target iteration interval corresponding to the to-be-quantized data according to the data change range of the to-be-quantized data so that the neural network updates the quantization parameter of the to-be-quantized data according to the target iteration interval includes:
  • Clause D20 The method according to Clause D19, wherein the obtaining a second data variation range according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration includes:
  • the square of the error is determined as the second data variation range.
  • Clause D21 The method according to Clause D19, wherein the determining the target iteration interval corresponding to the data to be quantized according to the first data variation amplitude and the second data variation amplitude of the data to be quantized includes:
  • the obtaining the data change range of the data to be quantified in the current iteration and the historical iteration includes:
  • the data change range of the data to be quantified in the current iteration and the historical iteration is obtained, and the update period includes at least one iteration.
  • Clause D23 The method according to any one of Clauses D14 to D22, the method further comprising:
  • the point position of the data to be quantized in the current iteration is determined.
  • a neural network quantification device applied to a terminal, the terminal is used to run a neural network to perform a target task, the device includes:
  • the data determining module determines a plurality of data to be quantified from the target data according to the target task and/or the accuracy requirements of the terminal, each of the data to be quantified is a subset of the target data, and the target data Is any kind of data to be quantified to be calculated in the layer to be quantized of the neural network, and the data to be calculated includes at least one of input neurons, weights, biases, and gradients;
  • a data quantization module to quantize each of the data to be quantized according to the corresponding quantization parameter to obtain quantized data corresponding to each of the data to be quantized;
  • the result determining module obtains the quantization result of the target data according to the quantization data corresponding to each of the data to be quantized, so that the layer to be quantized performs operations according to the quantization result of the target data.
  • Clause D25 The device according to Clause D24, wherein the data determining module includes at least one of the following submodules:
  • the first determining sub-module determines the target data in one or more layers to be quantized as a piece of data to be quantized according to the target task and/or the accuracy requirements of the terminal;
  • the second determining sub-module determines the same type of data to be calculated in one or more layers to be quantized as one data to be quantized according to the target task and/or the accuracy requirements of the terminal;
  • the third determining sub-module determines the data in one or more channels in the target data corresponding to the layer to be quantized as one data to be quantized according to the target task and/or the accuracy requirements of the terminal;
  • a fourth determining sub-module determine one or more batches of data corresponding to the target data of the layer to be quantized as one data to be quantized;
  • the fifth determining sub-module determines the division size of the divided data according to the target task and/or the accuracy requirements of the terminal, and divides the target data in the corresponding layer to be quantized into one or more data to be quantized according to the division size .
  • Clause D26 The device according to Clause D24 or Clause D 25, wherein the device further includes:
  • the data bit width determination module determines the data bit width corresponding to the data to be quantified according to the target task and/or the accuracy requirements of the terminal;
  • the parameter determination module calculates the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width.
  • Clause D27 The device according to Clause D26, wherein the parameter determination module includes:
  • the first point position determination sub-module when the quantization parameter does not include the offset, obtain the first value of each of the data to be quantized according to the maximum value of the absolute value in each of the data to be quantized and the corresponding data bit width. Class point location.
  • Clause D28 The device according to Clause D26, wherein the parameter determination module includes:
  • the first maximum value determination sub-module when the quantization parameter does not include an offset, obtain the maximum value of the quantized data according to each of the data to be quantized and the corresponding data bit width;
  • the first scaling factor determining sub-module obtains the first type scaling factor of each data to be quantized according to the maximum absolute value of each data to be quantized and the maximum value of the quantized data.
  • Clause D29 The device according to Clause D26, wherein the parameter determination module includes:
  • the second point position determination sub-module when the quantization parameter includes an offset, obtains the second value of each of the data to be quantized according to the maximum value, the minimum value and the corresponding data bit width in each of the data to be quantized Class point location.
  • Clause D30 The device according to Clause D26, wherein the parameter determination module includes:
  • the second maximum value determining sub-module when the quantization parameter includes an offset, obtain the maximum value of the quantized data according to each of the data to be quantized and the corresponding data bit width;
  • the first scaling factor determining sub-module obtains the second type scaling factor of each of the data to be quantized according to the maximum value, the minimum value and the maximum value of the quantized data in each of the data to be quantized.
  • Clause D31 The device according to Clause D26, wherein the parameter determination module includes:
  • the offset determining sub-module obtains the offset of each data to be quantized according to the maximum value and the minimum value in each of the data to be quantized.
  • Clause D32 The device according to any one of Clauses D24 to D31, the device further comprising:
  • the first quantization error determination module determines the quantization error corresponding to each of the data to be quantized according to each of the data to be quantized and the quantized data corresponding to each of the data to be quantized;
  • the adjustment bit width determination module adjusts the data bit width corresponding to each data to be quantized according to the quantization error and error threshold corresponding to each data to be quantized to obtain the adjustment bit width corresponding to each data to be quantized;
  • the adjustment quantization parameter determination module updates the data bit width corresponding to each of the data to be quantized to the corresponding adjustment bit width, and calculates the corresponding adjustment quantization parameter according to each of the data to be quantized and the corresponding adjustment bit width, so that each The data to be quantized is quantized according to the corresponding adjusted quantization parameter.
  • the first adjustment bit width determination sub-module when the quantization error is greater than a first error threshold, increases the corresponding data bit width to obtain the corresponding adjustment bit width.
  • Clause D34 The device according to Clause D32 or Clause D33, the device further comprising:
  • the first adjusted quantization error module calculates the adjusted quantization error of each of the data to be quantized according to each of the data to be quantized and the corresponding adjustment bit width;
  • the first adjustment bit width cyclic determination module continues to increase the corresponding adjustment bit width according to the adjusted quantization error and the first error threshold, until the adjusted quantization error is less than or equal to the first error Threshold.
  • the second adjustment bit width determination sub-module when the quantization error is less than a second error threshold, increase the corresponding data bit width to obtain the corresponding adjustment bit width, and the second error threshold is less than the first error Threshold.
  • Clause D36 The device according to Clause D35, the device further comprising:
  • a second adjusted quantization error module which calculates the adjusted quantization error of the to-be-quantized data according to the adjustment bit width and the to-be-quantized data;
  • the second adjustment bit width cyclic determination module continues to reduce the adjustment bit width according to the adjusted quantization error and the second error threshold, until the adjusted quantization calculated according to the adjustment bit width and the data to be quantized The error is greater than or equal to the second error threshold.
  • Clause D37 The device according to any one of clauses D24 to D36, in the fine-tuning phase and/or training phase of the neural network operation, the device further includes:
  • the first data change range determination module which obtains the data change range of the data to be quantified in the current iteration and the historical iteration, where the historical iteration is the iteration before the current iteration;
  • the target iteration interval determination module determines the target iteration interval corresponding to the to-be-quantized data according to the data change range of the to-be-quantized data, so that the to-be-quantized layer updates the quantization of the to-be-quantized data according to the target iteration interval.
  • the target iteration interval includes at least one iteration.
  • Clause D38 The device according to Clause D37, the device further comprising:
  • the first target iteration interval application module determines, according to the data bit width of the data to be quantized in the current iteration, the data bit width corresponding to the iteration of the data to be quantized in the target iteration interval, so that the nerve
  • the network determines the quantization parameter according to the data bit width corresponding to the iteration of the data to be quantized within the target iteration interval.
  • the second target iteration interval application module determines the point position corresponding to the iteration of the to-be-quantized data within the target iteration interval according to the point position corresponding to the current iteration of the to-be-quantized data, where the point position includes the first Point position of the first type and/or point position of the second type.
  • Clause D40 The device according to Clause D37, wherein the first data variation range determination module includes:
  • the sliding average calculation sub-module calculates the point position of the data to be quantified corresponding to each iteration interval according to the point position of the data to be quantified in the current iteration and the point position of the historical iteration corresponding to the current iteration determined according to the historical iteration interval. Sliding average value, where the point position includes a first-type point position and/or a second-type point position;
  • the first data variation range determination sub-module obtains the first data according to the first moving average value of the point position of the current iteration of the data to be quantified and the second moving average value of the point position corresponding to the iteration in the previous iteration interval Range of change
  • the target iteration interval determination module includes:
  • the first target iteration interval determination sub-module determines the target iteration interval corresponding to the data to be quantized according to the amplitude of the first data change, so that the neural network updates the quantization of the data to be quantized according to the target iteration interval parameter.
  • the first amplitude determination sub-module calculates the difference between the first sliding average and the second sliding average; and determines the absolute value of the difference as the first data variation amplitude.
  • Clause D42 The device according to Clause D41, the device further comprising:
  • the second data variation range determination module obtains the second data variation range according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration;
  • the target iteration interval determination module includes:
  • the second target iteration interval determination sub-module determines the target iteration interval corresponding to the to-be-quantized data according to the first data change range and the second data change range of the to-be-quantized data, so that the neural network is based on the
  • the target iteration interval updates the quantization parameter of the data to be quantized.
  • the second amplitude determination sub-module calculates the error between the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration; and determines the square of the error as the second data variation amplitude.
  • Clause D44 The device according to Clause D42, wherein the second target iteration interval determination submodule includes:
  • the interval determination sub-module determines the target iteration interval corresponding to the data to be quantified according to the maximum value of the first data change range and the second data change range.
  • Clause D45 The device according to any one of clauses D37 to D44, wherein the first data variation range determination module includes:
  • the second data change range determination submodule obtains the data change range of the data to be quantified in the current iteration and the historical iteration when the current iteration is outside the update period, and the update period includes at least one iteration.
  • Clause D46 The device according to any one of Clauses D37 to D45, the device further comprising:
  • the cycle interval determination module when the current iteration is within a preset cycle, determines the cycle interval according to the current iteration, the iteration corresponding to the current iteration in the next cycle of the preset cycle, and the iteration interval corresponding to the current iteration;
  • the first period interval application module determines the data bit width of the data to be quantized in the iteration within the period interval according to the data bit width corresponding to the data to be quantized in the current iteration; or
  • the second cycle interval application module determines the point position of the data to be quantized in the iteration within the cycle interval according to the point position corresponding to the data to be quantized in the current iteration.
  • Clause D48 An electronic device comprising the artificial intelligence chip as described in Clause D47.
  • a board card comprising: a storage device, an interface device and a control device, and the artificial intelligence chip as described in Clause D47;
  • the artificial intelligence chip is connected to the storage device, the control device, and the interface device respectively;
  • the storage device is used to store data
  • the interface device is used to implement data transmission between the artificial intelligence chip and external equipment
  • the control device is used to monitor the state of the artificial intelligence chip.
  • the storage device includes: multiple groups of storage units, each group of the storage unit is connected to the artificial intelligence chip through a bus, and the storage unit is: DDR SDRAM;
  • the chip includes: a DDR controller, which is used to control data transmission and data storage of each storage unit;
  • the interface device is: a standard PCIE interface.
  • Clause D51 A non-volatile computer-readable storage medium on which computer program instructions are stored, which, when executed by a processor, implement the neural network quantification method described in any one of Clauses D1 to D23 .
  • the data to be calculated in the neural network is usually in the floating-point data format or the fixed-point data format with higher precision.
  • the floating-point data format or the fixed-point data format with higher precision is various to be calculated Data, which leads to a large amount of computation and memory access overhead for neural network operation.
  • the neural network quantification method, device, computer equipment and storage medium provided by the embodiments of the present disclosure can perform local quantization of the data to be calculated in the neural network according to different types of data to be calculated.
  • the quantized data format is usually It is a fixed-point data format with shorter bit width and lower precision. The use of lower-precision quantized data to perform neural network operations can reduce the amount of calculation and memory access.
  • the quantized data format can be a fixed-point data format with a shorter bit width.
  • the to-be-calculated data in the floating-point data format can be quantized into the to-be-calculated data in the fixed-point data format, and the to-be-calculated data in the fixed-point format with higher precision can also be quantized into the data to be calculated in the fixed-point format with lower precision.
  • the size of the neural network model is reduced, and the performance requirements for the terminal running the neural network model are reduced, so that the neural network model can be applied to terminals such as mobile phones with relatively limited computing power, size, and power consumption.
  • the quantization accuracy refers to the size of the error between the quantized data and the pre-quantized data.
  • the quantization accuracy can affect the accuracy of the neural network operation result. The higher the conversion accuracy, the higher the accuracy of the calculation result, but the greater the amount of calculation and the greater the memory access overhead.
  • the quantized data with a longer bit width has a higher quantization accuracy, and is also more accurate when used to perform neural network operations.
  • the quantization with a longer bit width has a larger amount of data calculation, a larger memory access overhead, and lower calculation efficiency.
  • the quantized data obtained by using different quantization parameters will have different quantization precisions, which will produce different quantization results, and will also have different effects on the calculation efficiency and accuracy of the calculation results.
  • the neural network is quantized to balance the calculation efficiency and the accuracy of the calculation results, and the quantized data bit width and quantization parameters that are more in line with the data characteristics of the data to be calculated can be used.
  • the data to be calculated in the neural network may include at least one of weights, neurons, biases, and gradients.
  • the data to be calculated is a matrix containing multiple elements.
  • traditional neural network quantization the whole data to be calculated is usually quantified and then operated.
  • a part of the overall quantized data to be calculated is usually used to perform calculations.
  • the convolutional layer when the overall quantized input neuron is used for convolution operation, according to the dimensionality and step size of the convolution kernel, the dimensionality equivalent to the convolution kernel is extracted from the overall quantized input neuron.
  • the quantized neuron performs convolution operation.
  • the entire data to be calculated is quantized and then calculated according to the partially quantized data, and the overall calculation efficiency is low.
  • quantizing the entire data to be calculated before performing calculations requires storing the entire quantized data to be calculated, which occupies a large storage space.
  • the neural network quantification method can be applied to a processor, and the processor can be a general-purpose processor, such as a CPU (Central Processing Unit, central processing unit), or an artificial intelligence for performing artificial intelligence operations.
  • processor processor
  • Artificial intelligence operations can include machine learning operations, brain-like operations, and so on. Among them, machine learning operations include neural network operations, k-means operations, support vector machine operations, and so on.
  • the artificial intelligence processor may, for example, include GPU (Graphics Processing Unit), NPU (Neural-Network Processing Unit, neural network processing unit), DSP (Digital Signal Process, digital signal processing unit), field programmable gate array (Field-Programmable Gate Array, FPGA) One or a combination of chips.
  • GPU Graphics Processing Unit
  • NPU Neuro-Network Processing Unit
  • DSP Digital Signal Process, digital signal processing unit
  • field programmable gate array Field-Programmable Gate Array
  • FPGA Field-Programmable Gate Array
  • the processor mentioned in the present disclosure may include multiple processing units, and each processing unit can independently run various tasks assigned to it, such as convolution computing tasks and pooling tasks. Or fully connected tasks, etc.
  • the present disclosure does not limit the processing unit and the tasks run by the processing unit.
  • Figure 3-1 shows a block diagram of a neural network quantization device according to an embodiment of the present disclosure.
  • the device may include a control module 11 and a processing module 12.
  • the processing module 12 may include a first operation sub-module 121, and the first operation sub-module 121 includes a main operation sub-module 1210 and a slave operation sub-module 1211.
  • the control module 11 is configured to determine a plurality of data to be quantized from the target data of the neural network, and obtain the quantization result of the target data according to the quantization data corresponding to each of the data to be quantized, each of the data to be quantized
  • the quantized data is obtained by quantizing the corresponding quantization parameter, and the quantization parameter includes the point position.
  • the first operation sub-module 121 is configured to perform operations related to the quantization result to obtain the operation result, wherein:
  • the main operation submodule 1210 is configured to send first data to the slave operation submodule, where the first data includes data of the first type obtained by quantization according to the point position in the quantization result.
  • the slave operation sub-module 1211 is configured to perform a multiplication operation on the received first data to obtain an intermediate result.
  • the main operation sub-module 1210 is also used to perform operations on data other than the first data in the intermediate result and the quantization result to obtain an operation result.
  • the amount of data to be calculated, the data volume of each data to be calculated, and the accuracy requirements determined based on the calculation accuracy of the terminal, the current processing capacity, and the storage capacity, the data to be calculated The types of operations involved, etc., determine the way to determine multiple data to be quantified from the target data.
  • the layer to be quantified in the neural network can be any layer in the neural network. Some or all of the layers in the neural network can be determined as the layers to be quantified according to requirements. When the neural network includes multiple layers to be quantized, each layer to be quantized may be continuous or discontinuous. According to different neural networks, the types of layers to be quantized may also be different. For example, the layers to be quantized may be convolutional layers, fully connected layers, etc. The present disclosure does not limit the number and types of quantized layers.
  • the data to be calculated includes at least one of neurons, weights, biases, and gradients. At least one of neurons, weights, biases, and gradients in the layer to be quantified can be quantified according to requirements.
  • the target data is any kind of data to be calculated to be quantified.
  • the data to be calculated are neurons, weights, and biases. If the neurons and weights need to be quantified, the neuron is target data 1, and the weight is target data 2.
  • the quantization method in the present disclosure can be used to quantify each target data to obtain the quantized data corresponding to each target data, and then use the quantized data and non-target data of various target data.
  • the to-be-calculated data that needs to be quantized performs the calculation of the to-be-quantized layer.
  • the quantization parameter may also include an offset and/or a scaling factor
  • the quantization result may also include a second type of data
  • the second type of data may include a point position.
  • the first part of the data and the second part of the data represented by the offset and/or the scaling factor, the first data may also include the first part of the second type of data in the quantization result.
  • the quantization result is a fixed-point number, which can be expressed in the following data format:
  • I is the fixed point number
  • n is the binary representation value of the quantized data (or quantization result)
  • s is the point position (including the first type point position and the second type point position described below)
  • f is the scaling factor ( Including the zoom factor of the first type and the zoom factor of the second type described below)
  • o is the offset.
  • the first part of the second type of data is I ⁇ 2S
  • the second part of the second type of data is f and o.
  • the reasoning stage of the neural network operation may include a stage in which the trained neural network is subjected to forward operation to complete the set task.
  • the inference stage of the neural network at least one of neurons, weights, biases, and gradients can be used as the data to be quantified. After quantization is performed according to the method in the embodiment of the present disclosure, the quantized data is used to complete the layer to be quantized. Operation.
  • the fine-tuning stage of the neural network operation may include: performing forward and backward operations of a preset number of iterations on the trained neural network, and fine-tuning the parameters to adapt to the stage of the set task.
  • at least one of neurons, weights, biases, and gradients can be quantified according to the method in the embodiment of the present disclosure, and then the quantized data is used to complete the forward direction of the layer to be quantized. Operation or reverse operation.
  • the training phase of the neural network operation may include: iterative training of the initialized neural network to obtain a trained neural network, and the trained neural network can perform specific tasks.
  • the training phase of the neural network at least one of neurons, weights, biases, and gradients can be quantized according to the method in the embodiment of the present disclosure, and then the quantized data is used to complete the forward operation of the layer to be quantized Or reverse operation.
  • a subset of the target data can be used as the data to be quantified, the target data can be divided into multiple subsets in different ways, and each subset can be regarded as the data to be quantified.
  • the target data can be divided into multiple data to be quantified according to the type of operation to be performed on the target data. For example, when the target data needs to be subjected to a convolution operation, the target data can be divided into multiple data to be quantized corresponding to the convolution kernel according to the height and width of the convolution kernel.
  • the target data is a left matrix that requires a matrix multiplication operation, the target data can be divided into multiple data to be quantized by rows.
  • the target data can be divided into multiple data to be quantized at one time, or the target data can be divided into multiple data to be quantized in sequence according to the order of operations.
  • the target data into multiple data may be quantified according to a preset data division method.
  • the preset data division method may be: division according to a fixed data size, or division according to a fixed data shape.
  • each data to be quantized can be quantized separately, and operations can be performed based on the quantized data of each data to be quantized.
  • the quantization time required for a data to be quantized is shorter than the overall quantization time of the target data.
  • the quantized data can be used to perform subsequent operations without waiting for all the target data. After the quantized data are all quantized, the calculation is performed. Therefore, the quantization method of target data in the present disclosure can improve the calculation efficiency of target data.
  • the quantization parameter corresponding to the data to be quantized may be one quantization parameter or multiple quantization parameters.
  • the quantization parameters may include parameters used for quantizing the data to be quantized, such as point positions.
  • the point position can be used to determine the position of the decimal point in the quantized data.
  • the quantization parameters can also include scaling factors, offsets, and so on.
  • the method of determining the quantization parameter corresponding to the data to be quantized may include a method of determining the quantization parameter corresponding to the target data as the quantization parameter of the data to be quantized after determining the quantization parameter corresponding to the target data.
  • each target data may have a corresponding quantization parameter, and the quantization parameter corresponding to each target data may be different or the same, which is not limited in the present disclosure.
  • the quantization parameter corresponding to the target data can be determined as the quantization parameter corresponding to each data to be quantized. At this time, the quantization parameter corresponding to each data to be quantized is the same.
  • the method of determining the quantization parameter corresponding to the data to be quantized may also include a method of directly determining the quantization parameter corresponding to each data to be quantized.
  • the target data may not have a corresponding quantization parameter, or the target data may have a corresponding quantization parameter but the data to be quantized is not used.
  • the corresponding quantization parameter can be directly set for each data to be quantized.
  • the corresponding quantization parameter can also be calculated according to the data to be quantized. At this time, the quantization parameters corresponding to the data to be quantized may be the same or different.
  • the weight can be divided into multiple weight data to be quantized according to channels, and the weight data to be quantized in different channels can correspond to different quantization parameters.
  • the quantization parameter corresponding to each data to be quantized is different, after each data to be quantized is quantized using the corresponding quantization parameter, the obtained quantization result does not need to affect the calculation of the target data.
  • the method of determining the quantization parameter corresponding to the target data may include: looking up a preset quantization parameter to directly determine the quantization parameter, looking up a correspondence relationship to determine the quantization parameter, Or the way to calculate the quantization parameter based on the data to be quantified.
  • the method of determining the quantization parameter corresponding to the data to be quantized is taken as an example for description:
  • the quantization parameter corresponding to the data to be quantized can be directly set.
  • the set quantization parameters can be stored in the set storage space.
  • the set storage space can be on-chip or off-chip storage space.
  • the set quantization parameter can be stored in the set storage space.
  • each data to be quantized is quantized, it can be quantized after the corresponding quantization parameter is extracted in the set storage space.
  • the quantization parameter corresponding to each type of data to be quantized can be set according to empirical values.
  • the stored quantization parameters corresponding to each type of data to be quantized can also be updated according to requirements.
  • the quantization parameter can be determined by searching the correspondence relationship between the data feature and the quantization parameter according to the data feature of each data to be quantized. For example, when the data distribution of the data to be quantized is sparse and dense, it can correspond to different quantization parameters respectively.
  • the quantization parameter corresponding to the data distribution of the data to be quantized can be determined by searching for the corresponding relationship.
  • the quantization parameter corresponding to each layer to be quantized can be calculated by using the set quantization parameter calculation method according to the data to be quantized.
  • the point position in the quantization parameter can be calculated by using a rounding algorithm according to the maximum value of the absolute value of the data to be quantized and the preset data bit width.
  • the set quantization algorithm can be used to quantize the data to be quantized according to the quantization parameter to obtain the quantized data.
  • a rounding algorithm can be used as the quantization algorithm, and the quantized data can be rounded and quantized according to the data bit width and point position to obtain the quantized data.
  • the rounding algorithm may include rounding up, rounding down, rounding to zero, and rounding to five. The present disclosure does not limit the specific implementation of the quantization algorithm.
  • Each data to be quantized can be quantized using corresponding quantization parameters. Since the quantization parameter corresponding to each data to be quantized is more suitable for the characteristics of each data to be quantized, the quantization accuracy of each type of quantized data in each layer to be quantized is more in line with the calculation requirements of the target data, and also more in line with the layer to be quantized. Computing needs. On the premise of ensuring the accuracy of the calculation result of the layer to be quantized, the calculation efficiency of the layer to be quantized can be improved, and a balance between the calculation efficiency of the layer to be quantized and the accuracy of the calculation result can be achieved. Further, the target data is divided into multiple data to be quantized and quantized separately.
  • the quantization of the second data to be quantized can be performed while performing calculations based on the quantization result obtained by the quantization.
  • the overall improvement of the computing efficiency of the target data also increases the computing efficiency of the layer to be quantified.
  • the quantized data of the data to be quantized can be combined to obtain the quantized result of the target data. It is also possible to perform a set operation on the quantized data of each data to be quantized to obtain the quantized result of the target data. For example, the quantized data of each data to be quantized can be weighted according to the set weight to obtain the quantized result of the target data. This disclosure does not limit this.
  • the data to be quantified can be quantified offline or online.
  • offline quantization may be to use quantization parameters to perform offline processing on the data to be quantized.
  • Online quantization can be the online processing of the data to be quantized using quantization parameters.
  • a neural network running on an artificial intelligence chip can send the data to be quantified and quantified parameters to an arithmetic device outside the artificial intelligence chip for offline quantification, or use an arithmetic device other than the artificial intelligence chip to analyze the data to be quantified in advance.
  • quantization parameters for offline quantization In the process of the artificial intelligence chip running the neural network, the artificial intelligence chip can use the quantitative parameters to quantify the quantified data online. In the present disclosure, there is no limitation on whether the quantization process of each data to be quantified is online or offline.
  • the quantization result of the target data is obtained according to the quantized data corresponding to each data to be quantized, and the first operation sub-module is used to perform The operation related to the quantization result obtains the operation result.
  • the main operation sub-module is used to send the first data to the slave operation sub-module
  • the slave operation sub-module is used to multiply the received first data to obtain an intermediate result
  • the main operation sub-module is used to calculate the intermediate result. Perform an operation with data other than the first data in the quantization result to obtain an operation result.
  • the quantization process of each data to be quantized can be executed in parallel with the operation process of the main operation sub-module and the slave operation sub-module, which can improve the quantization efficiency and operation efficiency of the target data, and can also improve the quantization layer until the quantization efficiency of the entire neural network And computing efficiency.
  • a quantization parameter corresponding to the target data may be used for quantization in the process of quantizing the target data. After dividing the target data into multiple data to be quantized, the quantization parameter corresponding to each data to be quantized can be used for quantization.
  • the quantization parameter corresponding to each data to be quantized can be preset or calculated according to the data to be quantized. No matter what method is used to determine the quantization parameter corresponding to each data to be quantized, the quantization of the data to be quantized can be achieved.
  • the parameters are more in line with the quantitative requirements of the data to be quantified. For example, when the corresponding quantization parameter is calculated according to the target data, the quantization parameter can be calculated by using the maximum value and the minimum value of each element in the target data.
  • the maximum and minimum values of each element in the data to be quantized can be used to calculate the quantization parameter.
  • the quantization parameter of the data to be quantized can be more suitable for the target data than the quantization parameter of the target data.
  • the data characteristics of the quantized data can make the quantization result of the data to be quantized more accurate and the quantization precision higher.
  • the processing module 12 may further include a data conversion sub-module 122.
  • the data conversion sub-module 122 is configured to perform format conversion of the data to be converted to obtain converted data.
  • the format type of the converted data includes any one of the first type and the second type, wherein the data to be converted includes The data in the target data that has not been subjected to quantization processing, the first data further includes the first part of the converted data of the first type and/or the converted data of the second type,
  • the main arithmetic submodule 1210 is also used to compare the intermediate result, the data other than the first data in the quantization result, and the data after the conversion except the first data. The data is operated to obtain the result of the operation.
  • the data to be converted may also include other data whose data format is different from the first type and the second type and needs to be multiplied together with the quantization result, which is not limited in the present disclosure.
  • the main arithmetic sub-module can send Fixed 1 and Fixed 2 as the first data to the slave arithmetic sub-module, so that the slave arithmetic sub-module implements the multiplication operation of Fixed 1 and Fixed 2 and obtains an intermediate result.
  • the main arithmetic sub-module can send Fixed 1 and Fixed 3 as the first data to the slave arithmetic sub-module, so that the slave arithmetic sub-module implements the multiplication operation of Fixed 1 and Fixed 3 and obtains an intermediate result.
  • FP 4 and data FP 5 need to be multiplied.
  • FP 4 ⁇ FP 5 f 4 ⁇ f 5 ⁇ Fixed 4 ⁇ Fixed 5 +Fixed 4 ⁇ f 4 ⁇ o 5 +Fixed 5 ⁇ f 5 ⁇ o 4 + o 4 ⁇ o 5 .
  • the main arithmetic sub-module can send Fixed 4 and Fixed 5 as the first data to the slave arithmetic sub-module, so that the slave arithmetic sub-module implements the multiplication operation of Fixed 4 and Fixed 5 and obtains an intermediate result.
  • the quantization result may be expressed in the first type or the second type format, and the first operation sub-module may directly perform operations on the quantization result.
  • the quantization result may also be obtained after format conversion of the to-be-converted quantization result that has not been converted into the first type or the second type, wherein the data conversion sub-module 122 is further configured to compare data corresponding to each of the to-be-quantized data The quantized data obtains the to-be-converted quantized result of the target data for format conversion to obtain the quantized result.
  • control module adopts at least one of the following methods (that is, manner 5-1-5) to determine multiple data to be quantized.
  • Manner 5-1 Determine the target data in one or more layers to be quantized as a piece of data to be quantized.
  • the quantitative data volume of the data quantized by the terminal each time can be determined according to the target task and the accuracy requirements of the terminal, and then according to the data volume of the target data and the quantitative data volume in different quantization layers, Determine the target data in one or more to-be-quantized layers as one to-be-quantized data.
  • the input neuron in a layer to be quantized is determined as a piece of data to be quantized.
  • Manner 5-2 Determine the same kind of data to be calculated in one or more layers to be quantized as one data to be quantized.
  • the quantitative data volume of the data quantized by the terminal each time can be determined according to the target task and the accuracy requirements of the terminal, and then according to the data volume of the target data and the quantitative data volume in different quantization layers, A certain type of target data in one or more to-be-quantized layers is determined as one to-be-quantized data.
  • the input neurons in all the layers to be quantized are determined as one piece of data to be quantized.
  • Manner 5-3 Determine the data in one or more channels in the target data corresponding to the layer to be quantized as one data to be quantized.
  • the layer to be quantized When the layer to be quantized is a convolutional layer, the layer to be quantized contains channels. According to the channel and the quantized data volume of the data quantized by the terminal determined according to the accuracy requirements of the target task and the terminal, one or more The data in the channel is determined as a data to be quantized. For example, for a certain convolutional layer, the target data in 2 channels can be determined as one data to be quantized. Or, the target data in each channel can be determined as a piece of data to be quantized.
  • Manner 5-4 Determine one or more batches of data corresponding to the target data of the layer to be quantized as one data to be quantized;
  • the dimensions of the input neuron in the convolutional layer can include batch number (batch, B), channel (channel, C), height (height, H), and width (width, W) .
  • each batch of input neurons can be regarded as three-dimensional data with dimensions of channel, height, and width.
  • Each batch of input neurons can correspond to multiple convolution kernels, and the number of channels of each batch of input neurons is consistent with the number of channels of each corresponding convolution kernel.
  • any batch number of input neurons and for any one of the multiple convolution kernels corresponding to the batch number of input neurons, it can be based on the quantitative data volume and the batch number of input neuron data
  • the amount of data (subset) corresponding to the batch of input neurons and the convolution kernel is determined as a plurality of data to be quantified corresponding to the batch of input neurons and the convolution kernel. For example, assuming that the target data B1 has 3 batches of data, if one batch of data in the target data is determined as one to-be-quantized data, the target data B can be divided into 3 to-be-quantized data.
  • the quantization process is performed in parallel for each data to be quantized. Since the amount of data to be quantified is smaller than the input neuron, the calculation amount for quantifying a piece of data to be quantified is smaller than the calculation amount for the overall quantization of the input neuron. Therefore, the quantization method in this embodiment can improve the input neuron’s performance. Quantization speed, improve quantization efficiency. It is also possible to divide the input neuron according to the dimension and step size of the convolution kernel, and after each data to be quantized is obtained in turn, the obtained data to be quantized are respectively subjected to convolution operation with the convolution kernel. The quantization process and the convolution operation process of each data to be quantized can be executed in parallel, and the quantization method in this embodiment can improve the quantization efficiency and operation efficiency of the input neuron.
  • Manner 5-5 Divide the target data corresponding to the layer to be quantized into one or more data to be quantized according to the determined division size.
  • the real-time processing capability of the terminal can be determined according to the target task and the accuracy requirements of the terminal to determine the division size.
  • the real-time processing capability of the terminal may include: the speed at which the terminal quantifies the target data, the speed at which the quantized data is calculated, the amount of data that the terminal can process when quantizing and calculating the target data, etc., which characterize the terminal's processing capability of processing the target data.
  • the size of the data to be quantized can be determined according to the speed at which the target data is quantized and the speed at which the quantized data is operated, so that the time to quantize the data to be quantized is the same as the speed at which the quantized data is operated. Quantization and calculation are performed simultaneously, which can improve the calculation efficiency of the target data. The stronger the real-time processing capability of the terminal, the larger the size of the data to be quantified.
  • the method of determining the data to be quantified can be set as required.
  • the data to be quantified can include a kind of data to be calculated, such as input neuron (it can also be weight, bias, gradient, and input neuron below.
  • the data to be calculated can be part or all of the input neurons in a certain layer to be quantized, or it can be in multiple layers to be quantized, including all or part of the input neurons in each layer to be quantized. yuan.
  • the data to be quantized may also be all or part of the input neurons corresponding to a certain channel of the layer to be quantized, or all input neurons corresponding to several channels of the layer to be quantized.
  • the data to be quantified can also be part or all of a certain input neuron, and so on. That is to say, the target data can be divided according to any manner, and the present disclosure does not limit this.
  • the processing module 12 may further include a second operation sub-module 123.
  • the second arithmetic sub-module 123 is used to perform arithmetic processing in the device other than the arithmetic processing performed by the first arithmetic sub-module.
  • the first arithmetic sub-module is used to perform the multiplication between the first and second types of fixed-point data
  • the second arithmetic sub-module is used to perform other arithmetic processing, which can speed up the operation efficiency and operation of the device on the data. speed.
  • control module 11 may further include an instruction storage sub-module 111, an instruction processing sub-module 112, and a queue storage sub-module 113.
  • the instruction storage sub-module 111 is used to store instructions corresponding to the neural network.
  • the instruction processing sub-module 112 is configured to parse the instruction to obtain the operation code and operation domain of the instruction.
  • the queue storage submodule 113 is configured to store an instruction queue.
  • the instruction queue includes a plurality of instructions to be executed sequentially arranged in an execution order, and the plurality of instructions to be executed may include instructions corresponding to the neural network.
  • the execution order of the multiple instructions to be executed can be arranged according to the receiving time, priority level, etc. of the instructions to be executed to obtain an instruction queue, so that the multiple instructions to be executed can be executed sequentially according to the instruction queue.
  • control module 11 may include a dependency processing submodule 114.
  • the dependency processing sub-module 114 is configured to cache the first instruction to be executed in the instruction when it is determined that the first instruction to be executed in the plurality of instructions to be executed is associated with the zeroth instruction to be executed before the first instruction to be executed.
  • the first instruction to be executed is extracted from the instruction storage submodule 111 and sent to the processing module 12.
  • the first instruction to be executed and the zeroth instruction to be executed are instructions among a plurality of instructions to be executed.
  • the correlation between the first instruction to be executed and the zeroth instruction to be executed before the first instruction to be executed includes: a first storage address interval for storing data required by the first instruction to be executed and data required for storing the zeroth instruction to be executed
  • the zeroth storage address interval has an overlapping area. Conversely, if there is no correlation between the first instruction to be executed and the zeroth instruction to be executed, it may be that the first storage address interval and the zeroth storage address interval have no overlapping area.
  • the subsequent instructions to be executed can be executed after the execution of the previous instructions to be executed is completed, so as to ensure the accuracy of the calculation result.
  • the device may further include a storage module 10.
  • the storage module 10 is used to store calculation data related to neural network operations, such as quantization parameters, data to be calculated, and quantization results.
  • the storage module may include one or more of the cache 202 and the register 201, the cache 202 may include a fast temporary storage cache, and may also include at least one NRAM (Neuron Random Access Memory). ).
  • the buffer 202 can be used to store calculation data, and the register 201 can be used to store scalars in the calculation data.
  • the cache may include a neuron cache.
  • the neuron cache which is the aforementioned neuron random access memory, can be used to store neuron data in the calculation data, and the neuron data can include neuron vector data.
  • the storage module 10 may include a data I/O unit 203 for controlling the input and output of calculation data.
  • the device may also include a direct memory access module 50, which is used to read or store data from the storage module, read or store data from external equipment/other components, to implement the storage module, and Data transfer between external devices/other components.
  • a direct memory access module 50 which is used to read or store data from the storage module, read or store data from external equipment/other components, to implement the storage module, and Data transfer between external devices/other components.
  • control module may include a parameter determination sub-module.
  • the parameter determination sub-module is used to calculate the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width.
  • the quantization parameter may include one or more of point position, scaling factor, and offset.
  • the parameter determination sub-module may include:
  • the first point position determination sub-module is used to obtain each of the to- be-quantized data according to the maximum absolute value Z 1 in each of the to-be-quantized data and the corresponding data bit width when the quantization parameter does not include an offset.
  • the first type of point location of the data is the maximum value obtained by taking the absolute value of the data in the data to be quantized.
  • the quantization parameter may not include the offset, assuming that Z 1 is the maximum value of the absolute value of the element in the data to be quantized, and the data corresponding to the data to be quantized
  • the bit width is n
  • a 1 is the maximum value that can be represented by the quantized data after the quantized data is quantized with the data bit width n
  • a 1 is A 1 needs to include Z 1
  • Z 1 must be greater than Therefore
  • the processor may calculate the position s 1 of the first type point according to the maximum absolute value Z 1 and the data bit width n in the data to be quantized. For example, the following formula (5-2) can be used to calculate the first-type point position s 1 corresponding to the data to be quantified:
  • ceil is rounded up
  • Z 1 is the maximum absolute value in the data to be quantized
  • s 1 is the position of the first type of point
  • n is the data bit width.
  • the parameter determination sub-module may include:
  • the second point position determination sub-module is used to obtain the value of each data to be quantized according to the maximum value, minimum value and the corresponding data bit width in each of the data to be quantized when the quantization parameter includes an offset.
  • the second type of point position s 2 is used to obtain the value of each data to be quantized according to the maximum value, minimum value and the corresponding data bit width in each of the data to be quantized when the quantization parameter includes an offset.
  • the maximum value Z max and the minimum value Z min in the data to be quantized can be obtained first, and then the following formula (5-3) is used to calculate according to the maximum value Z max and the minimum value Z min,
  • the absolute maximum value is obtained directly based on the maximum value and minimum value of the saved data to be quantized. Consume more resources to determine the absolute value of the quantitative data, saving the time to determine the statistical results.
  • the parameter determination sub-module may include:
  • the first maximum value determining sub-module is configured to obtain the maximum value of the quantized data according to each of the data to be quantized and the corresponding data bit width when the quantization parameter does not include an offset;
  • the first scaling factor determining sub-module is configured to obtain the first-type scaling factor f'of each data to be quantized according to the maximum absolute value of each data to be quantized and the maximum value of the quantized data.
  • the first type of scaling factor f' may include a first scaling factor f 1 and a second scaling factor f 2 .
  • the first scaling factor f 1 can be calculated in the following manner (5-5):
  • the second scaling factor f 2 can be calculated according to the following formula (5-6):
  • the parameter determination submodule may include:
  • the offset determination sub-module is used to obtain the offset of each data to be quantized according to the maximum value and the minimum value in each of the data to be quantized.
  • Fig. 3-2 shows a schematic diagram of a symmetric fixed-point number representation according to an embodiment of the present disclosure.
  • the number field of the data to be quantized is distributed with "0" as the symmetric center.
  • Z 1 is the maximum absolute value of all floating-point numbers in the number domain of the data to be quantized.
  • a 1 is the maximum floating-point number that can be represented by an n-bit fixed-point number, and the floating-point number A 1 is converted to a fixed-point number. Yes (2 n-1 -1).
  • a 1 needs to include Z 1 .
  • Fig. 3-3 shows a schematic diagram of a fixed-point number representation with an offset introduced according to an embodiment of the present disclosure. As shown in Figure 3-3. The number field of the data to be quantized is not symmetrically distributed with "0" as the center.
  • Z min is the minimum value of all floating-point numbers in the number field of the data to be quantized
  • Z max is the maximum value of all floating-point numbers in the number field of the data to be quantized
  • a 2 is the maximum value of the translated floating-point number represented by n-bit fixed-point number
  • a 2 is P is the center point between Z min ⁇ Z max , which shifts the number field of the data to be quantized as a whole, so that the number field of the data to be quantized after translation is distributed with "0" as the symmetric center to avoid data "overflow” .
  • the maximum absolute value in the number field of the data to be quantized after translation is Z 2 . It can be seen from Figure 3-3 that the offset is the horizontal distance from point "0" to point "P”, and this distance is called offset o.
  • the offset can be calculated according to the minimum value Z min and maximum value Z max according to the following formula (5-7):
  • o represents the offset
  • Z min represents the minimum value among all the elements of the data to be quantized
  • Z max represents the maximum value among all the elements of the data to be quantized.
  • the parameter determination submodule may include:
  • the second maximum value determining sub-module is configured to obtain the maximum value of the quantized data according to each of the data to be quantized and the corresponding data bit width when the quantization parameter includes an offset;
  • the first scaling factor determination sub-module is configured to obtain the second type scaling factor f" of each of the data to be quantized according to the maximum value, the minimum value and the maximum value of the quantized data in each of the data to be quantized.
  • the second type of scaling factor f" may include a third scaling factor f 3 and a fourth scaling factor f 4 .
  • a 2 is the maximum value that can be represented by the quantized data after quantizing the shifted data to be quantized with the data bit width n
  • a 2 is The maximum absolute value Z 2 in the number domain of the translated data to be quantized can be calculated according to Z max and the minimum value Z min in the data to be quantized, and then the third scaling factor f can be calculated according to the following formula (5-8) 3 :
  • the fourth scaling factor f 4 can be calculated according to the following formula (5-9):
  • the quantization parameters used are different, and the data used for quantization is different.
  • the quantization parameter may include the first-type point position s 1 .
  • the following formula (5-10) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • I x is the quantized data belonging to the first type of data
  • F x is the data to be quantized
  • round is the rounding operation performed by rounding.
  • the quantized data of the target data can be dequantized according to formula (5-11) to obtain the dequantized data of the target data
  • the quantization parameter may include a first-type point position and a first scaling factor.
  • the following formula (5-12) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • the quantized data of the target data can be inversely quantized according to formula (5-13) to obtain the inverse-quantized data of the target data
  • the quantization parameter may include a second scaling factor.
  • the following formula (5-14) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • the quantized data of the target data can be inversely quantized according to formula (5-15) to obtain the inverse quantized data of the target data
  • the quantization parameter may include an offset.
  • the following formula (5-16) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • the quantized data of the target data can be inversely quantized according to formula (5-17) to obtain the inverse quantized data of the target data
  • the quantization parameter may include the position and offset of the second type of point.
  • the following formula (5-18) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • the quantized data of the target data can be inversely quantized according to formula (5-19) to obtain the inverse quantized data of the target data
  • the quantization parameter may include the second-type scaling factor f" and the offset o.
  • the following formula (5-20) can be used to quantize the quantized data to obtain the quantized data I x :
  • the quantized data of the target data can be inversely quantized according to formula (5-21) to obtain the inverse quantized data of the target data
  • the quantization parameter may include the position of the second-type point, the second-type scaling factor, and the offset.
  • the following formula (5-22) can be used to quantify the data to be quantized to obtain the quantized data I x :
  • the quantized data of the target data can be dequantized according to formula (5-23) to obtain the dequantized data of the target data
  • rounding operations can also be used, for example, rounding operations such as rounding up, rounding down, and rounding to zero may be used to replace the rounding operation round in the above formula. It is understandable that, in the case of a certain data bit width, in the quantized data obtained by quantization according to the point position, the more digits after the decimal point, the greater the quantization accuracy of the quantized data.
  • control module may also determine the quantization parameter corresponding to each type of data to be quantized in the layer to be quantized by searching for the corresponding relationship between the data to be quantized and the quantization parameter.
  • the quantization parameter corresponding to each type of data to be quantized in each to-be-quantized layer may be a saved preset value.
  • a corresponding relationship between the data to be quantized and the quantization parameter can be established for the neural network.
  • the corresponding relationship can include the corresponding relationship between each type of data to be quantized and the quantization parameter in each layer to be quantized, and the corresponding relationship can be saved in each layer for shared access Storage space. It is also possible to establish multiple correspondences between the data to be quantized and the quantization parameters for the neural network, and each layer to be quantized corresponds to one of the correspondences.
  • the corresponding relationship of each layer can be stored in the storage space exclusive to this layer, or the corresponding relationship of each layer can be stored in the storage space that can be shared and accessed by each layer.
  • the correspondence between the data to be quantized and the quantization parameter may include correspondences between multiple data to be quantized and multiple quantization parameters corresponding thereto.
  • the corresponding relationship A between the data to be quantized and the quantization parameter may include two data to be quantized: the neuron and the weight of the layer 1 to be quantized.
  • the neuron corresponds to the point position 1, the scaling factor 1, and the offset 1.
  • Parameter and weight correspond to two quantization parameters of point position 2 and offset 2.
  • the present disclosure does not limit the specific format of the corresponding relationship between the quantized data and the quantized parameter.
  • the quantization parameter corresponding to each type of data to be quantized in the layer to be quantized can be determined by searching for the corresponding relationship between the data to be quantized and the quantization parameter.
  • Corresponding quantization parameters can be preset for each layer to be quantized, and the corresponding quantization parameters can be stored for use after searching for the layer to be quantized.
  • the method of acquiring the quantization parameter in this embodiment is simple and convenient.
  • control module may further include: a first quantization error determining sub-module, an adjusting bit width determining sub-module, and an adjusting quantization parameter determining sub-module.
  • the first quantization error determination sub-module is configured to determine the quantization error corresponding to each of the data to be quantized according to the data to be quantized and the quantized data corresponding to each of the data to be quantized.
  • the quantization error of the data to be quantized can be determined according to the error between the quantized data corresponding to the data to be quantized and the data to be quantized. You can use the set error calculation method, such as the standard deviation calculation method, the root mean square error calculation method, etc., to calculate the quantization error of the data to be quantified.
  • F i is the corresponding floating point value to be quantized
  • i is the subscript of the data in the data to be quantized.
  • I is the dequantized data corresponding to the floating point value.
  • the quantization error diff bit can also be determined according to the formula (5-25) according to the quantization interval, the number of quantized data, and the corresponding data before quantization.
  • C is the corresponding quantization interval during quantization
  • m is the number of quantized data obtained after quantization
  • F i is the floating point value corresponding to the to-be-quantized data
  • i is the subscript of the data in the to-be-quantized data.
  • the quantization error diff bit can also be determined according to the formula (5-26) according to the quantized data and the corresponding inverse quantized data.
  • F i is the corresponding floating point value to be quantized
  • i is the subscript of the data in the data set to be quantized.
  • I is the dequantized data corresponding to the floating point value.
  • the adjustment bit width determination sub-module is configured to adjust the data bit width corresponding to each data to be quantized according to the quantization error and error threshold corresponding to each data to be quantized to obtain the adjustment bit width corresponding to each data to be quantized.
  • the error threshold can be determined based on empirical values, and the error threshold can be used to indicate the expected value of the quantization error.
  • the data bit width corresponding to the number to be quantized can be adjusted to obtain the adjustment bit width corresponding to the data to be quantized.
  • the data bit width can be adjusted to a longer bit width or a shorter bit width to increase or decrease the quantization accuracy.
  • the error threshold can be determined according to the maximum acceptable error. When the quantization error is greater than the error threshold, it means that the quantization accuracy cannot meet expectations, and the data bit width needs to be adjusted to a longer bit width. A smaller error threshold can also be determined according to the higher quantization accuracy. When the quantization error is less than the error threshold, it means that the quantization accuracy is higher and the operating efficiency of the neural network will be affected. The data bit width can be appropriately adjusted to be shorter. The bit width of, in order to appropriately reduce the quantization accuracy and improve the operating efficiency of the neural network.
  • the data bit width can be adjusted according to a fixed number of bits, or the data bit width can be adjusted according to a variable adjustment step according to the difference between the quantization error and the error threshold. This disclosure does not limit this.
  • the adjustment quantization parameter determination sub-module is used to update the data bit width corresponding to each of the data to be quantized to the corresponding adjustment bit width, and calculate the corresponding adjusted quantization parameter according to each of the data to be quantized and the corresponding adjustment bit width, So that each of the data to be quantized is quantized according to the corresponding adjusted quantization parameter.
  • the data bit width corresponding to the data to be quantized can be updated to the adjustment bit width. For example, if the data bit width before the update of the data to be quantized is 8 bits, and the adjustment bit width is 12 bits, the data bit width corresponding to the data to be quantized after the update is 12 bits.
  • the adjusted quantization parameter corresponding to the data to be quantized can be calculated according to the adjustment bit width and the data to be quantized.
  • the to-be-quantized data can be re-quantized according to the adjusted quantization parameter corresponding to the to-be-quantized data to obtain quantized data with higher or lower quantization accuracy, so that the to-be-quantized layer achieves a balance between quantization accuracy and processing efficiency.
  • the data to be quantified between the layers can be considered to have a certain relevance.
  • the adjusted quantization parameter of the layer to be quantized may be used as the adjusted quantization parameter of one or more subsequent layers for quantizing the data to be quantized in one or more subsequent layers of the to-be-quantized layer.
  • the adjusted quantization parameters obtained in the current iteration of the layer to be quantized can also be used to quantize the layer to be quantized in subsequent iterations.
  • control module is further configured to adopt the quantization parameter of the layer to be quantized in one or more layers after the layer to be quantized.
  • the neural network performs quantization according to the adjusted quantization parameter, which may include re-quantizing the to-be-quantized data using the adjusted quantization parameter only in the to-be-quantized layer, and use the re-obtained quantized data in the calculation of the to-be-quantized layer. It can also include not using the adjusted quantization parameter to re-quantize the data to be quantized in the layer to be quantized, and using the adjusted quantization parameter in one or more subsequent layers of the layer to be quantized for quantization, and/or use the quantized parameter in the layer to be quantized in subsequent iterations Adjust the quantization parameter for quantization.
  • the adjusted quantization parameter may include re-quantizing the to-be-quantized data using the adjusted quantization parameter only in the to-be-quantized layer, and use the re-obtained quantized data in the calculation of the to-be-quantized layer. It can also include not using the adjusted quantization parameter to re-quantize the data to be quantized in the layer to be quantized, and using
  • It may also include using the adjusted quantization parameter to re-quantize in the layer to be quantized, and use the retrieved quantized data in the operation of the layer to be quantized, and use the adjusted quantization parameter to perform quantization in one or more subsequent layers of the layer to be quantized, and /Or in subsequent iterations, quantization is performed by adjusting the quantization parameter in the layer to be quantized.
  • This disclosure does not limit this.
  • the data bit width is adjusted according to the error between the data to be quantized and the quantized data corresponding to the data to be quantized, and the adjusted quantization parameter is calculated according to the adjusted data bit width.
  • different adjusted quantization parameters can be obtained to achieve different quantization requirements such as improving quantization accuracy or improving operation efficiency.
  • the adjusted quantization parameters calculated according to the data to be quantified and the quantized data of the data to be quantified can also be more in line with the data characteristics of the data to be quantized, and achieve a quantization result that more in line with the needs of the data to be quantized. It is between quantization accuracy and processing efficiency. Achieve a better balance.
  • the adjusting bit width determining sub-module may include a first adjusting bit width determining sub-module.
  • the first adjustment bit width determination sub-module is configured to increase the corresponding data bit width when the quantization error is greater than a first error threshold to obtain the corresponding adjustment bit width.
  • the first error threshold may be determined according to the maximum quantization error that can be accepted.
  • the quantization error can be compared with the first error threshold. When the quantization error is greater than the first error threshold, it can be considered that the quantization error is already unacceptable.
  • the quantization accuracy of the data to be quantized can be improved by increasing the data bit width corresponding to the data to be quantized.
  • the data bit width corresponding to the data to be quantized can be increased according to a fixed adjustment step to obtain the adjustment bit width.
  • the fixed adjustment step size can be N bits, and N is a positive integer. Each time you adjust the data bit width, you can increase N bits.
  • the data bit width after each increase the original data bit width + N bits.
  • the data bit width corresponding to the data to be quantized can be increased according to the variable adjustment step length to obtain the adjustment bit width. For example, when the difference between the quantization error and the error threshold is greater than the first threshold, the data bit width can be adjusted according to the adjustment step M1, and when the difference between the quantization error and the error threshold is less than the first threshold, the data can be adjusted according to the adjustment step M1.
  • the step size M2 adjusts the data bit width, where the first threshold is greater than the second threshold, and M1 is greater than M2.
  • the variable adjustment step length can be determined according to requirements. The present disclosure does not limit the adjustment step length of the data bit width and whether the adjustment step length is variable.
  • the adjusted quantization parameter can be obtained by calculating the data to be quantized according to the adjusted bit width.
  • the quantized data obtained by using the adjusted quantization parameter to requantize the data to be quantized has higher quantization accuracy than the quantized data obtained by using the quantized parameter before adjustment.
  • control module may further include a first adjusted quantization error sub-module and a first adjusted bit width cycle determination module.
  • the first adjusted quantization error sub-module is configured to calculate the adjusted quantization error of each of the data to be quantized according to each of the data to be quantized and the corresponding adjustment bit width;
  • the first adjustment bit width cyclic determination module is configured to continue to increase the corresponding adjustment bit width according to the adjusted quantization error and the first error threshold, until the adjusted quantization error is less than or equal to the first error threshold.
  • An error threshold is configured to continue to increase the corresponding adjustment bit width according to the adjusted quantization error and the first error threshold, until the adjusted quantization error is less than or equal to the first error threshold.
  • the bit width When increasing the data bit width corresponding to the data to be quantized according to the quantization error, adjust the bit width once to obtain the adjusted bit width, calculate the adjusted quantization parameter according to the adjusted bit width, and quantize the data to be quantized according to the adjusted quantization parameter to obtain the adjusted
  • the quantized data is calculated based on the adjusted quantized data and the to-be-quantized data to obtain the adjusted quantization error of the to-be-quantized data.
  • the adjusted quantization error may still be greater than the first error threshold, that is, the data bit width adjusted once may not meet the adjustment purpose.
  • the adjusted quantization error is still greater than the first error threshold, you can continue to adjust the adjusted data bit width, that is, increase the data bit width corresponding to the data to be quantized multiple times, until the adjusted bit width and the to-be quantized data are finally obtained.
  • the adjusted quantization error obtained from the data is less than the first error threshold.
  • the adjustment step length that is increased multiple times may be a fixed adjustment step length or a variable adjustment step length.
  • the final data bit width original data bit width+B*N bits, where N is the fixed adjustment step length for each increase, and B is the number of times the data bit width is increased.
  • the final data bit width original data bit width+M1+M2+...+Mm, where M1, M2...Mm are the variable adjustment step lengths that are increased each time.
  • the data bit width corresponding to the data to be quantized is increased to obtain the adjustment bit width corresponding to the data to be quantized.
  • the data bit width can be increased by setting the first error threshold and adjusting the step size, so that the adjusted data bit width can meet the quantization requirement.
  • the data bit width can also be adjusted multiple times. The setting of the first error threshold and the adjustment step length enables the quantization parameters to be flexibly adjusted according to the quantization requirements to meet different quantization requirements, so that the quantization accuracy can be adaptively adjusted according to its own data characteristics.
  • the adjusting bit width determining sub-module may further include a second adjusting bit width determining sub-module.
  • the second adjustment bit width determination submodule is configured to increase the corresponding data bit width when the quantization error is less than a second error threshold to obtain the corresponding adjustment bit width, and the second error threshold is less than the first error threshold.
  • the second error threshold can be determined according to the acceptable quantization error and the expected operating efficiency of the neural network.
  • the quantization error can be compared with a second error threshold. When the quantization error is less than the second error threshold, it can be considered that the quantization error exceeds expectations, but the operating efficiency is too low to be acceptable.
  • the quantization accuracy can be reduced to improve the operating efficiency of the neural network, and the quantization accuracy of the data to be quantized can be reduced by reducing the data bit width corresponding to the data to be quantized.
  • the data bit width corresponding to the data to be quantized can be reduced according to a fixed adjustment step length to obtain the adjustment bit width.
  • the data bit width corresponding to the data to be quantized can be reduced according to the variable adjustment step length to obtain the adjustment bit width. For example, when the difference between the quantization error and the error threshold is greater than the first threshold, the data bit width can be adjusted according to the adjustment step M1, and when the difference between the quantization error and the error threshold is less than the first threshold, the data can be adjusted according to the adjustment step M1.
  • the step size M2 adjusts the data bit width, where the first threshold is greater than the second threshold, and M1 is greater than M2.
  • the variable adjustment step length can be determined according to requirements. The present disclosure does not limit the adjustment step length of the data bit width and whether the adjustment step length is variable.
  • the adjusted quantization parameter can be calculated from the data to be quantized according to the adjustment bit width, and the quantized data obtained after re-quantization of the quantized data using the adjusted quantization parameter is higher than the quantization accuracy of the quantized data obtained by using the quantized parameter before adjustment. Lower.
  • control module may further include a second adjusted quantization error sub-module and a second adjusted bit width cycle determination sub-module.
  • the second adjusted quantization error sub-module is configured to calculate the adjusted quantization error of the to-be-quantized data according to the adjustment bit width and the to-be-quantized data;
  • the second adjustment bit width cyclic determination sub-module is configured to continue to reduce the adjustment bit width according to the adjusted quantization error and the second error threshold, until the adjustment calculated according to the adjustment bit width and the data to be quantized The subsequent quantization error is greater than or equal to the second error threshold.
  • the bit width When increasing the data bit width corresponding to the data to be quantized according to the quantization error, adjust the bit width once to obtain the adjusted bit width, calculate the adjusted quantization parameter according to the adjusted bit width, and quantize the data to be quantized according to the adjusted quantization parameter to obtain the adjusted
  • the quantized data is calculated based on the adjusted quantized data and the to-be-quantized data to obtain the adjusted quantization error of the to-be-quantized data.
  • the adjusted quantization error may still be less than the second error threshold, that is, the data bit width adjusted once may not meet the adjustment purpose.
  • the adjusted quantization error is still less than the second error threshold, you can continue to adjust the adjusted data bit width, that is, reduce the data bit width corresponding to the data to be quantized multiple times, until the adjusted bit width and the to-be quantized data are finally obtained.
  • the adjusted quantization error obtained from the data is greater than the second error threshold.
  • the adjustment step length reduced multiple times may be a fixed adjustment step length or a variable adjustment step length.
  • the final data bit width original data bit width-B*N bits, where N is the fixed adjustment step length for each increase, and B is the number of times the data bit width is increased.
  • the final data bit width the original data bit width-M1-M2-...-Mm, where M1, M2...Mm are the variable adjustment step lengths that are reduced each time.
  • the data bit width corresponding to the data to be quantized is reduced to obtain the adjustment bit width corresponding to the data to be quantized.
  • the data bit width can be reduced by setting the second error threshold and adjusting the step size, so that the adjusted data bit width can meet the quantization requirement.
  • the data bit width can also be adjusted multiple times. The setting of the second error threshold and the adjustment step length enables the quantization parameters to be flexibly and adaptively adjusted according to the quantization requirements to meet different quantization requirements, so that the quantization accuracy is adjustable, and a balance is achieved between the quantization accuracy and the operating efficiency of the neural network .
  • control module is further configured to increase the data bit width corresponding to the data to be quantized when the quantization error is greater than the first error threshold, and when the quantization error is less than the second error threshold.
  • the error threshold is used, the data bit width corresponding to the data to be quantized is reduced, and the adjustment bit width corresponding to the data to be quantized is obtained.
  • the first error threshold is used to indicate that the quantization accuracy is too low, which can increase the number of bits in the data bit width
  • the second error threshold is used to indicate that the quantization accuracy is too high, which can reduce the data bit width. Digits.
  • the first error threshold is greater than the second error threshold.
  • the quantization error of the data to be quantized can be compared with the two error thresholds at the same time. When the quantization error is greater than the first error threshold, the number of bits of the data bit width is increased. When the quantization error is less than the first error threshold, For the second error threshold, reduce the number of digits of the data bit width. When the quantization error is between the first error threshold and the second error threshold, the data bit width can remain unchanged.
  • the data bit width can be increased or decreased according to the comparison result, and the first error threshold and the second error threshold can be used for more flexible adjustment.
  • Data bit width This makes the adjustment result of the data bit width more in line with quantitative requirements.
  • control module may further include a first data variation amplitude determination sub-module and a target iteration interval determination sub-module.
  • the first data change range determination submodule is used to obtain the data change range of the data to be quantified in the current iteration and the historical iteration, and the historical iteration is the iteration before the current iteration;
  • the target iteration interval determination sub-module is configured to determine the target iteration interval corresponding to the to-be-quantized data according to the data change range of the to-be-quantized data, so that the to-be-quantized layer updates the to-be-quantized layer according to the target iteration interval.
  • the quantization parameter of the data, and the target iteration interval includes at least one iteration.
  • the fine-tuning phase and/or training phase of the neural network operation includes multiple iterations.
  • Each layer to be quantized in the neural network performs one forward operation and one reverse operation, and after updating the weight of the quantized layer, an iteration is completed.
  • the data to be quantized in the layer to be quantized and/or the quantized data corresponding to the quantized data can be used to measure whether the data to be quantized and/or the quantized data in different iterations can be the same.
  • the quantization parameter is quantized. If the data change range of the data to be quantified in the current iteration and the historical iteration is small, for example, less than the set range change threshold, the same quantization parameter can be used in multiple iterations with smaller data change range.
  • the quantization parameter corresponding to the data to be quantized can be determined by extracting the pre-stored quantization parameter.
  • the quantization parameter can also be calculated according to the data to be quantized and the data bit width. When quantizing the data to be quantized in different iterations, it is necessary to calculate the quantization parameters separately in each iteration. If the data to be quantized in multiple iterations and/or the quantized data corresponding to the data to be quantized has a small data variation range, the same quantization parameter can be used in multiple iterations with a small data variation range, and each iteration can be directly Use the quantization parameter calculated in the first iteration instead of calculating the quantization parameter in each iteration.
  • the weight value between each iteration is constantly updated. If the data change range of the weight value of multiple iterations is small, or the quantified data corresponding to the weight value of multiple iterations The data change range of is small, and the same quantization parameter can be used to quantify the weight in multiple iterations.
  • the target iteration interval can be determined according to the data variation range of the data to be quantified.
  • the target iteration interval includes at least one iteration.
  • the same quantization parameters can be used in each iteration within the target iteration interval, that is, the iteration within the target iteration interval will no longer be updated.
  • the quantization parameter of the quantized data The neural network updates the quantization parameters of the data to be quantized according to the target iteration interval, including the iterations within the target iteration interval, and does not obtain the preset quantization parameters or calculate the quantization parameters, that is, the iterations within the target iteration interval do not update the quantization parameters.
  • the preset quantization parameter is obtained or the quantization parameter is calculated, that is, the quantization parameter is updated in the iteration outside the target iteration interval.
  • the calculated data fluctuation range the corresponding relationship between the preset data fluctuation range and the iteration interval can be searched, and the target iteration interval corresponding to the calculated data fluctuation range can be determined.
  • the corresponding relationship between the data change range and the iteration interval can be preset according to requirements. You can also calculate the target iteration interval by using the set calculation method according to the calculated data variation range.
  • the present disclosure does not limit the calculation method of the data variation range and the acquisition method of the target iteration interval.
  • the data change range of the data to be quantized in the current iteration and historical iteration is obtained, and the data change range of the data to be quantized is determined according to the data change range of the data to be quantized.
  • the target iteration interval can be determined according to the data change range of the to-be-quantized data or the quantized data corresponding to the to-be-quantized data in multiple iterations.
  • the neural network can determine whether to update the quantization parameter according to the target iteration interval.
  • the quantization parameters are not updated in the iterations within the target iteration interval to ensure the quantization accuracy.
  • multiple iterations within the target iteration interval do not update the quantization parameters, which can reduce the number of extractions or calculations of the quantization parameters, thereby improving the computational efficiency of the neural network.
  • control module may further include a first target iteration interval application sub-module.
  • the first target iteration interval application submodule is used to determine the data bit width corresponding to the iteration of the to-be-quantized data in the target iteration interval according to the data bit width of the to-be-quantized data in the current iteration, so that The neural network determines the quantization parameter according to the data bit width corresponding to the iteration of the data to be quantized within the target iteration interval.
  • the quantization parameter of the data to be quantized can be preset or calculated according to the data bit width corresponding to the data to be quantized.
  • the data bit width corresponding to the data to be quantized in different layers to be quantized, or the data bit width corresponding to the data to be quantized in the same layer to be quantized in different iterations can be adaptively adjusted according to the method in the above-mentioned embodiment of the present disclosure.
  • the iterative correspondence of the data to be quantized in the target iteration interval can be determined according to the preset data bit width of the data to be quantized in the current iteration The data bit width.
  • Each iteration within the target iteration interval may not use its own preset value.
  • the data bit width corresponding to the iteration of the data to be quantized within the target iteration interval can be determined according to the data bit width corresponding to the current iteration of the data to be quantized.
  • the data bit width can be adjusted adaptively, the data bit width can be adjusted once or adjusted multiple times.
  • the data bit width after adaptive adjustment of the data to be quantized in the current iteration can be used as the data bit width corresponding to each iteration within the target iteration interval, and the data bit width is no longer adaptively adjusted for each iteration within the target iteration interval. (Update).
  • the data bit width after adaptive adjustment can be used, or the data bit width before adaptive adjustment can be used, which is not limited in the present disclosure.
  • the data bit width can be adaptively adjusted according to the above-mentioned method of the present disclosure to obtain the data of the data to be quantized that is more in line with the current iteration.
  • the bit width can also use the calculation method of the target iteration interval in the present disclosure to calculate and use a new target iteration interval, thereby ensuring the quantization accuracy of iterations outside the target iteration interval and improving the operating efficiency of the neural network.
  • the data bit width of each iteration within the target iteration interval is the same, and each iteration can calculate the corresponding quantization parameter separately according to the same data bit width.
  • the quantization parameter may include at least one of a point position, a scaling factor, and an offset amount.
  • the quantization parameters can be calculated separately for each iteration within the target iteration interval based on the same data bit width.
  • the quantization parameters include the point position (including the first type of point position and the second type of point position), the zoom factor (including the first type of zoom factor and the second type of zoom factor) and the offset, each iteration within the target iteration interval .
  • the same data bit width can be used to calculate the respective corresponding point positions, scaling factors and offsets.
  • the corresponding quantization parameter of each iteration within the target iteration interval can be determined according to the quantization parameter of the current iteration.
  • the quantization parameters of each iteration within the target iteration interval are not calculated again based on the same data bit width, which can further improve the computational efficiency of the neural network.
  • the corresponding quantization parameter of each iteration within the target iteration interval can be determined according to all quantization parameters or part of the quantization parameters of the current iteration.
  • the quantization parameter includes the position of the second-type point, the second-type scaling factor, and the offset.
  • the data bit width of each iteration and the position of the second type point in the target iteration interval can be determined according to the data bit width of the current iteration and the position of the second type point.
  • the second-type scaling factor and offset of each iteration within the target iteration interval need to be calculated based on the same data bit width. It is also possible to determine the data bit width of each iteration within the target iteration interval, the position of the second type of point, the second type of zoom factor and the offset according to the data bit width of the current iteration, the position of the second type of point, the second type of zoom factor and the offset. Offset, the quantization parameters of each iteration within the target iteration interval do not need to be calculated.
  • the data bit width corresponding to the iteration of the data to be quantized in the target iteration interval is determined, so that the neural network is based on the data to be quantized in the target iteration interval. Iterate the corresponding data bit width to determine the quantization parameter.
  • the data bit width of each iteration in the target iteration interval is determined according to the data bit width of the current iteration. Since the data change range of the data to be quantized in each iteration within the target iteration interval meets the set conditions, the same data bit width is used for calculation
  • the obtained quantization parameters can ensure the quantization accuracy of each iteration within the target iteration interval.
  • Each iteration within the target iteration interval uses the same data bit width, which can also improve the computational efficiency of the neural network. After quantifying the neural network, a balance is reached between the accuracy of the calculation result and the calculation efficiency of the neural network.
  • control module may further include a second target iteration interval application sub-module.
  • the second target iteration interval application sub-module is used to determine the point position corresponding to the iteration of the to-be-quantized data within the target iteration interval according to the point position of the to-be-quantized data corresponding to the current iteration, the point The location includes the first-category point location and/or the second-category point location.
  • the position of the first-type point corresponding to the current iteration of the data to be quantized is determined.
  • the position of the second-type point corresponding to the current iteration of the data to be quantized is determined.
  • the point position corresponding to the iteration within the target iteration interval can be determined according to the point position corresponding to the current iteration of the data to be quantified.
  • the point position of the data to be quantized in the current iteration can be set as the point position corresponding to each iteration of the data to be quantized in the target iteration interval, or the data to be quantized can be set according to the current iteration.
  • the point position calculated from the preset data bit width is used as the point position corresponding to each iteration of the data to be quantified in the target iteration interval.
  • the point position of the data to be quantized adjusted in the current iteration can be used as the point position of the data to be quantized in each iteration within the target iteration interval.
  • the point position corresponding to the iteration of the data to be quantized in the target iteration interval is determined, and at the same time, the scaling corresponding to the data to be quantized in the current iteration is also possible.
  • Coefficient, determining the scaling factor corresponding to the iteration of the data to be quantized in the target iteration interval, and/or determining the data to be quantized in the target iteration interval according to the offset corresponding to the data to be quantized in the current iteration The offset corresponding to the iteration within.
  • the point position corresponding to the iteration of the data to be quantized in the target iteration interval is determined, and the data to be quantized in the current iteration may also be determined according to the data to be quantized in the current iteration.
  • the bit width determines the data bit width corresponding to the iteration of the data to be quantized in the target iteration interval, where the data bit width corresponding to the current iteration of the data to be quantized can be the preset data bit width of the current iteration or self Adapt to the adjusted data bit width.
  • the point position corresponding to the iteration of the data to be quantized in the target iteration interval is determined.
  • the point position of each iteration in the target iteration interval is determined according to the point position of the current iteration. Since the data change range of the data to be quantified in each iteration within the target iteration interval meets the set conditions, the same point position can be used to ensure the target The quantization accuracy of each iteration within the iteration interval. Using the same point position for each iteration within the target iteration interval can also improve the computational efficiency of the neural network. After quantifying the neural network, a balance is reached between the accuracy of the calculation result and the calculation efficiency of the neural network.
  • the first data variation range determination sub-module may include a sliding average calculation sub-module and a first data variation range determination sub-module.
  • the target iteration interval determination sub-module may include a first target iteration interval determination sub-module
  • the moving average calculation sub-module is used to calculate the points corresponding to each iteration interval of the data to be quantified according to the point position of the data to be quantified in the current iteration and the point position of the historical iteration corresponding to the current iteration determined according to the historical iteration interval A sliding average of positions, where the point positions include the first-type point positions and/or the second-type point positions;
  • the first data variation range determination sub-module is used to obtain the first sliding average value of the data to be quantified at the point position of the current iteration and the second sliding average value of the point position corresponding to the iteration in the previous iteration interval.
  • the first target iteration interval determination sub-module is configured to determine the target iteration interval corresponding to the data to be quantified according to the magnitude of the first data change, so that the neural network updates the data to be quantified according to the target iteration interval The quantization parameter.
  • the first type of the data to be quantified corresponding to each iteration interval is calculated
  • the historical iteration corresponding to the current iteration determined according to the historical iteration interval may be a historical iteration for calculating the target iteration interval.
  • the correspondence between the current iteration and the corresponding target iteration interval may include:
  • the target iteration interval can be counted from the current iteration, and the target iteration interval can be recalculated at the next iteration after the end of the target iteration interval corresponding to the current iteration.
  • the current iteration is the 100th generation
  • the target iteration interval is 3
  • the iterations within the target iteration interval include: the 100th, 101st, and 102nd generations.
  • the historical iteration corresponding to the current iteration determined according to the historical iteration interval is 100 generations.
  • the target iteration interval can be counted from the next iteration of the current iteration, and the target iteration interval can be recalculated from the last iteration within the target iteration interval.
  • the current iteration is the 100th generation
  • the target iteration interval is 3
  • the iterations within the target iteration interval include: the 101st, 102nd, and 103rd generations.
  • And use 104 as the new calculation to get the first iteration within the target iteration interval.
  • the historical iteration corresponding to the current iteration determined according to the historical iteration interval is 100 generations.
  • the target iteration interval can be counted from the next iteration of the current iteration, and the target iteration interval can be recalculated at the next iteration after the end of the target iteration interval.
  • the current iteration is the 100th generation
  • the target iteration interval is 3
  • the iterations within the target iteration interval include: the 101st, 102nd, and 103rd generations.
  • the target iteration interval corresponding to the 104th generation can be calculated in the 104th generation.
  • And use 105 as the new calculation to get the first iteration within the target iteration interval.
  • the historical iteration corresponding to the current iteration determined according to the historical iteration interval is 100 generations.
  • the target iteration interval may be counted from the Nth iteration after the current iteration, and N is greater than 1, which is not limited in the present disclosure.
  • the calculated moving average of the point positions of the data to be quantized corresponding to each iteration interval includes the first moving average of the point positions of the data to be quantized in the current iteration, and corresponds to the data to be quantized in the previous iteration interval.
  • Formula (5-27) can be used to calculate the first moving average m (t) of the corresponding point position of the current iteration:
  • t is the current iteration
  • t-1 is the historical iteration determined according to the previous iteration interval
  • m (t-1) is the second moving average of the historical iteration determined according to the previous iteration interval
  • s (t) is the point position of the current iteration, which can be the first type point position or the second type point position.
  • is the first parameter.
  • the first parameter may be a hyperparameter.
  • the sliding of the point position of the data to be quantified corresponding to each iteration interval is calculated.
  • Average According to the first moving average of the point position of the current iteration of the data to be quantified, and the second moving average of the point position of the corresponding iteration in the previous iteration interval, the first data variation range is obtained. Determine the target iteration interval corresponding to the data to be quantized according to the first data variation range, so that the neural network updates the quantization parameter of the data to be quantized according to the target iteration interval.
  • the target iteration interval can follow the change trend of the data point to be quantified, and the calculated target iteration interval can be based on the data point to be quantified.
  • the changing trend of the location changes. Since the quantization parameter is determined according to the target iteration interval, the quantization data obtained by quantization according to the quantization parameter can be more in line with the changing trend of the point position of the data to be quantized, and the operation efficiency of the neural network is improved while ensuring the quantization accuracy.
  • the first data variation amplitude determination sub-module may include a first amplitude determination sub-module.
  • the first amplitude determination sub-module is configured to calculate the difference between the first sliding average and the second sliding average; and determine the absolute value of the difference as the first data variation amplitude.
  • the first data change range diff update1 can be calculated using formula (5-28):
  • the target iteration interval corresponding to the data to be quantized may be determined according to the first data variation range, so that the neural network updates the quantization parameter of the data to be quantized according to the target iteration interval.
  • the target iteration interval I can be calculated according to formula (5-29):
  • is the second parameter
  • is the third parameter
  • the second parameter and the third parameter may be hyperparameters.
  • the first data change range can be used to measure the change trend of the point position.
  • the larger the first data change range it means that the numerical range of the quantized data changes drastically.
  • a shorter target iteration interval is required when updating the quantized parameters. I.
  • the difference between the first moving average value and the second moving average value is calculated; the absolute value of the difference value is determined as the first data variation range. According to the difference between the moving average values, an accurate first data variation range can be obtained.
  • control module may further include a second data variation range determination sub-module
  • target iteration interval determination sub-module may include a second target iteration interval determination sub-module
  • the second data variation range determination sub-module is configured to obtain the second data variation range according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration;
  • the second target iteration interval determination sub-module is configured to determine the target iteration interval corresponding to the to-be-quantized data according to the first data change range and the second data change range of the to-be-quantized data, so that the neural network
  • the quantization parameter of the data to be quantized is updated according to the target iteration interval.
  • the second data variation range may be obtained according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration. It is also possible to obtain the second data variation range according to the data to be quantized and the inverse quantization data corresponding to the data to be quantized in the current iteration.
  • the second data variation amplitude diff bit between the data to be quantized and the dequantized data corresponding to the data to be quantized in the current iteration can be calculated according to formula (5-30). It is also possible to use other error calculation methods to calculate the second data variation range diff bit between the data to be quantized and the dequantized data. This disclosure does not limit this.
  • z i is the data to be quantized
  • z i (n) is the inverse quantized data corresponding to the data to be quantized.
  • the second data change range can be used to measure the change trend of the data bit width corresponding to the data to be quantified. The larger the second data change range is, the more likely the data to be quantized need to update the corresponding data bit width, and the interval is required. The shorter the iteration is to update, the greater the second data change range, and the smaller the target iteration interval is required.
  • the second data variation range is obtained according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration.
  • the quantization parameter of the data is obtained according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration.
  • the second data change range can be used to measure the change demand of the data bit width
  • the target iteration interval calculated according to the first data change range and the second data change range can track the changes of the point position and the data bit width at the same time, and the target iteration The interval can also be more in line with the data quantification requirements of the data to be quantified.
  • obtaining the second data variation range according to the data to be quantized and the quantized data corresponding to the data to be quantized in the current iteration may include:
  • the square of the error is determined as the second data variation range.
  • the second data change range diff update2 can be calculated using formula (5-31):
  • is the fourth parameter
  • the fourth parameter may be a hyperparameter
  • different quantization parameters can be obtained by using different data bit widths, and then different quantized data can be obtained, and different second data variation ranges can be generated.
  • the second data change range can be used to measure the change trend of the data bit width. The larger the second data change range, the shorter the target iteration interval is needed to update the data bit width more frequently, that is, the target iteration interval needs to be smaller.
  • the second target iteration interval determination sub-module may include an interval determination sub-module.
  • the interval determination sub-module is configured to determine the target iteration interval corresponding to the data to be quantified according to the maximum value of the first data change range and the second data change range.
  • the target iteration interval can be calculated according to formula (5-32):
  • is the second parameter
  • is the third parameter
  • the second parameter and the third parameter may be hyperparameters.
  • the target iteration interval obtained by using the first data change range and the second data change range can simultaneously measure the change trend of the data bit width and the point position.
  • the change trend of one of the two is larger, Then the target iteration interval can be changed accordingly.
  • the target iteration interval can simultaneously track changes in data bit width and point position and make corresponding adjustments.
  • the quantization parameter updated according to the target iteration interval can be more in line with the change trend of the target data, and finally the quantized data obtained according to the quantization parameter can be more in line with the quantization requirement.
  • the first data change amplitude determination submodule may include a second data change amplitude determination submodule.
  • the second data change range determination submodule is configured to obtain the data change range of the data to be quantified in the current iteration and the historical iteration when the current iteration is outside the update period, and the update period includes at least one iteration.
  • the data to be quantified varies greatly. If the target iteration interval is calculated in multiple iterations at the beginning of training or fine-tuning, the calculated target iteration interval may lose its usefulness. According to the preset update period, the target iteration interval is not calculated for each iteration within the update period, and the target iteration interval is not applicable so that multiple iterations use the same data bit width or point position.
  • the iteration progresses beyond the update period, that is, when the current iteration is outside the update period, obtain the data change range of the data to be quantified in the current iteration and historical iterations, and determine the data change range of the data to be quantified according to the data change range of the data to be quantified.
  • the target iteration interval corresponding to the data, so that the neural network updates the quantization parameter of the data to be quantized according to the target iteration interval. For example, if the preset update period is 100 generations, the target iteration interval is not calculated from the first generation to the 100th generation.
  • the iteration progresses to the 101st generation, that is, when the current iteration is the 101st generation, the current iteration is outside the update cycle.
  • the target iteration interval corresponding to the data to be quantized in the 101st generation, and the calculated target iteration interval is used in the 101st generation or a preset number of iterations with the 101st interval.
  • the update cycle can be counted from a preset generation number, for example, multiple iterations in the update cycle can be counted from the first generation, or multiple iterations in the update cycle can be counted from the Nth generation, which is not limited in the present disclosure.
  • the target iteration interval is calculated and used when the iteration progresses beyond the update period. It can avoid the problem that the target iteration interval is of little significance due to the large fluctuation of the data to be quantified in the initial stage of the training process or fine-tuning process of the neural network operation.
  • the neural network can be further improved when the target iteration interval is used. Operating efficiency.
  • control module may further include a periodic interval determining sub-module, a first periodic interval applying sub-module, and a second periodic interval applying sub-module.
  • the cycle interval determination sub-module is used to determine when the current iteration is within a preset cycle, according to the current iteration, the iteration corresponding to the current iteration in the next cycle of the preset cycle, and the iteration interval corresponding to the current iteration Periodic interval
  • the first period interval application sub-module is configured to determine the data bit width of the data to be quantized in the iteration within the period interval according to the data bit width corresponding to the data to be quantized in the current iteration; or
  • the second periodic interval application sub-module is configured to determine the point position of the data to be quantized in the iteration within the periodic interval according to the point position of the data to be quantized in the current iteration.
  • each cycle can include multiple iterations.
  • the data used for neural network calculations is completely calculated once into a cycle.
  • the weight changes of the neural network tend to stabilize.
  • the neurons, weights, bias, and gradient waiting for quantified data tend to stabilize.
  • the data to be quantized stabilizes, the data bit width and quantization parameters of the data to be quantized also stabilize.
  • the fine-tuning process after the fine-tuning is stable, the data bit width and quantization parameters of the data to be quantized also tend to be stable.
  • the preset period can be determined according to the period of training stability or fine-tuning stability.
  • the period after the period where the training is stable or the fine-tuning stable period can be determined as the preset period.
  • the stable training period is the Mth period, and the periods after the Mth period can be used as the preset period.
  • a target iteration interval can be calculated every interval, and the data bit width or quantization parameter can be adjusted once according to the calculated target iteration interval to reduce the update times of the data bit width or quantization parameter and improve the performance of the neural network. operating efficiency.
  • the preset period is the period after the Mth period.
  • the target iteration interval calculated according to the Pth iteration in the Mth cycle ends at the Qth iteration in the M+1th cycle.
  • the target iteration interval I m+1 corresponding to it is calculated according to the Q m+ 1th iteration in the M+1th cycle.
  • a period interval can be set. After the training or fine-tuning of the neural network operation reaches a stable level, the quantization parameters such as the data bit width or the point position are updated every period according to the period interval.
  • the period interval can reduce the update times of the data bit width or point position after the training is stable or the fine-tuning is stable, while ensuring the quantization accuracy, it can improve the operating efficiency of the neural network.
  • Figures 3-4 show a flowchart of a neural network quantification method according to an embodiment of the present disclosure. As shown in Figure 3-4, the method is applied to a neural network quantization device.
  • the device includes a control module and a processing module.
  • the processing module includes a first operation submodule, and the first operation submodule includes a main operation submodule.
  • slave operation sub-module the method includes step S5-11 and step S5-12.
  • step S5-11 the control module is used to determine a plurality of data to be quantized from the target data of the neural network, and the quantization result of the target data is obtained according to the quantized data corresponding to each of the data to be quantized.
  • the quantized data of the data to be quantized is obtained by quantization using a corresponding quantization parameter, and the quantization parameter includes a point position;
  • step S5-12 the first operation submodule is used to perform operations related to the quantization result to obtain the operation result, where step S5-12 includes:
  • the main operation sub-module is used to perform operations on the intermediate result and the quantized result data other than the first data to obtain an operation result.
  • the quantization parameter further includes an offset and/or a scaling factor
  • the quantization result further includes a second type of data
  • the second type of data includes data represented by dot positions The first part, and the second part of the data expressed by offset and/or scaling factor
  • the first data also includes the first part of the second type of data in the quantization result.
  • the processing module further includes a data conversion sub-module
  • the method further includes:
  • the data conversion submodule is used to perform format conversion of the data to be converted to obtain the converted data.
  • the format type of the converted data includes any one of the first type and the second type, wherein the data to be converted includes all The data in the target data that has not been subjected to quantization processing, the first data further includes the first part of the converted data of the first type and/or the converted data of the second type,
  • using the main operation sub-module to perform operations on the intermediate result and the data other than the first data in the quantization result to obtain the operation result includes:
  • the method further includes:
  • the data conversion sub-module is used to perform format conversion on the to-be-converted quantization result of the target data obtained from the quantized data corresponding to each of the to-be-quantized data to obtain the quantization result.
  • each of the data to be quantized is a subset of the target data
  • the target data is any data to be quantized in the layer to be quantized of the neural network
  • the data to be calculated includes at least one of input neurons, weights, biases, and gradients.
  • using the control module to determine multiple data to be quantified from the target data of the neural network includes at least one of the following methods:
  • the target data in the corresponding layer to be quantized is divided into one or more data to be quantized according to the determined division size.
  • the processing module further includes a second operation sub-module
  • the method further includes:
  • the second arithmetic sub-module is used to perform arithmetic processing in the device other than the arithmetic processing performed by the first arithmetic sub-module.
  • the method further includes:
  • the corresponding quantization parameter is calculated according to each of the data to be quantized and the corresponding data bit width.
  • calculating the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width includes:
  • the first-type point position of each data to be quantized is obtained according to the maximum absolute value of each data to be quantized and the corresponding data bit width.
  • calculating the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width includes:
  • the first-type scaling coefficient of each data to be quantized is obtained.
  • calculating the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width includes:
  • the position of the second type point of each data to be quantized is obtained according to the maximum value, the minimum value and the corresponding data bit width in each of the data to be quantized.
  • calculating the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width includes:
  • the quantization parameter includes an offset
  • the second-type scaling coefficient of each data to be quantized is obtained.
  • calculating the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width includes:
  • the offset of each data to be quantized is obtained.
  • the method further includes:
  • the data bit width corresponding to each of the data to be quantized is updated to the corresponding adjustment bit width, and the corresponding adjusted quantization parameter is calculated according to each of the data to be quantized and the corresponding adjustment bit width, so that each of the data to be quantized is based on The corresponding adjusted quantization parameter is quantized.
  • the data bit width corresponding to each data to be quantized is adjusted to obtain the adjustment bit width corresponding to each data to be quantized, include:
  • the corresponding data bit width is increased to obtain the corresponding adjustment bit width.
  • the method further includes:
  • the data bit width corresponding to each data to be quantized is adjusted to obtain the adjustment bit width corresponding to each data to be quantized, include:
  • the corresponding data bit width is increased to obtain the corresponding adjustment bit width, and the second error threshold is less than the first error threshold.
  • the method further includes:
  • the method further includes:
  • the target iteration interval corresponding to the data to be quantized is determined, so that the layer to be quantized updates the quantization parameter of the data to be quantized according to the target iteration interval, and the target iteration The interval includes at least one iteration.
  • the method further includes:
  • the data bit width corresponding to the iteration of the data to be quantized in the target iteration interval is determined, so that the neural network is based on the data to be quantized.
  • the data bit width corresponding to the iteration within the target iteration interval determines the quantization parameter.
  • the method further includes:
  • the point position corresponding to the current iteration of the data to be quantized is determined, and the point position includes the first-type point position and/or the first type point position.
  • the second-class point location is determined.
  • obtaining the data change range of the data to be quantified in the current iteration and the historical iteration includes:
  • the moving average of the point positions of the data to be quantified corresponding to each iteration interval is calculated.
  • the location includes the first-category point location and/or the second-category point location;
  • determining the target iteration interval corresponding to the to-be-quantized data according to the data change range of the to-be-quantized data, so that the to-be-quantized layer updates the quantization parameter of the to-be-quantized data according to the target iteration interval including:
  • obtaining the data change range of the data to be quantified in the current iteration and the historical iteration includes:
  • the absolute value of the difference is determined as the first data variation range.
  • the method further includes:
  • determining the target iteration interval corresponding to the to-be-quantized data according to the data change range of the to-be-quantized data, so that the to-be-quantized layer updates the quantization parameter of the to-be-quantized data according to the target iteration interval including:
  • obtaining the second data variation range according to the to-be-quantized data and the quantized data corresponding to the to-be-quantized data in the current iteration includes:
  • the square of the error is determined as the second data variation range.
  • determining the target iteration interval corresponding to the data to be quantized according to the first data variation amplitude and the second data variation amplitude of the data to be quantized includes:
  • obtaining the data change range of the data to be quantified in the current iteration and the historical iteration includes:
  • the data change range of the data to be quantified in the current iteration and the historical iteration is obtained, and the update period includes at least one iteration.
  • the method further includes:
  • the point position of the data to be quantized in the current iteration is determined.
  • the neural network quantization method uses corresponding quantization parameters to quantize multiple data to be quantized in the target data, and performs operations related to the quantization results through the first operation sub-module to ensure accuracy At the same time, the storage space occupied by the stored data is reduced, the accuracy and reliability of the calculation results are guaranteed, and the efficiency of the calculation can be improved, and the quantization also reduces the size of the neural network model and reduces the need to run the neural network.
  • the performance requirements of the model's terminal enable the neural network model to be applied to terminals such as mobile phones with relatively limited computing power, size, and power consumption.
  • steps in the flowcharts of FIGS. 3-4 are displayed in sequence according to the directions of the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless there is a clear description in this article, there is no strict order for the execution of these steps, and these steps can be executed in other orders. Moreover, at least part of the steps in Figure 3-4 may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. These sub-steps or stages The execution order of is not necessarily performed sequentially, but may be performed alternately or alternately with at least a part of other steps or sub-steps or stages of other steps.
  • the embodiments of the present disclosure also provide a non-volatile computer-readable storage medium on which computer program instructions are stored.
  • the computer program instructions are executed by a processor, the above-mentioned neural network data quantization processing method is realized.
  • a neural network quantization device includes a control module and a processing module, the processing module includes a first arithmetic sub-module, the first arithmetic sub-module includes a master arithmetic sub-module and a slave arithmetic sub-module,
  • the control module is configured to determine a plurality of data to be quantized from the target data of the neural network, and obtain the quantization result of the target data according to the quantization data corresponding to each of the data to be quantized, each of the data to be quantized
  • the quantized data of is obtained by quantizing the corresponding quantization parameter, and the quantization parameter includes the point position;
  • the first operation sub-module is used to perform operations related to the quantization result to obtain the operation result
  • the main operation sub-module is configured to send first data to the slave operation sub-module, and the first data includes data of the first type obtained by quantization according to the point position in the quantization result;
  • the slave operation submodule is configured to perform a multiplication operation on the received first data to obtain an intermediate result
  • the main operation sub-module is also used to perform operations on data other than the first data in the intermediate result and the quantization result to obtain an operation result.
  • the first data also includes the first part of the second type of data in the quantization result.
  • the data conversion sub-module is used to perform format conversion of the data to be converted to obtain the converted data.
  • the format type of the converted data includes any one of the first type and the second type, wherein the data to be converted includes all The data in the target data that has not been subjected to quantization processing, the first data further includes the first part of the converted data of the first type and/or the converted data of the second type,
  • the main arithmetic sub-module is also used to compare the intermediate result, data other than the first data in the quantization result, and data other than the first data in the converted data. Perform calculations to get the results of the calculations.
  • the data conversion sub-module is further configured to perform format conversion on the to-be-converted quantization result of the target data obtained according to the quantized data corresponding to each of the to-be-quantized data to obtain the quantization result.
  • each of the data to be quantified is a subset of the target data, and the target data is any one of the layers to be quantized of the neural network
  • the to-be-calculated data to be quantified, where the to-be-calculated data includes at least one of input neurons, weights, offsets, and gradients.
  • control module uses at least one of the following methods to determine a plurality of data to be quantified:
  • the target data in the corresponding layer to be quantized is divided into one or more data to be quantized according to the determined division size.
  • the second arithmetic sub-module is used to perform arithmetic processing in the device other than the arithmetic processing performed by the first arithmetic sub-module.
  • control module includes:
  • the parameter determination sub-module is configured to calculate the corresponding quantization parameter according to each of the data to be quantized and the corresponding data bit width.
  • the first point position determination sub-module when the quantization parameter does not include the offset, obtain the first value of each of the data to be quantized according to the maximum value of the absolute value in each of the data to be quantized and the corresponding data bit width. Class point location.
  • the first maximum value determination sub-module when the quantization parameter does not include an offset, obtain the maximum value of the quantized data according to each of the data to be quantized and the corresponding data bit width;
  • the first scaling factor determining sub-module obtains the first type scaling factor of each data to be quantized according to the maximum absolute value of each data to be quantized and the maximum value of the quantized data.
  • the second point position determination sub-module when the quantization parameter includes an offset, obtains the second value of each of the data to be quantized according to the maximum value, the minimum value and the corresponding data bit width in each of the data to be quantized Class point location.
  • the second maximum value determining sub-module when the quantization parameter includes an offset, obtain the maximum value of the quantized data according to each of the data to be quantized and the corresponding data bit width;
  • the first scaling factor determining sub-module obtains the second type scaling factor of each of the data to be quantized according to the maximum value, the minimum value and the maximum value of the quantized data in each of the data to be quantized.
  • the offset determining sub-module obtains the offset of each data to be quantized according to the maximum value and the minimum value in each of the data to be quantized.
  • control module further includes:
  • the first quantization error determination sub-module is configured to determine the quantization error corresponding to each of the data to be quantized according to each of the data to be quantized and the quantized data corresponding to each of the data to be quantized;
  • the adjustment bit width determination submodule is configured to adjust the data bit width corresponding to each data to be quantized according to the quantization error and error threshold corresponding to each data to be quantized to obtain the adjustment bit width corresponding to each data to be quantized;
  • the adjustment quantization parameter determination sub-module is used to update the data bit width corresponding to each of the data to be quantized to the corresponding adjustment bit width, and calculate the corresponding adjusted quantization parameter according to each of the data to be quantized and the corresponding adjustment bit width, So that each of the data to be quantized is quantized according to the corresponding adjusted quantization parameter.
  • the first adjustment bit width determination sub-module is configured to increase the corresponding data bit width when the quantization error is greater than a first error threshold to obtain the corresponding adjustment bit width.
  • control module further includes:
  • the first adjusted quantization error sub-module is configured to calculate the adjusted quantization error of each of the data to be quantized according to each of the data to be quantized and the corresponding adjustment bit width;
  • the first adjustment bit width cyclic determination module is configured to continue to increase the corresponding adjustment bit width according to the adjusted quantization error and the first error threshold, until the adjusted quantization error is less than or equal to the first error threshold.
  • An error threshold is configured to continue to increase the corresponding adjustment bit width according to the adjusted quantization error and the first error threshold, until the adjusted quantization error is less than or equal to the first error threshold.
  • the second adjustment bit width determination sub-module is configured to increase the corresponding data bit width when the quantization error is less than a second error threshold to obtain the corresponding adjustment bit width, and the second error threshold is less than the second error threshold.
  • the first error threshold is configured to increase the corresponding data bit width when the quantization error is less than a second error threshold to obtain the corresponding adjustment bit width, and the second error threshold is less than the second error threshold.
  • control module further includes:
  • the second adjusted quantization error sub-module is configured to calculate the adjusted quantization error of the to-be-quantized data according to the adjustment bit width and the to-be-quantized data;
  • the second adjustment bit width cyclic determination sub-module is configured to continue to reduce the adjustment bit width according to the adjusted quantization error and the second error threshold, until the adjustment calculated according to the adjustment bit width and the data to be quantized The subsequent quantization error is greater than or equal to the second error threshold.
  • control module further includes:
  • the first data change range determination submodule is used to obtain the data change range of the data to be quantified in the current iteration and the historical iteration, and the historical iteration is the iteration before the current iteration;

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Data Mining & Analysis (AREA)
  • General Health & Medical Sciences (AREA)
  • Biomedical Technology (AREA)
  • Biophysics (AREA)
  • Computational Linguistics (AREA)
  • Life Sciences & Earth Sciences (AREA)
  • Evolutionary Computation (AREA)
  • Artificial Intelligence (AREA)
  • Molecular Biology (AREA)
  • Computing Systems (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Mathematical Physics (AREA)
  • Software Systems (AREA)
  • Health & Medical Sciences (AREA)
  • Compression, Expansion, Code Conversion, And Decoders (AREA)

Abstract

一种数据处理方法、装置、计算机设备和存储介质。所述方法包括:根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种(S4-11);将各所述待量化数据分别根据对应的量化参数进行量化,得到与各所述待量化数据对应的量化数据(S4-12);根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算(S4-13)。

Description

数据处理方法、装置、计算机设备和存储介质 技术领域
本公开涉及计算机技术领域,特别是涉及一种神经网络的数据量化方法、装置、计算机设备和存储介质。
背景技术
神经网络(neural network,NN)是一种模仿生物神经网络的结构和功能的数学模型或计算模型。神经网络通过样本数据的训练,不断修正网络权值和阈值使误差函数沿负梯度方向下降,逼近期望输出。它是一种应用较为广泛的识别分类模型,多用于函数逼近、模型识别分类、数据压缩和时间序列预测等。神经网络被应用到图像识别、语音识别、自然语言处理等领域中,然而,随着神经网络复杂度提高,数据的数据量和数据维度都在不断增大,不断增大的数据量等对运算装置的数据处理效率、存储装置的存储容量及访存效率等提出了较大的挑战。相关技术中,采用固定位宽对神经网络的运算数据进行量化,即将浮点型的运算数据转换为定点型的运算数据,以实现神经网络的运算数据的压缩。但相关技术中针对整个神经网络采用相同的量化方案,但神经网络的不同运算数据之间可能存在较大的差异,往往会导致精度较低,影响数据运算结果。
发明内容
基于此,有必要针对上述技术问题,提供一种神经网络的数据量化处理方法、装置、计算机设备和存储介质。
基于此,有必要针对上述技术问题,提供一种神经网络量化方法、装置、计算机设备和存储介质。
根据本公开的一方面,提供了一种神经网络量化方法,应用于终端,所述终端用于运行神经网络以执行目标任务,所述方法包括:
根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种;
将各所述待量化数据分别根据对应的量化参数进行量化,得到与各所述待量化数据对应的量化数据;
根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
根据本公开的另一方面,提供了一种神经网络量化装置,,应用于终端,所述终端用于运行神经网络以执行目标任务,所述装置包括:
数据确定模块,根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种;
数据量化模块,将各所述待量化数据分别根据对应的量化参数进行量化,得到与各所述待量化数据对应的量化数据;
结果确定模块,根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
根据本公开的另一方面,提供了一种人工智能芯片,其特征在于,所述芯片包括上述神经网络量化装置。
根据本公开的另一方面,提供了一种电子设备,所述电子设备包括上述人工智能芯片。
根据本公开的另一方面,提供了一种板卡,所述板卡包括:存储器件、接口装置和控制器件以及上述人工智能芯片;
其中,所述人工智能芯片与所述存储器件、所述控制器件以及所述接口装置分别连接;
所述存储器件,用于存储数据;
所述接口装置,用于实现所述人工智能芯片与外部设备之间的数据传输;
所述控制器件,用于对所述人工智能芯片的状态进行监控。
根据本公开的另一方面,提供了一种非易失性计算机可读存储介质,其上存储有计算机程序指令,所述计算机程序指令被处理器执行时实现上述神经网络量化方法。
本公开实施例所提供的神经网络量化方法、装置、计算机设备和存储介质,该方法包括:根据所述目标任务和/或所述终端的精度需求,从所述待量化层的目标数据中确定多个待量化数据,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种;将各所述待量化数据分别根据对应的量化参数进行量化,得到与各所述待量化数据对应的量化数据;根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。本公开实施例所提供的神经网络量化方法、装置、计算机设备和存储介质,根据目标任务和/或终端的精度需求从目标数据中确定多个待量化数据,利用对应的量化参数对待量化数据进行量化,在保证精度的同时,减小了存储数据所占用的存储空间,保证了运算结果的准确性和可靠性,且能够提高运算的效率,且量化同样缩减了神经网络模型的大小,降低了对运行该神经网络模型的终端的性能要求。
基于此,有必要针对上述技术问题,提供一种神经网络量化方法、装置、计算机设备和存储介质。
根据本公开的一方面,提供了一种神经网络量化装置,所述装置包括控制模块和处理模块,所述处理模块包括第一运算子模块,所述第一运算子模块包括主运算子模块和从运算子模块,
所述控制模块,用于从神经网络的目标数据中确定出多个待量化数据,并根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,各所述待量化数据的量化数据是利用对应的量化参数进行量化得到的,所述量化参数包括点位置;
所述第一运算子模块,用于进行与所述量化结果相关的运算,得到运算结果,
其中,所述主运算子模块,用于将第一数据发送至所述从运算子模块,所述第一数据包括所述量化结果中根据所述点位置量化得到的第一类型的数据;
所述从运算子模块,用于对接收到的所述第一数据进行乘法运算,得到中间结果;
所述主运算子模块,还用于对所述中间结果和所述量化结果中的除所述第一数据之外的数据进行运算,得到运算结果。
根据本公开的另一方面,提供了一种神经网络量化方法,所述方法应用于神经网络量化装置,所述装置包括控制模块和处理模块,所述处理模块包括第一运算子模块,所述第一运算子模块包括主运算子模块和从运算子模块,所述方法包括:
利用所述控制模块从神经网络的目标数据中确定出多个待量化数据,并根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,各所述待量化数据的量化数据是利用对应的量化参数进行量化得到的,所述量化参数包括点位置;
利用所述第一运算子模块进行与所述量化结果相关的运算,得到运算结果,
其中,利用所述第一运算子模块进行与所述量化结果相关的运算,得到运算结果,包括:
利用所述主运算子模块将第一数据发送至所述从运算子模块,所述第一数据包括所述量化结果中根据所述点位置量化得到的第一类型的数据;
利用所述从运算子模块对接收到的所述第一数据进行乘法运算,得到中间结果;
利用所述主运算子模块对所述中间结果和所述量化结果中的除所述第一数据之外的数据进行运算,得到运算结果。
根据本公开的另一方面,提供了一种人工智能芯片,其特征在于,所述芯片包括上述神经网络量化装置。
根据本公开的另一方面,提供了一种电子设备,所述电子设备包括上述人工智能芯片。
根据本公开的另一方面,提供了一种板卡,所述板卡包括:存储器件、接口装置和控制器件以及上述人工智能芯片;
其中,所述人工智能芯片与所述存储器件、所述控制器件以及所述接口装置分别连接;
所述存储器件,用于存储数据;
所述接口装置,用于实现所述人工智能芯片与外部设备之间的数据传输;
所述控制器件,用于对所述人工智能芯片的状态进行监控。
根据本公开的另一方面,提供了一种非易失性计算机可读存储介质,其上存储有计算机程序指令,所述计算机程序指令被处理器执行时实现上述神经网络量化方法。
本公开实施例所提供的神经网络量化方法、装置、计算机设备和存储介质,该装置包括控制模块和处理模块,处理模块包括第一运算子模块,第一运算子模块包括主运算子模块和从运算子模块,控制模块,用于从神经网络的目标数据中确定出多个待量化数据,并根据与各待量化数据对应的量化数据得到目标数据的量化结果,各待量化数据的量化数据是利用对应的量化参数进行量化得到的,量化参数包括点位置;第一运算子模块,用于进行与量化结果相关的运算,得到运算结果,其中,主运算子模块,用于将第一数据发送至从运算子模块,第一数据包括量化结果中根据点位置量化得到的第一类型的数据;从运算子模块,用于对接收到的第一数据进行乘法运算,得到中间结果;主运算子模块,还用于对中间结果和量化结果中的除第一数据之外的数据进行运算,得到运算结果。本公开实施例所提供的神经网络量化方法、装置、计算机设备和存储介质,利用对应的量化参数对目标数据中的多个待量化数据分别进行量化,且在通过第一运算子模块执行与量化结果相关的运算,在保证精度的同时,减小了存储数据所占用的存储空间,保证了运算结果的准确性和可靠性,且能够提高运算的效率,且量化同样缩减了神经网络模型的大小,降低了对运行该神经网络模型的终端的性能要求。
基于此,有必要针对上述技术问题,提供一种神经网络量化方法、装置、计算机设备和存储介质。
根据本公开的一方面,提供了一种神经网络量化方法,对于所述神经网络中的任意待量化层,所述方法包括:
根据运行所述神经网络的终端的存储能力,从所述待量化层的目标数据中确定多个待量化数据,所述目标数据为所述待量化层中的任意一批待运算数据,所述待运算数据包括输入神经元和/或梯度中的至少一种;
采用循环处理的方式,依次根据每个待量化数据根据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据;
根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
根据本公开的另一方面,提供了一种神经网络量化装置,对于所述神经网络中的任意待量化层,所述装置包括:
数据确定模块,根据运行所述神经网络的终端的存储能力,从所述待量化层的目标数据中确定多个待量化数据,所述目标数据为所述待量化层中的任意一批待运算数据,所述待运算数据包括输入神经元和/或梯度中的至少一种;
数据量化模块,采用循环处理的方式,依次根据每个待量化数据根据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据;
结果确定模块,根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
根据本公开的另一方面,提供了一种人工智能芯片,其特征在于,所述芯片包括上述神经网络量化装置。
根据本公开的另一方面,提供了一种电子设备,所述电子设备包括上述人工智能芯片。
根据本公开的另一方面,提供了一种板卡,所述板卡包括:存储器件、接口装置和控制器件以及上述人工智能芯片;
其中,所述人工智能芯片与所述存储器件、所述控制器件以及所述接口装置分别连接;
所述存储器件,用于存储数据;
所述接口装置,用于实现所述人工智能芯片与外部设备之间的数据传输;
所述控制器件,用于对所述人工智能芯片的状态进行监控。
根据本公开的另一方面,提供了一种非易失性计算机可读存储介质,其上存储有计算机程序指令,所述计算机程序指令被处理器执行时实现上述神经网络量化方法。
本公开实施例所提供的神经网络量化方法、装置、计算机设备和存储介质,该方法包括:根据运行所述神经网络的终端的存储能力,从所述待量化层的目标数据中确定多个待量化数据,所述目标数据为所述待量化层中的任意一批待运算数据,所述待运算数据包括输入神经元和/或梯度中的至少一种;采用循环处理的方式,依次根据每个待量化数据根据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据;根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。本公开实施例所提供的神经网络量化方法、装置、计算机设备和存储介质,根据运行神经网络的终端的存储能力从待量化层的目标数据中确定多个待量化数据,利用对应的量化参数多个待量化数据分别进行量化,在保证精度的同时,减小了存储数据所占用的存储空间,保证了运算结果的准确性和可靠性,且能够提高运算的效率,且量化同样缩减了神经网络模型的大小,降低了对运行该神经网络模型的终端的性能要求。
通过权要中的技术特征进行推导,能够达到对应背景技术中的技术问题的有益效果。根据下面参考附图对示例性实施例的详细说明,本公开的其它特征及方面将变得清楚。
附图说明
包含在说明书中并且构成说明书的一部分的附图与说明书一起示出了本公开的示例性实施例、特征和方面,并且用于解释本公开的原理。
图1示出根据本公开实施例的处理器的示意图。
图2-1示出根据本公开实施例的神经网络量化方法的流程图。
图2-2示出根据本公开实施例的对称的定点数表示的示意图。
图2-3示出根据本公开实施例的引入偏移量的定点数表示的示意图。
图2-4示出根据本公开实施例的神经网络量化方法的流程图。
图2-5示出根据本公开一实施例的神经网络量化装置的框图。
图3-1示出根据本公开实施例的神经网络量化装置的框图。
图3-2示出根据本公开实施例的对称的定点数表示的示意图。
图3-3示出根据本公开实施例的引入偏移量的定点数表示的示意图。
图3-4示出根据本公开实施例的神经网络量化方法的流程图。
图4-1示出根据本公开实施例的神经网络量化方法的流程图。
图4-2示出根据本公开实施例的对称的定点数表示的示意图。
图4-3示出根据本公开实施例的引入偏移量的定点数表示的示意图。
图4-4示出根据本公开实施例的神经网络量化方法的流程图。
图4-5示出根据本公开一实施例的神经网络量化装置的框图。
图5示出根据本公开实施例的板卡的结构框图。
具体实施方式
下面将结合本公开实施例中的附图,对本公开实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本公开一部分实施例,而不是全部的实施例。基于本公开中的实施例,本领域技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本公开保护的范围。
应当理解,本公开的权利要求、说明书及附图中的术语“第一”、“第二”等是用于区别不同对象,而不是用于描述特定顺序。本公开的说明书和权利要求书中使用的术语“包括”和“包含”指示所描述特征、整体、步骤、操作、元素和/或组件的存在,但并不排除一个或多个其它特征、整体、步骤、操作、元素、组件和/或其集合的存在或添加。
还应当理解,在此本公开说明书中所使用的术语仅仅是出于描述特定实施例的目的,而并不意在限定本公开。如在本公开说明书和权利要求书中所使用的那样,除非上下文清楚地指明其它情况,否则单数形式的“一”、“一个”及“该”意在包括复数形式。还应当进一步理解,在本公开说明书和权利要求书中使用的术语“和/或”是指相关联列出的项中的一个或多个的任何组合以及所有可能组合,并且包括这些组合。
如在本说明书和权利要求书中所使用的那样,术语“如果”可以依据上下文被解释为“当...时”或“一旦”或“响应于确定”或“响应于检测到”。类似地,短语“如果确定”或“如果检测到[所描述条件或事件]”可以依据上下文被解释为意指“一旦确定”或“响应于确定”或“一旦检测到[所描述条件或事件]”或“响应于检测到[所描述条件或事件]”。
图1示出根据本公开实施例的处理器的示意图。如图1所示,该处理器100可以执行下述方法,处理器100包括多个处理单元101以及存储单元102,多个处理单元101用于执行指令序列,存储单元102用于存储数据,可包括随机存储器(RAM,Random Access Memory)和寄存器堆。处理器100中的多个处理单元101既可共用部分存储空间,例如共用部分RAM存储空间和寄存器堆,又可同时拥有各自的存储空间。
随着神经网络运算复杂度的提高,数据的数据量和数据维度也在不断增大,而传统的神经网络算法通常采用浮点数据格式来执行神经网络运算,这就使得不断增大的数据量等对运算装置的数据处理效率、存储装置的存储容量及访存效率等提出了较大的挑战。为解决上述问题,相关技术中,对神经网络运算过程涉及的全部数据均由浮点数转化定点数,但由于不同的数据之间具有差异性,或者,同一数据在不同阶段具有差异性,仅“由浮点数转化定点数”时,往往会导致精度不够,从而会影响运算结果。
神经网络中待运算数据通常为浮点数据格式或精度较高的定点数据格式,在承载神经网络的装置中运行神经网络时,浮点数据格式或精度较高的定点数据格式的各种待运算数据,导致神经网络运行的运算量和访存开销都较大。为提高运算效率,本公开实施例所提供的神经网络量化方法、装置、计 算机设备和存储介质,可以根据不同类待运算数据进行神经网络中的待运算数据的局部量化,量化后的数据格式通常为位宽较短、精度较低的定点数据格式。利用精度较低的量化后数据执行神经网络的运算,可以降低运算量和访存量。量化后的数据格式可以为位宽较短的定点数据格式。可以将浮点数据格式的待运算数据量化为定点数据格式的待运算数据,也可以将精度较高的定点格式的待运算数据量化为精度较低的定点格式的待运算数据。根据目标任务和/或终端的精度需求,从目标数据中确定出多个待量化数据,利用对应的量化参数对待量化数据进行量化,在保证精度的同时,减小了存储数据所占用的存储空间,保证了运算结果的准确性和可靠性,且能够提高运算的效率,且量化同样缩减了神经网络模型的大小,降低了对运行该神经网络模型的终端的性能要求,使神经网络模型可以应用于算力、体积、功耗相对受限的手机等终端。
可以理解的是,量化精度即量化后数据与量化前数据之间的误差的大小。量化精度可以影响神经网络运算结果的准确度。量化精度越高,运算结果的准确率越高,但运算量更大、访存开销也更大。相较于位宽较短的量化后数据,位宽较长的量化后数据的量化精度更高,用于执行神经网络的运算时准确率也更高。但在用于进行神经网络的运算时,位宽较长的量化后数据运算量更大、访存开销也较大,运算效率较低。同理,对于相同的待量化数据,采用不同的量化参数得到的量化后数据有不同的量化精度,将产生不同的量化结果,对运算效率和运算结果的准确率也会带来不同的影响。对神经网络进行量化,在运算效率和运算结果的准确率之间进行平衡,可以采用更加符合待运算数据的数据特征的量化后数据位宽和量化参数。
神经网络中的待运算数据可以包括权值、神经元、偏置、梯度中的至少一种。待运算数据为包含多个元素的矩阵。在传统的神经网络量化中,通常将待运算数据的整体进行量化后进行运算。而在利用量化后的待运算数据进行运算时,通常利用整体量化后的待运算数据中的一部分数据进行运算。例如,在卷积层,利用整体量化后的输入神经元进行卷积运算时,根据卷积核的维度和步长,在整体量化后的输入神经元中分别提取与卷积核的维度相当的量化后的神经元进行卷积运算。在全连接层,利用整体量化后的输入神经元进行矩阵乘运算时,在整体量化后的输入神经元中分别按行提取量化后的神经元进行矩阵乘的运算。因此,在传统的神经网络量化方法中,将待运算数据的整体进行量化后再按照部分量化后的数据进行运算,整体的运算效率较低。且将待运算数据的整体量化后再进行运算,需要将整体量化后的待运算数据进行存储,占用的存储空间较大,不能满足不同终端的精度需求,且不能满足终端利用神经网络执行不同任务的任务需求。
根据本公开实施例的神经网络量化方法可应用于处理器中,该处理器可以是通用处理器,例如CPU(Central Processing Unit,中央处理器),也可以是用于执行人工智能运算的人工智能处理器(IPU)。人工智能运算可包括机器学习运算,类脑运算等。其中,机器学习运算包括神经网络运算、k-means运算、支持向量机运算等。该人工智能处理器可例如包括GPU(Graphics Processing Unit,图形处理单元)、NPU(Neural-Network Processing Unit,神经网络处理单元)、DSP(Digital Signal Process,数字信号处理单元)、现场可编程门阵列(Field-Programmable Gate Array,FPGA)芯片中的一种或组合。本公开对处理器的具体类型不作限制。
在一种可能的实现方式中,本公开中所提及的处理器可包括多个处理单元,每个处理单元可以独立运行所分配到的各种任务,如:卷积运算任务、池化任务或全连接任务等。本公开对处理单元及处理单元所运行的任务不作限制。
图2-1示出根据本公开实施例的神经网络量化方法的流程图。如图2-1所示,该方法包括步骤S4-11至步骤S4-13。该方法可以应用于包括图1所示的处理器100的终端,终端用于运行神经网络以执行目标任务。其中,处理单元101用于执行步骤S4-11至步骤S4-13。存储单元102用于存储待量化数据、量化参数、数据位宽等与步骤S4-11至步骤S4-13的处理过程相关的数据。
在步骤S4-11中,根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种。
可以根据目标任务的任务类型、所需使用的待运算数据的数量、每个待运算数据的数据量,以及基于终端的计算精度、当前处理能力、存储能力等确定的精度需求,待运算数据所参与的运算类型等,确定从目标数据中确定出多个待量化数据的方式。
神经网络中的待量化层可以为神经网络中的任意一层。可以根据需求将神经网络中的部分层或全部层确定为待量化层。当神经网络中包括多个待量化层时,各待量化层可以连续也可以不连续。根据神经网络的不同,待量化层的种类也可以不同,例如待量化层可以为卷积层、全连接层等,本公开对待量化层的数量及类型不作限定。
在一种可能的实现方式中,所述待运算数据包括神经元、权值、偏置、梯度中的至少一种。可以根据需求将待量化层中的神经元、权值、偏置、梯度中的至少一种进行量化。目标数据为任意一种待量化的待运算数据。例如,待运算数据为神经元、权值和偏置,需要将神经元和权值进行量化,则神经元为目标数据1,权值为目标数据2。
当待量化层中有多种目标数据时,针对每种目标数据可以采用本公开中的量化方法进行量化后,得到与各目标数据对应的量化数据,再利用各种目标数据的量化数据和不需要进行量化的待运算数据执行待量化层的运算。
神经网络运算的推理阶段可包括:将训练好的神经网络进行前向运算以完成设定任务的阶段。在神经网络的推理阶段,可以将神经元、权值、偏置和梯度中的至少一种作为待量化数据,根据本公开实施例中的方法进行量化后,利用量化后的数据完成待量化层的运算。
神经网络运算的微调阶段可包括:将训练好的神经网络进行预设数量迭代的前向运算和反向运算,进行参数的微调以适应设定任务的阶段。在神经网络运算的微调阶段,可以将神经元、权值、偏置、梯度中的至少一种,根据本公开实施例中的方法进行量化后,利用量化后的数据完成待量化层的前向运算或反向运算。
神经网络运算的训练阶段可包括:将初始化的神经网络进行迭代训练以得到训练好的神经网络的阶段,训练好的神经网络可执行特定任务。在神经网络的训练阶段,可以将神经元、权值、偏置、梯度中的至少一种,根据本公开实施例中的方法进行量化后,利用量化后的数据完成待量化层的前向运算或反向运算。
可以将一个目标数据中的子集作为待量化数据,可以按照不同的方式将目标数据划分为多个子集,将每个子集作为一个待量化数据。将一个目标数据划分为多个待量化数据。可以根据目标数据所要进行的运算类型,将目标数据划分为多个待量化数据。例如,目标数据需要进行卷积运算时,可以根据卷积核的高度和宽度,将目标数据划分为与卷积核对应的多个待量化数据。目标数据为需要进行矩阵乘运算的左矩阵时,可以将目标数据按行划分为多个待量化数据。可以一次将目标数据划分为多个待量化数据,也可以按照运算的顺序,将目标数据依次划分为多个待量化数据。
也可以根据预先设定的数据划分方式,将目标数据划分为多个待量化数据。例如,预先设定的数据划分方式可以为:按照固定的数据大小进行划分,或按照固定的数据形状进行划分。
将目标数据划分为多个待量化数据后,可以将各待量化数据分别进行量化,并根据各待量化数据量化后的数据进行运算。一个待量化数据所需的量化时间,短于目标数据的整体的量化时间,将其中一个待量化数据量化完毕后,即可以用量化后的数据执行后续的运算,而不用等目标数据中的所有待量化数据均量化完成后再执行运算。因此本公开中的目标数据的量化方法,可以提高目标数据的运算效率。
在步骤S4-12中,将各所述待量化数据分别根据对应的量化参数进行量化,得到与各所述待量化数据对应的量化数据。
待量化数据对应的量化参数可以为一个量化参数,也可以为多个量化参数。量化参数可以包括点位置等用于对待量化数据进行量化的参数。点位置可以用于确定量化后数据中小数点的位置。量化参数还可以包括缩放系数、偏移量等。
确定与待量化数据对应的量化参数的方式,可以包括:确定与目标数据对应的量化参数后,并将与目标数据对应的量化参数确定为待量化数据的量化参数的方式。当待量化层包括多个目标数据时,各目标数据均可以有与之对应的量化参数,且各目标数据对应的量化参数可以不同,也可以相同,本公开对此不作限定。将目标数据划分为多个待量化数据后,可以将目标数据对应的量化参数确定为各待量化数据对应的量化参数,此时各待量化数据对应的量化参数相同。
确定与待量化数据对应的量化参数的方式,也可以包括:直接确定各待量化数据对应的量化参数的方式。目标数据可以没有与之对应的量化参数,或目标数据可以有与之对应的量化参数但待量化数据不采用。可以直接为各待量化数据设定对应的量化参数。也可以根据待量化数据计算得到对应的量化参数。此时各待量化数据对应的量化参数可以相同也可以不同。例如,当待量化层为卷积层,目标数据为权重时,可以将权重按照通道划分为多个待量化权重数据,不同通道的待量化权重数据可以对应不同的量化参数。当各待量化数据对应的量化参数不同时,各待量化数据利用对应的量化参数进行量化后,所得到的量化结果需不影响目标数据的运算。
确定与目标数据对应的量化参数的方式、或确定与待量化数据对应的量化参数的方式,可以包括:查找预设的量化参数直接确定量化参数的方式、查找对应关系以确定量化参数的方式,或根据待量化数据计算得到量化参数的方式。以下以确定与待量化数据对应的量化参数的方式为例进行说明:
可以直接设定与待量化数据对应的量化参数。可以将设定好的量化参数存储于设定的存储空间。设定的存储空间可以为片上或片外的存储空间。例如,可以将设定好的量化参数存储于设定的存储空间。各待量化数据在进行量化时,可以在设定的存储空间提取对应的量化参数后进行量化。可以根据经验值设定与每种待量化数据对应的量化参数。也可以根据需求更新所存储的与每种待量化数据对应的量化参数。
可以根据各待量化数据的数据特征,通过查找数据特征与量化参数的对应关系,确定量化参数。例如,待量化数据的数据分布为稀疏和稠密时可以分别对应不同的量化参数。可以通过查找对应关系确定与待量化数据的数据分布对应的量化参数。
还可以根据各待量化数据,利用设定的量化参数计算方法,计算得到各待量化层对应的量化参数。例如,可以根据待量化数据的绝对值最大值和预设的数据位宽,利用取整算法计算得到量化参数中的点位置。
在步骤S4-13中,根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
可以利用设定的量化算法,根据量化参数对待量化数据进行量化,得到量化数据。例如,可以利用取整算法作为量化算法,可以根据数据位宽和点位置对待量化数据进行取整量化得到量化数据。其中,取整算法可以包括向上取整、向下取整、向零取整和四舍五入取整等。本公开对量化算法的具体实现方式不作限定。
各待量化数据可以分别采用对应的量化参数进行量化。由于与各待量化数据对应的量化参数更为贴合各待量化数据自身的特征,使得各待量化层的每种量化数据的量化精度更加符合目标数据的运算需求,也就更加符合待量化层的运算需求。在保证待量化层的运算结果准确率的前提下,能够提高待量化层的运算效率,达到待量化层的运算效率和运算结果准确率之间的平衡。进一步的,将目标数据 划分为多个待量化数据分别量化,可以在量化完一个待量化数据后,根据量化得到的量化结果执行运算的同时,可以进行第二个待量化数据的量化,从而在整体上提高目标数据的运算效率,也就提高了待量化层的计算效率。
可以将各待量化数据的量化数据进行合并后得到目标数据的量化结果。也可以将各待量化数据的量化数据进行设定的运算后得到目标数据的量化结果。例如可以将各待量化数据的量化数据按照设定的权重进行加权运算后得到目标数据的量化结果。本公开对此不作限定。
在神经网络的推理、训练和微调过程中,可以对待量化数据进行离线量化或在线量化。其中,离线量化可以为利用量化参数对待量化数据进行离线处理。在线量化可以为利用量化参数对待量化数据进行在线处理。例如,神经网络运行在人工智能芯片上,可以将待量化数据和量化参数发送至人工智能芯片之外的运算装置进行离线量化,或利用人工智能芯片之外的运算装置对预先得到的待量化数据和量化参数进行离线量化。而在人工智能芯片运行神经网络的过程中,人工智能芯片可以对待量化数据利用量化参数进行在线量化。本公开中对各待量化数据的量化过程为在线或离线不作限定。
在本实施例所提供的神经网络量化方法,应用于终端,所述终端用于运行神经网络以执行目标任务,所述方法包括:根据所述目标任务和/或所述终端的精度需求,从待量化层的目标数据中确定多个待量化数据,各待量化数据均为目标数据的子集,目标数据为待量化层的任意一种待量化的待运算数据,待运算数据包括输入神经元、权值、偏置、梯度中的至少一种;将各待量化数据分别根据对应的量化参数进行量化,得到与各待量化数据对应的量化数据;根据与各待量化数据对应的量化数据得到目标数据的量化数据,以使待量化层根据目标数据的量化数据进行运算。将目标数据划分为多个待量化数据后,各待量化数据的量化过程与运算过程可以并行执行,从而可以提高目标数据的量化效率和运算效率,也可以提高待量化层直至提高整个神经网络的量化效率和运算效率。
下面给出步骤S4-11的五种实现方式示例,也即方式1-方式5以便于更好的理解步骤S4-11的实现过程。
方式1:根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的目标数据确定为一个待量化数据。
在神经网络包括多个待量化层时,可以根据目标任务和终端的精度需求确定终端每一次所能量化的数据的量化数据量,进而根据不同量化层中目标数据的数据量以及量化数据量,将一个或多个待量化层中的目标数据确定为一个待量化数据。例如,将一个待量化层中的输入神经元确定为一个待量化数据。
方式2:根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的同一种待运算数据确定为一个待量化数据。
在神经网络包括多个待量化层时,可以根据目标任务和终端的精度需求确定终端每一次所能量化的数据的量化数据量,进而根据不同量化层中目标数据的数据量以及量化数据量,将一个或多个待量化层中的某一种目标数据确定为一个待量化数据。例如,将全部待量化层中的输入神经元确定为一个待量化数据。
方式3:根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个通道中的数据确定为一个待量化数据。
在待量化层为卷积层时,待量化层中含有通道,可以根据通道以及根据目标任务和终端的精度需求所确定的终端每一次所能量化的数据的量化数据量,将一个或多个通道中的数据确定为一个待量化数据。例如,对于某个卷积层,可以将2个通道中的目标数据确定为一个待量化数据。或者可以将每一个通道中的目标数据确定为一个待量化数据。
方式4:根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个 批数的数据确定为一个待量化数据;
在待量化层为卷积层时,在卷积层的输入神经元的维度可以包括批数(batch,B)、通道(channel,C)、高度(height,H)和宽度(width,W)。当输入神经元的批数为多个时,各批数的输入神经元可以看作维度为通道、高度和宽度的三维数据。各批数的输入神经元可以对应多个卷积核,各批数的输入神经元的通道数,和与之对应的各卷积核的通道数一致。
对于任意一个批数的输入神经元,以及对于与该批数的输入神经元对应的多个卷积核中的任意一个卷积核,可以根据量化数据量和该批数的输入神经元的数据量,将该批次的输入神经元与该卷积核对应的部分数据(子集),确定为该批次的输入神经元与该卷积核对应的多个待量化数据。例如,假定目标数据B1有3个批数的数据,若将目标数据中一个批数的数据确定为一个待量化数据,则该目标数据B可以被划分为3个待量化数据。
可以根据卷积核的维度和步长,将输入神经元划分得到全部的待量化数据后,将各待量化数据并行地执行量化过程。由于待量化数据的数据量小于输入神经元,对一个待量化数据进行量化的计算量,小于对输入神经元进行整体量化的计算量,因此,本实施例中的量化方法可以提高输入神经元的量化速度,提高量化效率。也可以将输入神经元根据卷积核的维度和步长进行划分,依次得到各待量化数据后,将得到的各待量化数据分别与卷积核进行卷积运算。各待量化数据的量化过程和卷积运算过程可以并行执行,本实施例中的量化方法可以提高输入神经元的量化效率和运算效率。
方式5:根据所述目标任务和/或所述终端的精度需求确定划分数据的划分尺寸,将对应待量化层中的目标数据按照所述划分尺寸划分为一个或多个待量化数据。
可以根据目标任务和终端的精度需求确定终端的实时处理能力,可以包括:终端对目标数据进行量化的速度,对量化后数据进行运算的速度,对目标数据进行量化和运算时终端所能处理的数据量等表征终端处理目标数据的处理能力相关的信息。例如,可以根据对目标数据进行量化的速度和对量化后数据进行运算的速度,确定待量化数据的尺寸,以使得对待量化数据进行量化的时间与对量化后数据进行运算的速度相同,这样可以量化和运算同步进行,可以提高目标数据的运算效率。终端的实时处理能力越强,待量化数据的尺寸越大。
在本实施例中,可以根据需要对确定待量化数据的方式进行设置,待量化数据可以包括一种待运算数据如输入神经元(也可以是权值、偏置、梯度,以下以输入神经元为例进行说明),该待运算数据可以是某一个待量化层中的部分或全部输入神经元,也可以是多个待量化层中的、包括每个待量化层中的全部或部分输入神经元。待量化数据还可以是对应于待量化层某一个通道的全部或部分输入神经元,或者对应于待量化层几个通道的全部输入神经元。待量化数据也可以是某一个输入神经元的部分或全部等等。也就是说,可以根据任意的方式对目标数据进行划分,本公开对此不作限制。
在一种可能的实现方式中,在对目标数据进行量化的过程中可以采用与目标数据对应的量化参数进行量化。而将目标数据划分为多个待量化数据后,可以采用与各待量化数据对应的量化参数进行量化。与各待量化数据对应的量化参数,可以采用预设的方式或根据待量化数据计算的方式,无论采用何种方式确定与各待量化数据对应的量化参数,都可以使得各待量化数据的量化参数,更加符合待量化数据自身的量化需求。例如,当根据目标数据计算得到对应的量化参数时,可以利用目标数据中各元素的最大值和最小值计算得到量化参数。而在根据待量化数据计算得到对应的量化参数时,可以利用待量化数据中各元素的最大值和最小值计算得到量化参数,待量化数据的量化参数比目标数据的量化参数能够更加贴合待量化数据的数据特征,可以使得待量化数据的量化结果更加准确,量化精度更高。
在一种可能的实现方式中,该方法还可以包括:
根据所述目标任务和/或所述终端的精度需求,确定待量化数据对应的数据位宽;
根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
在该实现方式中,可以根据目标任务和终端的精度需求,确定满足需求的参考数据位宽或数据位宽参考范围,进而结合待量化数据自身的特征确定待量化数据对应的数据位宽。
在该实现方式中,可以对待量化数据进行统计,根据统计结果和数据位宽确定待量化数据对应的量化参数。量化参数可以包括点位置、缩放系数和偏移量中的一种或多种。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,可以包括:
当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值Z 1和对应的数据位宽,得到各所述待量化数据的第一类点位置。其中,该绝对值最大值Z 1是待量化数据中数据取绝对值后所得到的最大值。
在该实现方式中,当待量化数据为相对于原点对称的数据时,量化参数可以不包括偏移量,假设Z 1为待量化数据中元素的绝对值的最大值,待量化数据对应的数据位宽为n,A 1为用数据位宽n对待量化数据进行量化后的量化数据可以表示的最大值,A 1
Figure PCTCN2020095673-appb-000001
A 1需要包含Z 1,且Z 1要大于
Figure PCTCN2020095673-appb-000002
因此有公式(4-1)的约束:
Figure PCTCN2020095673-appb-000003
处理器可以根据待量化数据中的绝对值最大值Z 1和数据位宽n,计算得到第一类点位置s 1。例如,可以利用如下公式(4-2)计算得到待量化数据对应的第一类点位置s 1
Figure PCTCN2020095673-appb-000004
其中,ceil为向上取整,Z 1为待量化数据中的绝对值最大值,s 1为第一类点位置,n为数据位宽。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,可以包括:
当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置s 2
在该实现方式中,可以先获取待量化数据中的最大值Z max、最小值Z min,进而根据最大值Z max、最小值Z min利用下述公式(4-3)进行计算,
Figure PCTCN2020095673-appb-000005
进一步地,根据计算得到的Z 2和对应的数据位宽利用下述公式(4-4)计算第二类点位置s 2
Figure PCTCN2020095673-appb-000006
在该实现方式中,由于量化时,常规情况下会将待量化数据中的最大值和最小值保存下来,直接基于保存的待量化数据中的最大值和最小值来获取绝对值最大值,无需消耗更多的资源去对待量化数据求绝对值,节省确定统计结果的时间。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数f’。其中,第一类缩放系数f’可以包括第一缩放系数f 1和第二缩放系数f 2
其中,该第一缩放系数f 1可以按照如下方式(4-5)进行计算:
Figure PCTCN2020095673-appb-000007
其中,第二缩放系数f 2可以按照如下公式(4-6)进行计算:
Figure PCTCN2020095673-appb-000008
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,可以包括:
根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。
在该实现方式中,图2-2示出根据本公开实施例的对称的定点数表示的示意图。如图2-2所示的待量化数据的数域是以“0”为对称中心分布。Z 1为待量化数据的数域中所有浮点数的绝对值最大值,在图3中,A 1为n位定点数可以表示的浮点数的最大值,浮点数A 1转换为定点数是(2 n-1-1)。为了避免溢出,A 1需要包含Z 1。在实际运算中,神经网络运算过程中的浮点数据趋向于某个确定区间的正态分布,但是并不一定满足以“0”为对称中心的分布,这时用定点数表示时,容易出现溢出情况。为了改善这一情况,量化参数中引入偏移量。图2-3示出根据本公开实施例的引入偏移量的定点数表示的示意图。如图2-3所示。待量化数据的数域不是以“0”为对称中心分布,Z min是待量化数据的数域中所有浮点数的最小值,Z max是待量化数据的数域中所有浮点数的最大值,A 2为用n位定点数表示的平移后的浮点数的最大值,A 2
Figure PCTCN2020095673-appb-000009
P为Z min~Z max之间的中心点,将待量化数据的数域整体偏移,使得平移后的待量化数据的数域以“0”为对称中心分布,以避免数据的“溢出”。平移后的待量化数据的数域中的绝对值最大值为Z 2。由图2-3可知,偏移量为“0”点到“P”点之间的水平距离,该距离称为偏移量o。
可以根据该最小值Z min和最大值Z max按照如下公式(4-7)计算获得偏移量:
Figure PCTCN2020095673-appb-000010
其中,o表示偏移量,Z min表示待量化数据所有元素中的最小值,Z max表示待量化数据所有元素中的最大值。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,可以包括:
当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数f”。其中,第二类缩放系数f”可以包括第三缩放系数f 3和第四缩放系数f 4
在该实现方式中,当量化参数包括偏移量时,A 2为用数据位宽n对平移后的待量化数据进行量化后的量化数据可以表示的最大值,A 2
Figure PCTCN2020095673-appb-000011
可以根据待量化数据中的Z max、最小值Z min进行计算得到平移后的待量化数据的数域中的绝对值最大值Z 2,进而按照如下公式(4-8)计算第三缩放系数f 3
Figure PCTCN2020095673-appb-000012
进一步地,第四缩放系数f 4可以按照如下公式(4-9)进行计算:
Figure PCTCN2020095673-appb-000013
在对待量化数据进行量化时,所采用的量化参数不同,进行量化所使用的数据不同。
在一种可能的实现方式中,量化参数可以包括第一类点位置s 1。可以利用如下的公式(4-10)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000014
其中,I x为量化数据,F x为待量化数据,round为进行四舍五入的取整运算。
量化参数可以包括第一类点位置s 1时,可以根据公式(4-11)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000015
Figure PCTCN2020095673-appb-000016
在一种可能的实现方式中,量化参数可以包括第一类点位置和第一缩放系数。可以利用如下的公式(4-12)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000017
当量化参数包括第一类点位置和第一缩放系数时,可以根据公式(4-13)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000018
Figure PCTCN2020095673-appb-000019
在一种可能的实现方式中,量化参数可以包括第二缩放系数。可以利用如下的公式(4-14)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000020
当量化参数包括第二缩放系数时,可以根据公式(4-15)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000021
Figure PCTCN2020095673-appb-000022
在一种可能的实现方式中,量化参数可以包括偏移量。可以利用如下的公式(4-16)对待量化数据进行量化,得到量化数据I x
I x=round(F x-o)       公式(4-16)
当量化参数包括偏移量时,可以根据公式(4-17)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000023
Figure PCTCN2020095673-appb-000024
在一种可能的实现方式中,量化参数可以包括第二类点位置和偏移量。可以利用如下的公式(4-18)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000025
当量化参数包括第二类点位置和偏移量时,可以根据公式(4-19)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000026
Figure PCTCN2020095673-appb-000027
在一种可能的实现方式中,量化参数可以包括第二类缩放系数f”和偏移量o。可以利用如下的公式(4-20)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000028
当量化参数包括第二类缩放系数和偏移量时,可以根据公式(4-21)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000029
Figure PCTCN2020095673-appb-000030
在一种可能的实现方式中,量化参数可以包括第二类点位置、第二类缩放系数和偏移量。可以利用如下的公式(4-22)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000031
当量化参数包括第二类点位置、第二类缩放系数和偏移量时,可以根据公式(4-23)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000032
Figure PCTCN2020095673-appb-000033
可以理解的是,也可以采用其他的取整运算方法,例如采用向上取整、向下取整、向零取整等取整运算,替换上述公式中的四舍五入的取整运算round。可以理解的是,在数据位宽一定的情况下,根据点位置量化得到的量化数据中,小数点后的位数越多,量化数据的量化精度越大。
在一种可能的实现方式中,上述步骤S4-11可以包括:
通过查找待量化数据与量化参数对应关系,确定与所述待量化层中每种待量化数据对应的量化参数。
在一种可能的实现方式中,各待量化层中与每种待量化数据对应的量化参数,可以是保存的预设值。可以为神经网络建立一个待量化数据与量化参数之间的对应关系,该对应关系可以包括各待量化层的每种待量化数据与量化参数对应关系,并将对应关系保存在各层可以共享访问的存储空间。也可以为神经网络建立多个待量化数据与量化参数之间的对应关系,各待量化层分别对应其中一个对应关系。可以将各层的对应关系保存在本层独享的存储空间,也可以将各层的对应关系保存在各层可以共享访问的存储空间。
在待量化数据与量化参数对应关系中,可以包括多个待量化数据和与之对应的多个量化参数之间的对应关系。例如,待量化数据与量化参数对应关系A中,可以包括待量化层1的神经元和权值两个待量化数据,神经元对应点位置1、缩放系数1和偏移量1三个量化参数,权值对应点位置2和偏移量2两个量化参数。本公开对待量化数据与量化参数对应关系的具体格式不作限定。
在本实施例中,可以通过查找待量化数据与量化参数对应关系,确定与所述待量化层中每种待量化数据对应的量化参数。可以为各待量化层预设对应的量化参数,并通过对应关系进行存储后,供待量化层查找后使用。本实施例中量化参数的获取方式简单方便。
图2-4示出根据本公开实施例的神经网络量化方法的流程图。在一种可能的实现方式中,如图2-4所示,该方法还可以包括步骤S4-14至步骤S4-16。
在步骤S4-14中,根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差。
可以根据待量化数据对应的量化数据与待量化数据之间的误差,确定待量化数据的量化误差。可以利用设定的误差计算方法,例如标准差计算方法、均方根误差计算方法等,计算待量化数据的量化误差。
也可以根据量化参数,将待量化数据对应的量化数据进行反量化后得到反量化数据,根据反量化数据与待量化数据之间的误差,按照公式(4-24)确定待量化数据的量化误差diff bit
Figure PCTCN2020095673-appb-000034
其中,F i为待量化对应的浮点值,其中,i为待量化数据中数据的下标。
Figure PCTCN2020095673-appb-000035
为浮点值对应的反量化数据。
还可以根据量化间隔、量化后的数据的个数以及对应的量化前的数据按照公式(4-25)确定量化误差diff bit
Figure PCTCN2020095673-appb-000036
其中,C为量化时对应的量化间隔,m为量化后获得的量化数据的个数,F i为待量化对应的浮点值,其中,i为待量化数据中数据的下标。
也可以根据量化后的数据以及对应的反量化数据按照公式(4-26)确定量化误差diff bit
Figure PCTCN2020095673-appb-000037
其中,F i为待量化对应的浮点值,其中,i为待量化数据集合中数据的下标。
Figure PCTCN2020095673-appb-000038
为浮点值对应的反量化数据。
在步骤S4-15中,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽。
可以根据经验值确定误差阈值,误差阈值可以用于表示对量化误差的期望值。当量化误差大于或小于误差阈值时,可以调整待量化数对应的数据位宽,得到待量化数据对应的调整位宽。可以将数据位宽调整为更长的位宽或更短的位宽,以提高或降低量化精度。
可以根据能够接受的最大误差确定误差阈值,当量化误差大于误差阈值时,说明量化精度不能达到预期,需要将数据位宽调整为更长的位宽。也可以根据较高的量化精度确定一个较小的误差阈值,当量化误差小于误差阈值时,说明量化精度较高,神经网络的运行效率将受到影响,可以适当的将数据位宽调整为更短的位宽,以适当的降低量化精度,提高神经网络的运行效率。
可以将数据位宽按照固定的位数步长进行调整,也可以根据量化误差与误差阈值之间的差值的不同,按照可变的调整步长调整数据位宽。本公开对此不作限定。
在步骤S4-16中,将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
确定调整位宽后,可以将待量化数据对应的数据位宽更新为调整位宽。例如,待量化数据更新前的数据位宽为8位,调整位宽为12位,则更新后待量化数据对应的数据位宽为12位。可以根据调整位宽和待量化数据计算得到待量化数据对应的调整量化参数。可以根据待量化数据对应的调整量化参数重新对待量化数据进行量化,以得到量化精度更高或更低的量化数据,使得待量化层在量化精度和处 理效率之间达到平衡。
在神经网络的推理、训练和微调过程中,各层之间的待量化数据可以认为具有一定的关联性。例如,各层的待量化数据之间的均值之间的差小于设定的均值阈值,且各层的待量化数据之间的最大值之间的差值也小于设定的差值阈值时,可以将待量化层的调整量化参数作为后续的一个或多个层的调整量化参数,用于对待量化层后续的一个或多个层的待量化数据进行量化。也可以在神经网络的训练和微调过程中,将待量化层在当前迭代得到的调整量化参数,用于在后续的迭代中对待量化层进行量化。
在一种可能的实现方式中,所述方法还包括:
在所述待量化层之后的一层或多层采用所述待量化层的量化参数。
神经网络根据调整量化参数进行量化,可以包括只在待量化层利用调整量化参数对待量化数据重新进行量化,并将重新得到的量化数据用于待量化层的运算。也可以包括在待量化层不使用调整量化参数重新对待量化数据进行量化,而在待量化层后续的一个或多个层使用调整量化参数进行量化,和/或后续的迭代中在待量化层使用调整量化参数进行量化。还可以包括在待量化层使用调整量化参数重新进行量化,并将重新得到的量化数据用于待量化层的运算,并且在待量化层后续的一个或多个层使用调整量化参数进行量化,和/或后续的迭代中在待量化层使用调整量化参数进行量化。本公开对此不作限定。
在本实施例中,根据待量化数据和待量化数据对应的量化数据之间的误差调整数据位宽,并根据调整后的数据位宽计算得到调整量化参数。通过设置不同的误差阈值可以得到不同的调整量化参数,达到提高量化精度或提高运行效率等不同的量化需求。根据待量化数据和待量化数据的量化数据计算得到的调整量化参数,也能够更加符合待量化数据自身的数据特征,达到更加符合待量化数据自身需求的量化结果,在量化精度和处理效率之间达到更好的平衡。
在一种可能的实现方式中,步骤S4-15可以包括:
当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
可以根据能够接受的最大的量化误差,确定第一误差阈值。可以将量化误差与第一误差阈值进行比较。当量化误差大于第一误差阈值时,可以认为量化误差已经不可接受。需要提高量化精度,可以通过增加待量化数据对应的数据位宽的方式,提高待量化数据的量化精度。
可以将待量化数据对应的数据位宽按照固定的调整步长增加,得到调整位宽。固定的调整步长可以为N位,N为正整数。每次调整数据位宽可以增加N位。每次增加后的数据位宽=原数据位宽+N位。
可以将待量化数据对应的数据位宽按照可变的调整步长增加,得到调整位宽。例如,当量化误差与误差阈值之间的差值大于第一阈值时,可以按照调整步长M1调整数据位宽,当量化误差与误差阈值之间的差值小于第一阈值时,可以按照调整步长M2调整数据位宽,其中,第一阈值大于第二阈值,M1大于M2。可以根据需求确定各可变的调整步长。本公开对数据位宽的调整步长及调整步长是否可变不作限定。
可以将待量化数据按照调整位宽计算得到调整后的量化参数。利用调整后的量化参数对待量化数据进行重新量化后得到的量化数据,比利用调整前的量化参数量化得到的量化数据的量化精度更高。
在一种可能的实现方式中,该方法还可以包括:
根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
根据量化误差增加待量化数据对应的数据位宽时,调整一次位宽后得到调整位宽,根据调整位宽 计算得到调整后的量化参数,根据调整后的量化参数量化待量化数据得到调整后的量化数据,再根据调整后的量化数据与待量化数据计算得到待量化数据调整后的量化误差,调整后的量化误差可能依然大于第一误差阈值,即根据调整一次的数据位宽可能不能满足调整目的。当调整后的量化误差依然大于第一误差阈值时,可以继续对调整后的数据位宽进行调整,即多次增加待量化数据对应的数据位宽,直至根据最终得到的调整位宽和待量化数据得到的调整后的量化误差小于第一误差阈值。
多次增加的调整步长可以是固定的调整步长,也可以是可变的调整步长。例如,最终的数据位宽=原数据位宽+B*N位,其中N为每次增加的固定的调整步长,B为数据位宽的增加次数。最终的数据位宽=原数据位宽+M1+M2+…+Mm,其中,M1、M2…Mm为每次增加的可变的调整步长。
在本实施例中,当量化误差大于第一误差阈值时,增加所述待量化数据对应的数据位宽,得到所述待量化数据对应的调整位宽。可以通过设置第一误差阈值和调整步长增加数据位宽,以使调整后的数据位宽能够满足量化的需求。当一次调整不能满足调整需求时,还可以对数据位宽进行多次调整。第一误差阈值和调整步长的设置,使得量化参数可以按照量化需求进行灵活调整,满足不同的量化需求,使得量化精度可根据自身数据特征进行自适应调整。
在一种可能的实现方式中,步骤S4-15可以包括:
当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值
可以根据能够接受的量化误差和期望的神经网络的运行效率,确定第二误差阈值。可以将量化误差与第二误差阈值进行比较。当量化误差小于第二误差阈值时,可以认为量化误差超出预期,但运行效率过低已经不可接受。可以降低量化精度以提高神经网络的运行效率,可以通过减少待量化数据对应的数据位宽的方式,降低待量化数据的量化精度。
可以将待量化数据对应的数据位宽按照固定的调整步长减少,得到调整位宽。固定的调整步长可以为N位,N为正整数。每次调整数据位宽可以减少N位。增加后的数据位宽=原数据位宽-N位。
可以将待量化数据对应的数据位宽按照可变的调整步长减少,得到调整位宽。例如,当量化误差与误差阈值之间的差值大于第一阈值时,可以按照调整步长M1调整数据位宽,当量化误差与误差阈值之间的差值小于第一阈值时,可以按照调整步长M2调整数据位宽,其中,第一阈值大于第二阈值,M1大于M2。可以根据需求确定各可变的调整步长。本公开对数据位宽的调整步长及调整步长是否可变不作限定。
可以将待量化数据按照调整位宽计算得到调整后的量化参数,利用调整后的量化参数对待量化数据进行重新量化后得到的量化数据,比利用调整前的量化参数量化得到的量化数据的量化精度更低。
在一种可能的实现方式中,该方法还可以包括:
根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
根据量化误差增加待量化数据对应的数据位宽时,调整一次位宽后得到调整位宽,根据调整位宽计算得到调整后的量化参数,根据调整后的量化参数量化待量化数据得到调整后的量化数据,再根据调整后的量化数据与待量化数据计算得到待量化数据调整后的量化误差,调整后的量化误差可能依然小于第二误差阈值,即根据调整一次的数据位宽可能不能满足调整目的。当调整后的量化误差依然小于第二误差阈值时,可以继续对调整后的数据位宽进行调整,即多次减少待量化数据对应的数据位宽,直至根据最终得到的调整位宽和待量化数据得到的调整后的量化误差大于第二误差阈值。
多次减少的调整步长可以是固定的调整步长,也可以是可变的调整步长。例如,最终的数据位宽=原数据位宽-B*N位,其中N为每次增加的固定的调整步长,B为数据位宽的增加次数。最终的数据位 宽=原数据位宽-M1-M2-…-Mm,其中,M1、M2…Mm为每次减少的可变的调整步长。
在本实施例中,当量化误差小于第二误差阈值时,减少所述待量化数据对应的数据位宽,得到所述待量化数据对应的调整位宽。可以通过设置第二误差阈值和调整步长减少数据位宽,以使调整后的数据位宽能够满足量化的需求。当一次调整不能满足调整需求时,还可以对数据位宽进行多次调整。第二误差阈值和调整步长的设置,使得量化参数可以按照量化需求进行灵活的自适应调整,满足不同的量化需求,使得量化精度可调,在量化精度和神经网络的运行效率之间达到平衡。
在一种可能的实现方式中,所述方法还包括:
当所述量化误差大于第一误差阈值时,增加所述待量化数据对应的数据位宽,以及当所述量化误差小于第二误差阈值时,减少所述待量化数据对应的数据位宽,得到所述待量化数据对应的调整位宽。
也可以同时设置两个误差阈值,其中,第一误差阈值用于表示量化精度过低,可以增加数据位宽的位数,第二误差阈值用于表示量化精度过高,可以减少数据位宽的位数。第一误差阈值大于第二误差阈值,可以将待量化数据的量化误差同时与两个误差阈值进行比较,当量化误差大于第一误差阈值时,增加数据位宽的位数,当量化误差小于第二误差阈值时,减少数据位宽的位数。当量化误差位于第一误差阈值和第二误差阈值之间时,数据位宽可以保持不变。
在本实施例中,通过将量化误差与第一误差阈值和第二误差阈值同时进行比较,可以根据比较结果增加或减少数据位宽,可以利用第一误差阈值和第二误差阈值更加灵活的调整数据位宽。使得数据位宽的调整结果更加符合量化需求。
在一种可能的实现方式中,在所述神经网络运算的微调阶段和/或训练阶段,该方法还可以包括:
获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
在神经网络运算的微调阶段和/或训练阶段包括多次迭代。神经网络中的各待量化层,在进行一次正向运算和一次反向运算,并对待量化层的权值进行更新后,完成一次迭代。在多次迭代中,待量化层中的待量化数据和/或待量化数据对应的量化数据的数据变动幅度,可以用于衡量在不同迭代中的待量化数据和/或量化数据是否可采用相同的量化参数进行量化。若当前迭代以及历史迭代中待量化数据的数据变动幅度较小,例如小于设定的幅度变动阈值时,可以在数据变动幅度较小的多个迭代中采用相同的量化参数。
可以通过提取预存的量化参数的方式,确定与待量化数据对应的量化参数。在不同的迭代中对待量化数据进行量化时,需要在各迭代提取与待量化数据对应的量化参数。若多个迭代的待量化数据和/或待量化数据对应的量化数据的数据变动幅度较小,可将在数据变动幅度较小的多个迭代中采用的相同的量化参数进行暂存,各迭代在进行量化时可以利用暂存的量化参数进行量化运算,不用在每次迭代提取量化参数。
也可以根据待量化数据和数据位宽计算得到量化参数。在不同的迭代中对待量化数据进行量化时,需要在各迭代分别计算量化参数。若多个迭代的待量化数据和/或待量化数据对应的量化数据的数据变动幅度较小,可在数据变动幅度较小的多个迭代中采用的相同的量化参数,则各迭代均可以直接使用其中第一个迭代计算得到的量化参数,而不是每次迭代计算量化参数。
可以理解的是,当待量化数据为权值时,各迭代之间的权值在不断更新,若多个迭代的权值的数据变动幅度较小,或多个迭代的权值对应的量化数据的数据变动幅度较小,可以在多个迭代中利用相同的量化参数对权值进行量化。
可以根据待量化数据的数据变动幅度确定目标迭代间隔,目标迭代间隔包括至少一次迭代,可以 在目标迭代间隔内的各迭代使用相同的量化参数,即在目标迭代间隔内的各迭代不再更新待量化数据的量化参数。神经网络根据目标迭代间隔更新待量化数据的量化参数,包括在目标迭代间隔内的迭代,不获取预设的量化参数或不计算量化参数,即在目标迭代间隔内的迭代不更新量化参数。而在目标迭代间隔外的迭代,再获取预设的量化参数或计算量化参数,即在目标迭代间隔外的迭代更新量化参数。
可以理解的是,多个迭代之间的待量化数据或待量化数据的量化数据的数据变动幅度越小,确定出的目标迭代间隔包括的迭代次数越多。可以根据计算得到的数据变动幅度,查找预设的数据变动幅度与迭代间隔的对应关系,确定与计算得到的数据变动幅度对应的目标迭代间隔。可以根据需求预设数据变动幅度与迭代间隔的对应关系。也可以根据计算得到的数据变动幅度,利用设定的计算方法计算得到目标迭代间隔。本公开不限定数据变动幅度的计算方式,以及目标迭代间隔的获取方式。
在本实施例中,在神经网络运算的微调阶段和/或训练阶段,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,根据所述待量化数据的数据变动幅度,确定待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。可以根据多个迭代中待量化数据或待量化数据对应的量化数据的数据变动幅度,确定目标迭代间隔。神经网络可以根据目标迭代间隔确定是否更新量化参数。由于目标迭代间隔所包括的多个迭代的数据变动幅度较小,目标迭代间隔内的迭代不更新量化参数也可以保证量化精度。而目标迭代间隔内的多个迭代不更新量化参数,可以减少量化参数的提取次数或计算次数,从而提高神经网络的运算效率。
在一种可能的实现方式中,该方法还包括:
根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
如本公开上述实施例所述,待量化数据的量化参数可以预设,也可以根据待量化数据对应的数据位宽计算得到。而不同待量化层中待量化数据对应的数据位宽,或相同待量化层中待量化数据在不同迭代中对应的数据位宽,可以根据本公开上述实施例中的方式进行自适应调整。
当待量化数据的数据位宽不可自适应调整,为预设的数据位宽时,可以根据待量化数据在当前迭代的预设的数据位宽,确定待量化数据在目标迭代间隔内的迭代对应的数据位宽。在目标迭代间隔内的各迭代可不使用自身的预设值。
当待量化数据的数据位宽可自适应调整时,可以根据待量化数据在当前迭代对应的数据位宽,确定待量化数据在目标迭代间隔内的迭代对应的数据位宽。在数据位宽可自适应调整时,数据位宽可进行一次调整或多次调整。可以将待量化数据在当前迭代进行自适应调整后的数据位宽,作为目标迭代间隔内的各迭代对应的数据位宽,在目标迭代间隔内的各迭代不再对数据位宽进行自适应调整(更新)。待量化数据在当前迭代可以使用自适应调整后的数据位宽,也可以使用自适应调整前的数据位宽,本公开对此不作限定。
在目标迭代间隔以外的其他迭代,由于待量化数据的数据变动幅度不满足设定条件,可以根据本公开上述的方法对数据位宽进行自适应调整,得到更加符合当前迭代的待量化数据的数据位宽,也可使用本公开中的目标迭代间隔的计算方法,计算得到新的目标迭代间隔并使用,从而在保证目标迭代间隔以外的迭代的量化精度的同时,提高神经网络的运行效率。
在目标迭代间隔内的各迭代的数据位宽相同,各迭代可以根据相同的数据位宽各自计算得到对应的量化参数。量化参数可以包括点位置、缩放系数和偏移量中的至少一种。可以在目标迭代间隔内的各迭代,根据相同的数据位宽分别计算得到量化参数。量化参数包括点位置(包括第一类点位置、第二类点位置)、缩放系数(包括第一类缩放系数和第二类缩放系数)和偏移量时,在目标迭代间隔内的各迭代,可利用相同的数据位宽,分别计算各自对应的点位置、缩放系数和偏移量。
在根据当前迭代的数据位宽,确定目标迭代间隔内各迭代的数据位宽的同时,可以根据当前迭代的量化参数,确定目标迭代间隔内各迭代的对应的量化参数。目标迭代间隔内各迭代的量化参数,也不再重新根据相同的数据位宽计算得到,可以进一步提高神经网络的运算效率。可以根据当前迭代的全部量化参数或部分量化参数,确定目标迭代间隔内各迭代的对应的量化参数。当根据当前迭代的部分量化参数,确定目标迭代间隔内各迭代的对应的量化参数时,剩余部分的量化参数,在目标迭代间隔内各迭代仍需计算。
例如,量化参数包括第二类点位置、第二类缩放系数和偏移量。可以根据当前迭代的数据位宽和第二类点位置,确定目标迭代间隔内各迭代的数据位宽和第二类点位置。则目标迭代间隔内各迭代的第二类缩放系数和偏移量需要根据相同的数据位宽计算得到。也可以根据当前迭代的数据位宽、第二类点位置、第二类缩放系数和偏移量,确定目标迭代间隔内各迭代的数据位宽、第二类点位置、第二类缩放系数和偏移量,则目标迭代间隔内各迭代的各量化参数均不需要计算得到。
在本实施例中,根据待量化数据在当前迭代对应的数据位宽,确定待量化数据在目标迭代间隔内的迭代对应的数据位宽,以使神经网络根据待量化数据在目标迭代间隔内的迭代对应的数据位宽,确定量化参数。在目标迭代间隔内的各迭代的数据位宽,根据当前迭代的数据位宽确定,由于目标迭代间隔内各迭代的待量化数据的数据变化幅度满足设定的条件,利用相同的数据位宽计算得到的量化参数,可以保证目标迭代间隔内的各迭代的量化精度。目标迭代间隔内各迭代使用相同的数据位宽,也可以提高神经网络的运算效率。在对神经网络进行量化后运算结果的准确率和神经网络的运算效率之间,达到平衡。
在一种可能的实现方式中,该方法还可以包括:根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位置。
其中,根据所述待量化数据在所述当前迭代对应的第一类点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的第一类点位置。根据所述待量化数据在所述当前迭代对应的第二类点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的第二类点位置。
在量化参数中,相对于缩放系数和偏移量,不同的点位置对相同待量化数据的量化结果产生的影响较大。可以根据待量化数据在当前迭代对应的点位置,确定目标迭代间隔内的迭代对应的点位置。当数据位宽不可自适应调整时,可以将待量化数据在当前迭代预设的点位置,作为待量化数据在目标迭代间隔内各迭代对应的点位置,也可以将待量化数据在当前迭代根据预设的数据位宽计算得到的点位置,作为待量化数据在目标迭代间隔内各迭代对应的点位置。当数据位宽可自适应调整时,可以将待量化数据在当前迭代调整后的点位置,作为待量化数据在目标迭代间隔内各迭代对应的点位置。
根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置的同时,也可以根据待量化数据在当前迭代对应的缩放系数,确定所述待量化数据在所述目标迭代间隔内的迭代对应的缩放系数,和/或根据待量化数据在当前迭代对应的偏移量,确定所述待量化数据在所述目标迭代间隔内的迭代对应的偏移量。
根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置的同时,还可以根据待量化数据在当前迭代对应的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,其中,待量化数据在当前迭代对应的数据位宽,可以是当前迭代预设的数据位宽或自适应调整后的数据位宽。
在本实施例中,根据待量化数据在当前迭代对应的点位置,确定待量化数据在目标迭代间隔内的迭代对应的点位置。在目标迭代间隔内的各迭代的点位置,根据当前迭代的点位置确定,由于目标迭代间隔内各迭代的待量化数据的数据变化幅度满足设定的条件,利用相同的点位置,可以保证目标迭 代间隔内的各迭代的量化精度。目标迭代间隔内各迭代使用相同的点位置,也可以提高神经网络的运算效率。在对神经网络进行量化后运算结果的准确率和神经网络的运算效率之间,达到平衡。
在一种可能的实现方式中,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,可以包括:
根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
其中,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数,可以包括:
根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
其中,根据待量化数据在当前迭代的第一类点位置和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的第一类点位置,计算待量化数据对应各迭代间隔的第一类点位置的滑动平均值;根据所述待量化数据在当前迭代的第一类点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的第一类点位置的第二滑动平均值,得到所述待量化数据变动幅度。或者,根据待量化数据在当前迭代的第二类点位置和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的第二类点位置,计算待量化数据对应各迭代间隔的第二类点位置的滑动平均值;根据所述待量化数据在当前迭代的第二类点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的第二类点位置的第二滑动平均值,得到所述待量化数据变动幅度。
在一种可能的实现方式中,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代,可以为计算目标迭代间隔的历史迭代。当前迭代与对应的目标迭代间隔之间的对应关系可以包括:
可以从当前迭代开始计数目标迭代间隔,并在当前迭代对应的目标迭代间隔结束后的下一个迭代开始重新计算目标迭代间隔。例如,当前迭代为第100代,目标迭代间隔为3,目标迭代间隔内的迭代包括:第100代、第101代和第102代,可以在第103代计算与第103代对应的目标迭代间隔,并以103代为新计算得到当目标迭代间隔内的第一个迭代。此时,当前迭代为103代时,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代为100代。
可以从当前迭代的下一个迭代开始计数目标迭代间隔,并在目标迭代间隔内的最后一个迭代开始重新计算目标迭代间隔。例如,当前迭代为第100代,目标迭代间隔为3,目标迭代间隔内的迭代包括:第101代、第102代和第103代,可以在第103代计算与第103代对应的目标迭代间隔,并以104代为新计算得到当目标迭代间隔内的第一个迭代。此时,当前迭代为103代时,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代为100代。
可以从当前迭代的下一个迭代开始计数目标迭代间隔,并在目标迭代间隔结束后的下一个迭代开始重新计算目标迭代间隔。例如,当前迭代为第100代,目标迭代间隔为3,目标迭代间隔内的迭代包括:第101代、第102代和第103代,可以在第104代计算与第104代对应的目标迭代间隔,并以105代为新计算得到当目标迭代间隔内的第一个迭代。此时,当前迭代为104代时,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代为100代。
可以根据需求确定当前迭代以及目标迭代间隔之间的其他的对应关系,例如可以从当前迭代之后的第N个迭代开始计数目标迭代间隔,N大于1,本公开对此不作限定。
可以理解的是,计算得到的待量化数据对应各迭代间隔的点位置的滑动平均值,包括待量化数据在当前迭代的点位置的第一滑动平均值,和待量化数据在上一迭代间隔对应迭代的点位置的第二滑动 平均值。可以利用公式(4-27)计算当前迭代对应点位置的第一滑动平均值m (t)
m (t)←α×s (t)+(1-α)×m (t-1)     公式(4-27)
其中,t为当前迭代,t-1为根据上一迭代间隔确定的历史迭代,m (t-1)为根据上一迭代间隔确定的历史迭代的第二滑动平均值。s (t)为当前迭代的点位置,可以为第一类点位置或第二类点位置。α为第一参数。第一参数可以为超参数。
在本实施例中,根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值;根据待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度。根据第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。由于第一数据变动幅度可以用于衡量点位置的变化趋势,使得目标迭代间隔可以跟随待量化数据点位置的变化趋势而变化,也使得计算得到的各目标迭代间隔的大小可以根据待量化数据点位置的变化趋势而变化。由于量化参数根据目标迭代间隔确定,也就使得根据量化参数进行量化得到的量化数据,能够更加符合待量化数据的点位置的变动趋势,在保证量化精度的同时,提高神经网络的运行效率。
在一种可能的实现方式中,所述根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度,可以包括:
计算所述第一滑动平均值和所述第二滑动平均值的差值;
将所述差值的绝对值确定为第一数据变动幅度。
可以利用公式(4-28)计算第一数据变动幅度diff update1
diff update1=|m (t)-m (t-1)|=α|s (t)-m (t-1)|      公式(4-28)
可以根据第一数据变动幅度,确定待量化数据对应的目标迭代间隔,以使神经网络根据目标迭代间隔更新所述待量化数据的量化参数。可以根据公式(4-29)计算得到目标迭代间隔I:
Figure PCTCN2020095673-appb-000039
其中,β为第二参数,γ为第三参数。第二参数和第三参数可以为超参数。
可以理解的是,第一数据变动幅度可以用于衡量点位置的变化趋势,第一数据变动幅度越大,说明量化数据的数值范围变化剧烈,在更新量化参数时需要间隔更短的目标迭代间隔I。
在本实施例中,计算所述第一滑动平均值和所述第二滑动平均值的差值;将差值的绝对值确定为第一数据变动幅度。根据滑动平均值之间的差值可以得到精确的第一数据变动幅度。
在一种可能的实现方式中,该方法还可以包括:根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
其中,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数,可以包括:
根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
可以根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度。也可以根据在当前迭代所述待量化数据和所述待量化数据对应的反量化数据,得到第二数据变动幅度。
同样的,可以根据公式(4-30)计算得到在当前迭代中,所述待量化数据和所述待量化数据对应的反量化数据之间的第二数据变动幅度diff bit。也可以利用其它误差的计算方法,计算待量化数据和反量化数据之间的第二数据变动幅度diff bit。本公开对此不作限定。
Figure PCTCN2020095673-appb-000040
其中,z i为待量化数据,z i (n)为待量化数据对应的反量化数据。可以理解的是,第二数据变动幅度可以用于衡量待量化数据对应的数据位宽的变化趋势,第二数据变动幅度越大,待量化数据越有可能需要更新对应的数据位宽,需要间隔更短的迭代进行更新,则第二数据变动幅度越大,需要目标迭代间隔更小。
在本实施例中,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度。根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。第二数据变动幅度可以用于衡量数据位宽的变动需求,则根据第一数据变动幅度和第二数据变动幅度计算得到的目标迭代间隔,可以同时跟踪点位置和数据位宽的变动,目标迭代间隔也可以更加符合待量化数据自身的数据量化需求。
在一种可能的实现方式中,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度,可以包括:
计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;
将所述误差的平方确定为所述第二数据变动幅度。
可以利用公式(4-31)计算得到第二数据变动幅度diff update2
diff update2=δ*diff bit 2   公式(4-31)
其中,δ为第四参数,第四参数可以为超参数。
可以理解的是,利用不同的数据位宽可以得到不同的量化参数,进而得到不同的量化数据,产生不同的第二数据变动幅度。第二数据变动幅度可以用于衡量数据位宽的变化趋势,第二数据变动幅度越大,说明需要更短的目标迭代间隔来更加频繁的更新数据位宽,即目标迭代间隔需要更小。
在一种可能的实现方式中,根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,可以包括:
根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
可以根据公式(4-32)计算得到目标迭代间隔:
Figure PCTCN2020095673-appb-000041
其中,β为第二参数,γ为第三参数。第二参数和第三参数可以为超参数。
可以理解的是,利用第一数据变动幅度和第二数据变动幅度得到的目标迭代间隔,可以同时衡量数据位宽和点位置的变化趋势,当两者中的其一的变化趋势较大时,便可以使得目标迭代间隔产生相应的变化。目标迭代间隔可以同时追踪数据位宽和点位置的变化并做出相应的调整。使得根据目标迭代间隔更新的量化参数能够更加符合目标数据的变动趋势,最终使得根据量化参数得到的量化数据能够更加符合量化需求。
在一种可能的实现方式中,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,可以包括:
在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
在神经网络运算的训练过程和/或微调过程中,在训练开始或微调开始的多个迭代中,待量化数据的变化幅度较大。若在训练开始或微调开始的多个迭代中计算目标迭代间隔,则计算得到的目标迭代间隔可能会失去其使用的意义。可以根据预设更新周期,在更新周期以内的各迭代,不计算目标迭 代间隔,也不适用目标迭代间隔使得多个迭代使用相同的数据位宽或点位置。
当迭代进行至更新周期以外时,即当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,并根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。例如,预设更新周期为100代,则自第1代开始至第100代的迭代中,不计算目标迭代间隔。当迭代进行至101代,即当前迭代为101代时,当前迭代位于更新周期以外,此时,可以根据第101代以及第1代至第100代的迭代中待量化数据的数据变动幅度,确定第101代的待量化数据对应的目标迭代间隔,并在第101代或与第101间隔预设代数的迭代中,使用计算得到的目标迭代间隔。
可以自预设的代数开始计数更新周期,例如可以自第一代开始计数更新周期中多个迭代,也可以自第N代开始计数更新周期中的多个迭代,本公开对此不作限定。
在本实施例中,当迭代进行至更新周期以外时计算并使用目标迭代间隔。可以避免在神经网络运算的训练过程或微调过程的初期,由于待量化数据的变动幅度较大引起的目标迭代间隔使用意义不大的问题,可以在使用目标迭代间隔的情况下,进一步提高神经网络的运行效率。
在一种可能的实现方式中,该方法还可以包括:
在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
在神经网络运算的训练过程或微调过程中,可以包括多个周期。各周期可以包括多个迭代。用于神经网络运算的数据被完整的运算一遍为一个周期。在训练过程中,随着迭代的进行,神经网络的权值变化趋于稳定,当训练稳定后,神经元、权值、偏置和梯度等待量化数据均趋于稳定。待量化数据趋于稳定后,待量化数据的数据位宽和量化参数也趋于稳定。同理,在微调过程中,微调稳定后,待量化数据的数据位宽和量化参数也趋于稳定。
因此,可以根据训练稳定或微调稳定的周期确定预设周期。可以将训练稳定或微调稳定所在周期以后的周期,确定为预设周期。例如训练稳定的周期为第M个周期,则可以将第M个周期以后的周期作为预设周期。在预设周期内,可以每间隔一个周期计算一个目标迭代间隔,并根据计算得到的目标迭代间隔调整一次数据位宽或量化参数,以减少数据位宽或量化参数的更新次数,提高神经网络的运行效率。
例如,预设周期为第M个周期以后的周期。在第M+1个周期中,根据第M个周期中的第P个迭代计算得到的目标迭代间隔,截止至第M+1个周期中的第Q个迭代。根据第M+1个周期中的第Q m+1个迭代计算得到与之对应的目标迭代间隔I m+1。在第M+2个周期中,与第M+1个周期中的第Q m+1个迭代对应的迭代为第Q m+2个迭代。在自第M+1个周期中的第Q m+1个迭代开始,直至第M+2个周期中第Q m+2+I m+1个迭代为止,为周期间隔。在周期间隔内的各迭代,均采用第M+1个周期中的第Q m+1个迭代确定的数据位宽或点位置等量化参数。
在本实施例中,可以设置周期间隔,在神经网络运算的训练或微调达到稳定后,根据周期间隔,每周期更新一次数据位宽或点位置等量化参数。周期间隔可以在训练稳定或微调稳定后,减少数据位宽或点位置的更新次数,在保证量化精度的同时,提高神经网络的运行效率。
需要说明的是,对于前述的各方法实施例,为了简单描述,故将其都表述为一系列的动作组合,但是本领域技术人员应该知悉,本公开并不受所描述的动作顺序的限制,因为依据本公开,某些步骤 可以采用其他顺序或者同时进行。其次,本领域技术人员也应该知悉,说明书中所描述的实施例均属于可选实施例,所涉及的动作和模块并不一定是本公开所必须的。
进一步需要说明的是,虽然图2-1、图2-4的流程图中的各个步骤按照箭头的指示依次显示,但是这些步骤并不是必然按照箭头指示的顺序依次执行。除非本文中有明确的说明,这些步骤的执行并没有严格的顺序限制,这些步骤可以以其它的顺序执行。而且,图2-1、图2-4中的至少一部分步骤可以包括多个子步骤或者多个阶段,这些子步骤或者阶段并不必然是在同一时刻执行完成,而是可以在不同的时刻执行,这些子步骤或者阶段的执行顺序也不必然是依次进行,而是可以与其它步骤或者其它步骤的子步骤或者阶段的至少一部分轮流或者交替地执行。
本公开实施例还提供了一种非易失性计算机可读存储介质,其上存储有计算机程序指令,计算机程序指令被处理器执行时实现上述神经网络的数据量化处理方法。
图2-5示出根据本公开一实施例的神经网络量化装置的框图。如图2-5所示,该装置应用于包含如图1所示的处理器100的终端,终端用于运行神经网络以执行目标任务,该装置包括数据确定模块4-61、数据量化模块4-62和结果确定模块4-63。其中,某一个处理单元101中设置有数据确定模块4-61、数据量化模块4-62和结果确定模块4-63。或者,数据确定模块4-61、数据量化模块4-62和结果确定模块4-63分别设置在不同的处理单元101中。存储单元102用于存储待量化数据、量化参数、数据位宽等与数据确定模块4-61、数据量化模块4-62和结果确定模块4-63的运行相关的数据。
数据确定模块4-61,根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种;
数据量化模块4-62,将各所述待量化数据分别根据对应的量化参数进行量化,得到与各所述待量化数据对应的量化数据;
结果确定模块4-63,根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
在一种可能的实现方式中,所述数据确定模块,包括以下至少一个子模块:
第一确定子模块,根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的目标数据确定为一个待量化数据;
第二确定子模块,根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的同一种待运算数据确定为一个待量化数据;
第三确定子模块,根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个通道中的数据确定为一个待量化数据;
第四确定子模块,根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个批数的数据确定为一个待量化数据;
第五确定子模块,根据所述目标任务和/或所述终端的精度需求确定划分数据的划分尺寸,将对应待量化层中的目标数据按照所述划分尺寸划分为一个或多个待量化数据。
在一种可能的实现方式中,所述装置还包括:
数据位宽确定模块,根据所述目标任务和/或所述终端的精度需求,确定待量化数据对应的数据位宽;
参数确定模块,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
在一种可能的实现方式中,所述参数确定模块,包括:
第一点位置确定子模块,当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值和对应的数据位宽,得到各所述待量化数据的第一类点位置。
在一种可能的实现方式中,所述参数确定模块,包括:
第一最大值确定子模块,当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
第一缩放系数确定子模块,根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数。
在一种可能的实现方式中,所述参数确定模块,包括:
第二点位置确定子模块,当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
在一种可能的实现方式中,所述参数确定模块,包括:
第二最大值确定子模块,当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
第一缩放系数确定子模块,根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。
在一种可能的实现方式中,所述参数确定模块,包括:
偏移量确定子模块,根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。
在一种可能的实现方式中,所述装置还包括:
第一量化误差确定模块,根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
调整位宽确定模块,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
调整量化参数确定模块,将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
在一种可能的实现方式中,所述调整位宽确定模块,包括:
第一调整位宽确定子模块,当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
在一种可能的实现方式中,所述装置还包括:
第一调整后量化误差模块,根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
第一调整位宽循环确定模块,根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
在一种可能的实现方式中,所述调整位宽确定模块,包括:
第二调整位宽确定子模块,当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值。
在一种可能的实现方式中,所述装置还包括:
第二调整后量化误差模块,根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
第二调整位宽循环确定模块,根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
在一种可能的实现方式中,在所述神经网络运算的微调阶段和/或训练阶段,所述装置还包括:
第一数据变动幅度确定模块,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
目标迭代间隔确定模块,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
在一种可能的实现方式中,所述装置还包括:
第一目标迭代间隔应用模块,根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
在一种可能的实现方式中,所述装置还包括:
第二目标迭代间隔应用模块,根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位置。
在一种可能的实现方式中,所述第一数据变动幅度确定模块,包括:
滑动平均值计算子模块,根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
第一数据变动幅度确定子模块,根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
其中,所述目标迭代间隔确定模块,包括:
第一目标迭代间隔确定子模块,根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
在一种可能的实现方式中,所述第一数据变动幅度确定子模块,包括:
第一幅度确定子模块,计算所述第一滑动平均值和所述第二滑动平均值的差值;将所述差值的绝对值确定为第一数据变动幅度。
在一种可能的实现方式中,所述装置还包括:
第二数据变动幅度确定模块,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
其中,目标迭代间隔确定模块,包括:
第二目标迭代间隔确定子模块,根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
在一种可能的实现方式中,所述第二数据变动幅度确定模块,包括:
第二幅度确定子模块,计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;将所述误差的平方确定为所述第二数据变动幅度。
在一种可能的实现方式中,所述第二目标迭代间隔确定子模块,包括:
间隔确定子模块,根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
在一种可能的实现方式中,所述第一数据变动幅度确定模块,包括:
第二数据变动幅度确定子模块,在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中 待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
在一种可能的实现方式中,所述装置还包括:
周期间隔确定模块,在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
第一周期间隔应用模块,根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
第二周期间隔应用模块,根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
本公开实施例所提供的神经网络量化装置,根据目标任务和/或终端的精度需求从目标数据中确定多个待量化数据,利用对应的量化参数对目标数据中的多个待量化数据分别进行量化,在保证精度的同时,减小了存储数据所占用的存储空间,保证了运算结果的准确性和可靠性,且能够提高运算的效率,且量化同样缩减了神经网络模型的大小,降低了对运行该神经网络模型的终端的性能要求,使神经网络模型可以应用于算力、体积、功耗相对受限的手机等终端。
在一种可能的实现方式中,还公开了一种非易失性计算机可读存储介质,其上存储有计算机程序指令,所述计算机程序指令被处理器执行时实现上述神经网络量化方法。
依据以下条款可更好地理解前述内容:
条款D1.一种神经网络量化方法,应用于终端,所述终端用于运行神经网络以执行目标任务,所述方法包括:
根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种;
将各所述待量化数据分别根据对应的量化参数进行量化,得到与各所述待量化数据对应的量化数据;
根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
条款D2.根据条款D1所述的方法,根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,包括以下至少一项:
根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的目标数据确定为一个待量化数据;
根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的同一种待运算数据确定为一个待量化数据;
根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个通道中的数据确定为一个待量化数据;
根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个批数的数据确定为一个待量化数据;
根据所述目标任务和/或所述终端的精度需求确定划分数据的划分尺寸,将对应待量化层中的目标数据按照所述划分尺寸划分为一个或多个待量化数据。
条款D3.根据条款D1或条款D2所述的方法,所述方法还包括:
根据所述目标任务和/或所述终端的精度需求,确定待量化数据对应的数据位宽;
根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
条款D4.根据条款D3所述的方法,所述根据各所述待量化数据和对应的数据位宽计算得到对应的 量化参数,包括:
当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值和对应的数据位宽,得到各所述待量化数据的第一类点位置。
条款D5.根据条款D3所述的方法,所述根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数。
条款D6.根据条款D3所述的方法,所述根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
条款D7.根据条款D3所述的方法,所述根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。
条款D8.根据条款D3所述的方法,所述根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。
条款D9.根据条款D1至条款D8中任一项所述的方法,所述方法还包括:
根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
条款D10.根据条款D9所述的方法,所述根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽,包括:
当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
条款D11.根据条款D9或条款D 10所述的方法,所述方法还包括:
根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
条款D12.根据条款D9或条款D 10所述的方法,所述根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽,包括:
当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值。
条款D13.根据条款D12所述的方法,所述方法还包括:
根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
条款D14.根据条款D1至条款D13中任一项所述的方法,在所述神经网络运算的微调阶段和/或训练阶段,所述方法还包括:
获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
条款D15.根据条款D14所述的方法,所述方法还包括:
根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
条款D16.根据条款D15所述的方法,所述方法还包括:
根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位置。
条款D17.根据条款D14所述的方法,所述获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
所述根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数,包括:
根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款D18.根据条款D17所述的方法,所述根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度,包括:
计算所述第一滑动平均值和所述第二滑动平均值的差值;
将所述差值的绝对值确定为第一数据变动幅度。
条款D19.根据条款D18所述的方法,所述方法还包括:
根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
所述根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数,包括:
根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款D20.根据条款D19所述的方法,所述根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度,包括:
计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;
将所述误差的平方确定为所述第二数据变动幅度。
条款D21.根据条款D19所述的方法,所述根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,包括:
根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
条款D22.根据条款D14至条款D21中任一项所述的方法,所述获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
条款D23.根据条款D14至条款D22中任一项所述的方法,所述方法还包括:
在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
条款D24.一种神经网络量化装置,应用于终端,所述终端用于运行神经网络以执行目标任务,所述装置包括:
数据确定模块,根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种;
数据量化模块,将各所述待量化数据分别根据对应的量化参数进行量化,得到与各所述待量化数据对应的量化数据;
结果确定模块,根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
条款D25.根据条款D24所述的装置,所述数据确定模块,包括以下至少一个子模块:
第一确定子模块,根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的目标数据确定为一个待量化数据;
第二确定子模块,根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的同一种待运算数据确定为一个待量化数据;
第三确定子模块,根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个通道中的数据确定为一个待量化数据;
第四确定子模块,根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个批数的数据确定为一个待量化数据;
第五确定子模块,根据所述目标任务和/或所述终端的精度需求确定划分数据的划分尺寸,将对应待量化层中的目标数据按照所述划分尺寸划分为一个或多个待量化数据。
条款D26.根据条款D24或条款D 25所述的装置,所述装置还包括:
数据位宽确定模块,根据所述目标任务和/或所述终端的精度需求,确定待量化数据对应的数据位宽;
参数确定模块,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
条款D27.根据条款D26所述的装置,所述参数确定模块,包括:
第一点位置确定子模块,当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最 大值和对应的数据位宽,得到各所述待量化数据的第一类点位置。
条款D28.根据条款D26所述的装置,所述参数确定模块,包括:
第一最大值确定子模块,当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
第一缩放系数确定子模块,根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数。
条款D29.根据条款D26所述的装置,所述参数确定模块,包括:
第二点位置确定子模块,当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
条款D30.根据条款D26所述的装置,所述参数确定模块,包括:
第二最大值确定子模块,当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
第一缩放系数确定子模块,根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。
条款D31.根据条款D26所述的装置,所述参数确定模块,包括:
偏移量确定子模块,根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。
条款D32.根据条款D24至条款D31任一项所述的装置,所述装置还包括:
第一量化误差确定模块,根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
调整位宽确定模块,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
调整量化参数确定模块,将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
条款D33.根据条款D32所述的装置,所述调整位宽确定模块,包括:
第一调整位宽确定子模块,当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
条款D34.根据条款D32或条款D33所述的装置,所述装置还包括:
第一调整后量化误差模块,根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
第一调整位宽循环确定模块,根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
条款D35.根据条款D32或条款D33所述的装置,所述调整位宽确定模块,包括:
第二调整位宽确定子模块,当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值。
条款D36.根据条款D35所述的装置,所述装置还包括:
第二调整后量化误差模块,根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
第二调整位宽循环确定模块,根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈 值。
条款D37.根据条款D24至条款D36任一项所述的装置,在所述神经网络运算的微调阶段和/或训练阶段,所述装置还包括:
第一数据变动幅度确定模块,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
目标迭代间隔确定模块,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
条款D38.根据条款D37所述的装置,所述装置还包括:
第一目标迭代间隔应用模块,根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
条款D39.根据条款D38所述的装置,所述装置还包括:
第二目标迭代间隔应用模块,根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位置。
条款D40.根据条款D37所述的装置,所述第一数据变动幅度确定模块,包括:
滑动平均值计算子模块,根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
第一数据变动幅度确定子模块,根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
其中,所述目标迭代间隔确定模块,包括:
第一目标迭代间隔确定子模块,根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款D41.根据条款D40所述的装置,所述第一数据变动幅度确定子模块,包括:
第一幅度确定子模块,计算所述第一滑动平均值和所述第二滑动平均值的差值;将所述差值的绝对值确定为第一数据变动幅度。
条款D42.根据条款D41所述的装置,所述装置还包括:
第二数据变动幅度确定模块,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
其中,目标迭代间隔确定模块,包括:
第二目标迭代间隔确定子模块,根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款D43.根据条款D42所述的装置,所述第二数据变动幅度确定模块,包括:
第二幅度确定子模块,计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;将所述误差的平方确定为所述第二数据变动幅度。
条款D44.根据条款D42所述的装置,所述第二目标迭代间隔确定子模块,包括:
间隔确定子模块,根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
条款D45.根据条款D37至条款D44任一项所述的装置,所述第一数据变动幅度确定模块,包括:
第二数据变动幅度确定子模块,在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
条款D46.根据条款D37至条款D45任一项所述的装置,所述装置还包括:
周期间隔确定模块,在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
第一周期间隔应用模块,根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
第二周期间隔应用模块,根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
条款D47.一种人工智能芯片,所述芯片包括如条款D24至条款D46中任意一项所述的神经网络量化装置。
条款D48.一种电子设备,所述电子设备包括如条款D47所述的人工智能芯片。
条款D49.一种板卡,所述板卡包括:存储器件、接口装置和控制器件以及如条款D47所述的人工智能芯片;
其中,所述人工智能芯片与所述存储器件、所述控制器件以及所述接口装置分别连接;
所述存储器件,用于存储数据;
所述接口装置,用于实现所述人工智能芯片与外部设备之间的数据传输;
所述控制器件,用于对所述人工智能芯片的状态进行监控。
条款D50.根据条款D49所述的板卡,
所述存储器件包括:多组存储单元,每一组所述存储单元与所述人工智能芯片通过总线连接,所述存储单元为:DDR SDRAM;
所述芯片包括:DDR控制器,用于对每个所述存储单元的数据传输与数据存储的控制;
所述接口装置为:标准PCIE接口。
条款D51.一种非易失性计算机可读存储介质,其上存储有计算机程序指令,所述计算机程序指令被处理器执行时实现条款D1至条款D23中任意一项所述的神经网络量化方法。
随着神经网络运算复杂度的提高,数据的数据量和数据维度也在不断增大,而传统的神经网络算法通常采用浮点数据格式来执行神经网络运算,这就使得不断增大的数据量等对运算装置的数据处理效率、存储装置的存储容量及访存效率等提出了较大的挑战。为解决上述问题,相关技术中,对神经网络运算过程涉及的全部数据均由浮点数转化定点数,但由于不同的数据之间具有差异性,或者,同一数据在不同阶段具有差异性,仅“由浮点数转化定点数”时,往往会导致精度不够,从而会影响运算结果。
神经网络中待运算数据通常为浮点数据格式或精度较高的定点数据格式,在承载神经网络的装置中运行神经网络时,浮点数据格式或精度较高的定点数据格式的各种待运算数据,导致神经网络运行的运算量和访存开销都较大。为提高运算效率,本公开实施例所提供的神经网络量化方法、装置、计算机设备和存储介质,可以根据不同类待运算数据进行神经网络中的待运算数据的局部量化,量化后的数据格式通常为位宽较短、精度较低的定点数据格式。利用精度较低的量化后数据执行神经网络的运算,可以降低运算量和访存量。量化后的数据格式可以为位宽较短的定点数据格式。可以将浮点数据格式的待运算数据量化为定点数据格式的待运算数据,也可以将精度较高的定点格式的待运算数据量化为精度较低的定点格式的待运算数据。利用对应的量化参数对数据进行局部量化,在保证精度的 同时,减小了存储数据所占用的存储空间,保证了运算结果的准确性和可靠性,且能够提高运算的效率,且量化同样缩减了神经网络模型的大小,降低了对运行该神经网络模型的终端的性能要求,使神经网络模型可以应用于算力、体积、功耗相对受限的手机等终端。
可以理解的是,量化精度即量化后数据与量化前数据之间的误差的大小。量化精度可以影响神经网络运算结果的准确度。化精度越高,运算结果的准确率越高,但运算量更大、访存开销也更大。相较于位宽较短的量化后数据,位宽较长的量化后数据的量化精度更高,用于执行神经网络的运算时准确率也更高。但在用于进行神经网络的运算时,位宽较长的量化后数据运算量更大、访存开销也较大,运算效率较低。同理,对于相同的待量化数据,采用不同的量化参数得到的量化后数据有不同的量化精度,将产生不同的量化结果,对运算效率和运算结果的准确率也会带来不同的影响。对神经网络进行量化,在运算效率和运算结果的准确率之间进行平衡,可以采用更加符合待运算数据的数据特征的量化后数据位宽和量化参数。
神经网络中的待运算数据可以包括权值、神经元、偏置、梯度中的至少一种。待运算数据为包含多个元素的矩阵。在传统的神经网络量化中,通常将待运算数据的整体进行量化后进行运算。而在利用量化后的待运算数据进行运算时,通常利用整体量化后的待运算数据中的一部分数据进行运算。例如,在卷积层,利用整体量化后的输入神经元进行卷积运算时,根据卷积核的维度和步长,在整体量化后的输入神经元中分别提取与卷积核的维度相当的量化后的神经元进行卷积运算。在全连接层,利用整体量化后的输入神经元进行矩阵乘运算时,在整体量化后的输入神经元中分别按行提取量化后的神经元进行矩阵乘的运算。因此,在传统的神经网络量化方法中,将待运算数据的整体进行量化后再按照部分量化后的数据进行运算,整体的运算效率较低。且将待运算数据的整体量化后再进行运算,需要将整体量化后的待运算数据进行存储,占用的存储空间较大。
根据本公开实施例的神经网络量化方法可应用于处理器中,该处理器可以是通用处理器,例如CPU(Central Processing Unit,中央处理器),也可以是用于执行人工智能运算的人工智能处理器(IPU)。人工智能运算可包括机器学习运算,类脑运算等。其中,机器学习运算包括神经网络运算、k-means运算、支持向量机运算等。该人工智能处理器可例如包括GPU(Graphics Processing Unit,图形处理单元)、NPU(Neural-Network Processing Unit,神经网络处理单元)、DSP(Digital Signal Process,数字信号处理单元)、现场可编程门阵列(Field-Programmable Gate Array,FPGA)芯片中的一种或组合。本公开对处理器的具体类型不作限制。
在一种可能的实现方式中,本公开中所提及的处理器可包括多个处理单元,每个处理单元可以独立运行所分配到的各种任务,如:卷积运算任务、池化任务或全连接任务等。本公开对处理单元及处理单元所运行的任务不作限制。
图3-1示出根据本公开实施例的神经网络量化装置的框图。如图3-1所示,该装置可以包括控制模块11和处理模块12。处理模块12可以包括第一运算子模块121,第一运算子模块121包括主运算子模块1210和从运算子模块1211。
所述控制模块11,用于从神经网络的目标数据中确定多个待量化数据,并根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,各所述待量化数据的量化数据是利用对应的量化参数进行量化得到的,所述量化参数包括点位置。
所述第一运算子模块121,用于进行与所述量化结果相关的运算,得到运算结果,其中,
所述主运算子模块1210,用于将第一数据发送至所述从运算子模块,所述第一数据包括所述量化结果中根据所述点位置量化得到的第一类型的数据。
所述从运算子模块1211,用于对接收到的所述第一数据进行乘法运算,得到中间结果。
所述主运算子模块1210,还用于对所述中间结果和所述量化结果中的除所述第一数据之外的数据 进行运算,得到运算结果。
可以根据目标任务的任务类型、所需使用的待运算数据的数量、每个待运算数据的数据量,以及基于终端的计算精度、当前处理能力、存储能力等确定的精度需求,待运算数据所参与的运算类型等,确定从目标数据中确定出多个待量化数据的方式。
神经网络中的待量化层可以为神经网络中的任意一层。可以根据需求将神经网络中的部分层或全部层确定为待量化层。当神经网络中包括多个待量化层时,各待量化层可以连续也可以不连续。根据神经网络的不同,待量化层的种类也可以不同,例如待量化层可以为卷积层、全连接层等,本公开对待量化层的数量及类型不作限定。
在一种可能的实现方式中,所述待运算数据包括神经元、权值、偏置、梯度中的至少一种。可以根据需求将待量化层中的神经元、权值、偏置、梯度中的至少一种进行量化。目标数据为任意一种待量化的待运算数据。例如,待运算数据为神经元、权值和偏置,需要将神经元和权值进行量化,则神经元为目标数据1,权值为目标数据2。
当待量化层中有多种目标数据时,针对每种目标数据可以采用本公开中的量化方法进行量化后,得到与各目标数据对应的量化数据,再利用各种目标数据的量化数据和不需要进行量化的待运算数据执行待量化层的运算。
在一种可能的实现方式中,所述量化参数还可以包括偏移量和/或缩放系数,所述量化结果中还包括第二类型的数据,所述第二类型的数据包括用点位置表示的第一部分、以及用偏移量和/或缩放系数表示的第二部分的数据,所述第一数据还可以包括所述量化结果中第二类型的数据的第一部分。
在该实现方式中,量化结果为定点数,可以采用下述数据格式表示:
第一类型的数据为:Fixed_Style 1=I×2 S
第二类型的数据表示为:Fixed_Style 2=I×2 S×f+O
其中,I为定点数、是量化数据(或量化结果)的n为二进制表示值,s为点位置(包括下文所述的第一类点位置和第二类点位置),f为缩放系数(包括下文所述的第一类缩放系数和第二类缩放系数),o为偏移量。
其中,第二类型的数据的第一部分为I×2S,第二类型的数据的第二部分为f和o。
神经网络运算的推理阶段可包括:将训练好的神经网络进行前向运算以完成设定任务的阶段。在神经网络的推理阶段,可以将神经元、权值、偏置和梯度中的至少一种作为待量化数据,根据本公开实施例中的方法进行量化后,利用量化后的数据完成待量化层的运算。
神经网络运算的微调阶段可包括:将训练好的神经网络进行预设数量迭代的前向运算和反向运算,进行参数的微调以适应设定任务的阶段。在神经网络运算的微调阶段,可以将神经元、权值、偏置、梯度中的至少一种,根据本公开实施例中的方法进行量化后,利用量化后的数据完成待量化层的前向运算或反向运算。
神经网络运算的训练阶段可包括:将初始化的神经网络进行迭代训练以得到训练好的神经网络的阶段,训练好的神经网络可执行特定任务。在神经网络的训练阶段,可以将神经元、权值、偏置、梯度中的至少一种,根据本公开实施例中的方法进行量化后,利用量化后的数据完成待量化层的前向运算或反向运算。
可以将一个目标数据中的子集作为待量化数据,可以按照不同的方式将目标数据划分为多个子集,将每个子集作为一个待量化数据。将一个目标数据划分为多个待量化数据。可以根据目标数据所要进行的运算类型,将目标数据划分为多个待量化数据。例如,目标数据需要进行卷积运算时,可以根据卷积核的高度和宽度,将目标数据划分为与卷积核对应的多个待量化数据。目标数据为需要进行矩阵乘运算的左矩阵时,可以将目标数据按行划分为多个待量化数据。可以一次将目标数据划分为多个待 量化数据,也可以按照运算的顺序,将目标数据依次划分为多个待量化数据。
也可以根据预先设定的数据划分方式,将目标数据划分为多个待量化数据。例如,预先设定的数据划分方式可以为:按照固定的数据大小进行划分,或按照固定的数据形状进行划分。
将目标数据划分为多个待量化数据后,可以将各待量化数据分别进行量化,并根据各待量化数据量化后的数据进行运算。一个待量化数据所需的量化时间,短于目标数据的整体的量化时间,将其中一个待量化数据量化完毕后,即可以用量化后的数据执行后续的运算,而不用等目标数据中的所有待量化数据均量化完成后再执行运算。因此本公开中的目标数据的量化方法,可以提高目标数据的运算效率。
待量化数据对应的量化参数可以为一个量化参数,也可以为多个量化参数。量化参数可以包括点位置等用于对待量化数据进行量化的参数。点位置可以用于确定量化后数据中小数点的位置。量化参数还可以包括缩放系数、偏移量等。
确定与待量化数据对应的量化参数的方式,可以包括:确定与目标数据对应的量化参数后,并将与目标数据对应的量化参数确定为待量化数据的量化参数的方式。当待量化层包括多个目标数据时,各目标数据均可以有与之对应的量化参数,且各目标数据对应的量化参数可以不同,也可以相同,本公开对此不作限定。将目标数据划分为多个待量化数据后,可以将目标数据对应的量化参数确定为各待量化数据对应的量化参数,此时各待量化数据对应的量化参数相同。
确定与待量化数据对应的量化参数的方式,也可以包括:直接确定各待量化数据对应的量化参数的方式。目标数据可以没有与之对应的量化参数,或目标数据可以有与之对应的量化参数但待量化数据不采用。可以直接为各待量化数据设定对应的量化参数。也可以根据待量化数据计算得到对应的量化参数。此时各待量化数据对应的量化参数可以相同也可以不同。例如,当待量化层为卷积层,目标数据为权重时,可以将权重按照通道划分为多个待量化权重数据,不同通道的待量化权重数据可以对应不同的量化参数。当各待量化数据对应的量化参数不同时,各待量化数据利用对应的量化参数进行量化后,所得到的量化结果需不影响目标数据的运算。
确定与目标数据对应的量化参数的方式、或确定与待量化数据对应的量化参数的方式,可以包括:查找预设的量化参数直接确定量化参数的方式、查找对应关系以确定量化参数的方式,或根据待量化数据计算得到量化参数的方式。以下以确定与待量化数据对应的量化参数的方式为例进行说明:
可以直接设定与待量化数据对应的量化参数。可以将设定好的量化参数存储于设定的存储空间。设定的存储空间可以为片上或片外的存储空间。例如,可以将设定好的量化参数存储于设定的存储空间。各待量化数据在进行量化时,可以在设定的存储空间提取对应的量化参数后进行量化。可以根据经验值设定与每种待量化数据对应的量化参数。也可以根据需求更新所存储的与每种待量化数据对应的量化参数。
可以根据各待量化数据的数据特征,通过查找数据特征与量化参数的对应关系,确定量化参数。例如,待量化数据的数据分布为稀疏和稠密时可以分别对应不同的量化参数。可以通过查找对应关系确定与待量化数据的数据分布对应的量化参数。
还可以根据各待量化数据,利用设定的量化参数计算方法,计算得到各待量化层对应的量化参数。例如,可以根据待量化数据的绝对值最大值和预设的数据位宽,利用取整算法计算得到量化参数中的点位置。
可以利用设定的量化算法,根据量化参数对待量化数据进行量化,得到量化数据。例如,可以利用取整算法作为量化算法,可以根据数据位宽和点位置对待量化数据进行取整量化得到量化数据。其中,取整算法可以包括向上取整、向下取整、向零取整和四舍五入取整等。本公开对量化算法的具体实现方式不作限定。
各待量化数据可以分别采用对应的量化参数进行量化。由于与各待量化数据对应的量化参数更为贴合各待量化数据自身的特征,使得各待量化层的每种量化数据的量化精度更加符合目标数据的运算需求,也就更加符合待量化层的运算需求。在保证待量化层的运算结果准确率的前提下,能够提高待量化层的运算效率,达到待量化层的运算效率和运算结果准确率之间的平衡。进一步的,将目标数据划分为多个待量化数据分别量化,可以在量化完一个待量化数据后,根据量化得到的量化结果执行运算的同时,可以进行第二个待量化数据的量化,从而在整体上提高目标数据的运算效率,也就提高了待量化层的计算效率。
可以将各待量化数据的量化数据进行合并后得到目标数据的量化结果。也可以将各待量化数据的量化数据进行设定的运算后得到目标数据的量化结果。例如可以将各待量化数据的量化数据按照设定的权重进行加权运算后得到目标数据的量化结果。本公开对此不作限定。
在神经网络的推理、训练和微调过程中,可以对待量化数据进行离线量化或在线量化。其中,离线量化可以为利用量化参数对待量化数据进行离线处理。在线量化可以为利用量化参数对待量化数据进行在线处理。例如,神经网络运行在人工智能芯片上,可以将待量化数据和量化参数发送至人工智能芯片之外的运算装置进行离线量化,或利用人工智能芯片之外的运算装置对预先得到的待量化数据和量化参数进行离线量化。而在人工智能芯片运行神经网络的过程中,人工智能芯片可以对待量化数据利用量化参数进行在线量化。本公开中对各待量化数据的量化过程为在线或离线不作限定。
在本实施例所提供的神经网络量化方法,控制模块将目标数据划分为多个待量化数据后,根据与各待量化数据对应的量化数据得到目标数据的量化结果,利用第一运算子模块进行与所述量化结果相关的运算,得到运算结果。其中,利用主运算子模块向从运算子模块发送第一数据,利用从运算子模块对接收到的所述第一数据进行乘法运算得到中间结果,利用所述主运算子模块对所述中间结果和所述量化结果中的除所述第一数据之外的数据进行运算,得到运算结果。各待量化数据的量化过程与主运算子模块、从运算子模块的运算过程可以并行执行,从而可以提高目标数据的量化效率和运算效率,也可以提高待量化层直至提高整个神经网络的量化效率和运算效率。
在一种可能的实现方式中,在对目标数据进行量化的过程中可以采用与目标数据对应的量化参数进行量化。而将目标数据划分为多个待量化数据后,可以采用与各待量化数据对应的量化参数进行量化。与各待量化数据对应的量化参数,可以采用预设的方式或根据待量化数据计算的方式,无论采用何种方式确定与各待量化数据对应的量化参数,都可以使得各待量化数据的量化参数,更加符合待量化数据自身的量化需求。例如,当根据目标数据计算得到对应的量化参数时,可以利用目标数据中各元素的最大值和最小值计算得到量化参数。而在根据待量化数据计算得到对应的量化参数时,可以利用待量化数据中各元素的最大值和最小值计算得到量化参数,待量化数据的量化参数比目标数据的量化参数能够更加贴合待量化数据的数据特征,可以使得待量化数据的量化结果更加准确,量化精度更高。
在一种可能的实现方式中,如图3-1所示,处理模块12还可以包括数据转换子模块122。
数据转换子模块122,用于对待转换数据进行格式转换,得到转换后数据,所述转换后数据的格式类型包括第一类型和第二类型中的任一种,其中,所述待转换数据包括所述目标数据中未进行量化处理的数据,所述第一数据还包括第一类型的转换后数据和/或第二类型的转换后数据中的第一部分,
所述主运算子模块1210,还用于对所述中间结果、所述量化结果中的除所述第一数据之外的数据和所述转换后数据中的除所述第一数据之外的数据进行运算,得到运算结果。
在该实现方式中,待转换数据还可以包括其余数据格式与第一类型和第二类型均不相同的、且需与量化结果共同进行乘法运算的数据,本公开对此不作限制。
举例来说,假定需要进行乘法运算的是数据Fixed1和数据Fixed2,其中,
Figure PCTCN2020095673-appb-000042
Figure PCTCN2020095673-appb-000043
当将数据Fixed1与数据Fixed2进行乘法运算时,
Figure PCTCN2020095673-appb-000044
Figure PCTCN2020095673-appb-000045
主运算子模块可以将Fixed 1和Fixed 2作为第一数据发送至从运算子模块,使得从运算子模块实现Fixed 1和Fixed 2的乘法运算,得到中间结果。
假定需要进行乘法运算的是数据Fixed 1和数据FP 3
Figure PCTCN2020095673-appb-000046
当将数据Fixed 1和数据FP 3进行乘法运算时,Fixed 1×FP 3=f 3×Fixed 1×Fixed 3+Fixed 1×O 3。主运算子模块可以将Fixed 1和Fixed 3作为第一数据发送至从运算子模块,使得从运算子模块实现Fixed 1和Fixed 3的乘法运算,得到中间结果。
假定需要进行乘法运算的是数据FP 4和数据FP 5
Figure PCTCN2020095673-appb-000047
Figure PCTCN2020095673-appb-000048
当将数据FP 4和数据FP 5进行乘法运算时,FP 4×FP 5=f 4×f 5×Fixed 4×Fixed 5+Fixed 4×f 4×o 5+Fixed 5×f 5×o 4+o 4×o 5。主运算子模块可以将Fixed 4和Fixed 5作为第一数据发送至从运算子模块,使得从运算子模块实现Fixed 4和Fixed 5的乘法运算,得到中间结果。
在一种可能的实现方式中,量化结果可以是以第一类型或者第二类型的格式表示,第一运算子模块可以直接对量化结果进行运算。量化结果也可以是未转化为第一类型或者第二类型的待转换量化结果进行格式转换后得到的,其中,所述数据转换子模块122,还用于对根据与各所述待量化数据对应的量化数据得到所述目标数据的待转换量化结果进行格式转换,得到所述量化结果。
在一种可能的实现方式中,控制模块采用以下至少一种方式(也即方式5-1-方式5-5)确定多个待量化数据。
方式5-1:将一层或多层待量化层中的目标数据确定为一个待量化数据。
在神经网络包括多个待量化层时,可以根据目标任务和终端的精度需求确定终端每一次所能量化的数据的量化数据量,进而根据不同量化层中目标数据的数据量以及量化数据量,将一个或多个待量化层中的目标数据确定为一个待量化数据。例如,将一个待量化层中的输入神经元确定为一个待量化数据。
方式5-2:将一层或多层待量化层中的同一种待运算数据确定为一个待量化数据。
在神经网络包括多个待量化层时,可以根据目标任务和终端的精度需求确定终端每一次所能量化的数据的量化数据量,进而根据不同量化层中目标数据的数据量以及量化数据量,将一个或多个待量化层中的某一种目标数据确定为一个待量化数据。例如,将全部待量化层中的输入神经元确定为一个待量化数据。
方式5-3:将对应待量化层的目标数据中一个或多个通道中的数据确定为一个待量化数据。
在待量化层为卷积层时,待量化层中含有通道,可以根据通道以及根据目标任务和终端的精度需求所确定的终端每一次所能量化的数据的量化数据量,将一个或多个通道中的数据确定为一个待量化数据。例如,对于某个卷积层,可以将2个通道中的目标数据确定为一个待量化数据。或者可以将每一个通道中的目标数据确定为一个待量化数据。
方式5-4:将对应待量化层的目标数据中一个或多个批数的数据确定为一个待量化数据;
在待量化层为卷积层时,在卷积层的输入神经元的维度可以包括批数(batch,B)、通道(channel,C)、高度(height,H)和宽度(width,W)。当输入神经元的批数为多个时,各批数的输入神经元可以看作维度为通道、高度和宽度的三维数据。各批数的输入神经元可以对应多个卷积核,各批数的输入神经元的通道数,和与之对应的各卷积核的通道数一致。
对于任意一个批数的输入神经元,以及对于与该批数的输入神经元对应的多个卷积核中的任意一个卷积核,可以根据量化数据量和该批数的输入神经元的数据量,将该批次的输入神经元与该卷积核对应的部分数据(子集),确定为该批次的输入神经元与该卷积核对应的多个待量化数据。例如,假 定目标数据B1有3个批数的数据,若将目标数据中一个批数的数据确定为一个待量化数据,则该目标数据B可以被划分为3个待量化数据。
可以根据卷积核的维度和步长,将输入神经元划分得到全部的待量化数据后,将各待量化数据并行地执行量化过程。由于待量化数据的数据量小于输入神经元,对一个待量化数据进行量化的计算量,小于对输入神经元进行整体量化的计算量,因此,本实施例中的量化方法可以提高输入神经元的量化速度,提高量化效率。也可以将输入神经元根据卷积核的维度和步长进行划分,依次得到各待量化数据后,将得到的各待量化数据分别与卷积核进行卷积运算。各待量化数据的量化过程和卷积运算过程可以并行执行,本实施例中的量化方法可以提高输入神经元的量化效率和运算效率。
方式5-5:将对应待量化层中的目标数据按照确定的划分尺寸划分为一个或多个待量化数据。
可以根据目标任务和终端的精度需求确定终端的实时处理能力确定划分尺寸。终端的实时处理能力可以包括:终端对目标数据进行量化的速度,对量化后数据进行运算的速度,对目标数据进行量化和运算时终端所能处理的数据量等表征终端处理目标数据的处理能力相关的信息。例如,可以根据对目标数据进行量化的速度和对量化后数据进行运算的速度,确定待量化数据的尺寸,以使得对待量化数据进行量化的时间与对量化后数据进行运算的速度相同,这样可以量化和运算同步进行,可以提高目标数据的运算效率。终端的实时处理能力越强,待量化数据的尺寸越大。
在本实施例中,可以根据需要对确定待量化数据的方式进行设置,待量化数据可以包括一种待运算数据如输入神经元(也可以是权值、偏置、梯度,以下以输入神经元为例进行说明),该待运算数据可以是某一个待量化层中的部分或全部输入神经元,也可以是多个待量化层中的、包括每个待量化层中的全部或部分输入神经元。待量化数据还可以是对应于待量化层某一个通道的全部或部分输入神经元,或者对应于待量化层几个通道的全部输入神经元。待量化数据也可以是某一个输入神经元的部分或全部等等。也就是说,可以根据任意的方式对目标数据进行划分,本公开对此不作限制。
在一种可能的实现方式中,如图3-1所示,处理模块12还可以包括第二运算子模块123。第二运算子模块123,用于进行所述装置中除所述第一运算子模块所执行的运算处理之外的运算处理。
通过上述方式,利用第一运算子模块进行上述第一类型和第二类型的定点数据之间的乘法运算,利用第二运算子模块进行其他运算处理,可以加快装置对数据进行运算的运算效率和速度。
在一种可能的实现方式中,如图3-1所示,控制模块11还可以包括指令存储子模块111、指令处理子模块112和队列存储子模块113。
指令存储子模块111用于存储对应于所述神经网络的指令。
指令处理子模块112用于对所述指令进行解析,得到所述指令的操作码和操作域。
队列存储子模块113用于存储指令队列,指令队列包括按照执行顺序依次排列的多个待执行指令,多个待执行指令可以包括对应于所述神经网络的指令。
在该实现方式中,可以根据待执行指令的接收时间、优先级别等对多个待执行指令的执行顺序进行排列获得指令队列,以便于根据指令队列依次执行多个待执行指令。
在一种可能的实现方式中,如图3-1所示,控制模块11可以包括依赖关系处理子模块114。
依赖关系处理子模块114,用于在确定多个待执行指令中的第一待执行指令与第一待执行指令之前的第零待执行指令存在关联关系时,将第一待执行指令缓存在指令存储子模块111中,在第零待执行指令执行完毕后,从指令存储子模块111中提取第一待执行指令发送至处理模块12。其中,第一待执行指令和第零待执行指令是多个待执行指令中的指令。
其中,第一待执行指令与第一待执行指令之前的第零待执行指令存在关联关系包括:存储第一待执行指令所需数据的第一存储地址区间与存储第零待执行指令所需数据的第零存储地址区间具有重叠的区域。反之,第一待执行指令与第零待执行指令之间没有关联关系可以是第一存储地址区间与第 零存储地址区间没有重叠区域。
通过这种方式,可以根据待执行指令之间的依赖关系,使得在先的待执行指令执行完毕之后,再执行在后的待执行指令,保证运算结果的准确性。
在一种可能的实现方式中,如图3-1所示,该装置还可以包括存储模块10。存储模块10用于存储量化参数、待运算数据、量化结果等与神经网络运算相关的计算数据。
在该实现方式中,存储模块可以包括缓存202和寄存器201中的一种或多种,缓存202可以包括速暂存缓存,还可以包括至少一个NRAM(Neuron Random Access Memory,神经元随机存取存储器)。缓存202可以用于存储计算数据,寄存器201可以用于存储计算数据中的标量。
在一种可能的实现方式中,缓存可以包括神经元缓存。神经元缓存也即上述神经元随机存取存储器,可以用于存储计算数据中的神经元数据,神经元数据可以包括神经元向量数据。
在一种可能的实现方式中,存储模块10可以包括数据I/O单元203,用于控制计算数据的输入和输出。
在一种可能的实现方式中,该装置还可以包括直接内存访问模块50,用于从存储模块中读取或者存储数据、从外部设备/其他部件中读取或存储数据,实现存储模块、与外部设备/其他部件之间的数据传输。
在一种可能的实现方式中,控制模块可以包括参数确定子模块。参数确定子模块用于根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
在该实现方式中,可以对待量化数据进行统计,根据统计结果和数据位宽确定待量化数据对应的量化参数。量化参数可以包括点位置、缩放系数和偏移量中的一种或多种。
在一种可能的实现方式中,参数确定子模块可以包括:
第一点位置确定子模块,用于当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值Z 1和对应的数据位宽,得到各所述待量化数据的第一类点位置。其中,该绝对值最大值Z 1是待量化数据中数据取绝对值后所得到的最大值。
在该实现方式中,当待量化数据为相对于原点对称的数据时,量化参数可以不包括偏移量,假设Z 1为待量化数据中元素的绝对值的最大值,待量化数据对应的数据位宽为n,A 1为用数据位宽n对待量化数据进行量化后的量化数据可以表示的最大值,A 1
Figure PCTCN2020095673-appb-000049
A 1需要包含Z 1,且Z 1要大于
Figure PCTCN2020095673-appb-000050
因此有公式(5-1)的约束:
Figure PCTCN2020095673-appb-000051
处理器可以根据待量化数据中的绝对值最大值Z 1和数据位宽n,计算得到第一类点位置s 1。例如,可以利用如下公式(5-2)计算得到待量化数据对应的第一类点位置s 1
Figure PCTCN2020095673-appb-000052
其中,ceil为向上取整,Z 1为待量化数据中的绝对值最大值,s 1为第一类点位置,n为数据位宽。
在一种可能的实现方式中,参数确定子模块可以包括:
第二点位置确定子模块,用于当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置s 2
在该实现方式中,可以先获取待量化数据中的最大值Z max、最小值Z min,进而根据最大值Z max、最小值Z min利用下述公式(5-3)进行计算,
Figure PCTCN2020095673-appb-000053
进一步地,根据计算得到的Z 2和对应的数据位宽利用下述公式(5-4)计算第二类点位置s 2
Figure PCTCN2020095673-appb-000054
在该实现方式中,由于量化时,常规情况下会将待量化数据中的最大值和最小值保存下来,直接基于保存的待量化数据中的最大值和最小值来获取绝对值最大值,无需消耗更多的资源去对待量化数据求绝对值,节省确定统计结果的时间。
在一种可能的实现方式中,参数确定子模块,可以包括:
第一最大值确定子模块,用于当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
第一缩放系数确定子模块,用于根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数f’。其中,第一类缩放系数f’可以包括第一缩放系数f 1和第二缩放系数f 2
其中,该第一缩放系数f 1可以按照如下方式(5-5)进行计算:
Figure PCTCN2020095673-appb-000055
其中,第二缩放系数f 2可以按照如下公式(5-6)进行计算:
Figure PCTCN2020095673-appb-000056
在一种可能的实现方式中,所述参数确定子模块,可以包括:
偏移量确定子模块,用于根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。
在该实现方式中,图3-2示出根据本公开实施例的对称的定点数表示的示意图。如图3-2所示的待量化数据的数域是以“0”为对称中心分布。Z 1为待量化数据的数域中所有浮点数的绝对值最大值,在图3-2中,A 1为n位定点数可以表示的浮点数的最大值,浮点数A 1转换为定点数是(2 n-1-1)。为了避免溢出,A 1需要包含Z 1。在实际运算中,神经网络运算过程中的浮点数据趋向于某个确定区间的正态分布,但是并不一定满足以“0”为对称中心的分布,这时用定点数表示时,容易出现溢出情况。为了改善这一情况,量化参数中引入偏移量。图3-3示出根据本公开实施例的引入偏移量的定点数表示的示意图。如图3-3所示。待量化数据的数域不是以“0”为对称中心分布,Z min是待量化数据的数域中所有浮点数的最小值,Z max是待量化数据的数域中所有浮点数的最大值,A 2为用n位定点数表示的平移后的浮点数的最大值,A 2
Figure PCTCN2020095673-appb-000057
P为Z min~Z max之间的中心点,将待量化数据的数域整体偏移,使得平移后的待量化数据的数域以“0”为对称中心分布,以避免数据的“溢出”。平移后的待量化数据的数域中的绝对值最大值为Z 2。由图3-3可知,偏移量为“0”点到“P”点之间的水平距离,该距离称为偏移量o。
可以根据该最小值Z min和最大值Z max按照如下公式(5-7)计算获得偏移量:
Figure PCTCN2020095673-appb-000058
其中,o表示偏移量,Z min表示待量化数据所有元素中的最小值,Z max表示待量化数据所有元素中的最大值。
在一种可能的实现方式中,所述参数确定子模块,可以包括:
第二最大值确定子模块,用于当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
第一缩放系数确定子模块,用于根据各所述待量化数据中的最大值、最小值和量化后数据的最大 值,得到各所述待量化数据的第二类缩放系数f”。其中,第二类缩放系数f”可以包括第三缩放系数f 3和第四缩放系数f 4
在该实现方式中,当量化参数包括偏移量时,A 2为用数据位宽n对平移后的待量化数据进行量化后的量化数据可以表示的最大值,A 2
Figure PCTCN2020095673-appb-000059
可以根据待量化数据中的Z max、最小值Z min进行计算得到平移后的待量化数据的数域中的绝对值最大值Z 2,进而按照如下公式(5-8)计算第三缩放系数f 3
Figure PCTCN2020095673-appb-000060
进一步地,第四缩放系数f 4可以按照如下公式(5-9)进行计算:
Figure PCTCN2020095673-appb-000061
在对待量化数据进行量化时,所采用的量化参数不同,进行量化所使用的数据不同。
在一种可能的实现方式中,量化参数可以包括第一类点位置s 1。可以利用如下的公式(5-10)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000062
其中,I x为量化数据属于第一类型的数据,F x为待量化数据,round为进行四舍五入的取整运算。
量化参数可以包括第一类点位置s 1时,可以根据公式(5-11)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000063
Figure PCTCN2020095673-appb-000064
在一种可能的实现方式中,量化参数可以包括第一类点位置和第一缩放系数。可以利用如下的公式(5-12)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000065
当量化参数包括第一类点位置和第一缩放系数时,可以根据公式(5-13)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000066
Figure PCTCN2020095673-appb-000067
在一种可能的实现方式中,量化参数可以包括第二缩放系数。可以利用如下的公式(5-14)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000068
当量化参数包括第二缩放系数时,可以根据公式(5-15)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000069
Figure PCTCN2020095673-appb-000070
在一种可能的实现方式中,量化参数可以包括偏移量。可以利用如下的公式(5-16)对待量化数据进行量化,得到量化数据I x
I x=round(F x-o)        公式(5-16)
当量化参数包括偏移量时,可以根据公式(5-17)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000071
Figure PCTCN2020095673-appb-000072
在一种可能的实现方式中,量化参数可以包括第二类点位置和偏移量。可以利用如下的公式(5-18)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000073
当量化参数包括第二类点位置和偏移量时,可以根据公式(5-19)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000074
Figure PCTCN2020095673-appb-000075
在一种可能的实现方式中,量化参数可以包括第二类缩放系数f”和偏移量o。可以利用如下的公式(5-20)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000076
当量化参数包括第二类缩放系数和偏移量时,可以根据公式(5-21)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000077
Figure PCTCN2020095673-appb-000078
在一种可能的实现方式中,量化参数可以包括第二类点位置、第二类缩放系数和偏移量。可以利用如下的公式(5-22)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000079
当量化参数包括第二类点位置、第二类缩放系数和偏移量时,可以根据公式(5-23)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000080
Figure PCTCN2020095673-appb-000081
可以理解的是,也可以采用其他的取整运算方法,例如采用向上取整、向下取整、向零取整等取整运算,替换上述公式中的四舍五入的取整运算round。可以理解的是,在数据位宽一定的情况下,根据点位置量化得到的量化数据中,小数点后的位数越多,量化数据的量化精度越大。
在一种可能的实现方式中,控制模块还可以通过查找待量化数据与量化参数对应关系,确定与所述待量化层中每种待量化数据对应的量化参数。
在一种可能的实现方式中,各待量化层中与每种待量化数据对应的量化参数,可以是保存的预设值。可以为神经网络建立一个待量化数据与量化参数之间的对应关系,该对应关系可以包括各待量化层的每种待量化数据与量化参数对应关系,并将对应关系保存在各层可以共享访问的存储空间。也可以为神经网络建立多个待量化数据与量化参数之间的对应关系,各待量化层分别对应其中一个对应关系。可以将各层的对应关系保存在本层独享的存储空间,也可以将各层的对应关系保存在各层可以共享访问的存储空间。
在待量化数据与量化参数对应关系中,可以包括多个待量化数据和与之对应的多个量化参数之间的对应关系。例如,待量化数据与量化参数对应关系A中,可以包括待量化层1的神经元和权值两个待量化数据,神经元对应点位置1、缩放系数1和偏移量1共三个量化参数,权值对应点位置2和偏移量2共两个量化参数。本公开对待量化数据与量化参数对应关系的具体格式不作限定。
在本实施例中,可以通过查找待量化数据与量化参数对应关系,确定与所述待量化层中每种待量化数据对应的量化参数。可以为各待量化层预设对应的量化参数,并通过对应关系进行存储后,供待 量化层查找后使用。本实施例中量化参数的获取方式简单方便。
在一种可能的实现方式中,控制模块还可以包括:第一量化误差确定子模块、调整位宽确定子模块和调整量化参数确定子模块。
第一量化误差确定子模块,用于根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差。
可以根据待量化数据对应的量化数据与待量化数据之间的误差,确定待量化数据的量化误差。可以利用设定的误差计算方法,例如标准差计算方法、均方根误差计算方法等,计算待量化数据的量化误差。
也可以根据量化参数,将待量化数据对应的量化数据进行反量化后得到反量化数据,根据反量化数据与待量化数据之间的误差,按照公式(5-24)确定待量化数据的量化误差diff bit
Figure PCTCN2020095673-appb-000082
其中,F i为待量化对应的浮点值,其中,i为待量化数据中数据的下标。
Figure PCTCN2020095673-appb-000083
为浮点值对应的反量化数据。
还可以根据量化间隔、量化后的数据的个数以及对应的量化前的数据按照公式(5-25)确定量化误差diff bit
Figure PCTCN2020095673-appb-000084
其中,C为量化时对应的量化间隔,m为量化后获得的量化数据的个数,F i为待量化对应的浮点值,其中,i为待量化数据中数据的下标。
也可以根据量化后的数据以及对应的反量化数据按照公式(5-26)确定量化误差diff bit
Figure PCTCN2020095673-appb-000085
其中,F i为待量化对应的浮点值,其中,i为待量化数据集合中数据的下标。
Figure PCTCN2020095673-appb-000086
为浮点值对应的反量化数据。
调整位宽确定子模块,用于根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽。
可以根据经验值确定误差阈值,误差阈值可以用于表示对量化误差的期望值。当量化误差大于或小于误差阈值时,可以调整待量化数对应的数据位宽,得到待量化数据对应的调整位宽。可以将数据位宽调整为更长的位宽或更短的位宽,以提高或降低量化精度。
可以根据能够接受的最大误差确定误差阈值,当量化误差大于误差阈值时,说明量化精度不能达到预期,需要将数据位宽调整为更长的位宽。也可以根据较高的量化精度确定一个较小的误差阈值, 当量化误差小于误差阈值时,说明量化精度较高,神经网络的运行效率将受到影响,可以适当的将数据位宽调整为更短的位宽,以适当的降低量化精度,提高神经网络的运行效率。
可以将数据位宽按照固定的位数步长进行调整,也可以根据量化误差与误差阈值之间的差值的不同,按照可变的调整步长调整数据位宽。本公开对此不作限定。
调整量化参数确定子模块,用于将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
确定调整位宽后,可以将待量化数据对应的数据位宽更新为调整位宽。例如,待量化数据更新前的数据位宽为8位,调整位宽为12位,则更新后待量化数据对应的数据位宽为12位。可以根据调整位宽和待量化数据计算得到待量化数据对应的调整量化参数。可以根据待量化数据对应的调整量化参数重新对待量化数据进行量化,以得到量化精度更高或更低的量化数据,使得待量化层在量化精度和处理效率之间达到平衡。
在神经网络的推理、训练和微调过程中,各层之间的待量化数据可以认为具有一定的关联性。例如,各层的待量化数据之间的均值之间的差小于设定的均值阈值,且各层的待量化数据之间的最大值之间的差值也小于设定的差值阈值时,可以将待量化层的调整量化参数作为后续的一个或多个层的调整量化参数,用于对待量化层后续的一个或多个层的待量化数据进行量化。也可以在神经网络的训练和微调过程中,将待量化层在当前迭代得到的调整量化参数,用于在后续的迭代中对待量化层进行量化。
在一种可能的实现方式中,所述控制模块还用于在所述待量化层之后的一层或多层采用所述待量化层的量化参数。
神经网络根据调整量化参数进行量化,可以包括只在待量化层利用调整量化参数对待量化数据重新进行量化,并将重新得到的量化数据用于待量化层的运算。也可以包括在待量化层不使用调整量化参数重新对待量化数据进行量化,而在待量化层后续的一个或多个层使用调整量化参数进行量化,和/或后续的迭代中在待量化层使用调整量化参数进行量化。还可以包括在待量化层使用调整量化参数重新进行量化,并将重新得到的量化数据用于待量化层的运算,并且在待量化层后续的一个或多个层使用调整量化参数进行量化,和/或后续的迭代中在待量化层使用调整量化参数进行量化。本公开对此不作限定。
在本实施例中,根据待量化数据和待量化数据对应的量化数据之间的误差调整数据位宽,并根据调整后的数据位宽计算得到调整量化参数。通过设置不同的误差阈值可以得到不同的调整量化参数,达到提高量化精度或提高运行效率等不同的量化需求。根据待量化数据和待量化数据的量化数据计算得到的调整量化参数,也能够更加符合待量化数据自身的数据特征,达到更加符合待量化数据自身需求的量化结果,在量化精度和处理效率之间达到更好的平衡。
在一种可能的实现方式中,所述调整位宽确定子模块可以包括第一调整位宽确定子模块。第一调整位宽确定子模块,用于当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
可以根据能够接受的最大的量化误差,确定第一误差阈值。可以将量化误差与第一误差阈值进行比较。当量化误差大于第一误差阈值时,可以认为量化误差已经不可接受。需要提高量化精度,可以通过增加待量化数据对应的数据位宽的方式,提高待量化数据的量化精度。
可以将待量化数据对应的数据位宽按照固定的调整步长增加,得到调整位宽。固定的调整步长可以为N位,N为正整数。每次调整数据位宽可以增加N位。每次增加后的数据位宽=原数据位宽+N位。
可以将待量化数据对应的数据位宽按照可变的调整步长增加,得到调整位宽。例如,当量化误差与误差阈值之间的差值大于第一阈值时,可以按照调整步长M1调整数据位宽,当量化误差与误差阈值之间的差值小于第一阈值时,可以按照调整步长M2调整数据位宽,其中,第一阈值大于第二阈值,M1大于M2。可以根据需求确定各可变的调整步长。本公开对数据位宽的调整步长及调整步长是否可变不作限定。
可以将待量化数据按照调整位宽计算得到调整后的量化参数。利用调整后的量化参数对待量化数据进行重新量化后得到的量化数据,比利用调整前的量化参数量化得到的量化数据的量化精度更高。
在一种可能的实现方式中,控制模块还可以包括第一调整后量化误差子模块和第一调整位宽循环确定模块。
第一调整后量化误差子模块,用于根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
第一调整位宽循环确定模块,用于根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
根据量化误差增加待量化数据对应的数据位宽时,调整一次位宽后得到调整位宽,根据调整位宽计算得到调整后的量化参数,根据调整后的量化参数量化待量化数据得到调整后的量化数据,再根据调整后的量化数据与待量化数据计算得到待量化数据调整后的量化误差,调整后的量化误差可能依然大于第一误差阈值,即根据调整一次的数据位宽可能不能满足调整目的。当调整后的量化误差依然大于第一误差阈值时,可以继续对调整后的数据位宽进行调整,即多次增加待量化数据对应的数据位宽,直至根据最终得到的调整位宽和待量化数据得到的调整后的量化误差小于第一误差阈值。
多次增加的调整步长可以是固定的调整步长,也可以是可变的调整步长。例如,最终的数据位宽=原数据位宽+B*N位,其中N为每次增加的固定的调整步长,B为数据位宽的增加次数。最终的数据位宽=原数据位宽+M1+M2+…+Mm,其中,M1、M2…Mm为每次增加的可变的调整步长。
在本实施例中,当量化误差大于第一误差阈值时,增加所述待量化数据对应的数据位宽,得到所述待量化数据对应的调整位宽。可以通过设置第一误差阈值和调整步长增加数据位宽,以使调整后的数据位宽能够满足量化的需求。当一次调整不能满足调整需求时,还可以对数据位宽进行多次调整。第一误差阈值和调整步长的设置,使得量化参数可以按照量化需求进行灵活调整,满足不同的量化需求,使得量化精度可根据自身数据特征进行自适应调整。
在一种可能的实现方式中,所述调整位宽确定子模块还可以包括第二调整位宽确定子模块。
第二调整位宽确定子模块,用于当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值
可以根据能够接受的量化误差和期望的神经网络的运行效率,确定第二误差阈值。可以将量化误差与第二误差阈值进行比较。当量化误差小于第二误差阈值时,可以认为量化误差超出预期,但运行效率过低已经不可接受。可以降低量化精度以提高神经网络的运行效率,可以通过减少待量化数据对应的数据位宽的方式,降低待量化数据的量化精度。
可以将待量化数据对应的数据位宽按照固定的调整步长减少,得到调整位宽。固定的调整步长可以为N位,N为正整数。每次调整数据位宽可以减少N位。增加后的数据位宽=原数据位宽-N位。
可以将待量化数据对应的数据位宽按照可变的调整步长减少,得到调整位宽。例如,当量化误差与误差阈值之间的差值大于第一阈值时,可以按照调整步长M1调整数据位宽,当量化误差与误差阈值之间的差值小于第一阈值时,可以按照调整步长M2调整数据位宽,其中,第一阈值大于第二阈值,M1大于M2。可以根据需求确定各可变的调整步长。本公开对数据位宽的调整步长及调整步长是否可变不作限定。
可以将待量化数据按照调整位宽计算得到调整后的量化参数,利用调整后的量化参数对待量化数据进行重新量化后得到的量化数据,比利用调整前的量化参数量化得到的量化数据的量化精度更低。
在一种可能的实现方式中,控制模块还可以包括第二调整后量化误差子模块、第二调整位宽循环确定子模块。
第二调整后量化误差子模块,用于根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
第二调整位宽循环确定子模块,用于根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
根据量化误差增加待量化数据对应的数据位宽时,调整一次位宽后得到调整位宽,根据调整位宽计算得到调整后的量化参数,根据调整后的量化参数量化待量化数据得到调整后的量化数据,再根据调整后的量化数据与待量化数据计算得到待量化数据调整后的量化误差,调整后的量化误差可能依然小于第二误差阈值,即根据调整一次的数据位宽可能不能满足调整目的。当调整后的量化误差依然小于第二误差阈值时,可以继续对调整后的数据位宽进行调整,即多次减少待量化数据对应的数据位宽,直至根据最终得到的调整位宽和待量化数据得到的调整后的量化误差大于第二误差阈值。
多次减少的调整步长可以是固定的调整步长,也可以是可变的调整步长。例如,最终的数据位宽=原数据位宽-B*N位,其中N为每次增加的固定的调整步长,B为数据位宽的增加次数。最终的数据位宽=原数据位宽-M1-M2-…-Mm,其中,M1、M2…Mm为每次减少的可变的调整步长。
在本实施例中,当量化误差小于第二误差阈值时,减少所述待量化数据对应的数据位宽,得到所述待量化数据对应的调整位宽。可以通过设置第二误差阈值和调整步长减少数据位宽,以使调整后的数据位宽能够满足量化的需求。当一次调整不能满足调整需求时,还可以对数据位宽进行多次调整。第二误差阈值和调整步长的设置,使得量化参数可以按照量化需求进行灵活的自适应调整,满足不同的量化需求,使得量化精度可调,在量化精度和神经网络的运行效率之间达到平衡。
在一种可能的实现方式中,所述控制模块,还用于当所述量化误差大于第一误差阈值时,增加所述待量化数据对应的数据位宽,以及当所述量化误差小于第二误差阈值时,减少所述待量化数据对应的数据位宽,得到所述待量化数据对应的调整位宽。
也可以同时设置两个误差阈值,其中,第一误差阈值用于表示量化精度过低,可以增加数据位宽的位数,第二误差阈值用于表示量化精度过高,可以减少数据位宽的位数。第一误差阈值大于第二误差阈值,可以将待量化数据的量化误差同时与两个误差阈值进行比较,当量化误差大于第一误差阈值时,增加数据位宽的位数,当量化误差小于第二误差阈值时,减少数据位宽的位数。当量化误差位于第一误差阈值和第二误差阈值之间时,数据位宽可以保持不变。
在本实施例中,通过将量化误差与第一误差阈值和第二误差阈值同时进行比较,可以根据比较结果增加或减少数据位宽,可以利用第一误差阈值和第二误差阈值更加灵活的调整数据位宽。使得数据位宽的调整结果更加符合量化需求。
在一种可能的实现方式中,在所述神经网络运算的微调阶段和/或训练阶段,控制模块还可以包括第一数据变动幅度确定子模块、目标迭代间隔确定子模块。
第一数据变动幅度确定子模块,用于获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
目标迭代间隔确定子模块,用于根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
在神经网络运算的微调阶段和/或训练阶段包括多次迭代。神经网络中的各待量化层,在进行一次正向运算和一次反向运算,并对待量化层的权值进行更新后,完成一次迭代。在多次迭代中,待量化层中的待量化数据和/或待量化数据对应的量化数据的数据变动幅度,可以用于衡量在不同迭代中的待量化数据和/或量化数据是否可采用相同的量化参数进行量化。若当前迭代以及历史迭代中待量化数据的数据变动幅度较小,例如小于设定的幅度变动阈值时,可以在数据变动幅度较小的多个迭代中采用相同的量化参数。
可以通过提取预存的量化参数的方式,确定与待量化数据对应的量化参数。在不同的迭代中对待量化数据进行量化时,需要在各迭代提取与待量化数据对应的量化参数。若多个迭代的待量化数据和/或待量化数据对应的量化数据的数据变动幅度较小,可将在数据变动幅度较小的多个迭代中采用的相同的量化参数进行暂存,各迭代在进行量化时可以利用暂存的量化参数进行量化运算,不用在每次迭代提取量化参数。
也可以根据待量化数据和数据位宽计算得到量化参数。在不同的迭代中对待量化数据进行量化时,需要在各迭代分别计算量化参数。若多个迭代的待量化数据和/或待量化数据对应的量化数据的数据变动幅度较小,可在数据变动幅度较小的多个迭代中采用的相同的量化参数,则各迭代均可以直接使用其中第一个迭代计算得到的量化参数,而不是每次迭代计算量化参数。
可以理解的是,当待量化数据为权值时,各迭代之间的权值在不断更新,若多个迭代的权值的数据变动幅度较小,或多个迭代的权值对应的量化数据的数据变动幅度较小,可以在多个迭代中利用相同的量化参数对权值进行量化。
可以根据待量化数据的数据变动幅度确定目标迭代间隔,目标迭代间隔包括至少一次迭代,可以在目标迭代间隔内的各迭代使用相同的量化参数,即在目标迭代间隔内的各迭代不再更新待量化数据的量化参数。神经网络根据目标迭代间隔更新待量化数据的量化参数,包括在目标迭代间隔内的迭代,不获取预设的量化参数或不计算量化参数,即在目标迭代间隔内的迭代不更新量化参数。而在目标迭代间隔外的迭代,再获取预设的量化参数或计算量化参数,即在目标迭代间隔外的迭代更新量化参数。
可以理解的是,多个迭代之间的待量化数据或待量化数据的量化数据的数据变动幅度越小,确定出的目标迭代间隔包括的迭代次数越多。可以根据计算得到的数据变动幅度,查找预设的数据变动幅度与迭代间隔的对应关系,确定与计算得到的数据变动幅度对应的目标迭代间隔。可以根据需求预设数据变动幅度与迭代间隔的对应关系。也可以根据计算得到的数据变动幅度,利用设定的计算方法计算得到目标迭代间隔。本公开不限定数据变动幅度的计算方式,以及目标迭代间隔的获取方式。
在本实施例中,在神经网络运算的微调阶段和/或训练阶段,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,根据所述待量化数据的数据变动幅度,确定待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。可以根据多个迭代中待量化数据或待量化数据对应的量化数据的数据变动幅度,确定目标迭代间隔。神经网络可以根据目标迭代间隔确定是否更新量化参数。由于目标迭代间隔所包括的多个迭代的数据变动幅度较小,目标迭代间隔内的迭代不更新量化参数也可以保证量化精度。而目标迭代间隔内的多个迭代不更新量化参数,可以减少量化参数的提取次数或计算次数,从而提高神经网络的运算效率。
在一种可能的实现方式中,控制模块还可以包括第一目标迭代间隔应用子模块。
第一目标迭代间隔应用子模块,用于根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
如本公开上述实施例所述,待量化数据的量化参数可以预设,也可以根据待量化数据对应的数据位宽计算得到。而不同待量化层中待量化数据对应的数据位宽,或相同待量化层中待量化数据在不同 迭代中对应的数据位宽,可以根据本公开上述实施例中的方式进行自适应调整。
当待量化数据的数据位宽不可自适应调整,为预设的数据位宽时,可以根据待量化数据在当前迭代的预设的数据位宽,确定待量化数据在目标迭代间隔内的迭代对应的数据位宽。在目标迭代间隔内的各迭代可不使用自身的预设值。
当待量化数据的数据位宽可自适应调整时,可以根据待量化数据在当前迭代对应的数据位宽,确定待量化数据在目标迭代间隔内的迭代对应的数据位宽。在数据位宽可自适应调整时,数据位宽可进行一次调整或多次调整。可以将待量化数据在当前迭代进行自适应调整后的数据位宽,作为目标迭代间隔内的各迭代对应的数据位宽,在目标迭代间隔内的各迭代不再对数据位宽进行自适应调整(更新)。待量化数据在当前迭代可以使用自适应调整后的数据位宽,也可以使用自适应调整前的数据位宽,本公开对此不作限定。
在目标迭代间隔以外的其他迭代,由于待量化数据的数据变动幅度不满足设定条件,可以根据本公开上述的方法对数据位宽进行自适应调整,得到更加符合当前迭代的待量化数据的数据位宽,也可使用本公开中的目标迭代间隔的计算方法,计算得到新的目标迭代间隔并使用,从而在保证目标迭代间隔以外的迭代的量化精度的同时,提高神经网络的运行效率。
在目标迭代间隔内的各迭代的数据位宽相同,各迭代可以根据相同的数据位宽各自计算得到对应的量化参数。量化参数可以包括点位置、缩放系数和偏移量中的至少一种。可以在目标迭代间隔内的各迭代,根据相同的数据位宽分别计算得到量化参数。量化参数包括点位置(包括第一类点位置、第二类点位置)、缩放系数(包括第一类缩放系数和第二类缩放系数)和偏移量时,在目标迭代间隔内的各迭代,可利用相同的数据位宽,分别计算各自对应的点位置、缩放系数和偏移量。
在根据当前迭代的数据位宽,确定目标迭代间隔内各迭代的数据位宽的同时,可以根据当前迭代的量化参数,确定目标迭代间隔内各迭代的对应的量化参数。目标迭代间隔内各迭代的量化参数,也不再重新根据相同的数据位宽计算得到,可以进一步提高神经网络的运算效率。可以根据当前迭代的全部量化参数或部分量化参数,确定目标迭代间隔内各迭代的对应的量化参数。当根据当前迭代的部分量化参数,确定目标迭代间隔内各迭代的对应的量化参数时,剩余部分的量化参数,在目标迭代间隔内各迭代仍需计算。
例如,量化参数包括第二类点位置、第二类缩放系数和偏移量。可以根据当前迭代的数据位宽和第二类点位置,确定目标迭代间隔内各迭代的数据位宽和第二类点位置。则目标迭代间隔内各迭代的第二类缩放系数和偏移量需要根据相同的数据位宽计算得到。也可以根据当前迭代的数据位宽、第二类点位置、第二类缩放系数和偏移量,确定目标迭代间隔内各迭代的数据位宽、第二类点位置、第二类缩放系数和偏移量,则目标迭代间隔内各迭代的各量化参数均不需要计算得到。
在本实施例中,根据待量化数据在当前迭代对应的数据位宽,确定待量化数据在目标迭代间隔内的迭代对应的数据位宽,以使神经网络根据待量化数据在目标迭代间隔内的迭代对应的数据位宽,确定量化参数。在目标迭代间隔内的各迭代的数据位宽,根据当前迭代的数据位宽确定,由于目标迭代间隔内各迭代的待量化数据的数据变化幅度满足设定的条件,利用相同的数据位宽计算得到的量化参数,可以保证目标迭代间隔内的各迭代的量化精度。目标迭代间隔内各迭代使用相同的数据位宽,也可以提高神经网络的运算效率。在对神经网络进行量化后运算结果的准确率和神经网络的运算效率之间,达到平衡。
在一种可能的实现方式中,控制模块还可以包括第二目标迭代间隔应用子模块。第二目标迭代间隔应用子模块,用于根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位置。
其中,根据所述待量化数据在所述当前迭代对应的第一类点位置,确定所述待量化数据在所述 目标迭代间隔内的迭代对应的第一类点位置。根据所述待量化数据在所述当前迭代对应的第二类点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的第二类点位置。
在量化参数中,相对于缩放系数和偏移量,不同的点位置对相同待量化数据的量化结果产生的影响较大。可以根据待量化数据在当前迭代对应的点位置,确定目标迭代间隔内的迭代对应的点位置。当数据位宽不可自适应调整时,可以将待量化数据在当前迭代预设的点位置,作为待量化数据在目标迭代间隔内各迭代对应的点位置,也可以将待量化数据在当前迭代根据预设的数据位宽计算得到的点位置,作为待量化数据在目标迭代间隔内各迭代对应的点位置。当数据位宽可自适应调整时,可以将待量化数据在当前迭代调整后的点位置,作为待量化数据在目标迭代间隔内各迭代对应的点位置。
根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置的同时,也可以根据待量化数据在当前迭代对应的缩放系数,确定所述待量化数据在所述目标迭代间隔内的迭代对应的缩放系数,和/或根据待量化数据在当前迭代对应的偏移量,确定所述待量化数据在所述目标迭代间隔内的迭代对应的偏移量。
根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置的同时,还可以根据待量化数据在当前迭代对应的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,其中,待量化数据在当前迭代对应的数据位宽,可以是当前迭代预设的数据位宽或自适应调整后的数据位宽。
在本实施例中,根据待量化数据在当前迭代对应的点位置,确定待量化数据在目标迭代间隔内的迭代对应的点位置。在目标迭代间隔内的各迭代的点位置,根据当前迭代的点位置确定,由于目标迭代间隔内各迭代的待量化数据的数据变化幅度满足设定的条件,利用相同的点位置,可以保证目标迭代间隔内的各迭代的量化精度。目标迭代间隔内各迭代使用相同的点位置,也可以提高神经网络的运算效率。在对神经网络进行量化后运算结果的准确率和神经网络的运算效率之间,达到平衡。
在一种可能的实现方式中,所述第一数据变动幅度确定子模块可以包括滑动平均值计算子模块和第一数据变动幅度确定子模块。所述目标迭代间隔确定子模块可以包括第一目标迭代间隔确定子模块
滑动平均值计算子模块,用于根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
第一数据变动幅度确定子模块,用于根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
第一目标迭代间隔确定子模块,用于根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
其中,根据待量化数据在当前迭代的第一类点位置和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的第一类点位置,计算待量化数据对应各迭代间隔的第一类点位置的滑动平均值;根据所述待量化数据在当前迭代的第一类点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的第一类点位置的第二滑动平均值,得到所述待量化数据变动幅度。或者,根据待量化数据在当前迭代的第二类点位置和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的第二类点位置,计算待量化数据对应各迭代间隔的第二类点位置的滑动平均值;根据所述待量化数据在当前迭代的第二类点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的第二类点位置的第二滑动平均值,得到所述待量化数据变动幅度。
在一种可能的实现方式中,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代,可以为计算目标迭代间隔的历史迭代。当前迭代与对应的目标迭代间隔之间的对应关系可以包括:
可以从当前迭代开始计数目标迭代间隔,并在当前迭代对应的目标迭代间隔结束后的下一个迭代 开始重新计算目标迭代间隔。例如,当前迭代为第100代,目标迭代间隔为3,目标迭代间隔内的迭代包括:第100代、第101代和第102代,可以在第103代计算与第103代对应的目标迭代间隔,并以103代为新计算得到当目标迭代间隔内的第一个迭代。此时,当前迭代为103代时,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代为100代。
可以从当前迭代的下一个迭代开始计数目标迭代间隔,并在目标迭代间隔内的最后一个迭代开始重新计算目标迭代间隔。例如,当前迭代为第100代,目标迭代间隔为3,目标迭代间隔内的迭代包括:第101代、第102代和第103代,可以在第103代计算与第103代对应的目标迭代间隔,并以104代为新计算得到当目标迭代间隔内的第一个迭代。此时,当前迭代为103代时,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代为100代。
可以从当前迭代的下一个迭代开始计数目标迭代间隔,并在目标迭代间隔结束后的下一个迭代开始重新计算目标迭代间隔。例如,当前迭代为第100代,目标迭代间隔为3,目标迭代间隔内的迭代包括:第101代、第102代和第103代,可以在第104代计算与第104代对应的目标迭代间隔,并以105代为新计算得到当目标迭代间隔内的第一个迭代。此时,当前迭代为104代时,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代为100代。
可以根据需求确定当前迭代以及目标迭代间隔之间的其他的对应关系,例如可以从当前迭代之后的第N个迭代开始计数目标迭代间隔,N大于1,本公开对此不作限定。
可以理解的是,计算得到的待量化数据对应各迭代间隔的点位置的滑动平均值,包括待量化数据在当前迭代的点位置的第一滑动平均值,和待量化数据在上一迭代间隔对应迭代的点位置的第二滑动平均值。可以利用公式(5-27)计算当前迭代对应点位置的第一滑动平均值m (t)
m (t)←α×s (t)+(1-α)×m (t-1)     公式(5-27)
其中,t为当前迭代,t-1为根据上一迭代间隔确定的历史迭代,m (t-1)为根据上一迭代间隔确定的历史迭代的第二滑动平均值。s (t)为当前迭代的点位置,可以为第一类点位置或第二类点位置。α为第一参数。第一参数可以为超参数。
在本实施例中,根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值;根据待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度。根据第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。由于第一数据变动幅度可以用于衡量点位置的变化趋势,使得目标迭代间隔可以跟随待量化数据点位置的变化趋势而变化,也使得计算得到的各目标迭代间隔的大小可以根据待量化数据点位置的变化趋势而变化。由于量化参数根据目标迭代间隔确定,也就使得根据量化参数进行量化得到的量化数据,能够更加符合待量化数据的点位置的变动趋势,在保证量化精度的同时,提高神经网络的运行效率。
在一种可能的实现方式中,所述第一数据变动幅度确定子模块可以包括第一幅度确定子模块。第一幅度确定子模块,用于计算所述第一滑动平均值和所述第二滑动平均值的差值;将所述差值的绝对值确定为第一数据变动幅度。
可以利用公式(5-28)计算第一数据变动幅度diff update1
diff update1=|m (t)-m (t-1)|=α|s (t)-m (t-1)|      公式(5-28)
可以根据第一数据变动幅度,确定待量化数据对应的目标迭代间隔,以使神经网络根据目标迭代间隔更新所述待量化数据的量化参数。可以根据公式(5-29)计算得到目标迭代间隔I:
Figure PCTCN2020095673-appb-000087
其中,β为第二参数,γ为第三参数。第二参数和第三参数可以为超参数。
可以理解的是,第一数据变动幅度可以用于衡量点位置的变化趋势,第一数据变动幅度越大,说明量化数据的数值范围变化剧烈,在更新量化参数时需要间隔更短的目标迭代间隔I。
在本实施例中,计算所述第一滑动平均值和所述第二滑动平均值的差值;将差值的绝对值确定为第一数据变动幅度。根据滑动平均值之间的差值可以得到精确的第一数据变动幅度。
在一种可能的实现方式中,控制模块还可以包括第二数据变动幅度确定子模块,目标迭代间隔确定子模块可以包括第二目标迭代间隔确定子模块。
第二数据变动幅度确定子模块,用于根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
第二目标迭代间隔确定子模块,用于根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
可以根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度。也可以根据在当前迭代所述待量化数据和所述待量化数据对应的反量化数据,得到第二数据变动幅度。
同样的,可以根据公式(5-30)计算得到在当前迭代中,所述待量化数据和所述待量化数据对应的反量化数据之间的第二数据变动幅度diff bit。也可以利用其它误差的计算方法,计算待量化数据和反量化数据之间的第二数据变动幅度diff bit。本公开对此不作限定。
Figure PCTCN2020095673-appb-000088
其中,z i为待量化数据,z i (n)为待量化数据对应的反量化数据。可以理解的是,第二数据变动幅度可以用于衡量待量化数据对应的数据位宽的变化趋势,第二数据变动幅度越大,待量化数据越有可能需要更新对应的数据位宽,需要间隔更短的迭代进行更新,则第二数据变动幅度越大,需要目标迭代间隔更小。
在本实施例中,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度。根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。第二数据变动幅度可以用于衡量数据位宽的变动需求,则根据第一数据变动幅度和第二数据变动幅度计算得到的目标迭代间隔,可以同时跟踪点位置和数据位宽的变动,目标迭代间隔也可以更加符合待量化数据自身的数据量化需求。
在一种可能的实现方式中,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度,可以包括:
计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;
将所述误差的平方确定为所述第二数据变动幅度。
可以利用公式(5-31)计算得到第二数据变动幅度diff update2
diff update2=δ*diff bit 2   公式(5-31)
其中,δ为第四参数,第四参数可以为超参数。
可以理解的是,利用不同的数据位宽可以得到不同的量化参数,进而得到不同的量化数据,产生不同的第二数据变动幅度。第二数据变动幅度可以用于衡量数据位宽的变化趋势,第二数据变动幅度越大,说明需要更短的目标迭代间隔来更加频繁的更新数据位宽,即目标迭代间隔需要更小。
在一种可能的实现方式中,所述第二目标迭代间隔确定子模块可以包括间隔确定子模块。
间隔确定子模块,用于根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所 述待量化数据对应的目标迭代间隔。
可以根据公式(5-32)计算得到目标迭代间隔:
Figure PCTCN2020095673-appb-000089
其中,β为第二参数,γ为第三参数。第二参数和第三参数可以为超参数。
可以理解的是,利用第一数据变动幅度和第二数据变动幅度得到的目标迭代间隔,可以同时衡量数据位宽和点位置的变化趋势,当两者中的其一的变化趋势较大时,便可以使得目标迭代间隔产生相应的变化。目标迭代间隔可以同时追踪数据位宽和点位置的变化并做出相应的调整。使得根据目标迭代间隔更新的量化参数能够更加符合目标数据的变动趋势,最终使得根据量化参数得到的量化数据能够更加符合量化需求。
在一种可能的实现方式中,所述第一数据变动幅度确定子模块可以包括第二数据变动幅度确定子模块。第二数据变动幅度确定子模块,用于在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
在神经网络运算的训练过程和/或微调过程中,在训练开始或微调开始的多个迭代中,待量化数据的变化幅度较大。若在训练开始或微调开始的多个迭代中计算目标迭代间隔,则计算得到的目标迭代间隔可能会失去其使用的意义。可以根据预设更新周期,在更新周期以内的各迭代,不计算目标迭代间隔,也不适用目标迭代间隔使得多个迭代使用相同的数据位宽或点位置。
当迭代进行至更新周期以外时,即当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,并根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。例如,预设更新周期为100代,则自第1代开始至第100代的迭代中,不计算目标迭代间隔。当迭代进行至101代,即当前迭代为101代时,当前迭代位于更新周期以外,此时,可以根据第101代以及第1代至第100代的迭代中待量化数据的数据变动幅度,确定第101代的待量化数据对应的目标迭代间隔,并在第101代或与第101间隔预设代数的迭代中,使用计算得到的目标迭代间隔。
可以自预设的代数开始计数更新周期,例如可以自第一代开始计数更新周期中多个迭代,也可以自第N代开始计数更新周期中的多个迭代,本公开对此不作限定。
在本实施例中,当迭代进行至更新周期以外时计算并使用目标迭代间隔。可以避免在神经网络运算的训练过程或微调过程的初期,由于待量化数据的变动幅度较大引起的目标迭代间隔使用意义不大的问题,可以在使用目标迭代间隔的情况下,进一步提高神经网络的运行效率。
在一种可能的实现方式中,控制模块还可以包括周期间隔确定子模块、第一周期间隔应用子模块和第二周期间隔应用子模块。
周期间隔确定子模块,用于在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
第一周期间隔应用子模块,用于根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
第二周期间隔应用子模块,用于根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
在神经网络运算的训练过程或微调过程中,可以包括多个周期。各周期可以包括多个迭代。用于神经网络运算的数据被完整的运算一遍为一个周期。在训练过程中,随着迭代的进行,神经网络的权值变化趋于稳定,当训练稳定后,神经元、权值、偏置和梯度等待量化数据均趋于稳定。待量化数据趋于稳定后,待量化数据的数据位宽和量化参数也趋于稳定。同理,在微调过程中,微调稳定后,待 量化数据的数据位宽和量化参数也趋于稳定。
因此,可以根据训练稳定或微调稳定的周期确定预设周期。可以将训练稳定或微调稳定所在周期以后的周期,确定为预设周期。例如训练稳定的周期为第M个周期,则可以将第M个周期以后的周期作为预设周期。在预设周期内,可以每间隔一个周期计算一个目标迭代间隔,并根据计算得到的目标迭代间隔调整一次数据位宽或量化参数,以减少数据位宽或量化参数的更新次数,提高神经网络的运行效率。
例如,预设周期为第M个周期以后的周期。在第M+1个周期中,根据第M个周期中的第P个迭代计算得到的目标迭代间隔,截止至第M+1个周期中的第Q个迭代。根据第M+1个周期中的第Q m+1个迭代计算得到与之对应的目标迭代间隔I m+1。在第M+2个周期中,与第M+1个周期中的第Q m+1个迭代对应的迭代为第Q m+2个迭代。在自第M+1个周期中的第Q m+1个迭代开始,直至第M+2个周期中第Q m+2+I m+1个迭代为止,为周期间隔。在周期间隔内的各迭代,均采用第M+1个周期中的第Q m+1个迭代确定的数据位宽或点位置等量化参数。
在本实施例中,可以设置周期间隔,在神经网络运算的训练或微调达到稳定后,根据周期间隔,每周期更新一次数据位宽或点位置等量化参数。周期间隔可以在训练稳定或微调稳定后,减少数据位宽或点位置的更新次数,在保证量化精度的同时,提高神经网络的运行效率。
图3-4示出根据本公开一实施例的神经网络量化方法的流程图。如图3-4所示,该方法应用于神经网络量化装置,所述装置包括控制模块和处理模块,所述处理模块包括第一运算子模块,所述第一运算子模块包括主运算子模块和从运算子模块,所述方法包括步骤S5-11和步骤S5-12。
在步骤S5-11中,利用所述控制模块从神经网络的目标数据中确定出多个待量化数据,并根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,各所述待量化数据的量化数据是利用对应的量化参数进行量化得到的,所述量化参数包括点位置;
在步骤S5-12中,利用所述第一运算子模块进行与所述量化结果相关的运算,得到运算结果,其中,步骤S5-12包括:
利用所述主运算子模块将第一数据发送至所述从运算子模块,所述第一数据包括所述量化结果中根据所述点位置量化得到的第一类型的数据;
利用所述从运算子模块对接收到的所述第一数据进行乘法运算,得到中间结果;
利用所述主运算子模块对所述中间结果和所述量化结果中的除所述第一数据之外的数据进行运算,得到运算结果。
在一种可能的实现方式中,所述量化参数还包括偏移量和/或缩放系数,所述量化结果中还包括第二类型的数据,所述第二类型的数据包括用点位置表示的第一部分、以及用偏移量和/或缩放系数表示的第二部分的数据,
所述第一数据还包括所述量化结果中第二类型的数据的第一部分。
在一种可能的实现方式中,所述处理模块还包括数据转换子模块,所述方法还包括:
利用所述数据转换子模块对待转换数据进行格式转换,得到转换后数据,所述转换后数据的格式类型包括第一类型和第二类型中的任一种,其中,所述待转换数据包括所述目标数据中未进行量化处理的数据,所述第一数据还包括第一类型的转换后数据和/或第二类型的转换后数据中的第一部分,
其中,利用所述主运算子模块对所述中间结果和所述量化结果中的除所述第一数据之外的数据进行运算,得到运算结果,包括:
利用所述主运算子模块对所述中间结果、所述量化结果中的除所述第一数据之外的数据和所述转换后数据中的除所述第一数据之外的数据进行运算,得到运算结果。
在一种可能的实现方式中,所述方法还包括:
利用所述数据转换子模块对根据与各所述待量化数据对应的量化数据得到所述目标数据的待转换量化结果进行格式转换,得到所述量化结果。
在一种可能的实现方式中,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种。
在一种可能的实现方式中,利用所述控制模块从神经网络的目标数据中确定出多个待量化数据,包括以下至少一种方式:
将一层或多层待量化层中的目标数据确定为一个待量化数据;
将一层或多层待量化层中的同一种待运算数据确定为一个待量化数据;
将对应待量化层的目标数据中一个或多个通道中的数据确定为一个待量化数据;
将对应待量化层的目标数据中一个或多个批数的数据确定为一个待量化数据;
将对应待量化层中的目标数据按照确定的划分尺寸划分为一个或多个待量化数据。
在一种可能的实现方式中,所述处理模块还包括第二运算子模块,所述方法还包括:
利用所述第二运算子模块进行所述装置中除所述第一运算子模块所执行的运算处理之外的运算处理。
在一种可能的实现方式中,所述方法还包括:
根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值和对应的数据位宽,得到各所述待量化数据的第一类点位置。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。
在一种可能的实现方式中,所述方法还包括:
根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
在一种可能的实现方式中,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽,包括:
当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
在一种可能的实现方式中,所述方法还包括:
根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
在一种可能的实现方式中,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽,包括:
当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值。
在一种可能的实现方式中,所述方法还包括:
根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
在一种可能的实现方式中,在所述神经网络运算的微调阶段和/或训练阶段,所述方法还包括:
获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
在一种可能的实现方式中,所述方法还包括:
根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
在一种可能的实现方式中,所述方法还包括:
根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位置。
在一种可能的实现方式中,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
其中,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,包括:
根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
在一种可能的实现方式中,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
计算所述第一滑动平均值和所述第二滑动平均值的差值;
将所述差值的绝对值确定为第一数据变动幅度。
在一种可能的实现方式中,所述方法还包括:
根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
其中,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,包括:
根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
在一种可能的实现方式中,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度,包括:
计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;
将所述误差的平方确定为所述第二数据变动幅度。
在一种可能的实现方式中,根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,包括:
根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
在一种可能的实现方式中,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
在一种可能的实现方式中,所述方法还包括:
在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
本公开实施例所提供的神经网络量化方法,利用对应的量化参数对目标数据中的多个待量化数据分别进行量化,且在通过第一运算子模块执行与量化结果相关的运算,在保证精度的同时,减小了存储数据所占用的存储空间,保证了运算结果的准确性和可靠性,且能够提高运算的效率,且量化同样缩减了神经网络模型的大小,降低了对运行该神经网络模型的终端的性能要求,使神经网络模型可以应用于算力、体积、功耗相对受限的手机等终端。
需要说明的是,对于前述的各方法实施例,为了简单描述,故将其都表述为一系列的动作组合,但是本领域技术人员应该知悉,本公开并不受所描述的动作顺序的限制,因为依据本公开,某些步骤可以采用其他顺序或者同时进行。其次,本领域技术人员也应该知悉,说明书中所描述的实施例均属于可选实施例,所涉及的动作和模块并不一定是本公开所必须的。
进一步需要说明的是,虽然图3-4的流程图中的各个步骤按照箭头的指示依次显示,但是这些步骤并不是必然按照箭头指示的顺序依次执行。除非本文中有明确的说明,这些步骤的执行并没有严格的顺序限制,这些步骤可以以其它的顺序执行。而且,图3-4中的至少一部分步骤可以包括多个子步 骤或者多个阶段,这些子步骤或者阶段并不必然是在同一时刻执行完成,而是可以在不同的时刻执行,这些子步骤或者阶段的执行顺序也不必然是依次进行,而是可以与其它步骤或者其它步骤的子步骤或者阶段的至少一部分轮流或者交替地执行。
本公开实施例还提供了一种非易失性计算机可读存储介质,其上存储有计算机程序指令,计算机程序指令被处理器执行时实现上述神经网络的数据量化处理方法。
依据以下条款可更好地理解前述内容:
条款E1.一种神经网络量化装置,所述装置包括控制模块和处理模块,所述处理模块包括第一运算子模块,所述第一运算子模块包括主运算子模块和从运算子模块,
所述控制模块,用于从神经网络的目标数据中确定出多个待量化数据,并根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,各所述待量化数据的量化数据是利用对应的量化参数进行量化得到的,所述量化参数包括点位置;
所述第一运算子模块,用于进行与所述量化结果相关的运算,得到运算结果,
其中,所述主运算子模块,用于将第一数据发送至所述从运算子模块,所述第一数据包括所述量化结果中根据所述点位置量化得到的第一类型的数据;
所述从运算子模块,用于对接收到的所述第一数据进行乘法运算,得到中间结果;
所述主运算子模块,还用于对所述中间结果和所述量化结果中的除所述第一数据之外的数据进行运算,得到运算结果。
条款E2.根据条款E1所述的装置,所述量化参数还包括偏移量和/或缩放系数,所述量化结果中还包括第二类型的数据,所述第二类型的数据包括用点位置表示的第一部分、以及用偏移量和/或缩放系数表示的第二部分的数据,
所述第一数据还包括所述量化结果中第二类型的数据的第一部分。
条款E3.根据条款E1所述的装置,所述处理模块,还包括:
数据转换子模块,用于对待转换数据进行格式转换,得到转换后数据,所述转换后数据的格式类型包括第一类型和第二类型中的任一种,其中,所述待转换数据包括所述目标数据中未进行量化处理的数据,所述第一数据还包括第一类型的转换后数据和/或第二类型的转换后数据中的第一部分,
所述主运算子模块,还用于对所述中间结果、所述量化结果中的除所述第一数据之外的数据和所述转换后数据中的除所述第一数据之外的数据进行运算,得到运算结果。
条款E4.根据条款E3所述的装置,
所述数据转换子模块,还用于对根据与各所述待量化数据对应的量化数据得到所述目标数据的待转换量化结果进行格式转换,得到所述量化结果。
条款E5.根据条款E1至条款E4任一项所述的装置,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种。
条款E6.根据条款E5所述的装置,所述控制模块采用以下至少一种方式确定多个待量化数据:
将一层或多层待量化层中的目标数据确定为一个待量化数据;
将一层或多层待量化层中的同一种待运算数据确定为一个待量化数据;
将对应待量化层的目标数据中一个或多个通道中的数据确定为一个待量化数据;
将对应待量化层的目标数据中一个或多个批数的数据确定为一个待量化数据;
将对应待量化层中的目标数据按照确定的划分尺寸划分为一个或多个待量化数据。
条款E7.根据条款E1所述的装置,所述处理模块,还包括:
第二运算子模块,用于进行所述装置中除所述第一运算子模块所执行的运算处理之外的运算处理。
条款E8.根据权利要求1或条款E2所述的装置,所述控制模块,包括:
参数确定子模块,用于根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
条款E9.根据条款E8所述的装置,所述参数确定子模块,包括:
第一点位置确定子模块,当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值和对应的数据位宽,得到各所述待量化数据的第一类点位置。
条款E10.根据条款E8所述的装置,所述参数确定子模块,包括:
第一最大值确定子模块,当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
第一缩放系数确定子模块,根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数。
条款E11.根据条款E8所述的装置,所述参数确定子模块,包括:
第二点位置确定子模块,当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
条款E12.根据条款E8所述的装置,所述参数确定子模块,包括:
第二最大值确定子模块,当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
第一缩放系数确定子模块,根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。
条款E13.根据条款E8所述的装置,所述参数确定子模块,包括:
偏移量确定子模块,根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。
条款E14.根据条款E1至条款E13中任一项所述的装置,所述控制模块,还包括:
第一量化误差确定子模块,用于根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
调整位宽确定子模块,用于根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
调整量化参数确定子模块,用于将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
条款E15.根据条款E14所述的装置,所述调整位宽确定子模块,包括:
第一调整位宽确定子模块,用于当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应的调整位宽。
条款E16.根据条款E14或条款E15所述的装置,所述控制模块,还包括:
第一调整后量化误差子模块,用于根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
第一调整位宽循环确定模块,用于根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
条款E17.根据条款E14或条款E15所述的装置,所述调整位宽确定子模块,包括:
第二调整位宽确定子模块,用于当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应的调整位宽,所述第二误差阈值小于所述第一误差阈值。
条款E18.根据条款E17所述的装置,所述控制模块,还包括:
第二调整后量化误差子模块,用于根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
第二调整位宽循环确定子模块,用于根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
条款E19.根据条款E1至条款E18中任一项所述的装置,在所述神经网络运算的微调阶段和/或训练阶段,所述控制模块,还包括:
第一数据变动幅度确定子模块,用于获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
目标迭代间隔确定子模块,用于根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
条款E20.根据条款E19所述的装置,所述控制模块,还包括:
第一目标迭代间隔应用子模块,用于根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
条款E21.根据条款E20所述的装置,所述控制模块,还包括:
第二目标迭代间隔应用子模块,用于根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位置。
条款E22.根据条款E19所述的装置,所述第一数据变动幅度确定子模块,包括:
滑动平均值计算子模块,用于根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
第一数据变动幅度确定子模块,用于根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
其中,所述目标迭代间隔确定子模块,包括:
第一目标迭代间隔确定子模块,用于根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款E23.根据条款E22所述的装置,所述第一数据变动幅度确定子模块,包括:
第一幅度确定子模块,用于计算所述第一滑动平均值和所述第二滑动平均值的差值;将所述差值的绝对值确定为第一数据变动幅度。
条款E24.根据条款E23所述的装置,所述控制模块,还包括:
第二数据变动幅度确定子模块,用于根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
其中,目标迭代间隔确定子模块,包括:
第二目标迭代间隔确定子模块,用于根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款E25.根据条款E24所述的装置,所述第二数据变动幅度确定子模块,包括:
第二幅度确定子模块,用于计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之 间的误差;将所述误差的平方确定为所述第二数据变动幅度。
条款E26.根据条款E24所述的装置,所述第二目标迭代间隔确定子模块,包括:
间隔确定子模块,用于根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
条款E27.根据条款E19至条款E26中任一项所述的装置,所述第一数据变动幅度确定子模块,包括:
第二数据变动幅度确定子模块,用于在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
条款E28.根据条款E19至条款E27中任一项所述的装置,所述控制模块,还包括:
周期间隔确定子模块,用于在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
第一周期间隔应用子模块,用于根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
第二周期间隔应用子模块,用于根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
条款E29.一种神经网络量化方法,所述方法应用于神经网络量化装置,所述装置包括控制模块和处理模块,所述处理模块包括第一运算子模块,所述第一运算子模块包括主运算子模块和从运算子模块,所述方法包括:
利用所述控制模块从神经网络的目标数据中确定出多个待量化数据,并根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,各所述待量化数据的量化数据是利用对应的量化参数进行量化得到的,所述量化参数包括点位置;
利用所述第一运算子模块进行与所述量化结果相关的运算,得到运算结果,
其中,利用所述第一运算子模块进行与所述量化结果相关的运算,得到运算结果,包括:
利用所述主运算子模块将第一数据发送至所述从运算子模块,所述第一数据包括所述量化结果中根据所述点位置量化得到的第一类型的数据;
利用所述从运算子模块对接收到的所述第一数据进行乘法运算,得到中间结果;
利用所述主运算子模块对所述中间结果和所述量化结果中的除所述第一数据之外的数据进行运算,得到运算结果。
条款E30.根据条款E29所述的方法,所述量化参数还包括偏移量和/或缩放系数,所述量化结果中还包括第二类型的数据,所述第二类型的数据包括用点位置表示的第一部分、以及用偏移量和/或缩放系数表示的第二部分的数据,
所述第一数据还包括所述量化结果中第二类型的数据的第一部分。
条款E31.根据条款E29所述的方法,所述处理模块还包括数据转换子模块,所述方法还包括:
利用所述数据转换子模块对待转换数据进行格式转换,得到转换后数据,所述转换后数据的格式类型包括第一类型和第二类型中的任一种,其中,所述待转换数据包括所述目标数据中未进行量化处理的数据,所述第一数据还包括第一类型的转换后数据和/或第二类型的转换后数据中的第一部分,
其中,利用所述主运算子模块对所述中间结果和所述量化结果中的除所述第一数据之外的数据进行运算,得到运算结果,包括:
利用所述主运算子模块对所述中间结果、所述量化结果中的除所述第一数据之外的数据和所述转换后数据中的除所述第一数据之外的数据进行运算,得到运算结果。
条款E32.根据条款E31所述的方法,所述方法还包括:
利用所述数据转换子模块对根据与各所述待量化数据对应的量化数据得到所述目标数据的待转换量化结果进行格式转换,得到所述量化结果。
条款E33.根据条款E29至条款E32任一项所述的方法,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种。
条款E34.根据条款E33所述的方法,利用所述控制模块从神经网络的目标数据中确定出多个待量化数据,包括以下至少一种方式:
将一层或多层待量化层中的目标数据确定为一个待量化数据;
将一层或多层待量化层中的同一种待运算数据确定为一个待量化数据;
将对应待量化层的目标数据中一个或多个通道中的数据确定为一个待量化数据;
将对应待量化层的目标数据中一个或多个批数的数据确定为一个待量化数据;
将对应待量化层中的目标数据按照确定的划分尺寸划分为一个或多个待量化数据。
条款E35.根据条款E29所述的方法,所述处理模块还包括第二运算子模块,所述方法还包括:
利用所述第二运算子模块进行所述装置中除所述第一运算子模块所执行的运算处理之外的运算处理。
条款E36.根据条款E29或条款E30所述的方法,所述方法还包括:
根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
条款E37.根据条款E36所述的方法,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值和对应的数据位宽,得到各所述待量化数据的第一类点位置。
条款E38.根据条款E36所述的方法,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数。
条款E39.根据条款E36所述的方法,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
条款E40.根据条款E36所述的方法,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。
条款E41.根据条款E36所述的方法,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。
条款E42.根据条款E29至条款E41任一项所述的方法,所述方法还包括:
根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
条款E43.根据条款E42所述的方法,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽,包括:
当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应的调整位宽。
条款E44.根据条款E42或条款E43所述的方法,所述方法还包括:
根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
条款E45.根据条款E42或条款E43所述的方法,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽,包括:
当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应的调整位宽,所述第二误差阈值小于所述第一误差阈值。
条款E46.根据条款E45所述的方法,所述方法还包括:
根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
条款E47.根据条款E29至条款E44任一项所述的方法,在所述神经网络运算的微调阶段和/或训练阶段,所述方法还包括:
获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
条款E48.根据条款E47所述的方法,所述方法还包括:
根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
条款E49.根据条款E48所述的方法,所述方法还包括:
根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位置。
条款E50.根据条款E47所述的方法,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
其中,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,包括:
根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款E51.根据条款E50所述的方法,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
计算所述第一滑动平均值和所述第二滑动平均值的差值;
将所述差值的绝对值确定为第一数据变动幅度。
条款E52.根据条款E51所述的方法,所述方法还包括:
根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
其中,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,包括:
根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款E53.根据条款E52所述的方法,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度,包括:
计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;
将所述误差的平方确定为所述第二数据变动幅度。
条款E54.根据条款E52所述的方法,根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,包括:
根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
条款E55.根据条款E47至条款E54任一项所述的方法,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
条款E56.根据条款E47至条款E55任一项所述的方法,所述方法还包括:
在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
条款E57.一种人工智能芯片,所述芯片包括如条款E1至条款E28中任意一项所述的神经网络量化装置。
条款E58.一种电子设备,所述电子设备包括如条款E57所述的人工智能芯片。
条款E59.一种板卡,所述板卡包括:存储器件、接口装置和控制器件以及如条款E58所述的人工智能芯片;
其中,所述人工智能芯片与所述存储器件、所述控制器件以及所述接口装置分别连接;
所述存储器件,用于存储数据;
所述接口装置,用于实现所述人工智能芯片与外部设备之间的数据传输;
所述控制器件,用于对所述人工智能芯片的状态进行监控。
条款E60.根据条款E59所述的板卡,
所述存储器件包括:多组存储单元,每一组所述存储单元与所述人工智能芯片通过总线连接,所述存储单元为:DDR SDRAM;
所述芯片包括:DDR控制器,用于对每个所述存储单元的数据传输与数据存储的控制;
所述接口装置为:标准PCIE接口。
条款E61.一种非易失性计算机可读存储介质,其上存储有计算机程序指令,所述计算机程序指令被处理器执行时实现条款E29至条款E56中任意一项所述的神经网络量化方法。
随着神经网络运算复杂度的提高,数据的数据量和数据维度也在不断增大,而传统的神经网络算法通常采用浮点数据格式来执行神经网络运算,这就使得不断增大的数据量等对运算装置的数据处理效率、存储装置的存储容量及访存效率等提出了较大的挑战。为解决上述问题,相关技术中,对神经网络运算过程涉及的全部数据均由浮点数转化定点数,但由于不同的数据之间具有差异性,或者,同一数据在不同阶段具有差异性,仅“由浮点数转化定点数”时,往往会导致精度不够,从而会影响运算结果。
神经网络中待运算数据通常为浮点数据格式或精度较高的定点数据格式,在承载神经网络的装置中运行神经网络时,浮点数据格式或精度较高的定点数据格式的各种待运算数据,导致神经网络运行的运算量和访存开销都较大。为提高运算效率,本公开实施例所提供的神经网络量化方法、装置、计算机设备和存储介质,输入神经元和梯度可以按批数(batch)对每一批的输入神经元和/或梯度进行划分得到多个待量化数据,进而进行量化,量化后的数据格式通常为位宽较短、精度较低的定点数据格式。利用精度较低的量化后数据执行神经网络的运算,可以降低运算量和访存量。量化后的数据格式可以为位宽较短的定点数据格式。可以将浮点数据格式的待运算数据量化为定点数据格式的待运算数据,也可以将精度较高的定点格式的待运算数据量化为精度较低的定点格式的待运算数据。利用对应的量化参数对待量化数据进行量化,在保证精度的同时,减小了存储数据所占用的存储空间,保证了运算结果的准确性和可靠性,且能够提高运算的效率,且量化同样缩减了神经网络模型的大小,降低了对运行该神经网络模型的终端的性能要求,使神经网络模型可以应用于算力、体积、功耗相对受限的手机等终端。
可以理解的是,量化精度即量化后数据与量化前数据之间的误差的大小。量化精度可以影响神经网络运算结果的准确度。量化精度越高,运算结果的准确率越高,但运算量更大、访存开销也更大。相较于位宽较短的量化后数据,位宽较长的量化后数据的量化精度更高,用于执行神经网络的运算时准确率也更高。但在用于进行神经网络的运算时,位宽较长的量化后数据运算量更大、访存开销也较大,运算效率较低。同理,对于相同的待量化数据,采用不同的量化参数得到的量化后数据有不同的量化精度,将产生不同的量化结果,对运算效率和运算结果的准确率也会带来不同的影响。对神经网络进行量化,在运算效率和运算结果的准确率之间进行平衡,可以采用更加符合待运算数据的数据特征的量化后数据位宽和量化参数。
神经网络中的待运算数据可以包括权值、神经元、偏置、梯度中的至少一种。待运算数据为包含多个元素的矩阵。在传统的神经网络量化中,通常将待运算数据的整体进行量化后进行运算。而在利用量化后的待运算数据进行运算时,通常利用整体量化后的待运算数据中的一部分数据进行运算。例如,在卷积层,利用整体量化后的输入神经元进行卷积运算时,根据卷积核的维度和步长,在整体量化后的输入神经元中分别提取与卷积核的维度相当的量化后的神经元进行卷积运算。在全连接层,利用整体量化后的输入神经元进行矩阵乘运算时,在整体量化后的输入神经元中分别按行提取量化后的 神经元进行矩阵乘的运算。因此,在传统的神经网络量化方法中,将待运算数据的整体进行量化后再按照部分量化后的数据进行运算,整体的运算效率较低。且将待运算数据的整体量化后再进行运算,需要将整体量化后的待运算数据进行存储,占用的存储空间较大,尤其整体进行量化对运算神经网络的终端的存储能力要求较高,不能满足性能较低的终端的使用需求。
根据本公开实施例的神经网络量化方法可应用于处理器中,该处理器可以是通用处理器,例如CPU(Central Processing Unit,中央处理器),也可以是用于执行人工智能运算的人工智能处理器(IPU)。人工智能运算可包括机器学习运算,类脑运算等。其中,机器学习运算包括神经网络运算、k-means运算、支持向量机运算等。该人工智能处理器可例如包括GPU(Graphics Processing Unit,图形处理单元)、NPU(Neural-Network Processing Unit,神经网络处理单元)、DSP(Digital Signal Process,数字信号处理单元)、现场可编程门阵列(Field-Programmable Gate Array,FPGA)芯片中的一种或组合。本公开对处理器的具体类型不作限制。
在一种可能的实现方式中,本公开中所提及的处理器可包括多个处理单元,每个处理单元可以独立运行所分配到的各种任务,如:卷积运算任务、池化任务或全连接任务等。本公开对处理单元及处理单元所运行的任务不作限制。
图4-1示出根据本公开实施例的神经网络量化方法的流程图。如图4-1所示,该方法可以应用于神经网络中的任一层,该方法包括步骤S6-11至步骤S6-13。该方法可以应用于图1所示的处理器100。其中,处理单元101用于执行步骤S6-11至步骤S6-13。存储单元102用于存储待量化数据、量化参数、数据位宽等与步骤S6-11至步骤S6-13的处理过程相关的数据。
在步骤S6-11中,根据运行所述神经网络的终端的存储能力,从所述待量化层的目标数据中确定多个待量化数据,所述目标数据为所述待量化层中的任意一批待运算数据,所述待运算数据包括输入神经元、梯度中的至少一种。
神经网络中的待量化层可以为神经网络中的任意一层。可以根据需求将神经网络中的部分层或全部层确定为待量化层。当神经网络中包括多个待量化层时,各待量化层可以连续也可以不连续。根据神经网络的不同,待量化层的种类也可以不同,例如待量化层可以为卷积层、全连接层等,本公开对待量化层的数量及类型不作限定。
对于所述待运算数据还可以包括权值、偏置。可以根据需求将待量化层中的权值、梯度中的至少一种进行量化。量化参数可以包括点位置(包括下文所述的第一类点位置和第二类点位置)、缩放系数(包括下文所述的第一类缩放系数和第二类缩放系数)和偏移量中的一种或多种。上述步骤S6-11至步骤S6-13所提供的量化方式是适用于缩放参数包括偏移量,且包括第二类点位置和/或第二类缩放系数的输入神经元和梯度。对于量化参数包括不同于步骤S6-11至步骤S6-13所适用的其他参数组合(如包括第一类点位置和/或第一类缩放系数)的待运算数据,可以采用其他方式进行待量化数据选取,选取后根据待量化数据及其对应的数据位宽确定对应的量化参数,进而根据对应的量化参数实现量化。例如,可以将待量化层中的权值作为待量化数据进行量化,或者可以将待量化层中的偏置进行量化。不同的量化参数的确定方式可以参照下文描述。
当待量化层中有多种目标数据时,针对每种目标数据可以采用本公开中的量化方法进行量化后,得到与各目标数据对应的量化数据,再利用各种目标数据的量化数据和不需要进行量化的待运算数据执行待量化层的运算。
神经网络运算的推理阶段可包括:将训练好的神经网络进行前向运算以完成设定任务的阶段。在神经网络的推理阶段,可以将神经元、权值、偏置和梯度中的至少一种作为待量化数据,根据本公开实施例中的方法进行量化后,利用量化后的数据完成待量化层的运算。
神经网络运算的微调阶段可包括:将训练好的神经网络进行预设数量迭代的前向运算和反向运算, 进行参数的微调以适应设定任务的阶段。在神经网络运算的微调阶段,可以将神经元、权值、偏置、梯度中的至少一种,根据本公开实施例中的方法进行量化后,利用量化后的数据完成待量化层的前向运算或反向运算。
神经网络运算的训练阶段可包括:将初始化的神经网络进行迭代训练以得到训练好的神经网络的阶段,训练好的神经网络可执行特定任务。在神经网络的训练阶段,可以将神经元、权值、偏置、梯度中的至少一种,根据本公开实施例中的方法进行量化后,利用量化后的数据完成待量化层的前向运算或反向运算。
也可以根据预先设定的数据划分方式,将目标数据划分为多个待量化数据。例如,预先设定的数据划分方式可以为:按照固定的数据大小进行划分,或按照固定的数据形状进行划分。
将目标数据划分为多个待量化数据后,可以将各待量化数据分别进行量化,并根据各待量化数据量化后的数据进行运算。一个待量化数据所需的量化时间,短于目标数据的整体的量化时间,将其中一个待量化数据量化完毕后,即可以用量化后的数据执行后续的运算,而不用等目标数据中的所有待量化数据均量化完成后再执行运算。因此本公开中的目标数据的量化方法,可以提高目标数据的运算效率。
在步骤S6-12中,采用循环处理的方式,依次根据每个待量化数据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据。
可以采用循环处理的方式,将同一批待运算数据通过多次量化,完成该批待运算数据的量化。
在一种可能的实现方式中,步骤S6-12可以包括:
在上一次循环处理过程结束之后开始当前循环处理过程,将上一次循环处理过程量化的待量化数据之后的下一待量化数据,确定为当前循环处理过程所需量化的当前待量化数据;
对当前待量化数据的统计结果和存储的参考统计结果进行比较,得到比较结果;
在所述比较结果满足量化参数计算条件时,根据当前待量化数据的统计结果和所述参考统计结果,确定更新统计结果;
根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数;
根据对应的量化参数对当前待量化数据进行量化,得到与当前待量化数据对应的量化数据,结束当前循环处理过程。
在一种可能的实现方式中,步骤S6-12还可以包括:将所述更新统计结果以及对应所述更新统计结果的量化参数,分别作为新的参考统计结果和新的参考量化参数进行存储。
在一种可能的实现方式中,步骤S6-12还可以包括:在所述比较结果不满足所述量化参数计算条件时,将所述参考统计结果所对应的参考量化参数确定为当前待量化数据所对应的量化参数。
在一种可能的实现方式中,当前待量化数据的统计结果包括所述当前待量化数据中的最大值和最小值,所述参考统计结果包括参考最大值和参考最小值,
其中,在所述比较结果满足量化参数计算条件时,根据当前待量化数据的统计结果和所述参考统计结果,确定更新统计结果,可以包括以下任一种方式:
在所述当前待量化数据中的最大值大于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述当前待量化数据的统计结果确定为更新统计结果;
在所述当前待量化数据中的最大值小于或等于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述参考最大值和所述当前待量化数据中的最小值确定为更新统计结果;
在所述当前待量化数据中的最大值大于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述当前待量化数据中的最大值和所述参考最小值确定为更新统计结果。
在该实现方式中,在针对某一批待运算数据的多个待量化数据进行量化时,可以将第一个待量化 数据的统计结果和对应的量化参数分别确定为参考统计结果和参考量化参数。
在步骤S6-13中,根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
可以利用设定的量化算法,根据量化参数对待量化数据进行量化,得到量化数据。例如,可以利用取整算法作为量化算法,可以根据数据位宽和点位置对待量化数据进行取整量化得到量化数据。其中,取整算法可以包括向上取整、向下取整、向零取整和四舍五入取整等。本公开对量化算法的具体实现方式不作限定。
各待量化数据可以分别采用对应的量化参数进行量化。由于与各待量化数据对应的量化参数更为贴合各待量化数据自身的特征,使得各待量化层的每种量化数据的量化精度更加符合目标数据的运算需求,也就更加符合待量化层的运算需求。在保证待量化层的运算结果准确率的前提下,能够提高待量化层的运算效率,达到待量化层的运算效率和运算结果准确率之间的平衡。进一步的,将目标数据划分为多个待量化数据分别量化,可以在量化完一个待量化数据后,根据量化得到的量化结果执行运算的同时,可以进行第二个待量化数据的量化,从而在整体上提高目标数据的运算效率,也就提高了待量化层的计算效率。
可以将各待量化数据的量化数据进行合并后得到目标数据的量化结果。也可以将各待量化数据的量化数据进行设定的运算后得到目标数据的量化结果。例如可以将各待量化数据的量化数据按照设定的权重进行加权运算后得到目标数据的量化结果。本公开对此不作限定。
在神经网络的推理、训练和微调过程中,可以对待量化数据进行离线量化或在线量化。其中,离线量化可以为利用量化参数对待量化数据进行离线处理。在线量化可以为利用量化参数对待量化数据进行在线处理。例如,神经网络运行在人工智能芯片上,可以将待量化数据和量化参数发送至人工智能芯片之外的运算装置进行离线量化,或利用人工智能芯片之外的运算装置对预先得到的待量化数据和量化参数进行离线量化。而在人工智能芯片运行神经网络的过程中,人工智能芯片可以对待量化数据利用量化参数进行在线量化。本公开中对各待量化数据的量化过程为在线或离线不作限定。
在本实施例所提供的神经网络量化方法,对对于所述神经网络中的任意待量化层,所述方法包括:根据运行所述神经网络的终端的存储能力,从所述待量化层的目标数据中确定多个待量化数据,所述目标数据为所述待量化层中的任意一批待运算数据,所述待运算数据包括输入神经元和/或梯度中的至少一种;采用循环处理的方式,依次根据每个待量化数据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据;根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。将同一批数的待运算数据划分为多个待量化数据后,采用循环处理的方式依次对多个待量化数据进行量化,从而可以提高目标数据的量化效率和运算效率,也可以提高待量化层直至提高整个神经网络的量化效率和运算效率。
在一种可能的实现方式中,对于除上述步骤S6-11至步骤S6-13所提供方法适用的其余待量化数据,确定与待量化数据对应的量化参数的方式可以包括方式一’-方式三’,方式一’:确定与目标数据对应的量化参数后,并将与目标数据对应的量化参数确定为待量化数据的量化参数的方式。当待量化层包括多个目标数据时,各目标数据均可以有与之对应的量化参数,且各目标数据对应的量化参数可以不同,也可以相同,本公开对此不作限定。将目标数据划分为多个待量化数据后,可以将目标数据对应的量化参数确定为各待量化数据对应的量化参数,此时各待量化数据对应的量化参数相同,而后依次对多个待量化数据进行量化。
方式二’:直接确定各待量化数据对应的量化参数的方式。目标数据可以没有与之对应的量化参数,或目标数据可以有与之对应的量化参数但待量化数据不采用。可以直接为各待量化数据设定对应的量化参数。也可以根据待量化数据计算得到对应的量化参数。此时各待量化数据对应的量化参数可 以相同也可以不同。例如,当待量化层为卷积层,目标数据为权重时,可以将权重按照通道划分为多个待量化权重数据,不同通道的待量化权重数据可以对应不同的量化参数。当各待量化数据对应的量化参数不同时,各待量化数据利用对应的量化参数进行量化后,所得到的量化结果需不影响目标数据的运算。
方式三’,可以包括:查找预设的量化参数直接确定量化参数的方式、查找对应关系以确定量化参数的方式,或根据待量化数据计算得到量化参数的方式。以下以确定与待量化数据对应的量化参数的方式为例进行说明:
可以直接设定与待量化数据对应的量化参数。可以将设定好的量化参数存储于设定的存储空间。设定的存储空间可以为片上或片外的存储空间。例如,可以将设定好的量化参数存储于设定的存储空间。各待量化数据在进行量化时,可以在设定的存储空间提取对应的量化参数后进行量化。可以根据经验值设定与每种待量化数据对应的量化参数。也可以根据需求更新所存储的与每种待量化数据对应的量化参数。
可以根据各待量化数据的数据特征,通过查找数据特征与量化参数的对应关系,确定量化参数。例如,待量化数据的数据分布为稀疏和稠密时可以分别对应不同的量化参数。可以通过查找对应关系确定与待量化数据的数据分布对应的量化参数。
还可以根据各待量化数据,利用设定的量化参数计算方法,计算得到各待量化层对应的量化参数。例如,可以根据待量化数据的绝对值最大值和预设的数据位宽,利用取整算法计算得到量化参数中的点位置。
在一种可能的实现方式中,该方法还可以包括:根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
在该实现方式中,可以对待量化数据进行统计,根据统计结果和数据位宽确定待量化数据对应的量化参数。量化参数可以包括点位置、缩放系数和偏移量中的一种或多种。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,可以包括:
当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值Z 1和对应的数据位宽,得到各所述待量化数据的第一类点位置。其中,该绝对值最大值Z 1是待量化数据中数据取绝对值后所得到的最大值。
在该实现方式中,当待量化数据为相对于原点对称的数据时,量化参数可以不包括偏移量,假设Z 1为待量化数据中元素的绝对值的最大值,待量化数据对应的数据位宽为n,A 1为用数据位宽n对待量化数据进行量化后的量化数据可以表示的最大值,A 1
Figure PCTCN2020095673-appb-000090
A 1需要包含Z 1,且Z 1要大于
Figure PCTCN2020095673-appb-000091
因此有公式(6-1)的约束:
Figure PCTCN2020095673-appb-000092
处理器可以根据待量化数据中的绝对值最大值Z 1和数据位宽n,计算得到第一类点位置s 1。例如,可以利用如下公式(6-2)计算得到待量化数据对应的第一类点位置s 1
Figure PCTCN2020095673-appb-000093
其中,ceil为向上取整,Z 1为待量化数据中的绝对值最大值,s 1为第一类点位置,n为数据位宽。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,可以包括:
当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽, 得到各所述待量化数据的第二类点位置s 2。或者,当所述量化参数包括偏移量时,根据所述更新统计结果中的最大值、最小值和对应的数据位宽,得到所述当前待量化数据的第二类点位置s 2
在该实现方式中,可以先获取待量化数据(或者更新统计结果)中的最大值Z max、最小值Z min,进而根据最大值Z max、最小值Z min利用下述公式(6-3)进行计算,
Figure PCTCN2020095673-appb-000094
进一步地,根据计算得到的Z 2和对应的数据位宽利用下述公式(6-4)计算第二类点位置s 2
Figure PCTCN2020095673-appb-000095
在该实现方式中,由于量化时,常规情况下会将待量化数据中的最大值和最小值保存下来,直接基于保存的待量化数据中的最大值和最小值来获取绝对值最大值,无需消耗更多的资源去对待量化数据求绝对值,节省确定统计结果的时间。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数f’。其中,第一类缩放系数f’可以包括第一缩放系数f 1和第二缩放系数f 2
其中,该第一缩放系数f 1可以按照如下方式(6-5)进行计算:
Figure PCTCN2020095673-appb-000096
其中,第二缩放系数f 2可以按照如下公式(6-6)进行计算:
Figure PCTCN2020095673-appb-000097
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,可以包括:
根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。或者,根据所述更新统计结果中的最大值和最小值,得到当前待量化数据的偏移量。
在该实现方式中,图4-2示出根据本公开实施例的对称的定点数表示的示意图。如图4-2所示的待量化数据的数域是以“0”为对称中心分布。Z 1为待量化数据的数域中所有浮点数的绝对值最大值,在图4-2中,A 1为n位定点数可以表示的浮点数的最大值,浮点数A 1转换为定点数是(2 n-1-1)。为了避免溢出,A 1需要包含Z 1。在实际运算中,神经网络运算过程中的浮点数据趋向于某个确定区间的正态分布,但是并不一定满足以“0”为对称中心的分布,这时用定点数表示时,容易出现溢出情况。为了改善这一情况,量化参数中引入偏移量。图4-3示出根据本公开实施例的引入偏移量的定点数表示的示意图。如图4-3所示。待量化数据的数域不是以“0”为对称中心分布,Z min是待量化数据的数域中所有浮点数的最小值,Z max是待量化数据的数域中所有浮点数的最大值,A 2为用n位定点数表示的平移后的浮点数的最大值,A 2
Figure PCTCN2020095673-appb-000098
P为Z min~Z max之间的中心点,将待量化数据的数域整体偏移,使得平移后的待量化数据的数域以“0”为对称中心分布,以避免数据的“溢出”。平移后的待量化数据的数域中的绝对值最大值为Z 2。由图4-3可知,偏移量为“0”点到“P”点之间的水平距离,该距离称为偏移量o。
可以根据该最小值Z min和最大值Z max按照如下公式(6-7)计算获得偏移量:
Figure PCTCN2020095673-appb-000099
其中,o表示偏移量,Z min表示待量化数据所有元素中(或者更新统计结果中)的最小值,Z max表示待量化数据所有元素中(或者更新统计结果中)的最大值。
在一种可能的实现方式中,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,可以包括:
当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数f”。其中,第二类缩放系数f”可以包括第三缩放系数f 3和第四缩放系数f 4。或者,根据所述更新统计结果中的最大值、最小值和量化后数据的最大值,得到当前待量化数据的第二类缩放系数。
在该实现方式中,当量化参数包括偏移量时,A 2为用数据位宽n对平移后的待量化数据进行量化后的量化数据可以表示的最大值,A 2
Figure PCTCN2020095673-appb-000100
可以根据待量化数据中(或者更新统计结果中)的Z max、最小值Z min进行计算得到平移后的待量化数据的数域中的绝对值最大值Z 2,进而按照如下公式(6-8)计算第三缩放系数f 3
Figure PCTCN2020095673-appb-000101
进一步地,第四缩放系数f 4可以按照如下公式(6-9)进行计算:
Figure PCTCN2020095673-appb-000102
在对待量化数据进行量化时,所采用的量化参数不同,进行量化所使用的数据不同。
在一种可能的实现方式中,量化参数可以包括第一类点位置s 1。可以利用如下的公式(6-10)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000103
其中,I x为量化数据,F x为待量化数据,round为进行四舍五入的取整运算。
量化参数可以包括第一类点位置s 1时,可以根据公式(6-11)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000104
Figure PCTCN2020095673-appb-000105
在一种可能的实现方式中,量化参数可以包括第一类点位置和第一缩放系数。可以利用如下的公式(6-12)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000106
当量化参数包括第一类点位置和第一缩放系数时,可以根据公式(6-13)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000107
Figure PCTCN2020095673-appb-000108
在一种可能的实现方式中,量化参数可以包括第二缩放系数。可以利用如下的公式(6-14)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000109
当量化参数包括第二缩放系数时,可以根据公式(6-15)对目标数据的量化数据进行反量化,得 到目标数据的反量化数据
Figure PCTCN2020095673-appb-000110
Figure PCTCN2020095673-appb-000111
在一种可能的实现方式中,量化参数可以包括偏移量。可以利用如下的公式(6-16)对待量化数据进行量化,得到量化数据I x
I x=round(F x-o)       公式(6-16)
当量化参数包括偏移量时,可以根据公式(6-17)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000112
Figure PCTCN2020095673-appb-000113
在一种可能的实现方式中,量化参数可以包括第二类点位置和偏移量。可以利用如下的公式(6-18)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000114
当量化参数包括第二类点位置和偏移量时,可以根据公式(6-19)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000115
Figure PCTCN2020095673-appb-000116
在一种可能的实现方式中,量化参数可以包括第二类缩放系数f”和偏移量o。可以利用如下的公式(6-20)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000117
当量化参数包括第二类缩放系数和偏移量时,可以根据公式(6-21)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000118
Figure PCTCN2020095673-appb-000119
在一种可能的实现方式中,量化参数可以包括第二类点位置、第二类缩放系数和偏移量。可以利用如下的公式(6-22)对待量化数据进行量化,得到量化数据I x
Figure PCTCN2020095673-appb-000120
当量化参数包括第二类点位置、第二类缩放系数和偏移量时,可以根据公式(6-23)对目标数据的量化数据进行反量化,得到目标数据的反量化数据
Figure PCTCN2020095673-appb-000121
Figure PCTCN2020095673-appb-000122
可以理解的是,也可以采用其他的取整运算方法,例如采用向上取整、向下取整、向零取整等取整运算,替换上述公式中的四舍五入的取整运算round。可以理解的是,在数据位宽一定的情况下,根据点位置量化得到的量化数据中,小数点后的位数越多,量化数据的量化精度越大。
在一种可能的实现方式中,上述步骤S6-11可以包括:
通过查找待量化数据与量化参数对应关系,确定与所述待量化层中每种待量化数据对应的量化参数。
在一种可能的实现方式中,各待量化层中与每种待量化数据对应的量化参数,可以是保存的预设值。可以为神经网络建立一个待量化数据与量化参数之间的对应关系,该对应关系可以包括各待量化层的每种待量化数据与量化参数对应关系,并将对应关系保存在各层可以共享访问的存储空间。也可 以为神经网络建立多个待量化数据与量化参数之间的对应关系,各待量化层分别对应其中一个对应关系。可以将各层的对应关系保存在本层独享的存储空间,也可以将各层的对应关系保存在各层可以共享访问的存储空间。
在待量化数据与量化参数对应关系中,可以包括多个待量化数据和与之对应的多个量化参数之间的对应关系。例如,待量化数据与量化参数对应关系A中,可以包括待量化层1的神经元和权值两个待量化数据,神经元对应点位置1、缩放系数1和偏移量1三个量化参数,权值对应点位置2和偏移量2两个量化参数。本公开对待量化数据与量化参数对应关系的具体格式不作限定。
在本实施例中,可以通过查找待量化数据与量化参数对应关系,确定与所述待量化层中每种待量化数据对应的量化参数。可以为各待量化层预设对应的量化参数,并通过对应关系进行存储后,供待量化层查找后使用。本实施例中量化参数的获取方式简单方便。
图4-4示出根据本公开实施例的神经网络量化方法的流程图。在一种可能的实现方式中,如图4-4所示,该方法还可以包括步骤S6-14至步骤S6-16。
在步骤S6-14中,根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差。
可以根据待量化数据对应的量化数据与待量化数据之间的误差,确定待量化数据的量化误差。可以利用设定的误差计算方法,例如标准差计算方法、均方根误差计算方法等,计算待量化数据的量化误差。
也可以根据量化参数,将待量化数据对应的量化数据进行反量化后得到反量化数据,根据反量化数据与待量化数据之间的误差,按照公式(6-24)确定待量化数据的量化误差diff bit
Figure PCTCN2020095673-appb-000123
其中,F i为待量化对应的浮点值,其中,i为待量化数据中数据的下标。
Figure PCTCN2020095673-appb-000124
为浮点值对应的反量化数据。
还可以根据量化间隔、量化后的数据的个数以及对应的量化前的数据按照公式(6-25)确定量化误差diff bit
Figure PCTCN2020095673-appb-000125
其中,C为量化时对应的量化间隔,m为量化后获得的量化数据的个数,F i为待量化对应的浮点值,其中,i为待量化数据中数据的下标。
也可以根据量化后的数据以及对应的反量化数据按照公式(6-26)确定量化误差diff bit
Figure PCTCN2020095673-appb-000126
其中,F i为待量化对应的浮点值,其中,i为待量化数据集合中数据的下标。
Figure PCTCN2020095673-appb-000127
为浮点值对应的反量化数据。
在步骤S6-15中,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽。
可以根据经验值确定误差阈值,误差阈值可以用于表示对量化误差的期望值。当量化误差大于或小于误差阈值时,可以调整待量化数对应的数据位宽,得到待量化数据对应的调整位宽。可以将数据位宽调整为更长的位宽或更短的位宽,以提高或降低量化精度。
可以根据能够接受的最大误差确定误差阈值,当量化误差大于误差阈值时,说明量化精度不能达到预期,需要将数据位宽调整为更长的位宽。也可以根据较高的量化精度确定一个较小的误差阈值,当量化误差小于误差阈值时,说明量化精度较高,神经网络的运行效率将受到影响,可以适当的将数据位宽调整为更短的位宽,以适当的降低量化精度,提高神经网络的运行效率。
可以将数据位宽按照固定的位数步长进行调整,也可以根据量化误差与误差阈值之间的差值的不同,按照可变的调整步长调整数据位宽。本公开对此不作限定。
在步骤S6-16中,将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
确定调整位宽后,可以将待量化数据对应的数据位宽更新为调整位宽。例如,待量化数据更新前的数据位宽为8位,调整位宽为12位,则更新后待量化数据对应的数据位宽为12位。可以根据调整位宽和待量化数据计算得到待量化数据对应的调整量化参数。可以根据待量化数据对应的调整量化参数重新对待量化数据进行量化,以得到量化精度更高或更低的量化数据,使得待量化层在量化精度和处理效率之间达到平衡。
在神经网络的推理、训练和微调过程中,各层之间的待量化数据可以认为具有一定的关联性。例如,各层的待量化数据之间的均值之间的差小于设定的均值阈值,且各层的待量化数据之间的最大值之间的差值也小于设定的差值阈值时,可以将待量化层的调整量化参数作为后续的一个或多个层的调整量化参数,用于对待量化层后续的一个或多个层的待量化数据进行量化。也可以在神经网络的训练和微调过程中,将待量化层在当前迭代得到的调整量化参数,用于在后续的迭代中对待量化层进行量化。
在一种可能的实现方式中,所述方法还包括:
在所述待量化层之后的一层或多层采用所述待量化层的量化参数。
神经网络根据调整量化参数进行量化,可以包括只在待量化层利用调整量化参数对待量化数据重新进行量化,并将重新得到的量化数据用于待量化层的运算。也可以包括在待量化层不使用调整量化参数重新对待量化数据进行量化,而在待量化层后续的一个或多个层使用调整量化参数进行量化,和/或后续的迭代中在待量化层使用调整量化参数进行量化。还可以包括在待量化层使用调整量化参数重新进行量化,并将重新得到的量化数据用于待量化层的运算,并且在待量化层后续的一个或多个层使用调整量化参数进行量化,和/或后续的迭代中在待量化层使用调整量化参数进行量化。本公开对此不作限定。
在本实施例中,根据待量化数据和待量化数据对应的量化数据之间的误差调整数据位宽,并根据调整后的数据位宽计算得到调整量化参数。通过设置不同的误差阈值可以得到不同的调整量化参数,达到提高量化精度或提高运行效率等不同的量化需求。根据待量化数据和待量化数据的量化数据计算得到的调整量化参数,也能够更加符合待量化数据自身的数据特征,达到更加符合待量化数据自身需 求的量化结果,在量化精度和处理效率之间达到更好的平衡。
在一种可能的实现方式中,步骤S6-15可以包括:
当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
可以根据能够接受的最大的量化误差,确定第一误差阈值。可以将量化误差与第一误差阈值进行比较。当量化误差大于第一误差阈值时,可以认为量化误差已经不可接受。需要提高量化精度,可以通过增加待量化数据对应的数据位宽的方式,提高待量化数据的量化精度。
可以将待量化数据对应的数据位宽按照固定的调整步长增加,得到调整位宽。固定的调整步长可以为N位,N为正整数。每次调整数据位宽可以增加N位。每次增加后的数据位宽=原数据位宽+N位。
可以将待量化数据对应的数据位宽按照可变的调整步长增加,得到调整位宽。例如,当量化误差与误差阈值之间的差值大于第一阈值时,可以按照调整步长M1调整数据位宽,当量化误差与误差阈值之间的差值小于第一阈值时,可以按照调整步长M2调整数据位宽,其中,第一阈值大于第二阈值,M1大于M2。可以根据需求确定各可变的调整步长。本公开对数据位宽的调整步长及调整步长是否可变不作限定。
可以将待量化数据按照调整位宽计算得到调整后的量化参数。利用调整后的量化参数对待量化数据进行重新量化后得到的量化数据,比利用调整前的量化参数量化得到的量化数据的量化精度更高。
在一种可能的实现方式中,该方法还可以包括:
根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
根据量化误差增加待量化数据对应的数据位宽时,调整一次位宽后得到调整位宽,根据调整位宽计算得到调整后的量化参数,根据调整后的量化参数量化待量化数据得到调整后的量化数据,再根据调整后的量化数据与待量化数据计算得到待量化数据调整后的量化误差,调整后的量化误差可能依然大于第一误差阈值,即根据调整一次的数据位宽可能不能满足调整目的。当调整后的量化误差依然大于第一误差阈值时,可以继续对调整后的数据位宽进行调整,即多次增加待量化数据对应的数据位宽,直至根据最终得到的调整位宽和待量化数据得到的调整后的量化误差小于第一误差阈值。
多次增加的调整步长可以是固定的调整步长,也可以是可变的调整步长。例如,最终的数据位宽=原数据位宽+B*N位,其中N为每次增加的固定的调整步长,B为数据位宽的增加次数。最终的数据位宽=原数据位宽+M1+M2+…+Mm,其中,M1、M2…Mm为每次增加的可变的调整步长。
在本实施例中,当量化误差大于第一误差阈值时,增加所述待量化数据对应的数据位宽,得到所述待量化数据对应的调整位宽。可以通过设置第一误差阈值和调整步长增加数据位宽,以使调整后的数据位宽能够满足量化的需求。当一次调整不能满足调整需求时,还可以对数据位宽进行多次调整。第一误差阈值和调整步长的设置,使得量化参数可以按照量化需求进行灵活调整,满足不同的量化需求,使得量化精度可根据自身数据特征进行自适应调整。
在一种可能的实现方式中,步骤S6-15可以包括:
当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值
可以根据能够接受的量化误差和期望的神经网络的运行效率,确定第二误差阈值。可以将量化误差与第二误差阈值进行比较。当量化误差小于第二误差阈值时,可以认为量化误差超出预期,但运行效率过低已经不可接受。可以降低量化精度以提高神经网络的运行效率,可以通过减少待量化数据对应的数据位宽的方式,降低待量化数据的量化精度。
可以将待量化数据对应的数据位宽按照固定的调整步长减少,得到调整位宽。固定的调整步长可以为N位,N为正整数。每次调整数据位宽可以减少N位。增加后的数据位宽=原数据位宽-N位。
可以将待量化数据对应的数据位宽按照可变的调整步长减少,得到调整位宽。例如,当量化误差与误差阈值之间的差值大于第一阈值时,可以按照调整步长M1调整数据位宽,当量化误差与误差阈值之间的差值小于第一阈值时,可以按照调整步长M2调整数据位宽,其中,第一阈值大于第二阈值,M1大于M2。可以根据需求确定各可变的调整步长。本公开对数据位宽的调整步长及调整步长是否可变不作限定。
可以将待量化数据按照调整位宽计算得到调整后的量化参数,利用调整后的量化参数对待量化数据进行重新量化后得到的量化数据,比利用调整前的量化参数量化得到的量化数据的量化精度更低。
在一种可能的实现方式中,该方法还可以包括:
根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
根据量化误差增加待量化数据对应的数据位宽时,调整一次位宽后得到调整位宽,根据调整位宽计算得到调整后的量化参数,根据调整后的量化参数量化待量化数据得到调整后的量化数据,再根据调整后的量化数据与待量化数据计算得到待量化数据调整后的量化误差,调整后的量化误差可能依然小于第二误差阈值,即根据调整一次的数据位宽可能不能满足调整目的。当调整后的量化误差依然小于第二误差阈值时,可以继续对调整后的数据位宽进行调整,即多次减少待量化数据对应的数据位宽,直至根据最终得到的调整位宽和待量化数据得到的调整后的量化误差大于第二误差阈值。
多次减少的调整步长可以是固定的调整步长,也可以是可变的调整步长。例如,最终的数据位宽=原数据位宽-B*N位,其中N为每次增加的固定的调整步长,B为数据位宽的增加次数。最终的数据位宽=原数据位宽-M1-M2-…-Mm,其中,M1、M2…Mm为每次减少的可变的调整步长。
在本实施例中,当量化误差小于第二误差阈值时,减少所述待量化数据对应的数据位宽,得到所述待量化数据对应的调整位宽。可以通过设置第二误差阈值和调整步长减少数据位宽,以使调整后的数据位宽能够满足量化的需求。当一次调整不能满足调整需求时,还可以对数据位宽进行多次调整。第二误差阈值和调整步长的设置,使得量化参数可以按照量化需求进行灵活的自适应调整,满足不同的量化需求,使得量化精度可调,在量化精度和神经网络的运行效率之间达到平衡。
在一种可能的实现方式中,所述方法还包括:
当所述量化误差大于第一误差阈值时,增加所述待量化数据对应的数据位宽,以及当所述量化误差小于第二误差阈值时,减少所述待量化数据对应的数据位宽,得到所述待量化数据对应的调整位宽。
也可以同时设置两个误差阈值,其中,第一误差阈值用于表示量化精度过低,可以增加数据位宽的位数,第二误差阈值用于表示量化精度过高,可以减少数据位宽的位数。第一误差阈值大于第二误差阈值,可以将待量化数据的量化误差同时与两个误差阈值进行比较,当量化误差大于第一误差阈值时,增加数据位宽的位数,当量化误差小于第二误差阈值时,减少数据位宽的位数。当量化误差位于第一误差阈值和第二误差阈值之间时,数据位宽可以保持不变。
在本实施例中,通过将量化误差与第一误差阈值和第二误差阈值同时进行比较,可以根据比较结果增加或减少数据位宽,可以利用第一误差阈值和第二误差阈值更加灵活的调整数据位宽。使得数据位宽的调整结果更加符合量化需求。
在一种可能的实现方式中,在所述神经网络运算的微调阶段和/或训练阶段,该方法还可以包括:
获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
在神经网络运算的微调阶段和/或训练阶段包括多次迭代。神经网络中的各待量化层,在进行一次正向运算和一次反向运算,并对待量化层的权值进行更新后,完成一次迭代。在多次迭代中,待量化层中的待量化数据和/或待量化数据对应的量化数据的数据变动幅度,可以用于衡量在不同迭代中的待量化数据和/或量化数据是否可采用相同的量化参数进行量化。若当前迭代以及历史迭代中待量化数据的数据变动幅度较小,例如小于设定的幅度变动阈值时,可以在数据变动幅度较小的多个迭代中采用相同的量化参数。
可以通过提取预存的量化参数的方式,确定与待量化数据对应的量化参数。在不同的迭代中对待量化数据进行量化时,需要在各迭代提取与待量化数据对应的量化参数。若多个迭代的待量化数据和/或待量化数据对应的量化数据的数据变动幅度较小,可将在数据变动幅度较小的多个迭代中采用的相同的量化参数进行暂存,各迭代在进行量化时可以利用暂存的量化参数进行量化运算,不用在每次迭代提取量化参数。
也可以根据待量化数据和数据位宽计算得到量化参数。在不同的迭代中对待量化数据进行量化时,需要在各迭代分别计算量化参数。若多个迭代的待量化数据和/或待量化数据对应的量化数据的数据变动幅度较小,可在数据变动幅度较小的多个迭代中采用的相同的量化参数,则各迭代均可以直接使用其中第一个迭代计算得到的量化参数,而不是每次迭代计算量化参数。
可以理解的是,当待量化数据为权值时,各迭代之间的权值在不断更新,若多个迭代的权值的数据变动幅度较小,或多个迭代的权值对应的量化数据的数据变动幅度较小,可以在多个迭代中利用相同的量化参数对权值进行量化。
可以根据待量化数据的数据变动幅度确定目标迭代间隔,目标迭代间隔包括至少一次迭代,可以在目标迭代间隔内的各迭代使用相同的量化参数,即在目标迭代间隔内的各迭代不再更新待量化数据的量化参数。神经网络根据目标迭代间隔更新待量化数据的量化参数,包括在目标迭代间隔内的迭代,不获取预设的量化参数或不计算量化参数,即在目标迭代间隔内的迭代不更新量化参数。而在目标迭代间隔外的迭代,再获取预设的量化参数或计算量化参数,即在目标迭代间隔外的迭代更新量化参数。
可以理解的是,多个迭代之间的待量化数据或待量化数据的量化数据的数据变动幅度越小,确定出的目标迭代间隔包括的迭代次数越多。可以根据计算得到的数据变动幅度,查找预设的数据变动幅度与迭代间隔的对应关系,确定与计算得到的数据变动幅度对应的目标迭代间隔。可以根据需求预设数据变动幅度与迭代间隔的对应关系。也可以根据计算得到的数据变动幅度,利用设定的计算方法计算得到目标迭代间隔。本公开不限定数据变动幅度的计算方式,以及目标迭代间隔的获取方式。
在本实施例中,在神经网络运算的微调阶段和/或训练阶段,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,根据所述待量化数据的数据变动幅度,确定待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。可以根据多个迭代中待量化数据或待量化数据对应的量化数据的数据变动幅度,确定目标迭代间隔。神经网络可以根据目标迭代间隔确定是否更新量化参数。由于目标迭代间隔所包括的多个迭代的数据变动幅度较小,目标迭代间隔内的迭代不更新量化参数也可以保证量化精度。而目标迭代间隔内的多个迭代不更新量化参数,可以减少量化参数的提取次数或计算次数,从而提高神经网络的运算效率。
在一种可能的实现方式中,该方法还包括:
根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
如本公开上述实施例所述,待量化数据的量化参数可以预设,也可以根据待量化数据对应的数据位宽计算得到。而不同待量化层中待量化数据对应的数据位宽,或相同待量化层中待量化数据在不同迭代中对应的数据位宽,可以根据本公开上述实施例中的方式进行自适应调整。
当待量化数据的数据位宽不可自适应调整,为预设的数据位宽时,可以根据待量化数据在当前迭代的预设的数据位宽,确定待量化数据在目标迭代间隔内的迭代对应的数据位宽。在目标迭代间隔内的各迭代可不使用自身的预设值。
当待量化数据的数据位宽可自适应调整时,可以根据待量化数据在当前迭代对应的数据位宽,确定待量化数据在目标迭代间隔内的迭代对应的数据位宽。在数据位宽可自适应调整时,数据位宽可进行一次调整或多次调整。可以将待量化数据在当前迭代进行自适应调整后的数据位宽,作为目标迭代间隔内的各迭代对应的数据位宽,在目标迭代间隔内的各迭代不再对数据位宽进行自适应调整(更新)。待量化数据在当前迭代可以使用自适应调整后的数据位宽,也可以使用自适应调整前的数据位宽,本公开对此不作限定。
在目标迭代间隔以外的其他迭代,由于待量化数据的数据变动幅度不满足设定条件,可以根据本公开上述的方法对数据位宽进行自适应调整,得到更加符合当前迭代的待量化数据的数据位宽,也可使用本公开中的目标迭代间隔的计算方法,计算得到新的目标迭代间隔并使用,从而在保证目标迭代间隔以外的迭代的量化精度的同时,提高神经网络的运行效率。
在目标迭代间隔内的各迭代的数据位宽相同,各迭代可以根据相同的数据位宽各自计算得到对应的量化参数。量化参数可以包括点位置、缩放系数和偏移量中的至少一种。可以在目标迭代间隔内的各迭代,根据相同的数据位宽分别计算得到量化参数。量化参数包括点位置(包括第一类点位置、第二类点位置)、缩放系数(包括第一类缩放系数和第二类缩放系数)和偏移量时,在目标迭代间隔内的各迭代,可利用相同的数据位宽,分别计算各自对应的点位置、缩放系数和偏移量。
在根据当前迭代的数据位宽,确定目标迭代间隔内各迭代的数据位宽的同时,可以根据当前迭代的量化参数,确定目标迭代间隔内各迭代的对应的量化参数。目标迭代间隔内各迭代的量化参数,也不再重新根据相同的数据位宽计算得到,可以进一步提高神经网络的运算效率。可以根据当前迭代的全部量化参数或部分量化参数,确定目标迭代间隔内各迭代的对应的量化参数。当根据当前迭代的部分量化参数,确定目标迭代间隔内各迭代的对应的量化参数时,剩余部分的量化参数,在目标迭代间隔内各迭代仍需计算。
例如,量化参数包括第二类点位置、第二类缩放系数和偏移量。可以根据当前迭代的数据位宽和第二类点位置,确定目标迭代间隔内各迭代的数据位宽和第二类点位置。则目标迭代间隔内各迭代的第二类缩放系数和偏移量需要根据相同的数据位宽计算得到。也可以根据当前迭代的数据位宽、第二类点位置、第二类缩放系数和偏移量,确定目标迭代间隔内各迭代的数据位宽、第二类点位置、第二类缩放系数和偏移量,则目标迭代间隔内各迭代的各量化参数均不需要计算得到。
在本实施例中,根据待量化数据在当前迭代对应的数据位宽,确定待量化数据在目标迭代间隔内的迭代对应的数据位宽,以使神经网络根据待量化数据在目标迭代间隔内的迭代对应的数据位宽,确定量化参数。在目标迭代间隔内的各迭代的数据位宽,根据当前迭代的数据位宽确定,由于目标迭代间隔内各迭代的待量化数据的数据变化幅度满足设定的条件,利用相同的数据位宽计算得到的量化参数,可以保证目标迭代间隔内的各迭代的量化精度。目标迭代间隔内各迭代使用相同的数据位宽,也可以提高神经网络的运算效率。在对神经网络进行量化后运算结果的准确率和神经网络的运算效率之间,达到平衡。
在一种可能的实现方式中,该方法还可以包括:根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置 和/或第二类点位置。
其中,根据所述待量化数据在所述当前迭代对应的第一类点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的第一类点位置。根据所述待量化数据在所述当前迭代对应的第二类点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的第二类点位置。
在量化参数中,相对于缩放系数和偏移量,不同的点位置对相同待量化数据的量化结果产生的影响较大。可以根据待量化数据在当前迭代对应的点位置,确定目标迭代间隔内的迭代对应的点位置。当数据位宽不可自适应调整时,可以将待量化数据在当前迭代预设的点位置,作为待量化数据在目标迭代间隔内各迭代对应的点位置,也可以将待量化数据在当前迭代根据预设的数据位宽计算得到的点位置,作为待量化数据在目标迭代间隔内各迭代对应的点位置。当数据位宽可自适应调整时,可以将待量化数据在当前迭代调整后的点位置,作为待量化数据在目标迭代间隔内各迭代对应的点位置。
根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置的同时,也可以根据待量化数据在当前迭代对应的缩放系数,确定所述待量化数据在所述目标迭代间隔内的迭代对应的缩放系数,和/或根据待量化数据在当前迭代对应的偏移量,确定所述待量化数据在所述目标迭代间隔内的迭代对应的偏移量。
根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置的同时,还可以根据待量化数据在当前迭代对应的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,其中,待量化数据在当前迭代对应的数据位宽,可以是当前迭代预设的数据位宽或自适应调整后的数据位宽。
在本实施例中,根据待量化数据在当前迭代对应的点位置,确定待量化数据在目标迭代间隔内的迭代对应的点位置。在目标迭代间隔内的各迭代的点位置,根据当前迭代的点位置确定,由于目标迭代间隔内各迭代的待量化数据的数据变化幅度满足设定的条件,利用相同的点位置,可以保证目标迭代间隔内的各迭代的量化精度。目标迭代间隔内各迭代使用相同的点位置,也可以提高神经网络的运算效率。在对神经网络进行量化后运算结果的准确率和神经网络的运算效率之间,达到平衡。
在一种可能的实现方式中,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,可以包括:
根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
其中,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数,可以包括:
根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
其中,根据待量化数据在当前迭代的第一类点位置和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的第一类点位置,计算待量化数据对应各迭代间隔的第一类点位置的滑动平均值;根据所述待量化数据在当前迭代的第一类点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的第一类点位置的第二滑动平均值,得到所述待量化数据变动幅度。或者,根据待量化数据在当前迭代的第二类点位置和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的第二类点位置,计算待量化数据对应各迭代间隔的第二类点位置的滑动平均值;根据所述待量化数据在当前迭代的第二类点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的第二类点位置的第二滑动平均值,得到所述待量化数据变动幅度。
在一种可能的实现方式中,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代,可以为计算目标迭代间隔的历史迭代。当前迭代与对应的目标迭代间隔之间的对应关系可以包括:
可以从当前迭代开始计数目标迭代间隔,并在当前迭代对应的目标迭代间隔结束后的下一个迭代开始重新计算目标迭代间隔。例如,当前迭代为第100代,目标迭代间隔为3,目标迭代间隔内的迭代包括:第100代、第101代和第102代,可以在第103代计算与第103代对应的目标迭代间隔,并以103代为新计算得到当目标迭代间隔内的第一个迭代。此时,当前迭代为103代时,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代为100代。
可以从当前迭代的下一个迭代开始计数目标迭代间隔,并在目标迭代间隔内的最后一个迭代开始重新计算目标迭代间隔。例如,当前迭代为第100代,目标迭代间隔为3,目标迭代间隔内的迭代包括:第101代、第102代和第103代,可以在第103代计算与第103代对应的目标迭代间隔,并以104代为新计算得到当目标迭代间隔内的第一个迭代。此时,当前迭代为103代时,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代为100代。
可以从当前迭代的下一个迭代开始计数目标迭代间隔,并在目标迭代间隔结束后的下一个迭代开始重新计算目标迭代间隔。例如,当前迭代为第100代,目标迭代间隔为3,目标迭代间隔内的迭代包括:第101代、第102代和第103代,可以在第104代计算与第104代对应的目标迭代间隔,并以105代为新计算得到当目标迭代间隔内的第一个迭代。此时,当前迭代为104代时,根据历史迭代间隔确定的与所述当前迭代对应的历史迭代为100代。
可以根据需求确定当前迭代以及目标迭代间隔之间的其他的对应关系,例如可以从当前迭代之后的第N个迭代开始计数目标迭代间隔,N大于1,本公开对此不作限定。
可以理解的是,计算得到的待量化数据对应各迭代间隔的点位置的滑动平均值,包括待量化数据在当前迭代的点位置的第一滑动平均值,和待量化数据在上一迭代间隔对应迭代的点位置的第二滑动平均值。可以利用公式(6-27)计算当前迭代对应点位置的第一滑动平均值m (t)
m (t)←α×s (t)+(1-α)×m (t-1)     公式(6-27)
其中,t为当前迭代,t-1为根据上一迭代间隔确定的历史迭代,m (t-1)为根据上一迭代间隔确定的历史迭代的第二滑动平均值。s (t)为当前迭代的点位置,可以为第一类点位置或第二类点位置。α为第一参数。第一参数可以为超参数。
在本实施例中,根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值;根据待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度。根据第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。由于第一数据变动幅度可以用于衡量点位置的变化趋势,使得目标迭代间隔可以跟随待量化数据点位置的变化趋势而变化,也使得计算得到的各目标迭代间隔的大小可以根据待量化数据点位置的变化趋势而变化。由于量化参数根据目标迭代间隔确定,也就使得根据量化参数进行量化得到的量化数据,能够更加符合待量化数据的点位置的变动趋势,在保证量化精度的同时,提高神经网络的运行效率。
在一种可能的实现方式中,所述根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度,可以包括:
计算所述第一滑动平均值和所述第二滑动平均值的差值;
将所述差值的绝对值确定为第一数据变动幅度。
可以利用公式(6-28)计算第一数据变动幅度diff update1
diff update1=|m (t)-m (t-1)|=α|s (t)-m (t-1)|      公式(6-28)
可以根据第一数据变动幅度,确定待量化数据对应的目标迭代间隔,以使神经网络根据目标迭代间隔更新所述待量化数据的量化参数。可以根据公式(6-29)计算得到目标迭代间隔I:
Figure PCTCN2020095673-appb-000128
其中,β为第二参数,γ为第三参数。第二参数和第三参数可以为超参数。
可以理解的是,第一数据变动幅度可以用于衡量点位置的变化趋势,第一数据变动幅度越大,说明量化数据的数值范围变化剧烈,在更新量化参数时需要间隔更短的目标迭代间隔I。
在本实施例中,计算所述第一滑动平均值和所述第二滑动平均值的差值;将差值的绝对值确定为第一数据变动幅度。根据滑动平均值之间的差值可以得到精确的第一数据变动幅度。
在一种可能的实现方式中,该方法还可以包括:根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
其中,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数,可以包括:
根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
可以根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度。也可以根据在当前迭代所述待量化数据和所述待量化数据对应的反量化数据,得到第二数据变动幅度。
同样的,可以根据公式(6-30)计算得到在当前迭代中,所述待量化数据和所述待量化数据对应的反量化数据之间的第二数据变动幅度diff bit。也可以利用其它误差的计算方法,计算待量化数据和反量化数据之间的第二数据变动幅度diff bit。本公开对此不作限定。
Figure PCTCN2020095673-appb-000129
其中,z i为待量化数据,z i (n)为待量化数据对应的反量化数据。可以理解的是,第二数据变动幅度可以用于衡量待量化数据对应的数据位宽的变化趋势,第二数据变动幅度越大,待量化数据越有可能需要更新对应的数据位宽,需要间隔更短的迭代进行更新,则第二数据变动幅度越大,需要目标迭代间隔更小。
在本实施例中,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度。根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。第二数据变动幅度可以用于衡量数据位宽的变动需求,则根据第一数据变动幅度和第二数据变动幅度计算得到的目标迭代间隔,可以同时跟踪点位置和数据位宽的变动,目标迭代间隔也可以更加符合待量化数据自身的数据量化需求。
在一种可能的实现方式中,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度,可以包括:
计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;
将所述误差的平方确定为所述第二数据变动幅度。
可以利用公式(6-31)计算得到第二数据变动幅度diff update2
diff update2=δ*diff bit 2     公式(6-31)
其中,δ为第四参数,第四参数可以为超参数。
可以理解的是,利用不同的数据位宽可以得到不同的量化参数,进而得到不同的量化数据,产生不同的第二数据变动幅度。第二数据变动幅度可以用于衡量数据位宽的变化趋势,第二数据变动幅度 越大,说明需要更短的目标迭代间隔来更加频繁的更新数据位宽,即目标迭代间隔需要更小。
在一种可能的实现方式中,根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,可以包括:
根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
可以根据公式(6-32)计算得到目标迭代间隔:
Figure PCTCN2020095673-appb-000130
其中,β为第二参数,γ为第三参数。第二参数和第三参数可以为超参数。
可以理解的是,利用第一数据变动幅度和第二数据变动幅度得到的目标迭代间隔,可以同时衡量数据位宽和点位置的变化趋势,当两者中的其一的变化趋势较大时,便可以使得目标迭代间隔产生相应的变化。目标迭代间隔可以同时追踪数据位宽和点位置的变化并做出相应的调整。使得根据目标迭代间隔更新的量化参数能够更加符合目标数据的变动趋势,最终使得根据量化参数得到的量化数据能够更加符合量化需求。
在一种可能的实现方式中,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,可以包括:
在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
在神经网络运算的训练过程和/或微调过程中,在训练开始或微调开始的多个迭代中,待量化数据的变化幅度较大。若在训练开始或微调开始的多个迭代中计算目标迭代间隔,则计算得到的目标迭代间隔可能会失去其使用的意义。可以根据预设更新周期,在更新周期以内的各迭代,不计算目标迭代间隔,也不适用目标迭代间隔使得多个迭代使用相同的数据位宽或点位置。
当迭代进行至更新周期以外时,即当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,并根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。例如,预设更新周期为100代,则自第1代开始至第100代的迭代中,不计算目标迭代间隔。当迭代进行至101代,即当前迭代为101代时,当前迭代位于更新周期以外,此时,可以根据第101代以及第1代至第100代的迭代中待量化数据的数据变动幅度,确定第101代的待量化数据对应的目标迭代间隔,并在第101代或与第101间隔预设代数的迭代中,使用计算得到的目标迭代间隔。
可以自预设的代数开始计数更新周期,例如可以自第一代开始计数更新周期中多个迭代,也可以自第N代开始计数更新周期中的多个迭代,本公开对此不作限定。
在本实施例中,当迭代进行至更新周期以外时计算并使用目标迭代间隔。可以避免在神经网络运算的训练过程或微调过程的初期,由于待量化数据的变动幅度较大引起的目标迭代间隔使用意义不大的问题,可以在使用目标迭代间隔的情况下,进一步提高神经网络的运行效率。
在一种可能的实现方式中,该方法还可以包括:
在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
在神经网络运算的训练过程或微调过程中,可以包括多个周期。各周期可以包括多个迭代。用于 神经网络运算的数据被完整的运算一遍为一个周期。在训练过程中,随着迭代的进行,神经网络的权值变化趋于稳定,当训练稳定后,神经元、权值、偏置和梯度等待量化数据均趋于稳定。待量化数据趋于稳定后,待量化数据的数据位宽和量化参数也趋于稳定。同理,在微调过程中,微调稳定后,待量化数据的数据位宽和量化参数也趋于稳定。
因此,可以根据训练稳定或微调稳定的周期确定预设周期。可以将训练稳定或微调稳定所在周期以后的周期,确定为预设周期。例如训练稳定的周期为第M个周期,则可以将第M个周期以后的周期作为预设周期。在预设周期内,可以每间隔一个周期计算一个目标迭代间隔,并根据计算得到的目标迭代间隔调整一次数据位宽或量化参数,以减少数据位宽或量化参数的更新次数,提高神经网络的运行效率。
例如,预设周期为第M个周期以后的周期。在第M+1个周期中,根据第M个周期中的第P个迭代计算得到的目标迭代间隔,截止至第M+1个周期中的第Q个迭代。根据第M+1个周期中的第Q m+1个迭代计算得到与之对应的目标迭代间隔I m+1。在第M+2个周期中,与第M+1个周期中的第Q m+1个迭代对应的迭代为第Q m+2个迭代。在自第M+1个周期中的第Q m+1个迭代开始,直至第M+2个周期中第Q m+2+I m+1个迭代为止,为周期间隔。在周期间隔内的各迭代,均采用第M+1个周期中的第Q m+1个迭代确定的数据位宽或点位置等量化参数。
在本实施例中,可以设置周期间隔,在神经网络运算的训练或微调达到稳定后,根据周期间隔,每周期更新一次数据位宽或点位置等量化参数。周期间隔可以在训练稳定或微调稳定后,减少数据位宽或点位置的更新次数,在保证量化精度的同时,提高神经网络的运行效率。
需要说明的是,对于前述的各方法实施例,为了简单描述,故将其都表述为一系列的动作组合,但是本领域技术人员应该知悉,本公开并不受所描述的动作顺序的限制,因为依据本公开,某些步骤可以采用其他顺序或者同时进行。其次,本领域技术人员也应该知悉,说明书中所描述的实施例均属于可选实施例,所涉及的动作和模块并不一定是本公开所必须的。
进一步需要说明的是,虽然图4-1、图4-4的流程图中的各个步骤按照箭头的指示依次显示,但是这些步骤并不是必然按照箭头指示的顺序依次执行。除非本文中有明确的说明,这些步骤的执行并没有严格的顺序限制,这些步骤可以以其它的顺序执行。而且,图4-1、图4-4中的至少一部分步骤可以包括多个子步骤或者多个阶段,这些子步骤或者阶段并不必然是在同一时刻执行完成,而是可以在不同的时刻执行,这些子步骤或者阶段的执行顺序也不必然是依次进行,而是可以与其它步骤或者其它步骤的子步骤或者阶段的至少一部分轮流或者交替地执行。
本公开实施例还提供了一种非易失性计算机可读存储介质,其上存储有计算机程序指令,计算机程序指令被处理器执行时实现上述神经网络的数据量化处理方法。
图4-5示出根据本公开一实施例的神经网络量化装置的框图。如图4-5所示,该装置应用于如图1所示的处理器100,该装置包括数据确定模块6-61、数据量化模块6-62和结果确定模块6-63。其中,某一个处理单元101中设置有数据确定模块6-61、数据量化模块6-62和结果确定模块6-63。或者,数据确定模块6-61、数据量化模块6-62和结果确定模块6-63分别设置在不同的处理单元101中。存储单元102用于存储待量化数据、量化参数、数据位宽等与数据确定模块6-61、数据量化模块6-62和结果确定模块6-63的运行相关的数据。
数据确定模块6-61,根据运行所述神经网络的终端的存储能力,从所述待量化层的目标数据中确定多个待量化数据,所述目标数据为所述待量化层中的任意一批待运算数据,所述待运算数据包括输入神经元和/或梯度中的至少一种。
数据量化模块6-62,采用循环处理的方式,依次根据每个待量化数据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据。
结果确定模块6-63,根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
在一种可能的实现方式中,所述数据量化模块,包括:
待量化数据确定子模块,在上一次循环处理过程结束之后开始当前循环处理过程,将上一次循环处理过程量化的待量化数据之后的下一待量化数据,确定为当前循环处理过程所需量化的当前待量化数据;
比较子模块,对当前待量化数据的统计结果和存储的参考统计结果进行比较,得到比较结果;
统计结果确定子模块,在所述比较结果满足量化参数计算条件时,根据当前待量化数据的统计结果和所述参考统计结果,确定更新统计结果;
第一量化参数确定子模块,根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数;
量化处理子模块,根据对应的量化参数对当前待量化数据进行量化,得到与当前待量化数据对应的量化数据,结束当前循环处理过程。
在一种可能的实现方式中,所述数据量化模块,还包括:
参考数据更新模块,将所述更新统计结果以及对应所述更新统计结果的量化参数,分别作为新的参考统计结果和新的参考量化参数进行存储。
在一种可能的实现方式中,所述数据量化模块,还包括:
第二量化参数确定子模块,在所述比较结果不满足所述量化参数计算条件时,将所述参考统计结果所对应的参考量化参数确定为当前待量化数据所对应的量化参数。
在一种可能的实现方式中,当前待量化数据的统计结果包括所述当前待量化数据中的最大值和最小值,所述参考统计结果包括参考最大值和参考最小值,
其中,在所述比较结果满足量化参数计算条件时,根据当前待量化数据的统计结果和所述参考统计结果,确定更新统计结果,包括以下任一种方式:
在所述当前待量化数据中的最大值大于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述当前待量化数据的统计结果确定为更新统计结果;
在所述当前待量化数据中的最大值小于或等于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述参考最大值和所述当前待量化数据中的最小值确定为更新统计结果;
在所述当前待量化数据中的最大值大于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述当前待量化数据中的最大值和所述参考最小值确定为更新统计结果。
在一种可能的实现方式中,根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数,包括:
当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
在一种可能的实现方式中,所述装置还包括:
参数确定子模块,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
在一种可能的实现方式中,所述参数确定子模块,包括:
第一点位置确定子模块,当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值和对应的数据位宽,得到各所述待量化数据的第一类点位置。
在一种可能的实现方式中,所述参数确定子模块,包括:
第一最大值确定子模块,当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
第一缩放系数确定子模块,根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数。
在一种可能的实现方式中,所述参数确定子模块,包括:
第二点位置确定子模块,当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。或者,当所述量化参数包括偏移量时,根据各所述更新统计结果中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
在一种可能的实现方式中,所述参数确定子模块,包括:
第二最大值确定子模块,当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
第一缩放系数确定子模块,根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。或者,根据各所述更新统计结果中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。
在一种可能的实现方式中,所述参数确定子模块,包括:
偏移量确定子模块,根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。或者,根据各所述更新统计结果中的最大值和最小值,得到各所述待量化数据的偏移量。
在一种可能的实现方式中,所述装置还包括:
第一量化误差确定模块,根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
调整位宽确定模块,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
调整量化参数确定模块,将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
在一种可能的实现方式中,所述调整位宽确定模块,包括:
第一调整位宽确定子模块,当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
在一种可能的实现方式中,所述装置还包括:
第一调整后量化误差模块,根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
第一调整位宽循环确定模块,根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
在一种可能的实现方式中,所述调整位宽确定模块,包括:
第二调整位宽确定子模块,当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值。
在一种可能的实现方式中,所述装置还包括:
第二调整后量化误差模块,根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
第二调整位宽循环确定模块,根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
在一种可能的实现方式中,在所述神经网络运算的微调阶段和/或训练阶段,所述装置还包括:
第一数据变动幅度确定模块,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
目标迭代间隔确定模块,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
在一种可能的实现方式中,所述装置还包括:
第一目标迭代间隔应用模块,根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
在一种可能的实现方式中,所述装置还包括:
第二目标迭代间隔应用模块,根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位置。
在一种可能的实现方式中,所述第一数据变动幅度确定模块,包括:
滑动平均值计算子模块,根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
第一数据变动幅度确定子模块,根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
其中,所述目标迭代间隔确定模块,包括:
第一目标迭代间隔确定子模块,根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
在一种可能的实现方式中,所述第一数据变动幅度确定子模块,包括:
第一幅度确定子模块,计算所述第一滑动平均值和所述第二滑动平均值的差值;将所述差值的绝对值确定为第一数据变动幅度。
在一种可能的实现方式中,所述装置还包括:
第二数据变动幅度确定模块,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
其中,目标迭代间隔确定模块,包括:
第二目标迭代间隔确定子模块,根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
在一种可能的实现方式中,所述第二数据变动幅度确定模块,包括:
第二幅度确定子模块,计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;将所述误差的平方确定为所述第二数据变动幅度。
在一种可能的实现方式中,所述第二目标迭代间隔确定子模块,包括:
间隔确定子模块,根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
在一种可能的实现方式中,所述第一数据变动幅度确定模块,包括:
第二数据变动幅度确定子模块,在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中 待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
在一种可能的实现方式中,所述装置还包括:
周期间隔确定模块,在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
第一周期间隔应用模块,根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
第二周期间隔应用模块,根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
本公开实施例所提供的神经网络量化装置,利用对应的量化参数对目标数据中的多个待量化数据分别进行量化,在保证精度的同时,减小了存储数据所占用的存储空间,保证了运算结果的准确性和可靠性,且能够提高运算的效率,且量化同样缩减了神经网络模型的大小,降低了对运行该神经网络模型的终端的性能要求,使神经网络模型可以应用于算力、体积、功耗相对受限的手机等终端。
依据以下条款可更好地理解前述内容:
条款F1.一种神经网络量化方法,对于所述神经网络中的任意待量化层,所述方法包括:
根据运行所述神经网络的终端的存储能力,从所述待量化层的目标数据中确定多个待量化数据,所述目标数据为所述待量化层中的任意一批待运算数据,所述待运算数据包括输入神经元和/或梯度中的至少一种;
采用循环处理的方式,依次根据每个待量化数据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据;
根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
条款F2.根据条款F1所述的方法,采用循环处理的方式,依次根据每个待量化数据根据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据,包括:
在上一次循环处理过程结束之后开始当前循环处理过程,将上一次循环处理过程量化的待量化数据之后的下一待量化数据,确定为当前循环处理过程所需量化的当前待量化数据;
对当前待量化数据的统计结果和存储的参考统计结果进行比较,得到比较结果;
在所述比较结果满足量化参数计算条件时,根据当前待量化数据的统计结果和所述参考统计结果,确定更新统计结果;
根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数;
根据对应的量化参数对当前待量化数据进行量化,得到与当前待量化数据对应的量化数据,结束当前循环处理过程。
条款F3.根据条款F2所述的方法,采用循环处理的方式,依次根据每个待量化数据根据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据,还包括:
将所述更新统计结果以及对应所述更新统计结果的量化参数,分别作为新的参考统计结果和新的参考量化参数进行存储。
条款F4.根据条款F2所述的方法,采用循环处理的方式,依次根据每个待量化数据根据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据,还包括:
在所述比较结果不满足所述量化参数计算条件时,将所述参考统计结果所对应的参考量化参数确定为当前待量化数据所对应的量化参数。
条款F5.根据条款F2至条款F4任一项所述的方法,当前待量化数据的统计结果包括所述当前待量化数据中的最大值和最小值,所述参考统计结果包括参考最大值和参考最小值,
其中,在所述比较结果满足量化参数计算条件时,根据当前待量化数据的统计结果和所述参考统计结果,确定更新统计结果,包括以下任一种方式:
在所述当前待量化数据中的最大值大于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述当前待量化数据的统计结果确定为更新统计结果;
在所述当前待量化数据中的最大值小于或等于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述参考最大值和所述当前待量化数据中的最小值确定为更新统计结果;
在所述当前待量化数据中的最大值大于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述当前待量化数据中的最大值和所述参考最小值确定为更新统计结果。
条款F6.根据条款F2所述的方法,根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数,包括:
当所述量化参数包括偏移量时,根据所述更新统计结果中的最大值、最小值和对应的数据位宽,得到所述当前待量化数据的第二类点位置。
条款F7.根据条款F2所述的方法,根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数,包括:
当所述量化参数包括偏移量时,根据当前待量化数据和对应的数据位宽得到量化后数据的最大值;
根据所述更新统计结果中的最大值、最小值和量化后数据的最大值,得到当前待量化数据的第二类缩放系数。
条款F8.根据条款F2所述的方法,根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数,包括:
根据所述更新统计结果中的最大值和最小值,得到当前待量化数据的偏移量。
条款F9.根据条款F1至条款F8中任一项所述的方法,所述方法还包括:
根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
条款F10.根据条款F9所述的方法,所述根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽,包括:
当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
条款F11.根据条款F9或条款F10所述的方法,所述方法还包括:
根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
条款F12.根据条款F9或条款F10所述的方法,所述根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽,包括:
当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值。
条款F13.根据条款F12所述的方法,所述方法还包括:
根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所 述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
条款F14.根据条款F1至条款F13中任一项所述的方法,在所述神经网络运算的微调阶段和/或训练阶段,所述方法还包括:
获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
条款F15.根据条款F14所述的方法,所述方法还包括:
根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
条款F16.根据条款F15所述的方法,所述方法还包括:
根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第二类点位置。
条款F17.根据条款F14所述的方法,所述获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第二类点位置;
根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
所述根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数,包括:
根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款F18.根据条款F17所述的方法,所述根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度,包括:
计算所述第一滑动平均值和所述第二滑动平均值的差值;
将所述差值的绝对值确定为第一数据变动幅度。
条款F19.根据条款F18所述的方法,所述方法还包括:
根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
所述根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数,包括:
根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款F20.根据条款F19所述的方法,所述根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度,包括:
计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;
将所述误差的平方确定为所述第二数据变动幅度。
条款F21.根据条款F19所述的方法,所述根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,包括:
根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目 标迭代间隔。
条款F22.根据条款F14至条款F21中任一项所述的方法,所述获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
条款F23.根据条款F14至条款F22中任一项所述的方法,所述方法还包括:
在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
条款F24.一种神经网络量化装置,对于所述神经网络中的任意待量化层,所述装置包括:
数据确定模块,根据运行所述神经网络的终端的存储能力,从所述待量化层的目标数据中确定多个待量化数据,所述目标数据为所述待量化层中的任意一批待运算数据,所述待运算数据包括输入神经元和/或梯度中的至少一种;
数据量化模块,采用循环处理的方式,依次根据每个待量化数据对应的量化参数进行量化,得到与每个待量化数据对应的量化数据;
结果确定模块,根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
条款F25.根据条款F24所述的装置,所述数据量化模块,包括:
待量化数据确定子模块,在上一次循环处理过程结束之后开始当前循环处理过程,将上一次循环处理过程量化的待量化数据之后的下一待量化数据,确定为当前循环处理过程所需量化的当前待量化数据;
比较子模块,对当前待量化数据的统计结果和存储的参考统计结果进行比较,得到比较结果;
统计结果确定子模块,在所述比较结果满足量化参数计算条件时,根据当前待量化数据的统计结果和所述参考统计结果,确定更新统计结果;
第一量化参数确定子模块,根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数;
量化处理子模块,根据对应的量化参数对当前待量化数据进行量化,得到与当前待量化数据对应的量化数据,结束当前循环处理过程。
条款F26.根据条款F25所述的装置,所述数据量化模块,还包括:
参考数据更新模块,将所述更新统计结果以及对应所述更新统计结果的量化参数,分别作为新的参考统计结果和新的参考量化参数进行存储。
条款F27.根据条款F25所述的装置,所述数据量化模块,还包括:
第二量化参数确定子模块,在所述比较结果不满足所述量化参数计算条件时,将所述参考统计结果所对应的参考量化参数确定为当前待量化数据所对应的量化参数。
条款F28.根据条款F25至条款F27任一项所述的装置,当前待量化数据的统计结果包括所述当前待量化数据中的最大值和最小值,所述参考统计结果包括参考最大值和参考最小值,
其中,在所述比较结果满足量化参数计算条件时,根据当前待量化数据的统计结果和所述参考统计结果,确定更新统计结果,包括以下任一种方式:
在所述当前待量化数据中的最大值大于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述当前待量化数据的统计结果确定为更新统计结果;
在所述当前待量化数据中的最大值小于或等于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述参考最大值和所述当前待量化数据中的最小值确定为更新统计结果;
在所述当前待量化数据中的最大值大于所述参考最大值、且所述当前待量化数据中的最小值小于所述参考最小值时,将所述当前待量化数据中的最大值和所述参考最小值确定为更新统计结果。
条款F29.根据条款F25所述的装置,根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数,包括:
当所述量化参数包括偏移量时,根据各所述更新统计结果中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
条款F30.根据条款F25所述的装置,根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数,包括:
当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
根据各所述更新统计结果中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。
条款F31.根据条款F25所述的装置,根据所述更新统计结果和对应的数据位宽,确定当前待量化数据所对应的量化参数,包括:
根据各所述更新统计结果中的最大值和最小值,得到各所述待量化数据的偏移量。
条款F32.根据条款F24至条款F31任一项所述的装置,所述装置还包括:
第一量化误差确定模块,根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
调整位宽确定模块,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
调整量化参数确定模块,将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
条款F33.根据条款F32所述的装置,所述调整位宽确定模块,包括:
第一调整位宽确定子模块,当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
条款F34.根据条款F32或条款F33所述的装置,所述装置还包括:
第一调整后量化误差模块,根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
第一调整位宽循环确定模块,根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
条款F35.根据条款F32或条款F33所述的装置,所述调整位宽确定模块,包括:
第二调整位宽确定子模块,当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值。
条款F36.根据条款F35所述的装置,所述装置还包括:
第二调整后量化误差模块,根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
第二调整位宽循环确定模块,根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
条款F37.根据条款F24至条款F36任一项所述的装置,在所述神经网络运算的微调阶段和/或训练阶段,所述装置还包括:
第一数据变动幅度确定模块,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
目标迭代间隔确定模块,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
条款F38.根据条款F37所述的装置,所述装置还包括:
第一目标迭代间隔应用模块,根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
条款F39.根据条款F38所述的装置,所述装置还包括:
第二目标迭代间隔应用模块,根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第二类点位置。
条款F40.根据条款F37所述的装置,所述第一数据变动幅度确定模块,包括:
滑动平均值计算子模块,根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第二类点位置;
第一数据变动幅度确定子模块,根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
其中,所述目标迭代间隔确定模块,包括:
第一目标迭代间隔确定子模块,根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款F41.根据条款F40所述的装置,所述第一数据变动幅度确定子模块,包括:
第一幅度确定子模块,计算所述第一滑动平均值和所述第二滑动平均值的差值;将所述差值的绝对值确定为第一数据变动幅度。
条款F42.根据条款F41所述的装置,所述装置还包括:
第二数据变动幅度确定模块,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
其中,目标迭代间隔确定模块,包括:
第二目标迭代间隔确定子模块,根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
条款F43.根据条款F42所述的装置,所述第二数据变动幅度确定模块,包括:
第二幅度确定子模块,计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;将所述误差的平方确定为所述第二数据变动幅度。
条款F44.根据条款F42所述的装置,所述第二目标迭代间隔确定子模块,包括:
间隔确定子模块,根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待 量化数据对应的目标迭代间隔。
条款F45.根据条款F37至条款F44任一项所述的装置,所述第一数据变动幅度确定模块,包括:
第二数据变动幅度确定子模块,在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
条款F46.根据条款F37至条款F45任一项所述的装置,所述装置还包括:
周期间隔确定模块,在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
第一周期间隔应用模块,根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
第二周期间隔应用模块,根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
条款F47.一种人工智能芯片,所述芯片包括如条款F24至条款F46中任意一项所述的神经网络量化装置。
条款F48.一种电子设备,所述电子设备包括如条款F47所述的人工智能芯片。
条款F49.一种板卡,所述板卡包括:存储器件、接口装置和控制器件以及如条款F47所述的人工智能芯片;
其中,所述人工智能芯片与所述存储器件、所述控制器件以及所述接口装置分别连接;
所述存储器件,用于存储数据;
所述接口装置,用于实现所述人工智能芯片与外部设备之间的数据传输;
所述控制器件,用于对所述人工智能芯片的状态进行监控。
条款F50.根据条款F49所述的板卡,
所述存储器件包括:多组存储单元,每一组所述存储单元与所述人工智能芯片通过总线连接,所述存储单元为:DDR SDRAM;
所述芯片包括:DDR控制器,用于对每个所述存储单元的数据传输与数据存储的控制;
所述接口装置为:标准PCIE接口。
条款F51.一种非易失性计算机可读存储介质,其上存储有计算机程序指令,所述计算机程序指令被处理器执行时实现条款F1至条款F23中任意一项所述的神经网络量化方法。
应该理解,上述的装置实施例仅是示意性的,本公开的装置还可通过其它的方式实现。例如,上述实施例中所述单元/模块的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式。例如,多个单元、模块或组件可以结合,或者可以集成到另一个系统,或一些特征可以忽略或不执行。
另外,若无特别说明,在本公开各个实施例中的各功能单元/模块可以集成在一个单元/模块中,也可以是各个单元/模块单独物理存在,也可以两个或两个以上单元/模块集成在一起。上述集成的单元/模块既可以采用硬件的形式实现,也可以采用软件程序模块的形式实现。
所述集成的单元/模块如果以硬件的形式实现时,该硬件可以是数字电路,模拟电路等等。硬件结构的物理实现包括但不局限于晶体管,忆阻器等等。若无特别说明,所述人工智能处理器可以是任何适当的硬件处理器,比如CPU、GPU、FPGA、DSP和ASIC等等。若无特别说明,所述存储单元可以是任何适当的磁存储介质或者磁光存储介质,比如,阻变式存储器RRAM(Resistive Random Access Memory)、动态随机存取存储器DRAM(Dynamic Random Access Memory)、静态随机存取存储器SRAM(Static Random-Access Memory)、增强动态随机存取存储器EDRAM(Enhanced Dynamic Random Access Memory)、高带宽内存HBM(High-Bandwidth Memory)、混合存储立方HMC(Hybrid Memory Cube)等等。
所述集成的单元/模块如果以软件程序模块的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储器中。基于这样的理解,本公开的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的全部或部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储器中,包括若干指令用以使得一台计算机设备(可为个人计算机、服务器或者网络设备等)执行本公开各个实施例所述方法的全部或部分步骤。而前述的存储器包括:U盘、只读存储器(ROM,Read-Only Memory)、随机存取存储器(RAM,Random Access Memory)、移动硬盘、磁碟或者光盘等各种可以存储程序代码的介质。
在一种可能的实现方式中,还公开了一种人工智能芯片,其包括了上述数据处理装置。
在一种可能的实现方式中,还公开了一种板卡,其包括存储器件、接口装置和控制器件以及上述人工智能芯片;其中,所述人工智能芯片与所述存储器件、所述控制器件以及所述接口装置分别连接;所述存储器件,用于存储数据;所述接口装置,用于实现所述人工智能芯片与外部设备之间的数据传输;所述控制器件,用于对所述人工智能芯片的状态进行监控。
图5示出根据本公开实施例的板卡的结构框图。参阅图5,上述板卡除了包括上述芯片389以外,还可以包括其他的配套部件,该配套部件包括但不限于:存储器件390、接口装置391和控制器件392;
所述存储器件390与所述人工智能芯片通过总线连接,用于存储数据。所述存储器件可以包括多组存储单元393。每一组所述存储单元与所述人工智能芯片通过总线连接。可以理解,每一组所述存储单元可以是DDR SDRAM(英文:Double Data Rate SDRAM,双倍速率同步动态随机存储器)。
上述处理器100中的存储单元102可以包括一组或多组存储单元393。在存储单元102包括一组存储单元393时,多个处理单元101公用存储单元393进行数据存储。在存储单元102包括多组存储单元393时,可以为每一个处理单元101设置其专用的一组存储单元393,并为多个处理单元101中的部分或全部设置其公用的一组存储单元393。
DDR不需要提高时钟频率就能加倍提高SDRAM的速度。DDR允许在时钟脉冲的上升沿和下降沿读出数据。DDR的速度是标准SDRAM的两倍。在一个实施例中,所述存储装置可以包括4组所述存储单元。每一组所述存储单元可以包括多个DDR4颗粒(芯片)。在一个实施例中,所述人工智能芯片内部可以包括4个72位DDR4控制器,上述72位DDR4控制器中64bit用于传输数据,8bit用于ECC校验。可以理解,当每一组所述存储单元中采用DDR4-3200颗粒时,数据传输的理论带宽可达到25600MB/s。
在一个实施例中,每一组所述存储单元包括多个并联设置的双倍速率同步动态随机存储器。DDR在一个时钟周期内可以传输两次数据。在所述芯片中设置控制DDR的控制器,用于对每个所述存储单元的数据传输与数据存储的控制。
所述接口装置与所述人工智能芯片电连接。所述接口装置用于实现所述人工智能芯片与外部设备(例如服务器或计算机)之间的数据传输。例如在一个实施例中,所述接口装置可以为标准PCIE接口。比如,待处理的数据由服务器通过标准PCIE接口传递至所述芯片,实现数据转移。优选的,当采用PCIE3.0X 16接口传输时,理论带宽可达到16000MB/s。在另一个实施例中,所述接口装置还可以是其他的接口,本公开并不限制上述其他的接口的具体表现形式,所述接口单元能够实现转接功能即可。另外,所述人工智能芯片的计算结果仍由所述接口装置传送回外部设备(例如服务器)。
所述控制器件与所述人工智能芯片电连接。所述控制器件用于对所述人工智能芯片的状态进行监控。具体的,所述人工智能芯片与所述控制器件可以通过SPI接口电连接。所述控制器件可以包括单片机(Micro Controller Unit,MCU)。如所述人工智能芯片可以包括多个处理芯片、多个处理核或多个处理电路,可以带动多个负载。因此,所述人工智能芯片可以处于多负载和轻负载等不同的工作状态。通过所述控制装置可以实现对所述人工智能芯片中多个处理芯片、多个处理和或多个处理电路的工作状态的调控。
在一种可能的实现方式中,公开了一种电子设备,其包括了上述人工智能芯片。电子设备包括数据处理装置、机器人、电脑、打印机、扫描仪、平板电脑、智能终端、手机、行车记录仪、导航仪、传感器、摄像头、服务器、云端服务器、相机、摄像机、投影仪、手表、耳机、移动存储、可穿戴设备、交通工具、家用电器、和/或医疗设备。所述交通工具包括飞机、轮船和/或车辆;所述家用电器包括电视、空调、微波炉、冰箱、电饭煲、加湿器、洗衣机、电灯、燃气灶、油烟机;所述医疗设备包括核磁共振仪、B超仪和/或心电图仪。
在上述实施例中,对各个实施例的描述都各有侧重,某个实施例中没有详述的部分,可以参见其他实施例的相关描述。上述实施例的各技术特征可以进行任意的组合,为使描述简洁,未对上述实施例中的各个技术特征所有可能的组合都进行描述,然而,只要这些技术特征的组合不存在矛盾,都应当认为是本说明书记载的范围。
以上对本公开实施例进行了详细介绍,本文中应用了具体个例对本公开的原理及实施方式进行了阐述,以上实施例的说明仅用于帮助理解本公开的方法及其核心思想。同时,本领域技术人员依据本公开的思想,基于本公开的具体实施方式及应用范围上做出的改变或变形之处,都属于本公开保护的范围。综上所述,本说明书内容不应理解为对本公开的限制。

Claims (51)

  1. 一种神经网络量化方法,其特征在于,应用于终端,所述终端用于运行神经网络以执行目标任务,所述方法包括:
    根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种;
    将各所述待量化数据分别根据对应的量化参数进行量化,得到与各所述待量化数据对应的量化数据;
    根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
  2. 根据权利要求1所述的方法,其特征在于,根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,包括以下至少一项:
    根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的目标数据确定为一个待量化数据;
    根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的同一种待运算数据确定为一个待量化数据;
    根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个通道中的数据确定为一个待量化数据;
    根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个批数的数据确定为一个待量化数据;
    根据所述目标任务和/或所述终端的精度需求确定划分数据的划分尺寸,将对应待量化层中的目标数据按照所述划分尺寸划分为一个或多个待量化数据。
  3. 根据权利要求1或2所述的方法,其特征在于,所述方法还包括:
    根据所述目标任务和/或所述终端的精度需求,确定待量化数据对应的数据位宽;
    根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
  4. 根据权利要求3所述的方法,其特征在于,所述根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
    当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值和对应的数据位宽,得到各所述待量化数据的第一类点位置。
  5. 根据权利要求3所述的方法,其特征在于,所述根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
    当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
    根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数。
  6. 根据权利要求3所述的方法,其特征在于,所述根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
    当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
  7. 根据权利要求3所述的方法,其特征在于,所述根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
    当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大 值;
    根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。
  8. 根据权利要求3所述的方法,其特征在于,所述根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数,包括:
    根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。
  9. 根据权利要求1至8中任一项所述的方法,其特征在于,所述方法还包括:
    根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
    根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
    将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
  10. 根据权利要求9所述的方法,其特征在于,所述根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽,包括:
    当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
  11. 根据权利要求9或10所述的方法,其特征在于,所述方法还包括:
    根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
    根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
  12. 根据权利要求9或10所述的方法,其特征在于,所述根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽,包括:
    当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值。
  13. 根据权利要求12所述的方法,其特征在于,所述方法还包括:
    根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
    根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
  14. 根据权利要求1至13中任一项所述的方法,其特征在于,在所述神经网络运算的微调阶段和/或训练阶段,所述方法还包括:
    获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
    根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
  15. 根据权利要求14所述的方法,其特征在于,所述方法还包括:
    根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
  16. 根据权利要求15所述的方法,其特征在于,所述方法还包括:
    根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位置。
  17. 根据权利要求14所述的方法,其特征在于,所述获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
    根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
    根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
    所述根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数,包括:
    根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
  18. 根据权利要求17所述的方法,其特征在于,所述根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度,包括:
    计算所述第一滑动平均值和所述第二滑动平均值的差值;
    将所述差值的绝对值确定为第一数据变动幅度。
  19. 根据权利要求18所述的方法,其特征在于,所述方法还包括:
    根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
    所述根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数,包括:
    根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
  20. 根据权利要求19所述的方法,其特征在于,所述根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度,包括:
    计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;
    将所述误差的平方确定为所述第二数据变动幅度。
  21. 根据权利要求19所述的方法,其特征在于,所述根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,包括:
    根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
  22. 根据权利要求14至21中任一项所述的方法,其特征在于,所述获取当前迭代以及历史迭代中待量化数据的数据变动幅度,包括:
    在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
  23. 根据权利要求14至22中任一项所述的方法,其特征在于,所述方法还包括:
    在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
    根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
    根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
  24. 一种神经网络量化装置,其特征在于,应用于终端,所述终端用于运行神经网络以执行目标任务,所述装置包括:
    数据确定模块,根据所述目标任务和/或所述终端的精度需求,从目标数据中确定多个待量化数据,各所述待量化数据均为所述目标数据的子集,所述目标数据为所述神经网络的待量化层的任意一种待量化的待运算数据,所述待运算数据包括输入神经元、权值、偏置、梯度中的至少一种;
    数据量化模块,将各所述待量化数据分别根据对应的量化参数进行量化,得到与各所述待量化数据对应的量化数据;
    结果确定模块,根据与各所述待量化数据对应的量化数据得到所述目标数据的量化结果,以使所述待量化层根据所述目标数据的量化结果进行运算。
  25. 根据权利要求24所述的装置,其特征在于,所述数据确定模块,包括以下至少一个子模块:
    第一确定子模块,根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的目标数据确定为一个待量化数据;
    第二确定子模块,根据所述目标任务和/或所述终端的精度需求,将一层或多层待量化层中的同一种待运算数据确定为一个待量化数据;
    第三确定子模块,根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个通道中的数据确定为一个待量化数据;
    第四确定子模块,根据所述目标任务和/或所述终端的精度需求,将对应待量化层的目标数据中一个或多个批数的数据确定为一个待量化数据;
    第五确定子模块,根据所述目标任务和/或所述终端的精度需求确定划分数据的划分尺寸,将对应待量化层中的目标数据按照所述划分尺寸划分为一个或多个待量化数据。
  26. 根据权利要求24或25所述的装置,其特征在于,所述装置还包括:
    数据位宽确定模块,根据所述目标任务和/或所述终端的精度需求,确定待量化数据对应的数据位宽;
    参数确定模块,根据各所述待量化数据和对应的数据位宽计算得到对应的量化参数。
  27. 根据权利要求26所述的装置,其特征在于,所述参数确定模块,包括:
    第一点位置确定子模块,当所述量化参数不包括偏移量时,根据各所述待量化数据中的绝对值最大值和对应的数据位宽,得到各所述待量化数据的第一类点位置。
  28. 根据权利要求26所述的装置,其特征在于,所述参数确定模块,包括:
    第一最大值确定子模块,当所述量化参数不包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
    第一缩放系数确定子模块,根据各所述待量化数据中的绝对值最大值和所述量化后数据的最大值,得到各所述待量化数据的第一类缩放系数。
  29. 根据权利要求26所述的装置,其特征在于,所述参数确定模块,包括:
    第二点位置确定子模块,当所述量化参数包括偏移量时,根据各所述待量化数据中的最大值、最小值和对应的数据位宽,得到各所述待量化数据的第二类点位置。
  30. 根据权利要求26所述的装置,其特征在于,所述参数确定模块,包括:
    第二最大值确定子模块,当所述量化参数包括偏移量时,根据各所述待量化数据和对应的数据位宽得到量化后数据的最大值;
    第一缩放系数确定子模块,根据各所述待量化数据中的最大值、最小值和量化后数据的最大值,得到各所述待量化数据的第二类缩放系数。
  31. 根据权利要求26所述的装置,其特征在于,所述参数确定模块,包括:
    偏移量确定子模块,根据各所述待量化数据中的最大值和最小值,得到各所述待量化数据的偏移量。
  32. 根据权利要求24至31任一项所述的装置,其特征在于,所述装置还包括:
    第一量化误差确定模块,根据各所述待量化数据和各所述待量化数据对应的量化数据,确定各所述待量化数据对应的量化误差;
    调整位宽确定模块,根据各所述待量化数据对应的量化误差和误差阈值,调整各所述待量化数据对应的数据位宽,得到各所述待量化数据对应的调整位宽;
    调整量化参数确定模块,将各所述待量化数据对应的数据位宽更新为对应的调整位宽,根据各所述待量化数据和对应的调整位宽计算得到对应的调整量化参数,以使各所述待量化数据根据所述对应的调整量化参数进行量化。
  33. 根据权利要求32所述的装置,其特征在于,所述调整位宽确定模块,包括:
    第一调整位宽确定子模块,当所述量化误差大于第一误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽。
  34. 根据权利要求32或33所述的装置,其特征在于,所述装置还包括:
    第一调整后量化误差模块,根据各所述待量化数据和对应的调整位宽计算各所述待量化数据调整后的量化误差;
    第一调整位宽循环确定模块,根据所述调整后的量化误差和所述第一误差阈值继续增加所述对应的调整位宽,直至所述调整后的量化误差小于或等于所述第一误差阈值。
  35. 根据权利要求32或33所述的装置,其特征在于,所述调整位宽确定模块,包括:
    第二调整位宽确定子模块,当所述量化误差小于第二误差阈值时,增加所述对应的数据位宽,得到所述对应调整位宽,所述第二误差阈值小于所述第一误差阈值。
  36. 根据权利要求35所述的装置,其特征在于,所述装置还包括:
    第二调整后量化误差模块,根据所述调整位宽和所述待量化数据计算所述待量化数据调整后的量化误差;
    第二调整位宽循环确定模块,根据所述调整后的量化误差和所述第二误差阈值继续减少所述调整位宽,直至根据调整位宽和所述待量化数据计算得到的调整后的量化误差大于或等于所述第二误差阈值。
  37. 根据权利要求24至36任一项所述的装置,其特征在于,在所述神经网络运算的微调阶段和/或训练阶段,所述装置还包括:
    第一数据变动幅度确定模块,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述历史迭代为所述当前迭代之前的迭代;
    目标迭代间隔确定模块,根据所述待量化数据的数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述待量化层根据所述目标迭代间隔更新所述待量化数据的量化参数,所述目标迭代间隔包括至少一次迭代。
  38. 根据权利要求37所述的装置,其特征在于,所述装置还包括:
    第一目标迭代间隔应用模块,根据所述待量化数据在所述当前迭代的数据位宽,确定所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,以使所述神经网络根据所述待量化数据在所述目标迭代间隔内的迭代对应的数据位宽,确定量化参数。
  39. 根据权利要求38所述的装置,其特征在于,所述装置还包括:
    第二目标迭代间隔应用模块,根据所述待量化数据在所述当前迭代对应的点位置,确定所述待量化数据在所述目标迭代间隔内的迭代对应的点位置,所述点位置包括第一类点位置和/或第二类点位 置。
  40. 根据权利要求37所述的装置,其特征在于,所述第一数据变动幅度确定模块,包括:
    滑动平均值计算子模块,根据待量化数据在当前迭代的点位置,和根据历史迭代间隔确定的与所述当前迭代对应的历史迭代的点位置,计算待量化数据对应各迭代间隔的点位置的滑动平均值,所述点位置包括第一类点位置和/或第二类点位置;
    第一数据变动幅度确定子模块,根据所述待量化数据在当前迭代的点位置的第一滑动平均值,以及在上一迭代间隔对应迭代的点位置的第二滑动平均值,得到第一数据变动幅度;
    其中,所述目标迭代间隔确定模块,包括:
    第一目标迭代间隔确定子模块,根据所述第一数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
  41. 根据权利要求40所述的装置,其特征在于,所述第一数据变动幅度确定子模块,包括:
    第一幅度确定子模块,计算所述第一滑动平均值和所述第二滑动平均值的差值;将所述差值的绝对值确定为第一数据变动幅度。
  42. 根据权利要求41所述的装置,其特征在于,所述装置还包括:
    第二数据变动幅度确定模块,根据在当前迭代所述待量化数据和所述待量化数据对应的量化数据,得到第二数据变动幅度;
    其中,目标迭代间隔确定模块,包括:
    第二目标迭代间隔确定子模块,根据所述待量化数据的第一数据变动幅度和所述第二数据变动幅度,确定所述待量化数据对应的目标迭代间隔,以使所述神经网络根据所述目标迭代间隔更新所述待量化数据的量化参数。
  43. 根据权利要求42所述的装置,其特征在于,所述第二数据变动幅度确定模块,包括:
    第二幅度确定子模块,计算在当前迭代所述待量化数据和所述待量化数据对应的量化数据之间的误差;将所述误差的平方确定为所述第二数据变动幅度。
  44. 根据权利要求42所述的装置,其特征在于,所述第二目标迭代间隔确定子模块,包括:
    间隔确定子模块,根据所述第一数据变动幅度和所述第二数据变动幅度中的最大值,确定所述待量化数据对应的目标迭代间隔。
  45. 根据权利要求37至44任一项所述的装置,其特征在于,所述第一数据变动幅度确定模块,包括:
    第二数据变动幅度确定子模块,在当前迭代位于更新周期以外时,获取当前迭代以及历史迭代中待量化数据的数据变动幅度,所述更新周期包括至少一个迭代。
  46. 根据权利要求37至45任一项所述的装置,其特征在于,所述装置还包括:
    周期间隔确定模块,在当前迭代位于预设周期内时,根据当前迭代、在所述预设周期的下一周期中与所述当前迭代对应的迭代以及当前迭代对应的迭代间隔,确定周期间隔;
    第一周期间隔应用模块,根据所述待量化数据在当前迭代对应的数据位宽,确定在所述周期间隔内的迭代中所述待量化数据的数据位宽;或
    第二周期间隔应用模块,根据所述待量化数据在当前迭代对应的点位置,确定在所述周期间隔内的迭代中所述待量化数据的点位置。
  47. 一种人工智能芯片,其特征在于,所述芯片包括如权利要求24至46中任意一项所述的神经网络量化装置。
  48. 一种电子设备,其特征在于,所述电子设备包括如权利要求47所述的人工智能芯片。
  49. 一种板卡,其特征在于,所述板卡包括:存储器件、接口装置和控制器件以及如权利要求47 所述的人工智能芯片;
    其中,所述人工智能芯片与所述存储器件、所述控制器件以及所述接口装置分别连接;
    所述存储器件,用于存储数据;
    所述接口装置,用于实现所述人工智能芯片与外部设备之间的数据传输;
    所述控制器件,用于对所述人工智能芯片的状态进行监控。
  50. 根据权利要求49所述的板卡,其特征在于,
    所述存储器件包括:多组存储单元,每一组所述存储单元与所述人工智能芯片通过总线连接,所述存储单元为:DDR SDRAM;
    所述芯片包括:DDR控制器,用于对每个所述存储单元的数据传输与数据存储的控制;
    所述接口装置为:标准PCIE接口。
  51. 一种非易失性计算机可读存储介质,其上存储有计算机程序指令,其特征在于,所述计算机程序指令被处理器执行时实现权利要求1至23中任意一项所述的神经网络量化方法。
PCT/CN2020/095673 2019-08-23 2020-06-11 数据处理方法、装置、计算机设备和存储介质 WO2021036412A1 (zh)

Applications Claiming Priority (12)

Application Number Priority Date Filing Date Title
CN201910786019.6 2019-08-23
CN201910786064 2019-08-23
CN201910786019 2019-08-23
CN201910786064.1 2019-08-23
CN201910797127.3 2019-08-27
CN201910797127 2019-08-27
CN201910888552.3A CN112085151A (zh) 2019-06-12 2019-09-19 数据处理方法、装置、计算机设备和存储介质
CN201910888449.9A CN112085176B (zh) 2019-06-12 2019-09-19 数据处理方法、装置、计算机设备和存储介质
CN201910888449.9 2019-09-19
CN201910888552.3 2019-09-19
CN201910889202.9A CN112085187A (zh) 2019-06-12 2019-09-19 数据处理方法、装置、计算机设备和存储介质
CN201910889202.9 2019-09-19

Publications (1)

Publication Number Publication Date
WO2021036412A1 true WO2021036412A1 (zh) 2021-03-04

Family

ID=74684080

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/095673 WO2021036412A1 (zh) 2019-08-23 2020-06-11 数据处理方法、装置、计算机设备和存储介质

Country Status (1)

Country Link
WO (1) WO2021036412A1 (zh)

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20100057452A1 (en) * 2008-08-28 2010-03-04 Microsoft Corporation Speech interfaces
CN107636697A (zh) * 2015-05-08 2018-01-26 高通股份有限公司 基于浮点神经网络量化的定点神经网络
CN107967515A (zh) * 2016-10-19 2018-04-27 三星电子株式会社 用于神经网络量化的方法和设备
CN109214509A (zh) * 2017-07-05 2019-01-15 中国科学院沈阳自动化研究所 一种用于深度神经网络高速实时量化结构和运算实现方法
CN110096647A (zh) * 2019-05-10 2019-08-06 腾讯科技(深圳)有限公司 优化量化模型的方法、装置、电子设备及计算机存储介质

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20100057452A1 (en) * 2008-08-28 2010-03-04 Microsoft Corporation Speech interfaces
CN107636697A (zh) * 2015-05-08 2018-01-26 高通股份有限公司 基于浮点神经网络量化的定点神经网络
CN107967515A (zh) * 2016-10-19 2018-04-27 三星电子株式会社 用于神经网络量化的方法和设备
CN109214509A (zh) * 2017-07-05 2019-01-15 中国科学院沈阳自动化研究所 一种用于深度神经网络高速实时量化结构和运算实现方法
CN110096647A (zh) * 2019-05-10 2019-08-06 腾讯科技(深圳)有限公司 优化量化模型的方法、装置、电子设备及计算机存储介质

Similar Documents

Publication Publication Date Title
WO2021036904A1 (zh) 数据处理方法、装置、计算机设备和存储介质
WO2021036905A1 (zh) 数据处理方法、装置、计算机设备和存储介质
WO2021036908A1 (zh) 数据处理方法、装置、计算机设备和存储介质
WO2021036890A1 (zh) 数据处理方法、装置、计算机设备和存储介质
CN112085186A (zh) 一种神经网络的量化参数确定方法及相关产品
US11704556B2 (en) Optimization methods for quantization of neural network models
CN110874627B (zh) 数据处理方法、数据处理装置及计算机可读介质
EP4024281A1 (en) Method and apparatus for processing data, and related product
CN112085176B (zh) 数据处理方法、装置、计算机设备和存储介质
CN112085187A (zh) 数据处理方法、装置、计算机设备和存储介质
CN112085182A (zh) 数据处理方法、装置、计算机设备和存储介质
Zhan et al. Field programmable gate array‐based all‐layer accelerator with quantization neural networks for sustainable cyber‐physical systems
WO2021036412A1 (zh) 数据处理方法、装置、计算机设备和存储介质
WO2022247368A1 (en) Methods, systems, and mediafor low-bit neural networks using bit shift operations
WO2021169914A1 (zh) 数据量化处理方法、装置、电子设备和存储介质
CN116997910A (zh) 张量控制器架构
CN112085151A (zh) 数据处理方法、装置、计算机设备和存储介质
CN113177627A (zh) 优化系统、重新训练系统及其方法及处理器和可读介质
Choudhury et al. Variable batch size across layers for efficient prediction on CNNs
US20220222041A1 (en) Method and apparatus for processing data, and related product
Sun et al. A 974gops/w multi-level parallel architecture for binary weight network acceleration
Sabri et al. Redy: A novel reram-centric dynamic quantization approach for energy-efficient cnn inference
CN112085177A (zh) 数据处理方法、装置、计算机设备和存储介质
Lei et al. Compression of Convolutional Neural Networks With Divergent Representation of Filters
US20230185352A1 (en) Analog mac aware dnn improvement

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 20857094

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 20857094

Country of ref document: EP

Kind code of ref document: A1

122 Ep: pct application non-entry in european phase

Ref document number: 20857094

Country of ref document: EP

Kind code of ref document: A1

32PN Ep: public notification in the ep bulletin as address of the adressee cannot be established

Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205A DATED 22.05.2023)

122 Ep: pct application non-entry in european phase

Ref document number: 20857094

Country of ref document: EP

Kind code of ref document: A1