Data format conversion method, device, equipment, storage medium and program product

CN120743996BActive Publication Date: 2026-08-18MOORE THREADS TECHNOLOGY (SHANGHAI) CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202510859770.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-06-24
Publication Date
2026-08-18
Estimated Expiration
2045-06-24

AI Technical Summary

Technical Problem

该数据格式转换方式需要有更多的中间资源缓存中间结果,并且包含有两套数据格式转换逻辑,使得逻辑实现较复杂

Benefits of technology

[0015] The data format conversion method, apparatus, device, storage medium, and program product provided in this application embodiment obtain the source data to be converted, the source data format corresponding to the source data, and the target data format. Then, based on the source data, the source data format, and the target data format, a target conversion method is used to directly convert the source data from the source data format to the target data format. Compared with traditional data format conversion methods, this application embodiment only requires a single-step conversion to convert the source data from the source data format to the target data format, without the need for intermediate resource caching of intermediate results. The logic implementation is simple and highly versatile.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120743996B_ABST
    Figure CN120743996B_ABST
Patent Text Reader

Abstract

The application discloses a data format conversion method and device, equipment, a storage medium and a program product, and relates to the technical field of data processing. The method comprises the following steps: acquiring source data to be subjected to data format conversion, a source data format corresponding to the source data, and a target data format; and determining a target conversion mode for directly converting the source data from the source data format to the target data format based on the source data, the source data format and the target data format. The application can directly convert the source data from the source data format to the target data format through a single-step conversion, and does not need to cache intermediate results in an intermediate resource, so that the logical implementation is simple and has strong universality.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of data processing technology, and in particular to a data format conversion method, apparatus, device, storage medium, and program product. Background Technology

[0002] Currently, low-precision floating-point data is widely used to reduce the computational complexity and memory requirements of artificial intelligence model training or inference, and to improve model computational efficiency. Therefore, converting high-precision floating-point data to low-precision floating-point data is crucial.

[0003] In related technologies, high-precision floating-point data in the source format is typically converted to an intermediate floating-point format first, and then the intermediate floating-point data is converted to low-precision floating-point data in the destination format. This data format conversion method requires more intermediate resources to cache intermediate results and includes two sets of data format conversion logic, making the logic implementation relatively complex. Summary of the Invention

[0004] To address the aforementioned technical problems, embodiments of this application provide a data format conversion method, apparatus, device, storage medium, and program product.

[0005] The technical solution of this application is implemented as follows:

[0006] In a first aspect, embodiments of this application provide a data format conversion method, including:

[0007] Obtain the source data to be converted, the source data format corresponding to the source data, and the destination data format;

[0008] Based on the source data, source data format, and destination data format, it is determined that a target conversion method will be used to directly convert the source data from the source data format to the destination data format.

[0009] Secondly, embodiments of this application provide a data format conversion apparatus, including:

[0010] The acquisition module is used to acquire the source data to be converted, the source data format corresponding to the source data, and the destination data format;

[0011] The conversion module is used to determine the target conversion method to directly convert the source data from the source data format to the target data format based on the source data, source data format, and target data format.

[0012] Thirdly, embodiments of this application provide an electronic device, including a memory and a processor. The memory is used to store executable data instructions; when the processor executes the executable data instructions stored in the memory, it implements the steps in the data format conversion method provided in the first aspect.

[0013] Fourthly, embodiments of this application provide a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps in the data format conversion method provided in the first aspect.

[0014] Fifthly, embodiments of this application provide a computer program product, including a computer program or instructions, which, when executed by a processor, implement the steps in the data format conversion method provided in the first aspect.

[0015] The data format conversion method, apparatus, device, storage medium, and program product provided in this application embodiment obtain the source data to be converted, the source data format corresponding to the source data, and the target data format. Then, based on the source data, the source data format, and the target data format, a target conversion method is used to directly convert the source data from the source data format to the target data format. Compared with traditional data format conversion methods, this application embodiment only requires a single-step conversion to convert the source data from the source data format to the target data format, without the need for intermediate resource caching of intermediate results. The logic implementation is simple and highly versatile. Attached Figure Description

[0016] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0017] Figure 1 This is one of the flowcharts illustrating a data format conversion method provided in an embodiment of this application;

[0018] Figure 2 A second schematic flowchart illustrating a data format conversion method provided in this application embodiment;

[0019] Figure 3 The third flowchart illustrates a data format conversion method provided in this application embodiment;

[0020] Figure 4 A fourth flowchart illustrating a data format conversion method provided in this application embodiment;

[0021] Figure 5 This is a schematic diagram of the structure of a data format conversion system provided in an embodiment of this application;

[0022] Figure 6 This is a schematic diagram of the structure of a data format conversion device provided in an embodiment of this application;

[0023] Figure 7 This is a schematic diagram of the physical structure of an electronic device provided in an embodiment of this application. Detailed Implementation

[0024] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0025] It should be noted that in the description of the embodiments of this application, the terms "first," "second," etc., are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class, without limiting the number of objects; for example, the first object can be one or more. Furthermore, in the description of the embodiments of this application, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects have an "or" relationship.

[0026] Unless otherwise defined, all technical and scientific terms used in the embodiments of this application have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used in the embodiments of this application is for descriptive purposes only and is not intended to limit the application.

[0027] To facilitate a clearer understanding of the various embodiments of this application, some related technologies will be introduced as follows.

[0028] Floating-point arithmetic is a crucial function in chips, making floating-point data format conversion equally important. In recent years, the industry has proposed lower-precision floating-point data formats (FP6 and FP4), and more lower-precision formats (such as FP5, FP3, and FP2) may emerge in the future. Therefore, it is essential to develop a universal method for converting high-precision floating-point data to low-precision floating-point data.

[0029] In related technologies, floating-point data conversion methods in graphics processing unit (GPU) architectures include multi-dedicated unit conversion and general-purpose unit two-step conversion. The multi-dedicated unit conversion method incorporates multiple data conversion units within the GPU, each capable of converting only specific high-precision to specific low-precision floating-point data. This method has low scalability and occupies a large chip area. The general-purpose unit two-step conversion method requires first converting the source format data to an intermediate format, and then converting the intermediate format to the destination format. This method requires two steps, necessitates additional caching of intermediate results, and has complex logic implementation, also leading to increased chip area.

[0030] In order to overcome at least some of the above-mentioned defects in the related technologies, the embodiments of this application provide a data format conversion method, apparatus, device, storage medium and program product, which can directly convert source data from source data format to target data format without the need for intermediate resource caching of intermediate results, and the logic implementation is simple and highly versatile.

[0031] The data format conversion method, apparatus, device, storage medium, and program product provided in the embodiments of this application will be described exemplarily below with reference to the accompanying drawings.

[0032] Figure 1 This is one of the flowcharts illustrating a data format conversion method provided in an embodiment of this application, such as... Figure 1 As shown, the method includes:

[0033] S101. Obtain the source data to be converted, the source data format corresponding to the source data, and the destination data format.

[0034] It should be noted that the execution subject of the data format conversion method provided in this application embodiment can be an electronic device, a component in the electronic device, an integrated circuit, or a chip. The electronic device can be a mobile electronic device or a non-mobile electronic device. For example, a mobile electronic device can be a mobile phone, tablet computer, laptop computer, PDA, wearable device, ultra-mobile personal computer (UMPC), netbook, or personal digital assistant (PDA), etc., while a non-mobile electronic device can be a server, network attached storage (NAS), personal computer (PC), television (TV), ATM, or self-service machine, etc. This application embodiment does not specifically limit the scope of the application.

[0035] The following describes the technical solution of the embodiments of this application in detail, taking the data format conversion method provided in the embodiments of this application as an example.

[0036] It should be noted that the source data to be converted in this embodiment is a floating-point number. A floating-point number typically consists of three parts: a sign bit, an exponent bit, and a mantissa bit. The sign bit indicates the sign of the floating-point number; it can be 0 or 1, representing a positive or negative number respectively, and its width is 1 bit. The exponent bit indicates the range of the floating-point number; adjusting the value of the exponent bit changes the size of the floating-point number. In single-precision floating-point numbers, the exponent bit width is 8 bits, while in double-precision floating-point numbers, it is 11 bits. The mantissa bit represents the fractional part of the floating-point number and determines its precision. In single-precision floating-point numbers, the mantissa bit width is 23 bits, while in double-precision floating-point numbers, it is 52 bits to provide higher precision.

[0037] It is understandable that the wider the mantissa of a floating-point number, the higher its precision.

[0038] It should be noted that the source data format can be any existing floating-point data format, such as FP32, FP16, FP8, FP6, and FP4. The destination data format for the data format conversion can also be any existing floating-point data format, such as FP32, FP16, FP8, FP6, and FP4. If converting high-precision floating-point data to low-precision floating-point data, the mantissa width of the source data format is greater than that of the destination data format, resulting in higher precision for the source data format. Conversely, if converting low-precision floating-point data to high-precision floating-point data, the mantissa width of the source data format is less than that of the destination data format, resulting in lower precision for the source data format.

[0039] In some embodiments, before performing data format conversion, the source data to be converted can be obtained first, and then the source data can be parsed or identified to obtain the source data format corresponding to the source data. The source data format may include the sign bit width, exponent bit width, and mantissa bit width.

[0040] In some embodiments, before performing data format conversion, in addition to obtaining the source data to be converted and the source data format corresponding to the source data, it is also necessary to obtain the target data format to be converted. The target data format may include the sign bit width, exponent bit width, and mantissa bit width.

