Compiling method and device and computer readable storage medium

By extracting and optimizing the linear algebra calculation process in domain-specific language DSL, efficient machine code is generated, which solves the problems of insufficient linear algebra calculation efficiency and resource utilization in the existing technology, and achieves higher processing efficiency and lower cost. Energy consumption improves the application potential of high-performance computing.

CN118796196APending Publication Date: 2024-10-18CHINA MOBILE COMM LTD RES INST +2
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410034003.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-01-09
Publication Date
2024-10-18

AI Technical Summary

Technical Problem

Existing technologies have insufficient processing efficiency and resource utilization in the linear algebra calculation part of scientific computing, resulting in prolonged computing task running time and increased energy consumption, which limits the application potential of high-performance computing.

Method used

By extracting the linear algebra calculation process in the domain-specific language DSL, convert it into an intermediate representation, and perform optimization operations to generate an equivalent intermediate representation with a smaller computational load, and finally convert it into efficient machine code, combining front-end and back-end modules work together to give full play to the computing capabilities of the hardware.

Benefits of technology

It improves the processing efficiency of linear algebra calculations, shortens the running time of computing tasks, expands the amount of processing tasks, reduces energy consumption, and enhances the potential of applications in high-performance computing scenarios.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118796196A_ABST
    Figure CN118796196A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a compiling method, a compiling device, processing equipment, a chip and a computer readable storage medium. The method comprises the steps of obtaining a first source code; the first source code is a source code programmed through a first programming language; the first programming language is a programming language with a domain specific language DSL; extracting a DSL (Digital Subscriber Line) in the first source code; the DSL represents a linear algebraic calculation process in the first source code; converting the DSL into a first intermediate representation; performing optimization operation on the first intermediate representation to generate a second intermediate representation; the second intermediate representation is an equivalent intermediate representation of the first intermediate representation, and the calculated amount of the second intermediate representation is smaller than the calculated amount of the first intermediate representation; converting the second intermediate representation into a first code; the coding language of the first code is the first coding language; and combining the first code with the first source code to generate a second source code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of cloud computing technology, and specifically to a compilation method, a compilation device, a processing device, a chip, and a computer-readable storage medium. Background Art

[0002] Scientific computing is one of the core workloads of modern high-performance computing, and linear algebra computing is an important computing paradigm for scientific computing. As the scale of scientific computing tasks continues to increase, the requirements for computing performance are also getting higher and higher. Therefore, it is particularly important to perform linear algebra calculations efficiently. It is necessary to optimize the linear algebra calculation part that accounts for a large proportion of scientific computing time. The processing devices in related technologies often focus on how to maximize the use of hardware resources, and it is necessary to further improve the processing efficiency of the linear algebra calculation part. Summary of the invention

[0003] Embodiments of the present application provide a compilation method, a compilation device, a processing device, a chip, and a computer-readable storage medium.

[0004] The compilation method provided in the embodiment of the present application includes:

[0005] Obtain a first source code; the first source code is a source code programmed in a first programming language; the first programming language is a programming language with a domain specific language DSL;

[0006] Extracting a DSL in the first source code; wherein the DSL represents a linear algebra calculation process in the first source code;

[0007] Converting the DSL to a first intermediate representation;

[0008] performing an optimization operation on the first intermediate representation to generate a second intermediate representation; the second intermediate representation is an equivalent intermediate representation of the first intermediate representation, and the computation amount of the second intermediate representation is less than the computation amount of the first intermediate representation;

[0009] converting the second intermediate representation into a first code; the coding language of the first code is the first coding language;

[0010] The first code is combined with the first source code to generate a second source code.

[0011] The compiling device provided in the embodiment of the present application includes:

[0012] Front-end module: used to obtain a first source code; the first source code is a source code programmed in a first programming language; the first programming language is a programming language with a domain-specific language DSL; extract the DSL in the first source code; the DSL represents the linear algebra calculation process in the first source code; convert the DSL into a first intermediate representation;

[0013] An optimization module: performing an optimization operation on the first intermediate representation to generate a second intermediate representation; the second intermediate representation is an equivalent intermediate representation of the first intermediate representation, and the computation amount of the second intermediate representation is less than the computation amount of the first intermediate representation;

[0014] Backend module: convert the second intermediate representation into a first code; the coding language of the first code is the first coding language; combine the first code with the first source code to generate a second source code.

[0015] The processing device provided in the embodiment of the present application includes: a processor and a memory, the memory is used to store a computer program, and the processor is used to call and run the computer program stored in the memory to execute any compilation method provided in the embodiment of the present application.

[0016] The chip provided in the embodiment of the present application includes: a processor, which is used to call and run a computer program from a memory, so that a device equipped with the chip executes any compilation method provided in the embodiment of the present application.

[0017] The computer-readable storage medium provided in the embodiments of the present application is characterized in that it is used to store a computer program, and the computer program enables a computer to execute any compilation method provided in the embodiments of the present application.

[0018] Through the compilation method provided in the embodiment of the present application, the amount of linear algebra calculation is optimized, and linear algebra calculations are generated into calculations with smaller calculation amounts, which shortens the running time of the calculation tasks, improves the processing efficiency of the algorithm, and expands the amount of processing tasks under the same hardware resources, thereby greatly enhancing the application potential. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0020] Figure 1 This is a schematic diagram of the implementation process of the compilation method provided in the embodiment of the present application. Figure 1 ;

