Floating-point data precision conversion method and apparatus

MYPI2025005299A0Undetermined Publication Date: 2024-09-03HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
MYPI2025005299
Authority / Receiving Office
MY · MY
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-03-03
Filing Date
2024-02-28
Publication Date
2024-09-03

AI Technical Summary

Technical Problem

During the neural network training process, when high-precision floating-point data is converted into low-precision floating-point data, conversion errors will occur, affecting the accuracy of the training model.

Method used

The mixed rounding method is adopted, and the rounding method is determined according to the exponential field encoding value of floating point data. Random rounding is used when the high index field encoding value is greater than or equal to the threshold, and rounding away from 0 is used when it is lower than the threshold, reducing data format conversion. conversion error.

Benefits of technology

It effectively reduces the conversion error when converting high-precision floating point data into low-precision floating point data, and improves the training accuracy and data conversion efficiency of the training model.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure PI2025005299_DRAWING_001
    Figure PI2025005299_DRAWING_001
  • Figure PI2025005299_DRAWING_002
    Figure PI2025005299_DRAWING_002
  • Figure PI2025005299_DRAWING_003
    Figure PI2025005299_DRAWING_003
Patent Text Reader

Abstract

Embodiments of this application relate to the field of chip technologies, and provide a floating-point data precision conversion method and apparatus, reducing a conversion error during conversion from high-precision data into low-precision data. A specific solution is: determining a bit width of a second mantissa field based on a coded value of a first exponent field; determining a reserved coded value and a discarded coded value in a first mantissa field, where the reserved coded value includes a coded value that starts from a most significant bit in the first mantissa field and whose bit width is the same as the bit width of the second mantissa field; and if the coded value of the first exponent field is greater than or equal to a first preset threshold, performing a rounding operation on the reserved coded value based on a coded value that starts from a most significant bit and whose bit width is a preset bit width in the discarded coded value, to obtain a coded value of the second mantissa field; or if the coded value of the first exponent field is less than the first preset threshold, performing a rounding operation on the reserved coded value based on the most significant bit of the discarded coded value, to obtain the coded value of the second mantissa field. Embodiments of this application are applied to a process of floating-point data precision conversion
Need to check novelty before this filing date? Find Prior Art

Description

Floating point data precision conversion method and device