[0041] For example, suppose we need to convert source data in FP32 format to destination data in FP8 format. Before performing the data format conversion, we can first obtain the source data in FP32 format, the exponent width and mantissa width of FP32 format, and the exponent width and mantissa width of FP8 format. Specifically, the exponent width of FP32 format is 8 bits, and the mantissa width is 23 bits; the exponent width of FP8 format is 5 bits, and the mantissa width is 2 bits, as shown in Table 1.

[0042] Table 1

[0043]

[0044]

[0045] Table 1 also shows that for the encoding of the infinite number Inf, the FP32 data format encoding is S.11111111.0..0 (S represents the sign bit, 11111111 represents the exponent bit, and 0..0 represents the mantissa bit), and the FP8 data format encoding is S.11111.00 (S represents the sign bit, 11111 represents the exponent bit, and 00 represents the mantissa bit). That is, the encoding of the infinite number Inf is that all exponent bits are 1 and all mantissa bits are 0. For the encoding of Zero, both the exponent and mantissa bits are 0. For the encoding of the maximum normalized number (Max Normal), the last exponent bit is 0, the remaining exponent bits are 1, and the mantissa bits are all 1. For the encoding of the minimum normalized number (Min Normal), the last exponent bit is 1, the remaining exponent bits are 0, and the mantissa bits are all 0. For the encoding of the maximum non-normalized number (Max Subnormal), all exponent bits are 0 and all mantissa bits are 1. For the encoding of the minimum non-normalized number (Min Subnormal), the exponent bits are all 0, the last mantissa bit is 1, and the remaining mantissa bits are 0.

[0046] It should be noted that numerical data types in computer science also include a type of non-numerical data, NaN (NotaNumber), which represents an undefined or unrepresentable value. Typically, its encoding is such that all exponent bits are 1 and the mantissa bits are not all 0.

[0047] In some embodiments, the encoding expression for a non-number NaN is: (((1< <exp width )-1)< <mant width )|1<<(mant width -1).

[0048] In some embodiments, the encoding expression for the infinite number Inf is: (((1< <exp width )-1)< <mantwidth ).

[0049] In some embodiments, the encoding expression for the maximum normalized number (Max Normal) is: Sign << (exp width +mant width )|(((1< <exp width )-2)< <mant width )|((1< <mant width )-1) or Sign<<(exp width +mant width )|(((1< <exp width )-1)< <mant width )|((1< <mant width )-1).

[0050] In some embodiments, the encoding expression for the minimum normal number (Min Normal) is: Sign << (exp width +mant width )|(1< <mant width ).

[0051] In some embodiments, the encoding expression for zero is: Sign << (exp width +mant width ).

[0052] In some embodiments, the encoding expression for the maximum subnormal number (Max Subnormal) is: Sign << (exp width +mant width )|(0< <mant width )|((1< <mant width )-1).

[0053] In some embodiments, the encoding expression for the minimum subnormal number (Min Subnormal) is: Sign << (exp width +mant width )|(0< <mant width )|1.

[0054] It should be noted that the exp in the above embodiments width Mant indicates the exponent width. width The sign bit indicates the width of the mantissa bits, the sign bit indicates the sign bit value, the << sign is the left shift operator, and the | sign is the bitwise OR operator.

[0055] S102. Based on the source data, source data format, and destination data format, determine the target conversion method to directly convert the source data from the source data format to the destination data format.

[0056] It should be noted that the target conversion method refers to a specific conversion method determined based on the source data, source data format, and target data format, and this specific conversion method can directly convert the source data from the source data format to the target data format. In other words, the target conversion method can be used to convert the source data from the source data format to the target data format in a single step.

[0057] In some embodiments, the target conversion method for directly converting source data from source data format to target data format can be determined based on the exponent width and mantissa width of the source data format, and the exponent width and mantissa width of the target data format.

[0058] In some embodiments, based on the source data, source data format, and destination data format, a target conversion method can be selected from a variety of pre-set data conversion methods to directly convert the source data from the source data format to the destination data format.

[0059] For example, if you need to convert source data in FP32 format to destination data in FP8 format, you can select the target conversion method from a variety of pre-set data conversion methods that can convert source data in FP32 format to FP8 format in a single step to achieve the data format conversion.

[0060] It is understood that the data format conversion method provided in this application embodiment obtains the source data to be converted, the source data format corresponding to the source data, and the target data format. Then, based on the source data, the source data format, and the target data format, the target conversion method is used to directly convert the source data from the source data format to the target data format. Compared with traditional data format conversion methods, this application embodiment only needs to perform a single-step conversion to convert the source data from the source data format to the target data format. There is no need to cache intermediate results with intermediate resources. The logic implementation is simple and highly versatile.

[0061] In some embodiments, the target conversion method includes a first conversion method and a second conversion method;

[0062] The first conversion method is determined based on the data type of the source data; the second conversion method is determined based on the data range of the source data.

[0063] It should be noted that the source data includes two types: one is the non-numeric NaN and infinite Inf type, and the other is the overflow data type. The data range of the source data includes normalized data range and non-normalized data range. The normalized data range refers to the data range consisting of the minimum and maximum normalized numbers, while the non-normalized data range refers to the data range consisting of the minimum and maximum non-normalized numbers.

[0064] In some embodiments, assuming the source data is of type NaN (not a number), the first conversion method can be determined based on the encoding method of the destination data format corresponding to the NaN type. Similarly, assuming the source data is of type Inf (infinite number), the first conversion method can be determined based on the encoding method of the destination data format corresponding to the Inf type.

[0065] In some embodiments, assuming the data range containing the source data is a normalized data range, the second conversion method can be determined based on the encoding method of the source data format corresponding to the normalized data range and the encoding method of the destination data format. Similarly, assuming the data range containing the source data is a non-normalized data range, the second conversion method can be determined based on the encoding method of the source data format corresponding to the non-normalized data range and the encoding method of the destination data format.

[0066] It is understood that the embodiments of this application can avoid the loss of data accuracy caused by uniform conversion processing in traditional methods by determining different conversion methods based on the data type and data range of the source data.

[0067] In some embodiments, based on the source data, the source data format, and the destination data format, it is determined that a target conversion method will be used to directly convert the source data from the source data format to the destination data format, including one of the following:

[0068] If it is determined that the source data is not within the range of target data that can be represented by the target data format, the first conversion method is adopted to directly convert the source data from the source data format to the target data format.

[0069] If it is determined that the source data is within the range of target data that the target data format can represent, then the second conversion method is adopted to directly convert the source data from the source data format to the target data format.

[0070] It should be noted that different data formats can represent different data ranges due to variations in the configuration of the exponent and mantissa widths, and these ranges are finite. For example, the FP8 data format has an exponent width of 5 bits and a mantissa width of 2 bits. If the impact of the mantissa on precision is disregarded, the FP8 data format can represent a data range from -57344 to 57344. This range is calculated based on the maximum and minimum values ​​that the exponent can represent. Therefore, once the target data format is obtained, the target data range that the target data format can represent can be determined based on its exponent and mantissa widths.

[0071] In this embodiment of the application, if it is determined that the source data is not within the range of target data that the target data format can represent, a first conversion method can be used to directly convert the source data from the source data format to the target data format; if it is determined that the source data is within the range of target data that the target data format can represent, a second conversion method can be used to directly convert the source data from the source data format to the target data format.

[0072] For example, if the source data format is FP32, the data range that the FP32 data format can represent is ±3.40282347 × 10 38 Up to ±1.17549435×10 -38 The destination data format is FP8, and the data range that FP8 can represent is -57344 to 57344. Therefore, some data in the FP32 data format must not fall within the data range that FP8 can represent. Therefore, if the source data in FP32 format is outside the data range that FP8 can represent, the first conversion method can be used to directly convert the source data from the source data format to the destination data format; if the source data in FP32 format is within the data range that FP8 can represent, the second conversion method can be used to directly convert the source data from the source data format to the destination data format.

[0073] It is understandable that if the source data is not within the range of target data that the target data format can represent, it indicates that the source data exceeds the maximum or minimum value that the target data format can represent, that is, the source data has overflowed or underflowed relative to the target data format.

[0074] In some embodiments, if it is determined that the source data has overflowed or underflowed relative to the destination data format, a first conversion method can be used to directly convert the source data from the source data format to the destination data format.

[0075] For example, if based on the source data, the exponent width and mantissa width of the source data format, and the exponent width and mantissa width of the destination data format, it is determined that the source data exceeds the maximum value that the destination data format can represent, i.e., data overflow has occurred, then the first conversion method can be used to directly convert the source data from the source data format to the destination data format.

[0076] For example, if based on the source data, the exponent width and mantissa width of the source data format, and the exponent width and mantissa width of the destination data format, it is determined that the source data exceeds the minimum value that the destination data format can represent, i.e., data underflow has occurred, then the first conversion method can be used to directly convert the source data from the source data format to the destination data format.

[0077] It should be noted that if the source data is special data, such as an infinite number Inf or a non-numerical number NaN, then the source data is not within the range of target data that the destination data format can represent.

[0078] For example, if the source data is an infinite number Inf, since the infinite number Inf is not within the range of target data that the target data format can represent, the first conversion method can be used to directly convert the source data from the source data format to the target data format.

[0079] For example, if the source data is a non-NaN, since the non-NaN is not within the range of target data that the target data format can represent, the first conversion method can be used to directly convert the source data from the source data format to the target data format.

[0080] It should be noted that if the source data is within the target data range that the target data format can represent, then it means that the source data is within the intersection range of the data range that the source data format can represent and the data range that the target data format can represent.

