Precision conversion device, data processing method, processor, and electronic device
By performing at least two accuracy conversions in the artificial intelligence model, converting high-precision floating-point numbers into low-precision floating-point numbers, the problem of floating-point accuracy conversion error is solved, and the consistency of results and cost reduction are achieved.
Patent Information
- Application Number
- CN202410578384.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-11
- Publication Date
- 2024-11-26
- Estimated Expiration
- 2044-05-11
AI Technical Summary
The existing technology has errors in floating point accuracy conversion in artificial intelligence models, resulting in inconsistent results of training tasks of different precision conversion methods, which cannot be compared or adjusted by parameters, and introduce additional errors and R&D costs.
By at least two precision conversions, convert high-precision input floating point numbers (such as FP32) into low-precision output floating point numbers (such as BF16), reducing the accuracy of parameters in each conversion, and using the original precision conversion operator to Eliminate errors at the lowest cost and avoid providing new precision conversion operators.
The results of multiple accuracy conversions are achieved the same as those of one accuracy conversions, which eliminates errors, reduces R&D costs, and can perform error-free conversions between different precisions.
Smart Images

Figure CN118170347B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present disclosure relate to a precision conversion method, a data processing method, a processor, a precision conversion device, an electronic device, and a non-transitory computer-readable storage medium. Background Art
[0002] Floating point (FP) is mainly used to represent decimals, and usually consists of three parts, namely the sign bit, the exponent part, and the mantissa part. The exponent part can also be called the exponent part. For example, a floating point number V can usually be expressed as follows:
[0003]
[0004] Among them, the sign bit s can be 1 bit, which determines whether the floating-point number V is negative or positive; M represents the mantissa part, which can include multiple bits, which is in the form of a binary decimal and defines the precision of the floating-point number; E represents the exponent (also called the exponent value), which is used to weight the floating-point number, reflects the position of the decimal point in the floating-point number V, and defines the value range of the floating-point number. Summary of the invention
[0005] At least one embodiment of the present disclosure provides a precision conversion method, comprising: obtaining an input parameter, wherein the input parameter is a floating point number; performing at least two precision conversions on the input parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an output parameter, wherein the number of mantissa bits of the output parameter is a, and a is a positive integer; wherein, in each precision conversion, the number of mantissa bits of the parameter before the precision conversion is greater than the number of mantissa bits of the parameter after the precision conversion.
[0006] For example, in the precision conversion method provided in at least one embodiment of the present disclosure, the at least two precision conversions include a first precision conversion and a second precision conversion, and the input parameter is subjected to at least two precision conversions according to the a+1th bit in the mantissa part of the input parameter to obtain an output parameter, including: performing the first precision conversion on the input parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an intermediate parameter, wherein the number of mantissa bits of the intermediate parameter is less than the number of mantissa bits of the input parameter but greater than a; performing the second precision conversion on the intermediate parameter according to the a+1th bit in the mantissa part of the input parameter to obtain the output parameter.
[0007] For example, in the precision conversion method provided by at least one embodiment of the present disclosure, the first precision conversion is performed on the input parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an intermediate parameter, including: in response to the a+1th bit being a first value: setting the b+1th bit in the mantissa part of the input parameter to the first value to obtain a first intermediate parameter, and performing a rounding operation on the first intermediate parameter in combination with a first rounding rule to obtain the intermediate parameter; wherein b is the number of mantissa bits of the intermediate parameter, b is a positive integer and is greater than a, and the first rounding rule is a predetermined digital rounding rule for converting the precision format of the input parameter into the precision format of the intermediate parameter.
[0008] For example, in the precision conversion method provided by at least one embodiment of the present disclosure, according to the a+1th bit in the mantissa part of the input parameter, the first precision conversion is performed on the input parameter to obtain an intermediate parameter, including: in response to the a+1th bit being the second value and the a+2th to cth bits in the mantissa part of the input parameter not all being the first value: setting the b+1th bit in the mantissa part of the input parameter to the first value, and setting at least one bit from the a+2th to the bth bit in the mantissa part of the input parameter to the second value, to obtain a second intermediate parameter, and performing the first rounding rule on the a+1th bit. The second intermediate parameter is rounded to obtain the intermediate parameter; in response to the a+1th bit being the second value and the a+2th bit to the cth bit in the mantissa part of the input parameter being the first value: the input parameter is rounded in combination with the first rounding rule to obtain the intermediate parameter; wherein b is the number of mantissa digits of the intermediate parameter, c is the number of mantissa digits of the input parameter, b is a positive integer greater than a but less than c, c is a positive integer, and the first rounding rule is a predefined digital rounding rule for converting the precision format of the input parameter into the precision format of the intermediate parameter.
[0009] For example, in the precision conversion method provided by at least one embodiment of the present disclosure, the first rounding rule indicates that in response to the b+1th bit in the mantissa part of the input parameter being the first value, the 1st to bth bits in the mantissa part of the input parameter are directly used as the mantissa part of the intermediate parameter.
[0010] For example, in the precision conversion method provided in at least one embodiment of the present disclosure, the second precision conversion is performed on the intermediate parameter according to the a+1th bit in the mantissa part of the input parameter to obtain the output parameter, including: in combination with a second rounding rule, rounding the intermediate parameter according to the a+1th bit in the mantissa part of the input parameter to obtain the output parameter, wherein the second rounding rule is a predetermined digital rounding rule for converting the precision format of the intermediate parameter into the precision format of the output parameter.
[0011] For example, in the precision conversion method provided in at least one embodiment of the present disclosure, in combination with the second rounding rule, the intermediate parameter is rounded according to the a+1th bit in the mantissa part of the input parameter to obtain the output parameter, including: in response to the a+1th bit in the mantissa part of the intermediate parameter being a first value, directly using the 1st to ath bits in the mantissa part of the intermediate parameter as the mantissa part of the output parameter; in response to the a+1th bit in the mantissa part of the intermediate parameter being a second value and the a+1th bit in the mantissa part of the intermediate parameter being a second value At least one of the a+2th to bth bits is the second value, the a+1th to bth bits in the mantissa part of the intermediate parameter are discarded, and a carry operation is performed on the 1st to ath bits in the mantissa part of the intermediate parameter to obtain the mantissa part of the output parameter; in response to the a+1th bit in the mantissa part of the intermediate parameter being the second value and the a+2th to bth bits in the mantissa part of the intermediate parameter being the first value, the mantissa part of the output parameter is determined according to the ath bit in the mantissa part of the intermediate parameter.
[0012] For example, in the precision conversion method provided by at least one embodiment of the present disclosure, the mantissa part of the output parameter is determined according to the ath bit in the mantissa part of the intermediate parameter, including: in response to the ath bit in the mantissa part of the intermediate parameter being the first value, determining the 1st to ath bits in the mantissa part of the intermediate parameter directly as the mantissa part of the output parameter; in response to the ath bit in the mantissa part of the intermediate parameter being the second value, discarding the a+1th to bth bits in the mantissa part of the intermediate parameter, and performing the carry operation on the 1st to ath bits in the mantissa part of the intermediate parameter to obtain the mantissa part of the output parameter.
[0013] For example, in the precision conversion method provided in at least one embodiment of the present disclosure, the output parameter is the same as a reference output parameter obtained by performing a precision conversion on the input parameter according to a third rounding rule, the number of digits in the mantissa of the reference output parameter is a, and the third rounding rule is a digital rounding rule that directly converts the precision format of the input parameter into the precision format of the output parameter.
[0014] At least one embodiment of the present disclosure provides a precision conversion device, comprising: an acquisition module, configured to acquire an input parameter, wherein the input parameter is a floating point number; a precision conversion module, configured to perform at least two precision conversions on the input parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an output parameter, wherein the number of mantissa bits of the output parameter is a, and a is a positive integer; wherein, in each precision conversion, the number of mantissa bits of the parameter before the precision conversion is greater than the number of mantissa bits of the parameter after the precision conversion.
[0015] At least one embodiment of the present disclosure provides a data processing method, comprising: receiving a precision conversion instruction, wherein the precision conversion instruction includes an input parameter, parsing the precision conversion instruction, and then executing the precision conversion instruction using a precision conversion unit, wherein the precision conversion unit is used to perform at least two precision conversions, wherein executing the precision conversion instruction using the precision conversion unit comprises: performing the at least two precision conversions on the input parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an output parameter, wherein the number of mantissa bits of the output parameter is a, wherein a is a positive integer, and in each precision conversion, the number of mantissa bits of the parameter before the precision conversion is greater than the number of mantissa bits of the parameter after the precision conversion.
[0016] At least one embodiment of the present disclosure provides a processor, comprising an instruction parsing unit and a precision conversion unit, wherein the instruction parsing unit is used to receive and parse a precision conversion instruction, wherein the precision conversion instruction comprises an input parameter, and the precision conversion unit executes the precision conversion method according to any embodiment of the present disclosure after the instruction parsing unit parses the precision conversion instruction.
[0017] At least one embodiment of the present disclosure provides an electronic device, comprising: a memory, which non-transiently stores computer-executable instructions; and a processor, which is configured to execute the computer-executable instructions, wherein the computer-executable instructions, when executed by the processor, implement the precision conversion method or data processing method according to any embodiment of the present disclosure.
[0018] At least one embodiment of the present disclosure provides a non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, the precision conversion method or the data processing method described in any embodiment of the present disclosure is implemented. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the drawings of the embodiments will be briefly introduced below. Obviously, the drawings in the following description only relate to some embodiments of the present disclosure, but are not intended to limit the present disclosure.
[0020] Figure 1 A schematic flow chart of a precision conversion method provided in at least one embodiment of the present disclosure;
[0021] Figure 2 A schematic block diagram of a precision conversion device provided in at least one embodiment of the present disclosure;
[0022] Figure 3 A schematic flow chart of a data processing method provided for at least one embodiment of the present disclosure;
[0023] Figure 4 A schematic structural diagram of a processor provided for at least one embodiment of the present disclosure;
[0024] Figure 5 A schematic block diagram of an electronic device provided in accordance with an embodiment of the present disclosure;
[0025] Figure 6 A schematic diagram of a non-transitory computer-readable storage medium provided for at least one embodiment of the present disclosure. DETAILED DESCRIPTION
[0026] In order to make the purpose, technical solution and advantages of the embodiments of the present disclosure clearer, the technical solution of the embodiments of the present disclosure will be clearly and completely described below in conjunction with the drawings of the embodiments of the present disclosure. Obviously, the described embodiments are part of the embodiments of the present disclosure, not all of the embodiments. Based on the described embodiments of the present disclosure, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present disclosure.
[0027] Unless otherwise defined, the technical terms or scientific terms used in the present disclosure should be understood by people with ordinary skills in the field to which the present disclosure belongs. The "first", "second" and similar words used in the present disclosure do not indicate any order, quantity or importance, but are only used to distinguish different components. "Include" or "comprise" and similar words mean that the elements or objects appearing in front of the word cover the elements or objects listed after the word and their equivalents, without excluding other elements or objects. "Connect" or "connected" and similar words are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. "Up", "down", "left", "right" and the like are only used to indicate relative positional relationships. When the absolute position of the described object changes, the relative positional relationship may also change accordingly. In order to keep the following description of the embodiments of the present disclosure clear and concise, the present disclosure omits the detailed description of some known functions and known components.
[0028] Traditional floating-point numbers generally include three formats, namely half-precision floating-point numbers (FP16), single-precision floating-point numbers (FP32), and double-precision floating-point numbers (FP64), and their exponent and mantissa parts have different numbers of bits.
[0029] AI (Artificial Intelligence) accelerators have been widely used in deep learning model training. For the common convolution operations in deep learning models, special optimizations have been made in the design of software and hardware to accelerate calculations. For example, a variety of floating-point data formats have been optimized and developed for fields such as artificial intelligence or deep learning, such as BF16 (brain floating point 16, bit width is 16 bits), BF24 (brain floating point 24, bit width is 24 bits), TF32 (Tensor Float 32, bit width is 19 bits), etc. These data formats can greatly reduce the computing processing, especially the computing resources and power consumption required for matrix multiplication or convolution multiplication operations. In addition, the processor also supports some conventional floating-point types, such as half-precision floating point (FP16, bit width is 16 bits) or single-precision floating point (FP32, bit width is 32 bits).
[0030] Table 1 Data format
[0031]
[0032] Table 1 shows the data formats of several floating point precision types. As shown in Table 1, for the floating point precision type FP32, the total number of bits is 32 bits, including 1 sign bit, the exponent part (that is, the exponent code) includes 8 bits, and the mantissa part includes 23 bits. For BF20, the total number of bits is 20 bits, including 1 sign bit, the exponent part (that is, the exponent code) includes 8 bits, and the mantissa part includes 11 bits. For BF16, the total number of bits is 16 bits, including 1 sign bit, the exponent part (that is, the exponent code) includes 8 bits, and the mantissa part includes 7 bits. For BF24, the total number of bits is 24 bits, including 1 sign bit, the exponent part (that is, the exponent code) includes 8 bits, and the mantissa part includes 15 bits. For BF19, the total number of bits is 19 bits, including 1 sign bit, the exponent part (that is, the exponent code) includes 8 bits, and the mantissa part includes 10 bits.
[0033] As shown in Table 1, taking FP32 and BF16 as examples, the sign bit of FP32 and BF16 is represented by 1 bit, the exponent part is represented by 8 bits, and the mantissa part is represented by 23 and 7 bits respectively. Therefore, the data range represented by FP32 and BF16 is the same, but FP32 has higher precision.
[0034] High-precision data formats such as FP32 are conducive to the convergence of artificial intelligence models, while low-precision data formats such as BF16 are conducive to improving the speed of model training or reasoning.
[0035] For example, some operators use high-precision data formats such as FP32 for inference, and some operators use low-precision data formats such as BF16 for inference. Therefore, data transmission between operators using different precision data requires precision conversion. For floating-point numbers, precision conversion is to round or add the number of digits in the mantissa to change the data precision. In the process of processing data, the number of significant digits of each measurement value involved may be different. After the number of significant digits of each measurement value is determined, the excess digits behind it must be discarded according to certain rules. The process of discarding excess digits is called "digital rounding", and the rules it follows are called "digital rounding rules", that is, rounding rules.
[0036] For example, within an operator, the input data format is a low-precision data format such as BF16, but a high-precision data format such as FP32 is required for calculation. The output data format still needs to be a low-precision data format such as BF16, so multiple precision conversions are required.
[0037] In order to balance the contradiction between computing speed and computing accuracy, some artificial intelligence chips use intermediate precision data formats to store in registers. For example, in storage spaces such as HBM (High Bandwidth Memory), data is stored in low-precision formats such as BF16 to be compatible with input and output data, and the BF16 format is expanded to intermediate precision data formats such as BF20 in registers, thereby improving the calculation accuracy of BF16 data. For example, in another specific example, data is stored in BF19 format in storage spaces such as HBM to be compatible with input and output data, and the BF19 format is expanded to BF24 in registers, thereby improving the calculation accuracy of BF19 data.
[0038] Since there are multiple precisions in the model, a precision conversion operator is needed to convert between different precisions, such as converting from high-precision FP32 to low-precision BF16 following certain rounding rules.
[0039] Although the intermediate precision data formats such as BF20 and BF24 in the register can improve the accuracy of the calculation process, they introduce certain errors to the precision conversion. For example, in some training tasks, this error may cause the results of training tasks using different precision conversion methods to be completely different, and it is impossible to compare or adjust parameters.
[0040] At least one embodiment of the present disclosure provides a precision conversion method, a data processing method, a processor, an electronic device, and a non-transitory computer-readable storage medium. The precision conversion method includes: obtaining an input parameter, wherein the input parameter is a floating point number; performing at least two precision conversions on the input parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an output parameter, wherein the number of mantissa bits of the output parameter is a, and a is a positive integer; wherein, in each precision conversion, the number of mantissa bits of the parameter before the precision conversion is greater than the number of mantissa bits of the parameter after the precision conversion.
[0041] In at least one embodiment, when multiple precision conversions are unavoidable, the precision conversion method performs multiple precision conversions in software in combination with the a+1th bit in the mantissa of the input parameter, and can continue to reuse the original precision conversion operator including multiple precision conversions, so that the results of the multiple precision conversions are the same as the result of a single precision conversion, thereby eliminating errors at the lowest cost, without the need to provide a new precision conversion operator that has no errors with the result of the single precision conversion, thereby reducing R&D costs.
[0042] For example, in the present disclosure, multiple precision conversions are used to convert high-precision input parameters (such as FP32) into low-precision output parameters (such as BF16) through at least two precision conversions, and one precision conversion is used to directly convert high-precision input parameters (such as FP32) into low-precision output parameters (such as BF16). In the precision conversion method provided in at least one embodiment of the present disclosure, the output parameters obtained after multiple precision conversions can be made completely identical to the output parameters obtained after one precision conversion, eliminating errors.
[0043] The embodiments of the present disclosure are described in detail below with reference to the accompanying drawings, but the present disclosure is not limited to these specific embodiments.
[0044] Figure 1 A schematic flowchart of a precision conversion method provided for at least one embodiment of the present disclosure.
[0045] like Figure 1 As shown, the calculation method provided by at least one embodiment of the present disclosure includes steps S10 to S20.
[0046] For example, in step S10, input parameters are obtained.
[0047] For example, the input parameter is a floating point number, for example, the input parameter is a floating point number in a high-precision format, such as a floating point number with more mantissa digits such as FP32.
[0048] For example, the input parameter is a parameter that needs to be converted in precision, such as an input parameter that needs to reduce the number of digits in the mantissa using a rounding rule.
[0049] For example, in step S20, the input parameter is precision-converted at least twice according to the a+1th bit in the mantissa of the input parameter to obtain the output parameter.
[0050] For example, the number of digits in the tail of the output parameter is a, where a is a positive integer.
[0051] For example, in each precision conversion, the number of mantissa digits of the parameter before the precision conversion is greater than the number of mantissa digits of the parameter after the precision conversion, that is, the precision of the parameter is reduced in each precision conversion.
[0052] For example, at least two precision conversions include a first precision conversion and a second precision conversion. Of course, the present disclosure is not limited thereto, and more precision conversions can also be implemented in a manner similar to the two precision conversions, which will not be described in detail here.
[0053] For example, step S20 may include: performing a first precision conversion on the input parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an intermediate parameter, wherein the number of mantissa digits of the intermediate parameter is less than the number of mantissa digits of the input parameter but greater than a; performing a second precision conversion on the intermediate parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an output parameter.
[0054] For example, in some embodiments, performing a first precision conversion on the input parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an intermediate parameter may include: in response to the a+1th bit being a first value: setting the b+1th bit in the mantissa part of the input parameter to the first value to obtain a first intermediate parameter, and rounding the first intermediate parameter in combination with a first rounding rule to obtain the intermediate parameter; wherein b is the number of mantissa digits of the intermediate parameter, b is a positive integer and is greater than a, and the first rounding rule is a predetermined digital rounding rule for converting the precision format of the input parameter to the precision format of the intermediate parameter.
[0055] For example, in some embodiments, according to the a+1th bit in the mantissa part of the input parameter, performing a first precision conversion on the input parameter to obtain an intermediate parameter may also include: in response to the a+1th bit being the second value and the a+2th to cth bits in the mantissa part of the input parameter are not all the first value: setting the b+1th bit in the mantissa part of the input parameter to the first value, and setting at least one of the a+2th to bth bits in the mantissa part of the input parameter to the second value to obtain a second intermediate parameter, and rounding the second intermediate parameter in combination with the first rounding rule to obtain the intermediate parameter; in response to the a+1th bit being the second value and the a+2th to cth bits in the mantissa part of the input parameter are all the first value: rounding the input parameter in combination with the first rounding rule to obtain the intermediate parameter; wherein b is the number of mantissa digits of the intermediate parameter, c is the number of mantissa digits of the input parameter, b is a positive integer greater than a but less than c, and the first rounding rule is a pre-defined digital rounding rule for converting the precision format of the input parameter into the precision format of the intermediate parameter.
[0056] For example, the first rounding rule indicates that in response to the b+1th bit in the mantissa part of the input parameter being the first value, the 1st to bth bits in the mantissa part of the input parameter are directly used as the mantissa part of the intermediate parameter.
[0057] In the present disclosure, the number of mantissa digits of an input parameter is, for example, c, where c is a positive integer and greater than b. The first bit in the mantissa part refers to the highest bit, and the cth bit refers to the lowest bit. The same is true for the mantissa parts of other parameters.
[0058] For example, the first rounding rule stipulates that in various numerical situations, the precision format of the input parameter (with c digits in the mantissa) is rounded to the precision format of the intermediate parameter (with b digits in the mantissa).
[0059] For example, the first value may be 0 and the second value may be 1.
[0060] For example, the first rounding rule may specify that when the b+1th bit in the mantissa part of the input parameter is 0, the b+1th bit to the cth bit in the mantissa part of the input parameter are discarded, and the 1st bit to the bth bit are directly used as the mantissa part of the intermediate parameter. For example, the first rounding rule may also specify the rounding rule when the b+1th bit in the mantissa part of the input parameter is 1, for example, when the b+1th bit in the mantissa part of the input parameter is 1 and the b+2th bit to the cth bit are both 0, the 1st bit to the bth bit are directly used as the mantissa part of the intermediate parameter.
[0061] The first rounding rule can be set and adjusted as needed, and the present disclosure does not impose specific restrictions on this. For example, the first rounding rule can also set different rounding operations as needed, and the present disclosure does not impose specific restrictions on this. According to the different rounding operations specified, the specific process of precision conversion in combination with the first rounding rule can be adaptively adjusted with reference to the above embodiment, and will not be repeated here one by one.
[0062] For example, in the first precision conversion, the b+1th bit in the mantissa part of the input parameter is adjusted according to the value of the a+1th bit in the mantissa part of the input parameter to obtain an intermediate parameter.
[0063] For example, when the a+1th bit in the mantissa of the input parameter is 0, the b+1th bit in the mantissa of the input parameter is set to 0 to obtain a first intermediate parameter, and then the first intermediate parameter is rounded according to the first rounding rule, that is, the 1st to bth bits of the first intermediate parameter are retained as the intermediate parameters.
[0064] For example, when the a+1th bit in the mantissa of the input parameter is 1, if the a+2th to cth bits in the mantissa of the input parameter are not all 0 at this time, the b+1th bit in the mantissa of the input parameter is set to 0, and at least one bit from the a+2th to the bth bits in the mantissa of the input parameter is set to 1 to obtain a second intermediate parameter, and then the second intermediate parameter is rounded in combination with the first rounding rule, that is, the 1st to the bth bits of the first intermediate parameter are retained as the intermediate parameter.
[0065] For example, when the a+1th bit in the mantissa of the input parameter is 1, if the a+2th bit to the cth bit in the mantissa of the input parameter are all 0, the input parameter is directly rounded according to the first rounding rule. For example, since the b+1th bit of the input parameter is 0 (here, b+1 is greater than or equal to a+2), the 1st bit to the bth bit of the input parameter are retained as intermediate parameters.
[0066] The number of mantissa digits of the intermediate parameter is greater than that of the output parameter, so directly discarding the b+1th to the lowest bit in the mantissa part of the output parameter will not cause errors in the conversion. The first rounding rule stipulates that when the b+1th bit in the mantissa part of the input parameter is 0, the 1st to the bth bit in the mantissa part of the input parameter is directly used as the mantissa part of the intermediate parameter, that is, the b+1th to the cth bit are discarded. Therefore, after setting the b+1th bit in the mantissa part of the input parameter to 0, the rounding operation can be performed in combination with the first rounding rule, retaining the 1st to the bth bit in the mantissa part of the first intermediate parameter or the second intermediate parameter as the mantissa part of the intermediate parameter, and using the exponent of the input parameter as the exponent of the intermediate parameter. For example, after adjusting some data bits in the mantissa part of the input parameter, the rounding operation in the first precision conversion can be directly performed using the existing precision conversion operator.
[0067] For example, operators in network models (such as neural networks) generally refer to the basic mathematical operations or operations used in the network layers of the model. These operators are used to build the various layers and components of the network to achieve data transmission, conversion and calculation. They are the basic building blocks of the network model, defining the structure and operation flow of the model, including input, output and intermediate calculations. In the model, the connection relationship between operators forms a directed graph, which reflects the calculation order of different operations in the model. By combining these operators, complex and powerful neural network models can be constructed to handle various complex tasks and data. A precision conversion operator can be an operator used to perform precision conversion operations according to certain rounding rules. The precision conversion operator can have its own input and output tensors and may have some adjustable parameters to control its behavior.
[0068] After obtaining the intermediate parameters, a second precision conversion is performed on the intermediate parameters.
[0069] For example, in some embodiments, performing a second precision conversion on the intermediate parameter according to the a+1th bit in the mantissa of the input parameter to obtain the output parameter may include: in combination with the second rounding rule, performing a rounding operation on the intermediate parameter according to the a+1th bit in the mantissa of the input parameter to obtain the output parameter, wherein the second rounding rule is a predetermined digital rounding rule for converting the precision format of the intermediate parameter into the precision format of the output parameter. For example, in some embodiments, in combination with the second rounding rule, rounding the intermediate parameter according to the a+1th bit in the mantissa part of the input parameter to obtain the output parameter may include: in response to the a+1th bit in the mantissa part of the intermediate parameter being the first value, directly using the 1st to ath bits in the mantissa part of the intermediate parameter as the mantissa part of the output parameter; in response to the a+1th bit in the mantissa part of the intermediate parameter being the second value and at least one bit from the a+1th to bth bits in the mantissa part of the intermediate parameter being the second value, discarding the a+1th to bth bits in the mantissa part of the intermediate parameter, and performing a carry operation on the 1st to ath bits in the mantissa part of the intermediate parameter to obtain the mantissa part of the output parameter; in response to the a+1th bit in the mantissa part of the intermediate parameter being the second value and the a+2th to bth bits in the mantissa part of the intermediate parameter being the first value, determining the mantissa part of the output parameter according to the ath bit in the mantissa part of the intermediate parameter.
[0070] For example, in some embodiments, determining the mantissa part of the output parameter based on the ath bit in the mantissa part of the intermediate parameter may include: in response to the ath bit in the mantissa part of the intermediate parameter being a first value, determining the 1st to ath bits in the mantissa part of the intermediate parameter directly as the mantissa part of the output parameter; in response to the ath bit in the mantissa part of the intermediate parameter being a second value, discarding the a+1th to bth bits in the mantissa part of the intermediate parameter, and performing a carry operation on the 1st to ath bits in the mantissa part of the intermediate parameter to obtain the mantissa part of the output parameter.
[0071] For example, the second rounding rule may specify that, when the a+1th bit in the mantissa part of the intermediate parameter is 0, the a+1th bit to the bth bit in the mantissa part of the intermediate parameter are discarded, and the 1st bit to the ath bit are directly used as the mantissa part of the output parameter; when the a+1th bit in the mantissa part of the intermediate parameter is 1, if any of the a+2th bit to the bth bit in the mantissa part of the intermediate parameter is 1, a carry operation is performed on the 1st bit to the ath bit in the mantissa part of the intermediate parameter, that is, 1 is added to the ath bit to obtain the mantissa part of the output parameter. If the a+2th to bth bits in the mantissa part of the intermediate parameter are all 0 and the ath bit in the mantissa part of the intermediate parameter is 0, then the a+1th to bth bits in the mantissa part of the intermediate parameter are discarded, and the 1st to ath bits are directly used as the mantissa part of the output parameter; if the a+2th to bth bits in the mantissa part of the intermediate parameter are all 0 and the ath bit in the mantissa part of the intermediate parameter is 1, then a carry operation is performed on the 1st to ath bits in the mantissa part of the intermediate parameter, that is, 1 is added to the ath bit to obtain the mantissa part of the output parameter.
[0072] Of course, the second rounding rule can also set different rounding operations as needed, and the present disclosure does not make specific restrictions on this. According to the different rounding operations specified, the specific process of precision conversion combined with the second rounding rule can refer to the above embodiment for adaptive adjustment, which will not be repeated here.
[0073] Since the values of the data bits that may cause precision conversion errors have been set in the first precision conversion, the rounding operation can be directly performed in combination with the second rounding rule in the second precision conversion.
[0074] For example, as mentioned above, when the a+1th bit in the mantissa of the intermediate parameter is 0, the 1st to ath bits are retained as the mantissa of the output parameter, and when the a+1th bit in the mantissa of the intermediate parameter is 1, if any of the a+2th to bth bits in the mantissa of the intermediate parameter is 1 at this time, a carry operation is performed to obtain the mantissa of the output parameter, and if the a+2th to bth bits in the mantissa of the intermediate parameter are all 0 at this time, then a corresponding rounding operation is performed according to the ath bit in the mantissa of the intermediate parameter to obtain the mantissa of the output parameter. In addition, the exponent of the intermediate parameter is used as the exponent of the output parameter. For example, the rounding operation in the second precision conversion can be directly performed using the existing precision conversion operator.
[0075] For example, in at least one embodiment of the present disclosure, the output parameter is the same as the reference output parameter obtained by performing a precision conversion on the input parameter according to the third rounding rule, that is, there is no error between the output parameter obtained by the precision conversion method provided by at least one embodiment of the present disclosure and the reference output parameter obtained by a precision conversion. For example, the number of digits in the mantissa of the reference output parameter is a, and the third rounding rule is a digital rounding rule that directly converts the precision format of the input parameter into the precision format of the output parameter.
[0076] For example, the third rounding rule stipulates that when the a+1th bit in the mantissa part of the input parameter is a first value, such as 0, the 1st to ath bits are retained as the mantissa part of the output parameter; when the a+1th bit in the mantissa part of the input parameter is a second value, such as 1, if the a+2th to cth bits in the mantissa part of the input parameter are not all 0 at this time, a carry operation is performed to obtain the mantissa part of the output parameter, if the a+2th to cth bits in the mantissa part of the input parameter are all 0 and the ath bit is also 0, then the 1st to ath bits are retained as the mantissa part of the output parameter, if the a+2th to cth bits in the mantissa part of the input parameter are all 0 and the ath bit is 1 at this time, a carry operation is performed to obtain the mantissa part of the output parameter.
[0077] For example, when the a+1th bit in the mantissa of the input parameter is 0, the rounding operation performed according to the third rounding rule is to directly retain the 1st to the ath bits. In the precision conversion method provided in at least one embodiment of the present disclosure, the first precision conversion sets the b+1th bit to 0, the first precision conversion will directly retain the 1st to the bth bits, and the second precision conversion will directly retain the 1st to the ath bits. Therefore, the output parameter after multiple precision conversions are performed on the input parameter according to the precision conversion method provided in at least one embodiment of the present disclosure and the reference output parameter obtained by performing one precision conversion on the input parameter according to the third rounding rule are exactly the same.
[0078] For example, when the a+1th bit in the mantissa of the input parameter is 1 and the a+2th to cth bits are not all 0, the rounding operation performed according to the third rounding rule is to retain the 1st to ath bits and add 1 to the ath bit. In the precision conversion method provided in at least one embodiment of the present disclosure, the first precision conversion sets the b+1th bit to 0 and sets at least one of the a+2th to bth bits in the mantissa of the input parameter to 1, so that the first precision conversion directly retains the 1st to bth bits. Since the a+2th to bth bits are not all 0, the second precision conversion retains the 1st to ath bits and adds 1 to the ath bit. Therefore, the output parameter obtained according to the precision conversion method provided in at least one embodiment of the present disclosure is exactly the same as the reference output parameter.
[0079] For example, when the a+1th bit in the mantissa of the input parameter is 1 and the a+2th bit to the cth bit are all 0, the rounding operation performed according to the third rounding rule is to retain the 1st bit to the ath bit, and determine whether to perform a carry operation based on the ath bit. In the precision conversion method provided in at least one embodiment of the present disclosure, the first precision conversion directly retains the 1st bit to the bth bit as an intermediate parameter. Since the a+2th bit to the bth bit are all 0, the second precision conversion will retain the 1st bit to the ath bit, and determine whether to perform a carry operation based on the ath bit. Therefore, the output parameter obtained according to the precision conversion method provided in at least one embodiment of the present disclosure is exactly the same as the reference output parameter.
[0080] Therefore, in the precision conversion method provided in at least one embodiment of the present disclosure, individual data bits in the mantissa part of the original high-precision data can be adjusted according to rounding rules, and the original precision conversion operator including multiple precision conversions can continue to be reused, so that the results of multiple precision conversions are the same as the results of one precision conversion, thereby eliminating errors at the lowest cost, without providing a new precision conversion operator that has no errors with the result of one precision conversion, thereby reducing R&D costs.
[0081] Of course, the precision conversion operator or precision conversion method provided by at least one embodiment of the present disclosure can also be used to convert low-precision input parameters into high-precision output parameters, for example, by filling the low bits of the mantissa of the low-precision input parameter with 0 until the number of mantissa bits is expanded to the number of mantissa bits specified by the output parameter.
[0082] Two specific examples of performing precision conversion using the precision conversion method provided by at least one embodiment of the present disclosure are described in detail below.
[0083] For example, in one embodiment, the precision type of the input parameter obtained is FP32, as shown in Table 1, and the number of mantissa bits is 23. In the first precision conversion, FP32 is converted to BF20, as shown in Table 1, and the number of mantissa bits is 11. In the second precision conversion, BF20 is converted to BF16, as shown in Table 1, and the number of mantissa bits is 7.
[0084] Table 2 shows the third rounding rule for converting FP32 directly to BF16.
[0085] Table 2 Third rounding rule
[0086]
[0087] As shown in Table 2, the third rounding rule stipulates that if the 8th bit in the mantissa part of FP32 is 0, the first 7 bits of the mantissa are directly retained as the mantissa part of BF16 after precision conversion; if the 8th bit in the mantissa part of FP32 is 1 and the 9th to 23rd bits are not all 0, the first 7 bits of the mantissa are retained and the 7th bit is added by 1 as the mantissa part of BF16 after precision conversion; if the 8th bit in the mantissa part of FP32 is 1 and the 9th to 23rd bits are all 0, when the 7th bit is 0, 1 to 7 bits are directly retained as the mantissa part of BF16 after precision conversion, and when the 7th bit is 1, the first 7 bits are retained and the 7th bit is added by 1 as the mantissa part of BF16 after precision conversion.
[0088] Table 3 shows specific operations in the multiple precision conversion process provided by at least one embodiment of the present disclosure.
[0089] Table 3 Conversion operations for multiple precision conversions
[0090]
[0091] For example, as shown in Table 3, in the first precision conversion, if the 8th bit in the mantissa part of the input parameter is 0, the 12th bit in the mantissa part of the input parameter is set to 0 to obtain the first intermediate parameter; then, combined with the first rounding rule, since the 12th bit in the mantissa part of the first intermediate parameter is 0, the 12th to 23rd bits in the mantissa part of the first intermediate parameter are discarded, and the first 11 bits in the mantissa part of the first intermediate parameter are used as the intermediate parameter with a precision format of BF20.
[0092] In the second precision conversion, since the eighth bit of the intermediate parameter is 0, the first seven bits of the intermediate parameter are used as the mantissa of the output parameter.
[0093] Referring to Table 2, the above two precision conversion operations are consistent with the precision conversion operation of Case 1 in Table 2, so the output parameters and the reference output parameters can obtain completely consistent results.
[0094] For example, as shown in Table 3, in the first precision conversion, if the 8th bit in the mantissa part of the input parameter is 1 and the 9th to 23rd bits of the input parameter are not all 0, the 12th bit in the mantissa part of the input parameter is set to 0, and any bit from the 9th to the 11th bits in the mantissa part of the input parameter is set to 1, to obtain the second intermediate parameter; then, in combination with the first rounding rule, since the 12th bit in the mantissa part of the second intermediate parameter is 0, the 12th to the 23rd bits in the mantissa part are discarded, and the first 11 bits in the mantissa part of the second intermediate parameter are used as the intermediate parameter with a precision format of BF20.
[0095] In the second precision conversion, since the 8th bit in the mantissa of the intermediate parameter is 1 and the 9th to 11th bits are not all 0, the first 7 bits of the mantissa of the intermediate parameter are retained and a carry operation is performed (1 is added to the 7th bit) to obtain the mantissa of the output parameter.
[0096] Referring to Table 2, the above two precision conversion operations are consistent with the precision conversion operation of Case 2 in Table 2, so the output parameters and the reference output parameters can obtain completely consistent results.
[0097] For example, as shown in Table 3, in the first precision conversion, if the 8th bit in the mantissa part of the input parameter is 1 and the 9th to 23rd bits of the input parameter are all 0, combined with the first rounding rule, since the 12th bit in the mantissa part of the first intermediate parameter is 0, the 12th to 23rd bits in the mantissa part are discarded, and the first 11 bits in the mantissa part of the intermediate parameter are used as the intermediate parameter with a precision format of BF20.
[0098] In the second precision conversion, since bits 9 to 11 are all 0, the specific rounding operation is determined based on bit 7.
[0099] For example, if the 7th bit is 0, the first 7 bits of the intermediate parameter are used as the mantissa of the output parameter. Referring to Table 2, the above two precision conversion operations are consistent with the precision conversion operation of Case 3 in Table 2, so the output parameter and the reference output parameter can obtain completely consistent results.
[0100] For example, if the 7th bit is 1, the first 7 bits of the mantissa of the intermediate parameter are retained and a carry operation is performed (add 1 to the 7th bit) to obtain the mantissa of the output parameter. Referring to Table 2, the above two precision conversion operations are consistent with the precision conversion operation of Case 4 in Table 2, so the output parameter and the reference output parameter can obtain completely consistent results.
[0101] For example, in another embodiment, the precision type of the input parameter obtained is FP32, as shown in Table 1, and the number of mantissa bits is 23. In the first precision conversion, FP32 is converted to BF24, as shown in Table 1, and the number of mantissa bits is 15. In the second precision conversion, BF24 is converted to BF19, as shown in Table 1, and the number of mantissa bits is 10.
[0102] Table 4 shows the third rounding rule for converting FP32 directly to BF19.
[0103] Table 4 Third rounding rule
[0104]
[0105] As shown in Table 4, the third rounding rule stipulates that if the 11th bit in the mantissa part of FP32 is 0, the first 10 bits of the mantissa part are directly retained as the mantissa part of BF19 after precision conversion; if the 11th bit in the mantissa part of FP32 is 1 and the 12th to 23rd bits are not all 0, the first 10 bits of the mantissa part are retained and the 10th bit is added by 1 as the mantissa part of BF19 after precision conversion; if the 11th bit in the mantissa part of FP32 is 1 and the 12th to 23rd bits are all 0, when the 10th bit is 0, 1 to 10 bits are directly retained as the mantissa part of BF19 after precision conversion, and when the 10th bit is 1, the first 10 bits are retained and the 10th bit is added by 1 as the mantissa part of BF19 after precision conversion.
[0106] Table 5 shows specific operations in the multiple precision conversion process provided by at least one embodiment of the present disclosure.
[0107] Table 5 Conversion operations for multiple precision conversions
[0108]
[0109] For example, as shown in Table 5, in the first precision conversion, if the 11th bit in the mantissa part of the input parameter is 0, the 16th bit in the mantissa part of the input parameter is set to 0 to obtain the first intermediate parameter; then, combined with the first rounding rule, since the 16th bit in the mantissa part of the first intermediate parameter is 0, the 16th to 23rd bits in the mantissa part of the first intermediate parameter are discarded, and the first 15 bits in the mantissa part of the first intermediate parameter are used as the intermediate parameter with a precision format of BF24.
[0110] In the second precision conversion, since the 11th bit of the intermediate parameter is 0, the first 10 bits of the intermediate parameter are used as the mantissa of the output parameter.
[0111] Referring to Table 4, the above two precision conversion operations are consistent with the precision conversion operation of Case 1 in Table 4, so the output parameters and the reference output parameters can obtain completely consistent results.
[0112] For example, as shown in Table 5, in the first precision conversion, if the 11th bit in the mantissa part of the input parameter is 1 and the 12th to 23rd bits of the input parameter are not all 0, the 16th bit in the mantissa part of the input parameter is set to 0, and any bit from the 12th to the 15th bits in the mantissa part of the input parameter is set to 1 (for example, the 12th bit is set to 1), to obtain the second intermediate parameter; then, combined with the first rounding rule, since the 16th bit in the mantissa part of the second intermediate parameter is 0, the 16th to the 23rd bits in the mantissa part are discarded, and the first 15 bits in the mantissa part of the second intermediate parameter are used as the intermediate parameter with a precision format of BF24.
[0113] In the second precision conversion, since the 11th bit in the mantissa of the intermediate parameter is 1 and the 12th to 15th bits are not all 0, the first 10 bits of the mantissa of the intermediate parameter are retained and a carry operation is performed (add 1 to the 10th bit) to obtain the mantissa of the output parameter.
[0114] Referring to Table 4, the above two precision conversion operations are consistent with the precision conversion operation of Case 2 in Table 4, so the output parameters and the reference output parameters can obtain completely consistent results.
[0115] For example, as shown in Table 5, in the first precision conversion, if the 11th bit in the mantissa part of the input parameter is 1 and the 12th to 23rd bits of the input parameter are all 0, combined with the first rounding rule, since the 16th bit in the mantissa part of the first intermediate parameter is 0, the 16th to 23rd bits in the mantissa part are discarded, and the first 15 bits in the mantissa part of the intermediate parameter are used as the intermediate parameter with a precision format of BF24.
[0116] In the second precision conversion, since bits 12 to 15 are all 0, the specific rounding operation is determined based on bit 10.
[0117] For example, if the 10th bit is 0, the first 10 bits of the intermediate parameter are used as the mantissa of the output parameter. Referring to Table 4, the above two precision conversion operations are consistent with the precision conversion operation of Case 3 in Table 4, so the output parameter and the reference output parameter can obtain completely consistent results.
[0118] For example, if the 10th bit is 1, the first 10 bits of the mantissa of the intermediate parameter are retained and a carry operation is performed (add 1 to the 10th bit) to obtain the mantissa of the output parameter. Referring to Table 4, the above two precision conversion operations are consistent with the precision conversion operation of Case 4 in Table 4, so the output parameter and the reference output parameter can obtain completely consistent results.
[0119] In the above embodiment, according to the a+1th bit in the mantissa of the input parameter, individual data bits in the mantissa of the original high-precision data are adjusted, and then the original precision conversion operator can be used to continue to perform multiple precision conversions, so that the original precision conversion operator including multiple precision conversions can continue to be reused, so that the results of the multiple precision conversions are the same as the result of the single precision conversion, thereby eliminating errors at the lowest cost, without providing a new precision conversion operator that has no errors with the result of the single precision conversion, thereby reducing R&D costs.
[0120] At least one embodiment of the present disclosure further provides a precision conversion device. Figure 2 A schematic block diagram of a precision conversion device provided for at least one embodiment of the present disclosure.
[0121] like Figure 2 As shown, the precision conversion device 100 includes an acquisition module 101 and a precision conversion module 102 .
[0122] For example, the acquisition module 101 is configured to acquire an input parameter. For example, the input parameter is a floating point number.
[0123] For example, the precision conversion module 102 is configured to perform at least two precision conversions on the input parameter according to the a+1th digit in the mantissa of the input parameter to obtain an output parameter, wherein the number of mantissa digits of the output parameter is a, and a is a positive integer.
[0124] For example, in each precision conversion, the number of mantissa digits of the parameter before the precision conversion is greater than the number of mantissa digits of the parameter after the precision conversion.
[0125] For example, the output parameters can be directly output from the precision conversion device 100 and transmitted to other components that need to use the output parameters, such as storage devices or other computing devices.
[0126] For example, the acquisition module 101 and the precision conversion module 102 include codes and programs stored in the memory. The acquisition module 101 and the precision conversion module 102 are implemented as a central processing unit (CPU) or other forms of processing units with data processing capabilities and / or instruction execution capabilities. The processing unit can be a general-purpose processor, and can also be a single-chip microcomputer, a microprocessor, a digital signal processor, a dedicated image processing chip, or a field programmable logic array, etc. The acquisition module 101 and the precision conversion module 102 execute the code and program to implement some or all of the functions of the acquisition module 101 and the precision conversion module 102 as described above. For example, the acquisition module 101 and the precision conversion module 102 can be a circuit board or a combination of multiple circuit boards for implementing the functions as described above. In an embodiment of the present application, the circuit board or the combination of multiple circuit boards may include: (1) one or more processors; (2) one or more non-temporary memories connected to the processor; and (3) firmware stored in the memory that can be executed by the processor.
[0127] It should be noted that the acquisition module 101 can be used to implement Figure 1 In step S10 shown in FIG. 1 , the precision conversion module 102 may be used to implement Figure 1 Step S20 shown. Therefore, for the specific description of the functions that can be achieved by the acquisition module 101 and the precision conversion module 102, reference can be made to the relevant descriptions of steps S10 to S20 in the embodiment of the above-mentioned precision conversion method, and the repeated parts will not be repeated. In addition, the precision conversion device 100 can achieve technical effects similar to those of the above-mentioned precision conversion method, which will not be repeated here.
[0128] It should be noted that in at least one embodiment of the present disclosure, the precision conversion device 100 may include more or fewer circuits or units, and the connection relationship between the various circuits or units is not limited and can be determined according to actual needs. The specific configuration of each circuit or unit is not limited and can be composed of analog devices according to circuit principles, or can be composed of digital chips, or in other applicable ways.
[0129] For example, in some embodiments, the precision conversion device 100 may be a precision conversion operator, which is an operation or function that changes the precision of a value during data processing or calculation. The precision conversion operator may be used to convert a value from one precision to another precision to meet different computing requirements or display requirements. For example, the precision conversion device 100 may be implemented in hardware, software, or a combination of hardware and software, and the present disclosure does not specifically limit this.
[0130] At least one embodiment of the present disclosure further provides a data processing method. Figure 3A schematic flowchart of a data processing method provided for at least one embodiment of the present disclosure.
[0131] like Figure 3 As shown, the data processing method at least includes steps S30-S40.
[0132] In step S30, a precision conversion instruction is received. For example, the precision conversion instruction includes input parameters.
[0133] In step S40, after parsing the precision conversion instruction, the precision conversion instruction is executed using a precision conversion unit.
[0134] For example, the precision conversion unit is configured to perform at least two precision conversions.
[0135] For example, using a precision conversion unit to execute the precision conversion instruction in step S40 may include: performing at least two precision conversions on the input parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an output parameter, wherein the number of mantissa bits of the output parameter is a, wherein a is a positive integer, and in each precision conversion, the number of mantissa bits of the parameter before the precision conversion is greater than the number of mantissa bits of the parameter after the precision conversion.
[0136] For example, the data processing method provided by at least one embodiment of the present disclosure can be applied to Figure 4 Processor shown.
[0137] For example, in the data processing method provided by at least one embodiment of the present disclosure, a precision conversion instruction is provided, and the precision conversion instruction includes input parameters. For example, after receiving the precision conversion instruction, the processor parses the precision conversion instruction, such as decoding the precision conversion instruction, generating a microinstruction and sending the microinstruction to an instruction allocation unit; the instruction allocation unit sends the microinstruction to a corresponding scheduling queue according to the microinstruction category; in response to the microinstruction, when the input parameters are prepared, the precision conversion unit reads the input parameters and executes the relevant operations of the precision conversion instruction.
[0138] Regarding the specific process of using the precision conversion unit to execute the precision conversion instruction, reference may be made to steps S10-S20 in the precision conversion method described above, and the repeated parts will not be repeated here.
[0139] The data processing method provided by at least one embodiment of the present disclosure can achieve a technical effect similar to the accuracy conversion method described above, and will not be described in detail here.
[0140] Figure 4 A schematic structural diagram of a processor provided in at least one embodiment of the present disclosure. Figure 4 As shown, the processor 200 includes an instruction parsing unit 201 and a precision conversion unit 202 .
[0141] For example, the instruction parsing unit 201 is used to receive and parse a precision conversion instruction, wherein the precision conversion instruction includes an input parameter.
[0142] For example, after the instruction parsing unit parses the precision conversion instruction, the precision conversion unit 202 executes the precision conversion method according to any embodiment of the present disclosure.
[0143] Specifically, when the upper-level software based on the processor (for example, AI applications, HPC applications, and scientific computing applications, etc.) can send precision conversion instructions for computing processing to the processor (such as CPU or GPU) through a uniformly encapsulated function library, the precision conversion instruction can carry input parameters; when the processor receives the precision conversion instruction, the instruction parsing unit 201 parses the precision conversion instruction to obtain the input parameters, and the processor schedules the precision conversion unit to perform the precision conversion task for the output parameters. For example, after parsing the precision conversion instruction, the processor can store the input parameters in the precision conversion instruction into a register or memory, so that when the precision conversion unit 202 performs computing processing, it can obtain the input parameters from the register or memory.
[0144] Regarding the specific process of using the precision conversion unit 202 to execute the precision conversion instruction, reference may be made to steps S10-S20 in the precision conversion method described above, and the repeated parts will not be repeated.
[0145] The precision conversion method, precision conversion device or data processing method provided by at least one embodiment of the present disclosure can be applied to different systems or devices, such as Figure 5 The electronic device 300 shown. The electronic device 300 can be a terminal, such as a mobile phone terminal, a tablet computer, a laptop computer, an AR device, a VR device, a vehicle terminal, etc., and can also be a server, etc. The precision conversion method provided in at least one embodiment of the present disclosure can be applied to the electronic device 300 involving CPU, high performance computing (High Performance Computing, HPC for short) and artificial intelligence (Artificial Intelligence, AI) and other scenarios related to precision conversion, such as scalar calculation units, vector calculation units, matrix calculation units and tensor calculation units. Of course, the present disclosure is not limited to this, and any scenario, device, apparatus, etc. involving floating-point precision conversion can adopt the precision conversion method or precision conversion apparatus provided by at least one embodiment of the present disclosure.
[0146] In some embodiments, the precision conversion device provided in at least one embodiment of the present disclosure may be a chip, for example, the chip is a system-on-a-chip (SoC). The system-on-a-chip includes a processor, which may be a single-core processor or a multi-core processor, a memory, and an I / O interface. After the processor loads the data and application in the memory, it processes the data, for example, performs calculations involving precision conversion.
[0147] For example, refer to Figure 5 The electronic device 300 shown, when implementing the precision conversion method provided by at least one embodiment of the present disclosure, the processing device 301 performs various appropriate actions and processes according to the non-temporary computer-readable instructions stored in the memory to implement the precision conversion method. For example, the input parameters are stored in a memory, such as a register, a cache, or a memory. When precision conversion is required, the input parameters are read from the memory, and the processing device 301 performs at least two precision conversions on the input parameters according to step S20 in the precision conversion method described in at least one embodiment of the present disclosure to obtain output parameters. The output parameters can be transmitted again to the corresponding operator, etc. for use, or transmitted to a memory (such as a high-bandwidth memory) for storage.
[0148] In addition, it should be noted that the parameter type of the precision conversion method or precision conversion device provided in at least one embodiment of the present disclosure is a floating point number, and the parameter type may have different specific physical meanings according to different application scenarios. For example, the precision conversion method provided in at least one embodiment of the present disclosure may be applied in the fields of speech processing, image processing, text processing, video processing, etc.
[0149] For example, in the field of speech processing, parameters can be any parameters used, input, or generated in tasks such as feature extraction, speech enhancement, and speech recognition, such as speech feature vectors, filtering parameters, and the like.
[0150] For example, in the field of image processing, parameters can be any parameters used, input, or generated in tasks such as image preprocessing, feature extraction, image segmentation, and target detection, such as image feature vectors, various edge detection operators (such as Sobel operator, Canny operator, Prewitt operator, etc.), image filtering operators (such as Gaussian filtering, median filtering, bilateral filtering, etc.), morphological operators (such as corrosion, expansion, opening operation, closing operation, etc.), etc.
[0151] For example, in the field of text processing, parameters can be any parameters used, input, or generated in tasks such as text classification, sentiment analysis, and text generation, such as the semantic feature vector of the text.
[0152] For example, in the field of video processing, the parameters may be parameters in the field of image processing as described above, or parameters used, inputted, or generated in the field of video processing, such as an optical flow operator (used to estimate motion between video frames) or a target tracking operator (used to track a specific target in a video).
[0153] Of course, the present disclosure is not limited to this. For other application scenarios or fields, as long as the precision conversion of floating-point numbers is required, the precision conversion method described in at least one embodiment of the present disclosure can be applied, and will not be described in detail here.
[0154] Figure 5 The following is a schematic block diagram of an electronic device provided by an embodiment of the present disclosure. Figure 5 As shown, the electronic device 300 is suitable for implementing the data processing method or precision conversion method provided by the embodiment of the present disclosure. It should be noted that Figure 5 The components of the electronic device 300 shown are merely exemplary and non-limiting. The electronic device 300 may also have other components according to actual application requirements.
[0155] like Figure 5 As shown, the electronic device 300 may include a processing device (eg, a central processing unit, a graphics processing unit, etc.) 301, which may perform various appropriate actions and processes according to non-transitory computer-readable instructions stored in a memory to implement various functions.
[0156] For example, when the computer-readable instruction is executed by the processing device 301, one or more steps in the data processing method or the precision conversion method described in any of the above embodiments can be executed. It should be noted that the detailed description of the processing process of the precision conversion method can refer to the relevant description in the embodiment of the above precision conversion method, and the detailed description of the processing process of the data processing method can refer to the relevant description in the embodiment of the above data processing method, and the repeated parts will not be repeated.
[0157] For example, the memory may include any combination of one or more computer program products, and the computer program product may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. Volatile memory may include, for example, random access memory (RAM) 303 and / or cache memory (cache), etc. For example, computer-readable instructions may be loaded from storage device 308 into random access memory (RAM) 303 to run computer-readable instructions. Non-volatile memory may include, for example, read-only memory (ROM) 302, hard disk, erasable programmable read-only memory (EPROM), portable compact disk read-only memory (CD-ROM), USB memory, flash memory, etc. Various applications and various data, such as various data used and / or generated by the application, may also be stored in the computer-readable storage medium.
[0158] For example, the processing device 301 , the ROM 302 , and the RAM 303 are connected to one another via a bus 304 . An input / output (I / O) interface 305 is also connected to the bus 304 .
[0159] Typically, the following devices may be connected to the I / O interface 305: an input device 306 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 307 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 308 including, for example, a magnetic tape, a hard disk, a flash memory, etc.; and a communication device 309. The communication device 309 may allow the electronic device 300 to communicate with other electronic devices wirelessly or by wire to exchange data. Although Figure 5 An electronic device 300 having various devices is shown, but it should be understood that it is not required to implement or have all the devices shown, and the electronic device 300 may alternatively implement or have more or fewer devices. For example, the processing device 301 can control other components in the electronic device 300 to perform the desired functions. The processing device 301 can be a device with data processing capabilities and / or program execution capabilities such as a central processing unit (CPU), a tensor processing unit (TPU) or a graphics processing unit GPU. The central processing unit (CPU) can be an X86, ARM, RISC-V architecture, etc. The GPU can be directly integrated into the SOC, directly integrated into the motherboard, or built into the north bridge chip of the motherboard.
[0160] Figure 6 A schematic diagram of a non-transitory computer-readable storage medium provided for at least one embodiment of the present disclosure. Figure 6As shown, the storage medium 400 may be a non-transitory computer-readable storage medium, and one or more computer-readable instructions 401 may be non-transitory stored on the storage medium 400. For example, when the computer-readable instructions 401 are executed by a processor, one or more steps in the precision conversion method described above, or one or more steps in the data processing method may be executed.
[0161] For example, the storage medium 400 may be applied to the electronic device 300 described above. For example, the storage medium 400 may include the storage device 308 in the electronic device 300 .
[0162] For example, the storage device may include any combination of one or more computer program products, and the computer program product may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. Volatile memory may include, for example, random access memory (RAM) and / or cache memory (cache), etc. Non-volatile memory may include, for example, read-only memory (ROM), hard disk, erasable programmable read-only memory (EPROM), portable compact disk read-only memory (CD-ROM), USB memory, flash memory, etc. One or more computer-readable instructions may be stored on the computer-readable storage medium, and the processor may execute the computer-readable instructions to implement various functions of the processor. Various applications and various data may also be stored in the storage medium.
[0163] For example, the storage medium may include a memory card of a smart phone, a storage component of a tablet computer, a hard disk of a personal computer, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), a portable compact disk read-only memory (CD-ROM), flash memory, or any combination of the above storage media, or other applicable storage media.
[0164] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0165] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware, wherein the name of a unit does not, in some cases, limit the unit itself.
[0166] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.
[0167] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above features are replaced with the technical features with similar functions disclosed in the present disclosure (but not limited to) by each other to form a technical solution.
[0168] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.
[0169] Although the subject matter has been described in language specific to structural features and / or methodological logical actions, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. On the contrary, the specific features and actions described above are merely example forms of implementing the claims.
[0170] There are a few points to note about this disclosure:
[0171] (1) The drawings of the embodiments of the present disclosure only relate to the structures related to the embodiments of the present disclosure. Other structures may refer to the general design.
[0172] (2) In the absence of conflict, the embodiments of the present disclosure and the features therein may be combined with each other to obtain new embodiments.
[0173] The above description is only a specific implementation of the present disclosure, but the protection scope of the present disclosure is not limited thereto. The protection scope of the present disclosure shall be based on the protection scope of the claims.
Claims
1. A processor, comprising an instruction parsing unit and a precision conversion unit, wherein: The instruction parsing unit is used to receive and parse the precision conversion instruction, wherein the precision conversion instruction includes input parameters, The precision conversion unit executes a precision conversion method after the instruction parsing unit parses the precision conversion instruction, wherein the precision conversion method is used for a precision conversion operator, and the precision conversion operator performs at least two precision conversions. The precision conversion method comprises: Obtain an input parameter, wherein the input parameter is a floating point number; According to the a+1th digit in the mantissa part of the input parameter, use the precision conversion operator to perform the at least two precision conversions on the input parameter to obtain an output parameter, wherein the number of mantissa digits of the output parameter is a, and a is a positive integer; In each precision conversion, the number of mantissa digits of the parameter before the precision conversion is greater than the number of mantissa digits of the parameter after the precision conversion; The output parameter is the same as a reference output parameter obtained by performing a precision conversion on the input parameter, and the number of mantissa digits of the reference output parameter is a; The at least two precision conversions include a first precision conversion and a second precision conversion. According to the a+1th bit in the mantissa of the input parameter, the input parameter is subjected to at least two precision conversions to obtain an output parameter, including: According to the a+1th bit in the mantissa part of the input parameter, the input parameter is subjected to the first precision conversion to obtain an intermediate parameter, wherein the number of mantissa bits of the intermediate parameter is less than the number of mantissa bits of the input parameter but greater than a; According to the a+1th bit in the mantissa of the input parameter, the second precision conversion is performed on the intermediate parameter to obtain the output parameter.
2. The processor according to claim 1, wherein: According to the a+1th bit in the mantissa of the input parameter, the first precision conversion is performed on the input parameter to obtain an intermediate parameter, including: In response to the a+1th bit being the first value: The b+1th bit in the mantissa of the input parameter is set to the first value to obtain a first intermediate parameter, performing a rounding operation on the first intermediate parameter in combination with a first rounding rule to obtain the intermediate parameter; Among them, b is the number of digits in the mantissa of the intermediate parameter, b is a positive integer and is greater than a, and the first rounding rule is a pre-defined digital rounding rule for converting the precision format of the input parameter into the precision format of the intermediate parameter.
3. The processor according to claim 1, wherein: According to the a+1th bit in the mantissa of the input parameter, the first precision conversion is performed on the input parameter to obtain an intermediate parameter, including: In response to the a+1th bit being the second value and the a+2th to cth bits in the mantissa portion of the input parameter not all being the first value: The b+1th bit in the mantissa part of the input parameter is set to the first value, and at least one bit from the a+2th bit to the bth bit in the mantissa part of the input parameter is set to the second value, to obtain a second intermediate parameter, performing a rounding operation on the second intermediate parameter in combination with the first rounding rule to obtain the intermediate parameter; In response to the a+1th bit being the second value and the a+2th to cth bits in the mantissa portion of the input parameter being the first value: performing a rounding operation on the input parameter in combination with the first rounding rule to obtain the intermediate parameter; Wherein, b is the number of digits in the mantissa of the intermediate parameter, c is the number of digits in the mantissa of the input parameter, b is a positive integer greater than a but less than c, c is a positive integer, and the first rounding rule is a predetermined digital rounding rule for converting the precision format of the input parameter into the precision format of the intermediate parameter.
4. The processor according to claim 2 or 3, wherein: The first rounding rule indicates that in response to the b+1th bit in the mantissa part of the input parameter being the first value, the 1st to bth bits in the mantissa part of the input parameter are directly used as the mantissa part of the intermediate parameter.
5. The processor according to claim 1, wherein: According to the a+1th bit in the mantissa of the input parameter, the second precision conversion is performed on the intermediate parameter to obtain an output parameter, including: In combination with the second rounding rule, the intermediate parameter is rounded according to the a+1th bit in the mantissa of the input parameter to obtain the output parameter, wherein the second rounding rule is a predetermined digital rounding rule for converting the precision format of the intermediate parameter into the precision format of the output parameter.
6. The processor according to claim 5, wherein: In combination with the second rounding rule, the intermediate parameter is rounded according to the a+1th bit in the mantissa of the input parameter to obtain the output parameter, including: In response to the a+1th bit in the mantissa part of the intermediate parameter being a first value, directly using the 1st bit to the ath bit in the mantissa part of the intermediate parameter as the mantissa part of the output parameter; In response to the a+1th bit in the mantissa part of the intermediate parameter being a second value and at least one of the a+2th bit to the bth bit in the mantissa part of the intermediate parameter being the second value, discarding the a+1th bit to the bth bit in the mantissa part of the intermediate parameter, and performing a carry operation on the 1st bit to the ath bit in the mantissa part of the intermediate parameter to obtain the mantissa part of the output parameter; In response to the a+1th bit in the mantissa part of the intermediate parameter being the second value and the a+2th to bth bits in the mantissa part of the intermediate parameter being the first value, the mantissa part of the output parameter is determined according to the ath bit in the mantissa part of the intermediate parameter.
7. The processor according to claim 6, wherein: Determining the mantissa part of the output parameter according to the ath bit in the mantissa part of the intermediate parameter comprises: In response to the ath bit in the mantissa part of the intermediate parameter being the first value, determining the first bit to the ath bit in the mantissa part of the intermediate parameter to be directly used as the mantissa part of the output parameter; In response to the ath bit in the mantissa part of the intermediate parameter being the second value, the a+1th bit to the bth bit in the mantissa part of the intermediate parameter are discarded, and the carry operation is performed on the 1st bit to the ath bit in the mantissa part of the intermediate parameter to obtain the mantissa part of the output parameter.
8. The processor according to any one of claims 1-3, 5-7, wherein: The output parameter is the same as the reference output parameter obtained by performing a precision conversion on the input parameter according to the third rounding rule, The third rounding rule is a digital rounding rule that directly converts the precision format of the input parameter into the precision format of the output parameter.
9. A precision conversion device, used for a precision conversion operator, wherein the precision conversion operator performs at least two precision conversions, the precision conversion device comprising: An acquisition module is configured to acquire an input parameter, wherein the input parameter is a floating point number; A precision conversion module is configured to perform the at least two precision conversions on the input parameter using the precision conversion operator according to the a+1th digit in the mantissa part of the input parameter to obtain an output parameter, wherein the number of mantissa digits of the output parameter is a, and a is a positive integer; In each precision conversion, the number of digits of the mantissa of the parameter before the conversion is greater than the number of digits of the mantissa of the parameter after the precision conversion; The output parameter is the same as a reference output parameter obtained by performing a precision conversion on the input parameter, and the number of mantissa digits of the reference output parameter is a; The at least two precision conversions include a first precision conversion and a second precision conversion. The precision conversion module performs at least two precision conversions on the input parameter according to the a+1th bit in the mantissa part of the input parameter to obtain an output parameter, including performing the following operations: According to the a+1th bit in the mantissa part of the input parameter, the input parameter is subjected to the first precision conversion to obtain an intermediate parameter, wherein the number of mantissa bits of the intermediate parameter is less than the number of mantissa bits of the input parameter but greater than a; According to the a+1th bit in the mantissa of the input parameter, the second precision conversion is performed on the intermediate parameter to obtain the output parameter.
10. A data processing method, comprising: Receiving a precision conversion instruction, wherein the precision conversion instruction includes an input parameter; After parsing the precision conversion instruction, the precision conversion instruction is executed using a precision conversion unit, wherein the precision conversion unit is used to perform at least two precision conversions. Wherein, using the precision conversion unit to execute the precision conversion instruction includes: According to the a+1th bit in the mantissa part of the input parameter, use the precision conversion unit to perform the at least two precision conversions on the input parameter to obtain an output parameter, wherein the number of mantissa bits of the output parameter is a, a is a positive integer, and in each precision conversion, the number of mantissa bits of the parameter before the precision conversion is greater than the number of mantissa bits of the parameter after the precision conversion; The output parameter is the same as a reference output parameter obtained by performing a precision conversion on the input parameter, and the number of mantissa digits of the reference output parameter is a; The at least two precision conversions include a first precision conversion and a second precision conversion. According to the a+1th bit in the mantissa of the input parameter, the input parameter is subjected to at least two precision conversions to obtain an output parameter, including: According to the a+1th bit in the mantissa part of the input parameter, the input parameter is subjected to the first precision conversion to obtain an intermediate parameter, wherein the number of mantissa bits of the intermediate parameter is less than the number of mantissa bits of the input parameter but greater than a; According to the a+1th bit in the mantissa of the input parameter, the second precision conversion is performed on the intermediate parameter to obtain the output parameter.
11. An electronic device, comprising: A memory non-transitorily stores computer executable instructions; a processor configured to execute the computer executable instructions, Wherein, when the computer executable instructions are executed by the processor, a precision conversion method or the data processing method according to claim 10 is implemented, The precision conversion method is used for a precision conversion operator, and the precision conversion operator performs at least two precision conversions. The precision conversion method comprises: Obtain an input parameter, wherein the input parameter is a floating point number; According to the a+1th digit in the mantissa part of the input parameter, use the precision conversion operator to perform the at least two precision conversions on the input parameter to obtain an output parameter, wherein the number of mantissa digits of the output parameter is a, and a is a positive integer; In each precision conversion, the number of mantissa digits of the parameter before the precision conversion is greater than the number of mantissa digits of the parameter after the precision conversion; The output parameter is the same as a reference output parameter obtained by performing a precision conversion on the input parameter, and the number of mantissa digits of the reference output parameter is a; The at least two precision conversions include a first precision conversion and a second precision conversion. According to the a+1th bit in the mantissa of the input parameter, the input parameter is subjected to at least two precision conversions to obtain an output parameter, including: According to the a+1th bit in the mantissa part of the input parameter, the input parameter is subjected to the first precision conversion to obtain an intermediate parameter, wherein the number of mantissa bits of the intermediate parameter is less than the number of mantissa bits of the input parameter but greater than a; According to the a+1th bit in the mantissa of the input parameter, the second precision conversion is performed on the intermediate parameter to obtain the output parameter.
12. A non-transitory computer-readable storage medium, wherein: The non-transitory computer-readable storage medium stores computer-executable instructions, When the computer executable instructions are executed by the processor, a precision conversion method or the data processing method according to claim 10 is implemented. The precision conversion method is used for a precision conversion operator, and the precision conversion operator performs at least two precision conversions. The precision conversion method comprises: Obtain an input parameter, wherein the input parameter is a floating point number; According to the a+1th digit in the mantissa part of the input parameter, use the precision conversion operator to perform the at least two precision conversions on the input parameter to obtain an output parameter, wherein the number of mantissa digits of the output parameter is a, and a is a positive integer; In each precision conversion, the number of mantissa digits of the parameter before the precision conversion is greater than the number of mantissa digits of the parameter after the precision conversion; The output parameter is the same as a reference output parameter obtained by performing a precision conversion on the input parameter, and the number of mantissa digits of the reference output parameter is a; The at least two precision conversions include a first precision conversion and a second precision conversion. According to the a+1th bit in the mantissa of the input parameter, the input parameter is subjected to at least two precision conversions to obtain an output parameter, including: According to the a+1th bit in the mantissa part of the input parameter, the input parameter is subjected to the first precision conversion to obtain an intermediate parameter, wherein the number of mantissa bits of the intermediate parameter is less than the number of mantissa bits of the input parameter but greater than a; According to the a+1th bit in the mantissa of the input parameter, the second precision conversion is performed on the intermediate parameter to obtain the output parameter.
