A method, device, equipment and medium for converting an MLIR file into a PyTorch file

By using topological sorting and data type conversion, the technical problem of unequal conversion of operational relations in the conversion of MLIR files to PyTorch model files was solved, and the equivalent conversion of MLIR files was achieved.

CN117708075BActive Publication Date: 2026-06-26HEFEI UNIV OF TECH

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
HEFEI UNIV OF TECH
Filing Date
2024-01-08
Publication Date
2026-06-26

AI Technical Summary

Technical Problem

Existing technologies cannot achieve equivalent conversion between MLIR files and PyTorch model files, especially when the operation relations are not executed in a linear order, they cannot be converted effectively.

Method used

A stack of operation nodes is created using a topological sorting method to determine the sequential relationship between operations. By converting data types and operation types, the dependency relationship of PyTorch files is constructed, achieving an equivalent conversion from MLIR files to PyTorch files.

Benefits of technology

This paper implements an equivalent conversion from MLIR files to PyTorch model files, ensuring that the converted neural network model is functionally consistent with the original MLIR model, thus solving the problem of inequivalent conversion in existing technologies.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117708075B_ABST
    Figure CN117708075B_ABST
Patent Text Reader

Abstract

The application relates to the technical field of deep learning compilation, and provides a method, device, equipment and medium for converting an MLIR file into a PyTorch file. The method comprises the following steps: obtaining an MLIR file, extracting data information and operation information in the MLIR file; retaining original identifiers of the data information and converting data types of the data information into Tensor data types in a PyTorch file; constructing a dependency relationship between operations according to a front-back order relationship between the operations; converting operation types in the MLIR file into operation types in the PyTorch file according to a conversion relationship between the PyTorch file and the MLIR file; and forming the PyTorch file according to the converted data types, the dependency relationship between the operations and the converted operation types. The method can equivalently convert the MLIR file into the corresponding PyTorch file.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of deep learning compilation technology, specifically involving a method for converting intermediate representations from MLIR compilation facilities to PyTorch models. Background Technology

[0002] The MLIR project is a reusable and scalable compiler infrastructure designed to address software fragmentation, reduce compilation complexity on heterogeneous hardware, and lower the cost of building domain-specific compilers. MLIR compilers help improve the design and implementation of code generators, translators, and optimizers across different levels of abstraction, application domains, hardware targets, and execution environments.

[0003] The Torch-MLIR project is a compilation facility that supports the conversion of PyTorch ecosystem computing devices to MLIR compilation ecosystem computing devices, that is, converting PyTorch programs into the representation of the MLIR compiler.

[0004] Currently, the Torch-MLIR project cannot achieve equivalent conversion between MLIR files and PyTorch model files. When converting MLIR files to PyTorch model files, the order of operations is not always a linear sequence; some operations can only be executed after two or more other operations. Therefore, if the dependencies of operations are chained together one by one using existing methods, equivalent conversion of operation relationships cannot be achieved.

[0005] In summary, existing technologies have the problem that they cannot achieve equivalent transformation of operational relations when converting MLIR files to PyTorch model files. Summary of the Invention

[0006] To overcome the shortcomings of existing technologies, this invention provides a method, apparatus, device, and medium for converting MLIR files into PyTorch files.

[0007] To achieve the above objectives, the present invention provides the following technical solution:

[0008] A method for converting MLIR files to PyTorch files, characterized by comprising:

[0009] Obtain MLIR files and extract data and operation information from them.

[0010] Preserve the original identifiers of the data information and convert its data type to the Tensor data type in the PyTorch file;

[0011] Create an empty stack;

[0012] Traverse all operation nodes in the operation information, push operation nodes with an in-degree of 0 onto an empty stack, until there are no operation nodes with an in-degree of 0 in the operation information, and obtain a stack that stores the sequential relationship between the various operations.

[0013] Output the node sequence in the stack to obtain the sequential relationship between each operation, and construct the dependency relationship between operations based on the sequential relationship between each operation;

[0014] Based on the conversion relationship between PyTorch files and MLIR files, convert the operation types in the MLIR file to the operation types in the PyTorch file;