[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office on March 3, 2023, with application number 202310238789.3 and application name “Floating-point data precision conversion method and device”, the entire contents of which are incorporated by reference into this application. Technical Field

[0002] The embodiments of the present application relate to the field of chip technology, and in particular to a floating-point data precision conversion method and device. Background Art

[0003] With the advancement of artificial intelligence (AI), the scale of neural networks (NNs) has grown, and the computing power required to train them has also increased. Currently, the surge in computing power required for model training has led to high chip costs. Continuing to use existing floating point (FP) 16 mixed precision data and brain floating point (BF) 16 precision data is too expensive. Using low-bit data for computation allows lossless training of large transformer models. Therefore, low-bit floating point training is a future trend in large model training.

[0004] However, during neural network training, mixed-precision computing scenarios require conversion between floating-point data of different precisons. Converting low-precision floating-point data to high-precision floating-point data can be error-free. However, converting high-precision floating-point data to low-precision floating-point data requires rounding, which can result in conversion errors and affect the accuracy of the training model.

[0005] Summary of the Invention

[0006] The embodiments of the present application provide a floating-point data precision conversion method and apparatus, which reduce the problem of conversion error when converting high-precision floating-point data to low-precision floating-point data.

[0007] To achieve the above objectives, the embodiments of the present application adopt the following technical solutions.

[0008] In a first aspect, an embodiment of the present application provides a method for converting the precision of floating-point data, wherein the first floating-point data includes a first exponent field and a first mantissa field, and the second floating-point data includes a second mantissa field. The precision of the first floating-point data is higher than the precision of the second floating-point data, and the method includes: determining the bit width of the second mantissa field according to the coding value of the first exponent field; determining the retained coding value and the discarded coding value in the first mantissa field, the retained coding value including the coding value starting from the highest bit in the first mantissa field and having the same bit width as the bit width of the second mantissa field; if the coding value of the first exponent field is greater than or equal to a first preset threshold, rounding the retained coding value according to the coding value starting from the highest bit in the discarded coding value and having a preset bit width to obtain the coding value of the second mantissa field; if the coding value of the first exponent field is less than the first preset threshold, rounding the retained coding value according to the highest bit of the discarded coding value to obtain the coding value of the second mantissa field.

[0009] Therefore, the floating-point data precision conversion method provided in the embodiment of the present application realizes the conversion of high-precision floating-point data into low-precision floating-point data. Among them, the rounding operation of the retained coding value based on the coding value starting from the highest bit in the discarded coding value and with a bit width of the preset bit width can be understood as a random rounding method, and the rounding operation of the retained coding value based on the highest bit of the discarded coding value can be understood as a rounding method away from 0. In the data format conversion, the selection of the random rounding method and the rounding method away from 0 can be understood as a mixed rounding method. When the coding value in the first exponent domain is greater than or equal to the first preset threshold, the random rounding method is adopted, and when the coding value in the first exponent domain is less than the first preset threshold, the rounding method away from 0 is adopted. Therefore, by providing different rounding methods, the conversion error during data format conversion can be reduced, the efficiency of data conversion is improved, and the training accuracy of the training model is improved.

[0010] In one possible design, the rounding operation includes a carry operation and a discard operation, and the retained code value is rounded according to the code value of the discarded code value starting from the highest bit and with a preset bit width to obtain the code value of the second mantissa domain, including: when the code value of the discarded code value starting from the highest bit and with a preset bit width is greater than or equal to a second preset threshold, a carry operation is performed on the lowest bit of the retained code value, and the discarded code value is discarded, and the code value after the carry of the retained code value is the code value of the second mantissa domain; when the code value of the discarded code value starting from the highest bit and with a preset bit width is less than the second preset threshold, a discard operation is performed on the discarded code value, and the retained code value is the code value of the second mantissa domain; wherein the second preset threshold is the code value of the discarded code value starting from the lowest bit and with a preset bit width.

[0011] In this design, for the random rounding method, the second preset threshold used for comparison is the coding value starting from the lowest one among the discarded coding values ​​and with a bit width of the preset bit width. The generation of the second preset threshold does not require an additional random number generator, and there is no performance bottleneck for random number generation. The conversion efficiency of high-precision floating-point data to low-precision floating-point data is improved, and the hardware overhead is lower.

[0012] In one possible design, the rounding operation includes a carry operation and a discard operation, and the retained code value is rounded according to the highest bit of the discarded code value to obtain the code value of the second mantissa domain, including: when the highest bit of the discarded code value is greater than or equal to a third preset threshold, a carry operation is performed on the lowest bit of the retained code value, and the discarded code value is discarded, and the code value after the carry of the retained code value is the code value of the second mantissa domain; when the highest bit of the discarded code value is less than the third preset threshold, a discard operation is performed on the discarded code value, and the retained code value is the code value of the second mantissa domain.

[0013] In this design, the third preset threshold can be 0 or 1, and the highest bit of the discarded coded value is compared with the third preset threshold, which is a rounding away from zero method. In addition to the rounding away from zero method, other methods can also include rounding away from even numbers and rounding away from odd numbers. However, the rounding away from zero method has a smaller hardware implementation area, lower power consumption, and higher data resolution than other rounding methods.

[0014] In a possible design, the first preset threshold is determined by traversing a plurality of first floating-point data.

[0015] In this design, the first preset threshold is an adjustable parameter, and the accuracy of data format conversion can be improved by setting a suitable value of the first preset threshold.

[0016] In one possible design, the first floating-point data also includes a sign field, the second floating-point data also includes a sign field, a prefix code field and a second exponent field, the prefix code field is used to indicate the bit width of the second exponent field, and before determining the bit width of the second mantissa field according to the encoding value of the first exponent field, the method also includes: determining the bit width of the prefix code field, the encoding value of the prefix code field, the bit width of the second exponent field and the encoding value of the second exponent field according to the encoding value of the first exponent field.

[0017] In this design, in the data format conversion, the sign domain of the second floating-point data can be obtained based on the sign domain of the first floating-point data, the prefix code domain and the second exponent domain of the second floating-point data can be obtained based on the first exponent domain of the first floating-point data, and the second mantissa domain of the second floating-point data can be obtained based on the first mantissa domain of the first floating-point data. In the second floating-point data, the bit width of the second exponent domain is indicated by a shorter prefix code domain, which can effectively improve the precision or bit width of the mantissa domain of the second floating-point data. At the same time, for the second floating-point data that only provides 1-bit mantissa precision, a larger numerical range can be represented, effectively balancing the relationship between the bit width, range and precision of the second floating-point data. In addition, the prefix code domain can adopt a prefix code encoding method, which occupies less bit width and is convenient for parsing the second exponent domain and the second mantissa domain.

[0018] In a second aspect, an embodiment of the present application provides a floating-point data precision conversion device, wherein the first floating-point data includes a first exponent field and a first mantissa field, and the second floating-point data includes a second mantissa field. The precision of the first floating-point data is higher than the precision of the second floating-point data, and the device includes: a bit width calculation unit, used to determine the bit width of the second mantissa field according to the code value of the first exponent field; a mantissa field calculation unit, used to determine the retained code value and the discarded code value in the first mantissa field, the retained code value including the code value starting from the highest bit in the first mantissa field and having the same bit width as the bit width of the second mantissa field; a rounding operation unit, used to round the retained code value according to the code value starting from the highest bit in the discarded code value and having a preset bit width if the code value of the first exponent field is greater than or equal to a first preset threshold, to obtain the code value of the second mantissa field; the rounding operation unit is also used to round the retained code value according to the highest bit of the discarded code value if the code value of the first exponent field is less than the first preset threshold, to obtain the code value of the second mantissa field.

[0019] The beneficial effects of the second aspect can be found in the description of the first aspect.

[0020] In one possible design, the rounding operation includes a carry operation and a discard operation, and the rounding operation unit is further used to: when the code value of the discarded code value starting from the highest bit and with a preset bit width is greater than or equal to a second preset threshold, perform a carry operation on the lowest bit of the retained code value, perform a discard operation on the discarded code value, and the code value after the carry of the retained code value is the code value of the second mantissa field; when the code value of the discarded code value starting from the highest bit and with a preset bit width is less than the second preset threshold, perform a discard operation on the discarded code value, and the retained code value is the code value of the second mantissa field; wherein the second preset threshold is the code value of the discarded code value starting from the lowest bit and with a preset bit width.

[0021] In one possible design, the rounding operation includes a carry operation and a discard operation, and the rounding operation unit is further used to: when the highest bit of the discarded code value is greater than or equal to a third preset threshold, perform a carry operation on the lowest bit of the retained code value, perform a discard operation on the discarded code value, and the code value after the carry of the retained code value is the code value of the second mantissa field; when the highest bit of the discarded code value is less than the third preset threshold, perform a discard operation on the discarded code value, and the retained code value is the code value of the second mantissa field.

[0022] In a possible design, the first preset threshold is determined by traversing a plurality of first floating-point data.

[0023] In one possible design, the first floating-point data also includes a sign field, the second floating-point data also includes a sign field, a prefix code field and a second exponent field, the prefix code field is used to indicate the bit width of the second exponent field, and the bit width calculation unit is further used to: determine the bit width of the prefix code field, the encoding value of the prefix code field, the bit width of the second exponent field and the encoding value of the second exponent field based on the encoding value of the first exponent field.

[0024] In a third aspect, an embodiment of the present application provides a floating-point data precision conversion device, including a processor core memory, the memory storing computer instructions, and the processor executing the computer instructions and then executing the floating-point data precision conversion method in any of the above aspects and any possible implementation methods.

[0025] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, comprising computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the floating-point data precision conversion method in any of the above aspects and any possible implementation methods.

[0026] In a fifth aspect, an embodiment of the present application provides a computer program product, which, when running on a computer or processor, enables the computer or processor to execute the floating-point data precision conversion method in any of the above aspects and any possible implementation methods.

[0027] In a sixth aspect, embodiments of the present application provide a system, which may include a wireless access device according to any possible implementation of any of the above aspects and at least one electronic device. The electronic device and the wireless access device may execute the floating-point data precision conversion method according to any of the above aspects and any possible implementation.

[0028] It can be understood that any of the floating-point data precision conversion devices, computer-readable storage media or computer program products provided above can be applied to the corresponding methods provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding methods and will not be repeated here.

[0029] These and other aspects of the present application will become more readily apparent from the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] FIG1 is a schematic diagram of a data structure provided in an embodiment of the present application;

[0031] FIG2 is a schematic diagram of the distribution of the exponent field and mantissa field bit width provided in an embodiment of the present application;

[0032] FIG3 is a schematic diagram of a system or device for applying a floating-point data precision conversion device according to an embodiment of the present application;

[0033] FIG4 is a schematic diagram of the structure of a SoC provided in an embodiment of the present application;

[0034] FIG5 is a flow chart of a floating-point data precision conversion method provided by an embodiment of the present application;

[0035] FIG6 is a schematic diagram of a mantissa field of converting FP32 data into HiFloat8 data based on an SR rounding method provided in an embodiment of the present application;

[0036] FIG7 is a schematic diagram of a mantissa field of converting FP16 data into HiFloat8 data based on an SR rounding method provided in an embodiment of the present application;

