Data conversion method, apparatus, device, and medium

CN122331859BActive Publication Date: 2026-09-18MOORE THREADS TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202610805565.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-06-05
Publication Date
2026-09-18
Estimated Expiration
2046-06-05

AI Technical Summary

Technical Problem

然而,当数值落入低精度浮点格式对应的非规格数区间时,由于该区间的数值表示方式不同于规格数区间,原有加法随机舍入方式难以准确保持无偏舍入,容易引入偏差

Benefits of technology

[0030] In the data conversion method of the example embodiments of this disclosure, on the one hand, by first determining the target numerical range in which the absolute value corresponding to the source format floating-point number falls in the target format, it is possible to identify non-standard number ranges. When the identification result shows that the target numerical range is a non-standard number range, rounding processing adapted to the non-standard number range is performed. On the other hand, when the target numerical range is a non-standard number range, determining the first candidate value and the second candidate value adjacent to the absolute value in the non-standard number range enables the target format floating-point number after conversion from the source format floating-point number to take values ​​only from the first candidate value and the second candidate value, thereby reducing rounding deviation in the non-standard number range. Furthermore, by performing random rounding processing based on the relative position of the source format floating-point number in the local range defined by the first candidate value and the second candidate value, the random rounding corresponds to the positional relationship of the absolute value corresponding to the source format floating-point number in the local range, thereby reducing rounding deviation in the non-standard number range when converting the source format floating-point number to the target format.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122331859B_ABST
    Figure CN122331859B_ABST
Patent Text Reader

Abstract

This disclosure provides a data conversion method, apparatus, device, and medium, relating to the field of computer technology. The method includes: obtaining a source-format floating-point number; determining the target numerical interval in which the absolute value of the source-format floating-point number falls within the target format; ensuring the precision of the source-format floating-point number is greater than the precision of the target-format floating-point number; if the target numerical interval is an undefined number interval, determining a target sub-interval from at least two sub-intervals contained within the undefined number interval, and determining a first candidate value and a second candidate value adjacent to the absolute value within the target sub-interval; and performing random rounding on the source-format floating-point number based on its relative position within the local interval defined by the first and second candidate values ​​to obtain the target-format floating-point number. This solution enables random rounding based on interval positional relationships, reducing rounding deviations within undefined number intervals when converting source-format floating-point numbers to the target format.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of computer technology, and more specifically, to a data conversion method, apparatus, device, and medium. Background Technology

[0002] When converting high-precision floating-point numbers to low-precision floating-point numbers, the low-precision floating-point format has limited representation precision, making it difficult to accurately represent high-precision floating-point numbers. Therefore, rounding is required during the conversion process.

[0003] In actual conversion processes, some related technologies employ additive random rounding. This involves first summing the source-format floating-point number with a random number, then performing a format conversion based on the sum to achieve random rounding up or down. However, when the value falls into the non-standard number range corresponding to the low-precision floating-point format, the original additive random rounding method struggles to accurately maintain unbiased rounding because the numerical representation in this range differs from that in the standard number range, easily introducing deviations. Summary of the Invention

[0004] The purpose of this disclosure is to provide a data conversion method, data conversion device, electronic device, and computer-readable storage medium that can perform random rounding based on interval position relationships, thereby reducing rounding deviation in non-standard number intervals when converting source format floating-point numbers to target format.

[0005] Other features and advantages of this disclosure will become apparent from the following detailed description, or may be learned in part by practice of this disclosure.

[0006] According to a first aspect of the present disclosure, a data conversion method is provided, the method comprising: obtaining a source format floating-point number; determining a target numerical interval in a target format into which the absolute value corresponding to the source format floating-point number falls; wherein the precision of the source format floating-point number is greater than the precision of the target format floating-point number, the target numerical interval is one of a plurality of numerical intervals in the target format, the plurality of numerical intervals including an unstandardized number interval; when the target numerical interval is an unstandardized number interval, determining a target sub-interval from at least two sub-intervals contained in the unstandardized number interval, determining a first candidate value and a second candidate value adjacent to the absolute value in the target sub-interval; performing random rounding on the source format floating-point number according to the relative position of the source format floating-point number in the local interval defined by the first candidate value and the second candidate value, to obtain a target format floating-point number, wherein the random rounding method is different for different numerical intervals; wherein the source format floating-point number includes: floating-point data to be converted in format during the training of a neural network model, the target format floating-point number is used for training calculation of the neural network model; the neural network model is used to perform any one of image processing tasks, speech processing tasks, and natural language processing tasks.

[0007] In some example embodiments of this disclosure, based on the foregoing scheme, the above-mentioned random rounding of the source format floating-point number according to its relative position in the local interval defined by the first candidate value and the second candidate value to obtain the target format floating-point number includes: determining the reference probability corresponding to the random rounding according to the relative position of the source format floating-point number in the local interval defined by the first candidate value and the second candidate value; obtaining the random probability; wherein the range of the random probability is determined according to the range of the reference probability; determining the target representation value from the first candidate value and the second candidate value according to the comparison result of the random probability and the reference probability; and generating the target format floating-point number corresponding to the source format floating-point number according to the target representation value.

[0008] In some example embodiments of this disclosure, based on the foregoing scheme, determining the reference probability corresponding to random rounding according to the relative position of the source-format floating-point number in the local interval defined by the first candidate value and the second candidate value includes: determining the difference between the source-format floating-point number and the first candidate value, and determining the reference probability corresponding to random rounding according to the ratio of the difference to the interval length of the local interval.

[0009] In some example embodiments of this disclosure, based on the aforementioned scheme, the reference probability determination methods for random rounding processing corresponding to the different sub-intervals are different; random rounding processing of the source format floating-point number includes: determining the reference probability corresponding to the random rounding processing according to the reference probability determination method corresponding to the target sub-interval; and performing random rounding processing of the source format floating-point number using the reference probability; wherein, the reference probability is used to determine the probability of selecting the first candidate value or the second candidate value when performing random rounding processing of the source format floating-point number.

[0010] In some example embodiments of this disclosure, based on the aforementioned scheme, the target format is a 4-bit floating-point number, and the target sub-interval includes a first non-standard number sub-interval [0, 0.5); when the target value interval is the first non-standard number sub-interval, the first candidate value is one of 0 and 0.5, and the second candidate value is the other; according to the relative position of the source format floating-point number in the local interval defined by the first and second candidate values, the source format floating-point number is randomly rounded to obtain the target format floating-point number, including: determining the sign value, mantissa, and exponent value of the source format floating-point number; generating a reference probability corresponding to random rounding based on the mantissa and exponent values; determining the target representation value from 0 and 0.5 based on the reference probability; and generating the target format floating-point number corresponding to the source format floating-point number based on the target representation value and sign value.

[0011] In some example embodiments of this disclosure, based on the aforementioned scheme, the source format floating-point number is a specification number; generating a reference probability corresponding to random rounding based on the mantissa and exponent value includes: concatenating the mantissa with a hidden bit of value 1 to obtain a significant number; determining a shift amount based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format; shifting the significant number based on the shift amount to obtain the reference probability corresponding to random rounding.

[0012] In some example embodiments of this disclosure, based on the aforementioned scheme, determining the shift amount according to the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format includes: subtracting the exponent bias value corresponding to the source format from the exponent value, and then adding it to the exponent bias value corresponding to the target format to obtain the shift amount.

[0013] In some example embodiments of this disclosure, based on the foregoing scheme, the above-mentioned source format floating-point number is an unstandardized number; determining the reference probability corresponding to random rounding based on the mantissa and exponent value includes: determining the integer value and mantissa digits corresponding to the mantissa; determining the reference probability corresponding to random rounding based on the exponent value, the integer value, and the mantissa digits.

[0014] In some example embodiments of this disclosure, based on the foregoing scheme, the above-mentioned determination of the target representation value from 0 and 0.5 according to the reference probability includes: generating a first random number; wherein the bit width of the first random number is the same as the bit width of the reference probability; and determining the target representation value from 0 and 0.5 according to the sum of the first random number and the reference probability.

[0015] In some example embodiments of this disclosure, based on the foregoing scheme, determining the target representation value from 0 and 0.5 according to the sum of the first random number and the reference probability includes: adding the first random number and the reference probability to obtain a first sum value; determining the carry result corresponding to the first sum value; determining the target representation value as 0.5 if the carry result is a carry; and determining the target representation value as 0 if the carry result is no carry.

[0016] In some example embodiments of this disclosure, based on the foregoing scheme, the target sub-interval includes a second non-standard number sub-interval [0.5, 1]; when the target value interval is the second non-standard number sub-interval, the first candidate value is one of 0.5 and 1, and the second candidate value is the other; according to the relative position of the source format floating-point number in the local interval defined by the first candidate value and the second candidate value, the source format floating-point number is randomly rounded to obtain the target format floating-point number, including: determining the mantissa and sign value of the source format floating-point number; determining the target representation value from 0.5 and 1 based on the mantissa; and generating the target format floating-point number corresponding to the source format floating-point number based on the target representation value and the sign value.

[0017] In some example embodiments of this disclosure, based on the foregoing scheme, the above-mentioned method of determining the target representation value from 0.5 and 1 according to the tail value includes: generating a second random number; wherein the bit width of the second random number is the same as the bit width of the tail value; and determining the target representation value from 0.5 and 1 according to the sum of the second random number and the tail value.

[0018] In some example embodiments of this disclosure, based on the foregoing scheme, the above-mentioned determination of the target representation value from 0.5 and 1 based on the sum of the second random number and the tail value includes: adding the second random number and the tail value to obtain a second sum value; determining the carry result corresponding to the second sum value; determining the target representation value as 1 if the carry result is a carry; and determining the target representation value as 0.5 if the carry result is no carry.

[0019] In some example embodiments of this disclosure, based on the foregoing scheme, the aforementioned multiple numerical intervals also include specification number intervals; the method further includes: when the target numerical interval is a specification number interval, determining the mantissa truncation value of the source format floating-point number relative to the target format; wherein, the mantissa truncation value represents the low-order mantissa field in the source format floating-point number that exceeds the mantissa bit width of the target format; generating a third random number; wherein, the bit width of the third random number is the same as the bit width of the mantissa truncation value; and generating the target format floating-point number corresponding to the source format floating-point number based on the sum of the mantissa truncation value and the third random number.

[0020] In some example embodiments of this disclosure, based on the foregoing scheme, the above-mentioned method of generating a target format floating-point number corresponding to a source format floating-point number by adding the mantissa truncation value and a third random number includes: adding the mantissa truncation value and the third random number to obtain a third sum value; shifting the third sum value according to the bit width of the mantissa truncation value to obtain a target representation value; and generating a target format floating-point number corresponding to a source format floating-point number based on the target representation value.

[0021] In some example embodiments of this disclosure, based on the foregoing scheme, the above-mentioned acquisition of source format floating-point numbers includes: during the training process of a neural network model, acquiring floating-point data to be converted as the source format floating-point number; wherein, the target format floating-point number obtained by converting the source format floating-point number is used for training calculation of the neural network model.

[0022] In some example embodiments of this disclosure, based on the foregoing scheme, the above-mentioned determination of the target numerical range in which the absolute value corresponding to the source format floating-point number falls in the target format includes: determining the exponent value of the source format floating-point number; and determining the target numerical range in which the absolute value corresponding to the source format floating-point number falls in the target format based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format.

[0023] According to a second aspect of the present disclosure, a data conversion apparatus is provided. The apparatus includes: an interval determination circuit, configured to acquire a source format floating-point number and determine a target numerical interval in a target format in which the absolute value corresponding to the source format floating-point number falls; wherein the precision of the source format floating-point number is greater than the precision of the target format floating-point number, and the target numerical interval is one of a plurality of numerical intervals included in the target format, the plurality of numerical intervals including an unstandardized number interval; an unstandardized number conversion circuit, configured to determine a target sub-interval from at least two sub-intervals included in the unstandardized number interval, and determine a first candidate value and a second candidate value adjacent to the absolute value in the target sub-interval; and to perform random rounding on the source format floating-point number according to the relative position of the source format floating-point number in the local interval defined by the first candidate value and the second candidate value to obtain a target format floating-point number, wherein the random rounding method is different for different numerical intervals; wherein the source format floating-point number includes: floating-point data to be format converted during the training of a neural network model, and the target format floating-point number is used for training calculation of the neural network model; the neural network model is used to perform any one of image processing tasks, speech processing tasks, and natural language processing tasks.