[0021] Figure 2 A schematic diagram of an optimization process provided for an embodiment of the present application;

[0022] Figure 3 Schematic diagram of the implementation process of the compilation method provided in the embodiment of the present application Figure 2 ;

[0023] Figure 4 A schematic diagram of the structure of the compiling device 400 provided in an embodiment of the present application;

[0024] Figure 5 A schematic structural diagram of a processing device provided in an embodiment of the present application;

[0025] Figure 6 A schematic structural diagram of a chip provided in an embodiment of the present application. DETAILED DESCRIPTION

[0026] The following will describe the technical solutions in the embodiments of the present application in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0027] It should be noted that in the embodiments of the present application, the term "and / or" is only a description of the association relationship of the associated objects, indicating that three relationships may exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, in the embodiments of the present application, the character " / " generally indicates that the associated objects before and after are in an "or" relationship.

[0028] In the description of the embodiments of the present application, the term "corresponding" may indicate a direct or indirect correspondence between two items, or an association relationship between the two items, or a relationship between indication and being indicated, configuration and being configured, and the like.

[0029] To facilitate understanding of the technical solutions of the embodiments of the present application, the relevant technologies of the embodiments of the present application are described below. The following related technologies can be arbitrarily combined with the technical solutions of the embodiments of the present application as optional solutions, and they all belong to the protection scope of the embodiments of the present application.

[0030] In the related technology, the optimization method aims to improve the resource utilization of the platform hardware. These optimization methods include: computational graph optimization, which analyzes the structure of the computational graph, such as eliminating redundant calculations and merging operations that can be executed together; operator fusion, which combines multiple operations into one operation, thereby reducing the number of times data is moved in memory and improving computing efficiency; loop optimization, which transforms and adjusts the loop structure, such as loop unrolling and loop swapping, to adapt to specific hardware characteristics; cache optimization, which optimizes the storage and access methods of data to make better use of the hardware's storage hierarchy; vectorization, which uses the hardware's Single Instruction Multiple Data (SIMD) and other features to process multiple data in parallel. In addition, computing tasks can be generated as a parallel programming language of the Compute Unified Device Architecture (CUDA) (such as TVM), and then a third-party compiler can be used to generate machine code, or computing tasks can be generated as calling libraries such as the Basic Linear Algebra Subprograms (BLAS) on the platform to directly use existing efficient machine code, or the calculation can be generated as the Low Level Virtual Machine Intermediate Representation (LLVM IR), and then machine code can be generated for multiple platforms.

[0031] In the related technology, the goal is to improve the efficiency of linear algebra calculations using platform hardware. Through the collaborative work of the front-end, optimization, and back-end, the device can convert high-level algorithm or computing task descriptions into efficient machine code, thereby giving full play to the computing power of the hardware.

[0032] The devices in the related art focus on how to maximize the use of hardware resources, but these devices often ignore the possibility of algebraic optimization of the algorithm itself. The linear algebra algorithms and computing task descriptions at the core of scientific computing may have redundant calculations, or there may be equivalent implementations with less computational complexity. These missed optimization opportunities could have greatly reduced the amount of computation and fundamentally reduced the computational complexity. The lack of optimization of computational workload may lead to unnecessary extensions of the running time of computing tasks, which means that for large-scale computing tasks, it may take more time to complete. Secondly, the lack of algebraic optimization of computational workload may lead to higher energy consumption and waste of resources, especially in large data centers or supercomputers. This waste of energy will not only increase operating costs. In addition, this also means that under the same hardware conditions, the amount of tasks processed by the algorithm will be greatly reduced, limiting its application potential in high-performance computing scenarios.

[0033] Figure 1 Schematic diagram of the implementation process of the compilation method provided in the embodiment of the present application Figure 1 ,like Figure 1 As shown, the embodiment of the present application provides a compilation method, which includes the following steps:

[0034] Step 101: Obtain a first source code; the first source code is a source code programmed in a first programming language; the first programming language is a programming language with a domain specific language (DSL).

[0035] Users can write source code using other parallel programming languages ​​with DSL (such as SYCL, CUDA, OpenCL, etc.) based on the linear algebra computing needs of their scientific computing business domain.

[0036] Here, DSL can be specifically referred to as Tensor Contraction (TC) DSL. TC DSL is a user portal used to describe target basic linear algebra calculations. DSL is designed as an intuitive expression of mathematical formulas, so that users can easily write DSL from basic linear algebra calculation formulas.

[0037] Each TC DSL file representing linear algebra calculations describes a function that uses a set of tensor inputs to perform calculations and obtains a set of tensor outputs. Each line in the DSL is a statement, and statements are divided into two categories: statements that use keywords to define information and calculation statements that express formulas. Refer to Table 1, which is an example of using TC DSL to represent matrix multiplication.

[0038]

[0039] Table 1

[0040] There are four types of statements that use keywords to define information. The functions of the four keywords are as follows:

[0041] index: used to define constant indexes in formulas, such as row 1 in Table 1. An axis of a tensor uses the index defined by index, indicating that a slice of the tensor on the axis is used, such as A(i,a,k) in Table 1, which means slicing on the second dimension of A.