[0015] Based on the converted data type, the dependencies between operations, and the converted operation type, a PyTorch file is formed.

[0016] Furthermore, the extraction of data information from the MLIR file includes:

[0017] Extract list operands, tensors, and constants from an MLIR file.

[0018] Furthermore, the extraction of operation information from the MLIR file includes:

[0019] Extract operation type information and operand information from the MLIR file.

[0020] Further, after extracting the data and operation information from the MLIR file, the following steps are performed:

[0021] Store the identifiers in the data information and their corresponding data content;

[0022] The operation information is saved in the form of [result identifier, operator, operand identifier 1, ...].

[0023] Furthermore, the conversion of operation types in MLIR to operation types in PyTorch includes:

[0024] For each operation message, map each operation to a PyTorch operation type based on the operator; and convert the data type of the operation's operands to a PyTorch data type.

[0025] Furthermore, after converting the operation type in MLIR to the operation type in PyTorch, the following steps are performed:

[0026] Based on dependencies, assign appropriate inputs to each operation in PyTorch and save intermediate results.

[0027] Furthermore, the operation node with an in-degree of 0 is an operation node without a predecessor node.

[0028] An apparatus for converting MLIR files to PyTorch files, characterized in that it comprises:

[0029] The information extraction module is used to acquire MLIR files and extract data and operation information from them.

[0030] The data information conversion module is used to retain the original identifier of the data information and convert its data type to the Tensor data type in PyTorch.

[0031] The dependency building module is used to create an empty stack; it iterates through all operation nodes in the operation information, pushes operation nodes with an in-degree of 0 onto the empty stack, until there are no operation nodes with an in-degree of 0 in the operation information, and obtains a stack that stores the sequential relationship between each operation; it outputs the node sequence in the stack to obtain the sequential relationship between each operation, and builds the dependency relationship between operations based on the sequential relationship between each operation.

[0032] The operation type conversion module is used to convert the operation types in the MLIR file to the operation types in the PyTorch file based on the conversion relationship between the PyTorch file and the MLIR file.

[0033] The PyTorch file generation module is used to generate PyTorch files based on the converted data types, the dependencies between operations, and the converted operation types.

[0034] A computer device for converting MLIR files to PyTorch files, characterized in that it comprises:

[0035] At least one processor; and a memory communicatively connected to said at least one processor; wherein,

[0036] The memory stores instructions that can be executed by the at least one processor to enable the at least one processor to perform the method of converting an MLIR file to a PyTorch file as described in any one of claims 1 to 7.

[0037] A non-transitory computer-readable storage medium storing computer instructions, characterized in that the computer instructions are used to cause the computer to perform the method of converting an MLIR file into a PyTorch file as described in any one of claims 1 to 7.

[0038] The method, apparatus, device, and medium for converting MLIR files to PyTorch files provided by this invention have the following beneficial effects:

[0039] This invention divides MLIR files into data information and operation information; first, the data information data type is converted to the Tensor data type in PyTorch; then, the operation information is converted, thereby realizing the conversion between MLIR files and PyTorch model files.

[0040] When converting operation information, an empty stack is first created. Then, operation nodes with an in-degree of 0 (i.e., those without predecessors) are continuously pushed onto the stack, thus ensuring that operation relationships are pushed onto the stack from front to back. Because operation nodes with an in-degree of 0 are pushed one by one, for any operation node in the stack, the nodes before it are always pushed onto the stack before it, ensuring that the order of elements in the stack is not disordered. This method completely preserves the operation relationships in the MLIR file, prevents errors during conversion, and achieves equivalent conversion. The converted neural network model is functionally equivalent to the neural network model represented in the MLIR file. This solves the problem in existing technologies where the Torch-MLIR project cannot achieve equivalent conversion between MLIR files and PyTorch model files. Attached Figure Description

[0041] To more clearly illustrate the embodiments and design schemes of the present invention, the accompanying drawings required for this embodiment will be briefly described below. The drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0042] Figure 1 This is a schematic diagram of a method, apparatus, device, and medium for converting MLIR files to PyTorch files according to the present invention.