[0037] FIG8 is a distribution diagram of a hybrid rounding method provided in an embodiment of the present application;

[0038] FIG9 is a flowchart of a neural network training method according to an embodiment of the present application;

[0039] FIG10 is another neural network training flow chart provided in an embodiment of the present application;

[0040] FIG11 is another neural network training flow chart provided in an embodiment of the present application;

[0041] FIG12 is a schematic diagram showing the composition of a floating-point data precision conversion device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0042] For ease of understanding, some examples of concepts related to the embodiments of this application are provided for reference as follows:

[0043] The neural network training process mainly includes forward calculations, backward calculations, and weight updates. The calculation modes during training include general matrix multiplication (GEMM) and non-general matrix multiplication. The layers corresponding to general matrix multiplication calculations can be called matrix multiplication calculation layers, and the layers corresponding to non-general matrix multiplication calculations can be called non-matrix multiplication calculation layers. Specifically, general matrix multiplication includes convolution, deconvolution, matrix multiplication (matmul), and batch matrix multiplication (batch matmul). Non-general matrix multiplication includes activation functions, normalization functions, optimizer gradient update calculations, etc. Activation functions include sigmoid, tanh, and relu. Normalization functions include batch normalization, layer normalization, and instance normalization.

[0044] Among them, general matrix multiplication can be calculated using FP16, BF16 or lower precision data, while non-general matrix multiplication requires calculation using higher precision data such as FP32. FP16 can also be called half-precision floating-point, BF16 can also be called 16-bit brain floating point, and FP32 can also be called single-precision floating point. As shown in the data structure diagram in Figure 1, specifically, FP16 includes 16 bits, of which the most significant 1 bit is the sign field, the middle 5 bits are the exponent field, and the remaining 10 bits are the mantissa field, which is used to represent decimals. BF16 includes 16 bits, of which the most significant 1 bit is the sign field, the middle 8 bits are the exponent field, and the remaining 7 bits are the mantissa field. FP32 includes 32 bits, of which the most significant 1 bit is the sign field, the middle 8 bits are the exponent field, and the remaining 23 bits are the mantissa field.

[0045] The technical solutions in the embodiments of the present application will be described below in conjunction with the accompanying drawings in the embodiments of the present application. In the description of the embodiments of the present application, unless otherwise specified, " / " means or, for example, A / B can mean A or B; "and / or" in this article is merely a description of the association relationship of associated objects, indicating that three relationships can exist, for example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. In addition, in the description of the embodiments of the present application, "multiple" means two or more than two.

[0046] In the following, the terms "first" and "second" are used for descriptive purposes only and should not be understood to indicate or imply relative importance or implicitly specify the number of the technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of this embodiment, unless otherwise specified, "plurality" means two or more.

[0047] Currently, the demand for computing power for training neural networks is increasing. Specifically, from the initial 100 petaflops (1 petaflop equals 1 quadrillion mathematical operations per second) required for a single neural network training process, to the current transformer model, a single neural network training process requires 1 billion petaflops, a 10 million-fold increase in computing power. Therefore, to reduce power consumption during neural network training, two low-precision floating-point data formats have been defined in the prior art: FP8 (E5M2) and FP8 (E4M3). FP8 (E5M2) has a 5-bit exponent and a 2-bit mantissa, while FP8 (E4M3) has a 4-bit exponent and a 3-bit mantissa. Mixed precision training is used for the forward and backward computations, as well as weight updates, of neural network training. GEMMs use two FP8 data formats for computation, while non-GEMMs use FP32 or FP16. It can be understood that by using the above two low-precision floating-point data formats for general matrix multiplication calculations, compared with using FP16 / BF16 data for general matrix multiplication calculations, since the total bit width of these two low-precision floating-point data formats is half of the total bit width of FP16 / BF16, the amount of data during general matrix multiplication calculations is reduced to half, thereby reducing the power consumption of the neural network chip.

[0048] However, converting high-precision floating-point data into the above-mentioned two low-precision floating-point data formats for general matrix multiplication calculations, and then converting the data into a high-precision data format for non-general matrix multiplication calculations, such as converting the data into FP16 / BF16 data formats, will cause data precision loss during the data format conversion process, which will lead to reduced training accuracy of the neural network. In order to reduce the precision loss of data, a scaling operation is introduced during the training process. However, after the scaling operation, the data generated by the tensor core area needs to be distributed and counted, which increases the power consumption of the neural network chip. Moreover, the two low-precision floating-point data have differences in accuracy and dynamic range. Users need to choose one data format from the two low-precision floating-point data for neural network training. Currently, this method cannot instruct users to make a choice, which will result in a poor user experience and poor generalization.

[0049] Another neural network training method is to use block floating point to express low-precision data, and adopt mixed precision to train when training the neural network, thereby reducing the power consumption of the neural network chip. Specifically, when performing general matrix multiplication calculations, the data is converted to block floating point data format before calculation, and when performing non-general matrix multiplication calculations, the data is converted to FP32 or FP16 data format before calculation. Among them, when converting the data to block floating point data format, the high-precision data is first divided into multiple blocks (blocks), and then the data distribution in each block is counted, and finally the data in the block is split into a common exponent domain (exponent), and the sign domain (sign) and mantissa domain (mantissa) of each data according to the data distribution. At this time, since the data in each block has a common exponent, the total bit width of each data in the block is reduced, so that when the data in the block floating point data format is used for training, the power consumption of the neural network chip can be reduced.

[0050] However, when training with data in block floating-point format, the limited representation range of the data will result in data loss, leading to reduced neural network training accuracy. Furthermore, different computational layers in the neural network have different block partitioning methods, which further complicates the algorithm. Furthermore, quantization and dequantization operations are required before and after general matrix multiplication, requiring statistical analysis of the data, which can affect neural network training performance.

[0051] Therefore, an embodiment of the present application provides a floating-point data precision conversion method, which involves the conversion of data formats of first floating-point data and second floating-point data, wherein the precision of the first floating-point data is higher than the precision of the second floating-point data. In this method, a mixed rounding method is adopted, that is, when the encoding value in the first exponent field is greater than or equal to the first preset threshold, a random rounding method is adopted, and when the encoding value in the first exponent field is less than the first preset threshold, a rounding method away from zero is adopted. By providing different rounding methods, the conversion error during data format conversion can be reduced, the efficiency of data conversion can be improved, and the training accuracy of the training model can be improved.

[0052] This method can be applied to the field of AI. This floating-point data precision conversion method can be applied to chips such as central processing units (CPUs), graphics processing units (GPUs), tensor processing units (TPUs), and neural network processing units (NPUs).