[0042] size: used to define the dimension size of a tensor, as shown in row 2 of Table 1. The dimension size identifier defined by the size keyword can be used by multiple tensors or by different dimensions of the same tensor. The dimension size identifier is followed by an equal sign. If it is followed by a positive integer, it indicates that the dimension size is a constant. If it is followed by two numbers in parentheses, it indicates that the dimension size is dynamic within a certain range, and this dimension size will become a parameter of the subsequent generation function.

[0043] Input: A tensor used to define the input, such as row 3 in Table 1. The tensor identifier is followed by a set of identifiers in parentheses, indicating the size of each dimension of the tensor. The dimension size of the tensor can be defined using the size keyword, or using the * sign to indicate that the dimension size can be inferred, such as the second dimension of the definition of A in Table 1.

[0044] keep: used to indicate which calculation statements are used as function outputs, such as row 5 in Table 1. There are multiple calculation statements in the DSL, and the results of these statements will not be automatically used as outputs, so keep statements are required to explicitly declare them.

[0045] TC DSL includes information statements and calculation statements.

[0046] There are four types of information statements, defined by the keywords index, size, input, and keep, which are used to indicate information about constant index, dimension size, input tensor, and result tensor. The text definition of an information statement is as follows:

[0047] As shown in Table 2.

[0048]

[0049]

[0050] Table 2

[0051] The core statement of TC DSL is the calculation statement that represents the formula. The simplified grammar definition of the calculation statement is as follows:

[0052] As shown in Table 3. <tensor>and <index>Both are identifiers.

[0053]

[0054] Table 3

[0055] Step 102: extracting a DSL in the first source code; the DSL represents a linear algebra calculation process in the first source code.

[0056] Here, after extracting the DSL from the source code, the tensor in the DSL can be bound to the memory handle of the function where the DSL is located in the source code, so as to restore the data reference relationship for the subsequent generation of the second source code; If the DSL is not in the function, a function encapsulation of the corresponding programming language will be generated for the DSL based on the input and output information of the DSL, so as to provide a function encapsulation for the subsequent generation of the second source code.

[0057] Step 103: Convert the DSL into a first intermediate representation.

[0058] In an optional implementation of the present application, the first intermediate representation may adopt TC Dialect.

[0059] Here, TC Dialect is an intermediate representation defined in a multi-level intermediate representation (MLIR) framework. Refer to Table 2, which is a definition of TC Dialect.

[0060]

[0061] Table 4

[0062] TC Dialect defines four attributes, namely AxesAttr, AxesMapsAttr, SignsAttr and SymmetryAttr. The AxesAttr attribute is an ordered set of int64_t type data, representing a set of summed indexes; the AxesMapsAttr attribute corresponds to an array of AxesMapTy type data, where AxesMapTy is a vector in implementation.<int64_t> Although AxesMapTy is an array, it actually corresponds to a map<int64_t,int64_t> The mapping is implemented as an array because the mapping key value ranges from 0 to map.size(). The key of this mapping is the index of the input tensor, and the value is the index of the output tensor; the SignsAttr attribute is an array of type bool, which is used in the addition operation so that the addition operation can include subtraction; the SymmetryAttr attribute is a pair<int64_t,int64_t> An array of type is used in the definition of a tensor type to indicate its symmetry.

[0063] All operations in TC Dialect use data of type tensor, which comes from MLIR's built-in dialect. Since the built-in data types of MLIR already meet the design requirements, TC Dialect does not define new data types, but TCDialect uses the custom attribute field Encoding of the tensor data type. TC Dialect defines two attributes in the Encoding field of the tensor type, which are used to indicate the dimension size and symmetry of the tensor.

[0064] TC Dialect defines a total of 8 operations, with the following specific functions: AddOp: represents an element-by-element tensor addition operation. The input of this operation is two or more tensor data, and the result is a tensor data. The operation contains two attributes, namely the axes_maps attribute of the AxesMapsAttr type and the signs attribute of the SignsAttr type. The number of input tensors of AddOp can be two or more. If the number of inputs of AddOp is more than two, AddOp does not specify the order of addition of these inputs. For example, for tc.add(%0,%1,%2), it can be regarded as %0+(%1+%2), or as %1+(%0+%2), etc. The ranks of multiple input tensors of the addition operation are the same, that is, the addition is element-by-element addition, and the input tensor does not need to expand the dimension; MulOp: represents an element-by-element multiplication operation after tensor expansion. The input of this operation is two or more tensor data, and the result is a tensor data. The operation contains an axes_maps attribute of the AxesMapsAttr type. MulOp is the same as AddOp. The number of input tensors can be two or more, and the multiplication order of these inputs is not specified. Unlike the addition operation AddOp, the ranks of the input tensors of the multiplication operation can be different, but they are all less than or equal to the rank of the result tensor. If the rank of the input tensor is smaller than the result tensor, the tensor with the missing dimensions expanded is used as the input of the element-by-element multiplication. ContractOp: represents the summation of tensors. The input of this operation is a tensor data, and the result is a tensor data. The operation contains an attribute axes of type AxesAttr. The attribute axes is used to specify the set of axes on which the input tensors are summed, but in practice it is an ordered vector<int64_t> Data of type; PermuteOp: indicates the rearrangement between tensor axes. The input of this operation is a tensor data and the result is a tensor data. The operation contains an attribute axes_map of type AxesMapsAttr. This operation only changes the data layout in the tensor. If the tensor is a matrix, this operation can represent matrix transposition. The attribute AxesMapsAttr is an array of type AxesMapTy. In the sorting operation, the array size is 1, and its first element is the axes_map of the sorting operation. The attribute axes_map has the same function as the axes_maps attribute in addition and multiplication operations; ConstantOp: represents a constant. This operation provides a 0-dimensional constant scalar and contains a FloatAttr attribute to store the value of the constant; SliceOp represents a slice of a tensor, NegOp represents a negative value of a tensor, and ReturnOp represents the tensor required for the calculation result.