[0024] In some example embodiments of this disclosure, based on the foregoing scheme, the target format is a 4-bit floating-point number, and the non-standard number interval includes a first non-standard number sub-interval [0, 0.5); when the target value interval is the first non-standard number sub-interval, the first candidate value is one of 0 and 0.5, and the second candidate value is the other; the non-standard number conversion circuit includes: a first non-standard number conversion sub-circuit, used to determine the sign value, mantissa, and exponent value of the source format floating-point number when the target value interval is the first non-standard number sub-interval; generate a reference probability corresponding to random rounding based on the mantissa and exponent value; determine the target representation value from 0 and 0.5 based on the reference probability; and generate the target format floating-point number corresponding to the source format floating-point number based on the target representation value and the sign value.

[0025] In some example embodiments of this disclosure, based on the foregoing scheme, the aforementioned non-standard number interval further includes a second non-standard number sub-interval [0.5, 1]; when the target value interval is the second non-standard number sub-interval, the first candidate value is one of 0.5 and 1, and the second candidate value is the other; the non-standard number conversion circuit further includes: a second non-standard number conversion sub-circuit, used to determine the mantissa of the source format floating-point number; determine the target representation value from 0.5 and 1 based on the mantissa; and generate the target format floating-point number corresponding to the source format floating-point number based on the target representation value and the sign value.

[0026] In some example embodiments of this disclosure, based on the foregoing scheme, the aforementioned multiple numerical intervals further include specification number intervals; the apparatus further includes: a specification number conversion circuit, used to determine the mantissa truncation value of the source format floating-point number relative to the target format when the target numerical interval is a specification number interval; wherein, the mantissa truncation value represents the low-order mantissa field of the source format floating-point number that exceeds the mantissa bit width of the target format; generate a third random number; wherein, the bit width of the third random number is the same as the bit width of the mantissa truncation value; and generate a target format floating-point number corresponding to the source format floating-point number based on the sum of the mantissa truncation value and the third random number.

[0027] According to a third aspect of the present disclosure, an electronic device is provided, including: a processor; and a memory storing computer-readable instructions, which, when executed by the processor, implement the data conversion method as described in the first aspect.

[0028] According to a fourth aspect of the present disclosure, a computer-readable storage medium is provided, on which a computer program is stored, which, when executed by a processor, implements the data conversion method as described in the first aspect.

[0029] The technical solutions provided in this disclosure may have the following beneficial effects:

[0030] In the data conversion method of the example embodiments of this disclosure, on the one hand, by first determining the target numerical range in which the absolute value corresponding to the source format floating-point number falls in the target format, it is possible to identify non-standard number ranges. When the identification result shows that the target numerical range is a non-standard number range, rounding processing adapted to the non-standard number range is performed. On the other hand, when the target numerical range is a non-standard number range, determining the first candidate value and the second candidate value adjacent to the absolute value in the non-standard number range enables the target format floating-point number after conversion from the source format floating-point number to take values ​​only from the first candidate value and the second candidate value, thereby reducing rounding deviation in the non-standard number range. Furthermore, by performing random rounding processing based on the relative position of the source format floating-point number in the local range defined by the first candidate value and the second candidate value, the random rounding corresponds to the positional relationship of the absolute value corresponding to the source format floating-point number in the local range, thereby reducing rounding deviation in the non-standard number range when converting the source format floating-point number to the target format.

[0031] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this disclosure. Attached Figure Description

[0032] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure. It is obvious that the drawings described below are merely some embodiments of this disclosure, and those skilled in the art can obtain other drawings based on these drawings without any inventive effort.

[0033] Figure 1 The illustration shows a schematic diagram of an additive random rounding method in a related technique.

[0034] Figure 2 The illustration shows a schematic flowchart of a data conversion method according to some embodiments of the present disclosure.

[0035] Figure 3 The schematic diagram illustrates a flow chart of a random rounding process according to some embodiments of the present disclosure.

[0036] Figure 4 The illustration schematically shows a process for converting BF16 to FP4 in a first non-standard number sub-interval according to some embodiments of the present disclosure.

[0037] Figure 5 The illustration schematically shows a process for converting FP32 to FP4 in a first non-standard number sub-interval according to some embodiments of the present disclosure.

[0038] Figure 6 The illustration schematically shows a process for converting BF16 to FP4 in a second non-standard number sub-interval according to some embodiments of the present disclosure.

[0039] Figure 7 The illustration schematically shows a process for converting FP32 to FP4 in a second non-standard number sub-interval according to some embodiments of the present disclosure.

[0040] Figure 8 The illustration schematically shows a process for converting BF16 to FP4 in a specification number sub-interval according to some embodiments of the present disclosure.

[0041] Figure 9 The illustration shows a schematic diagram of the process of converting FP32 to FP4 in a specification number sub-interval according to some embodiments of the present disclosure.

[0042] Figure 10 A schematic block diagram of a data conversion apparatus according to some embodiments of the present disclosure is shown.

[0043] Figure 11 A schematic block diagram of a data conversion apparatus according to other embodiments of the present disclosure is shown.

[0044] Figure 12 The schematic diagram illustrates the structural schematic of a computer system of an electronic device according to some embodiments of the present disclosure.

[0045] In the accompanying drawings, the same or corresponding reference numerals indicate the same or corresponding parts. Detailed Implementation

[0046] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numerals in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this specification. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this specification as detailed in the appended claims.

[0047] The terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to be limiting of this specification. The singular forms “a,” “the,” and “the” as used in this specification and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any and all possible combinations of one or more of the associated listed items.

[0048] Exemplary embodiments will now be described more fully with reference to the accompanying drawings. However, these exemplary embodiments can be implemented in many forms and should not be construed as limited to the examples set forth herein; rather, these embodiments are provided so that this disclosure will be more thorough and complete, and will fully convey the concept of the exemplary embodiments to those skilled in the art.

[0049] Furthermore, the described features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. Numerous specific details are provided in the following description to give a thorough understanding of embodiments of this disclosure. However, those skilled in the art will recognize that the technical solutions of this disclosure can be practiced without one or more of the specific details, or other methods, components, apparatuses, steps, etc., can be employed. In other instances, well-known methods, apparatuses, implementations, or operations are not shown or described in detail to avoid obscuring various aspects of this disclosure.

[0050] Furthermore, the accompanying drawings are for illustrative purposes only and are not necessarily drawn to scale. The block diagrams shown in the drawings are merely functional entities and do not necessarily correspond to physically independent entities. That is, these functional entities can be implemented in software, in one or more hardware modules or integrated circuits, or in different network and / or processor devices and / or microcontroller devices.

[0051] In applications such as artificial intelligence computing, graphics processing, and neural network training, it is often necessary to represent and process floating-point data with low precision while ensuring computational and storage efficiency. For example, to reduce storage bandwidth usage, improve data throughput, or adapt to low-precision computing hardware, it is usually necessary to convert high-precision floating-point numbers to low-precision floating-point numbers. However, due to the limited precision of the low-precision floating-point format, it is often difficult to accurately represent the high-precision floating-point numbers during the conversion process; therefore, rounding is required.

[0052] In the actual conversion process, some related technologies use an additive random rounding method. That is, the source format floating-point number and the random number are first added together, and then the format conversion operation is performed based on the summation result to achieve random rounding up or down.

[0053] The processing procedure for additive random rounding is as follows: Figure 1 As shown. In the additive random rounding method, the high-precision floating-point number to be converted is first obtained. The high-precision floating-point number can be composed of a sign bit, an exponent bit, and a mantissa bit. During format conversion, the mantissa bits of the high-precision floating-point number are truncated according to the mantissa bit width of the target format. The lower-order mantissa bits that exceed the mantissa bit width of the target format are determined as the target bit field for rounding. Subsequently, a random number corresponding to the bit width of the target bit field is generated, and the random number is added to the target bit field. Based on the result of this addition operation, the corresponding carry result is extracted, which can be represented as the carry_in signal. After obtaining the carry_in signal, the rounding direction is determined according to the carry_in signal: if the carry_in signal indicates that a carry has occurred, the high-precision floating-point number is rounded up; if the carry_in signal indicates that no carry has occurred, the high-precision floating-point number is rounded down. Based on the rounding result, the corresponding low-precision floating-point number is generated by combining the truncated high-order field, thus completing the format conversion from high-precision floating-point number to low-precision floating-point number.

[0054] However, the above-mentioned addition-based random rounding method is based on the numerical representation characteristics of the specified number range. When the value corresponding to the floating-point number in the source format falls into the non-specified number range of the target format, the rounding probability relationship will change, making it difficult for this method to maintain unbiased rounding.

[0055] Taking the conversion from half-precision floating point (FP16) to 4-bit floating point (FP4) as an example, assuming FP4 uses a format of 1 sign bit, 2 exponent bits, and 1 mantissa bit, then the non-standard values ​​that can be represented in the positive direction of FP4 include 0 and 0.5. Therefore, within the interval [0, 0.5], the candidate values ​​of any FP16 number to be converted in FP4 are 0 and 0.5. For values ​​within this interval, unbiased random rounding requires that the probability of rounding up to 0.5 corresponds to its distance from 0, and the probability of rounding down to 0 corresponds to its distance from 0.5.

[0056] For example, take the FP16 value 0.1875. Since 0.1875 lies in the interval [0, 0.5], its corresponding candidate values ​​in FP4 are 0 and 0.5. To maintain unbiased rounding, the probability of rounding up to 0.5 should be:

[0057] .

[0058] That is, theoretically, it should be rounded to 0.5 with a probability of 37.5% and to 0 with a probability of 62.5%.

[0059] However, if additive random rounding is used, the truncated lower digit of the FP16 mantissa needs to be added to the random number, and whether to round up depends on whether a carry occurs. For the value 0.1875, since the random number is added to the lower digit of the mantissa, its impact on the conversion result is mainly limited to the range of values ​​corresponding to the truncated mantissa. Therefore, when the value to be converted is in the range of 0 to 0.5, it is difficult to achieve probabilistic rounding to 0.5 through addition. That is, for 0.1875, when the original additive random rounding method is used directly, the conversion result often always falls on 0, and it is impossible to round up to 0.5 with the target probability of 37.5%.

[0060] In other words, for the value 0.1875, the expected value of ideal unbiased rounding should be:

[0061] .

[0062] When using the original additive random rounding method, if the result is always 0, the expected value of the rounding is 0. It is clear that there is a significant discrepancy between the two methods, making it difficult to maintain unbiased rounding.

[0063] For example, consider the FP16 value of 0.125. Its adjacent candidate values ​​in FP4 are also 0 and 0.5. If unbiased rounding is maintained, the probability of rounding up to 0.5 should be:

[0064] .

[0065] That is, it should be rounded to 0.5 with a 25% probability and to 0 with a 75% probability, corresponding to an expected value of 0.125. However, if the original additive random rounding method is still used, the value is also likely to be rounded to 0 all the time, thus causing the actual rounded expected value to deviate from the original value.

[0066] Therefore, during the format conversion from FP16 to FP4, when the value falls into the non-standard number range [0, 0.5] of FP4, directly using the additive random rounding method cannot accurately reflect the positional relationship between the value and 0.5. Consequently, it is difficult to obtain a random rounding result consistent with the target probability, ultimately introducing rounding bias. This makes it difficult to meet the accuracy and stability requirements of neural network training scenarios.

[0067] To address all or part of the technical problems in the aforementioned related technologies, this disclosure provides a data conversion method that can be applied to numerical precision conversion scenarios in artificial intelligence accelerators such as Graphics Processing Units (GPUs), General-Purpose Graphics Processing Units (GPGPUs), and Neural Processing Units (NPUs).

[0068] Figure 2 A schematic flowchart illustrating a data conversion method according to some embodiments of the present disclosure is shown. (Reference) Figure 2 As shown, the data transformation method may include the following steps.

[0069] Step S210: Obtain the source format floating-point number.

[0070] Step S220: Determine the target numerical range in the target format where the absolute value of the source format floating-point number falls. The precision of the source format floating-point number is greater than the precision of the target format floating-point number. The target numerical range is one of multiple numerical ranges in the target format, including non-standard number ranges.

[0071] Step S230: When the target value interval is a non-standard number interval, determine the target sub-interval from at least two sub-intervals contained in the non-standard number interval, and determine the first candidate value and the second candidate value adjacent to the absolute value in the target sub-interval.

[0072] Step S240: Based on the relative position of the source format floating-point number within the local interval defined by the first and second candidate values, perform random rounding on the source format floating-point number to obtain the target format floating-point number; wherein, the random rounding method is different for different numerical intervals; wherein, the source format floating-point number includes: floating-point data to be format converted during the training of the neural network model, and the target format floating-point number is used for the training calculation of the neural network model; the neural network model is used to perform any one of image processing tasks, speech processing tasks, and natural language processing tasks.