[0053] Specifically, the second floating-point data provided in the embodiment of the present application may be HiFloat8 data, as shown in Table 1, which is an encoding method for HiFloat8 data.

[0054] Table 1 HiFloat8 data encoding method

[0055] Among them, 8 is the total bit width of HiFloat8 data, and the bit width of the mantissa field can vary. The sign field occupies one bit, 0 represents a positive number, 1 represents a negative number, or 1 represents a negative number and 0 represents a positive number. The prefix code field occupies 2 or 3 bits, and the prefix code field can express 5 different information. The value of D can be 0, 1, 2, 3, or 4. The bit width of the exponent field changes according to the value of D, and the mantissa field occupies the remaining bit width. Among them, the prefix code field can use integer encoding, in which case D is a fixed value. The prefix code field can also use prefix code encoding, in which case D is a finite value set. In the prefix code encoding, 2 bits are used to encode the values ​​2, 3, and 4, and 3 bits are used to encode the values ​​0 and 1. The prefix code encoding method of the prefix code field is shown in Table 2, which shows the encoding method of the prefix code field.

[0056] Table 2 Coding method of prefix code field

[0057] As shown in Table 2, when the bit field is 2 bits, "11" can be used to encode the value "4," "10" can be used to encode the value "3," and "01" can be used to encode the value "2." When the bit field is 3 bits, "001" can be used to encode the value "1," and "000" can be used to encode the value "0." The encoding methods shown in Table 2 are for illustrative purposes only and do not constitute a limitation on the embodiments of this application.

[0058] Furthermore, the conversion formula between HiFloat8 data and decimal value (X) provided in the embodiment of the present application is:

[0059] Among them, Ec is the symmetry center of the exponent, which is also the bias in FP32 data.