[0081] In some embodiments, when performing data format conversion, the source data, the source data format (including the exponent width and mantissa width of the source data format), and the destination data format (including the exponent width and mantissa width of the destination data format) can be identified. Based on the identification result, it can be determined whether the source data is within the target data range that the destination data format can represent. If it is determined that the source data is not within the target data range that the destination data format can represent, a first conversion method can be used to directly convert the source data from the source data format to the destination data format. If it is determined that the source data is within the target data range that the destination data format can represent, a second conversion method can be used to directly convert the source data from the source data format to the destination data format.

[0082] In some embodiments, a first conversion method can be determined based on the data type of the source data (such as non-number type, infinite number type, overflow data type), and a second conversion method can be determined based on the data range in which the source data is located (such as normalized data range, non-normalized data range).

[0083] For example, Figure 2 This is a second flowchart illustrating a data format conversion method provided in an embodiment of this application, as shown below. Figure 2 As shown, the method includes:

[0084] S201. Obtain the source data to be converted, the source data format corresponding to the source data, and the destination data format.

[0085] S202. If it is determined that the source data is not within the range of target data that can be represented by the target data format, the first conversion method is adopted to directly convert the source data from the source data format to the target data format.

[0086] S203. If it is determined that the source data is within the range of target data that can be represented by the target data format, the second conversion method is adopted to directly convert the source data from the source data format to the target data format.

[0087] It should be noted that the descriptions of the same steps and contents as in other embodiments in this embodiment can be found in the descriptions in other embodiments, and will not be repeated here.

[0088] It is understood that the embodiments of this application achieve the direct conversion of source data from source data format to target data format by dynamically selecting different data format conversion methods (first conversion method and second conversion method) based on whether the source data is within the target data range that the target data format can represent. This not only avoids the performance loss caused by uniformly adopting a complex conversion process, but also adapts to the dynamic range differences of different precision formats, ensuring that the converted target data still has practical significance in the target data format.

[0089] In some embodiments, the first conversion method includes a first method and a second method;

[0090] The above S202 determines that the first conversion method is used to directly convert the source data from the source data format to the destination data format, including one of the following:

[0091] If the data type of the source data is determined to be a non-numeric type or an infinite number type, the first method is adopted, which directly converts the source data from the source data format to the destination data format.

[0092] If the data type of the source data is determined to be an overflow data type, the second method is adopted to directly convert the source data from the source data format to the destination data format.

[0093] The first method is determined based on whether the target data format supports encoding non-numeric or infinite number data types; the second method is determined based on the overflow type of the source data, which includes overflow type and underflow type.

[0094] It should be noted that data of non-numeric or infinite number types (such as non-numeric NaN and infinite number Inf) can be considered special data. Such special data typically has specific encoding methods or rules. For example, for non-numeric NaN, its encoding usually involves all exponent bits being 1 and the mantissa bits not all being 0; for infinite number Inf, its encoding usually involves all exponent bits being 1 and the mantissa bits being 0. Therefore, by considering the source data and its format, it can be determined whether the source data is special data. If the source data is special data, the first method can be used to directly convert the source data from its source data format to the target data format. For example, if the source data is non-numeric NaN, the first method can be used to convert it to data that conforms to the non-numeric NaN encoding supported by the target data format. If the source data is not special data, then the source data is an overflow data type, and the second method can be used to directly convert the source data from its source data format to the target data format. This second method can convert the source data into target data that conforms to the target data format.

[0095] In some embodiments, if based on the source data, the exponent width and mantissa width of the source data format, it is determined that the exponent of the source data is all 1 and the mantissa is not all 0, then the source data is determined to be a non-number NaN, that is, the source data is special data.

[0096] In some embodiments, if based on the source data, the exponent width and mantissa width of the source data format, it is determined that the exponent bits of the source data are all 1 and the mantissa bits are all 0, then the source data is determined to be an infinite number Inf, that is, the source data is special data.

[0097] In some embodiments, if the source data is a non-NaN, a first method can be used to convert the source data into a non-NaN that conforms to the target data format as the output of the converted target data.

[0098] In some embodiments, if the source data is an infinite number Inf, a first method can be used to convert the source data into an infinite number Inf that conforms to the target data format as the output of the converted target data.

[0099] In some embodiments, if the source data is 0, a first method can also be used to convert the source data into 0 that conforms to the target data format as the output of the converted target data, while retaining the sign bit of the source data.

[0100] In some embodiments, if the source data is of an overflow or underflow type, a second method can be used to directly convert the source data from the source data format to the destination data format, and this second method can convert the source data into destination data that conforms to the destination data format.

[0101] It should be noted that, in this embodiment, the first method can be determined based on whether the target data format supports encoding non-numerical or infinite number data. For example, assuming the target data format supports encoding non-numerical or infinite number data, the first method is determined to be the encoding method for non-numerical or infinite number data corresponding to the target data format; assuming the target data format does not support encoding non-numerical or infinite number data, the first method is determined to be the encoding method corresponding to the maximum value data that the target data format can represent. In this embodiment, the second method can also be determined based on the overflow type of the source data (including overflow type and underflow type). For example, assuming the overflow type of the source data is overflow type, the second method is determined to be the encoding method corresponding to the maximum value data that the target data format can represent; assuming the overflow type of the source data is underflow type, the second method is determined to be the encoding method corresponding to 0 in the target data format.

[0102] It is understood that the embodiments of this application, by supporting the encoding of non-numeric or infinite number types of data based on the target data format, automatically select to retain, replace with the maximum value, or mark as a special value, avoiding invalid values ​​(such as non-numeric NaN) from affecting the calculation results, while maintaining the consistency of mathematical semantics across formats (such as infinite number Inf still being represented as infinite number Inf during FP32→FP16 conversion); moreover, by distinguishing between overflow data (values ​​that are too large) and underflow data (values ​​that are too small), and using saturation truncation (clamping to the maximum / minimum value of the target format) or underflow to zero method respectively, the effective information can be preserved to the maximum extent and the accuracy loss caused by brute force truncation can be reduced.

[0103] In some embodiments, the first approach includes a first sub-approach and a second sub-approach;

[0104] The first method is adopted, which directly converts the source data from the source data format to the destination data format, including one of the following:

[0105] If it is determined that the target data format supports encoding of non-numeric or infinite number data, the first sub-method is adopted to directly convert the source data from the source data format to the target data format. The first sub-method is the encoding method of non-numeric or infinite number data corresponding to the target data format.

[0106] If it is determined that the target data format does not support encoding non-numeric or infinite number data types, the second sub-method is adopted to directly convert the source data from the source data format to the target data format. The second sub-method is the encoding method corresponding to the maximum value data that the target data format can represent.

[0107] In this embodiment, the encoding of the exponent and mantissa bits of the source data can be determined based on the source data and the exponent and mantissa bit widths of the source data format. Then, based on the encoding of the exponent and mantissa bits of the source data, it can be determined whether the source data is a non-number NaN or an infinite number Inf. If the source data is determined to be a non-number NaN or an infinite number Inf, it can be determined whether the destination data format supports encoding the non-number NaN or the infinite number Inf. For example, if the source data is determined to be an infinite number Inf, it can be determined whether the destination data format supports encoding the infinite number Inf; or, if the source data is determined to be a non-number NaN, it can be determined whether the destination data format supports encoding the non-number NaN. If it is determined that the target data format supports encoding non-Number NaN or infinite number Inf, then the first sub-method is adopted, which directly converts the source data from the source data format to the target data format. The first sub-method is the encoding method for non-Number NaN or infinite number Inf corresponding to the target data format. If it is determined that the target data format does not support encoding non-Number NaN or infinite number Inf, then the second sub-method is adopted, which directly converts the source data from the source data format to the target data format. The second sub-method is the encoding method corresponding to the maximum value data that the target data format can represent.

[0108] In some embodiments, indication information may be obtained regarding whether the target data format supports encoding non-numeric or infinite number data types, and this indication information may be included in the input data.

[0109] In some embodiments, if the source data is a non-NaN and the destination data format supports encoding non-NaN, a first sub-method can be used to convert the source data into a non-NaN that conforms to the destination data format as the converted destination data output; if the destination data format does not support encoding non-NaN (for example, the FP4 data format does not support encoding non-NaN), a second sub-method can be used to convert the source data into the maximum value that the destination data format can represent as the converted destination data output.

[0110] In some embodiments, if the source data is an infinite number Inf and the destination data format supports encoding an infinite number Inf, the first sub-method can be used to convert the source data into an infinite number Inf that conforms to the destination data format as the output of the converted destination data. If the destination data format does not support encoding an infinite number Inf (for example, the FP4 data format does not support encoding an infinite number Inf), the second sub-method can be used to convert the source data into the maximum value that the destination data format can represent as the output of the converted destination data.

[0111] In some embodiments, the maximum value that the target data format can represent is data where both the exponent and mantissa bits are 1.

[0112] For example, Figure 3 This is the third flowchart illustrating a data format conversion method provided in an embodiment of this application, as shown below. Figure 3 As shown, the method includes:

[0113] S301. Obtain the source data to be converted, the exponent width and mantissa width of the source data format corresponding to the source data, and the exponent width and mantissa width of the destination data format.

[0114] S302. Based on the source data, the exponent width and mantissa width of the source data format, if the source data is determined to be a non-number NaN or an infinite number Inf, determine whether the destination data format supports encoding non-number NaN or infinite number Inf.