[0073] According to the data conversion method in this example embodiment, on the one hand, by first determining the target numerical range in which the absolute value corresponding to the source format floating-point number falls in the target format, it is possible to identify non-standard number ranges. When the identification result indicates that the target numerical range is a non-standard number range, rounding processing adapted to the non-standard number range is performed. On the other hand, when the target numerical range is a non-standard number range, determining the first and second candidate values ​​adjacent to the absolute value in the non-standard number range ensures that the target format floating-point number converted from the source format floating-point number takes only values ​​from the first and second candidate values, thereby reducing rounding deviation within the non-standard number range. Furthermore, by performing random rounding processing based on the position of the source format floating-point number within the range defined by the first and second candidate values, the random rounding corresponds to the positional relationship of the absolute value corresponding to the source format floating-point number within a local range, thereby reducing rounding deviation within the non-standard number range when converting the source format floating-point number to the target format.

[0074] The data conversion method in this example embodiment will be further explained below.

[0075] In step S210, the source format floating-point number is obtained.

[0076] The source format floating-point number can represent the input floating-point data to be converted, and its precision is greater than that of the target format floating-point number. For example, the source format floating-point number can include any one or more of half-precision floating-point (FP16), brain floating-point (BF16), single-precision floating-point (FP32), and double-precision floating-point (FP64). Furthermore, in neural network training scenarios, the source format floating-point number can be a floating-point number corresponding to weight parameters, activation values, gradient values, bias parameters, or intermediate calculation results, etc.

[0077] For example, the source format floating-point number includes floating-point data to be converted during the training of the neural network model, and the target format floating-point number is used for the training calculation of the neural network model. That is, the source format floating-point number can be floating-point data to be converted during the training of the neural network model. Specifically, obtaining the source format floating-point number includes the following process: during the training of the neural network model, obtaining the floating-point data to be converted as the source format floating-point number; wherein, the target format floating-point number obtained by converting the source format floating-point number is used for the training calculation of the neural network model.

[0078] The neural network model is used to perform any one of image processing, speech processing, and natural language processing tasks. For example, the neural network model may include one or more of the following: convolutional neural network model, recurrent neural network model, long short-term memory network model, transformer model, generative adversarial network model, or hybrid expert model. This disclosure does not limit the type of neural network model or the specific task performed by the neural network model.

[0079] The floating-point data to be converted can represent the floating-point data used in computation, storage, or transmission during the training of a neural network model. For example, the floating-point data to be converted can be at least one of the following: floating-point data corresponding to weight parameters, floating-point data corresponding to activation values, floating-point data corresponding to gradient values, floating-point data corresponding to bias parameters, or floating-point data corresponding to intermediate computation results. Accordingly, the converted target format floating-point number can be used for convolution operations, parameter updates, or intermediate result caching during the training of the neural network model. By converting the source format floating-point number during neural network model training to a low-precision target format floating-point number, data storage overhead and transmission bandwidth overhead during training can be reduced, and data processing efficiency during training computation can be improved.

[0080] In step S220, the target numerical range in the target format is determined where the absolute value of the source format floating-point number falls. The precision of the source format floating-point number is greater than the precision of the target format floating-point number. The target numerical range is one of multiple numerical ranges in the target format, including non-standard number ranges.

[0081] Here, "absolute value" represents the absolute value of the corresponding numerical value of the source format floating-point number. "Target format" represents the floating-point data format corresponding to the source format floating-point number after format conversion, with a precision lower than the source format floating-point number. A precision lower than the source format floating-point number can mean that the total bit width of the target format is smaller than the bit width of the source format floating-point number, and / or that the number of exponent bits and mantissa bits in the target format are fewer than in the source format. For example, the target format can be FP4, or other floating-point formats with lower precision than the source format.

[0082] Furthermore, the target numerical range can represent at least one interval within the numerical representation range of the target format, divided according to the numerical representation characteristics, used to characterize the numerical range of the absolute value corresponding to the floating-point number in the source format when converted to the target format. Different target numerical ranges can correspond to different rounding rules. For example, the target format can contain multiple target numerical ranges, which can include non-standard number ranges and standard number ranges. The non-standard number range can represent the numerical range in the target format that uses non-standard number representation, and the standard number range can represent the numerical range in the target format that uses standard number representation.

[0083] In step S230, when the target value interval is an anomalous number interval, a target sub-interval is determined from at least two sub-intervals contained in the anomalous number interval, and a first candidate value and a second candidate value adjacent to the absolute value are determined in the target sub-interval.

[0084] The first candidate value and the second candidate value can represent two representable values ​​that are adjacent to the absolute value in the target format. The first candidate value and the second candidate value are used as two candidate values ​​when performing random rounding on the absolute value. Further, the first candidate value and the second candidate value can be the two target format representable values ​​that are closest to the absolute value within the non-normalized number interval. One candidate value can be less than or equal to the absolute value, and the other candidate value can be greater than or equal to the absolute value, so that the absolute value lies within the numerical range defined by the first candidate value and the second candidate value. For example, when the target format is FP4 and the absolute value falls within the non-normalized number interval [0, 0.5), the first candidate value and the second candidate value can be 0 and 0.5, and the first candidate value and the second candidate value are different.

[0085] For example, the non-standard number interval includes at least two sub-intervals. During the random rounding process, for different sub-intervals, reference probabilities corresponding to random rounding can be determined using different probability determination methods. These reference probabilities are used to determine the probability of selecting a first or second candidate value when randomly rounding a source-format floating-point number within the corresponding sub-interval. Since the positional relationship between the source-format floating-point number and adjacent candidate values, as well as the numerical representation characteristics of the target format, may differ in different sub-intervals, using different reference probability determination methods for different sub-intervals allows the reference probability generation process to better adapt to the numerical distribution characteristics of each sub-interval, thereby improving the targeting of the random rounding process.

[0086] For example, for a floating-point number with a target format of a preset number of bits, the sub-interval can include a first denormalized number sub-interval and a second denormalized number sub-interval. The upper limit of the first denormalized number sub-interval is no greater than the lower limit of the second denormalized number sub-interval. The processing logic for the first and second denormalized number sub-intervals differs. For instance, the methods for determining the reference probability differ between the two sub-intervals: when the target sub-interval is the first denormalized number sub-interval, because this sub-interval is close to zero, the source format floating-point number may be in a denormalized state or have a small exponent value. Therefore, determining the reference probability requires simultaneously utilizing both the mantissa and exponent value of the source format floating-point number. Specifically, the shift amount can be determined by combining the difference in exponent bias between the source and target formats, and the mantissa is concatenated with the hidden bit before shifting to generate a reference probability, which is used for random selection between the first and second candidate values. When the target subinterval is the second denormalized number subinterval, since the values ​​in this subinterval are relatively large, the exponent of the source-format floating-point number is usually already normalized. Therefore, the determination of the reference probability is mainly determined by the mantissa, without relying on the exponent value for calculation. Specifically, the mantissa or its high-order bits of the source-format floating-point number can be directly used in conjunction with a random number to determine which of the first and second candidate values ​​to choose as the target representation value. Thus, the core difference between the two types of subintervals lies in the different methods of determining the reference probability. In the first denormalized number subinterval, the determination of the reference probability requires a combination of the mantissa and the exponent value, while in the second denormalized number subinterval, the determination of the reference probability mainly relies on the mantissa. This differentiated processing method can optimize the hardware implementation complexity and processing efficiency for different numerical ranges while ensuring computational accuracy.

[0087] In step S240, the source format floating-point number is randomly rounded according to its relative position in the local interval defined by the first and second candidate values ​​to obtain the target format floating-point number; wherein, the random rounding method is different for different numerical intervals.

[0088] In this context, a local interval can represent a numerical range bounded by the first and second candidate values. Relative position represents the positional relationship of the source-format floating-point number within this numerical range relative to the first and second candidate values, used to determine the reference probability for random rounding. Random rounding represents the rounding process of selecting one value from multiple candidate values ​​according to probability. Unlike deterministic rounding, random rounding allows the selection of different candidate values ​​to be adapted to the positional relationship of the source-format floating-point number within the corresponding interval, thus making the conversion result statistically closer to the actual value corresponding to the source-format floating-point number.

[0089] Furthermore, different numerical ranges can correspond to different random rounding methods. Generally speaking, in different numerical ranges, the positional relationship between the source format floating-point number and the target format candidate value, the numerical representation characteristics, and the available numerical information may differ. Therefore, the basis for determining the target representation value during random rounding can also differ. Accordingly, the random rounding methods in different numerical ranges can be reflected in: different methods for determining the reference probability, different parameters involved in determining the reference probability, or different methods for determining the target representation value.

[0090] For example, different numerical intervals may include non-standard number intervals and other numerical intervals besides non-standard number intervals. For non-standard number intervals, the reference probability corresponding to random rounding can be determined based on the positional relationship of the source format floating-point number in the interval defined by adjacent candidate values, and the target representation value can be determined according to the reference probability; for other numerical intervals besides non-standard number intervals among multiple numerical intervals, random rounding can be performed based on the mantissa truncation value of the source format floating-point number relative to the target format and a random number to generate the target format floating-point number.

[0091] Specifically, for non-standard number ranges, the probability relationship for random rounding can be determined first by considering the positional relationship between the source format floating-point number and the first and second candidate values. Then, based on this probability relationship, one of the first and second candidate values ​​is selected. Finally, the target format floating-point number is generated based on the selected result. Optionally, the closer the source format floating-point number is to the first candidate value, the greater the probability of selecting the first candidate value after random rounding; similarly, the closer the source format floating-point number is to the second candidate value, the greater the probability of selecting the second candidate value after random rounding. This allows the random rounding result to match the actual numerical position of the source format floating-point number, making the random rounding result statistically closer to the actual value corresponding to the source format floating-point number, reducing rounding bias, and improving the accuracy of the format conversion result.

[0092] Next, the technical details of steps S210 to S240 described above will be further explained in other embodiments of this disclosure.

[0093] In some embodiments, determining the target numerical range in which the absolute value of the source format floating-point number falls within the target format specifically includes the following technical steps: determining the exponent value of the source format floating-point number; and determining the target numerical range in which the absolute value of the source format floating-point number falls within the target format based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format.

[0094] The exponent value represents the numerical information corresponding to the exponent bits in the source format floating-point number, characterizing the order of magnitude of the corresponding value. The exponent offset value corresponding to the source format represents the offset preset in the exponent encoding rule used by the source format floating-point number to achieve signed exponent representation. The encoded value corresponding to the exponent bits in the source format floating-point number, after being calculated with the exponent offset value corresponding to the source format, yields the corresponding actual exponent information. The exponent offset value corresponding to the target format represents the offset set in the exponent encoding rule used by the target format floating-point number to achieve signed exponent representation.

[0095] Specifically, based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format, the target numerical range in which the absolute value of the floating-point number in the source format falls within the target format can be determined through the following process: subtract the exponent bias value corresponding to the source format from the exponent value to obtain the actual exponent corresponding to the floating-point number in the source format; add the actual exponent to the exponent bias value corresponding to the target format to obtain the range judgment exponent under the target format; and determine the target numerical range in which the absolute value of the floating-point number in the source format falls within the target format based on the range judgment exponent.

[0096] Taking BF16 as the source format and FP4 as the target format as an example, FP4 is pre-set to include three numerical intervals: [0, 0.5), [0.5, 1], and greater than 1. For example, the interval boundaries can be determined by the following formula:

[0097] .

[0098] in, It can represent the exponent value of a floating-point number in source format. It can represent the exponential bias value corresponding to the source format. It can represent the exponential bias value corresponding to the target format.

[0099] For format conversion from BF16 to FP4, the exponential bias value for BF16 can be 127, while the exponential bias value for FP4 can be 1. Therefore, the corresponding interval division threshold... It can be:

[0100] .

[0101] Based on the relationship between the interval judgment index and the interval division threshold, the target numerical range can be determined. Specifically, when the interval judgment index is less than 0, the absolute value of the source format floating-point number is determined to fall within the interval [0, 0.5); when the interval judgment index is equal to 0, the absolute value of the source format floating-point number is determined to fall within the interval [0.5, 1]; and when the interval judgment index is greater than 0, the absolute value of the source format floating-point number is determined to fall within an interval greater than 1. Through the above interval judgment process, the numerical range of the source format floating-point number in the target format can be quickly determined based on the index information, thus providing a basis for subsequent format conversion processing.

[0102] In some embodiments, reference Figure 3 As shown, based on the relative position of the source format floating-point number within the local interval defined by the first and second candidate values, the source format floating-point number is randomly rounded to obtain the target format floating-point number. The specific technical steps include the following:

[0103] Step S310: Determine the reference probability corresponding to random rounding based on the relative position of the source format floating-point number in the local interval defined by the first candidate value and the second candidate value.