[0060] When D is 0, the exponent field is 0. When D is not 0, the exponent field uses signed magnitude encoding, meaning the sign bit follows the true form (TF). The exponent field is encoded as Ei = {Se, 1'b1, TF[2:end]}, where Se is the exponent sign bit. The highest bit of TF, 1'b1, is hidden and not stored, resulting in the exponent field being encoded as Es = {Se, TF[2:end]}. The decimal encoding of the exponent field is Ev = Ei + Ec.

[0061] HiFloat(N,5,Ec) can be configured as HiFloat(8,5,0), abbreviated as HiF8, or other configurations. The distribution of HiFloat8 encoding values ​​is shown in Table 3.

[0062] Table 3 HiFloat8 encoding value distribution table

[0063] Combining Tables 1 and 2, as shown in Table 3, when D = 0, the mantissa field bit width = 8-4-0 = 4. When D = 1, the mantissa field bit width = 8-4-1 = 3. When D = 2, the mantissa field bit width = 8-3-2 = 3. When D = 3, the mantissa field bit width = 8-3-3 = 2. When D = 4, the mantissa field bit width = 8-3-4 = 1. It can be understood that when the bit width of the exponent field is smaller (i.e., the smaller the numerical range), the larger the bit width occupied by the mantissa field, and the greater the numerical precision. When the bit width of the exponent field is larger (i.e., the larger the numerical range), the smaller the bit width occupied by the mantissa field, and the lower the numerical precision.

[0064] In addition, as shown in FIG2 , a schematic diagram of the distribution of the exponent field and mantissa field bit widths provided in an embodiment of the present application is shown. FIG2 shows that the smaller the absolute value of the exponent field, the larger the mantissa field bit width. Therefore, the second floating-point data can also be referred to as tapered low-precision floating-point data. Furthermore, tapered low-precision floating-point data can also take into account both the dynamic range and data accuracy of the data, reducing conversion errors during the conversion of high-precision data to a low-precision data format, thereby improving training accuracy.

[0065] In some possible implementations, in addition to the above-mentioned normalized representation, the value X represented by the floating-point number can also be customized to represent various special values.

[0066] For example, when S=0, D=4, Es=4'b1111=-15, and M=1'b0, X=0 (zero); when S=1, D=4, ES=4'b1111=-15, and M=1'b0, X can represent not a number (NaN); and when D=4, ES=4'b0111=15, and M=1'b1, X=±infinity.

[0067] In the above scenario, the floating-point data precision conversion method and device of the present application can be applied to different systems or devices, such as the floating-point data precision conversion device 30 shown in Figure 3, which is a schematic diagram of a system or device for applying a floating-point data precision conversion device provided by an embodiment of the present application. The floating-point data precision conversion device can be a terminal, such as a server 31, a mobile phone terminal 32, a tablet computer 33, a laptop computer 34, an augmented reality (AR) device (not shown in Figure 3), a virtual reality (VR) device (not shown in Figure 3) and a vehicle-mounted terminal (not shown in Figure 3), etc. The floating-point data precision conversion method provided in the present application can be applied to scenarios involving mixed-precision calculations such as CPU, high-performance computing (HPC) and AI in the floating-point data precision conversion device 30, such as scalar calculation units, vector calculation units, matrix calculation units and tensor calculation units.

[0068] In some embodiments, the floating-point data precision conversion device proposed in the present application can be a chip, for example, the chip is a system-on-a-chip (SoC). As shown in Figure 4, Figure 4 is a structural diagram of a SoC provided in an embodiment of the present application. The SoC includes a processor, which can be a single-core processor or a multi-core processor, a memory, and an input / output (I / O) interface, etc. After the processor loads the data and application in the memory, it processes the data, such as performing the calculation processing in the present application. For example, when the data is FP32 data, the encoding value of the second mantissa field in the second floating-point data can be determined by reading the encoding value of the first exponent field in the FP32 data.

[0069] Applied to the above-mentioned system or device, the process of the floating-point data precision conversion method provided in the embodiment of the present application is introduced below.

[0070] An embodiment of the present application provides a floating-point data precision conversion method, as shown in FIG5 , which is a flow chart of a floating-point data precision conversion method provided by an embodiment of the present application. The method is applied to convert first floating-point data into second floating-point data, where the first floating-point data may include a first exponent field and a first mantissa field, and the second floating-point data may include a second mantissa field. The method includes the following process.

[0071] Step 501: The floating-point data precision conversion device determines the bit width of the second mantissa field according to the encoding value of the first exponent field.

[0072] Exemplarily, the precision of the first floating-point data is higher than the precision of the second floating-point data, wherein the first floating-point data may be FP32 data or FP16 data, and the second floating-point data may be HiFloat8 data.

[0073] During the conversion process, the bit width of the mantissa field of the HiFloat8 data is variable, that is, the bit width of the second mantissa field is variable, and its bit width range is 1 to 4. It can be understood that the larger the bit width of the mantissa field, the greater the precision of the HiFloat8 data.

[0074] For example, if the first floating-point data is FP32 data, and the encoding value of the first exponent field is 8'b01111100, representing the decimal value 124, and the offset in the FP32 data is removed (for FP32 data, the offset is 127), then the first exponent field of the FP32 data is 2 to the power of -3. Combining Tables 2 and 3, we can see that the bit width of the second mantissa field is 2.

[0075] Step 502: The floating-point data precision conversion device determines the retained code values ​​and discarded code values ​​in the first mantissa field. The retained code values ​​include code values ​​in the first mantissa field starting from the highest bit and having the same bit width as the second mantissa field.

[0076] Exemplarily, since the precision of the first floating-point data is higher than the precision of the second floating-point data, the bit width of the first mantissa field of the first floating-point data is greater than the bit width of the second mantissa field of the second floating-point data. Therefore, if the first floating-point data is converted to the second floating-point data, since the bit width of the second mantissa field of the second floating-point data is limited, it is necessary to round off the code values ​​in the first mantissa field. Among them, the code values ​​in the first mantissa field starting from the highest bit and having the same bit width as the bit width of the second mantissa field are determined as retained code values, and the remaining code values ​​in the first mantissa field except the retained code values ​​are determined as discarded code values.

[0077] Continuing with the example of the first floating-point data being FP32, if the encoding value of the first mantissa field is 23'b010000000000000000000000, since the bit width of the second mantissa field is 2, the retained encoding value is 2'b01.

[0078] Step 503: If the encoding value of the first exponent field is greater than or equal to the first preset threshold, the floating-point data precision conversion device rounds the retained encoding value based on the encoding value with a preset bit width starting from the highest bit in the discarded encoding value to obtain the encoding value of the second mantissa field.

[0079] Exemplarily, the method of rounding the retained coding value based on the coding value starting from the highest bit in the discarded coding value and with a bit width of a preset bit width can be understood as a stochastic rounding (SR) method. The preset bit width can be an integer from 1 to 14. The SR rounding method introduces a random number, and the retained coding value is rounded based on the comparison result of the coding value starting from the highest bit in the discarded coding value and with a bit width of a preset bit width and the random number. It can be understood that the random number in the SR rounding method is related to the discarded coding value, and no additional random number generator is required. There is no performance bottleneck in random number generation. Therefore, the SR rounding method not only improves the conversion efficiency of high-precision data to low-precision data, but also has lower hardware overhead.

[0080] Step 504: If the code value of the first exponent field is less than the first preset threshold, the floating-point data precision conversion device rounds the retained code value according to the highest bit of the discarded code value to obtain the code value of the second mantissa field.

[0081] For example, the method of rounding the retained code value based on the discarded highest bit of the code value can be understood as a round half away from zero (TA) method. In the TA rounding method, the highest bit of the discarded code value can be compared with a preset threshold, and the retained code value is rounded based on the comparison result. The TA rounding method has a smaller hardware implementation area, lower power consumption, and higher data resolution than other rounding methods.

[0082] Therefore, SR rounding and TA rounding can be understood as hybrid rounding (HR). In the hybrid rounding method, SR rounding is used for the data on both sides of the Gaussian distribution, which accounts for a relatively small proportion, that is, the main data affecting the mean. TA rounding is used for the intermediate data, which accounts for a larger proportion. Hybrid rounding can be applied to both FP32 data and FP16 data, providing better mean invariance than using TA rounding or SR rounding alone, and with less hardware overhead.

[0083] Optionally, the rounding operation includes a carry operation and a discard operation. Step 503 may include: when the discarded code value starts from the highest bit and the code value with a bit width of a preset bit width is greater than or equal to a second preset threshold, the floating-point data precision conversion device performs a carry operation on the lowest bit of the retained code value, discards the discarded code value, and the code value after the carry operation of the retained code value is the code value of the second mantissa domain. When the discarded code value starts from the highest bit and the code value with a bit width of a preset bit width is less than the second preset threshold, the discarded code value is discarded, and the retained code value is the code value of the second mantissa domain. The second preset threshold is the code value starting from the lowest bit of the discarded code value and with a bit width of the preset bit width.

[0084] Exemplarily, for the SR rounding mode, the preset bit width may be an integer between 1 and 14. Taking the preset bit width of 14 bits as an example, the second preset threshold may be the discarded coded value starting from the least significant bit and having a bit width of 14 in the discarded coded value, and the partially discarded coded value in the discarded coded value used for comparison with the second preset threshold may be the coded value starting from the most significant bit and having a bit width of 14. Continuing with the example of the coded value of the first mantissa field being 23'b010000000000000000000000, and the bit width of the second mantissa field being 2, the retained coded value in the first mantissa field is 2'b01, the discarded coded value is 21'b0000000000000000000000, the partially discarded coded value is 14'b00000000000000, and the second preset threshold is 14'b000000000000000. Since the partially discarded code values ​​are equal to the second preset threshold, the discarded code values ​​are discarded, and the code value after the carry of the retained code value is the code value of the second mantissa field, that is, the code value of the second mantissa field is 2'b01.

[0085] In one example, as shown in FIG6 , FIG6 is a schematic diagram of a mantissa field of FP32 data converted to HiFloat8 data based on an SR rounding method according to an embodiment of the present application. The least significant bit (LSB) in the mantissa field of the FP32 data is taken to form a 2-bit second preset threshold value T2 = {LSB, 1'b1}. The first 4 bits of the code value can be discarded to form F2, that is, F2 is the retained code value, and M is the retained code value after the rounding operation.

[0086] In another example, as shown in FIG7 , FIG7 is a schematic diagram of the mantissa field of FP16 data converted to HiFloat8 data based on the SR rounding method provided by an embodiment of the present application. The least significant bit (LSB) in the mantissa field of the FP16 data is taken to form a 2-bit second preset threshold value T2 = {LSB, 1'b1}, and the first 4 bits of the encoded value can be discarded to form F2.

[0087] Applicable to the mantissa fields in Figures 6 and 7, SR rounding is defined as: if F2 ≥ T2, M + 1, else M. That is, if F2 is greater than or equal to T2, the retained coded value is carried out. If F2 is less than T2, the discarded coded value is discarded.

[0088] In another implementation, any bit width in the first mantissa field of the first floating point data is converted into n ,1}or{negate(a n), 1} as the second preset threshold, where the negate operation means the inversion of 0 and 1, negate(0)=1, negate(1)=0. The SR rounding result can be obtained by comparing the partially discarded code value in the first mantissa field with the second preset threshold. In one example, the preset bit width is 1 bit. If the highest bit in the discarded code value is 0, then {1'b0,1'b1}=0.25, and the discarded code value is discarded. If the highest bit in the discarded code value is 1, then {1'b1,1'b1}=0.75, and the lowest bit of the retained code value is carried.

[0089] The SR rounding method has a single-data error of 0.75 units in the last place (ulp), which is less than the 1 ulp error of standard random rounding. Furthermore, if the data is uniformly distributed, the SR rounding method has better mean invariance than standard random rounding.

[0090] Optionally, the rounding operation includes a carry operation and a discard operation. Step 504 may include: when the highest bit of the discarded code value is greater than or equal to a third preset threshold, the floating-point data precision conversion device performs a carry operation on the lowest bit of the retained code value, performs a discard operation on the discarded code value, and the code value after the carry operation of the retained code value is the code value of the second mantissa field. When the highest bit of the discarded code value is less than the third preset threshold, the discard operation is performed on the discarded code value, and the retained code value is the code value of the second mantissa field.

[0091] For example, for the TA rounding mode, the third preset threshold value may be 1. In one example, when the highest bit of the discarded code value is 1, a carry operation is performed on the lowest bit of the retained code value, and the discarded code value is discarded. In another example, when the highest bit of the discarded code value is 0, the discarded code value is discarded.

[0092] For the TA rounding method, the third preset threshold value may also be 0. When the highest bit of the discarded code value is greater than 0, a carry operation is performed on the lowest bit of the retained code value, and the discarded code value is discarded. The code value obtained after the carry operation on the retained code value is the code value of the second mantissa field. When the highest bit of the discarded code value is less than or equal to 0, the discarded code value is discarded, and the retained code value is the first code value of the second mantissa field.

[0093] Optionally, the first preset threshold is determined by traversing a plurality of first floating-point data.

[0094] Exemplarily, by traversing multiple first floating-point data, that is, traversing the encoding values ​​of different first exponent domains, it can be determined that when the first preset threshold is 4, the lowest conversion error can be obtained. As shown in Figure 8, Figure 8 is a distribution diagram of a hybrid rounding method provided in an embodiment of the present application. Among them, the exponent value E of HiFloat8 is E = [-15, 15], where abs(E) = [0, 15]. If E ≥ 4, the TA rounding method is adopted, and if E < 4, the SR rounding method is adopted.

[0095] Optionally, the first floating-point data further includes a sign field, and the second floating-point data further includes a sign field, a prefix code field, and a second exponent field, wherein the prefix code field is used to indicate the bit width of the second exponent field. The method further includes: determining the bit width of the prefix code field, the code value of the prefix code field, the bit width of the second exponent field, and the code value of the second exponent field based on the code value of the first exponent field. Exemplarily, during the data format conversion, the positive and negative signs of the data remain unchanged, that is, the bit width and code value of the sign fields of the first floating-point data and the second floating-point data are the same.

[0096] For the prefix code field and the second exponent field, the exponent value N of the first exponent field can be determined based on the encoding value of the first exponent field. According to Table 1, the value of D can be determined based on the exponent value of the first exponent field. In one example, the first floating-point data is FP32 data. Assuming that the encoding value of the first exponent field is 8'b01111100, which represents 124 in decimal, after removing the offset 127 for FP32 data, the decimal value is -3. Among them, -3 is the exponent value N of the first exponent field. Using the formula D=INT[log2|N|], it can be obtained that D is 2. By looking at Table 2, it can be determined that the bit width of the prefix code field corresponding to 2 is 2, the first encoding value of the prefix code field is 01, and the bit width of the second exponent field is 2. Continuing to look at Table 3, when D is 2 and the exponent value of the first exponent field is -3, that is, the exponent sign bit Se is 1, since the bit width of the second exponent field is 2, the encoding value of the second exponent field is determined to be 2'b11.

[0097] It is understandable that if a carry operation is performed on the retained code value, the retained code value may overflow. The execution device should first determine whether the retained code value after the carry operation overflows. If the retained code value after the carry operation overflows, the lowest bit of the code value of the first exponent field is added by 1 to obtain a new code value of the first exponent field. The execution device then determines the new bit width of the second exponent field and the new bit width of the prefix code field based on the new code value of the first exponent field. If the new bit width of the prefix code field is different from the original bit width of the prefix code field, the new code value of the prefix code field, the new code value of the second exponent field, the new bit width of the second mantissa field, and the new code value of the second mantissa field are determined according to the new code value of the first exponent field. If the new bit width of the prefix code field is the same as the original bit width of the prefix code field, then determine whether the new bit width of the second exponent field is the same as the original bit width of the second exponent field. If the new bit width of the second exponent field is smaller than the original bit width of the second exponent field, then add 1 to the bit width of the retained code value to obtain the new bit width of the second mantissa field and the new code value of the second mantissa field. If the new bit width of the second exponent field is larger than the original bit width of the second exponent field, then discard the least significant bit of the retained code value to obtain the new bit width of the second mantissa field and the new code value of the second mantissa field.

[0098] The floating-point data precision conversion method provided in the embodiment of the present application can be applied to the neural network training process, wherein the neural network training process includes model weight parameter initialization, forward calculation, reverse calculation, weight update process and data communication process between multiple machines and multiple cards.

[0099] The model weight parameter initialization process uses high-precision floating-point data format for weights, with different initialization schemes adopted for different application scenarios. For training and pre-training processes starting from scratch, high-precision floating-point data is used for random initialization, and the randomization method is consistent with FP16 mixed precision or FP32 precision. For retraining processes based on FP32 or FP16 mixed precision weights, the weight data is directly loaded from the FP32 or FP16 mixed precision training process. Other parameters in the model, such as normalization layer parameters and optimizer parameters, are initialized in the same way as weight initialization during training and retraining.

[0100] After the weight parameters are initialized, as shown in Figure 9, Figure 9 is a neural network training flow chart provided by an embodiment of the present application. Among them, L-1 represents the training process corresponding to the previous matrix multiplication calculation layer of the first matrix multiplication calculation layer, and L+1 represents the training process corresponding to the next matrix multiplication calculation layer of the first matrix multiplication calculation layer. The training process of L-1 and L+1 is the same as the training process of the data matrix and weight matrix corresponding to the first matrix multiplication calculation layer.

[0101] In the forward calculation process, for the GEMM operations of the first and middle layers of the network, the FP32 data or FP16 data needs to be converted to a tapered low-precision floating-point data format before being input into the GEMM calculation unit and outputting FP32 data or FP16 data. For the activation layer and the normalization layer, FP32 data or FP16 data is used for calculation. For the tail layer of the neural network, the FP32 data type or FP16 data type is uniformly maintained. The specific process is as follows: first, according to the data conversion matrix and the weight conversion matrix in the first floating-point data format, the matrix multiplication calculation layer is forward calculated to obtain the first output conversion matrix in the second floating-point data format. Then, according to the first output conversion matrix, the first non-matrix multiplication calculation layer is forward calculated, and the second output matrix in the first floating-point data format is obtained. The second output matrix is ​​used as the input matrix of the next calculation layer of the first non-matrix multiplication calculation layer during the forward calculation.

[0102] In the reverse calculation process, the tapered low-precision floating-point mixed-precision reverse process is consistent with the FP16 training reverse process, using an automatic scaling operation, that is, the loss is multiplied by an appropriate scaling value before reverse differentiation. For the derivation process of feature maps and weights, the GEMM input is tapered low-precision floating-point data. The specific process is as follows: obtaining a first output error gradient matrix in a second floating-point data format, which is used to update the parameters corresponding to the first non-matrix multiplication calculation layer. The first output error gradient matrix is ​​then backpropagated through the first non-matrix multiplication calculation layer to obtain a second output error gradient matrix in a first floating-point data format. The second output error gradient matrix is ​​converted into a second output error gradient conversion matrix in a second floating-point data format. The first matrix multiplication calculation layer is then backpropagated based on the transposed matrix of the second output error gradient conversion matrix and the weight conversion matrix to obtain a third output error gradient conversion matrix in a first floating-point data format. The third output error gradient conversion matrix is ​​used to update the parameters corresponding to the previous calculation layer of the first matrix multiplication calculation layer during the forward calculation of the neural network.

[0103] The weight gradient calculation process can be: inputting the second output error gradient conversion matrix and the data conversion matrix into the first matrix multiplication calculation layer to obtain weight gradient conversion data in a third data format, and updating the weight matrix according to the weight gradient conversion data.

[0104] In one possible embodiment, as shown in FIG9 , the weight matrix and other parameters can be converted into a high-precision data format and backed up in a local storage device. For example, the high-precision weight matrix can be stored in a first storage unit w-master , the optimizer state parameters can be stored in a second storage unit momentum , and other parameters can be stored in a third storage unit other states .

[0105] Furthermore, large-scale model training is often accomplished across multiple machines, involving data parallelism, model parallelism, pipeline parallelism, and combinations of these three approaches. Training across multiple machines and multiple graphics cards requires inter-card data communication, which can be effectively addressed by using tapered low-precision floating-point data.

[0106] The forward calculation and the reverse calculation may use different rounding methods to convert the first floating-point data into the second floating-point data, which is not limited in the embodiment of the present application.

[0107] In example 1, as shown in FIG9 , the forward calculation may adopt a mixed rounding method, and the backward calculation may adopt a TA / SR method.

[0108] Example 2, as shown in Figure 10, is another neural network training flow chart provided by an embodiment of the present application. The forward calculation can use the TA / SR method, and the reverse calculation can use the mixed rounding method.

[0109] Example 3, as shown in Figure 11, is another neural network training flow chart provided in an embodiment of the present application. Forward calculation can use a mixed rounding method, and reverse calculation can also use a mixed rounding method.

[0110] Therefore, the floating-point data precision conversion method provided in the embodiment of the present application can achieve training accuracy consistent with FP32 or FP16 mixed precision in artificial intelligence network models such as convolutional neural networks and transformer models. In bilateral mode (both forward calculation and reverse calculation use mixed rounding methods), the average accuracy is higher than that in unilateral mode (forward calculation or reverse calculation uses mixed rounding methods).

[0111] It is understandable that in order to implement the above functions, the electronic device includes hardware and / or software modules corresponding to the execution of each function. In combination with the algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application in combination with the embodiments, but such implementation should not be considered to be beyond the scope of this application.

[0112] In this embodiment, the electronic device can be divided into functional modules according to the above-mentioned method example. For example, each functional module can be divided according to each function, or two or more functions can be integrated into a single processing module. The above-mentioned integrated modules can be implemented in the form of hardware. It should be noted that the module division in this embodiment is illustrative and is only a logical functional division. In actual implementation, other division methods may be used.

[0113] In the case of dividing the functional modules according to the corresponding functions, Figure 12 shows a possible composition diagram of the floating-point data precision conversion device 1200 involved in the above embodiment. As shown in Figure 12, the floating-point data precision conversion device 1200 may include: a bit width calculation unit 1201, a mantissa field calculation unit 1202, and a rounding operation unit 1203.

[0114] The bit width calculation unit 1201 may be used to support the floating-point data precision conversion device 1200 in executing the above-mentioned step 501 and / or other processes of the technology described herein.

[0115] The mantissa domain calculation unit 1202 may be used to support the floating-point data precision conversion apparatus 1200 in executing the above-mentioned step 502 and / or other processes of the technology described herein.

[0116] The rounding operation unit 1203 may be used to support the floating-point data precision conversion apparatus 1200 in executing the above-mentioned steps 503 and 504 , etc., and / or other processes of the technology described herein.

[0117] It should be noted that all relevant contents of each step involved in the above method embodiment can be referred to the functional description of the corresponding functional module and will not be repeated here.

[0118] The floating-point data precision conversion device 1200 provided in this embodiment is used to execute the above-mentioned floating-point data precision conversion method, and thus can achieve the same effect as the above-mentioned implementation method.

[0119] When integrated, the floating-point data precision conversion device 1200 may include a processing module, a storage module, and a communication module. The processing module may be used to control and manage the operations of the floating-point data precision conversion device 1200. For example, it may be used to support the floating-point data precision conversion device 1200 in executing the steps performed by the bit width calculation unit 1201, the mantissa field calculation unit 1202, and the rounding operation unit 1203. The storage module may be used to support the floating-point data precision conversion device 1200 in storing program code and data. The communication module may be used to support communication between the floating-point data precision conversion device 1200 and other devices, such as wireless access devices.

[0120] The processing module may be a processor or a controller. It may implement or execute the various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. The processor may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of digital signal processing (DSP) and a microprocessor, and so on. The storage module may be a memory. The communication module may specifically be a device that interacts with other electronic devices, such as a radio frequency circuit, a Bluetooth chip, or a Wi-Fi chip.

[0121] The present application also provides an electronic device including one or more processors and one or more memories. The one or more memories are coupled to the one or more processors and are configured to store computer program code. The computer program code includes computer instructions. When the one or more processors execute the computer instructions, the electronic device executes the above-described related method steps to implement the floating-point data precision conversion method described in the above-described embodiment.

[0122] An embodiment of the present application further provides a computer storage medium storing computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the above-mentioned related method steps to implement the floating-point data precision conversion method in the above-mentioned embodiment.

[0123] An embodiment of the present application further provides a computer program product. When the computer program product is run on a computer, the computer is caused to execute the above-mentioned related steps to implement the floating-point data precision conversion method executed by the electronic device in the above-mentioned embodiment.

[0124] In addition, an embodiment of the present application also provides a device, which can specifically be a chip, component or module, and the device may include a connected processor and memory; wherein the memory is used to store computer-executable instructions, and when the device is running, the processor can execute the computer-executable instructions stored in the memory to enable the chip to execute the floating-point data precision conversion method performed by the electronic device in the above-mentioned method embodiments.

[0125] Among them, the electronic device, computer storage medium, computer program product or chip provided in this embodiment is used to execute the corresponding method provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding method provided above, and will not be repeated here.

[0126] Through the description of the above implementation methods, technical personnel in the relevant field can understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be distributed and completed by different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.

[0127] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the modules or units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0128] The units described as separate components may or may not be physically separate, and the components shown as units may be one physical unit or multiple physical units, that is, they may be located in one place or distributed in multiple places. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0129] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0130] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling a device (which can be a single-chip microcomputer, chip, etc.) or a processor (processor) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0131] The above content is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of the present application. Therefore, the scope of protection of the present application should be based on the scope of protection of the claims.