[0065] Step 104: Optimize the first intermediate representation to generate a second intermediate representation; the second intermediate representation is an equivalent intermediate representation of the first intermediate representation, and the computational complexity of the second intermediate representation is less than that of the first intermediate representation.

[0066] In an optional implementation manner of the present application, the optimizing operation on the first intermediate representation to generate the second intermediate representation includes:

[0067] Converting the first intermediate representation into a directed acyclic graph (DAG); the DAG includes at least one layer of an addition operator, a summation operator, a multiplication operator, and an input;

[0068] Selecting a summation order for the summation operators having multiple summation dimensions in the DAG to obtain at least one third intermediate representation; the at least one third intermediate representation is one or more intermediate representations with the smallest computational complexity among the intermediate representations generated after the summation order selection;

[0069] Performing the first operation on each of the at least one third intermediate representation to obtain at least one fourth intermediate representation;

[0070] The computational complexity of each intermediate representation of the at least one fourth intermediate representation is evaluated, and the fourth intermediate representation with the smallest computational complexity is selected as the second intermediate representation.

[0071] Here, the first intermediate representation is first normalized. Exemplarily, if the first intermediate representation includes addition, summation, and multiplication operations, the first intermediate representation is converted into a directed acyclic graph (DAG) with 4 layers in which an addition operator, a summation operator, a multiplication operator, and an input are sequentially arranged.

[0072] In an optional implementation manner of the present application, the step of selecting a summation order for the summation operators having multiple summation dimensions in the DAG to obtain at least one third intermediate representation includes:

[0073] The summation operator of the plurality of summation dimensions is split, and at least one third intermediate representation is obtained through a pruning strategy.

[0074] Here, the summation order selection is performed on the summation operators with multiple summation dimensions in the first intermediate representation, these summation operations are split, and under the pruning strategy, a set of intermediate representations with the smallest potential computational complexity are generated.

[0075] In an optional implementation manner of the present application, the first operation includes at least one of the following operations:

[0076] Public child means elimination;

[0077] Extracting a first common factor; the first common factor is a common factor irrelevant to the summation in a summation operation in which the input is a result of a multiplication operation;

[0078] Exchange the order of addition and summation operations;

[0079] Symmetric pattern matching;

[0080] extracting a second common factor; the second common factor being the common factor of multiplication in the addition operation; and,

[0081] Splits multiplication operations that represent consecutive multiplications of multiple tensors.

[0082] Here, the first common factor extraction operation can reduce the amount of calculation by extracting common factors that are not related to the summation from the summation operation whose input is the result of the multiplication operation in the intermediate representation. Exemplarily, sum((i), A(i, j)) refers to the summation of the first dimension of A, and the common factor extraction in the summation is: A(i, j) = B(j) * C(i, j), D(j) = sum((i), A(i, j)), which is transformed into: D(j) = B(j) * sum((i), C(i, j)).

[0083] Swap the order of addition and summation operations Swap the order of addition and summation operations in the intermediate representation so that the result of the multiplication operation in the summation becomes the input of the addition operation, which can reduce the amount of calculation. For example, sum((i), A(i, j)) refers to the sum of the first dimension of A. The summation in the combined addition is: sum((i), A(i, j)) + sum((i), B(i, j)) is transformed into: sum((i), (A(i, j) + B(i, j))).

[0084] The symmetric pattern matching operation transforms the intermediate representation in a pattern matching manner, which can reduce the amount of calculation.

[0085] Exemplarily, as a symmetry pattern matching operation (which may be called the first symmetry pattern matching), given a linear algebra expression expr, it can be decomposed into two sub-expressions expr = expr1 op expr2, where the computational complexity of expr1 is much lower than that of expr, and the result tensor of expr2 is the transpose of the result tensor of expr1. In this way, we only need to calculate the result tensor of expr1, without calculating the result tensor of expr2, reducing the computational complexity of expr.

[0086] Exemplarily, as another symmetry pattern matching operation (which may be referred to as the second symmetry pattern matching), given a linear algebraic expression of the form expr = A*B*AT, it can be equivalently transformed into expr = A*C*AT, where the tensor C can be written in upper triangular or lower triangular form, and each element of C is a linear combination of the elements of B. In this way, we can use the tensor C to calculate expr, reducing the amount of calculation.

[0087] The extract second common factor operation extracts the common factor from the multiplication in the addition operation of the intermediate representation, which can reduce the amount of calculation.

[0088] The multiplication operation representing the continuous multiplication of multiple tensors is split. According to the commutative law and associative law of multiplication, the multiplication order of the intermediate representation is rearranged to reduce the amount of calculation.

[0089] In an optional implementation manner of the present application, the first operation includes multiple common sub-representation elimination operations; wherein,

