A method, device and related equipment for fixing floating point matrix
By determining the appropriate fixed-point scale value in speech and image processing, and converting the floating-point matrix into a fixed-point matrix, the problem of data accuracy loss during fixed-point scale is solved, and the computing efficiency and accuracy are improved.
Patent Information
- Application Number
- CN201910817637.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-08-30
- Publication Date
- 2025-02-25
- Estimated Expiration
- 2039-08-30
AI Technical Summary
In the fields of speech and image processing, the direct calculation efficiency of floating point matrices is low, and the fixed-pointing process may lead to data accuracy loss, affecting subsequent calculation accuracy.
By obtaining the target floating point matrix, determine the fixed-point scale value, and use this scale value to convert the floating point matrix into a fixed-point matrix to reduce data accuracy loss. Specific methods include finding the maximum absolute value or building a small top heap to determine the fixed-pointed value.
Through the fixed-point scale value with high adaptability, the data accuracy loss when converting the floating-point matrix into a fixed-point matrix is reduced, and the efficiency and accuracy of matrix operations are improved.
Smart Images

Figure CN112446008B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of fixed-point technology, and in particular to a floating-point matrix fixed-point method, device and related equipment. Background Art
[0002] In fields such as speech and image processing, it is often necessary to perform operations on floating-point matrices, such as multiplying two floating-point matrices. If the floating-point matrix is directly operated, the operation efficiency is low. In order to improve the efficiency of matrix operations, the floating-point matrix is usually fixed-pointed to an integer matrix, and then the integer matrix is operated. However, the fixed-point process may cause a loss of data precision, resulting in a decrease in the accuracy of subsequent operations.
[0003] Therefore, how to reduce the data accuracy loss caused by the fixed-point process becomes an urgent problem to be solved. Summary of the invention
[0004] In order to solve the above technical problems, the embodiments of the present application provide a method, device and related equipment for converting a floating-point matrix into a fixed-point matrix, so as to reduce the data precision loss when converting a target floating-point matrix into a fixed-point matrix. The technical solution is as follows:
[0005] A method for converting a floating-point matrix to a fixed point, comprising:
[0006] Get the target floating-point matrix;
[0007] Determining fixed-point scalar values according to the elements in the target floating-point matrix;
[0008] The target floating-point matrix is converted into a fixed-point matrix using the fixed-point scalar value.
[0009] Preferably, determining the fixed-point calibration value of the target floating-point matrix according to the elements in the target floating-point matrix includes:
[0010] Determine the absolute value of each element in the target floating-point matrix respectively;
[0011] The maximum absolute value is found among the absolute values of the elements in the target floating-point matrix, and the maximum absolute value is used as the fixed-point calibration value.
[0012] Preferably, determining the fixed-point calibration value of the target floating-point matrix according to the elements in the target floating-point matrix includes:
[0013] Determine the absolute value of each element in the target floating-point matrix respectively;
[0014] Among the absolute values of the elements in the target floating-point matrix, determine the absolute values arranged in the first k order from largest to smallest, where k is an integer greater than or equal to 1;
[0015] The absolute values of the first k numbers in the arrangement are multiplied by their corresponding empirical parameters respectively to obtain products, and the sum of several of the products is used as the fixed-point calibration value. The sum of the several empirical parameters is 1.
[0016] Preferably, among the absolute values of the elements in the target floating-point matrix, determining the absolute values arranged in the first k in descending order includes:
[0017] Select k absolute values from the absolute values of each element in the target floating-point matrix, and construct a mini-heap using the k absolute values;
[0018] From the absolute values of the elements in the target floating-point matrix except the k absolute values, select an absolute value that has not been traversed as the target absolute value, and determine whether the target absolute value is greater than the top element of the mini-heap;
[0019] If the target absolute value is not greater than the top element of the mini-heap, determining whether there is an absolute value that has not been traversed among the absolute values of the elements in the target floating-point matrix other than the k absolute values;
[0020] If yes, returning to the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value;
[0021] If not, sort the k elements in the small top heap from large to small, and use the sorted k elements as the absolute values of the elements in the target floating-point matrix, and arrange them in the order from large to small in the first k absolute values;
[0022] If the target absolute value is greater than the top element in the mini-heap, the top element is replaced with the target absolute value, and the mini-heap is adjusted to a heap in which any parent node is not greater than its child node, and the step of returning to execute the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value.
[0023] Preferably, the converting the target floating-point matrix into a fixed-point matrix by using the fixed-point calibration value comprises:
[0024] The preset coefficient is calculated by using the relationship that the fixed-point calibration value multiplied by the preset coefficient is equal to the maximum value in the preset set point value range;
[0025] Each element in the target floating-point matrix is multiplied by the preset coefficient to obtain the fixed-point matrix.
[0026] Preferably, determining the fixed-point calibration value according to the elements in the target floating-point matrix includes:
[0027] If the number of elements in the target floating-point matrix is greater than a set number threshold, the target floating-point matrix is divided into a plurality of sub-floating-point matrices;
[0028] The fixed-point calibration value is determined according to the elements in each of the sub-floating-point matrices.
[0029] A floating-point matrix fixed-point device, comprising:
[0030] The acquisition module is used to obtain the target floating-point matrix;
[0031] A determination module, used for determining the fixed-point calibration value according to the elements in the target floating-point matrix;
[0032] A conversion module is used to convert the target floating-point matrix into a fixed-point matrix using the fixed-point scalar value.
[0033] Preferably, the determining module is specifically used to:
[0034] Determine the absolute value of each element in the target floating-point matrix respectively;
[0035] The maximum absolute value is found among the absolute values of the elements in the target floating-point matrix, and the maximum absolute value is used as the fixed-point calibration value.
[0036] Preferably, the determining module is specifically used to:
[0037] Determine the absolute value of each element in the target floating-point matrix respectively;
[0038] Among the absolute values of the elements in the target floating-point matrix, determine the absolute values arranged in the first k order from largest to smallest, where k is an integer greater than or equal to 1;
[0039] The absolute values of the first k numbers in the arrangement are multiplied by their corresponding empirical parameters respectively to obtain products, and the sum of several of the products is used as the fixed-point calibration value. The sum of the several empirical parameters is 1.
[0040] Preferably, the determining module is specifically used to:
[0041] Select k absolute values from the absolute values of each element in the target floating-point matrix, and construct a mini-heap using the k absolute values;
[0042] From the absolute values of the elements in the target floating-point matrix except the k absolute values, select an absolute value that has not been traversed as the target absolute value, and determine whether the target absolute value is greater than the top element of the mini-heap;
[0043] If the target absolute value is not greater than the top element of the mini-heap, determining whether there is an absolute value that has not been traversed among the absolute values of the elements in the target floating-point matrix other than the k absolute values;
[0044] If yes, returning to the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value;
[0045] If not, sort the k elements in the small top heap from large to small, and use the sorted k elements as the absolute values of the elements in the target floating-point matrix, and arrange them in the order from large to small in the first k absolute values;
[0046] If the target absolute value is greater than the top element in the mini-heap, the top element is replaced with the target absolute value, and the mini-heap is adjusted to a heap in which any parent node is not greater than its child node, and the step of returning to execute the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value.
[0047] Preferably, the conversion module is specifically used for:
[0048] The preset coefficient is calculated by using the relationship that the fixed-point calibration value multiplied by the preset coefficient is equal to the maximum value in the preset set point value range;
[0049] Each element in the target floating-point matrix is multiplied by the preset coefficient to obtain the fixed-point matrix.
[0050] Preferably, the determining module is specifically used to:
[0051] If the number of elements in the target floating-point matrix is greater than a set number threshold, the target floating-point matrix is divided into a plurality of sub-floating-point matrices;
[0052] The fixed-point calibration value is determined according to the elements in each of the sub-floating-point matrices.
[0053] An electronic device, comprising:
[0054] Memory, used to store programs;
[0055] The processor is used to run the program. When the processor runs the program, the processor implements any one of the floating-point matrix fixed-point conversion methods described above.
[0056] A storage medium, characterized in that it is used to store a program, and when the program is executed, it is used to implement any one of the floating-point matrix fixed-point conversion methods described above.
[0057] Compared with the prior art, the beneficial effects of this application are:
[0058] In the present application, the fixed-point calibration values are calculated based on the elements in the target floating-point matrix, which can ensure that the calculated fixed-point calibration values are compatible with the target floating-point matrix. Based on the fixed-point calibration values compatible with the target floating-point matrix, compared with the method of using the same fixed-point calibration values for different floating-point matrices, the data precision loss when converting the target floating-point matrix to a fixed-point matrix can be reduced. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0060] Figure 1 This is a flow chart of a method for converting a floating-point matrix to a fixed point in Example 1 provided by the present application;
[0061] Figure 2 This is a flow chart of a method for converting a floating-point matrix to a fixed point in Example 2 provided by the present application;
[0062] Figure 3 This is a flow chart of a method for converting a floating-point matrix to a fixed point in Example 3 provided by the present application;
[0063] Figure 4 The present application provides a flow chart of converting the target floating-point matrix into a fixed-point matrix by using the fixed-point calibration value;
[0064] Figure 5 This is a flow chart of a method for converting a floating-point matrix to a fixed point in Example 4 provided by the present application;
[0065] Figure 6 It is a logical structure diagram of a floating-point matrix fixed-point conversion device provided by the present application. DETAILED DESCRIPTION
[0066] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0067] The embodiment of the present application discloses a method for converting a floating-point matrix to a fixed point, comprising: obtaining a target floating-point matrix; calculating a fixed-point calibration value of the target floating-point matrix according to elements in the target floating-point matrix; and converting the target floating-point matrix to a fixed-point matrix using the fixed-point calibration value. In the present application, the data precision loss when converting the target floating-point matrix to a fixed-point matrix can be reduced.
[0068] Next, the floating-point matrix fixed-point conversion method disclosed in the embodiment of the present application is introduced. Figure 1 The figure is a flowchart of a method for fixing a floating-point matrix provided by the present application in Embodiment 1, and the method comprises the following steps:
[0069] Step S11, obtaining a target floating-point matrix.
[0070] The target floating-point matrix can be understood as a floating-point matrix that will participate in matrix operations. For example, taking the matrix calculation of the recognition model in speech recognition as an example, it involves multiplying two matrices A*B. The A matrix is obtained through model training and is a fixed resource. When receiving external audio data, the front-end module will process the audio and generate an audio feature matrix B. Both the A matrix and the B matrix can be used as target floating-point matrices.
[0071] It should be noted that the B matrix depends on the external audio data, and the parameters of the B matrix are difficult to determine and estimate. Therefore, whenever the incoming audio data changes, the B matrix will change, and the target floating-point matrix will change accordingly. However, since the A matrix is a fixed matrix, the target floating-point matrix is also a fixed matrix for the A matrix.
[0072] Step S12: Determine the fixed-point calibration value according to the elements in the target floating-point matrix.
[0073] The fixed-point scalar value can be understood as the scalar value used when the target floating-point matrix is fixed-pointed to a fixed-point matrix.
[0074] By determining the fixed-point calibration value according to the elements in the target floating-point matrix, it can be ensured that the fixed-point calibration value is compatible with the target floating-point matrix.
[0075] Step S13: convert the target floating-point matrix into a fixed-point matrix using the fixed-point calibration value.
[0076] The process of converting the target floating-point matrix into a fixed-point matrix using fixed-point calibration values can refer to the process of converting a floating-point matrix into a fixed-point matrix in the prior art, which will not be described in detail here.
[0077] In the present application, the fixed-point calibration values are calculated based on the elements in the target floating-point matrix, which can ensure that the calculated fixed-point calibration values are compatible with the target floating-point matrix. Based on the fixed-point calibration values compatible with the target floating-point matrix, compared with the method of using the same fixed-point calibration values for different floating-point matrices, the data precision loss when converting the target floating-point matrix to a fixed-point matrix can be reduced.
[0078] As another optional embodiment of the present application, refer to Figure 2 , is a flow chart of a floating-point matrix fixed-point method embodiment 2 provided by the present application. This embodiment is mainly a refinement of the floating-point matrix fixed-point method described in the above embodiment 1, such as Figure 2 As shown, the method may include but is not limited to the following steps:
[0079] Step S21, obtaining a target floating-point matrix.
[0080] The detailed process of step S21 can refer to the relevant introduction of step S11 in embodiment 1, which will not be repeated here.
[0081] Step S22: determine the absolute value of each element in the target floating-point matrix respectively.
[0082] Step S23, searching for the maximum absolute value among the absolute values of the elements in the target floating-point matrix, and using the maximum absolute value as the fixed-point calibration value.
[0083] In this embodiment, the maximum absolute value is found among the absolute values of the elements in the target floating-point matrix, and the maximum absolute value is used as the fixed-point calibration value to ensure that all elements in the target floating-point matrix can be converted into integers using the fixed-point calibration value.
[0084] Steps S22-S23 are a specific implementation of step S12 in Example 1.
[0085] Step S24: convert the target floating-point matrix into a fixed-point matrix using the fixed-point calibration value.
[0086] The detailed process of step S24 can refer to the relevant introduction of step S13 in embodiment 1, which will not be repeated here.
[0087] As another optional embodiment of the present application, refer to Figure 3 , is a flow chart of a floating-point matrix fixed-point method embodiment 3 provided by the present application. This embodiment is mainly a refinement of the floating-point matrix fixed-point method described in the above embodiment 1, such as Figure 3 As shown, the method may include but is not limited to the following steps:
[0088] Step S31, obtaining a target floating-point matrix.
[0089] The detailed process of step S31 can refer to the relevant introduction of step S11 in embodiment 1, which will not be repeated here.
[0090] Step S32: determine the absolute value of each element in the target floating-point matrix respectively.
[0091] Step S33: Determine the absolute values of the first k elements in the target floating-point matrix in descending order, where k is an integer greater than or equal to 1.
[0092] The inventor found in the research that although the method for determining the fixed-point calibration value in Example 2 can ensure that all elements of the target floating-point matrix can be fixed-pointed to integers, there is a problem of data precision loss. Therefore, the ideal fixed-point calibration value should be able to fix all elements of the floating-point matrix to integers while minimizing data precision loss. Based on this, an optimization scheme is provided in this embodiment to determine the fixed-point calibration value.
[0093] Specifically, first, it is necessary to determine the absolute values of the first k elements in the absolute values of the elements in the target floating-point matrix in descending order.
[0094] Preferably, a TopK algorithm may be used to determine the absolute values of the first k elements in the absolute values of the elements in the target floating-point matrix in descending order.
[0095] The process of using the TopK algorithm to determine the absolute values of the first k elements in the absolute values of the elements in the target floating-point matrix in descending order may include:
[0096] A11. Select k absolute values from the absolute values of each element in the target floating-point matrix, and construct a mini-heap using the k absolute values.
[0097] A12. Select an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value, and determine whether the target absolute value is greater than the top element of the mini-heap.
[0098] If the target absolute value is not greater than the top element in the mini-top heap, execute step A13; if the target absolute value is greater than the top element in the mini-top heap, execute step A15.
[0099] A13. Determine whether there is an absolute value that has not been traversed among the absolute values of each element in the target floating-point matrix except the k absolute values.
[0100] If so, return to the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value; if not, execute step A14.
[0101] A14. Sort the k elements in the mini-top heap from largest to smallest, and use the sorted k elements as the absolute values of the elements in the target floating-point matrix, and arrange them in the order from largest to smallest in the first k absolute values.
[0102] In this embodiment, the sorting method is not limited to sorting the k elements in the small top heap from large to small. For example, when k is small, the quick sorting method can be used to sort the k elements in the small top heap from large to small to ensure sorting efficiency. Of course, the method of combining a heap and a stack can also be used to achieve sorting, which can be specifically: construct a stack with a depth of k, and push the top elements of the small top heap into the stack in sequence. For each push, adjust the small top heap to a heap in which any parent node is not larger than its child node, ensure that the value pushed into the stack each time is the minimum value, and after the stack is full, take out the k numbers in descending order in sequence.
[0103] A15, replacing the top element of the heap with the target absolute value, adjusting the top heap to a heap in which any parent node is not larger than its child node, and returning to execute the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value.
[0104] Replacing the top element of the heap with the target absolute value requires adjusting the mini-heap. During the adjustment process, the elements in the mini-heap can be flexibly adjusted through the combination of the heap and the stack, and the mini-heap can be adjusted to a heap containing no more than k elements and any parent node is no larger than its child node.
[0105] Preferably, the mini-heap can be adjusted to a heap containing k elements and any parent node is not larger than its child node.
[0106] Step S34, multiply the absolute values of the first k items by their corresponding empirical parameters to obtain products, and use the sum of several products as the fixed-point calibration value. The sum of the several empirical parameters is 1.
[0107] The sum of the multiple empirical parameters is 1.
[0108] The absolute values of the first k absolute values are multiplied by their corresponding empirical parameters to obtain products, and the sum of the multiple products is used as the fixed-point calibration value of the target floating-point matrix, so that the absolute values of the first k absolute values are used to jointly determine the fixed-point calibration value. Compared with the method of using the largest absolute value as the fixed-point calibration value, the loss of data accuracy can be reduced. For example, when the target floating-point matrix {0.1, 0.2, 0.3, 1.1} is fixed-pointed, the fixed-point calibration value is 1.1 to ensure that the calibration value does not explode, but the calibration value is 0.8 to obtain higher data accuracy.
[0109] In this embodiment, the following relationship can be used to multiply the absolute values of the first k by their corresponding empirical parameters to obtain products, and the sum of the multiple products is used as the fixed-point calibration value of the target floating-point matrix:
[0110] Fixed-point calibration value = |a 1 |*θ 1 +|a 2 |*θ 2 +...|a k |*θ k ;
[0111] |a 1 |,|a 2 |, ... |a k | are the absolute values of the first k, θ 1 for |a 1 |The corresponding empirical parameter, θ 2 for |a 2 |The corresponding empirical parameters, ..., θ k for |a k |The corresponding empirical parameters, and θ 1 +θ 2 +...θ k =1.
[0112] It should be noted that some of the products are partial products or all products of the products obtained by multiplying the absolute values of the first k by their corresponding empirical parameters. For example, k is 10, the absolute values of the first k are a1, a2, a3, a4, a5, a6, a7, a8, a9 and a10, and a1, a2, a3, a4, a5, a6, a7, a8, a9 and a10 are multiplied by their corresponding empirical parameters, and the number of products obtained is 10. Then, the some products can be n products of the 10 products, and the value range of n is 1 to 10.
[0113] Preferably, the plurality of products are all products of the products obtained by multiplying the absolute values of the first k items by their corresponding empirical parameters respectively.
[0114] Steps S32-S34 are a specific implementation process of step S12 in Example 1.
[0115] Step S35: convert the target floating-point matrix into a fixed-point matrix using the fixed-point calibration value.
[0116] The detailed process of step S35 can refer to the relevant introduction of step S13 in embodiment 1, which will not be repeated here.
[0117] In another embodiment of the present application, the process of converting the target floating-point matrix into a fixed-point matrix by using the fixed-point calibration value in Embodiment 2 and Embodiment 3 is introduced. Figure 4 , which may include the following steps:
[0118] Step S41, using the relationship formula that the fixed-point calibration value multiplied by the preset coefficient is equal to the maximum value in the preset set point value range, to calculate the preset coefficient.
[0119] The preset point value range can be flexibly set as needed. For example, if the elements in the target floating-point matrix are mapped to int8, the preset point value range can be set to -128 to 127 or 0 to 255.
[0120] Step S42: multiply each element in the target floating-point matrix by the preset coefficient to obtain the fixed-point matrix.
[0121] Each element in the target floating-point matrix is multiplied by the preset coefficient to convert each element in the target floating-point matrix into an integer, and a matrix composed of the converted integers is used as a fixed-point matrix.
[0122] As another optional embodiment of the present application, refer to Figure 5 , is a flow chart of a method for fixing a floating-point matrix in Embodiment 4 provided by the present application. This embodiment is mainly a refinement of the method for fixing a floating-point matrix in Embodiment 1 above, such as Figure 5 As shown, the method may include but is not limited to the following steps:
[0123] Step S51, obtaining a target floating-point matrix.
[0124] The detailed process of step S51 can refer to the relevant introduction of step S11 in embodiment 1, which will not be repeated here.
[0125] Step S52: If the number of elements in the target floating-point matrix is greater than a set number threshold, the target floating-point matrix is divided into a plurality of sub-floating-point matrices.
[0126] If the number of elements in the target floating-point matrix is greater than a set number threshold, the target floating-point matrix is divided into a plurality of sub-floating-point matrices to reduce computational complexity.
[0127] Step S53: determine the fixed-point calibration value according to the elements in each of the sub-floating-point matrices.
[0128] It can be understood that after the target floating-point matrix is divided into multiple sub-floating-point matrices, it is necessary to determine the fixed-point calibration value according to the elements in each sub-floating-point matrix, and the number of the fixed-point calibration values is the same as the number of the sub-floating-point matrices. For example, if the target floating-point matrix is divided into three sub-floating-point matrices, namely sub-floating-point matrices A, B, and C, the fixed-point calibration value A is determined according to the elements in sub-floating-point matrix A, the fixed-point calibration value B is determined according to the elements in sub-floating-point matrix B, and the fixed-point calibration value C is determined according to the elements in sub-floating-point matrix C.
[0129] The process of determining the fixed-point calibration value according to the elements in each of the sub-floating-point matrices can refer to the process of determining the fixed-point calibration value in Embodiments 2 and 3, which will not be described in detail here.
[0130] The fixed-point calibration value can be determined in parallel according to the elements in each of the sub-floating-point matrices to improve efficiency.
[0131] Steps S52-S53 are a specific implementation process of step S12 in Example 1.
[0132] Step S54: convert the target floating-point matrix into a fixed-point matrix using the fixed-point calibration value.
[0133] The detailed process of step S54 can refer to the relevant introduction of step S13 in embodiment 1, which will not be repeated here.
[0134] Next, a floating-point matrix fixed-point conversion device provided in an embodiment of the present application is introduced. The floating-point matrix fixed-point conversion device introduced below and the floating-point matrix fixed-point conversion method introduced above can be referred to each other.
[0135] See also Figure 6 The floating-point matrix fixed-point device includes: an acquisition module 11, a determination module 12 and a conversion module 13.
[0136] An acquisition module 11 is used to acquire a target floating-point matrix;
[0137] A determination module 12, used for determining a fixed-point calibration value according to elements in the target floating-point matrix;
[0138] The conversion module 13 is used to convert the target floating-point matrix into a fixed-point matrix by using the fixed-point calibration value.
[0139] In this embodiment, the determining module 12 may be specifically used for:
[0140] Determine the absolute value of each element in the target floating-point matrix respectively;
[0141] The maximum absolute value is found among the absolute values of the elements in the target floating-point matrix, and the maximum absolute value is used as the fixed-point calibration value.
[0142] In this embodiment, the determining module 12 may be specifically used for:
[0143] Determine the absolute value of each element in the target floating-point matrix respectively;
[0144] Among the absolute values of the elements in the target floating-point matrix, determine the absolute values arranged in the first k order from largest to smallest, where k is an integer greater than or equal to 1;
[0145] The absolute values of the first k numbers in the arrangement are multiplied by their corresponding empirical parameters respectively to obtain products, and the sum of several of the products is used as the fixed-point calibration value. The sum of the several empirical parameters is 1.
[0146] In this embodiment, the determining module 12 is specifically used for:
[0147] Select k absolute values from the absolute values of each element in the target floating-point matrix, and construct a mini-heap using the k absolute values;
[0148] From the absolute values of the elements in the target floating-point matrix except the k absolute values, select an absolute value that has not been traversed as the target absolute value, and determine whether the target absolute value is greater than the top element of the mini-heap;
[0149] If the target absolute value is not greater than the top element of the mini-heap, determining whether there is an absolute value that has not been traversed among the absolute values of the elements in the target floating-point matrix other than the k absolute values;
[0150] If yes, returning to the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value;
[0151] If not, sort the k elements in the small top heap from large to small, and use the sorted k elements as the absolute values of the elements in the target floating-point matrix, and arrange them in the order from large to small in the first k absolute values;
[0152] If the target absolute value is greater than the top element in the mini-heap, the top element is replaced with the target absolute value, and the mini-heap is adjusted to a heap in which any parent node is not greater than its child node, and the step of returning to execute the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value.
[0153] In this embodiment, the determining module 12 may be specifically used for:
[0154] If the number of elements in the target floating-point matrix is greater than a set number threshold, the target floating-point matrix is divided into a plurality of sub-floating-point matrices;
[0155] The fixed-point calibration value is determined according to the elements in each of the sub-floating-point matrices.
[0156] In another embodiment of the present application, an electronic device is introduced, including: a memory and a processor.
[0157] Memory, used to store programs;
[0158] The processor is used to run the program. When the processor runs the program, the processor implements the floating-point matrix fixed-point conversion method as described in the above embodiments.
[0159] In another embodiment of the present application, a storage medium is introduced for storing a program. When the program is executed, it is used to implement the floating-point matrix fixed-point conversion method as introduced in the above embodiments.
[0160] It should be noted that each embodiment focuses on the differences from other embodiments, and the same or similar parts between the embodiments can be referred to each other. For the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0161] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0162] For the convenience of description, the above device is described in various units according to their functions. Of course, when implementing the present application, the functions of each unit can be implemented in the same or multiple software and / or hardware.
[0163] It can be known from the description of the above implementation methods that those skilled in the art can clearly understand that the present application can be implemented by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the present application can be essentially or partly contributed to the prior art in the form of a software product, which can be stored in a storage medium such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in the various embodiments of the present application or certain parts of the embodiments.
[0164] The above is a detailed introduction to a floating-point matrix fixed-point method, device and related equipment provided by the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea. At the same time, for general technical personnel in this field, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A method for converting a floating-point matrix to a fixed point, characterized in that: include: Obtain a target floating-point matrix, where the target floating-point matrix is a floating-point matrix of a speech recognition model; Determining fixed-point scalar values according to the elements in the target floating-point matrix; Converting the target floating-point matrix into a fixed-point matrix using the fixed-point calibration value; The step of determining the fixed-point calibration value according to the elements in the target floating-point matrix comprises: Determine the absolute value of each element in the target floating-point matrix respectively; Among the absolute values of the elements in the target floating-point matrix, determine the absolute values arranged in the first k order from largest to smallest, where k is an integer greater than or equal to 1; The absolute values of the first k items in the arrangement are multiplied by their corresponding empirical parameters respectively to obtain products, and the sum of several of the products is used as the fixed-point calibration value. The sum of several empirical parameters is 1.
2. The method according to claim 1, characterized in that Among the absolute values of the elements in the target floating-point matrix, the absolute values arranged in the first k are determined in descending order, including: Select k absolute values from the absolute values of each element in the target floating-point matrix, and construct a mini-heap using the k absolute values; From the absolute values of the elements in the target floating-point matrix except the k absolute values, select an absolute value that has not been traversed as the target absolute value, and determine whether the target absolute value is greater than the top element of the mini-heap; If the target absolute value is not greater than the top element of the mini-heap, determining whether there is an absolute value that has not been traversed among the absolute values of the elements in the target floating-point matrix other than the k absolute values; If yes, returning to the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value; If not, sort the k elements in the small top heap from large to small, and use the sorted k elements as the absolute values of the elements in the target floating-point matrix, and arrange them in the order from large to small in the first k absolute values; If the target absolute value is greater than the top element in the mini-heap, the top element is replaced with the target absolute value, and the mini-heap is adjusted to a heap in which any parent node is not greater than its child node, and the step of returning to execute the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value.
3. The method according to claim 1, characterized in that The converting the target floating-point matrix into a fixed-point matrix by using the fixed-point calibration value includes: The preset coefficient is calculated by using the relationship that the fixed-point calibration value multiplied by the preset coefficient is equal to the maximum value in the preset set point value range; Each element in the target floating-point matrix is multiplied by the preset coefficient to obtain the fixed-point matrix.
4. The method according to claim 1, characterized in that: The step of determining the fixed-point calibration value according to the elements in the target floating-point matrix comprises: If the number of elements in the target floating-point matrix is greater than a set number threshold, the target floating-point matrix is divided into a plurality of sub-floating-point matrices; The fixed-point calibration value is determined according to the elements in each of the sub-floating-point matrices.
5. A floating point matrix fixed point device, characterized in that: include: An acquisition module, used for acquiring a target floating-point matrix, wherein the target floating-point matrix is a floating-point matrix of a speech recognition model; A determination module, used for determining the fixed-point calibration value according to the elements in the target floating-point matrix; A conversion module, used for converting the target floating-point matrix into a fixed-point matrix using the fixed-point calibration value; The determination module is specifically used for: Determine the absolute value of each element in the target floating-point matrix respectively; Among the absolute values of the elements in the target floating-point matrix, determine the absolute values arranged in the first k order from largest to smallest, where k is an integer greater than or equal to 1; The absolute values of the first k items in the arrangement are multiplied by their corresponding empirical parameters respectively to obtain products, and the sum of several of the products is used as the fixed-point calibration value. The sum of several empirical parameters is 1.
6. The device according to claim 5, characterized in that The determination module is specifically used for: Select k absolute values from the absolute values of each element in the target floating-point matrix, and construct a mini-heap using the k absolute values; From the absolute values of the elements in the target floating-point matrix except the k absolute values, select an absolute value that has not been traversed as the target absolute value, and determine whether the target absolute value is greater than the top element of the mini-heap; If the target absolute value is not greater than the top element of the mini-heap, determining whether there is an absolute value that has not been traversed among the absolute values of the elements in the target floating-point matrix other than the k absolute values; If yes, returning to the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value; If not, sort the k elements in the small top heap from large to small, and use the sorted k elements as the absolute values of the elements in the target floating-point matrix, and arrange them in the order from large to small in the first k absolute values; If the target absolute value is greater than the top element in the mini-heap, the top element is replaced with the target absolute value, and the mini-heap is adjusted to a heap in which any parent node is not greater than its child node, and the step of returning to execute the step of selecting an absolute value that has not been traversed from the absolute values of each element in the target floating-point matrix except the k absolute values as the target absolute value.
7. The device according to claim 5, characterized in that The conversion module is specifically used for: The preset coefficient is calculated by using the relationship that the fixed-point calibration value multiplied by the preset coefficient is equal to the maximum value in the preset set point value range; Each element in the target floating-point matrix is multiplied by the preset coefficient to obtain the fixed-point matrix.
8. The device according to claim 5, characterized in that The determination module is specifically used for: If the number of elements in the target floating-point matrix is greater than a set number threshold, the target floating-point matrix is divided into a plurality of sub-floating-point matrices; The fixed-point calibration value is determined according to the elements in each of the sub-floating-point matrices.
9. An electronic device, characterized in that: include: Memory, used to store programs; The processor is used to run the program. When the processor runs the program, the processor implements the floating-point matrix fixed-point method according to any one of claims 1 to 4.
10. A storage medium, characterized in that: Used to store a program, which, when executed, is used to implement the floating-point matrix fixed-point method as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Method and system for improving floating point matrix operation efficiency
CN107045494A