Claims

1. A floating point data precision conversion method, characterized in that: The first floating point data includes a first exponent field and a first mantissa field, the second floating point data includes a second mantissa field, the precision of the first floating point data is higher than the precision of the second floating point data, and the method includes: Determining a bit width of the second mantissa field according to the encoding value of the first exponent field; Determine the retained code values ​​and discarded code values ​​in the first mantissa field, wherein the retained code values ​​include code values ​​in the first mantissa field starting from the most significant bit and having the same bit width as the second mantissa field; If the code value of the first exponent field is greater than or equal to a first preset threshold, the retained code value is rounded according to the code value of the discarded code value starting from the highest bit and having a bit width of a preset bit width to obtain the code value of the second mantissa field; If the code value of the first exponent field is less than the first preset threshold, the retained code value is rounded according to the highest bit of the discarded code value to obtain the code value of the second mantissa field.

2. The method according to claim 1, characterized in that The rounding operation includes a carry operation and a discard operation, and the rounding operation is performed on the retained code value according to the code value of the discarded code value starting from the highest bit and having a bit width of a preset bit width to obtain the code value of the second mantissa field, including: When the code value of the discarded code value starting from the highest bit and having a bit width of the preset bit width is greater than or equal to the second preset threshold, a carry operation is performed on the lowest bit of the retained code value, and the discarded code value is discarded, and the code value after the carry of the retained code value is the code value of the second mantissa field; When the code value of the discarded code value starting from the highest bit and having a bit width of the preset bit width is less than the second preset threshold, the discarded code value is discarded, and the retained code value is the code value of the second mantissa field; The second preset threshold is a coding value of the discarded coding value starting from the least significant bit and having a bit width equal to the preset bit width.