[0090] When performing other operations except the common sub-representation elimination operation in the first operation, there is a common sub-representation elimination operation after each operation.

[0091] After each step except for the common sub-representation elimination operation, a common sub-representation elimination is performed to further reduce the amount of calculation.

[0092] Exemplary, reference Figure 2 , Figure 2 This is a schematic diagram of the optimization process provided by the embodiment of this application. Figure 2 As shown, the following steps are included:

[0093] Step 201: Normalization.

[0094] After obtaining the first intermediate representation, the first intermediate representation is first normalized and converted into a DAG of 4 layers in which an addition operator, a summation operator, a multiplication operator and an input are sequentially sorted to obtain a normalized first intermediate representation.

[0095] Step 202: Select the summing order.

[0096] The summation order of the summation operators with multiple summation dimensions in the normalized first intermediate representation is selected, the summation operations are split, and under the pruning strategy, a set of intermediate representations with the smallest potential computational complexity is generated, namely, the third intermediate representation.

[0097] Step 203: Extract the first common factor.

[0098] After the order of summation is determined, for each third intermediate representation, the first common factor is extracted from the third intermediate representation, and the first common factor is the common factor irrelevant to the summation extracted from the summation operation in which the input is the result of the multiplication operation in the third representation, so as to reduce the amount of calculation.

[0099] Step 204: Exchange the order of the addition operation and the summation operation.

[0100] Swap the order of the addition operation and the summation operation so that the result of the multiplication operation in the summation becomes the input of the addition operation.

[0101] Step 205: first symmetry pattern matching.

[0102] Perform symmetry analysis, try to match the first symmetry pattern, and perform transformations in a pattern matching manner to reduce the amount of calculation.

[0103] Step 206: Extract the second common factor.

[0104] After steps 204 and 205, the second common factor is extracted, and the common factor is extracted from the multiplication in the addition operation to reduce the amount of calculation. The transformation of step 206 may produce summation and addition operations that can be exchanged by step 204, so return to step 204. If there is no calculation that can be optimized by step 204 after step 206, that is, no transformation is generated, step 207 is executed.

[0105] Step 207: Split the multiplication operations representing the consecutive multiplication of multiple tensors

[0106] The multiplication operations representing the continuous multiplication of multiple tensors are split, and the multiplication order is rearranged according to the commutative and associative laws of multiplication to reduce the amount of calculation.

[0107] Step 208: Second symmetry pattern matching.

[0108] Symmetry analysis is performed again to try to match the second symmetry pattern, and transformation is performed in a pattern matching manner to reduce the amount of calculation. After step 208, at least one fourth intermediate representation is obtained.

[0109] Step 209: Cost assessment.

[0110] A set of fourth intermediate representations generated in the above steps is evaluated for computational complexity, and an intermediate representation with the smallest computational complexity is selected as the optimized intermediate representation, that is, the second intermediate representation.

[0111] Step 105: Convert the second intermediate representation into a first code; the coding language of the first code is the first coding language.

[0112] Here, the intermediate representation is converted into code in preparation for subsequent combination with the first source code.

[0113] Step 106: Combine the first code with the first source code to generate a second source code.

[0114] Here, the DSL in the first source code can use #pragma, so whether or not the original DSL is retained in the first source code does not affect the generation and function of the second source code.

[0115] In an optional implementation manner of the present application, the method further includes: replacing the first calculation in the second intermediate representation with a corresponding BLAS calculation in TBLASDialect; the first calculation is a calculation with a first conversion form in the second intermediate representation; the first conversion form is converted into a BLAS calculation; the TBLASDialect is an intermediate representation of the BLAS calculation;

[0116] The converting the second intermediate representation into the first code comprises:

[0117] The second intermediate representation replaced by the corresponding BLAS calculation in TBLASDialect is converted into the first code.

[0118] Exemplarily, after identifying the BLAS calculation in the second intermediate representation, TCDialect and TBLASDialect exist in the intermediate representation at the same time, where TC Dialect will be translated into the source code of the corresponding platform (SYCL, etc.), and TBLASDialect will be translated into a function call if the relevant library exists, otherwise platform code will also be generated.

[0119] In an optional implementation manner of the present application, after the second source code is generated, the second source code is generated into an executable program, so that the program can run on multiple platforms such as Nvidia GPU, DPU and Sunway.

[0120] refer to Figure 3 , Figure 3 Schematic diagram of the implementation process of the compilation method provided in the embodiment of the present application Figure 2 ,like Figure 3 As shown, the method comprises the following steps:

[0121] Step 301: Write scientific calculations.

[0122] Based on the linear algebra computing needs of their scientific computing business field, users develop and generate source code of a parallel programming language with TC DSL. TC DSL is the user's entry point, used to describe the target basic linear algebra computing. DSL is designed as an intuitive expression of mathematical formulas, so that users can easily write DSL from basic linear algebra computing formulas.

[0123] Step 2: DSL separation.

[0124] Extract the DSL from the source code and bind the tensors in the DSL to the memory handles of the function where the DSL is located in the source code. If the DSL is not in the function, a function wrapper of the corresponding programming language will be generated for the DSL based on the input and output information of the DSL.

[0125] Step 303: Parsing and generating.