[0043] Figure 2 This is a diagram illustrating data dependencies. Detailed Implementation

[0044] To enable those skilled in the art to better understand and implement the technical solutions of the present invention, the present invention will be described in detail below with reference to the accompanying drawings and specific embodiments. The following embodiments are only used to more clearly illustrate the technical solutions of the present invention and should not be construed as limiting the scope of protection of the present invention.

[0045] Example:

[0046] For the emerging Torch-MLIR compiler architecture, there is currently no equivalent conversion between MLIR intermediate representation and PyTorch neural network model. This invention fills this gap.

[0047] Achieving an equivalent conversion from MLIR intermediate representation to PyTorch is a complex task. This process requires handling various information within the MLIR dialect, including data and operational information. The conversion process must consider not only the matching of corresponding data types and operation types, but also the sequentiality of related operation flows, i.e., control dependencies. A description of control dependencies is as follows... Figure 2 As shown, operation op4 can only be executed after operations op2 and op3 have been completed. In this invention, a topological sorting method is used to determine the sequential relationship between the operations. Finally, this transformation method outputs an equivalent neural network model represented in PyTorch.

[0048] The following is the concept of this invention:

[0049] Step 1: Obtain the MLIR file and extract the data and operation information from it; extract the list operands, tensors, constants, operation type information, and operand information from the MLIR file.

[0050] Step 2: Save the identifiers in the data information and their corresponding data content;

[0051] The operation information is saved in the form of "[result identifier, operator, operand identifier 1, ...]".

[0052] Step 3: Preserve the original identifier of the data information and convert its data type to the Tensor data type in PyTorch.

[0053] Specifically, for each operation message, the PyTorch operation type is mapped to each operation based on the operator; and the relevant operands are converted to PyTorch data types.

[0054] Step 4: For operation information, use topological sorting to determine the sequential order of operations, and construct dependencies between operations based on this sequential order. According to the conversion relationship between PyTorch and Torch-MLIR, convert the operation types in MLIR to PyTorch operation types. Based on the dependencies, assign appropriate inputs to each operation in PyTorch and save intermediate results. This completes the conversion from MLIR files to PyTorch files.

[0055] The conversion from MLIR representation to a PyTorch model requires handling the neural network layers in the MLIR dialect, which are typically arranged linearly. If the input to the current layer is the output of the previous layer, no explicit dependencies need to be built, as they are naturally arranged in order in the code. However, if the input to the current layer is not the output of the previous layer, the relevant information needs to be extracted from the intermediate code, and dependencies between operations and data need to be built to ensure that the reconstructed PyTorch neural network is functionally consistent with the network in MLIR.

[0056] Therefore, this invention uses a topological sorting method to construct dependencies. The specific execution steps are as follows: create an empty stack; traverse all operation nodes in the operation information and push operation nodes with an in-degree of 0 onto the stack until there are no operation nodes with an in-degree of 0; output the node sequence in the stack to obtain the sequential relationship between each operation.

[0057] like Figure 1 As shown, the following are the specific implementation steps of the present invention:

[0058] 1. Save the MLIR file:

[0059] First, save the MLIR dialect to a temporary file for later processing and analysis.

[0060] 2. Analyze the Dialect:

[0061] The saved MLIR files are analyzed to identify and classify the information in the dialog.

[0062] 3. Analysis of operations and data information: Analyze and process the data classified in the previous step.

[0063] Data information includes corresponding data identifiers and their data attributes.

[0064] Operational information includes specific operational details and identifiers.

[0065] Furthermore, based on complete operational information, the dependencies between various operations are constructed.

[0066] 4. Constructing operations in PyTorch:

[0067] Based on the conversion relationship between PyTorch and MLIR, the operation information in MLIR is restored to the operation in PyTorch, and the relevant operation data is adapted.

[0068] 5. Construct the computation process:

[0069] Based on the established dependencies, the processing order of each PyTorch operation is determined to realize the complete neural network model computation process.