[0104] The reference probability can represent the target probability used to characterize whether the source format floating-point number takes the first candidate value or the second candidate value. This reference probability can be determined based on the difference between the source format floating-point number and the first candidate value, and the difference between the second candidate value and the first candidate value, and is used to reflect the positional relationship between the source format floating-point number and the first and second candidate values.

[0105] Specifically, determining the reference probability for random rounding based on the relative position of the source-format floating-point number within the local interval defined by the first and second candidate values ​​can be achieved through the following steps: determining the difference between the source-format floating-point number and the first candidate value, and determining the reference probability for random rounding based on the ratio of this difference to the length of the local interval. Here, the length of the local interval can represent the numerical difference between the first and second candidate values. For example, if the first candidate value is greater than the second candidate value, the interval length can be the difference between the first and second candidate values; if the first candidate value is less than the second candidate value, the interval length can be the difference between the second and first candidate values.

[0106] Optionally, assuming the first candidate value is greater than the second candidate value, the reference probability for random rounding is determined based on the difference between the source-format floating-point number and the first candidate value, and the difference between the second candidate value and the first candidate value. This can be done in the following two ways:

[0107] The first approach is to determine the reference probability corresponding to the selection probability of the first candidate value based on the proportion of the difference between the source format floating-point number and the second candidate value to the length of the interval between the first and second candidate values.

[0108] For example, when the reference probability corresponds to the selection probability of the first candidate value, the reference probability can be determined by the following formula:

[0109] .

[0110] in, It can represent a reference probability. It can represent the absolute value corresponding to a floating-point number in the source format. It can represent the first candidate value. It can represent the second candidate value, and satisfies > .

[0111] The second approach is to determine the reference probability for random rounding based on the proportion of the difference between the first candidate value and the source format floating-point number to the length of the interval between the first candidate value and the second candidate value, if the reference probability corresponds to the selection probability of the second candidate value.

[0112] For example, when the reference probability corresponds to the selection probability of the second candidate value, the reference probability can be determined by the following formula:

[0113] .

[0114] in, > .

[0115] By using the above method, when the first candidate value is greater than the second candidate value, the reference probability can be determined based on the positional relationship between the source format floating-point number and the two candidate values, so that the random rounding result can be adapted to the actual numerical position of the source format floating-point number.

[0116] Optionally, assuming the first candidate value is less than the second candidate value, then in the case where the first candidate value is less than the second candidate value, the reference probability corresponding to random rounding is determined based on the difference between the source-format floating-point number and the first candidate value, and the difference between the second candidate value and the first candidate value. This can be done in the following two cases:

[0117] The first approach is to determine the reference probability corresponding to the selection probability of the first candidate value based on the proportion of the difference between the second candidate value and the source format floating-point number to the length of the interval between the second candidate value and the first candidate value.

[0118] For example, when the reference probability corresponds to the selection probability of the first candidate value, the reference probability can be determined by the following formula:

[0119] .

[0120] in, < .

[0121] The second approach is to determine the reference probability corresponding to the selection probability of the second candidate value based on the proportion of the difference between the source format floating-point number and the first candidate value to the length of the interval between the second candidate value and the first candidate value.

[0122] For example, when the reference probability corresponds to the selection probability of the second candidate value, the reference probability can be determined by the following formula:

[0123] .

[0124] in, < .

[0125] By using the above method, when the first candidate value is less than the second candidate value, the reference probability can be determined based on the positional relationship between the source format floating-point number and the two candidate values, so that the random rounding result can be adapted to the actual numerical position of the source format floating-point number.

[0126] Step S320: Obtain the random probability; wherein the range of the random probability is determined according to the range of the reference probability.

[0127] Here, the random probability can represent a random value introduced during the random rounding process. It can be compared with a reference probability to determine whether the first or second candidate value should be selected in the current rounding process. The range of the random probability can be consistent with the range of the reference probability to facilitate random rounding based on the relationship between the random probability and the reference probability. For example, the range of the random probability and the reference probability is [0,1], and the random probability can be any value in [0,1], such as 0, 0.1, 0.5, and 0.8. When the reference probability is represented using other preset probability intervals, the range of the random probability can also be set to be consistent with that preset probability interval.

[0128] Step S330: Determine the target representation value from the first candidate value and the second candidate value based on the comparison result between the random probability and the reference probability.

[0129] The target value can be represented by a candidate value determined through random rounding between the first and second candidate values. This value serves as the numerical basis for generating the target format floating-point number, and is combined with the sign information corresponding to the source format floating-point number to obtain the final target format floating-point number.

[0130] For example, if the reference probability corresponds to the selection probability of the first candidate value, and the random probability is less than or equal to the reference probability, then the first candidate value is determined as the target representation value. If the reference probability corresponds to the selection probability of the first candidate value, and the random probability is greater than the reference probability, then the second candidate value is determined as the target representation value. If the reference probability corresponds to the selection probability of the second candidate value, and the random probability is less than or equal to the reference probability, then the second candidate value is determined as the target representation value. If the reference probability corresponds to the selection probability of the second candidate value, and the random probability is greater than the reference probability, then the first candidate value is determined as the target representation value.

[0131] Step S340: Generate the target format floating-point number corresponding to the source format floating-point number based on the target representation value.

[0132] Specifically, when generating a target format floating-point number corresponding to a source format floating-point number based on a target representation value, the following steps can be taken: obtain the sign value corresponding to the source format floating-point number; determine the exponent and mantissa values ​​corresponding to the target format floating-point number based on the target representation value; and generate the target format floating-point number corresponding to the source format floating-point number based on the sign value, exponent value, and mantissa value.

[0133] In steps S310 to S340 above, the reference probability is first determined based on the distance relationship between the source format floating-point number and the two candidate values. Then, the candidate value selection is completed by combining random probability, and the target format floating-point number is generated based on the selected target representation value. This can make the rounding result correspond to the position relationship of the source format floating-point number in the candidate interval, thereby improving the accuracy of data conversion.

[0134] In some embodiments, the reference probabilities for random rounding are determined differently for different sub-intervals. Random rounding of the source format floating-point number includes: determining the reference probability corresponding to the random rounding according to the reference probability determination method for the target sub-interval; and performing random rounding on the source format floating-point number using the reference probability. The reference probability is used to determine the probability of selecting a first candidate value or a second candidate value when randomly rounding the source format floating-point number. Since the positional relationship between the source format floating-point number and adjacent candidate values, as well as the numerical representation characteristics of the target format, may differ in different sub-intervals, using different probability determination methods for different sub-intervals allows the reference probability generation process to better adapt to the numerical distribution characteristics of each sub-interval, thereby improving the targeting of the random rounding process.

[0135] For example, for a floating point number whose target format has a preset number of bits, the sub-intervals include a first subnormal subinterval [Y, X), a second subnormal subinterval [X, Z] and other subintervals. The preset number of bits is less than a first preset value. The first preset value may represent a bit width threshold for distinguishing between low-bit-width floating point formats and other floating point formats. Illustratively, the first preset value may be any one of 8, 10 or 16.

[0136] Wherein, Y may represent the lower boundary value of the subnormal interval of the target format, Z may represent the upper boundary value of the subnormal interval of the target format, and X may represent an interval division value between Y and Z, satisfying Y<X<Z. Thus, the first subnormal subinterval and the second subnormal subinterval may together constitute at least a part of the subnormal interval of the target format. Illustratively, Y may be 0, Z may be the minimum normal value in the target format, and X may be a preset division value between 0 and the minimum normal value. Further, the value of X can be determined according to the distribution of candidate representation values of the target format in the subnormal interval, the bit width configuration of the target format or a preset rounding strategy, so as to divide the subnormal interval into a plurality of subintervals suitable for separately determining probabilities.

[0137] Further, if the first subnormal subinterval is a subnormal subinterval close to 0, the two endpoint values of this subinterval are a first candidate value and a second candidate value respectively, and the numerical position of the source format floating point number in this subinterval can be determined based on the mantissa value and exponent value of the source format floating point number, then a reference probability corresponding to stochastic rounding can be generated according to the mantissa value and exponent value of the source format floating point number, and a target representation value can be determined from the first candidate value and the second candidate value according to the reference probability. Specifically, since the first subnormal subinterval is close to 0, the value of the source format floating point number in this subinterval is affected not only by the mantissa value but also by the exponent value, therefore, it is difficult to accurately reflect the actual position of the source format floating point number between the first candidate value and the second candidate value based only on the mantissa value. Based on this, by simultaneously using the mantissa value and the exponent value to generate the reference probability, the reference probability can more accurately characterize the positional relationship of the source format floating point number between the first candidate value and the second candidate value, and further make the process of determining the target representation value between the first candidate value and the second candidate value according to the reference probability consistent with the actual value situation of the source format floating point number in the first subnormal subinterval.

[0138] If the second denormalized number subinterval is a denormalized number subinterval far from 0, and the two endpoints of this subinterval are the first candidate value and the second candidate value, respectively, and the numerical position of the source-format floating-point number within this subinterval can be determined based on the mantissa of the source-format floating-point number, then the reference probability corresponding to random rounding can be determined based on the mantissa of the source-format floating-point number, and the target representation value can be determined from the first and second candidate values ​​based on this reference probability. Specifically, since the source-format floating-point number values ​​within this second denormalized number subinterval are relatively large, their position within this subinterval is mainly affected by the mantissa. Therefore, the positional relationship between the source-format floating-point number and the first and second candidate values ​​can be characterized using only the mantissa. Based on this, determining the reference probability based on the mantissa ensures that the process of determining the target representation value between the first and second candidate values ​​based on the reference probability is consistent with the actual value of the source-format floating-point number within this second denormalized number subinterval.

[0139] In order to further reduce the hardware implementation complexity of the floating-point format conversion process and improve the processing efficiency when converting high-precision floating-point numbers to FP4 format, this disclosure also provides a hardware-friendly floating-point format conversion method for high-precision floating-point numbers to FP4 format.

[0140] In some embodiments, the target format is a 4-bit floating-point number (FP4), and the denormalized number range includes a first denormalized number subrange [0, 0.5].

[0141] When the target value interval is within the first non-standard number sub-interval, the first candidate value is one of 0 and 0.5, and the second candidate value is the other. Based on the relative position of the source-format floating-point number within the local interval defined by the first and second candidate values, the source-format floating-point number is randomly rounded to obtain the target-format floating-point number. Specifically, this includes the following technical processes: determining the sign value, mantissa, and exponent value of the source-format floating-point number; generating a reference probability corresponding to the random rounding based on the mantissa and exponent values; determining the target representation value from 0 and 0.5 based on the reference probability; and generating the target-format floating-point number corresponding to the source-format floating-point number based on the target representation value and the sign value.

[0142] The sign value represents the sign information of the source-format floating-point number, indicating whether the corresponding value is positive or negative. The mantissa represents the numerical information corresponding to the mantissa bits in the source-format floating-point number, representing the significant digits of the number. The exponent represents the numerical information corresponding to the exponent bits in the source-format floating-point number, representing the order of magnitude of the corresponding value. In this embodiment, within the first non-standard number sub-interval of the target format, a reference probability corresponding to the numerical position can be generated based on the mantissa and exponent values, and the target representation value can be determined between 0 and 0.5 accordingly. This reduces hardware implementation complexity while improving the accuracy of high-precision floating-point number to FP4 format conversion.

[0143] Specifically, when the target value range is the first non-standard number sub-range, i.e. [0, 0.5), the absolute value of the source format floating-point number is between 0 and 0.5, so 0 and 0.5 can be used as two candidate values.

[0144] Within this interval, to ensure that the rounded result is statistically consistent with the absolute value corresponding to the source-format floating-point number, the probability of choosing the value 0.5 can be correlated with the relative position of the absolute value within the interval [0, 0.5). For example, the reference probability can be expressed as:

[0145] .

[0146] in, It can represent the absolute value corresponding to a floating-point number in the source format.

[0147] Furthermore, the absolute value of a source-format floating-point number can be represented by both the mantissa and the exponent. For specified numbers, It can be represented as:

[0148] .

[0149] in, It can represent the significant digits corresponding to the mantissa. It can represent an exponential value. It can represent the exponential bias value corresponding to the source format.

[0150] Substituting the above expression into the reference probability 2x, we get:

[0151] .

[0152] Therefore, the reference probability can be expressed as the result of combining the tail value and the exponent value and then performing an exponential shift. Based on this expression, in the implementation process, the tail value and the hidden bit can be concatenated first to obtain the significant digits. Then, the shift amount can be determined according to the exponent value and the exponent bias value corresponding to the source format, and the significant digits can be shifted to generate the reference probability.