[0126] The extracted DSL is parsed into an abstract syntax tree AST, and then the implicit input of the DSL and the dimensional relationship between tensors are analyzed on the abstract syntax tree. Finally, the AST is used to generate an intermediate representation TC Dialect (corresponding to the first intermediate representation).

[0127] Step 304: Optimization.

[0128] Normalize the intermediate representation, perform symmetry analysis, and optimize the computational effort. The computational effort optimization includes eliminating common subexpressions, optimizing the extraction of common factors in summation, optimizing the extraction of common factors in addition, optimizing the multiplication order selection, and optimizing symmetric tensors. Finally, the optimization module transforms the first intermediate representation into an equivalent intermediate representation with less computational effort. For specific steps, refer to Figure 2 The optimization steps shown in the optimization process will not be repeated here.

[0129] Step 305: BLAS calculation identification.

[0130] Identify the calculations that can be converted to BLAS operators from TC Dialect (corresponding to the second intermediate representation) and replace them with the corresponding BLAS calculations in TBLASDialect.

[0131] Step 306: Code generation.

[0132] After identifying the BLAS calculation of TC Dialect (corresponding to the second intermediate representation), the back-end module generates it as a specified platform code and combines it with the source code of the parallel programming language separated by the front-end module to generate the final source program.

[0133] Step 307: Native compilation.

[0134] After the final source program is generated, the calculation is generated into an executable program, so that the program can run on multiple platforms such as Nvidia GPU, DPU and Sunway.

[0135] For example, refer to Table 5, Table 6 and Table 7. Table 5 is the calculation code of SYCL implementation before optimization provided in the embodiment of the present application. Table 6 is the calculation code implemented using DSL provided in the embodiment of the present application. Table 7 is the optimized SYCL code provided in the embodiment of the present application.

[0136]

[0137] Table 5

[0138]

[0139] Table 6

[0140]

[0141]

[0142] Table 7

[0143] The code in Table 5 implements the linear algebra calculation of multiplying two matrices and adding the product of the two transposes. This part of the calculation is expressed as a function whose input contains the buffer with the same name as the three identifiers in the formula.

[0144] The code in Table 6 is calculated by pragma tc_dsl definition, and can be directly translated into SYCL calculation equivalent to the code in Table 5. After the compilation method provided by the embodiment of the present application, its calculation method first calculates the product of two matrices, and then adds the result to its own transposition. The optimized calculation generates the SYCL code shown in the code in Table 7. This process defines a new temporary buffer D in the function to store the intermediate calculation results. In addition, if the SYCL platform has relevant BLAS tools, the corresponding BLAS library can be directly called to speed up the calculation.

[0145] The compilation method provided in the embodiment of the present application, the intermediate representation has the characteristic of being able to flexibly perform algebraic transformations, and the intermediate representation implemented in the present application is designed for linear algebraic transformations. The basic operations in the intermediate representation of the technical design provide convenience for algebraic calculation transformations while describing the semantics of linear algebraic calculations. At the same time, the intermediate representation also introduces properties that describe the symmetry of tensors, providing more information for optimization and code generation.

[0146] The embodiments of the present application adopt an optimization strategy different from the traditional technology. Based on the equivalence of algebraic operations, the present application proposes a series of heuristic optimization transformation methods. Through algebraic operation rules, such as the distributive law, the associative law, and the commutative law, the present application achieves the minimization of the total amount of linear algebra calculation.

[0147] In the context of many existing technologies focusing on maximizing the use of hardware resources, the present application embodiment proposes a strategy for deep optimization of linear algebra algorithms. This method forms a powerful complement to traditional hardware resource strategies when looking for optimization opportunities in algorithm and computing task descriptions, thereby further improving the execution efficiency of the program.

[0148] The present application embodiment also provides a compiling device 400, referring to Figure 4 The compiling device 400 in this embodiment includes:

[0149] Front-end module 410: used to obtain a first source code; the first source code is a source code programmed in a first programming language; the first programming language is a programming language with a domain-specific language DSL; the DSL in the first source code is extracted by a DLS separation module; the DSL represents a linear algebra calculation process in the first source code; the DSL is converted into a first intermediate representation by a DSL parsing module and an intermediate representation generation module;

[0150] Optimization module 420: configured to perform an optimization operation on the first intermediate representation to generate a second intermediate representation; the second intermediate representation is an equivalent intermediate representation of the first intermediate representation, and the computation amount of the second intermediate representation is less than the computation amount of the first intermediate representation;

[0151] Backend module 430: converts the second intermediate representation into a first code; the coding language of the first code is the first coding language; and combines the first code with the first source code to generate a second source code.

[0152] In an embodiment of the present application, the optimization module 420 is used to convert the first intermediate representation into a directed acyclic graph DAG through a normalization module; the DAG includes at least one of the following layers: an addition operator, a summation operator, a multiplication operator and an input; the summation operator with multiple summation dimensions in the DAG is selected in a summation order through a computational optimization module and a symmetry analysis module to obtain at least one third intermediate representation; the at least one third intermediate representation is one or more intermediate representations with the smallest computational amount among the intermediate representations generated after the summation order selection; a first operation is performed on each third intermediate representation in the at least one third intermediate representation to obtain at least one fourth intermediate representation; the computational amount of each intermediate representation in the at least one fourth intermediate representation is evaluated, and the fourth intermediate representation with the smallest computational amount is selected as the second intermediate representation.