3. The method according to claim 1, characterized in that The rounding operation includes a carry operation and a discard operation, and the rounding operation is performed on the retained code value according to the highest bit of the discarded code value to obtain the code value of the second mantissa field, including: When the highest bit of the discarded code value is greater than or equal to a third preset threshold, a carry operation is performed on the lowest bit of the retained code value, and the discarded code value is discarded, and the code value after the carry of the retained code value is the code value of the second mantissa field; When the highest bit of the discarded code value is less than the third preset threshold, the discarded code value is discarded, and the retained code value is the code value of the second mantissa field.

4. The method according to any one of claims 1 to 3, characterized in that: The first preset threshold is determined by traversing a plurality of the first floating-point data.

5. The method according to claim 1, characterized in that The first floating-point data further includes a sign field, the second floating-point data further includes the sign field, a prefix code field, and a second exponent field, the prefix code field is used to indicate the bit width of the second exponent field, and before determining the bit width of the second mantissa field according to the encoded value of the first exponent field, the method further includes: The bit width of the prefix code field, the encoding value of the prefix code field, the bit width of the second exponent field, and the encoding value of the second exponent field are determined according to the encoding value of the first exponent field.

6. A floating point data precision conversion device, characterized in that: The first floating point data includes a first exponent field and a first mantissa field, the second floating point data includes a second mantissa field, the precision of the first floating point data is higher than the precision of the second floating point data, and the device includes: A bit width calculation unit, configured to determine a bit width of the second mantissa field according to a code value of the first exponent field; a mantissa field calculation unit, configured to determine a retained code value and a discarded code value in the first mantissa field, wherein the retained code value includes a code value in the first mantissa field starting from the highest bit and having the same bit width as the bit width of the second mantissa field; a rounding operation unit, configured to, if the code value of the first exponent field is greater than or equal to a first preset threshold, round the retained code value according to the code value of the discarded code value starting from the highest bit and having a bit width of a preset bit width, to obtain the code value of the second mantissa field; The rounding operation unit is also used to round the retained code value according to the highest bit of the discarded code value to obtain the code value of the second mantissa field if the code value of the first exponent field is less than the first preset threshold.