[0115] S303. If it is determined that the target data format supports encoding of non-number NaN or infinite number Inf, the first sub-method is adopted to directly convert the source data from the source data format to the target data format, and the first sub-method is: the encoding method of non-number NaN or infinite number Inf corresponding to the target data format.

[0116] S304. If it is determined that the target data format does not support encoding non-numerical NaN or infinite number Inf, the second sub-method is adopted to directly convert the source data from the source data format to the target data format. The second sub-method is the encoding method corresponding to the maximum value data that the target data format can represent.

[0117] It should be noted that the descriptions of the same steps and contents as in other embodiments in this embodiment can be found in the descriptions in other embodiments, and will not be repeated here.

[0118] It is understood that, for cases where the target data format supports encoding non-numerical or infinite data types, the first sub-method is used to directly inherit the special value encoding of the source data (e.g., in FP32→FP16, the infinite number Inf remains infinite), ensuring that the mathematical operation attributes remain unchanged after the format conversion; while for cases where the target data format does not support encoding non-numerical or infinite data types, the second sub-method is used to map the special value to the maximum value, and abnormal information is retained by explicit marking, realizing traceable abnormal handling of data format conversion.

[0119] In some embodiments, the second approach includes a third sub-approach and a fourth sub-approach;

[0120] The second method is adopted, which directly converts the source data from the source data format to the destination data format, including one of the following:

[0121] If the overflow type of the source data is determined to be an overflow type, the third sub-method is adopted to directly convert the source data from the source data format to the destination data format. The third sub-method is the encoding method corresponding to the maximum value data that the destination data format can represent.

[0122] If the overflow type of the source data is determined to be an underflow type, the fourth sub-method is adopted to directly convert the source data from the source data format to the destination data format. The fourth sub-method is the encoding method corresponding to 0 in the destination data format.

[0123] It should be noted that if the source data overflow type is overflow, it indicates that the source data has overflowed relative to the destination data format, that is, the value represented by the source data exceeds the maximum value that the destination data format can represent; if the source data overflow type is underflow, it indicates that the source data has underflowed relative to the destination data format, that is, the value represented by the source data exceeds the minimum value that the destination data format can represent.

[0124] In some embodiments, the exponent value of the source data can be determined based on the source data and the exponent bit width of the source data format, and then the magnitude of the exponent value of the source data can be judged. If it is determined that the exponent value of the source data is greater than the value obtained by summing the offset corresponding to the source data format and the offset corresponding to the destination data format, it indicates that the source data has overflowed relative to the destination data format, that is, the overflow type of the source data is an overflow type. In this case, a third sub-method is adopted to directly convert the source data from the source data format to the destination data format, and the third sub-method is: the encoding method corresponding to the maximum value data that the destination data format can represent. That is to say, if it is determined that the source data has overflowed relative to the destination data format, the maximum value data that the destination data format can represent is output as the destination data.

[0125] It should be noted that the offset corresponding to the source data format is determined based on the exponent width of the source data format, and the offset corresponding to the destination data format is determined based on the exponent width of the destination data format.

[0126] It should be noted that in the IEEE 754 standard, in order to expand the representation range of the exponent (the IEEE 754 standard chooses the exponent to represent the exponent), make the positive and negative ranges that the exponent can represent symmetrically, and ensure that 0 and special values ​​(such as the infinity number Inf and the non-Number NaN) have explicit representations, an offset for the exponent is defined. The actual value of the exponent can be obtained by subtracting the offset from the binary value of the exponent. This approach not only simplifies the calculation of the exponent but also ensures the accuracy and efficiency of floating-point number representation.

[0127] The following example uses the FP32 data format, where the sign bit width is 1 bit, the exponent bit width is 8 bits, and the mantissa bit width is 23 bits. The 8-bit exponent range is [0, 255]. Shifting it halfway to the left gives [-127, 128]. This means that subtracting 127 from the binary exponent value gives the true exponent value; this subtraction (127) is called the offset. Offset = 2. n-1 -1, where n represents the exponent width. For a single-precision floating-point number, the exponent width n = 8, so the offset of the single-precision floating-point number is 127; for a half-precision floating-point number, the exponent width n = 5, so the offset of the half-precision floating-point number is 15.

[0128] In some embodiments, the exponent value of the source data is defined as exp. src The last digit is mant src The exponent width of the source data format is exp. src_width The width of the mantissa is mant src_width The exponent width of the target data format is exp. dst_width The width of the mantissa is mant dst_width The offset corresponding to the source data format (i.e., the exponent of the source data) is exp. src_bias The offset corresponding to the target data format is exp dst_bias If exp src >exp src_bias +exp dst_bias If the source data overflows relative to the target data format, then the source data is converted to the maximum value that the target data format can represent based on the third sub-method, and the target data is output.

[0129] In some embodiments, if the exponent value of the source data is determined to be less than the value obtained by subtracting the offset of the destination data format and the mantissa width of the destination data format from the offset corresponding to the source data format, it indicates that the source data has underflowed relative to the destination data format, i.e., the overflow type of the source data is underflow. In this case, a fourth sub-method can be used to directly convert the source data from the source data format to the destination data format, and this fourth sub-method is: the encoding method corresponding to 0 in the destination data format. That is, if it is determined that the source data has underflowed relative to the destination data format, then 0 conforming to the destination data format is output as the destination data.

[0130] In some embodiments, the exponent value of the source data is defined as exp. src The offset corresponding to the source data format is exp src_bias The offset corresponding to the target data format is exp dst_bias The mantissa width of the target data format is mant dst_width If exp src <exp src_bias -exp dst_bias -mant dst_width If the source data underflows relative to the destination data format, then the fourth sub-method outputs 0 that conforms to the destination data format, and retains the sign bit of the source data as the destination data.

[0131] It is understood that, for overflow type source data, a third sub-method is used to convert the source data into the maximum value that the target data format can represent as the target data, that is, the source data is converted into the maximum value of the target data format using a saturation truncation method; for underflow type source data, a fourth sub-method is used to convert the source data into 0 of the target data format as the target data, that is, the source data is directly reduced to zero to simplify the calculation.

[0132] In some embodiments, determining to employ a third sub-method to directly convert source data from a source data format to a destination data format includes:

[0133] If the target data format is determined to support encoding of non-numeric or infinite number data types, a third sub-method is adopted to directly convert the source data from the source data format to the target data format. The third sub-method is the encoding method corresponding to the maximum value data that the target data format can represent. This encoding method encodes the last exponent bit of the maximum value data as 0, encodes the remaining exponent bits as 1, and encodes all the mantissa bits of the maximum value data as 1.

[0134] In this embodiment of the application, if it is determined that the source data has overflowed relative to the target data format, and the target data format supports encoding non-Number NaN or infinite number Inf, then a third sub-method is adopted to directly convert the source data from the source data format to the target data format. The third sub-method is: the encoding method corresponding to the maximum value data that the target data format can represent. The encoding method corresponding to the maximum value data that the target data format can represent is to encode the last exponent bit of the maximum value data as 0, encode the remaining exponent bits as 1, and encode the mantissa bits of the maximum value data as 1. That is, it is determined that the maximum value data that the target data format can represent is the maximum normalized number (Max Normal), which can ensure that the normalized data still remains within its original data range.

[0135] For example, if the target data format is FP8, and the exponent width of the FP8 data format is 5 bits and the mantissa width is 2 bits, then the binary representation of the maximum value data that the FP8 data format can represent is 011110 11.

[0136] In some embodiments, if it is determined that the source data has overflowed relative to the destination data format, and the destination data format supports encoding infinite numbers Inf, a third sub-method can be used to convert the source data into infinite numbers Inf that conform to the destination data format as the destination data output.

[0137] In some embodiments, if it is determined that the source data has overflowed relative to the destination data format, and the destination data format supports encoding an infinite number Inf, a third sub-method can be determined based on a preset mantissa rounding mode to output the maximum value data that the destination data format can represent as the destination data, or a third sub-method can be determined to output an infinite number Inf that conforms to the destination data format as the destination data.

[0138] It should be noted that the preset rounding modes include, but are not limited to, rounding modes close to even numbers, rounding modes close to 0, positive infinity rounding modes, and negative infinity rounding modes.

[0139] It is understood that, in this application embodiment, by determining that the source data has overflowed relative to the target data format, and the target data format supports encoding non-Number NaN or infinite number Inf, the third sub-method is adopted to output the maximum value data that the target data format can represent as the maximum normalized number as the target data, which can ensure that the normalized data still remains within its original data range.

[0140] In some embodiments, the second conversion method includes a third method and a fourth method;

[0141] The above S203 specifies the use of a second conversion method, which directly converts the source data from the source data format to the destination data format, including one of the following:

[0142] If the data range of the source data is determined to be a non-normalized data range, a third method is adopted to directly convert the source data from the source data format to the target data format. The third method is as follows: the first index value of the first target data that conforms to the target data format is determined to be 0; and based on the source data, the source data format, and the target data format, the first tail value of the first target data that conforms to the target data format is determined; and based on the first index value and the first tail value, the first target data obtained after conversion is determined.

[0143] Given that the data range of the source data is determined to be the normalized data range, the fourth method is adopted to directly convert the source data from the source data format to the target data format. The fourth method is as follows: based on the source data, the source data format, and the target data format, the second tail value and the second exponent value of the second target data that conform to the target data format are determined respectively; based on the second exponent value and the second tail value, the converted second target data is determined.