[0153] In an embodiment of the present application, the first operation includes at least one of the following operations: common sub-representation elimination; extracting a first common factor; the first common factor is a common factor irrelevant to the summation in a summation operation whose input is the result of a multiplication operation; exchanging the order of the addition operation and the summation operation; symmetry pattern matching; extracting a second common factor; the second common factor is a common factor of multiplication in the addition operation; and splitting the multiplication operation representing continuous multiplication of multiple tensors.

[0154] In an embodiment of the present application, the first operation includes multiple common sub-representation elimination operations; wherein, when performing other operations except the common sub-representation elimination operation in the first operation, there is a common sub-representation elimination operation after each operation.

[0155] In the embodiment of the present application, the optimization module 420 is used to split the summation operators of the multiple summation dimensions through a computational optimization module, and obtain at least one third intermediate representation through a pruning strategy.

[0156] In an embodiment of the present application, the back-end module 430 is used to replace the first calculation in the second intermediate representation with the corresponding BLAS calculation in TBLASDialect through the BLAS calculation identification module; the first calculation is a calculation with a first conversion form in the second intermediate representation; the first conversion form is converted into a BLAS calculation; the TBLASDialect is an intermediate representation of the BLAS calculation; the back-end module 430 is used to convert the second intermediate representation replaced with the corresponding BLAS calculation in TBLASDialect into a first code through the code generation module.

[0157] In the embodiment of the present application, the backend module 430 is used to generate the second source code into an executable program through a native compilation module.

[0158] Those skilled in the art should understand that Figure 4 The implementation functions of each unit in the compilation device 400 shown can be understood by referring to the relevant description of the above method. Figure 4 The functions of the various units in the compilation device 400 shown may be implemented by a program running on a processor, or may be implemented by a specific logic circuit.

[0159] Figure 5 It is a schematic structural diagram of a processing device 500 provided in an embodiment of the present application. Figure 5 The processing device 500 shown includes a processor 510, which can call and run a computer program from a memory to implement the method in the embodiment of the present application.

[0160] Alternatively, if Figure 5 As shown, the processing device 500 may further include a memory 520. The processor 510 may call and run a computer program from the memory 520 to implement the method in the embodiment of the present application.

[0161] The memory 520 may be a separate device independent of the processor 510 , or may be integrated into the processor 510 .

[0162] Alternatively, if Figure 5 As shown, the processing device 500 may further include a transceiver 530, and the processor 510 may control the transceiver 530 to communicate with other devices, specifically, may send information or data to other devices, or receive information or data sent by other devices.

[0163] The transceiver 530 may include a transmitter and a receiver. The transceiver 530 may further include an antenna, and the number of the antennas may be one or more.

[0164] The processing device 500 may specifically be the compiling device 400 of the embodiment of the present application, and the processing device 500 may implement the corresponding processes implemented by the compiling device 400 in each method of the embodiment of the present application, which will not be described in detail here for the sake of brevity.

[0165] Figure 6 It is a schematic structural diagram of the chip of an embodiment of the present application. Figure 6 The chip 600 shown includes a processor 610, which can call and run a computer program from a memory to implement the method in the embodiment of the present application.

[0166] Alternatively, if Figure 6 As shown, the chip 600 may further include a memory 620. The processor 610 may call and run a computer program from the memory 620 to implement the method in the embodiment of the present application.

[0167] The memory 620 may be a separate device independent of the processor 610 , or may be integrated into the processor 610 .

[0168] Optionally, the chip 600 may further include an input interface 630. The processor 610 may control the input interface 630 to communicate with other devices or chips, and specifically, may obtain information or data sent by other devices or chips.

[0169] Optionally, the chip 600 may further include an output interface 640. The processor 610 may control the output interface 640 to communicate with other devices or chips, and specifically, may output information or data to other devices or chips.

[0170] The chip can be applied to the compiling device 400 in the embodiment of the present application, and the chip can implement the corresponding processes implemented by the compiling device 400 in each method of the embodiment of the present application, which will not be described in detail here for the sake of brevity.

[0171] It should be understood that the chip mentioned in the embodiments of the present application can also be called a system-level chip, a system chip, a chip system or a system-on-chip chip, etc.

[0172] It should be understood that the processor of the embodiment of the present application may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method embodiment can be completed by the hardware integrated logic circuit or software instructions in the processor. The above processor can be a general processor, a digital signal processor (Digital Signal Processor, DSP), an application-specific integrated circuit (Application Specific Integrated Circuit, ASIC), a field programmable gate array (Field Programmable Gate Array, FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present application can be implemented or executed. The general processor can be a microprocessor or the processor can also be any conventional processor. The steps of the method disclosed in the embodiment of the present application can be directly embodied as a hardware decoding processor to perform, or the hardware and software modules in the decoding processor are combined and performed. The software module can be located in a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, and other mature storage media in the art. The storage medium is located in the memory, and the processor reads the information in the memory and completes the steps of the above method in combination with its hardware.

[0173] It can be understood that the memory in the embodiments of the present application can be a volatile memory or a non-volatile memory, or can include both volatile and non-volatile memories. Among them, the non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory can be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM), and direct RAM bus random access memory (DR RAM). It should be noted that the memory of the systems and methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.