[0153] For example, the source format floating-point number is a specification number; the reference probability corresponding to random rounding is generated based on the mantissa and exponent value, which can be done through the following technical steps: concatenating the mantissa with a hidden bit with a value of 1 to obtain the significant digits; determining the shift amount based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format; shifting the significant digits according to the shift amount to obtain the reference probability corresponding to random rounding.

[0154] The specification number represents a floating-point number in normalized representation. For BF16, the specification number represents a floating-point number whose exponent is not all zeros; in this case, the significant digits can be composed of a hidden bit with a value of 1 and a mantissa bit. For FP32, the specification number can also represent a floating-point number whose exponent is not all zeros; similarly, the significant digits can be composed of a hidden bit with a value of 1 and a mantissa bit. The significant digits represent the numerical information obtained by concatenating the mantissa and the hidden bit, used to characterize the significant numerical part of the source format floating-point number. For the specification number, the hidden bit can be 1; therefore, the significant digits can be composed of a hidden bit with a value of 1 and a mantissa bit.

[0155] The exponent bias value corresponding to the source format represents the offset preset in the exponent encoding rule used by the source format floating-point number to achieve signed exponent representation. The exponent bias value corresponding to the target format represents the offset set in the exponent encoding rule used by the target format floating-point number to achieve signed exponent representation. For example, the exponent bias value corresponding to BF16 can be 127, the exponent bias value corresponding to FP32 can also be 127, and the exponent bias value corresponding to FP4 can be 1.

[0156] Furthermore, shifting can represent the process of performing a bit-level shift operation on significant digits according to a defined shift amount. Through shifting, the numerical value corresponding to the significant digits can be adjusted to generate a reference probability for random rounding. Specifically, shifting significant digits according to the shift amount to obtain the reference probability for random rounding can include: determining the number of shift positions corresponding to the significant digits based on the shift amount; performing a right shift on the significant digits; and determining the result of the right shift as the reference probability for random rounding. Through the above process, the generation of the reference probability can be transformed into the concatenation and shifting of significant digits, thereby enabling the generation of the reference probability for random rounding through adders and shifters, improving hardware implementation friendliness and reducing implementation complexity.

[0157] In addition, the shift amount can be determined based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format. This can be done by subtracting the exponent bias value corresponding to the source format from the exponent value and then adding it to the exponent bias value corresponding to the target format to obtain the shift amount.

[0158] Specifically, subtracting the exponent bias value corresponding to the source format from the exponent value yields the actual exponent of the source format floating-point number, which represents the actual order of magnitude of the value. Adding this to the exponent bias value corresponding to the target format maps this actual order of magnitude to the exponent representation range of the target format, thus obtaining the shift amount corresponding to the numerical adjustment relationship under the target format. For example, when the source format is BF16 or FP32 and the target format is FP4, since the exponent bias value corresponding to BF16 and FP32 can be 127, and the exponent bias value corresponding to FP4 can be 1, the corresponding shift amount can be obtained by subtracting 127 from the exponent value and then adding 1. Through this process, the exponent adjustment relationship of the source format floating-point number can be transformed into addition and subtraction operations between the exponent value and the bias value, thus facilitating the determination of the shift amount using an adder, improving hardware implementation friendliness, and reducing implementation complexity.

[0159] In some embodiments, when the source format floating-point number is an unstandardized number, the reference probability corresponding to random rounding is determined based on the mantissa and exponent values. This can be achieved through the following technical steps: determining the integer value and mantissa digits corresponding to the mantissa; and determining the reference probability corresponding to random rounding based on the exponent value, the integer value, and the mantissa digits.

[0160] In this context, "denormalized number" refers to floating-point numbers that are not represented using a normalized format. For denormalized numbers, the exponent can have a preset special value, and the significant digits may not include the hidden 1 bit. Therefore, the corresponding value can be represented by both the mantissa and the number of bits. The integer value can represent the integer form corresponding to the mantissa. The number of mantissa bits can represent the number of bits contained in the mantissa portion of the source-format floating-point number. For example, taking FP16 as an example, FP16 includes 10 mantissa bits. When FP16 is a denormalized number and the mantissa is 0100000000, the binary code corresponding to this mantissa can be parsed as an unsigned integer to obtain the integer value 256; correspondingly, the number of mantissa bits can be 10. Through the above process, a reference probability corresponding to random rounding can be generated based on the integer value, the number of mantissa bits, and the exponent value, thereby facilitating hardware processing in the case of denormalized numbers.

[0161] Furthermore, the reference probability corresponding to random rounding can be determined based on the exponent value, the integer value, and the number of decimal places. This can be done through the following steps: determine the fractional value corresponding to the decimal place based on the integer value and the number of decimal places; and exponentially scale the fractional value based on the exponent value to obtain the reference probability corresponding to random rounding.

[0162] The fractional value represents the numerical value corresponding to the mantissa, which is determined by both the integer value and the number of mantissa digits. For example, the integer value can be used as the numerator, and the value obtained by dividing the integer value by 2 and the number of mantissa digits as the exponent can be used as the denominator to obtain the corresponding fractional value, thus representing the actual numerical relationship of the mantissa. Exponential scaling can represent the process of adjusting the numerical value corresponding to the fractional value based on the exponent value.

[0163] Specifically, when the source format floating-point number is an unstandardized number, since the significant digits do not contain hidden bits with a value of 1, the fractional value corresponding to the mantissa can be determined directly based on the integer value and the number of mantissa digits, and further combined with the exponent value to determine the reference probability corresponding to random rounding.

[0164] For example, the score can be expressed as , where m can represent the integer value corresponding to the mantissa, and n can represent the number of mantissa digits.

[0165] Based on this, the fractional value can be exponentially scaled according to the exponent value to obtain the reference probability corresponding to random rounding. For example, the reference probability can be expressed as:

[0166] .

[0167] in, It can represent the integer value corresponding to the mantissa. It can represent the number of digits in the mantissa. It can represent an exponential value. It can represent the exponential bias value corresponding to the source format.

[0168] Specifically, let's take FP16 as the source format and FP4 as the target format as an example. Assume the FP16 floating-point number is a denormalized number, with the mantissa's binary code being 0100000000. Then, the integer value corresponding to the mantissa can be 256, and the number of mantissa bits can be 10. Furthermore, since the exponent bias value corresponding to FP16 can be 15, and the exponent value corresponding to a denormalized number can be 0, the reference probability can be expressed as follows:

[0169] .

[0170] In some embodiments, determining the target representation value from 0 and 0.5 based on a reference probability specifically includes the following technical steps: generating a first random number; wherein the bit width of the first random number is the same as the bit width of the reference probability; and determining the target representation value from 0 and 0.5 based on the sum of the first random number and the reference probability.

[0171] Here, the first random number can represent a random value used to determine the target representation value between 0 and 0.5 when the target value range is a first non-standard number sub-range [0, 0.5). The bit width can represent the number of binary bits corresponding to the value. The first random number and the reference probability use the same bit width when being summed, thus facilitating hardware summation. In this embodiment, the target representation value can be determined between 0 and 0.5 based on the sum of the first random number and the reference probability, thereby achieving random rounding in a hardware-friendly manner.

[0172] In some embodiments, the target representation value is determined from 0 and 0.5 based on the sum of the first random number and the reference probability, specifically including the following technical steps: adding the first random number and the reference probability to obtain a first sum value; determining the carry result corresponding to the first sum value; if the carry result is a carry, determining the target representation value to be 0.5; if the carry result is no carry, determining the target representation value to be 0.

[0173] Here, the first sum value can represent the result obtained by adding the first random number to the reference probability. The carry result can represent whether the first sum value generated a carry result during the addition operation. For example, the carry result can be used to characterize whether the first sum value exceeds the representation range of the bit width corresponding to the reference probability, and the target representation value is determined based on the carry result between 0 and 0.5.

[0174] Specifically, since the bit width of the first random number is the same as that of the reference probability, whether the sum of the two exceeds the representation range of the corresponding bit width can be characterized by the carry result. If the carry result corresponding to the first sum produces a carry, it indicates that the sum of the first random number and the reference probability has reached or exceeded the upper limit that the current bit width can represent. In this case, the target value can be determined to be 0.5. Correspondingly, if the carry result corresponding to the first sum does not produce a carry, it indicates that the sum of the first random number and the reference probability has not reached the upper limit that the current bit width can represent. In this case, the target value can be determined to be 0. Through the above process, the comparison process between the reference probability and the first random number can be transformed into the determination of the carry result of the sum, thereby facilitating the determination of the target value through the adder and carry determination circuit, and improving the hardware implementation friendliness of random rounding processing.

[0175] The following is combined Figure 4 The process of converting BF16 to FP4 within the first non-standard number sub-interval, which is described above as a hardware-friendly data conversion method, will be further explained.

[0176] Given a source format of BF16 and a corresponding value falling within the first non-standard number sub-interval, obtain the mantissa and exponent values ​​of the source format floating-point number, where the mantissa bits can be 7 bits (7b(M)). Concatenate the mantissa with a hidden bit of value 1 to obtain the significant digits. Determine the shift amount based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format. Write the shift amount into shift bits, and perform shift processing on the significant digits based on the shift amount to obtain the reference probability corresponding to random rounding.

[0177] Further, a first random number is generated, and the bit width of the first random number is the same as the bit width of the reference probability. The first random number is added to the reference probability to obtain a first sum value, and the target representation value is determined based on the carry result corresponding to the first sum value. When the carry result is positive, the target representation value is determined to be 0.5, and the corresponding target format floating-point code is 0 for the sign bit S, 0 for the exponent bit, and 1 for the mantissa bit. When the carry result is negative, the target representation value is determined to be 0, and the corresponding target format floating-point code is 0 for the sign bit S, 0 for the exponent bit, and 0 for the mantissa bit.

[0178] Furthermore, in combination Figure 5 The process of converting FP32 to FP4 within the first non-standard number sub-interval, which is described above as a hardware-friendly data conversion method, will be further explained.

[0179] Given a source format of FP32 and a corresponding value falling within the first non-standard number sub-interval, obtain the mantissa and exponent of the source format floating-point number, where the mantissa can be 23 bits (23b(M)). Concatenate the mantissa with a hidden bit of value 1 to obtain the significant digits. Determine the shift amount based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format. Write the shift amount into the shift bits, and perform shift processing on the significant digits based on the shift amount to obtain the reference probability corresponding to random rounding.

[0180] Further, a first random number is generated, and the bit width of the first random number is the same as the bit width of the reference probability. The first random number is added to the reference probability to obtain a first sum value, and the target representation value is determined based on the carry result corresponding to the first sum value. When the carry result is positive, the target representation value is determined to be 0.5, and the corresponding target format floating-point code is 0.5, with the sign bit S, exponent 00, and mantissa 1; when the carry result is negative, the target representation value is determined to be 0, and the corresponding target format floating-point code is 0.

[0181] Furthermore, when the target format is FP4, since the candidate values ​​corresponding to the 0 to 0.5 interval and the 0.5 to 1 interval are different, with the candidate values ​​corresponding to the 0 to 0.5 interval being 0 and 0.5, and the candidate values ​​corresponding to the 0.5 to 1 interval being 0.5 and 1, it is necessary to set a second non-standard number sub-interval [0.5, 1] ​​so that different random rounding processing methods can be used for different intervals.

[0182] When the target value interval is the second non-standard number sub-interval, the first candidate value is one of 0.5 and 1, and the second candidate value is the other. Based on the relative position of the source format floating-point number in the local interval defined by the first and second candidate values, the source format floating-point number is randomly rounded to obtain the target format floating-point number. Specifically, the following technical steps are included: determining the mantissa and sign value of the source format floating-point number; determining the target representation value from 0.5 and 1 based on the mantissa; and generating the target format floating-point number corresponding to the source format floating-point number based on the target representation value and sign value.

[0183] Specifically, when the target value range is the second non-standard number sub-range, that is, [0.5, 1], the absolute value of the source format floating-point number is between 0.5 and 1, so 0.5 and 1 can be used as two candidate values.

[0184] Within this interval, to ensure that the rounded result is statistically consistent with the absolute value corresponding to the source-format floating-point number, the probability of choosing the value 1 can correspond to the relative position of the absolute value within the interval [0.5, 1]. For example, the reference probability can be expressed as:

[0185] .

[0186] in, It can represent the absolute value corresponding to a floating-point number in the source format.

[0187] Simplifying the above equation, we get:

[0188] .

[0189] Furthermore, the absolute value of a source-format floating-point number can be represented by both the mantissa and the exponent. For values ​​falling within the second denormalized sub-interval, the corresponding exponent relationship is fixed, therefore the absolute value can be expressed as:

[0190] .

[0191] in, It can represent the significant digits corresponding to the mantissa.

[0192] Substituting the above expression into the reference probability We can obtain:

[0193] .