[0144] In this embodiment, if the exponent value of the source data is greater than or equal to the value obtained by subtracting the offset of the target data format and the mantissa width of the target data format from the offset corresponding to the source data format, and the exponent value of the source data is less than the value obtained by subtracting the offset of the target data format from the offset corresponding to the source data format and adding 1, then it indicates that the source data is within the range of non-normalized data. In this case, a third method can be used to directly convert the source data from the source data format to the target data format. The third method is as follows: the first exponent value of the first target data that conforms to the target data format is determined to be 0, and the first mantissa value of the first target data that conforms to the target data format is determined based on the source data, the source data format, and the target data format. Finally, the first target data obtained by conversion is determined based on the first exponent value and the first mantissa value.

[0145] It is understood that, in the embodiments of this application, for source data that is not within the normalized data range, a third method can be used to convert the source data into first destination data, and the exponent value of the first destination data is 0. The tail value of the first destination data is obtained based on the source data, the source data format, and the destination data format.

[0146] In this embodiment of the application, if it is determined that the exponent value of the source data is greater than or equal to the value obtained by subtracting the offset corresponding to the target data format from the offset corresponding to the source data format and then adding 1, and the exponent value of the source data is less than or equal to the value obtained by summing the offset corresponding to the source data format and the offset corresponding to the target data format, then it indicates that the source data is within the normalized data range. In this case, a fourth method can be used to directly convert the source data from the source data format to the target data format. The fourth method is as follows: based on the source data, the source data format, and the target data format, determine the second tail value and the second exponent value of the second target data that conforms to the target data format; and based on the second exponent value and the second tail value, determine the converted second target data.

[0147] It is understood that, in the embodiments of this application, for source data within a normalized data range, the fourth method can be used to convert the source data into second destination data, and the exponent value and tail value of the second destination data are both obtained based on the source data, the source data format, and the destination data format.

[0148] In some embodiments, the offset corresponding to the source data format can be determined based on the exponent bit width of the source data format. Similarly, the offset corresponding to the destination data format can be determined based on the exponent bit width of the destination data format.

[0149] It is understood that the embodiments of this application perform data format conversion by employing different data format conversion methods for source data within the non-normalized data range and the normalized data range, respectively. Specifically, for source data within the non-normalized data range, the exponent value is forced to 0, which avoids the erroneous amplification or loss of extremely small values ​​under low-precision formats, and dynamically adjusts the mantissa to retain only the valid mantissa digits, reducing precision loss during non-normalized data conversion. For source data within the normalized data range, the exponent value and mantissa value are calculated independently, maximizing the retention of valid information and ensuring the accuracy of data format conversion.

[0150] In some embodiments, determining the first tail value of the first destination data conforming to the destination data format based on the source data, the source data format, and the destination data format includes:

[0151] The first tail value of the first destination data is determined based on the tail value of the source data, the tail width of the source data format, the exponent value of the source data, the offset corresponding to the source data format, the offset corresponding to the destination data format, and the tail width of the destination data format.

[0152] It's important to note that the mantissa of the source data is determined based on the width of the mantissa bits in the source data and its format, while the exponent value is determined based on the width of the exponent bits in the source data and its format. In other words, the mantissa of the source data is determined by both its binary representation and the width of the mantissa bits in the format. The mantissa is typically parsed as an implicit leading 1 or a directly stored decimal. Its actual value is calculated by summing the weighted binary decimal bits and applying implicit rules (such as normalization). The exponent value of the source data is determined by the binary exponent bits and the width of the exponent bits in the data format. First, the exponent bits are parsed as an unsigned integer, then a fixed offset is subtracted to obtain the actual exponent value (which may be negative).

[0153] It is understood that, in the embodiments of this application, a first tail value can be calculated using relevant tail value calculation methods based on the tail value of the source data, the tail width of the source data format, the exponent value of the source data, the offset corresponding to the source data format, the offset corresponding to the destination data format, and the tail width of the destination data format. This first tail value is the tail value of the first destination data obtained by conversion. That is, by comprehensively considering parameters such as the tail width, exponent value, and offset of the source data format and / or the destination data format, high-precision preservation of the tail value can be achieved.

[0154] In some embodiments, determining the first tail value of the first destination data based on the tail value of the source data, the tail width of the source data format, the exponent value of the source data, the offset corresponding to the source data format, the offset corresponding to the destination data format, and the tail width of the destination data format includes:

[0155] The value 1 is shifted left by the mantissa width of the source data format, and then bitwise ORed with the mantissa of the source data to obtain the first value.

[0156] The second value is obtained by subtracting the offset corresponding to the source data format and the exponent value of the source data from the value 1 and summing it with the offset corresponding to the destination data format; and the third value is obtained by subtracting the mantissa width of the destination data format from the mantissa width of the source data format.

[0157] Shift the first value to the right by the second value position, and then shift it to the right by the third value position to obtain the fourth value.

[0158] Based on the fourth value, determine the first tail value of the first target data.

[0159] In this embodiment of the application, when converting the source data into a data format, if it is determined that the source data is within the range of non-normalized data, the first tail value of the first destination data obtained after the source data has been converted into a data format can be determined based on the following steps 1 to 3: Step 1: Shift the value 1 to the left by the width of the mantissa of the source data format, and then perform a bitwise OR operation with the mantissa of the source data to obtain the first value. At the same time, sum the value 1 minus the offset corresponding to the source data format and the exponent value of the source data with the offset corresponding to the destination data format to obtain the second value. Subtract the width of the mantissa of the destination data format from the width of the mantissa of the source data format to obtain the third value. Step 2: Shift the first value to the right by the second value bit and then to the right by the third value bit to obtain the fourth value. Step 3: Determine the first tail value of the first destination data based on the fourth value.

[0160] It should be noted that the embodiments of this application are aimed at converting high-precision floating-point data into low-precision floating-point data. Therefore, the mantissa width of the source data format must be greater than the mantissa width of the destination data format.

[0161] In some embodiments, the exponent value of the source data is defined as exp. src The offset corresponding to the source data format is exp src_bias The offset corresponding to the target data format is exp dst_bias The mantissa width of the target data format is mant dst_width If the exponent value of the source data is exp src In [exp] src_bias -exp dst_bias -mant dst_width ,exp src_bias +1-exp dst_bias If the source data falls within the range of non-normalized data, then the exponential value exp of the first target data is output. dst =0, output the last value of the first destination data (mant) dst =((1< <mant src_width )|mant src )>>(1-exp dst_bias +exp src_bias -exp src )>>(mant src_width -mant dst_width ).

[0162] In some embodiments, the sign value (Sign) and exponent value (exp) of the calculated first target data can be used. dst sum of last digits (mant) dst Concatenate the data into the first target data according to the target data format. For example, use the expression Sign<<(expdst_width +mant dst_width )|exp dst < <mant dst_width |mant dst This will allow you to obtain the data for your primary objective.

[0163] It is understood that the embodiments of this application shift the binary representation of the value 1 to the left. src_width The result is a mant that is only on the leftmost position. src_width A mask is generated from a number with 1 in one bit and 0 in the rest. Then, the mask is bitwise ORed with the mantissa of the source data. The mask is then right-shifted according to the exponent of the source data, the offset corresponding to the source data format, the offset corresponding to the destination data format, the mantissa width of the source data format, and the mantissa width of the destination data format, so as to accurately obtain the first mantissa of the first destination data.

[0164] In some embodiments, determining the first tail value of the first destination data based on the fourth value includes:

[0165] Based on the fourth value and the preset mantissa rounding mode, determine the first mantissa value of the first target data.

[0166] In this embodiment of the application, when determining the first tail value of the first target data based on the fourth value, the first tail value of the first target data can be determined by combining a preset tail rounding mode. The preset tail rounding mode includes, but is not limited to, a rounding mode close to even numbers, a rounding mode close to 0, a positive infinity rounding mode, and a negative infinity rounding mode.

[0167] In some embodiments, if it is determined that the fourth value needs to be rounded up based on a preset mantissa rounding mode, then 1 is added to the fourth value to obtain the first mantissa value of the first destination data.

[0168] For example, suppose we need to convert source data in FP32 format to destination data in FP8 format, and the default rounding mode is a rounding mode close to even numbers. After obtaining the fourth value, we can determine whether the first 20 bits of the fourth value are all 0s. If they are not all 0s, we can determine whether the next third bit is 0 or 1. If it is 0, we do not carry over; if it is 1, we carry over. If the first 20 bits are all 0s, we can determine whether the next third bit is 0 or 1. If it is 0, we do not carry over; if it is 1, we can determine whether the next second bit is 0 or 1. If it is 0, we do not carry over; if it is 1, we carry over.

[0169] It is understood that, in calculating the first tail value of the first target data, the embodiments of this application take into account the tail rounding mode, which can improve the accuracy of data format conversion.

[0170] In some embodiments, determining the second tail value and the second exponent value of the second destination data conforming to the destination data format, based on the source data, the source data format, and the destination data format, includes:

[0171] Based on the index value of the source data, the offset corresponding to the source data format, and the offset corresponding to the destination data format, determine the second index value of the second destination data;

[0172] The second tail value of the second destination data is determined based on the tail value of the source data, the tail width of the source data format, and the tail width of the destination data format.

[0173] It should be noted that the second destination data is obtained by converting the source data into a different format. Specifically, the exponent value of the second destination data is the second exponent value, and the tail value is the second tail value.