[0070] The advantage of the method of this invention is that it can restore the MLIR intermediate representation to a PyTorch neural network model, realizing the migration of the neural network model between different ecological devices.

[0071] The above-described embodiments are merely preferred embodiments of the present invention, and the scope of protection of the present invention is not limited thereto. Any simple changes or equivalent substitutions of the technical solutions that can be obviously obtained by those skilled in the art within the scope of the technology disclosed in the present invention shall fall within the scope of protection of the present invention.

Claims

1. A method for converting MLIR files to PyTorch files, characterized in that, include: Obtain MLIR files and extract data and operation information from them. Preserve the original identifiers of the data information and convert its data type to the Tensor data type in the PyTorch file; Create an empty stack; Traverse all operation nodes in the operation information, push operation nodes with an in-degree of 0 onto an empty stack, until there are no operation nodes with an in-degree of 0 in the operation information, and obtain a stack that stores the sequential relationship between the various operations. Output the node sequence in the stack to obtain the sequential relationship between each operation, and construct the dependency relationship between operations based on the sequential relationship between each operation; Based on the conversion relationship between PyTorch files and MLIR files, convert the operation types in the MLIR file to the operation types in the PyTorch file; Based on the converted data type, the dependencies between operations, and the converted operation type, a PyTorch file is formed.

2. The method for converting MLIR files to PyTorch files according to claim 1, characterized in that, The extraction of data information from the MLIR file includes: Extract list operands, tensors, and constants from an MLIR file.

3. The method for converting MLIR files to PyTorch files according to claim 1, characterized in that, The operation information extracted from the MLIR file includes: Extract operation type information and operand information from the MLIR file.

4. The method for converting MLIR files to PyTorch files according to claim 1, characterized in that, After extracting the data and operation information from the MLIR file, the following steps are performed: Store the identifiers in the data information and their corresponding data content; The operation information is saved in the form of [result identifier, operator, operand identifier 1, ...].

5. The method for converting MLIR files to PyTorch files according to claim 1, characterized in that, The process of converting operation types in MLIR to operation types in PyTorch includes: For each operation message, map each operation to a PyTorch operation type based on the operator; and convert the data type of the operands of the operation to a PyTorch data type.

6. The method for converting MLIR files to PyTorch files according to claim 1, characterized in that, After converting the operation type in MLIR to the operation type in PyTorch, the following steps are performed: Based on dependencies, assign appropriate inputs to each operation in PyTorch and save intermediate results.

7. The method for converting MLIR files to PyTorch files according to claim 1, characterized in that, The operation node with an in-degree of 0 is an operation node without a predecessor node.

8. An apparatus for converting MLIR files to PyTorch files, characterized in that, include: The information extraction module is used to acquire MLIR files and extract data and operation information from them. The data information conversion module is used to retain the original identifier of the data information and convert its data type to the Tensor data type in PyTorch. The dependency building module is used to create an empty stack; it iterates through all operation nodes in the operation information, pushes operation nodes with an in-degree of 0 onto the empty stack, until there are no operation nodes with an in-degree of 0 in the operation information, and obtains a stack that stores the sequential relationship between each operation; it outputs the node sequence in the stack to obtain the sequential relationship between each operation, and builds the dependency relationship between operations based on the sequential relationship between each operation. The operation type conversion module is used to convert the operation types in the MLIR file to the operation types in the PyTorch file based on the conversion relationship between the PyTorch file and the MLIR file. The PyTorch file generation module is used to generate PyTorch files based on the converted data types, the dependencies between operations, and the converted operation types.

9. A computer device for converting MLIR files to PyTorch files, characterized in that, include: At least one processor; and a memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, which, when executed by the at least one processor, enables the at least one processor to perform the method of converting an MLIR file to a PyTorch file as described in any one of claims 1 to 7.

10. A non-transitory computer-readable storage medium storing computer instructions, characterized in that, The computer instructions are used to cause the computer to perform the method of converting an MLIR file to a PyTorch file as described in any one of claims 1 to 7.