Data format conversion method and device, data format conversion equipment and medium
By directly processing data format conversion in floating point and integer formats, avoiding the use of intermediate data formats, the problem of large bit width of intermediate data formats in the prior art is solved, and more efficient and general data format conversion is achieved.
Patent Information
- Application Number
- CN202411758479.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-03
- Publication Date
- 2025-03-14
AI Technical Summary
The prior art requires the use of a larger intermediate data format bit width in data format conversion, resulting in an increase in the additional chip area and the inability to effectively handle the conversion between floating point and integer formats.
By obtaining input operands and format conversion standards, the floating-point data format is directly converted to floating-point data format, or the integer data format is converted to integer data format, avoiding the use of intermediate data formats, and simplifying the data format conversion logic.
It realizes the use of intermediate data formats during data format conversion, simplifies the conversion logic, reduces chip area requirements, and improves the efficiency and versatility of data format conversion.
Smart Images

Figure CN119621820A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data conversion technology, and in particular to a data format conversion method, device, data format conversion equipment and medium. Background Art
[0002] The current mainstream deep learning algorithms generally support mixed-precision training and reasoning modes. In order to improve hardware throughput and computing power, low-precision data formats such as FP8 and INT8 are used for input and output, while high-precision data formats such as FP32 are often used for internal accumulation. In order to output the high-precision internal data format to the low-precision external data format, a data format conversion unit is required for format conversion.
[0003] In the existing technical solutions, in order to support conversion from multiple preset data formats to multiple target formats, an intermediate data format is adopted. In order to be compatible with multiple preset data formats at the same time, the intermediate data format needs to use a larger bit width. For example, if the preset multiple data formats are FP32 and INT32, the bit width of the intermediate data format is 41 bits, which is larger than the bit width of the preset data format, thereby increasing the additional chip area.
[0004] In actual artificial intelligence applications, it is often only necessary to convert the input floating-point data format into the output floating-point data format, or to convert the input integer data format into the output integer data format, without the need for floating-point to integer or integer to floating-point format conversion. Summary of the invention
[0005] The present invention provides a data format conversion method, device, data format conversion equipment and medium, which can avoid referencing intermediate data formats during data format conversion, simplify the data format conversion logic, and solve the problem of large bit width of intermediate data formats in the prior art.
[0006] According to one aspect of the present invention, a data format conversion method is provided, which is performed by a data format conversion device, and includes:
[0007] Obtaining an input operand and a format conversion standard; wherein the input operand is in a first floating point data format or a first integer data format;
[0008] According to the input operand and the format conversion standard, perform any of the following:
[0009] When the input operand is in the first floating point data format, determining a rounding mode and a second floating point data format according to the format conversion standard, and converting the input operand into an output operand according to the rounding mode, the second floating point data format, and each standard output data format supported by the data format conversion device;
[0010] When the input operand is in the first integer data format, the second integer data format is determined according to the format conversion standard, and the input operand is converted into an output operand according to the second integer data format and each output data format supported by the data format conversion device.
[0011] According to another aspect of the present invention, there is provided a data format conversion apparatus, which is executed by a data format conversion device and includes:
[0012] An input data acquisition module, used to acquire an input operand and a format conversion standard; wherein the input operand is in a first floating point data format or a first integer data format;
[0013] A data format conversion module is used to perform any of the following according to the input operand and the format conversion standard:
[0014] When the input operand is in the first floating point data format, determining a rounding mode and a second floating point data format according to the format conversion standard, and converting the input operand into an output operand according to the rounding mode, the second floating point data format, and each standard output data format supported by the data format conversion device;
[0015] When the input operand is in the first integer data format, the second integer data format is determined according to the format conversion standard, and the input operand is converted into an output operand according to the second integer data format and each output data format supported by the data format conversion device.
[0016] According to another aspect of the present invention, a data format conversion device is provided, the data format conversion device comprising:
[0017] at least one processor; and
[0018] a memory communicatively connected to the at least one processor; wherein,
[0019] The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the data format conversion method described in any embodiment of the present invention.
[0020] According to another aspect of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the data format conversion method described in any embodiment of the present invention when executed.
[0021] The technical solution of the embodiment of the present invention obtains input operands and format conversion standards. When the input operand is in a first floating-point data format, the input operand is converted into an output operand according to a rounding mode, a second floating-point data format, and various standard output data formats supported by a data format conversion device. When the input operand is in a first integer data format, the input operand is converted into an output operand according to a second integer data format and various output data formats supported by the data format conversion device. This method can simultaneously process input data in floating-point format and integer format, so that the data format conversion device can adapt to data from various sources, improve the versatility of the device, avoid referencing intermediate data formats during the data format conversion process, simplify the data format conversion logic, and solve the problem of large bit width of intermediate data formats in the prior art. Moreover, with a clear conversion standard and supported output data formats, the data format conversion device can perform efficient conversion operations, can quickly convert input operands into required output operands, reduce the time cost of data processing, and improve the performance of the entire computing system.
[0022] It should be understood that the contents described in this section are not intended to identify the key or important features of the embodiments of the present invention, nor are they intended to limit the scope of the present invention. Other features of the present invention will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0024] Figure 1 is a flow chart of a data format conversion method provided according to Embodiment 1 of the present invention;
[0025] Figure 2 is a schematic diagram of a data format conversion device provided according to an embodiment of the present invention;
[0026] Figure 3 is a flowchart of a floating point data format conversion method provided according to Embodiment 2 of the present invention;
[0027] Figure 4 is a schematic diagram of an input operand structure in a floating-point format provided according to an embodiment of the present invention;
[0028] Figure 5 is a schematic diagram of a data structure of a first mantissa provided according to an embodiment of the present invention;
[0029] Figure 6 1 is a schematic diagram of the splitting principle of a mantissa rounding operation provided according to an embodiment of the present invention;
[0030] Figure 7 is a schematic diagram of a structure of an output operand provided according to an embodiment of the present invention;
[0031] Figure 8 is a flowchart of an integer data format conversion method provided according to Embodiment 3 of the present invention;
[0032] Fig. 9 is a schematic diagram of another structure of an output operand provided according to an embodiment of the present invention;
[0033] Fig.10 is a structural schematic diagram of a data format conversion device provided according to a fourth embodiment of the present invention;
[0034] Fig.11 It is a structural schematic diagram of a data format conversion device for implementing the data format conversion method of an embodiment of the present invention. DETAILED DESCRIPTION
[0035] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0036] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0037] Embodiment 1
[0038] Figure 1This is a flow chart of a data format conversion method provided in the first embodiment of the present invention. This embodiment is applicable to the case where input data is converted into a corresponding data format through an independent floating point data conversion method and an integer data conversion method. The method can be executed by a data format conversion device, which can be implemented in the form of hardware and / or software and can generally be configured in a computer or processor with data processing capabilities. Figure 1 As shown, the method includes:
[0039] S110, obtaining input operands and format conversion standards.
[0040] The input operand is in a first floating point data format or a first integer data format.
[0041] Optionally, the input operand is input data to be format converted. When the input operand is in the first floating-point data format, the format conversion standard includes a rounding mode and an output format; when the input operand is in the first integer data format, the format conversion standard includes an output format.
[0042] Optionally, the data format conversion device supports two input data formats, namely, a first floating-point data format and a first integer data format, that is, if the input operand is floating-point data, then the input operand must be in the first floating-point data format, and if the input operand is integer data, then the input operand must be in the first integer data format. Based on the first floating-point data format and the first integer data format, the maximum bit width supported by the data format conversion device can be determined.
[0043] Optionally, the data format conversion device supports multiple standard output data formats, and the standard output data format can be a floating-point data format or an integer data format. However, the precision of each standard output data format cannot exceed the maximum precision in the first floating-point data format or the first integer data format, that is, the precision of the first floating-point data format must be greater than or equal to the precision of each floating-point data format in the standard output data format, and the precision of the first integer data format must be greater than or equal to the precision of each integer data format in the standard output data format.
[0044] Optionally, there is no specific limitation on the first floating-point data format, the first integer data format and the standard output data formats. For example, the first floating-point data format may be FP32, FP64 or other floating-point formats, which is only used for illustrative purposes. The first integer data format may be INT32, INT64, etc., without specific limitation. However, for a single data format conversion device, only one first floating-point data format and one first integer data format can be supported. The maximum bit width supported by the data format conversion device is determined according to the precision of the first floating-point data format and the first integer data format. For example, if the first floating-point data format is FP32 and the first integer data format is INT64, the maximum bit width is 64 bits.
[0045] Continuing with the previous example, when the first floating-point data format is FP32 and the first integer data format is INT64, the precision of the floating-point data format in the standard output data format cannot exceed the precision of FP32, and the precision of the integer data format in the standard output data format cannot exceed the precision of INT64.
[0046] Optionally, the output format may refer to the format of the operand finally output after the input operand is converted into a data format. When the input operand is in a first floating-point data format, the output format may be any floating-point data format in the standard output data formats. When the input operand is in a first integer data format, the output format may be any integer data format in the standard output data formats.
[0047] In an optional example, the standard output data format may be data formats such as FP16, BF16, FP8_E4M3, FP8_E5M2, INT16, and INT8, but this is not limited.
[0048] Optionally, the method may be configured in a data format conversion device, and the data format conversion device may be used as a hardware unit in a circuit. Figure 2 FIG. 1 is a schematic diagram of an optional data format conversion device. Figure 2 As shown, the data format conversion device may include three input signal lines and one output signal line, and each input signal line inputs: input operand, rounding mode and output format respectively. When the input operand is in integer data format, the rounding mode may not be input. The rounding mode may be used to determine how to perform rounding operations during data processing. The output format may be used to specify the data format requirements of the output result.
[0049] Optionally, the floating-point data path 10 can be used to specifically process floating-point data. When the input operand is in floating-point data format, the path will process it accordingly; the integer data path 11 can be used to process input operands in integer data format.
[0050] Optionally, since the result selection 12 simultaneously links the floating-point data path 10 and the integer data path 11 through signal lines, it is necessary to determine through the result selection 12 which signal line has data transmission at present, and transmit the output operand output by the signal line to other devices or systems through the signal line for further processing.
[0051] S120. Determine whether the input operand is in the first floating-point data format or the first integer data format according to the input operand and the format conversion standard; when the input operand is determined to be in the first floating-point data format, execute step S130; when the input operand is in the first integer data format, execute step S140.
[0052] S130, determining a rounding mode and a second floating-point data format according to a format conversion standard, and converting an input operand into an output operand according to the rounding mode, the second floating-point data format, and various standard output data formats supported by the data format conversion device.
[0053] Optionally, when it is determined that the input operand is in the first floating-point data format, the rounding mode and the second floating-point data format are determined according to a format conversion standard, and the second floating-point data format is a data format of a specified output result.
[0054] Optionally, the rounding mode may include, but is not limited to: rounding towards 0, rounding towards positive infinity, rounding towards negative infinity, and rounding towards the nearest even number.
[0055] Optionally, a single floating-point number can be divided into three parts: the sign bit, the exponent bit, and the fraction bit. The sign bit is the highest bit in the data bit. If the sign bit is 0, it means that the floating-point number is positive; if the sign bit is 1, it means that the floating-point number is negative. The exponent bit occupies several bits in the middle. The exponent bits may be different in different floating-point data formats. For example, in FP32, the exponent bit has 8 bits, and the value of the exponent bit needs to be offset to get the actual exponent value. The fraction bit occupies several bits in the lower order. For example, in FP32, the fraction bit has 23 bits, and the fraction bit represents the value of the decimal part. According to the value of the exponent bit, add a 1-bit integer 0 or 1 to the left of the highest bit of the fraction bit to get the mantissa.
[0056] Optionally, by parsing the input operand, the sign, exponent, mantissa and special value detection results can be obtained, and then the sign, exponent, mantissa and special value detection results are processed to obtain the output operand in floating-point data format.
[0057] S140: Determine a second integer data format according to a format conversion standard, and convert an input operand into an output operand according to the second integer data format and each output data format supported by the data format conversion device.
[0058] Optionally, when it is determined that the input operand is in the first integer data format, a second integer data format is determined according to a format conversion standard, and the second integer data format is a data format of a specified output result.
[0059] Optionally, a representable range value of the output format may be determined based on the second integer data format, and a value to be output may be determined based on an input operand and the representable range value of the output format.
[0060] Optionally, since the data format conversion device supports multiple different standard output data formats, and the bit widths of each standard output data format are different, a maximum output bit width that can be output can be determined according to each standard output data format, and when it is determined that the value to be output is less than the maximum output bit width, the value with output is padded to obtain the final output operand.
[0061] The technical solution of the embodiment of the present invention obtains input operands and format conversion standards. When the input operand is in a first floating-point data format, the input operand is converted into an output operand according to a rounding mode, a second floating-point data format, and various standard output data formats supported by a data format conversion device. When the input operand is in a first integer data format, the input operand is converted into an output operand according to a second integer data format and various output data formats supported by the data format conversion device. This method can simultaneously process input data in floating-point format and integer format, so that the data format conversion device can adapt to data from various sources, improve the versatility of the device, avoid referencing intermediate data formats during the data format conversion process, simplify the data format conversion logic, and solve the problem of large bit width of intermediate data formats in the prior art. Moreover, with a clear conversion standard and supported output data formats, the data format conversion device can perform efficient conversion operations, can quickly convert input operands into required output operands, reduce the time cost of data processing, and improve the performance of the entire computing system.
[0062] Embodiment 2
[0063] Figure 3 This is a flow chart of a floating point data format conversion method provided by the second embodiment of the present invention. This embodiment specifically describes the floating point data conversion method based on the above embodiment. Figure 3 As shown, the method includes:
[0064] S210: Obtain input operands and format conversion standards.
[0065] S220: When the input operand is in the first floating-point data format, determine a rounding mode and a second floating-point data format according to a format conversion standard.
[0066] S230. Determine the maximum output mantissa bit width according to each standard output data format, split the input operand, and obtain the sign, first exponent, first mantissa, and special value detection result of the input operand.
[0067] Optionally, splitting the input operand to obtain the sign, the first exponent, the first mantissa, and the special value detection result of the input operand may include:
[0068] Respectively obtain a sign, a first exponent, and a fraction in an input operand, and determine a special value detection result according to the first exponent and the fraction; wherein the special value detection result includes a non-special value, infinity, and non-number;
[0069] Determine the integer bit value of the first mantissa according to the first exponent, and determine the last 0 bit of the first mantissa according to the output maximum mantissa bit width;
[0070] The fraction is padded according to the integer digit value of the first mantissa and the last 0 digits of the first mantissa to obtain the first mantissa.
[0071] Optionally, the input operand in the first floating-point data format may be represented in binary form, and the binary data may be divided into a sign, an exponent, and a fraction in sequence. Figure 4 A schematic diagram of an optional floating-point format input operand structure, such as Figure 4 As shown, S is the sign, E is the exponent, and F is the fraction. By converting the fraction F, the mantissa can be obtained.
[0072] Optionally, the first index may refer to the exponent bits obtained by splitting the input operand, that is, Figure 4 In the E part, the first digit is the data obtained after processing according to the fraction, that is, according to Figure 4 The data obtained after processing part F in .
[0073] Optionally, the special value detection results may include non-special values, infinity and non-number. Non-special values can be understood as no special values are detected, infinity and non-number are cases where special values are detected, and the detection logic of special values is: when the value of the exponent part is the maximum value and the fractional part is 0, infinity is detected, and when the value of the exponent part is the maximum value and the fractional part is not 0, non-number is detected; wherein the value of the exponent part is the maximum value means that all bits in the binary form of the exponent part are 1.
[0074] Optionally, for different floating-point data formats, their bit width, exponent bit width, and mantissa bit width may be different. Therefore, according to each standard output data format, the mantissa bit width of each floating-point format in the standard output data format can be determined, and the maximum value of the mantissa bit width of each floating-point format in the standard output data format can be used as the output maximum mantissa bit width.
[0075] In an alternative example, the maximum mantissa width can be output by WMO max To indicate that when the output floating point format supports FP16, BF16, FP8_E4M3 and FP8_E5M2, the mantissa bit width of the FP16 format in the output floating point format is the largest, which is 11. Therefore, WMO max =11.
[0076] Optionally, the method for generating the first mantissa is: determine the integer place value of the first mantissa according to the first exponent, determine the last zero digits according to the output maximum mantissa bit width, and use the integer place value and the zero value of the last zero digits to pad the fraction to obtain the first mantissa.
[0077] Figure 5 is a schematic diagram of a data structure of an optional first mantissa. Figure 5 As shown, I represents the padded integer, the integer position value is the I value, F represents the fraction in the input operand, and 0 represents a number of redundant 0s added at the last position.
[0078] In an optional implementation, when the first index is 0, the integer bit value is 0, otherwise, the integer bit value is 1.
[0079] In an optional implementation, the last 0 digit is the value obtained by adding 1 to the maximum output digit width. Continuing with the previous example, when the maximum output mantissa width WMO max =11, the last digit is 12, that is, Figure 5 Add 12 zeros after the F shown.
[0080] S240, correcting the first exponent according to the first floating-point data format and the second floating-point data format, obtaining the corrected second exponent, and calculating the mantissa shift value according to the second exponent, the second floating-point data format and the output maximum mantissa bit width.
[0081] Optionally, according to the first floating-point data format and the second floating-point data format, correcting the first exponent, obtaining the corrected second exponent, and calculating the mantissa shift value according to the second exponent, the second floating-point data format, and the output maximum mantissa bit width may include:
[0082] Determine an input exponent bit width and an output exponent bit width according to the first floating-point data format and the second floating-point data format, determine a first offset and a second offset according to the input exponent bit width and the output exponent bit width, and determine a second index according to the first offset, the second offset and the first exponent;
[0083] Determine the current output mantissa bit width according to the second floating-point data format, and calculate the fixed displacement according to the output maximum mantissa bit width and the current output mantissa bit width;
[0084] Calculating a first variable shift value according to the second exponent, and determining a second variable shift value according to the current output mantissa bit width and the first variable shift value;
[0085] A mantissa shift value is calculated according to the first floating point data format, the second floating point data format, the fixed displacement, and the second variable shift value.
[0086] Optionally, according to the above content, different floating-point data formats may have different exponent bit widths. Therefore, the input exponent bit width of the input data and the output exponent bit width of the output data can be determined respectively according to the first floating-point data format and the second floating-point data format.
[0087] In an optional implementation, the first offset may refer to an offset determined according to the input exponent bit width, which may be represented by BI, where BI=2 WE1 -1, where WE1 is the input exponent bit width; the second offset may refer to an offset determined according to the output exponent bit width, which may be represented by BO, BO=2 WE2 -1, where WE2 is the output exponent width.
[0088] Optionally, the second index may refer to an index value after exponential correction is performed on the first index. The second index may be represented by EB, the first index may be represented by EI, and the second index may be calculated by EB=EI-BI+BO.
[0089] Optionally, according to the second floating-point data format, the mantissa bit width of the output data, that is, the current output mantissa bit width, may be determined.
[0090] In an optional implementation, the fixed displacement can be represented by SF, and the current output mantissa width can be represented by WMO, which can be expressed by SF=WMO max - Fixed displacement calculations in WMO.
[0091] In an optional implementation, the first variable shift value may be represented by SV1 and may be calculated by the formula SV1=1-EB.
[0092] Furthermore, the first variable shift value SV1 is clamped to limit the range of the variable shift value, and the clamped second variable shift value SV2 is obtained. SV2 can be determined by the following formula:
[0093]
[0094] Furthermore, the exponent bit width of the input operand can be determined according to the first floating-point data format, and the exponent bit width of the output operand can be determined according to the second floating-point data format. When the exponent bit width of the output operand is smaller than the exponent bit width of the input operand, the mantissa shift value SM=SV2+SF; when the exponent bit width of the output operand is equal to the exponent bit width of the input operand, the mantissa shift value SM=SF.
[0095] S250. According to the mantissa shift value, right shift the first mantissa to generate a second mantissa, and round the second mantissa according to the rounding mode and the maximum output mantissa bit width to obtain a third mantissa.
[0096] It can be understood that, because enough 0s have been added to the end when the first mantissa is generated and the shift value has been clamped, the mantissa after the right shift will not lose any valid data bits.
[0097] Optionally, rounding the second mantissa according to the rounding mode and the maximum mantissa bit width of the output mantissa to obtain the third mantissa may include:
[0098] Determine an input operand bit width according to the first floating-point data format, and determine a mantissa valid value and a carry value according to the output maximum mantissa bit width, the input operand bit width, the second mantissa, and the rounding mode;
[0099] A third mantissa is obtained according to the carry value and the mantissa effective value.
[0100] Figure 6 Figure 1 is a schematic diagram of the splitting principle of an optional mantissa rounding operation. Adding 1 bit of 0 to the highest bit of the second mantissa will yield Figure 6 The data arrangement shown, Figure 6 The total data width shown is WMI+WMO max +2bits, where WMI is the input operand width. Figure 6 The data contained in lbit, rbit, and sbits, as well as the input rounding mode, can calculate the carry value. The carry value can only be 1 or 0. After obtaining the carry value, Figure 6 The data contained in valid is added to the carry value to obtain the third mantissa. Figure 6The 1b in WMI represents 1 bit, WMI b represents the data bit that is equal to the input operand bit width, and WMO max -1b indicates that the number of data bits is equal to the maximum output mantissa bit width minus 1.
[0101] Optionally, the carry value is calculated according to the data and the rounding mode. This is a prior art and will not be described in detail here.
[0102] S260. According to the second exponent and the second floating-point data format, the second exponent is corrected to obtain a third exponent, and an output operand is determined according to the third exponent, the third mantissa, the sign, and the special value detection result.
[0103] Optionally, correcting the second exponent according to the second exponent and the second floating-point data format to obtain a third exponent, and determining an output operand according to the third exponent, the third mantissa, the sign, and the special value detection result may include:
[0104] Determine a current output mantissa bit width according to the second floating-point data format, and determine a value of a target data bit in a third mantissa according to the current output mantissa bit width;
[0105] According to the value of the target data bit, determining whether the second index needs to be corrected, and determining the third index according to the determination result;
[0106] Determine the output value according to the third exponent, the third mantissa, the sign and the special value detection result, and determine the maximum output bit width according to each standard output data format;
[0107] An output operand is determined according to the output value and the output maximum bit width.
[0108] Optionally, the target data bit can be the WMO bit or WMO-1bit in the third mantissa. When the second exponent is 0, determine whether the WMO-1bit is 1. If so, the second exponent needs to be +1, otherwise not. When the second exponent is not 0, determine whether the WMO bit is 1. If so, the second exponent needs to be +1, otherwise not, to obtain the corrected third exponent.
[0109] Optionally, when infinity or non-number is detected in the special value detection result, the infinity and non-number in the corresponding format are directly output according to the second floating-point data format. If the output format cannot represent infinity or non-number, it can be saturated to the maximum normalized number or 0.
[0110] Optionally, if infinity or non-number is not detected, determine whether the corrected third exponent is greater than or equal to the maximum exponent 2 WEO-1, if it is greater than the maximum exponent, the output is saturated to infinity or the maximum normalized number according to the sign and rounding mode. If the corrected exponent is less than the maximum exponent, the sign, the low WEO bits of the third exponent, and the low WMO bits of the third mantissa are packed into the output floating point number, that is, the output value.
[0111] Optionally, after determining the output value, it is necessary to determine the output operand according to the maximum output bit width. If the maximum output bit width is greater than the bit width of the output value, the redundant high bits are filled with 0. Figure 7 is a schematic diagram of the structure of an optional output operand, such as Figure 7 As shown, the output value is in the FP8_E4M3 format, but the maximum output bit width is 16 bits. Therefore, 8 zeros need to be added to the highest bit of the output value.
[0112] The technical solution of the embodiment of the present invention is to split the input operand, obtain the sign of the input operand, the first exponent, the first mantissa and the special value detection result, correct the first exponent according to the first floating-point data format and the second floating-point data format, obtain the corrected second exponent, and calculate the mantissa shift value according to the second exponent, the second floating-point data format and the output maximum mantissa bit width, perform right shift on the first mantissa according to the mantissa shift value, generate the second mantissa, and round the second mantissa according to the rounding mode and the output maximum mantissa bit width to obtain the third mantissa, and calculate the mantissa shift value according to the second exponent and the second floating-point data format. Point data format, correct the second index, obtain the third index, and determine the output operand according to the third index, the third mantissa, the sign and the special value detection result. In the process of data format conversion, it is possible to avoid referring to the intermediate data format, simplify the data format conversion logic, and solve the problem of large bit width of the intermediate data format in the prior art. In addition, with clear conversion standards and supported output data formats, the data format conversion device can perform efficient conversion operations, quickly convert input operands into required output operands, reduce the time cost of data processing, and improve the performance of the entire computing system.
[0113] Embodiment 3
[0114] Figure 8 This is a flowchart of a method for converting integer data format provided by Embodiment 3 of the present invention. Based on the above embodiments, this embodiment specifically describes a method for converting integer data. Figure 8 As shown, the method includes:
[0115] S310: Obtain input operands and format conversion standards.
[0116] S320: When the input operand is in the first integer data format, determine the second integer data format according to the format conversion standard.
[0117] S330. Determine a representable range of the output format according to the second integer data format, and determine an output value according to the input operand and the representable range of the output format.
[0118] Optionally, when the input operand exceeds the representable range of the output integer format, the input operand needs to be saturated. When the value of the input integer operand exceeds the representable range of the output format, it is saturated to the nearest representable value of the output format.
[0119] In an optional example, when the format of the input operand is INT32 and the output format is INT8, the output format can represent a range of [-128, 127]. If the value of the input operand is -300, it is saturated to -128. If the value of the input operand is 211, it is saturated to 127. If the value of the input operand is 100, the output is still 100.
[0120] S340, determining the maximum output bit width according to each standard output data format, and determining the output operand according to the output value and the maximum output bit width.
[0121] Optionally, after determining the output value, it is necessary to determine the output operand according to the maximum output bit width. If the maximum output bit width is greater than the bit width of the output value, the redundant high bits are filled with 0. Fig. 9 is a schematic diagram of the structure of an optional output operand, such as Fig. 9 As shown, the output value is in INT8 format, but the maximum output bit width is 16 bits. Therefore, 8 zeros need to be added to the highest bit of the output value.
[0122] The technical solution of the embodiment of the present invention determines the representable range of the output format according to the second integer data format, determines the output value according to the input operand and the representable range of the output format, determines the maximum output bit width according to each standard output data format, and determines the output operand according to the output value and the maximum output bit width. This can avoid referencing intermediate data formats during data format conversion, simplify the data format conversion logic, and solve the problem of large intermediate data format bit width in the prior art. Moreover, with clear conversion standards and supported output data formats, the data format conversion device can perform efficient conversion operations, can quickly convert input operands into required output operands, reduce the time cost of data processing, and improve the performance of the entire computing system.
[0123] Embodiment 4
[0124] Fig.10 This is a schematic diagram of the structure of a data format conversion device provided by Embodiment 4 of the present invention. Fig.10As shown, the device includes: an input data acquisition module 410 and a data format conversion module 420.
[0125] The input data acquisition module 410 is used to acquire input operands and a format conversion standard; wherein the input operands are in a first floating point data format or a first integer data format.
[0126] The data format conversion module 420 is configured to perform any of the following according to the input operand and the format conversion standard:
[0127] When the input operand is in the first floating point data format, determining a rounding mode and a second floating point data format according to the format conversion standard, and converting the input operand into an output operand according to the rounding mode, the second floating point data format, and each standard output data format supported by the data format conversion device;
[0128] When the input operand is in the first integer data format, the second integer data format is determined according to the format conversion standard, and the input operand is converted into an output operand according to the second integer data format and each output data format supported by the data format conversion device.
[0129] The technical solution of the embodiment of the present invention obtains input operands and format conversion standards. When the input operand is in a first floating-point data format, the input operand is converted into an output operand according to a rounding mode, a second floating-point data format, and various standard output data formats supported by a data format conversion device. When the input operand is in a first integer data format, the input operand is converted into an output operand according to a second integer data format and various output data formats supported by the data format conversion device. This method can simultaneously process input data in floating-point format and integer format, so that the data format conversion device can adapt to data from various sources, improve the versatility of the device, avoid referencing intermediate data formats during the data format conversion process, simplify the data format conversion logic, and solve the problem of large bit width of intermediate data formats in the prior art. Moreover, with a clear conversion standard and supported output data formats, the data format conversion device can perform efficient conversion operations, can quickly convert input operands into required output operands, reduce the time cost of data processing, and improve the performance of the entire computing system.
[0130] Based on the above embodiments, the data format conversion module 420 may include a floating point data conversion submodule and an integer data conversion submodule.
[0131] Based on the above embodiments, the floating point data conversion submodule may include:
[0132] A floating point data splitting unit, used to determine the maximum output mantissa bit width according to each standard output data format, and split the input operand to obtain the sign, first exponent, first mantissa and special value detection result of the input operand;
[0133] a mantissa shift value calculation unit, configured to correct the first exponent according to the first floating-point data format and the second floating-point data format, obtain a second exponent after the correction, and calculate a mantissa shift value according to the second exponent, the second floating-point data format, and the output maximum mantissa bit width;
[0134] a mantissa processing unit, configured to perform a right shift on the first mantissa according to the mantissa shift value to generate a second mantissa, and round the second mantissa according to the rounding mode and the maximum output mantissa bit width to obtain a third mantissa;
[0135] The output operand determination unit is used to correct the second exponent according to the second exponent and the second floating-point data format, obtain a third exponent, and determine the output operand according to the third exponent, the third mantissa, the sign and the special value detection result.
[0136] Based on the above embodiments, the floating point data splitting unit can be specifically used for:
[0137] Respectively obtain a sign, a first exponent, and a fraction in an input operand, and determine a special value detection result according to the first exponent and the fraction; wherein the special value detection result includes a non-special value, infinity, and non-number;
[0138] Determine the integer bit value of the first mantissa according to the first exponent, and determine the last 0 bit of the first mantissa according to the output maximum mantissa bit width;
[0139] The fraction is padded according to the integer digit value of the first mantissa and the last 0 digits of the first mantissa to obtain the first mantissa.
[0140] Based on the above embodiments, the mantissa shift value calculation unit can be specifically used for:
[0141] Determine an input exponent bit width and an output exponent bit width according to the first floating-point data format and the second floating-point data format, determine a first offset and a second offset according to the input exponent bit width and the output exponent bit width, and determine a second index according to the first offset, the second offset and the first exponent;
[0142] Determine the current output mantissa bit width according to the second floating-point data format, and calculate the fixed displacement according to the output maximum mantissa bit width and the current output mantissa bit width;
[0143] Calculating a first variable shift value according to the second exponent, and determining a second variable shift value according to the current output mantissa bit width and the first variable shift value;
[0144] A mantissa shift value is calculated according to the first floating point data format, the second floating point data format, the fixed displacement, and the second variable shift value.
[0145] Based on the above embodiments, the mantissa processing unit can be specifically used for:
[0146] Determine an input operand bit width according to the first floating-point data format, and determine a mantissa valid value and a carry value according to the output maximum mantissa bit width, the input operand bit width, the second mantissa, and the rounding mode;
[0147] A third mantissa is obtained according to the carry value and the mantissa effective value.
[0148] Based on the above embodiments, the output operand determination unit may be specifically used for:
[0149] Determine a current output mantissa bit width according to the second floating-point data format, and determine a value of a target data bit in a third mantissa according to the current output mantissa bit width;
[0150] According to the value of the target data bit, determining whether the second index needs to be corrected, and determining the third index according to the determination result;
[0151] Determine the output value according to the third exponent, the third mantissa, the sign and the special value detection result, and determine the maximum output bit width according to each standard output data format;
[0152] An output operand is determined according to the output value and the output maximum bit width.
[0153] Based on the above embodiments, the integer data conversion submodule can be specifically used for:
[0154] Determine the representable range of the output format according to the second integer data format, and determine the output value according to the input operand and the representable range of the output format;
[0155] According to each standard output data format, the maximum output bit width is determined, and according to the output value and the maximum output bit width, the output operand is determined.
[0156] The data format conversion device provided in the embodiment of the present invention can execute the data format conversion method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0157] Embodiment 5
[0158] Fig.11 The structural diagram of the data format conversion device 10 that can be used to implement an embodiment of the present invention is shown. The data format conversion device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The data format conversion device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are only examples, and are not intended to limit the implementation of the present invention described and / or required herein.
[0159] like Fig.11 As shown, the data format conversion device 10 includes at least one processor 11, and a memory connected to the at least one processor 11, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., wherein the memory stores a computer program that can be executed by at least one processor, and the processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 to the random access memory (RAM) 13. In the RAM 13, various programs and data required for the operation of the data format conversion device 10 can also be stored. The processor 11, the ROM 12, and the RAM 13 are connected to each other through a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.
[0160] A number of components in the data format conversion device 10 are connected to the I / O interface 15, including: an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the data format conversion device 10 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0161] The processor 11 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The processor 11 executes the various methods and processes described above, such as the data format conversion method described in any embodiment of the invention. That is:
[0162] Obtaining an input operand and a format conversion standard; wherein the input operand is in a first floating point data format or a first integer data format;
[0163] According to the input operand and the format conversion standard, perform any of the following:
[0164] When the input operand is in the first floating point data format, determining a rounding mode and a second floating point data format according to the format conversion standard, and converting the input operand into an output operand according to the rounding mode, the second floating point data format, and each standard output data format supported by the data format conversion device;
[0165] When the input operand is in the first integer data format, the second integer data format is determined according to the format conversion standard, and the input operand is converted into an output operand according to the second integer data format and each output data format supported by the data format conversion device.
[0166] In some embodiments, the data format conversion method may be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as a storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on the data format conversion device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps of the data format conversion method described above may be performed. Alternatively, in other embodiments, the processor 11 may be configured to perform the data format conversion method in any other appropriate manner (e.g., by means of firmware).
[0167] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0168] Computer programs for implementing the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that when the computer program is executed by the processor, the functions / operations specified in the flow chart and / or block diagram are implemented. The computer program may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.
[0169] In the context of the present invention, a computer-readable storage medium may be a tangible medium that may contain or store a computer program for use by or in combination with an instruction execution system, device or equipment. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0170] To provide interaction with a user, the systems and techniques described herein may be implemented on a data format conversion device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or trackball) through which the user can provide input to the data format conversion device. Other types of devices may also be used to provide interaction with the user; for example, the feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form (including acoustic input, voice input, or tactile input).
[0171] The systems and techniques described herein may be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.
[0172] A computing system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The client and server relationship is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services.
[0173] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps described in the present invention can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solution of the present invention can be achieved, and this document does not limit this.
[0174] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A data format conversion method, performed by a data format conversion device, characterized in that: include: Obtaining an input operand and a format conversion standard; wherein the input operand is in a first floating point data format or a first integer data format; According to the input operand and the format conversion standard, perform any of the following: When the input operand is in the first floating point data format, determining a rounding mode and a second floating point data format according to the format conversion standard, and converting the input operand into an output operand according to the rounding mode, the second floating point data format, and each standard output data format supported by the data format conversion device; When the input operand is in the first integer data format, the second integer data format is determined according to the format conversion standard, and the input operand is converted into an output operand according to the second integer data format and each output data format supported by the data format conversion device.
2. The method according to claim 1, characterized in that Converting an input operand into an output operand according to the rounding mode, the second floating-point data format, and each standard output data format supported by the data format conversion device includes: According to each standard output data format, determine the maximum output mantissa bit width, split the input operand, and obtain the sign, first exponent, first mantissa and special value detection result of the input operand; According to the first floating-point data format and the second floating-point data format, the first exponent is corrected to obtain a second exponent after the correction, and a mantissa shift value is calculated according to the second exponent, the second floating-point data format, and the output maximum mantissa bit width; According to the mantissa shift value, right shift the first mantissa to generate a second mantissa, and round the second mantissa according to the rounding mode and the output maximum mantissa bit width to obtain a third mantissa; According to the second exponent and the second floating-point data format, the second exponent is skewed to obtain a third exponent, and an output operand is determined according to the third exponent, the third mantissa, the sign, and the special value detection result.
3. The method according to claim 2, characterized in that The input operand is split to obtain a sign, a first exponent, a first mantissa, and a special value detection result of the input operand, including: Respectively obtain a sign, a first exponent, and a fraction in an input operand, and determine a special value detection result according to the first exponent and the fraction; wherein the special value detection result includes a non-special value, infinity, and non-number; Determine the integer bit value of the first mantissa according to the first exponent, and determine the last 0 bit of the first mantissa according to the output maximum mantissa bit width; The fraction is padded according to the integer digit value of the first mantissa and the last 0 digits of the first mantissa to obtain the first mantissa.
4. The method according to claim 2, characterized in that: According to the first floating-point data format and the second floating-point data format, the first exponent is corrected to obtain a second exponent after the correction, and according to the second exponent, the second floating-point data format and the output maximum mantissa bit width, a mantissa shift value is calculated, including: Determine an input exponent bit width and an output exponent bit width according to the first floating-point data format and the second floating-point data format, determine a first offset and a second offset according to the input exponent bit width and the output exponent bit width, and determine a second index according to the first offset, the second offset and the first exponent; Determine the current output mantissa bit width according to the second floating-point data format, and calculate the fixed displacement according to the output maximum mantissa bit width and the current output mantissa bit width; Calculating a first variable shift value according to the second exponent, and determining a second variable shift value according to the current output mantissa bit width and the first variable shift value; A mantissa shift value is calculated according to the first floating point data format, the second floating point data format, the fixed displacement, and the second variable shift value.
5. The method according to claim 2, characterized in that: According to the rounding mode and the maximum output mantissa bit width, the second mantissa is rounded to obtain a third mantissa, including: Determine an input operand bit width according to the first floating-point data format, and determine a mantissa valid value and a carry value according to the output maximum mantissa bit width, the input operand bit width, the second mantissa, and the rounding mode; A third mantissa is obtained according to the carry value and the mantissa effective value.
6. The method according to claim 2, characterized in that According to the second exponent and the second floating-point data format, the second exponent is corrected to obtain a third exponent, and according to the third exponent, the third mantissa, the sign, and the special value detection result, an output operand is determined, including: Determine a current output mantissa bit width according to the second floating-point data format, and determine a value of a target data bit in a third mantissa according to the current output mantissa bit width; According to the value of the target data bit, determining whether the second index needs to be corrected, and determining the third index according to the determination result; Determine the output value according to the third exponent, the third mantissa, the sign and the special value detection result, and determine the maximum output bit width according to each standard output data format; An output operand is determined according to the output value and the output maximum bit width.
7. The method according to claim 1, characterized in that Converting an input operand into an output operand according to the second integer data format and each standard output data format supported by the data format conversion device includes: Determine the representable range of the output format according to the second integer data format, and determine the output value according to the input operand and the representable range of the output format; According to each standard output data format, the maximum output bit width is determined, and according to the output value and the maximum output bit width, the output operand is determined.
8. A data format conversion device, executed by a data format conversion device, characterized in that: include: An input data acquisition module, used to acquire an input operand and a format conversion standard; wherein the input operand is in a first floating point data format or a first integer data format; A data format conversion module is used to perform any of the following according to the input operand and the format conversion standard: When the input operand is in the first floating point data format, determining a rounding mode and a second floating point data format according to the format conversion standard, and converting the input operand into an output operand according to the rounding mode, the second floating point data format, and each standard output data format supported by the data format conversion device; When the input operand is in the first integer data format, the second integer data format is determined according to the format conversion standard, and the input operand is converted into an output operand according to the second integer data format and each output data format supported by the data format conversion device.
9. A data format conversion device, characterized in that: The data format conversion device comprises: at least one processor; and a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the data format conversion method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the data format conversion method according to any one of claims 1 to 7 when executed.
Citation Information
Cited By
Floating-point number format conversion device and method, storage medium and program product
CN121657960A
Floating-point number format conversion apparatus and method, storage medium and program products
CN121657960B