[0174] In this embodiment, the second exponent value of the second destination data can be calculated using a relevant exponent value calculation method based on the exponent value of the source data, the offset corresponding to the source data format, and the offset corresponding to the destination data format. That is, by considering the offsets of the source and destination data formats when calculating the exponent value, numerical step errors caused by inconsistencies in offsets can be avoided. Furthermore, the second tail value of the second destination data can be calculated using a relevant tail value calculation method based on the tail value of the source data, the tail width of the source data format, and the tail width of the destination data format. That is, by considering the tail width of the source and destination data formats when calculating the tail value, lossless adjustment of the tail value can be achieved.

[0175] In some embodiments, determining the second exponent value of the second destination data based on the exponent value of the source data, the offset corresponding to the source data format, and the offset corresponding to the destination data format includes:

[0176] The second exponent value of the second destination data is obtained by subtracting the offset corresponding to the source data format from the exponent value of the source data and summing it with the offset corresponding to the destination data format.

[0177] In this embodiment of the application, when it is determined that the source data is within the normalized data range, a fourth method can be used to directly convert the source data from the source data format to the destination data format. Specifically, in this fourth method, the second exponent value of the second destination data can be obtained by subtracting the offset corresponding to the source data format from the exponent value of the source data and summing this sum with the offset corresponding to the destination data format.

[0178] In some embodiments, the exponent value of the source data is defined as exp. src The offset corresponding to the source data format is exp src_biasThe offset corresponding to the target data format is exp dst_bias If the exponent value of the source data is exp src In [exp] src_bias +1-exp dst_bias ,exp src_bias +exp dst_bias If the source data falls within the normalized data range, then the second exponential value exp of the second target data is output. dst =exp src -exp src_bias +expdst_bias.

[0179] It is understood that, in this embodiment of the application, when the source data is determined to be within the normalized data range, the second index value of the second target data can be effectively obtained by subtracting the offset corresponding to the source data format from the index value of the source data and summing it with the offset corresponding to the target data format. That is, through precise offset compensation calculation, the consistency of the index value across formats can be ensured.

[0180] In some embodiments, determining the second mantissa of the second destination data based on the mantissa value of the source data, the mantissa width of the source data format, and the mantissa width of the destination data format includes:

[0181] The sixth value is obtained by shifting the last digit of the source data to the right by five digits; the fifth value is obtained by subtracting the last digit width of the destination data format from the last digit width of the source data format.

[0182] Based on the sixth value, the second tail value of the second objective data is determined.

[0183] In this embodiment of the application, when it is determined that the source data is within the normalized data range, the fifth value can be obtained by subtracting the mantissa width of the destination data format from the mantissa width of the source data format. Then, the mantissa value of the source data is shifted to the right by the fifth value to obtain the sixth value. Based on the sixth value, the second mantissa value of the second destination data is determined.

[0184] In some embodiments, the exponent value of the source data is defined as exp. src The offset corresponding to the source data format is exp src_bias The offset corresponding to the target data format is exp dst_bias The source data's last digit is mant src The source data format has a mant width of 0.05. src_width The mantissa width of the target data format is mant dst_width If the exponent value of the source data is exp src In [exp] src_bias +1-exp dst_bias,exp src_bias +exp dst_bias If the source data falls within the normalized data range, then the second exponential value exp of the second target data is output. dst =exp src -exp src_bias +exp dst_bias Output the second tail value of the second target data (mant) dst =mant src >>(mant src_width -mant dst_width ).

[0185] It is understood that, in this embodiment of the application, when the source data is determined to be within the normalized data range, the second tail value of the second destination data can be effectively obtained by right-shifting the tail value of the source data by the difference in the tail width of the source data format and the tail width of the destination data format by the number of bits. That is, the tail value is precisely adjusted by dynamic shifting driven by the difference in tail width between the source data format and the destination data format.

[0186] In some embodiments, determining the second tail value of the second target data based on the sixth value includes:

[0187] Based on the sixth value and the preset mantissa rounding mode, the second mantissa value of the second target data is determined.

[0188] In this embodiment of the application, when determining the second tail value of the second target data based on the sixth value, the second tail value of the second target data can be determined by combining a preset tail rounding mode. The preset tail rounding mode includes, but is not limited to, a rounding mode close to even numbers, a rounding mode close to 0, a positive infinity rounding mode, and a negative infinity rounding mode.

[0189] In some embodiments, if it is determined that the sixth value needs to be rounded up based on a preset mantissa rounding mode, then 1 is added to the sixth value to obtain the second mantissa value of the second target data.

[0190] For example, suppose we need to convert source data in FP32 format to a second destination data in FP8 format, and the default rounding mode is a rounding mode close to even numbers. After obtaining the sixth value, we can determine whether the first 20 bits of the sixth value are all 0s. If they are not all 0s, we can determine whether the next third bit is 0 or 1. If it is 0, we do not carry over; if it is 1, we carry over. If the first 20 bits are all 0s, we can determine whether the next third bit is 0 or 1. If it is 0, we do not carry over; if it is 1, we can determine whether the next second bit is 0 or 1. If it is 0, we do not carry over; if it is 1, we carry over.

[0191] It is understood that, in calculating the second tail value of the second target data, the embodiments of this application take into account the tail rounding mode, which can improve the accuracy of data format conversion.

[0192] The overall process of a data format conversion method provided in this application embodiment is described below. For example, Figure 4 This is a fourth flowchart illustrating a data format conversion method provided in an embodiment of this application, as shown below. Figure 4 As shown, the method includes:

[0193] S401. Obtain the source data to be converted, the exponent width and mantissa width of the source data format corresponding to the source data, and the exponent width and mantissa width of the destination data format; the mantissa width of the source data format is greater than the mantissa width of the destination data format.

[0194] S402. Based on the source data, the exponent width and mantissa width of the source data format, if the source data is determined to be special data, determine whether the destination data format supports encoding special data; special data includes not-a-number NaN and infinite number Inf.

[0195] S403. If it is determined that the target data format supports encoding special data, the first sub-method is adopted to directly convert the source data from the source data format to the target data format, and the first sub-method is: the encoding method of the special data corresponding to the target data format.

[0196] S404. If it is determined that the destination data format does not support encoding special data, a second sub-method is adopted to directly convert the source data from the source data format to the destination data format. The second sub-method is the encoding method corresponding to the maximum value data that the destination data format can represent.

[0197] S405. Based on the source data, the exponent width and mantissa width of the source data format, and assuming that the source data is not special data, determine whether the source data is overflow data, underflow data, non-normalized data, or normalized data.

[0198] S406. When it is determined that the source data is overflow data, a third sub-method is adopted to directly convert the source data from the source data format to the destination data format. The third sub-method is the encoding method corresponding to the maximum value data that the destination data format can represent.

[0199] S407. When it is determined that the source data is underflow data, the fourth sub-method is adopted to directly convert the source data from the source data format to the destination data format, and the fourth sub-method is: the encoding method corresponding to 0 in the destination data format.

[0200] S408. When it is determined that the source data is non-normalized data, a third method is adopted to directly convert the source data from the source data format to the destination data format. The third method is as follows: the first index value that conforms to the destination data format is determined to be 0; and based on the source data, the source data format and the destination data format, the first tail value of the first destination data that conforms to the destination data format is determined; and based on the first index value and the first tail value, the first destination data obtained by conversion is determined.

[0201] S409. When the source data is determined to be normalized data, the fourth method is adopted to directly convert the source data from the source data format to the destination data format. The fourth method is as follows: based on the source data, the source data format, and the destination data format, the second tail value and the second exponent value of the second destination data that conform to the destination data format are determined respectively; based on the second exponent value and the second tail value, the converted second destination data is determined.

[0202] It should be noted that, in this embodiment, the source data is first determined to be special data based on the source data and the exponent and mantissa widths of the source data format. If the source data is special data, it is further determined whether the target data format supports encoding special data; if it does, data format conversion is performed according to step S403; if it does not, data format conversion is performed according to step S404. If the source data is not special data, it is further determined whether the source data has overflowed or underflowed, or whether the source data is within the normalized data range or the non-normalized data range. If the source data has overflowed, data format conversion is performed according to step S406; if the source data has underflowed, data format conversion is performed according to step S407; if the source data is within the non-normalized data range, data format conversion is performed according to step S408; if the source data is within the normalized data range, data format conversion is performed according to step S409.

[0203] It should be noted that the descriptions of the same steps and contents as in other embodiments in this embodiment can be found in the descriptions in other embodiments, and will not be repeated here.

[0204] It is understood that the data format conversion method provided in this application only needs to execute some judgment logic to convert any high-precision data into any low-precision data. Moreover, the conversion logic is simple, highly universal, and highly scalable.

[0205] It should be noted that the data format conversion method provided in the embodiments of this application can be implemented by a data format conversion system. For example, Figure 5 This is a schematic diagram of the structure of a data format conversion system provided in an embodiment of this application, such as... Figure 5As shown, the system includes: a data input module 510, a data format conversion module 520, and a data output module 530. The data format conversion module 520 includes a special value judgment unit 521, an overflow judgment unit 522, an underflow judgment unit 523, and a normal data processing unit 524. The normal data processing unit 524 includes an exponent calculation unit 5241 and a mantissa calculation unit 5242. Wherein:

[0206] The data input module 510 is used to record source data, the exponent width and mantissa width of the source data format corresponding to the source data, and the exponent width and mantissa width of the destination data format.

[0207] The data format conversion module 520 is used to convert the input data format. Its special value judgment unit 521 is used to convert the data format of special data, the overflow judgment unit 522 is used to convert the data format of overflowing data, the underflow judgment unit 523 is used to convert the data format of underflowing data, the normal data processing unit 524 is used to convert the data format of data within the normalized data range and data outside the normalized data range, the exponent calculation unit 5241 is used to calculate the exponent value of the target data to be converted, and the mantissa calculation unit 5242 is used to calculate the mantissa value of the target data to be converted.