7. The device according to claim 6, characterized in that The rounding operation includes a carry operation and a discard operation, and the rounding operation unit is further used for: When the code value of the discarded code value starting from the highest bit and having a bit width of the preset bit width is greater than or equal to the second preset threshold, a carry operation is performed on the lowest bit of the retained code value, and the discarded code value is discarded, and the code value after the carry of the retained code value is the code value of the second mantissa field; When the code value of the discarded code value starting from the highest bit and having a bit width of the preset bit width is less than the second preset threshold, the discarded code value is discarded, and the retained code value is the code value of the second mantissa field; The second preset threshold is a coding value starting from the least significant bit in the discarded coding value and having a bit width equal to a preset bit width.

8. The device according to claim 6, characterized in that The rounding operation includes a carry operation and a discard operation, and the rounding operation unit is further used for: When the highest bit of the discarded code value is greater than or equal to a third preset threshold, a carry operation is performed on the lowest bit of the retained code value, and the discarded code value is discarded, and the code value after the carry of the retained code value is the code value of the second mantissa field; When the highest bit of the discarded code value is less than the third preset threshold, the discarded code value is discarded, and the retained code value is the code value of the second mantissa field.

9. The device according to any one of claims 6 to 8, characterized in that: The first preset threshold is determined by traversing a plurality of the first floating-point data.

10. The device according to claim 6, characterized in that The first floating-point data further includes a sign field, the second floating-point data further includes the sign field, a prefix code field, and a second exponent field, the prefix code field is used to indicate a bit width of the second exponent field, and the bit width calculation unit is further used to: The bit width of the prefix code field, the encoding value of the prefix code field, the bit width of the second exponent field, and the encoding value of the second exponent field are determined according to the encoding value of the first exponent field.

11. A floating point data precision conversion device, characterized in that: The method comprises a processor and a memory, wherein the memory stores computer instructions, and the processor executes the method according to any one of claims 1 to 5 after executing the computer instructions.

12. A computer-readable storage medium, characterized in that: The method comprises computer instructions, which, when executed on an electronic device, cause the electronic device to execute the method according to any one of claims 1 to 5.