[0174] It should be understood that the above-mentioned memory is exemplary but not restrictive. For example, the memory in the embodiments of the present application may also be static random access memory (static RAM, SRAM), dynamic random access memory (dynamic RAM, DRAM), synchronous dynamic random access memory (synchronous DRAM, SDRAM), double data rate synchronous dynamic random access memory (double data rate SDRAM, DDR SDRAM), enhanced synchronous dynamic random access memory (enhanced SDRAM, ESDRAM), synchronous link dynamic random access memory (synch link DRAM, SLDRAM) and direct memory bus random access memory (Direct Rambus RAM, DR RAM), etc. That is to say, the memory in the embodiments of the present application is intended to include but not limited to these and any other suitable types of memory.

[0175] The embodiment of the present application further provides a computer-readable storage medium for storing a computer program. The computer-readable storage medium can be applied to the compiling device 400 in the embodiment of the present application, and the computer program enables the computer to execute the corresponding processes implemented by the compiling device 400 in each method of the embodiment of the present application, which will not be described in detail for the sake of brevity.

[0176] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0177] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

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

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

[0180] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0181] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a compiling device 400, etc.) to perform all or part of the steps of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0182] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.< / index> < / tensor>

Claims

1. A compiling method, characterized in that: include: Get the first source code; The first source code is a source code programmed in a first programming language; The first programming language is a programming language with a domain specific language DSL; Extracting a DSL in the first source code; wherein the DSL represents a linear algebra calculation process in the first source code; Converting the DSL to a first intermediate representation; performing an optimization operation on the first intermediate representation to generate a second intermediate representation; the second intermediate representation is an equivalent intermediate representation of the first intermediate representation, and the computation amount of the second intermediate representation is less than the computation amount of the first intermediate representation; converting the second intermediate representation into a first code; the coding language of the first code is the first coding language; The first code is combined with the first source code to generate a second source code.

2. The method according to claim 1, characterized in that The step of optimizing the first intermediate representation to generate a second intermediate representation includes: Converting the first intermediate representation into a directed acyclic graph DAG; the DAG includes at least one layer of: an addition operator, a summation operator, a multiplication operator and an input; Selecting a summation order for the summation operators having multiple summation dimensions in the DAG to obtain at least one third intermediate representation; the at least one third intermediate representation is one or more intermediate representations with the smallest computational complexity among the intermediate representations generated after the summation order selection; Performing the first operation on each of the at least one third intermediate representation to obtain at least one fourth intermediate representation; The computational complexity of each intermediate representation of the at least one fourth intermediate representation is evaluated, and the fourth intermediate representation with the smallest computational complexity is selected as the second intermediate representation.

3. The method according to claim 2, characterized in that The first operation includes at least one of the following operations: Public child means elimination; Extracting a first common factor; the first common factor is a common factor irrelevant to the summation in a summation operation in which the input is a result of a multiplication operation; Exchange the order of addition and summation operations; Symmetry pattern matching; extracting a second common factor; the second common factor being the common factor of multiplication in the addition operation; and, Splits multiplication operations that represent consecutive multiplications of multiple tensors.

4. The method according to claim 3, characterized in that The first operation includes multiple common sub-representation elimination operations; wherein, When performing other operations except the common sub-representation elimination operation in the first operation, there is a common sub-representation elimination operation after each operation.

5. The method according to claim 2, characterized in that: The step of selecting a summation order for the summation operators having multiple summation dimensions in the DAG to obtain at least one third intermediate representation includes: The summation operator of the plurality of summation dimensions is split, and at least one third intermediate representation is obtained through a pruning strategy.

6. The method according to any one of claims 1 to 5, characterized in that Also includes: The first calculation in the second intermediate representation is replaced by the corresponding BLAS calculation in TBLASDialect; the first calculation is the calculation in the second intermediate representation with the first conversion form; the first conversion form is the conversion into the BLAS calculation; the TBLASDialect is the intermediate representation of the BLAS calculation; The converting the second intermediate representation into the first code comprises: The second intermediate representation replaced by the corresponding BLAS calculation in TBLASDialect is converted into the first code.

7. A compiling device, characterized in that: include: Front-end module: used to obtain the first source code; The first source code is a source code programmed in a first programming language; The first programming language is a programming language with a domain specific language DSL; extracting the DSL in the first source code; the DSL represents the linear algebra calculation process in the first source code; converting the DSL into a first intermediate representation; An optimization module: performing an optimization operation on the first intermediate representation to generate a second intermediate representation; the second intermediate representation is an equivalent intermediate representation of the first intermediate representation, and the computation amount of the second intermediate representation is less than the computation amount of the first intermediate representation; Backend module: converting the second intermediate representation into a first code; the coding language of the first code is the first coding language; combining the first code with the first source code to generate a second source code.

8. A processing device, characterized in that: include: A processor and a memory, the memory being used to store a computer program, the processor being used to call and run the computer program stored in the memory, and executing the compiling method according to any one of claims 1 to 6.

9. A chip, characterized in that: include: A processor, configured to call and run a computer program from a memory, so that a device equipped with the chip executes the compilation method according to any one of claims 1 to 6.

10. A computer-readable storage medium, characterized in that: Used to store a computer program, wherein the computer program enables a computer to execute the compiling method according to any one of claims 1 to 6.

Citation Information

Cited By

  • Intelligent agent lightweight deployment method and computing power elasticity distribution method

    CN120407044A