[0194] Therefore, within the second denormalized number sub-interval, the reference probability corresponding to random rounding can be directly determined by the tail value. In implementation, the target representation value can be directly determined based on the tail value within the second denormalized number sub-interval, thus simplifying random rounding processing and reducing hardware implementation complexity.

[0195] In some embodiments, determining the target representation value from 0.5 and 1 based on the tail value specifically includes the following technical steps: generating a second random number; wherein the bit width of the second random number is the same as the bit width of the tail value; and determining the target representation value from 0.5 and 1 based on the sum of the second random number and the tail value.

[0196] The second random number can represent a random value used to determine the target representation value between 0.5 and 1 when the target value range is a second non-standard number sub-range. The bit width of the second random number is the same as the bit width of the tail value, so that the second random number and the tail value are added together, and the target representation value is determined between 0.5 and 1 based on the addition result. In this embodiment, by generating a second random number with the same bit width as the tail value, and determining the target representation value between 0.5 and 1 based on the addition result of the second random number and the tail value, the probability determination process corresponding to the tail value can be transformed into an addition determination process, thereby simplifying the random rounding processing within the second non-standard number sub-range and improving hardware implementation friendliness.

[0197] In some embodiments, the target representation value is determined from 0.5 and 1 based on the sum of the second random number and the tail value, specifically including the following technical steps: adding the second random number and the tail value to obtain a second sum value; determining the carry result corresponding to the second sum value; if the carry result is a carry, determining the target representation value as 1; if the carry result is no carry, determining the target representation value as 0.5.

[0198] The second sum value can represent the result obtained by adding the second random number to the last value.

[0199] Specifically, the second random number can be added to the last digit to obtain the second sum. Since the bit width of the second random number is the same as that of the last digit, the carry-over result corresponding to the second sum can be determined based on whether the second sum exceeds the representation range of the corresponding bit width.

[0200] In some embodiments, if the carry result is a carry, it can indicate that the sum of the second random number and the tail value reaches or exceeds the upper limit that the current bit width can represent. In this case, the target value can be determined to be 1. Correspondingly, if the carry result is no carry, it can indicate that the sum of the second random number and the tail value does not reach the upper limit that the current bit width can represent. In this case, the target value can be determined to be 0.5.

[0201] In the above way, the probability determination process corresponding to the tail value can be transformed into the determination process of the carry result of the second sum value, which makes it easier to determine the target representation value within the second non-standard number sub-interval through addition operation and carry determination, and improves the hardware implementation friendliness of random rounding processing.

[0202] The following is combined Figure 6 The process of converting BF16 to FP4 within the second non-standard number sub-interval, which is described above as a hardware-friendly data conversion method, will be further explained.

[0203] When the source format is BF16 and the corresponding value falls within the second non-standard number sub-interval, obtain the mantissa of the source format floating-point number, where the mantissa can be 7 bits (7b(M)). Since the reference probability corresponding to random rounding within this interval can be directly determined by the mantissa, the mantissa can be directly used to participate in the subsequent target representation value determination without having to generate a reference probability based on the hidden bit and shift amount.

[0204] Further, a second random number is generated. To perform addition with the mantissa, the effective bit width of the second random number can be the same as the bit width of the mantissa. In the BF16 scenario, the portion of the second random number corresponding to the bit width of the mantissa participates in the addition operation, and the remaining bits not involved in the operation can be marked as unused bits. Then, the second random number is added to the mantissa to obtain a second sum, and the target representation value is determined based on the carry result corresponding to the second sum.

[0205] When the carry-in result is 1, the target representation value is determined to be 1, and the corresponding target format floating-point encoding is a sign bit S, an exponent bit 01, and a mantissa bit 0; when the carry-in result is 0, the target representation value is determined to be 0.5, and the corresponding target format floating-point encoding is a sign bit S, an exponent bit 00, and a mantissa bit 1. Therefore, random rounding conversion from BF16 to FP4 can be completed within the second non-standard number sub-interval.

[0206] Furthermore, combined Figure 7 The process of converting FP32 to FP4 within the second non-standard number sub-interval using the aforementioned hardware-friendly data conversion method will be further explained.

[0207] When the source format is FP32 and the corresponding value falls within the second non-standard number sub-interval, obtain the mantissa of the source format floating-point number, where the mantissa can be 23 bits (23b(M)). Since the reference probability corresponding to random rounding within this interval can be directly determined by the mantissa, the mantissa can be directly used in the subsequent determination of the target representation value.

[0208] Further, a second random number is generated, with its bit width being the same as that of the mantissa. The second random number and the mantissa are added together to obtain a second sum. The target representation value is determined based on the carry-over result of this second sum. When the carry-over result is 1, the target representation value is determined to be 1, and the corresponding target format floating-point encoding is a sign bit S, an exponent bit 01, and a mantissa bit 0; when the carry-over result is 0, the target representation value is determined to be 0.5, and the corresponding target format floating-point encoding is a sign bit S, an exponent bit 00, and a mantissa bit 1.

[0209] Furthermore, when the target format is FP4 or other low-precision formats, the multiple numerical ranges corresponding to the target format also include a specification number range. For example, when the target format is FP4, its corresponding specification number range is a range greater than 1, such as (1, 6). Within this range, the target format floating-point number uses specification number representation, and adjacent representable values ​​can include 1, 1.5, 2, 3, 4, and 6. For source format floating-point numbers falling within this specification number range, a different random rounding method can be used compared to non-specification number ranges.

[0210] Specifically, when the target numerical range is a specified number range, the mantissa truncation value of the source format floating-point number relative to the target format is determined; where the mantissa truncation value represents the low-order mantissa field of the source format floating-point number that exceeds the mantissa bit width of the target format; a third random number is generated; where the bit width of the third random number is the same as the bit width of the mantissa truncation value; based on the sum of the mantissa truncation value and the third random number, the target format floating-point number corresponding to the source format floating-point number is generated.

[0211] The mantissa truncation value represents the lower-order bits of the mantissa that have been truncated relative to the mantissa width of the target format floating-point number. The third random number represents a random value used for random rounding within the specified range. Setting the bit width of the third random number to be the same as that of the mantissa truncation value facilitates addition operations between the third random number and the mantissa truncation value, and generates the target format floating-point number based on the addition result.

[0212] For example, when a source-format floating-point number falls within the specification range of the target-format floating-point number, the high-order mantissa bits, consistent with the mantissa width of the target format, can be retained first, and the remaining low-order mantissa bits can be used as the truncated mantissa value. Next, a third random number, with the same mantissa width as the truncated value, is generated, and added to the truncated value. If the addition results in a carry, it can be determined that the retained high-order mantissa bits will be rounded up; if the addition does not result in a carry, it can be determined that the retained high-order mantissa bits will remain unchanged. Finally, the sign and exponent information are combined to generate the corresponding target-format floating-point number.

[0213] Using the above method, the probability determination process corresponding to the lower digit of the mantissa can be transformed into the addition determination process of the mantissa truncation value and the third random number within the specification number range, thereby completing the random rounding process within the specification number range in a relatively simple hardware implementation.

[0214] Optionally, a target format floating-point number corresponding to the source format floating-point number is generated based on the sum of the mantissa truncation value and the third random number. Specifically, this includes the following technical steps: adding the mantissa truncation value and the third random number to obtain a third sum value; shifting the third sum value according to the bit width of the mantissa truncation value to obtain a target representation value; and generating a target format floating-point number corresponding to the source format floating-point number based on the target representation value.

[0215] The third sum value represents the result of adding the truncated mantissa value to the third random number. Specifically, the truncated mantissa value is first added to the third random number to obtain the third sum value. Since the bit width of the third random number is the same as that of the truncated mantissa value, the third sum value can represent the addition result of the truncated mantissa value and the third random number. Further, the third sum value can be shifted according to the bit width of the truncated mantissa value to obtain the target representation value. Specifically, the third sum value can be shifted according to the bit width of the truncated mantissa value to extract the target bits corresponding to the target format, and the extracted result is determined as the target representation value. Then, the target format floating-point number corresponding to the source format floating-point number can be generated according to the target representation value. For example, the target representation value can be combined with the sign information and exponent information corresponding to the source format floating-point number to generate the corresponding target format floating-point number. The above method can transform the random rounding process within the specified number range into an addition and shifting process of the mantissa truncation value and a third random number, thus facilitating the generation of the target format floating-point number with a relatively simple hardware structure.

[0216] The following is combined Figure 8 The process of converting BF16 to FP4 within the specification number sub-range using the aforementioned hardware-friendly data conversion method will be further explained.

[0217] Given a source format of BF16 and a corresponding value falling within the specified range, the sign bit (1 bit), exponent bit (8 bits), and mantissa bit of the source format floating-point number are obtained, with the mantissa bit being 7 bits. Based on the mantissa bit width of the target format FP4, the lower-order mantissa bits exceeding the target format's mantissa bit width are truncated to obtain a truncated mantissa value. Further, a third random number is generated, with a total bit width of 16 bits. The portion with the same bit width as the truncated mantissa value participates in the addition operation, while the remaining portion is used as unused bits. The truncated mantissa value is added to the third random number to obtain a third sum, and the target representation value is determined based on the carry result corresponding to the third sum. Subsequently, the third sum is shifted according to the bit width of the truncated mantissa value to obtain the target representation value, and combined with the sign bit and exponent bit of the source format floating-point number to generate an FP4 format floating-point number. The FP4 format floating-point number includes 1 sign bit, 2 exponent bits, and 1 mantissa bit.

[0218] Furthermore, combined Figure 9 The process of converting FP32 to FP4 within the specified number range using the aforementioned hardware-friendly data conversion method will be further explained.

[0219] Given a source format of FP32 and a corresponding value falling within the specified range, the sign bit (1 bit), exponent bit (8 bits), and mantissa bit of the source format floating-point number are obtained, with the mantissa bit being 23 bits. Based on the mantissa bit width of the target format FP4, the lower-order bits of the mantissa exceeding the target format's mantissa bit width are truncated to obtain a truncated mantissa value. Further, a third random number is generated, with its bit width matching that of the truncated mantissa value. The truncated mantissa value and the third random number are added to obtain a third sum, and the target representation value is determined based on the carry-over result of the third sum. Subsequently, the third sum is shifted according to the bit width of the truncated mantissa value to obtain the target representation value, and then combined with the sign bit and exponent bit of the source format floating-point number to generate an FP4 format floating-point number.

[0220] The hardware-friendly floating-point conversion method in the above embodiments is explained below with specific code instructions, taking BF16 as the source format and FP4 as the target format as an example. The specific code is as follows:

[0221] if exp <= 125: #[0-0.5) P = (x - 0) / (0.5-0) = 2x 2x = 2××(exp-127+1)×(1+f)

[0222] s8 = 1< <fbits|f

[0223] shift = abs(exp - 127 + 1)

[0224] add = rbits_16b + s8>>shift

[0225] carry_in = add>>16

[0226] if carry_in > 0:

[0227] FP4 = 0.5

[0228] else:

[0229] FP4 = 0

[0230] elif exp == 126: #[0.5-1) P = (2××-1×(1+f) - 0.5) / (1 - 0.5) = f

[0231] add = low_7bits_rbits_16b + f

[0232] carry_in = add>>7

[0233] if carry_in > 0:

[0234] FP4 = 1

[0235] else:

[0236] FP4 = 0.5

[0237] else: #>1 add SR regular handle

[0238] add = x + low_6bits_rbits_16b

[0239] carry_in = add>>6

[0240] FP4 = add >> 6.

[0241] Among them, 125 and 126 can be determined by the exponential relationship between the source format and the target format. Specifically, based on the exponential value of the source format, the exponential bias value corresponding to the source format, and the exponential bias value corresponding to the target format, the interval partitioning condition can be obtained as: exp-127+1≤0.

[0242] When exp = 126, it corresponds to the boundary case where the value is 0; when exp ≤ 125, it corresponds to the case where the value is less than 0; and when exp ≥ 127, it corresponds to the case where the value is greater than 0. Therefore, the source format floating-point number can be divided into three intervals: exp ≤ 125 corresponds to the interval [0, 0.5], exp = 126 corresponds to the interval [0.5, 1], and exp ≥ 127 corresponds to the interval greater than 1. Here, 1 in the formula can represent the exponent bias value corresponding to the target format FP4, exp can represent the exponent value of the source format floating-point number, and 127 can represent the exponent bias value corresponding to the source format BF16.

[0243] Specifically, when exp≤125, the corresponding target value range is [0, 0.5). The effective number s8 can be obtained by concatenating the tail value with the hidden bit, and the shift amount shift can be determined according to the exponential relationship. After shifting the effective number, it is added with the 16-bit random number rbits_16b to obtain the sum value add. The carry result carry_in is obtained by right shifting, thus determining the target representation value between 0 and 0.5.