[0208] The data output module 530 is used to output the target data obtained after converting the source data into a data format.

[0209] In some embodiments, the data format conversion system further includes a data storage device for storing input data, output data, and a computer program for implementing data format conversion.

[0210] It is understood that the data format conversion system provided in this application directly outputs the result based on the source data, source data format, and destination data format using a format conversion algorithm. It can convert high-precision floating-point data to low-precision floating-point data in just one step, without the need for intermediate resource caching of intermediate results. Furthermore, it does not require a dedicated data format conversion method for each data format; the hardware only needs to implement one method for converting high-precision floating-point data to low-precision floating-point data, resulting in high execution efficiency and a short execution pipeline. If new floating-point data formats emerge subsequently, this application embodiment can quickly adapt without further hardware expansion, requiring only the addition of software instructions containing judgment logic, thus achieving high scalability and better versatility. Moreover, since it does not occupy intermediate resources and has only one set of conversion logic, it occupies a small chip area, has low overhead, and low power consumption.

[0211] The data format conversion apparatus provided in the embodiments of this application is described below. The data format conversion apparatus described below can be referred to in correspondence with the data format conversion method described above.

[0212] Figure 6 This is a schematic diagram of the structure of a data format conversion device provided in an embodiment of this application, such as... Figure 6 As shown, the device includes: an acquisition module 610 and a conversion module 620; wherein:

[0213] The acquisition module 610 is used to acquire the source data to be converted, the source data format corresponding to the source data, and the destination data format;

[0214] The conversion module 620 is used to determine the target conversion method to directly convert the source data from the source data format to the target data format based on the source data, the source data format, and the target data format.

[0215] The data format conversion device provided in this application embodiment obtains the source data to be converted, the source data format corresponding to the source data, and the target data format. Then, based on the source data, the source data format, and the target data format, it directly converts the source data from the source data format to the target data format using a target conversion method. Compared with traditional data format conversion methods, this application embodiment only requires a single-step conversion to convert the source data from the source data format to the target data format, without the need for intermediate resource caching of intermediate results. The logic implementation is simple and highly versatile.

[0216] In some embodiments, the target conversion method includes a first conversion method and a second conversion method;

[0217] The first conversion method is determined based on the data type of the source data; the second conversion method is determined based on the data range of the source data.

[0218] In some embodiments, the conversion module 620 includes:

[0219] The first conversion unit is used to determine, when it is determined that the source data is not within the range of target data that can be represented by the target data format, to adopt the first conversion method and directly convert the source data from the source data format to the target data format.

[0220] The second conversion unit is used to determine, when it is determined that the source data is within the range of target data that the target data format can represent, to adopt the second conversion method to directly convert the source data from the source data format to the target data format.

[0221] In some embodiments, the first conversion method includes a first method and a second method;

[0222] The first conversion unit is also used to: determine, when the data type of the source data is determined to be a non-number type or an infinite number type, adopt the first method to directly convert the source data from the source data format to the destination data format; and determine, when the data type of the source data is determined to be an overflow data type, adopt the second method to directly convert the source data from the source data format to the destination data format.

[0223] The first method is determined based on whether the target data format supports encoding non-numeric or infinite number data types; the second method is determined by the overflow type of the source data, which includes overflow type and underflow type.

[0224] In some embodiments, the first approach includes a first sub-approach and a second sub-approach;

[0225] The first conversion unit is further configured to: when it is determined that the target data format supports encoding of non-numeric or infinite data types, determine to adopt a first sub-method to directly convert the source data from the source data format to the target data format, wherein the first sub-method is: the encoding method of the non-numeric or infinite data types corresponding to the target data format; when it is determined that the target data format does not support encoding of non-numeric or infinite data types, determine to adopt a second sub-method to directly convert the source data from the source data format to the target data format, wherein the second sub-method is: the encoding method corresponding to the maximum value data that the target data format can represent.

[0226] In some embodiments, the second approach includes a third sub-approach and a fourth sub-approach;

[0227] The first conversion unit is also used to: when the overflow type of the source data is determined to be an overflow type, determine to adopt a third sub-method to directly convert the source data from the source data format to the destination data format, and the third sub-method is: the encoding method corresponding to the maximum value data that the destination data format can represent; when the overflow type of the source data is determined to be an underflow type, determine to adopt a fourth sub-method to directly convert the source data from the source data format to the destination data format, and the fourth sub-method is: the encoding method corresponding to 0 in the destination data format.

[0228] In some embodiments, the first conversion unit is further configured to: determine, when it is determined that the target data format supports encoding of non-numerical or infinite number data, adopt a third sub-method to directly convert the source data from the source data format to the target data format, and the third sub-method is: the encoding method corresponding to the maximum value data that the target data format can represent, and the encoding method is to encode the last exponent bit of the maximum value data as 0, encode the remaining exponent bits as 1, and encode the mantissa bits of the maximum value data as 1.

[0229] In some embodiments, the second conversion method includes a third method and a fourth method;

[0230] The second conversion unit is further configured to: when the data range of the source data is determined to be a non-normalized data range, determine to adopt a third method to directly convert the source data from the source data format to the target data format, wherein the third method is: setting the first exponent value of the first target data conforming to the target data format to 0; and determining the first tail value of the first target data conforming to the target data format based on the source data, the source data format, and the target data format; and determining the converted first target data based on the first exponent value and the first tail value; when the data range of the source data is determined to be a normalized data range, determine to adopt a fourth method to directly convert the source data from the source data format to the target data format, wherein the fourth method is: determining the second tail value and the second exponent value of the second target data conforming to the target data format based on the source data, the source data format, and the target data format; and determining the converted second target data based on the second exponent value and the second tail value.

[0231] In some embodiments, the second conversion unit is further configured to: determine the first tail value of the first destination data based on the tail value of the source data, the tail width of the source data format, the exponent value of the source data, the offset corresponding to the source data format, the offset corresponding to the destination data format, and the tail width of the destination data format.

[0232] In some embodiments, the second conversion unit is further configured to: shift the value 1 to the left by the mantissa width of the source data format, and then perform a bitwise OR operation with the mantissa value of the source data to obtain a first value; subtract the offset corresponding to the source data format from the value 1 and the exponent value of the source data, and then sum the result with the offset corresponding to the destination data format to obtain a second value; subtract the mantissa width of the destination data format from the mantissa width of the source data format to obtain a third value; shift the first value to the right by a second value bit and then to the right by a third value bit to obtain a fourth value; and determine the first mantissa value of the first destination data based on the fourth value.

[0233] In some embodiments, the second conversion unit is further configured to: determine the first tail value of the first destination data based on the fourth value and a preset tail rounding mode.

[0234] In some embodiments, the second conversion unit is further configured to: determine the second exponent value of the second destination data based on the exponent value of the source data, the offset corresponding to the source data format, and the offset corresponding to the destination data format; and determine the second tail value of the second destination data based on the tail value of the source data, the tail width of the source data format, and the tail width of the destination data format.

[0235] In some embodiments, the second conversion unit is further configured to: subtract the offset corresponding to the source data format from the exponent value of the source data and sum it with the offset corresponding to the destination data format to obtain the second exponent value of the second destination data.

[0236] In some embodiments, the second conversion unit is further configured to: shift the last digit of the source data to the right by a fifth digit to obtain a sixth digit; the fifth digit is obtained by subtracting the last digit width of the destination data format from the last digit width of the source data format; and determine the second last digit of the second destination data based on the sixth digit.

[0237] In some embodiments, the second conversion unit is further configured to: determine the second tail value of the second target data based on the sixth value and a preset tail rounding mode.

[0238] It should be noted that the data format conversion device provided in this application embodiment can implement all the method steps implemented in the above data format conversion method embodiment and can achieve the same technical effect. Here, the parts that are the same as those in the method embodiment and the beneficial effects will not be described in detail.

[0239] Figure 7 This is a schematic diagram of the physical structure of an electronic device provided in an embodiment of this application, such as... Figure 7 As shown, the electronic device may include a processor 710, a communications interface 720, a memory 730, and a communication bus 740, wherein the processor 710, the communications interface 720, and the memory 730 communicate with each other via the communication bus 740. The processor 710 can execute executable data instructions stored in the memory 730 to implement some or all of the steps in the data format conversion methods provided in the above embodiments.

[0240] Furthermore, the executable data instructions stored in the aforementioned memory 730 can be implemented as software functional units and sold or used as independent products, and can be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, or the parts that contribute to related technologies, can be embodied in the form of software products. These software products are stored in a storage medium and include several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods in the embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0241] This application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements some or all of the steps in the data format conversion methods provided in the above embodiments.

[0242] This application also provides a computer program product, which includes a computer program or instructions. When the computer program or instructions are executed by a processor, they implement some or all of the steps in the data format conversion methods provided in the above embodiments.

[0243] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.

[0244] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, embodiments of this application can take the form of hardware embodiments, software embodiments, or embodiments combining software and hardware aspects. Furthermore, embodiments of this application can take the form of computer program products implemented on one or more computer-usable storage media (including but not limited to disk storage and optical storage) containing computer-usable program code.

[0245] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0246] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a processFigure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0247] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0248] The above are merely optional embodiments of this application and are not intended to limit the scope of protection of this application.

Claims

1. A data format conversion method, characterized in that, The method includes: Obtain the source data to be converted to a floating-point data format, the source data format corresponding to the source data, and the destination data format; Based on the source data, the source data format, and the destination data format, it is determined that a target conversion method will be used to directly convert the source data from the source data format to the destination data format. The target conversion method includes a first conversion method and a second conversion method; The first conversion method is determined based on the data type of the source data; the second conversion method is determined based on the data range of the source data. Based on the source data, the source data format, and the destination data format, determining to directly convert the source data from the source data format to the destination data format using a target conversion method includes one of the following: If it is determined that the source data is not within the range of target data that can be represented by the destination data format, the first conversion method is adopted to directly convert the source data from the source data format to the destination data format. If it is determined that the source data is within the range of target data that can be represented by the target data format, then the second conversion method is adopted to directly convert the source data from the source data format to the target data format. The first conversion method includes a first method and a second method; The determination to use the first conversion method to directly convert the source data from the source data format to the destination data format includes one of the following: If it is determined that the data type of the source data is a non-number type or an infinite number type, the first method is adopted to directly convert the source data from the source data format to the destination data format. If it is determined that the data type of the source data is an overflow data type, the second method is adopted to directly convert the source data from the source data format to the destination data format. The first method is determined based on whether the target data format supports encoding the non-number type or the infinite number type of data; the second method is determined based on the overflow type of the source data, where the overflow type of the source data includes overflow type and underflow type. The second conversion method includes the third and fourth methods; The determination to use the second conversion method to directly convert the source data from the source data format to the destination data format includes one of the following: If it is determined that the data range of the source data is a non-normalized data range, the third method is adopted to directly convert the source data from the source data format to the destination data format. The third method is as follows: the first index value of the first destination data that conforms to the destination data format is determined to be 0; and based on the source data, the source data format, and the destination data format, the first tail value of the first destination data that conforms to the destination data format is determined; and based on the first index value and the first tail value, the converted first destination data is determined. If the data range of the source data is determined to be a normalized data range, the fourth method is adopted to directly convert the source data from the source data format to the destination data format. The fourth method is as follows: based on the source data, the source data format, and the destination data format, the second tail value and the second exponent value of the second destination data that conform to the destination data format are determined respectively; based on the second exponent value and the second tail value, the converted second destination data is determined.

2. The data format conversion method according to claim 1, characterized in that, The first method includes a first sub-method and a second sub-method; The determination to use the first method to directly convert the source data from the source data format to the destination data format includes one of the following: If it is determined that the target data format supports encoding of the non-number type or the infinite number type of data, the first sub-method is adopted to directly convert the source data from the source data format to the target data format, and the first sub-method is: the encoding method of the non-number type or the infinite number type of data corresponding to the target data format; If it is determined that the target data format does not support the encoding of the non-number type or the infinite number type, the second sub-method is adopted to directly convert the source data from the source data format to the target data format, and the second sub-method is the encoding method corresponding to the maximum value data that the target data format can represent.

3. The data format conversion method according to claim 1, characterized in that, The second method includes a third sub-method and a fourth sub-method; The determination to adopt the second method, directly converting the source data from the source data format to the destination data format, includes one of the following: If the overflow type of the source data is determined to be the overflow type, the third sub-method is adopted to directly convert the source data from the source data format to the destination data format, and the third sub-method is: the encoding method corresponding to the maximum value data that the destination data format can represent; If the overflow type of the source data is determined to be the underflow type, the fourth sub-method is adopted to directly convert the source data from the source data format to the destination data format, and the fourth sub-method is: the encoding method corresponding to 0 of the destination data format.

4. The data format conversion method according to claim 3, characterized in that, The step of determining to use the third sub-method to directly convert the source data from the source data format to the destination data format includes: If it is determined that the target data format supports encoding the non-numerical type or the infinite number type, the third sub-method is adopted to directly convert the source data from the source data format to the target data format. The third sub-method is the encoding method corresponding to the maximum value data that the target data format can represent. The encoding method is to encode the last exponent bit of the maximum value data as 0, encode the remaining exponent bits as 1, and encode the mantissa bits of the maximum value data as 1.

5. The data format conversion method according to claim 1, characterized in that, The step of determining the first tail value of the first destination data that conforms to the destination data format based on the source data, the source data format, and the destination data format includes: The first tail value of the first destination data is determined based on the tail value of the source data, the tail width of the source data format, the exponent value of the source data, the offset corresponding to the source data format, the offset corresponding to the destination data format, and the tail width of the destination data format.

6. The data format conversion method according to claim 5, characterized in that, Determining the first tail value of the first destination data based on the tail value of the source data, the tail width of the source data format, the exponent value of the source data, the offset corresponding to the source data format, the offset corresponding to the destination data format, and the tail width of the destination data format includes: After shifting the value 1 to the left by the width of the mantissa of the source data format, perform a bitwise OR operation with the mantissa of the source data to obtain the first value. The second value is obtained by subtracting the offset corresponding to the source data format and the exponent value of the source data from the value 1 and summing the result with the offset corresponding to the destination data format; and the third value is obtained by subtracting the mantissa width of the destination data format from the mantissa width of the source data format. Shift the first value to the right by the second value position, and then shift it to the right by the third value position to obtain the fourth value. Based on the fourth value, the first tail value of the first target data is determined.

7. The data format conversion method according to claim 6, characterized in that, Determining the first tail value of the first target data based on the fourth value includes: Based on the fourth value and the preset rounding mode, the first tail value of the first target data is determined.

8. The data format conversion method according to claim 1, characterized in that, The step of determining the second tail value and the second exponent value of the second destination data that conforms to the destination data format based on the source data, the source data format, and the destination data format includes: Based on the index value of the source data, the offset corresponding to the source data format, and the offset corresponding to the destination data format, the second index value of the second destination data is determined; The second tail value of the second destination data is determined based on the tail value of the source data, the tail width of the source data format, and the tail width of the destination data format.

9. The data format conversion method according to claim 8, characterized in that, Determining the second index value of the second destination data based on the index value of the source data, the offset corresponding to the source data format, and the offset corresponding to the destination data format includes: The second exponent value of the second destination data is obtained by subtracting the offset corresponding to the source data format from the exponent value of the source data and summing it with the offset corresponding to the destination data format.

10. The data format conversion method according to claim 8, characterized in that, Determining the second tail value of the second destination data based on the tail value of the source data, the tail width of the source data format, and the tail width of the destination data format includes: The last digit of the source data is shifted to the right by five digits to obtain the sixth digit; the fifth digit is obtained by subtracting the last digit width of the destination data format from the last digit width of the source data format. Based on the sixth value, the second tail value of the second target data is determined.

11. The data format conversion method according to claim 10, characterized in that, The step of determining the second tail value of the second target data based on the sixth value includes: Based on the sixth value and the preset mantissa rounding mode, the second mantissa value of the second target data is determined.

12. A data format conversion device, characterized in that, The device includes: The acquisition module is used to acquire the source data to be converted to a floating-point data format, the source data format corresponding to the source data, and the destination data format; A conversion module is used to determine, based on the source data, the source data format, and the destination data format, to use a target conversion method to directly convert the source data from the source data format to the destination data format. The target conversion method includes a first conversion method and a second conversion method; wherein, the first conversion method is determined based on the data type of the source data; and the second conversion method is determined based on the data range in which the source data is located. The conversion module includes: The first conversion unit is configured to determine, when it is determined that the source data is not within the range of target data that can be represented by the target data format, to adopt a first conversion method to directly convert the source data from the source data format to the target data format. The second conversion unit is configured to determine, when it is determined that the source data is within the target data range that the target data format can represent, to adopt the second conversion method to directly convert the source data from the source data format to the target data format. The first conversion method includes a first method and a second method; The first conversion unit is further configured to: if the data type of the source data is determined to be a non-numeric type or an infinite number type, determine to adopt a first method to directly convert the source data from the source data format to the destination data format; if the data type of the source data is determined to be an overflow data type, determine to adopt a second method to directly convert the source data from the source data format to the destination data format; wherein, the first method is determined based on whether the destination data format supports encoding the non-numeric type or the infinite number type data; the second method is determined based on the overflow type of the source data, the overflow type of the source data including overflow type and underflow type; The second conversion method includes the third and fourth methods; The second conversion unit is further configured to: when it is determined that the data range of the source data is a non-normalized data range, determine to adopt the third method to directly convert the source data from the source data format to the destination data format, wherein the third method is: determining the first exponent value of the first destination data conforming to the destination data format as 0; and determining the first tail value of the first destination data conforming to the destination data format based on the source data, the source data format, and the destination data format; and determining the converted first destination data based on the first exponent value and the first tail value; when it is determined that the data range of the source data is a normalized data range, determine to adopt the fourth method to directly convert the source data from the source data format to the destination data format, wherein the fourth method is: determining the second tail value and the second exponent value of the second destination data conforming to the destination data format based on the source data, the source data format, and the destination data format; and determining the converted second destination data based on the second exponent value and the second tail value.

13. An electronic device, characterized in that, include: Memory, used to store executable data instructions; A processor, when executing executable data instructions stored in the memory, implements the steps of the data format conversion method according to any one of claims 1 to 11.

14. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the data format conversion method according to any one of claims 1 to 11.

15. A computer program product, comprising a computer program or instructions, characterized in that, When the computer program or instructions are executed by the processor, they implement the steps in the data format conversion method according to any one of claims 1 to 11.

Citation Information

Patent Citations

  • Conversion processing method and device of floating-point number, electronic equipment and storage medium

    CN117155396A