[0244] When exp=126, the corresponding target value range is [0.5, 1]. At this time, the reference probability can be directly determined by the tail value f. Therefore, the low part of the 16-bit random number low_7bits_rbits_16b can be added to the tail value f to obtain the sum value add. The carry result carry_in can be obtained by right shifting, thus determining the target representation value between 0.5 and 1.

[0245] When exp≥127, the corresponding target value range is greater than 1. The conventional random rounding method can be used to add the source format floating-point number x with the low-bit part low_6bits_rbits_16b of the random number to obtain the sum value add, and then obtain the target format floating-point number through a shift operation.

[0246] Using the above method, the source format floating-point number can be divided into different numerical intervals based on the exponent value, and the corresponding random rounding processing method can be used in each interval, thereby realizing the conversion of the source format floating-point number to the target format floating-point number.

[0247] It should be noted that although the steps of the method in this disclosure are described in a specific order in the accompanying drawings, this does not require or imply that the steps must be performed in that specific order, or that all the steps shown must be performed to achieve the desired result. Additional or alternative steps may be omitted, multiple steps may be combined into one step, and / or a step may be broken down into multiple steps.

[0248] Furthermore, in this embodiment of the disclosure, a data conversion apparatus is also provided, with reference to... Figure 10 As shown, the data conversion device may include an interval judgment circuit 101 and a non-standard number conversion circuit 102. The interval judgment circuit 101 can be used to obtain a source format floating-point number and determine the target numerical interval in the target format where the absolute value of the source format floating-point number falls; wherein the precision of the source format floating-point number is greater than the precision of the target format floating-point number, and the target numerical interval is one of multiple numerical intervals included in the target format, including non-standard number intervals.

[0249] The denormalized number conversion circuit 102 can be used to determine a target sub-interval from at least two sub-intervals contained in the denormalized number interval, and to determine a first candidate value and a second candidate value adjacent to the absolute value in the target sub-interval; based on the relative position of the source format floating-point number in the local interval defined by the first and second candidate values, the source format floating-point number is randomly rounded to obtain the target format floating-point number; wherein, the random rounding processing method is different for different numerical intervals; wherein, the source format floating-point number includes: floating-point data to be format converted during the training of the neural network model, and the target format floating-point number is used for the training calculation of the neural network model; the neural network model is used to perform any one of image processing tasks, speech processing tasks, and natural language processing tasks.

[0250] Optionally, the interval determination circuit 101 may include: a floating-point number determination module, used to determine the exponent value of the source format floating-point number; and an interval determination module, used to determine the target numerical interval in which the absolute value of the source format floating-point number falls in the target format based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format.

[0251] In some embodiments, the non-standard number interval includes at least two sub-intervals, and the non-standard number conversion circuit 102 may include a reference probability determination module. This module can be used to determine the reference probability corresponding to random rounding for different sub-intervals according to different probability determination methods. The reference probability is used to determine the probability of selecting a first candidate value or a second candidate value when randomly rounding a source format floating-point number within the sub-interval.

[0252] In some embodiments, the denominated number conversion circuit 102 can be used to convert a source format floating-point number into a 4-bit floating-point number, that is, the target format is a 4-bit floating-point number. The denominated number range includes a first denominated number subrange [0, 0.5). When the target value range is the first denominated number subrange, the first candidate value is one of 0 and 0.5, and the second candidate value is the other.

[0253] Then refer to Figure 11 As shown, the destandardized number conversion circuit 102 may include: a first destandardized number conversion sub-circuit 1021, which is used to determine the sign value, mantissa, and exponent value of the source format floating-point number when the target value range is a first destandardized number sub-range; generate a reference probability corresponding to random rounding based on the mantissa and exponent value; determine the target representation value from 0 and 0.5 based on the reference probability; and generate a target format floating-point number corresponding to the source format floating-point number based on the target representation value and sign value.

[0254] Optionally, the source format floating-point number is a standard number; the first non-standard number conversion sub-circuit 1021 may include: a bit concatenation module, used to concatenate the mantissa with a hidden bit of value 1 to obtain a significant number; a first adder, used to determine the shift amount based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format; and a first shifter, used to shift the significant number according to the shift amount to obtain the reference probability corresponding to random rounding.

[0255] Optionally, the first adder can be configured to subtract the exponent bias value corresponding to the source format from the exponent value, and then add it to the exponent bias value corresponding to the target format to obtain the shift amount.

[0256] Optionally, the source format floating-point number is a denominated number; the first denominated number conversion sub-circuit 1021 may include: a value determination module, used to determine the integer value and the number of mantissa digits corresponding to the mantissa value; and a first reference probability determination module, used to determine the reference probability corresponding to random rounding based on the exponent value, the integer value, and the number of mantissa digits.

[0257] Optionally, the first non-standard number conversion sub-circuit 1021 may include: a first random number generation module for generating a first random number; wherein the bit width of the first random number is the same as the bit width of the reference probability; and a first carry-determination module for determining a target representation value from 0 and 0.5 based on the sum of the first random number and the reference probability.

[0258] Optionally, the first carry-determination module can be configured to: add the first random number and the reference probability to obtain a first sum value; determine the carry result corresponding to the first sum value; if the carry result is a carry, determine the target representation value as 0.5; if the carry result is no carry, determine the target representation value as 0.

[0259] In some embodiments, the target format is a 4-bit floating-point number. The denominated number range also includes a second denominated number subrange [0.5, 1]; when the target value range is the second denominated number subrange, the first candidate value is one of 0.5 and 1, and the second candidate value is the other.

[0260] Then refer to Figure 11 As shown, the denominated number conversion circuit 102 may include: a second denominated number conversion sub-circuit 1022, which is used to determine the mantissa and sign value of the source format floating-point number; determine the target representation value from 0.5 and 1 based on the mantissa; and generate a target format floating-point number corresponding to the source format floating-point number based on the target representation value and the sign value.

[0261] Optionally, the second non-standard number conversion sub-circuit 1022 may include: a second random number generation module for generating a second random number; wherein the bit width of the second random number is the same as the bit width of the mantissa; and a second carry-determination module for determining the target representation value from 0.5 and 1 based on the sum of the second random number and the mantissa.

[0262] Optionally, the second carry-determination module can be configured to: add the second random number and the last digit to obtain a second sum value; determine the carry result corresponding to the second sum value; if the carry result is a carry, determine the target representation value as 1; if the carry result is no carry, determine the target representation value as 0.5.

[0263] In some embodiments, the multiple numerical ranges also include a specification range; Reference Figure 11 As shown, the data conversion device further includes: a specification number conversion circuit 103, used to determine the mantissa truncation value of the source format floating-point number relative to the target format when the target value range is a specification number range; wherein, the mantissa truncation value represents the low-order mantissa field of the source format floating-point number that exceeds the mantissa bit width of the target format; generate a third random number; wherein, the bit width of the third random number is the same as the bit width of the mantissa truncation value; and generate the target format floating-point number corresponding to the source format floating-point number based on the sum of the mantissa truncation value and the third random number.

[0264] Optionally, the specification number conversion circuit 103 may include: a second adder for adding the truncated mantissa value and a third random number to obtain a sum; a second shifter for shifting the sum according to the bit width of the truncated mantissa value to obtain a target representation value; and a format conversion module for generating a target format floating-point number corresponding to the source format floating-point number based on the target representation value.

[0265] Optionally, the non-standard number conversion circuit 102 may include: a probability determination module, used to determine a reference probability corresponding to random rounding based on the relative position of the source format floating-point number in a local interval defined by a first candidate value and a second candidate value; a random probability acquisition module, used to acquire a random probability; wherein the range of the random probability is determined according to the range of the reference probability; a representation value determination module, used to determine a target representation value from the first candidate value and the second candidate value based on the comparison result between the random probability and the reference probability; and a floating-point number generation module, used to generate a target format floating-point number corresponding to the source format floating-point number based on the target representation value.

[0266] Optionally, the probability determination module can be configured to: determine the difference between the source format floating-point number and the first candidate value, and determine the reference probability corresponding to random rounding based on the ratio of the difference to the interval length of the local interval.

[0267] It should be noted that although several modules or units of a geometric pipeline have been mentioned in the detailed description above, this division is not mandatory. In fact, according to embodiments of this disclosure, the features and functions of two or more modules or units described above can be embodied in one module or unit. Conversely, the features and functions of one module or unit described above can be further divided and embodied by multiple modules or units.

[0268] Furthermore, in an exemplary embodiment of this disclosure, an electronic device capable of implementing the above-described data conversion method is also provided.

[0269] Those skilled in the art will understand that various aspects of this disclosure can be implemented as a system, method, or program product. Therefore, various aspects of this disclosure can be embodied in the following forms: a completely hardware embodiment, a completely software embodiment (including firmware, microcode, etc.), or an embodiment combining hardware and software aspects, collectively referred to herein as a "circuit," "module," or "system."

[0270] The following reference Figure 12 To describe an electronic device 1200 according to such an embodiment of the present disclosure. Figure 12 The electronic device 1200 shown is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments disclosed herein.

[0271] like Figure 12 As shown, the electronic device 1200 is presented in the form of a general-purpose computing device. The components of the electronic device 1200 may include, but are not limited to: at least one processing unit 1210, at least one storage unit 1220, a bus 1230 connecting different system components (including storage unit 1220 and processing unit 1210), and a display unit 1240.

[0272] The storage unit stores program code that can be executed by the processing unit 1210, causing the processing unit 1210 to perform the steps described in the "Exemplary Methods" section above according to various exemplary embodiments of this disclosure.

[0273] For example, the processing unit 1210 may further include a host-side processor, such as a central processing unit. This host-side processor can be used to execute the data conversion method described in the above embodiments. For example, the processing unit 1210 may include an interval judgment circuit, a non-standard number conversion circuit, and a standard number conversion circuit.

[0274] Storage unit 1220 may include readable media in the form of volatile storage units, such as video memory, random access memory (RAM) 1221 and / or cache 1222, and may further include read-only memory (ROM) 1223. This storage unit can be used to store data conversion results.

[0275] Storage unit 1220 may also include a program / utility 1224 having a set (at least one) program module 1225, such program module 1225 including but not limited to: operating system, one or more application programs, other program modules and program data, each or some combination of these examples may include an implementation of a network environment.

[0276] Bus 1230 can represent one or more of several types of bus structures, including a memory cell bus or memory cell controller, a peripheral bus, a graphics acceleration port, a processing unit, or a local bus using any of the various bus structures.

[0277] Electronic device 1200 can also communicate with one or more external devices 1270 (e.g., keyboard, pointing device, Bluetooth device, etc.), and with one or more devices that enable a user to interact with electronic device 1200, and / or with any device that enables electronic device 1200 to communicate with one or more other computing devices (e.g., router, modem, etc.). This communication can be performed via input / output (I / O) interface 1250. Furthermore, electronic device 1200 can also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN), and / or public networks, such as the Internet) via network adapter 1260. As shown, network adapter 1260 communicates with other modules of electronic device 1200 via bus 1230. It should be understood that, although not shown in the figures, other hardware and / or software modules can be used in conjunction with electronic device 1200, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.

[0278] From the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein can be implemented by software or by combining software with necessary hardware. Therefore, the technical solutions according to the embodiments of this disclosure can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, external hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, terminal device, or network device, etc.) to execute the methods according to the embodiments of this disclosure.

[0279] In exemplary embodiments of this disclosure, a computer-readable storage medium is also provided, on which a program product capable of implementing the methods described above is stored. In some possible embodiments, various aspects of this disclosure may also be implemented as a program product including program code that, when the program product is run on a terminal device, causes the terminal device to perform the steps described in the "Exemplary Methods" section of this specification according to various exemplary embodiments of this disclosure.

[0280] The program product used to implement the above-described data conversion method in the embodiments of this disclosure may employ a portable compact disc read-only memory (CD-ROM) and include program code, and may run on a terminal device, such as a personal computer. However, the program product of this disclosure is not limited thereto. In this document, the readable storage medium may be any tangible medium containing or storing a program that may be used by or in conjunction with an instruction execution system, apparatus, or device.

[0281] The program product may employ any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of readable storage media (a non-exhaustive list) include: an electrical connection having one or more wires, a portable disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.

[0282] Computer-readable signal media may include data signals propagated in baseband or as part of a carrier wave, carrying readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A readable signal medium may also be any readable medium other than a readable storage medium, capable of sending, propagating, or transmitting programs for use by or in conjunction with an instruction execution system, apparatus, or device.

[0283] The program code contained on the readable medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, RF, etc., or any suitable combination thereof.

[0284] Program code for performing the operations of this disclosure can be written in any combination of one or more programming languages, including object-oriented programming languages ​​such as Java and C++, and conventional procedural programming languages ​​such as C or similar languages. The program code can execute entirely on the user's computing device, partially on the user's computing device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).

[0285] Furthermore, the above figures are merely illustrative of the processes included in the method according to exemplary embodiments of this disclosure and are not intended to be limiting. It is readily understood that the processes shown in the above figures do not indicate or limit the temporal order of these processes. Additionally, it is readily understood that these processes may be executed synchronously or asynchronously, for example, in multiple modules.

[0286] From the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein can be implemented by software or by combining software with necessary hardware. Therefore, the technical solutions according to the embodiments of this disclosure can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, external hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, touch terminal, or network device, etc.) to execute the methods according to the embodiments of this disclosure.

[0287] Other embodiments of this disclosure will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and embodiments are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the claims.

[0288] It should be understood that this disclosure is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this disclosure is limited only by the appended claims.

Claims

1. A data conversion method, characterized in that, The method includes: The source format floating-point number is obtained by an interval judgment circuit, and the target numerical interval in the target format is determined to be the absolute value of the source format floating-point number. The precision of the source format floating-point number is greater than the precision of the target format floating-point number. The target numerical interval is one of multiple numerical intervals of the target format, including non-standard number intervals. When the target numerical range is the non-standard number range, a non-standard number conversion circuit determines the target sub-range from at least two sub-ranges contained in the non-standard number range, and determines a first candidate value and a second candidate value adjacent to the absolute value in the target sub-range. Based on the relative position of the source-format floating-point number within the local interval defined by the first and second candidate values, the source-format floating-point number is randomly rounded to obtain the target-format floating-point number. The random rounding method differs for different numerical ranges. The reference probability of the random rounding for different sub-ranges is determined. The methods differ. The reference probability is used to determine the probability of selecting the first candidate value or the second candidate value when performing random rounding on the source format floating-point number. The target format includes a 4-bit floating-point number, and the at least two sub-intervals include a first non-standard number sub-interval and a second non-standard number sub-interval. The upper limit of the first non-standard number sub-interval is not greater than the lower limit of the second non-standard number sub-interval. The reference probability in the first non-standard number sub-interval is determined jointly based on the mantissa and exponent of the source format floating-point number, and the reference probability in the second non-standard number sub-interval is determined based on the mantissa of the source format floating-point number. The source format floating-point number includes: floating-point data to be converted during the training of the neural network model; the target format floating-point number is used for training calculations of the neural network model; the neural network model is used to perform any one of image processing tasks, speech processing tasks, and natural language processing tasks.

2. The data conversion method according to claim 1, characterized in that, The step of randomly rounding the source format floating-point number to obtain the target format floating-point number based on its relative position within the local interval defined by the first candidate value and the second candidate value includes: Based on the relative position of the source format floating-point number within the local interval defined by the first candidate value and the second candidate value, determine the reference probability corresponding to random rounding; Obtain a random probability; wherein the range of values ​​for the random probability is determined based on the range of values ​​for the reference probability; Based on the comparison between the random probability and the reference probability, a target representation value is determined from the first candidate value and the second candidate value; Generate the target format floating-point number corresponding to the source format floating-point number based on the target representation value.

3. The data conversion method according to claim 2, characterized in that, The step of determining the reference probability corresponding to random rounding based on the relative position of the source-format floating-point number within the local interval defined by the first candidate value and the second candidate value includes: The difference between the source-format floating-point number and the first candidate value is determined, and the reference probability corresponding to the random rounding is determined based on the ratio of the difference to the interval length of the local interval.

4. The data conversion method according to claim 1, characterized in that, The random rounding process for the source format floating-point number includes: The reference probability corresponding to the random rounding process is determined according to the reference probability determination method corresponding to the target sub-interval; The reference probability is used to perform random rounding on the source format floating-point number.

5. The data conversion method according to claim 4, characterized in that, The target format is a 4-bit floating-point number, and the target sub-interval includes the first non-standard number sub-interval [0, 0.5); When the target value interval is the first non-standard number sub-interval, the first candidate value is one of 0 and 0.5, and the second candidate value is the other; the step of randomly rounding the source format floating-point number according to its relative position in the local interval defined by the first candidate value and the second candidate value to obtain the target format floating-point number includes: Determine the sign value, mantissa, and exponent value of the source format floating-point number; Generate a reference probability corresponding to random rounding based on the tail value and the exponent value; The target representation value is determined from 0 and 0.5 based on the reference probability; Generate the target format floating-point number corresponding to the source format floating-point number based on the target representation value and the symbol value.

6. The data conversion method according to claim 5, characterized in that, The source format floating-point number is a specification number; the step of generating a reference probability corresponding to random rounding based on the mantissa and exponent value includes: The last digit is concatenated with the hidden bit that has a value of 1 to obtain the valid digit; The shift amount is determined based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format; The significant digits are shifted according to the shift amount to obtain the reference probability corresponding to the random rounding.

7. The data conversion method according to claim 6, characterized in that, The step of determining the shift amount based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format includes: The shift amount is obtained by subtracting the exponent bias value corresponding to the source format from the exponent value and then adding it to the exponent bias value corresponding to the target format.

8. The data conversion method according to claim 5, characterized in that, The source format floating-point number is a non-normal number; the step of determining the reference probability corresponding to random rounding based on the mantissa and exponent value includes: Determine the integer value and the number of digits corresponding to the last digit; The reference probability corresponding to the random rounding is determined based on the exponent value, the integer value, and the number of digits in the mantissa.

9. The data conversion method according to claim 5, characterized in that, The step of determining the target representation value from 0 and 0.5 based on the reference probability includes: Generate a first random number; wherein the bit width of the first random number is the same as the bit width of the reference probability; The target representation value is determined from 0 and 0.5 based on the sum of the first random number and the reference probability.

10. The data conversion method according to claim 9, characterized in that, The step of determining the target representation value from 0 and 0.5 based on the sum of the first random number and the reference probability includes: The first random number and the reference probability are added together to obtain the first sum value; Determine the carry result corresponding to the first sum; If the carry result is a carry, the target value is determined to be 0.5; If the carry result is no carry, the target representation value is determined to be 0.

11. The data conversion method according to claim 4, characterized in that, The target sub-interval includes the second non-standard number sub-interval [0.5, 1]; When the target value interval is the second non-standard number sub-interval, the first candidate value is one of 0.5 and 1, and the second candidate value is the other; the step of randomly rounding the source format floating-point number according to its relative position in the local interval defined by the first candidate value and the second candidate value to obtain the target format floating-point number includes: Determine the mantissa and sign value of the source format floating-point number; The target representation value is determined from 0.5 and 1 based on the tail value; Generate the target format floating-point number corresponding to the source format floating-point number based on the target representation value and the symbol value.

12. The data conversion method according to claim 11, characterized in that, The step of determining the target representation value from 0.5 and 1 based on the tail value includes: Generate a second random number; wherein the bit width of the second random number is the same as the bit width of the tail value; The target representation value is determined from 0.5 and 1 based on the sum of the second random number and the tail value.

13. The data conversion method according to claim 12, characterized in that, The step of determining the target representation value from 0.5 and 1 based on the sum of the second random number and the tail value includes: Add the second random number and the last value to obtain the second sum value; Determine the carry result corresponding to the second sum; If the carry result is a carry, the target representation value is determined to be 1; If the carry result is no carry, the target representation value is determined to be 0.

5.

14. The data conversion method according to claim 1, characterized in that, The multiple numerical ranges also include the specification ranges; The method further includes: When the target value range is a specification number range, determine the mantissa truncation value of the source format floating-point number relative to the target format; wherein, the mantissa truncation value represents the low-order mantissa field of the source format floating-point number that exceeds the mantissa bit width of the target format; Generate a third random number; wherein the bit width of the third random number is the same as the bit width of the truncated mantissa value; Based on the sum of the mantissa truncation value and the third random number, a target format floating-point number corresponding to the source format floating-point number is generated.

15. The data conversion method according to claim 14, characterized in that, The step of generating a target format floating-point number corresponding to the source format floating-point number based on the sum of the mantissa truncation value and the third random number includes: The truncated value and the third random number are added together to obtain the third sum value; Based on the bit width of the truncated mantissa value, the third sum value is shifted to obtain the target representation value; Generate the target format floating-point number corresponding to the source format floating-point number based on the target representation value.

16. The data conversion method according to claim 1, characterized in that, Determining the target numerical range into which the absolute value of the source format floating-point number falls in the target format includes: Determine the exponent value of the source format floating-point number; Based on the exponent value, the exponent bias value corresponding to the source format, and the exponent bias value corresponding to the target format, determine the target numerical range in which the absolute value of the floating-point number in the source format falls within the target format.

17. A data conversion device, characterized in that, The device includes: An interval determination circuit is used to obtain a source format floating-point number and determine the target numerical interval in the target format where the absolute value of the source format floating-point number falls; wherein, the precision of the source format floating-point number is greater than the precision of the target format floating-point number, and the target numerical interval is one of a plurality of numerical intervals included in the target format, the plurality of numerical intervals including non-standard number intervals; A denormalized number conversion circuit is used to determine a target sub-interval from at least two sub-intervals contained in the denormalized number interval, and to determine a first candidate value and a second candidate value adjacent to the absolute value in the target sub-interval; based on the relative position of the source format floating-point number in the local interval defined by the first candidate value and the second candidate value, to perform random rounding on the source format floating-point number to obtain the target format floating-point number; wherein the random rounding method is different for different numerical intervals; wherein the reference probability for the random rounding is determined differently for different sub-intervals, and the reference probability... This is used to determine the probability of selecting the first candidate value or the second candidate value when performing random rounding on the source format floating-point number; wherein, the target format includes a 4-bit floating-point number, and the at least two sub-intervals include: a first non-standard number sub-interval and a second non-standard number sub-interval, the upper limit of the first non-standard number sub-interval is not greater than the lower limit of the second non-standard number sub-interval; the reference probability in the first non-standard number sub-interval is determined based on the mantissa and exponent of the source format floating-point number, and the reference probability in the second non-standard number sub-interval is determined based on the mantissa of the source format floating-point number; The source format floating-point number includes: floating-point data to be converted during the training of the neural network model; the target format floating-point number is used for training calculations of the neural network model; the neural network model is used to perform any one of image processing tasks, speech processing tasks, and natural language processing tasks.

18. The data conversion apparatus according to claim 17, characterized in that, The target format is a 4-bit floating-point number, and the target sub-interval includes a first non-standard number sub-interval [0, 0.5); when the target value interval is the first non-standard number sub-interval, the first candidate value is one of 0 and 0.5, and the second candidate value is the other. The non-standard number conversion circuit includes: The first denormalized number conversion sub-circuit is used to determine the sign value, mantissa, and exponent value of the source format floating-point number when the target value range is the first denormalized number sub-range; and to generate a reference probability corresponding to random rounding based on the mantissa and exponent value. The target representation value is determined from 0 and 0.5 based on the reference probability; the target representation value and the sign value are used to generate the target format floating-point number corresponding to the source format floating-point number.

19. The data conversion apparatus according to claim 17, characterized in that, The target sub-interval includes a second non-standard number sub-interval [0.5, 1]; when the target value interval is the second non-standard number sub-interval, the first candidate value is one of 0.5 and 1, and the second candidate value is the other; The non-standard number conversion circuit also includes: The second non-standard number conversion sub-circuit is used to determine the mantissa and sign value of the source format floating-point number; determine the target representation value from 0.5 and 1 based on the mantissa; and generate the target format floating-point number corresponding to the source format floating-point number based on the target representation value and the sign value.

20. The data conversion apparatus according to claim 17, characterized in that, The multiple numerical ranges also include the specification ranges; The device further includes: A specification number conversion circuit is used to determine, when the target value range is a specification number range, the mantissa truncation value of the source format floating-point number relative to the target format; wherein the mantissa truncation value represents the low-order mantissa field of the source format floating-point number that exceeds the mantissa bit width of the target format; generate a third random number; wherein the bit width of the third random number is the same as the bit width of the mantissa truncation value; and generate a target format floating-point number corresponding to the source format floating-point number based on the sum of the mantissa truncation value and the third random number.

21. An electronic device, characterized in that, include: processor; as well as Memory for storing the executable instructions of the processor; The processor is configured to perform the data conversion method of any one of claims 1-16 by executing the executable instructions.

22. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the data conversion method according to any one of claims 1-16.

Citation Information

Patent Citations

  • GPU-based floating-point number conversion method and device

    CN107038016A

  • Floating point data precision conversion method and device

    CN118585164A

  • Floating-point number format conversion device and method, storage medium and program product

    CN121657960A