Processing method, apparatus and device for model intermediate representation of multi-core processor
By automatically propagating the fragment tensor type and split dimension in the compiler, the synchronization omission and over-synchronization problems of neural network models on multi-core processors are solved, achieving efficient splitting and distribution decisions and improving compilation efficiency.
Patent Information
- Application Number
- CN202611587293.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-10-14
- Publication Date
- 2026-11-20
AI Technical Summary
In existing technologies, when deploying neural network models to multi-core processors, the splitting, distribution, and synchronization are handled separately, which can easily lead to problems such as missing or excessive synchronization, and requires manual planning of data distribution.
By automatically propagating the fragment tensor type and split dimension in the compiler and setting cross-core synchronization instructions at mismatch locations, automatic decision-making for splitting, distribution, and synchronization is achieved, reducing manual intervention.
It improves compilation efficiency, reduces manual segmentation, annotation, and data distribution planning, and avoids synchronization omissions or over-synchronization issues.
Smart Images

Figure CN123137935A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of data processing technology, and more specifically, to a processing method, apparatus, and device for intermediate representation of a model for a multi-core processor. Background Technology
[0002] As neural network models continue to grow in size, deploying them to multi-core processors has become an important way to improve computing performance. During the process of compiling the model into instruction code for a multi-core processor, the compiler needs to determine how tensors are partitioned, how each processor core holds the tensors, and when cross-core synchronization should be performed.
[0003] Related solutions typically rely on manually labeling the partitioning method on the model's entry tensor, then propagating the partitioning strategy along the computation graph, and inserting set communication primitives at points of partitioning strategy mismatch, such as when the partitioning dimension of the tensor is inconsistent with the partitioning dimension required by the operator consuming that tensor. In this approach, partitioning, distribution, and synchronization are handled separately, and the data distribution and synchronization space still require manual planning, which can easily lead to problems such as missed synchronizations or excessive synchronization. Summary of the Invention
[0004] To address the aforementioned technical issues, this disclosure provides a method, apparatus, and device for processing intermediate representations of models for multi-core processors. By automatically propagating the fragment tensor type and partitioning dimension along the intermediate representation and automatically setting cross-core synchronization instructions at mismatch locations, the partitioning, distribution, and synchronization decisions are completed in a single compilation process, eliminating the need for manual partitioning and labeling.
[0005] To achieve the above objectives, in a first aspect, this disclosure provides a method for processing intermediate representations of a model on a multi-core processor, applied to a compiler configured to compile the model to be compiled into instruction code for a multi-core processor, the multi-core processor comprising N processor cores, where N is an integer greater than 1; the method includes: Obtain the intermediate representation of the model to be compiled, wherein the intermediate representation includes tensors; Based at least on the shape of the tensor in the intermediate representation, the fragment tensor type and the split dimension of the tensor in the intermediate representation are determined; the fragment tensor type is configured to indicate how the tensor is held on the N processor cores; the split dimension is configured to indicate the dimension along which the tensor is divided; Set the fragment tensor type and the segmentation dimension as the type information of the tensor in the intermediate representation; Based on the type information of the tensor in the intermediate representation, the storage allocation of the tensor on the N processor cores is determined.
[0006] Secondly, this disclosure provides a processing apparatus for intermediate representation of a model on a multi-core processor, applied to a compiler configured to compile a model to be compiled into instruction code for a multi-core processor, the multi-core processor including N processor cores, where N is an integer greater than 1; the apparatus includes: The acquisition module is configured to acquire an intermediate representation of the model to be compiled, the intermediate representation including tensors; The determination module is configured to determine, at least based on the shape of the tensor in the intermediate representation, the fragment tensor type and the partitioning dimension of the tensor in the intermediate representation; the fragment tensor type is configured to indicate how the tensor is held on the N processor cores; the partitioning dimension is configured to indicate the dimension along which the tensor is partitioned; The setting module is configured to set the fragment tensor type and the splitting dimension as the type information of the tensor in the intermediate representation; The storage allocation module is configured to determine the storage allocation of the tensor on the N processor cores based on the type information of the tensor in the intermediate representation.
[0007] Thirdly, this disclosure provides an electronic device, including: Processor; and A memory storing a computer program or readable instructions, which, when executed by the processor, implement the processing method for intermediate representation of a model for a multi-core processor as described above.
[0008] Fourthly, this disclosure provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the above-described processing method for intermediate representation of a model for a multi-core processor.
[0009] Fifthly, this disclosure provides a computer program product, including a computer program that, when executed by a processor, implements the above-described processing method for intermediate representation of a model for a multi-core processor.
[0010] By employing the above technical solution, after obtaining the intermediate representation of the model to be compiled, the type and partitioning dimension of the fragmented tensor are automatically determined based on the shape of the tensor in the intermediate representation, eliminating the need for manual annotation of the partitioning method on the model's entry tensor. The fragmented tensor type and partitioning dimension are set as the type information of the tensor in the intermediate representation, enabling the tensor's holding method on N processor cores and the dimensions along which the partitioning occurs to be recorded and transmitted along with the intermediate representation. Based on the tensor's type information in the intermediate representation, the storage allocation of the tensor on N processor cores can be determined, providing a basis for subsequent address allocation on multi-core processors and instruction code generation of the model to be compiled. This reduces the need for manual partitioning annotation and manual planning of data distribution, thereby improving compilation efficiency.
[0011] Other features and advantages of this disclosure will be described in detail in the following detailed description section. Attached Figure Description
[0012] The accompanying drawings are provided to further illustrate the present disclosure and form part of the specification. They are used together with the following detailed description to explain the present disclosure, but do not constitute a limitation thereof. In the drawings: Figure 1 This is a schematic diagram illustrating the data distribution of tensors of different fragmentation tensor types across multiple processor cores, according to an exemplary embodiment of this disclosure.
[0013] Figure 2 This is a schematic diagram illustrating the compilation stage of compiling a model to be compiled into instruction code for a multi-core processor using a compiler, according to an exemplary embodiment of this disclosure.
[0014] Figure 3 This is a flowchart illustrating a processing method for intermediate representation of a model for a multi-core processor, according to an exemplary embodiment of the present disclosure.
[0015] Figure 4 This is an intermediate representation showing a comparison diagram before and after processing according to the processing method of this disclosure, based on an exemplary embodiment of the present disclosure.
[0016] Figure 5 This is a flowchart illustrating a processing method for intermediate representation of a model for a multi-core processor, according to an exemplary embodiment of the present disclosure.
[0017] Figure 6 This is a block diagram of a processing apparatus for a multi-core processor, illustrating an exemplary embodiment of the present disclosure.
[0018] Figure 7 This is a schematic diagram of the structure of an electronic device configured to perform a processing method for a model intermediate representation for a multi-core processor, according to an exemplary embodiment of the present disclosure. Detailed Implementation
[0019] The specific embodiments of this disclosure will be described in detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are configured to illustrate and explain this disclosure only, and are not configured to limit this disclosure.
[0020] The following is an explanation of the terms used in this disclosure.
[0021] Intermediate Representation (IR) refers to the program representation used by the compiler to convert the model to be compiled into hardware instruction code. Intermediate representation includes tensors. A tensor can be understood as a data object with attributes such as shape and element type. The shape of a tensor is determined by its dimensions in each dimension. For example, the shape of a four-dimensional tensor can be represented as (1, 1, 128, 256), where each position represents the size of the tensor in the 0th, 1st, 2nd, and 3rd dimensions, respectively. In the tensor shape representation, dimensions are numbered sequentially from left to right as 0th dimension, 1st dimension, and so on; that is, the leftmost dimension is the highest dimension (0th dimension), and the rightmost dimension is the lowest dimension (last dimension). Intermediate representation can also include operators, which can be understood as computational nodes that perform operations on the input tensor and produce the output tensor.
[0022] The fragmented tensor type is configured to indicate how its associated tensor is held across N processor cores. In one possible implementation, the fragmented tensor type of the tensor is any one of a copy type, a distributed type, and a synchronization buffer type.
[0023] A copy type indicates that the tensor is not split, and each processor core holds all the data of the tensor. Correspondingly, if the tensor's fragmented tensor type is copy type, then each of the N processor cores can be allocated storage space configured to store all the data of the tensor.
[0024] The distributed type indicates that the tensor is divided into multiple non-overlapping parts along the partitioning dimension, and each of these parts is held by one of the N processor cores, carrying the partitioning dimension. Accordingly, the N processor cores partition the tensor along the partitioning dimension, with each processor core holding a portion of the tensor data. The multiple portions of data held by the N processor cores do not overlap. Storage space is allocated to each processor core to store its corresponding portion of data, according to the size of that portion.
[0025] The synchronization buffer type indicates that the data of the tensor held by each processor core needs to be aggregated across multiple processor cores. Cross-core aggregation refers to the process of multiple processor cores collaborating and combining their respective outputs or results into one place, also known as cross-core data synchronization. Specifically, each processor core reserves buffer space for the tensor in its private storage. Each processor core calculates the portion of the tensor corresponding to its own core and writes it into its own buffer space, carrying the partitioning dimension. Correspondingly, if the tensor fragmentation tensor type is the synchronization buffer type, the tensor can be divided into portions of data corresponding to each processor core according to the partitioning dimension. Each processor core is responsible for calculating the portion of the tensor corresponding to its own core, obtaining the calculation result, and writing the calculation result into its own buffer space, so that the cross-core synchronization instructions aggregate a tensor with a complete copy of the data held by each processor core.
[0026] like Figure 1 As shown, this disclosure defines three types of fragmented tensors, each corresponding to a different data distribution pattern on the processor core (i.e., the distribution or holding method of fragmented tensors on the processor core). Under the replication type, N processor cores (e.g., cores 0 to 3) each hold a complete copy of the tensor's data. Under the distributed type, N processor cores each hold non-overlapping portions of the tensor after it has been partitioned along the partitioning dimension (e.g., partial data 0 to partial data 3). Under the synchronization buffer type, each processor core reserves buffer space in its private storage (i.e., reserved space). Each core writes a portion of the results into its reserved space; that is, each core writes the calculation results (partial results) obtained by calculating the corresponding portion of the data into its reserved buffer space, so that after synchronous aggregation, the data stored in each core's buffer space can be synchronized so that each processor core holds a replication type tensor with complete data. Figure 1 This visually demonstrates the differences in data distribution among the three types of fragmented tensors on multi-core processors.
[0027] The partitioning dimension represents the dimension along which the tensor is partitioned. For example, for a four-dimensional tensor, the partitioning dimension could represent multi-core partitioning along the 0th, 1st, 2nd, or 3rd dimension. In one possible implementation, for tensors determined to be of distributed or synchronous buffer type, the value of the partitioning dimension is less than the reference dimension value, which is the dimension of the tensor minus one. For example, for a four-dimensional tensor, the reference dimension value could be 3, and the partitioning dimension could take values of 0, 1, 2, etc.
[0028] A multi-core processor refers to a processor that includes N processor cores, where N is an integer greater than 1. A processor core (also called a processor hub) is a hardware unit in the multi-core processor capable of independently executing computational tasks, and each processor core has its own private memory. In one possible implementation, each processor core's private memory reserves buffer space. Private memory refers to storage resources independently owned by each processor core and not shared with other processor cores, such as private on-chip memory or private DRAM partitions.
[0029] Buffer space refers to the storage area reserved in the processor core's private memory for synchronous buffer type tensors, used to write the calculation results obtained by the processor core in calculating the part of the tensor corresponding to the processor core.
[0030] The method for processing intermediate representations of a model for multi-core processors disclosed herein can be executed by an electronic device, which can be a terminal device, a server, a compiler device, or a computing device with compilation capabilities. That is, the method can be executed by the terminal device or the server alone, or by the terminal device and the server working together.
[0031] In one possible implementation, the method is applied to a compiler configured to compile the model to be compiled into instruction code for a multi-core processor. The multi-core processor comprises N processor cores, where N is an integer greater than 1, and each processor core has private memory.
[0032] like Figure 2 As shown, in the process of compiling the model to be compiled into instruction code for a multi-core processor using a compiler, the model to be compiled (e.g., a neural network model) is first converted into a high-level intermediate representation with a higher level of abstraction to preserve a more complete model semantics and operator structure; then the high-level intermediate representation is converted into a mid-level intermediate representation with a moderate level of abstraction (i.e., the intermediate representation of this disclosure), and the processing method of the model intermediate representation for multi-core processors disclosed in this disclosure is applied to this mid-level intermediate representation. By performing fragmented tensor type propagation (i.e., propagating the partitioning dimension and fragmented tensor type along the data flow), the tensor partitioning strategy (wherein, the strategy is configured to indicate whether the tensor needs to participate in multi-core partitioning, and if it needs to participate in multi-core partitioning, the partitioning strategy is also used to indicate the partitioning dimension) and tensor type (i.e., the fragmented tensor type of each tensor) can be determined. The segmentation strategy and tensor type are set in the intermediate representation to obtain the intermediate representation with completed segmentation annotation as output. This output is then converted into a low-level hardware intermediate representation that is closer to the hardware structure of the target multi-core processor, which includes the mapping from operators to hardware execution units and low-level information such as storage, address, and inter-core communication. Finally, hardware instruction code is generated, that is, hardware instruction code that can run on a multi-core processor is generated based on the low-level hardware intermediate representation.
[0033] like Figure 3 As shown, the processing method for intermediate representations of models for multi-core processors disclosed herein includes the following steps: Step S110: Obtain the intermediate representation of the model to be compiled, wherein the intermediate representation includes tensors.
[0034] The model to be compiled can be a neural network model, such as a Transformer model or a convolutional neural network model. The intermediate representation can be an intermediate representation generated by the compiler front-end that has not yet been segmented and labeled; that is, an intermediate representation that does not include the tensor type of tensor fragmentation and the segmentation dimension.
[0035] The intermediate representation can include one or more tensors. If there is only one, then the tensor is the entry tensor. If there are multiple tensors, the tensors in the intermediate representation include the entry tensor and tensors generated by operators. The entry tensor refers to the tensor parameters at the function entry point in the intermediate representation. The intermediate representation also includes operators and the connection relationships between the entry tensors and operators. After obtaining the intermediate representation of the model to be compiled, the compiler can parse the function definitions in the intermediate representation, and the tensor parameters listed in the obtained function signatures can be used as the entry tensors.
[0036] The connection between the entry tensor and the operator is used to describe the input-output dependency between the tensor and the operator in the intermediate representation. That is, starting from the function entry tensor, the output tensor is generated by the operator. The output tensor is then used as the input of the downstream operator to continue to be passed downstream until the final output tensor (i.e., the output tensor of the intermediate representation) is obtained.
[0037] Step S120: Determine the fragment tensor type and split dimension of the tensor in the intermediate representation based at least on the shape of the tensor in the intermediate representation.
[0038] In one possible implementation, if the tensors in the intermediate representation only include the entry tensor, the tensor fragmentation type and splitting dimension can be determined based on the shape of the entry tensor.
[0039] In one possible implementation, the tensors in the intermediate representation include the input tensor and the tensors generated by the operator. Step S120 includes steps S122 and S124.
[0040] Step S122: Determine the segmentation tensor type and segmentation dimension of the entry tensor based on its own shape.
[0041] The shape of the entry tensor refers to the dimension of the tensor itself at the function entry point in the intermediate representation. It can be defined in the function signature based on the input of the model to be compiled (such as the input activation tensor, weight tensor, etc.).
[0042] For an entry tensor, the number of valid dimensions and the splitting dimension can be determined based on its shape. The number of valid dimensions refers to the number of dimensions whose dimension value is not a specified value (e.g., 1). When the number of valid dimensions of the entry tensor is greater than 1, the fragmented tensor type is distributed, and the splitting dimension of the entry tensor is the dimension whose dimension value is the first dimension whose dimension value is not a specified value (e.g., 1) starting from the highest dimension. When the number of valid dimensions of the entry tensor is 1, the fragmented tensor type is replicated.
[0043] Optionally, step S122 includes: determining the partitioning dimension and the number of effective dimensions based on the shape of the entry tensor, wherein the partitioning dimension is the index of the first dimension whose size is not a specified value starting from the highest dimension. If the number of effective dimensions is one, the fragmented tensor type of the function entry tensor is determined to be a replication type, where the replication type indicates that each processor core holds all the data of the tensor. If the number of effective dimensions is multiple, the fragmented tensor type of the entry tensor is determined to be a distributed type, and the partitioning dimension is determined to be the index of the first dimension whose size is not a specified value starting from the highest dimension, where the distributed type indicates that the N processor cores partition the tensor along the partitioning dimension, each processor core holds a portion of the tensor's data, and the multiple portions of data held by the N processor cores do not overlap.
[0044] For example, in the tensor shape representation, dimensions are numbered sequentially from left to right as dimension 0, dimension 1, and so on, i.e., the leftmost dimension is the highest dimension (0th dimension), and the rightmost dimension is the lowest dimension (last dimension). If the shape of the function entry tensor is (1,1,128,256), the first dimension with a size not equal to 1 starting from the highest dimension is the second dimension, and there are multiple dimensions with a size not equal to a specified value (e.g., 1). Therefore, it can be determined that the fragmented tensor type of this entry tensor is a distributed type, with an initial partitioning dimension of 2. If the shape of the function entry tensor is (1,1,1,256), the first dimension with a size not equal to 1 starting from the highest dimension is the third dimension, and there is only one dimension with a size not equal to 1, then it can be determined that the fragmented tensor type of this entry tensor is a copy type.
[0045] It is worth noting that the partitioning dimension in this disclosure refers to the dimensional index along which the tensor is partitioned, rather than the dimension of the tensor itself. Taking the first dimension index (with a size not specified from the highest dimension) as the initial partitioning dimension, for example, a function entry tensor of shape (128, 256, 128, 64) has a dimension of 128 (the leftmost dimension), which is not a specified value (1). Therefore, the initial partitioning dimension of this function entry tensor is 0. Here, "0" indicates partitioning along the 0th dimension, that is, dividing the tensor into N processor cores along the leftmost dimension with a size of 128.
[0046] After determining the slice tensor type and initial split dimension of the function entry tensor, the function entry tensor can be marked as visited, and operators whose input operands have all been marked as visited can be added to the worksheet.
[0047] Through the above step S122, without requiring the user to provide any segmentation annotations, the fragment tensor type and initial segmentation dimension can be automatically determined for the function entry tensor, providing a starting point for subsequent tensor propagation.
[0048] Step S124: Based on the determined fragment tensor type and splitting dimension of the entry tensor, the connection relationship between the entry tensor and the operator in the intermediate representation, and the category to which the operator belongs, determine the fragment tensor type and splitting dimension of the tensor generated by the operator.
[0049] If multiple tensors are generated by the operator, step S124 above can be: retrieving ready operators one by one using a worksheet scheduling method. The ready operator is one whose segmentation tensor type and partitioning dimension have been determined for all input operands. The partitioning dimension of the output tensor of the ready operator is determined according to the partitioning dimension propagation rule corresponding to the category to which the ready operator belongs.
[0050] The worksheet refers to a set of ready operators. The worksheet scheduling method involves the compiler cyclically retrieving ready operators from the worksheet, determining the fragment tensor type and fragment dimension of the operator's output tensor according to the fragmentation dimension propagation rule corresponding to the operator's category, marking the output tensor as determined, and checking the subsequent operators to which the output tensor flows. If all tensors corresponding to the input operands of a subsequent operator are determined, the subsequent operator is added to the worksheet. This process is repeated until the worksheet is empty. In this way, the fragmentation dimension and fragment tensor type can propagate gradually along the data flow direction from the entry tensor to all tensors generated by the operators. The fragmentation dimension propagation rule indicates the specific rules for determining the fragment tensor type and fragmentation dimension of the operator's output tensor based on the fragment tensor type and fragmentation dimension of the tensors corresponding to its input operands for a given operator.
[0051] In this implementation, since the connection relationship between the entry tensor and the operator is configured to indicate the input-output dependency between each tensor and each operator in the intermediate representation, after the entry tensor is determined to be a fragmented tensor type and a split dimension, and after the entry tensor is marked as visited, operators whose input operands have all been marked as visited can be added to the worksheet according to the connection relationship; then, operators are retrieved one by one using a worksheet scheduling method, and the following processing is performed on the retrieved operators: Based on the fragment tensor type of the input tensor of the operator and the operator category of the operator, determine the fragment tensor type of the output tensor of the operator; if the fragment tensor type of the output tensor is a distributed type or a synchronous buffer type, then determine the partition dimension of the output tensor according to the partition dimension transfer rule corresponding to the operator category of the operator; if the fragment tensor type of the output tensor is a copy type, then do not set the partition dimension of the output tensor; record the determined partition dimension and fragment tensor type in the intermediate representation, and mark the output tensor as visited; and return to execute the steps of adding subsequent operators whose input operands have been marked as visited to the worktable according to the connection relationship, and retrieving operators one by one in the worktable scheduling method, until there are no operators in the worktable that have not been retrieved and whose input operands have been marked as visited, to obtain the fragment tensor type and partition dimension of each tensor in the intermediate representation.
[0052] When the number of tensors generated by the operator is small (less than K, where K is a positive integer), such as only one, the fragment tensor type and partitioning dimension of the tensor generated by the operator are determined based on the already determined fragment tensor type and partitioning dimension of the input tensor, the connection relationship between the input tensor and the operator, and the category to which the operator belongs. The determined fragment tensor type and partitioning dimension are then recorded in the intermediate representation. In other words, when there is only one tensor generated by the operator, the fragment tensor type and partitioning dimension of that tensor can be directly obtained from the input tensor through the operator, without needing to schedule operators one by one through a worksheet.
[0053] By employing steps S122 and S124, firstly, based on the shape of the entry tensor itself, the fragmentation tensor type and partitioning dimension of the entry tensor are automatically determined by identifying the dimension index of the first dimension whose size is not a specified value from the highest dimension, thus providing a starting point for subsequent propagation. Subsequently, based on the determined fragmentation tensor type and partitioning dimension of the entry tensor, the connection relationship between the entry tensor and operators in the intermediate representation, and the category to which the operators belong, the fragmentation tensor type and partitioning dimension of the tensors generated by the operators are determined, thereby obtaining the fragmentation tensor type and partitioning dimension of each tensor in the intermediate representation. Thus, the partitioning decision of each tensor and the data distribution decision of each processor core are completed simultaneously in the same propagation process, thereby improving compilation efficiency and reducing tensor synchronization omissions or over-synchronization problems that may arise from manual annotation and planning.
[0054] Optionally, the intermediate representation may include operators of various types, such as computational operators, inheritance operators, and dimension transformation operators. Different types of operators may have different transitive relationships between the split dimensions of their output tensors and the split dimensions of their input tensors; therefore, the split dimension transitive rules include: For operators belonging to the computational type, the splitting dimension of the output tensor is taken as the dimension along which the parallel computation of the computational type operator proceeds.
[0055] Among them, computational operators refer to operators with a fixed parallel computation dimension. The partitioning dimension of the output tensor of a computational operator does not inherit the partitioning dimension of the input tensor, but rather takes the dimension along which the computational operator itself performs parallel computation.
[0056] For example, computational operators include matrix multiplication operators and convolution operators; the partitioning dimension of the output tensor of the matrix multiplication operator is the innermost dimension of the operands of the matrix multiplication operator; the partitioning dimension of the output tensor of the convolution operator is the batch dimension of the convolution operator. By setting a preset partitioning dimension for computational operators, the computational task partitioning of each processor core can be aligned with the data dependency direction of operators such as matrix multiplication and convolution.
[0057] For example, if the tensor input to a computational operator has the shape (1,1,128,256), its fragmented tensor type can be determined to be distributed and its partitioning dimension is 2 through the aforementioned steps S110 and S120. That is, it is divided along the second dimension and distributed to multiple processor cores. When this tensor is multiplied by the weight tensor by the matrix multiplication operator, the matrix multiplication operator belongs to the computational operator. Its output tensor does not inherit the partitioning dimension of the input activation tensor, but takes the innermost dimension of the matrix multiplication operator operand, that is, the third dimension. Therefore, the output tensor is partitioned along the third dimension, so that the computational task division of each processor core in matrix multiplication is consistent with the data dependency direction of matrix multiplication.
[0058] By making the partitioning dimension of the output tensor of computational operators a fixed dimension along which the parallel computation of the computational operator proceeds, the parallel computation characteristics of computational operators can be adapted: matrix multiplication is performed along the innermost dimension, and if each processor core partitions the data along the innermost dimension, it can independently compute the corresponding partial sum in the output tensor without exchanging intermediate data between cores; convolution is performed independently on each sample along the batch dimension, and if each processor core partitions the data along the batch dimension, it can independently process different samples without exchanging intermediate data between cores. This partitioning method ensures that the computational task partitioning of each processor core is consistent with the data dependency direction of the computational operator, reducing additional cross-core synchronization and matching the partitioning dimension with the parallel computation characteristics of the computational operator.
[0059] For operators belonging to the inherited type, the partitioning dimension of the output tensor inherits the partitioning dimension of the first operand of the inherited type operator, where the first operand is the first input operand in the order of the multiple input operands of the inherited type operator.
[0060] Inheritance operators are those whose output tensor's partitioning dimension inherits the partitioning dimension of the tensor corresponding to its input operand. Specifically, the partitioning dimension of the output tensor of an inheritance operator inherits the partitioning dimension of the tensor corresponding to the first operand of that inheritance operator.
[0061] Here, the first operand can be understood as the first input operand in the input operand list of the inherited class operator. The order of operands in this list is usually determined by the compiler front-end when generating the intermediate representation, for example, according to the order of operand appearance in the source code (usually from left to right), or according to the operand order specified in the intermediate representation.
[0062] For example, inherited operators include element-wise operators, vector operators, etc. Optionally, if an inherited operator has two input operands and the partitioning dimensions of the two input operands are inconsistent, an alarm message can be output to locate the source of the partitioning dimension mismatch. The alarm message can be recorded only in the compilation log without blocking compilation; the data that needs to be synchronized can be determined as a synchronization buffer type according to the normal process and cross-core synchronization instructions can be set.
[0063] For example, if the first input tensor of an inherited operator (such as an element-wise addition operator) has a shape of (1,1,128,256), its fragmented tensor type is determined to be distributed and its partitioning dimension is 2 through the aforementioned steps S110 and S120, meaning it is partitioned along the second dimension and distributed to multiple processor cores; the second input tensor has the same shape as the first input tensor. When these two tensors are added by the element-wise addition operator, since the element-wise addition operator is an inherited operator, the partitioning dimension of its output tensor inherits the partitioning dimension of the tensor corresponding to the first operand, i.e., the second dimension. Therefore, the output tensor is also partitioned along the second dimension, with a partitioning dimension of 2. If the partitioning dimension of the second input tensor is inconsistent with that of the first input tensor, an alarm message can be output, and the data that needs to be synchronized is determined to be of the synchronization buffer type, and a cross-core synchronization instruction is set.
[0064] By keeping the output tensor of the inherited operator the same partitioning dimension as the first operand, each processor core only needs to process the input partition held by its own core to obtain the output partition held by its own core. There is no need to exchange intermediate data between cores, thereby reducing additional cross-core synchronization and matching the partitioning dimension with the element-wise parallel computation characteristics of the inherited operator.
[0065] For operators belonging to the dimension transformation category, the segmentation dimension of the output tensor is obtained by mapping the segmentation dimension of the input tensor according to the dimension transformation relationship of the dimension transformation category operator.
[0066] Among them, dimension transformation operators refer to operators that change the dimensional structure or dimensional order of a tensor. The split dimensions of the output tensor of this type of operator cannot directly inherit the split dimensions of the input tensor. Instead, the split dimensions of the input tensor need to be mapped according to the dimensional transformation relationship of the operator itself to obtain the split dimensions of the output tensor.
[0067] For example, dimension transformation operators include transpose operators, shape rearrangement operators, and slicing operators. The slicing dimension of the output tensor of the transpose operator is determined based on the slicing dimension of the input tensor of the transpose operator and the dimensions exchanged by the transpose operator. The slicing dimension of the output tensor of the shape rearrangement operator is the dimension obtained by subtracting one from the shrunk dimension and adding one to the expanded dimension of the input tensor of the shape rearrangement operator. The slicing dimension of the output tensor of the slicing operator is the innermost dimension. Thus, even if the tensor dimension structure changes, the correct mapping of the slicing dimensions can be maintained, reducing the unnecessary cross-core synchronization introduced by the misalignment of slicing dimensions due to dimension structure adjustments.
[0068] For example, if the input tensor shape of a one-dimensional transformation operator (such as the transpose operator) is (1,1,128,256), and its fragmented tensor type is determined to be distributed with a partitioning dimension of 2 through the aforementioned steps S110 and S120 (i.e., it is partitioned along the second dimension and distributed to multiple processor cores), when this tensor exchanges its second and third dimensions using the transpose operator, since the transpose operator is a dimension transformation operator, the partitioning dimensions of its output tensor are swapped according to the order of the swapped dimensions. That is, if it was originally partitioned along the second dimension, it should be partitioned along the third dimension after the swap, thus the partitioning dimension of the output tensor becomes 3. Through the partitioning dimension propagation rules of dimension transformation operators, even if the dimension order of the tensor changes, the correct mapping of the partitioning dimensions can be maintained, minimizing the introduction of unnecessary cross-core synchronization due to partitioning dimension misalignment caused by dimensional structure adjustments.
[0069] By mapping the partitioning dimension of the input tensor according to the dimensional transformation relationship of the dimensional transformation operator, the partitioning dimension of the output tensor is kept consistent with the transformed dimensional structure. This ensures that the data held by each processor core still corresponds to a continuous data range after the dimensional transformation, avoiding unnecessary cross-core synchronization caused by the misalignment of the partitioning dimension, and making the partitioning dimension match the parallel computing characteristics of the dimensional transformation operator.
[0070] It should be understood that the operator types described above are merely illustrative, and there may be many other types of operators. This embodiment does not impose any specific limitations. Through the above-described rules for passing the partitioning dimension, the partitioning tensor type and partitioning dimension of the input tensor can be correctly passed to the output tensor for different types of operators, thereby accurately determining the partitioning tensor type and partitioning dimension of each tensor in the intermediate representation during a single worksheet scheduling process.
[0071] By using the above rules for passing or propagating the partitioning dimension, the partitioning tensor type and partitioning dimension of the tensor corresponding to the input operand of the operator can be correctly passed to the tensor generated by the operator for different types of operators, thereby accurately determining the partitioning tensor type and partitioning dimension of each tensor in the intermediate representation during a worksheet scheduling process.
[0072] During the propagation of data along the intermediate representation according to the aforementioned partitioning dimension propagation rules, the fragment tensor type and partitioning dimension of each tensor can usually be passed from the input tensor to the tensor generated by the operator. However, since different operators may have different requirements for the partitioning dimension of the tensors corresponding to their input operands, or the tensors corresponding to multiple input operands of the same operator may be generated by different operators, during the propagation process, the partitioning dimension of a tensor may be inconsistent with the partitioning dimension required by the operator consuming that tensor, or the partitioning dimensions of multiple tensors consumed by the same operator may be inconsistent with each other. The above inconsistencies can be collectively referred to as partitioning dimension conflicts. When a partitioning dimension conflict occurs, if the tensor is still propagated as a distributed type, the operator consuming the tensor cannot correctly consume or use the tensor. Based on this, in one possible implementation, determining the fragment tensor type and partitioning dimension of the tensors in the intermediate representation further includes: In the event of a split dimension conflict in the intermediate representation, the tensor involved in the split dimension conflict is determined to be the synchronization buffer type; the split dimension conflict includes: the split dimensions of multiple tensors consumed by the same operator are inconsistent with each other, or the split dimension of the tensor is inconsistent with the split dimension required by the operator consuming the tensor.
[0073] In this approach, tensors involved in conflicting partition dimensions are identified as synchronous buffer types when the partition dimensions of multiple tensors consumed by the same operator are inconsistent, or when the partition dimensions of a tensor are inconsistent with the partition dimensions required by the consuming operator. Since synchronous buffer type data requires cross-core aggregation before consumption, while distributed type data can be consumed without cross-core aggregation, identifying tensors involved in partition dimension conflicts as synchronous buffer types allows cross-core aggregation to merge the partial data held by each processor core into complete data, ensuring that each processor core obtains complete data and is no longer affected by the original inconsistencies in partition dimensions, thus guaranteeing that the operator consuming the tensor can execute correctly.
[0074] In one possible implementation, the method further includes: setting cross-core synchronization instructions in the intermediate representation for the tensors involved in the split dimension conflict, the cross-core synchronization instructions being configured to aggregate the data of the tensors in the N processor cores into complete data.
[0075] By setting cross-core synchronization instructions for tensors involved in split dimension conflicts, after the tensor is determined to be a synchronization buffer type, each processor core writes the portion of the tensor corresponding to its own core, calculated by its own core, into a reserved buffer space in its own core's private memory. The cross-core synchronization instructions then aggregate the portion of data written by each processor core into its own core's buffer space, so that each processor core holds a complete copy of the tensor. This reduces split dimension conflicts and ensures that operators consuming the tensor can execute correctly. At the same time, the cross-core synchronization instructions are set on demand after the tensors involved in the split dimension conflicts are determined. There is no need to set up a separate synchronization derivation stage outside of the split propagation, nor is there a need to manually plan the data distribution of each processor core. Therefore, splitting, distribution, and synchronization decisions can be completed automatically in a single compilation process, reducing synchronization omissions and over-synchronization, and improving compilation efficiency.
[0076] Step S130: Set the fragment tensor type and the split dimension to the type information of the tensor in the intermediate representation.
[0077] Optionally, for each tensor in the intermediate representation, its fragment tensor type is set to a part of the type information of the tensor in the intermediate representation; for tensors determined to be of distributed type or synchronous buffer type, its partition dimension is also set to a part of the type information of the tensor; for tensors determined to be of replicated type, no partition dimension is set.
[0078] The above-mentioned setting method can be to record the fragmented tensor type and the partitioning dimension on the node or declaration corresponding to the tensor in the intermediate representation in the form of type markers, attribute markers, or annotations. For example, a type attribute field can be added to the node corresponding to the tensor, the fragmented tensor type and partitioning dimension can be embedded in the type signature of the tensor, an annotation can be added after the tensor declaration, or a corresponding field can be set for the tensor object in the data structure of the intermediate representation. For example, for a tensor that is determined to be of the distributed type and has a partitioning dimension of 2, its type information can be marked as "distributed, partitioning dimension = 2". For a tensor that is determined to be of the replication type, its type information can be marked as "replication type" without setting a partitioning dimension. For a tensor that is determined to be of the synchronization buffer type and has a partitioning dimension of 2, its type information can be marked as "synchronization buffer type, partitioning dimension = 2". This disclosure does not limit the setting method, as long as it enables the subsequent address allocation, executor assignment, and instruction code generation stages to directly read the type information.
[0079] Step S140: Determine the storage allocation of the tensor on the N processor cores based on the type information of the tensor in the intermediate representation.
[0080] Wherein, the value range of the tensor's partitioning dimension is from 0 to the tensor's dimension minus one; the step of determining the storage allocation of the tensor on the N processor cores based on the tensor's type information in the intermediate representation includes: If the tensor's fragmented tensor type is a copy type, then each of the N processor cores is allocated storage space configured to store all the data of the tensor.
[0081] If the tensor's fragmented tensor type is distributed, then the tensor is divided into partial data corresponding to each processor core according to the partitioning dimension, and storage space is allocated to each processor core to store the partial data corresponding to that processor core according to the size of the partial data corresponding to each processor core.
[0082] If the tensor's fragmented tensor type is a synchronization buffer type, each processor core reserves buffer space for the tensor in its private storage. The tensor is divided into parts of data corresponding to each processor core according to the partitioning dimension. Each processor core is responsible for calculating the part of the tensor corresponding to its own processor core to obtain the calculation result. The calculation result is written into the buffer space of the corresponding processor core so that the cross-core synchronization instructions converge to form a tensor with a complete copy of the data for each processor core.
[0083] In this implementation, by allocating storage space configured to store all the data of the tensor for the replication type tensor, each of the N processor cores holds a complete copy of the tensor, and each processor core can independently read all the data of the tensor; by allocating storage space for the distributed type tensor according to the size of the partial data corresponding to each processor core, each processor core stores only the partial data of the tensor corresponding to its own processor core, and the partial data stored by the N processor cores do not overlap and together constitute the tensor, which can be consumed without cross-core aggregation; by reserving buffer space in the private storage of each processor core for the synchronization buffer type tensor, and dividing the tensor into partial data corresponding to each processor core according to the partitioning dimension, each processor core can write the calculation result obtained by the core for the partial data into its own buffer space, and the cross-core synchronization instruction aggregates the calculation results written by each processor core into a tensor for each processor core to hold a complete copy of the data, thereby eliminating partitioning dimension conflicts and ensuring that the operator consuming the tensor can be executed correctly.
[0084] In one possible implementation, for each tensor identified as either a distributed or synchronous buffer type, after the tensor is divided along its partitioning dimension, the size of the portion held by each processor core can be equal to the size of the tensor along that partitioning dimension rounded up to N.
[0085] In another possible implementation, if the size of the tensor in the partition dimension is not divisible by N, the size of the portion held by the first portion of the N processor cores is equal to the size obtained by rounding up the ratio of the size in the partition dimension to N, the size of the portion held by the second portion of the N processor cores (excluding the first portion of the processor cores) is equal to the size obtained by rounding down the ratio of the size in the partition dimension to N, and the number of the first portion of the processor cores is equal to the remainder obtained by dividing the size in the partition dimension by N.
[0086] Specifically, when the size of the tensor along the partitioning dimension is not divisible by N, the N processor cores can be divided into a first group and a second group. The number of processor cores in the first group is equal to the remainder of the size divided by N, and the second group includes all the processor cores other than those in the first group. The tensor is divided along the partitioning dimension into N parts, each corresponding one-to-one with one of the N processor cores. The size of the part corresponding to a processor core in the first group is equal to the size obtained by rounding up the ratio, and the size of the part corresponding to a processor core in the second group is equal to the size obtained by rounding down the ratio. For tensors determined to be of a distributed type, each of the N processor cores holds its own corresponding part. For tensors determined to be of a synchronization buffer type, each of the N processor cores writes its corresponding part into a reserved buffer space in its own private storage; the part written into the buffer space is the data of that core.
[0087] Let's take a tensor with the shape (1, 1, 128, 256) as an example again. The tensor's partitioning dimension is 2, and its size in the partitioning dimension is 128. Using 3 processor cores as an example (N processor cores), since 128 divided by 3 rounded up is 43 and rounded down is 42, with a remainder of 2, we can divide the 3 processor cores into two groups. The first group contains two processor cores, and the second group contains one processor core. The two processor cores in the first group can hold a portion of size 43, and the one processor core in the second group can hold a portion of size 42. The sum of the portions held by the three processor cores is 43 + 43 + 42 = 128, consistent with the original size, thus avoiding address calculation out-of-bounds errors.
[0088] This implementation method ensures that even when the size of a tensor in the partition dimension is not divisible by N, the size of the portion held by each processor core can still be clearly defined, making the sum of the sizes of the portions held by N processor cores equal to the original size of the tensor in that partition dimension. This avoids address calculation out-of-bounds errors caused by direct rounding up for allocation. At the same time, for tensors of the synchronization buffer type, the data written by each processor core to its reserved buffer space is also determined according to the same rule, so that the buffer space capacity matches the actual partition size of each core, thereby ensuring the accuracy of subsequent address allocation and cross-core synchronization.
[0089] Through steps S110-S140, after obtaining the intermediate representation of the model to be compiled, the fragment tensor type and splitting dimension of the tensors in the intermediate representation are automatically determined based on at least the shape of the tensors in the intermediate representation. The fragment tensor type and the splitting dimension are set as the type information of the tensors in the intermediate representation. Then, the storage allocation of the tensors on the N processor cores is determined based on the type information. The whole process does not require manual annotation of the splitting method on the model entry tensor, nor does it require separate planning of data distribution and synchronization buffer space for each processor core outside of splitting propagation. Thus, the splitting, distribution and storage allocation decisions can be automatically completed in one compilation process of the intermediate representation, providing directly consumable type information for subsequent address allocation and instruction code generation, thereby improving compilation efficiency.
[0090] Furthermore, by determining the number of dimensions with a size other than 1 based on the shape of the entry tensor itself, the entry tensor is determined to be of the replication type when there is only one dimension, and of the distributed type when there are multiple dimensions. The index of the first dimension with a size other than 1 from the highest dimension is used as the initial split dimension. Thus, without requiring any split label from the user, the fragment tensor type and initial split dimension can be automatically determined for the entry tensor. Each tensor generated by the operator needs to be checked to see if the fragment tensor type and split dimension have been determined for its input operands to determine whether the operator is ready. Therefore, determining the fragment tensor type and split dimension for the entry tensor provides a starting point for the subsequent propagation of the fragment tensor type and split dimension.
[0091] Furthermore, by retrieving ready operators one by one using a worksheet scheduling method, and determining the split dimension of the output tensor according to the split dimension transfer rule corresponding to the operator's category, the split dimension can be matched with the parallel computing characteristics of various operators, and unnecessary cross-core synchronization is avoided due to the misalignment of split dimensions caused by the adjustment of the dimension structure.
[0092] Furthermore, when the partition dimensions of multiple tensors consumed by the same operator are inconsistent, or when the partition dimensions of a tensor are inconsistent with the partition dimensions required by the operator consuming that tensor, the tensor involved in the conflict is identified as a synchronization buffer type. A cross-core synchronization instruction is then set after the defining operator of this tensor. This causes each processor core to write the portion of the tensor calculated by its own core into a reserved buffer space in its own core's private memory. The cross-core synchronization instruction then aggregates these data into a tensor where each processor core holds a complete copy of the data. This eliminates partition dimension conflicts and ensures that cross-core synchronization is triggered only where truly needed, avoiding synchronization omissions and over-synchronization.
[0093] The following will combine Figure 4 Taking the "Normalization (RMSNorm) → Fully Connected (Linear) → Residual Addition (Add)" segment in the Transformer model structure shown as an example, according to... Figure 5 The overall process shown is explained.
[0094] First, such as Figure 4 As shown in (a), an intermediate representation of a tensor indeterminate form can be input into the compiler (i.e., the aforementioned...). Figure 2 The intermediate representation (in the middle layer of the algorithm) serves as the intermediate representation before propagation. This intermediate representation includes the entry tensor %act, the normalized weight tensor %norm_w, the fully connected weight tensor %lin_w, and sequentially connected normalization, fully connected, and addition operators. %act has a shape of (1,1,128,256); %norm_w is the weight tensor of the normalized operator; and %lin_w is the weight tensor of the fully connected operator. Tensor %n in the intermediate representation is obtained by operating the normalization operator on %act and %norm_w; tensor %l is obtained by operating the fully connected operator on tensor %n and the fully connected weight tensor %lin_w; and tensor %a is obtained by operating the addition operator on the entry tensor %act and tensor %l. At this point, the fragmentation type and splitting dimension of each tensor are not yet labeled in the intermediate representation.
[0095] according to Figure 5The overall process shown, after inputting the aforementioned undefined intermediate representation of the tensor (the intermediate representation before propagation) to the compiler, can first execute the function input parameter initialization. For the tensor %act, in the shape (1,1,128,256), the first dimension with a size not equal to 1 from the highest dimension is the second dimension, and there are multiple dimensions with a size not equal to 1. Therefore, the fragmented tensor type of %act can be determined to be a distributed type (i.e., DTensor), with a partitioning dimension of 2. For %norm_w, if its shape has only one dimension with a size not equal to 1, it can be determined to be a copy type (RTensor). For %lin_w, its fragmented tensor type and partitioning dimension can be determined based on its shape and operator consumption method.
[0096] Then, the partitioning dimension is propagated. Using the partitioning tensor type and partitioning dimension of %act, the partitioning tensor type and partitioning dimension of the tensors output by each operator are determined according to the connection relationship between the input tensor and the operator in the intermediate representation. Since normalization can be performed on %act and %norm_w using a normalization operator, tensor %n is obtained. Because the operator used in the normalization process is an inherited operator or a dimension-preserving operator, its output tensor %n inherits the partitioning dimension of %act; therefore, the partitioning tensor type of %n is distributed, and the partitioning dimension is 2.
[0097] Subsequently, mismatch detection is performed and synchronization instructions are automatically inserted. Specifically, if the partitioning dimension of tensor %n is inconsistent with the partitioning dimension required by its consumer, or if the partitioning dimension of tensor %n changes before and after its generation (i.e., the partitioning dimensions of multiple tensors consumed by the same operator are inconsistent, including %n), the fragmented tensor type of tensor %n can be determined to be a synchronization buffer type (SBTensor), and a cross-core synchronization instruction is set after the definition operator rmsnorm of %n. Each processor core writes the calculation result obtained by calculating a portion of the tensor allocated to its core into its reserved buffer space; through the cross-core synchronization instruction, the portion of data written by each processor core into its own buffer space can be aggregated to form the complete data of the tensor (i.e., a synchronization RTeensor), so that each processor core holds a complete copy of the tensor data. Here, %s is the tensor aggregated by the cross-core synchronization instruction, and the fragmented tensor type of %s is a copy type.
[0098] Subsequently, the operator used for full connection processing can be processed using the output tensor of the operator used for normalization, and the output tensor is then processed. Since the operator used for full connection processing is a computational operator, the split dimension of its output tensor is taken as the innermost dimension of the operand. Specifically, the operator for full connection processing takes %s (copy type) and %lin_w (distributed type) as input, and according to the split dimension propagation rules of computational operators, takes the innermost dimension of the operand (e.g., the 3rd dimension) as the split dimension of the output tensor %l (e.g., split dimension = x, ...). Figure 4 (b) shows the case where x is 3). Then, the mismatch detection is performed again as described above, and finally the fragmented tensor type of tensor %l is distributed or synchronous buffered, and carries the corresponding split dimension.
[0099] Subsequently, the operator used for residual addition can be processed using the input tensor %act (normalized) and the output tensor %l (fully connected) to produce a new tensor. Since the operator used for residual addition is an addition operator, which is an inherited operator, the partition dimension of its output tensor inherits the partition dimension of the first operand. For %a = add(%act, %l), the addition operator takes %act (distributed type, partition dimension = 2) and %l (distributed type, partition dimension = x) as input. If the partition dimension of %act is inconsistent with that of %l, %l is determined to be a synchronization buffer type, and a cross-core synchronization instruction is set after the linear operator in the definition of %l, for example: %rs = sync_tensor(%l); %a = add(%act, %rs). Here, %rs is a replicated type tensor obtained through synchronization and has no partition dimension, so it can be directly added to %act. The output tensor %a of the addition operator is ultimately determined to be a distributed type based on %act and has the same partition dimension as %act.
[0100] Finally, tensor type determination is performed to obtain, as follows: Figure 4The intermediate representation of the output tensor model shown in (b) is (i.e., the processed intermediate representation). In the intermediate representation, each tensor is labeled with its fragmented tensor type and partition dimension. The entry tensor %act is a distributed type (DTensor) with a partition dimension of 2; the normalized weight tensor %norm_w is a replicated type (RTensor); the fully connected weight tensor %lin_w is a distributed type (DTensor) with a partition dimension of 2; the tensor %n output by the normalization operator is determined to be a synchronization buffer type (SBTensor) with a partition dimension of 2, and a cross-core synchronization instruction is provided after its definition operator. After aggregation, a replicated type (RTensor) tensor %s is obtained; the tensor %l output by the fully connected operator is determined to be a synchronization buffer type (SBTensor) with a partition dimension of 3, and a cross-core synchronization instruction is provided after its definition operator. After aggregation, a replicated type (RTensor) tensor %rs is obtained; the tensor %a output by the addition operator is a distributed type (DTensor) with a partition dimension of 2. Overall, this intermediate representation sets up cross-core synchronization instructions at locations where there are conflicts in the splitting dimensions, so that each tensor has a clear holding method and splitting dimension.
[0101] The processed intermediate representation can be directly used for address allocation and instruction code generation in multi-core processors. For example, the address allocation stage can calculate the private memory address of each processor core based on the distributed type and partitioning dimension; the executor assignment stage can determine the computational tasks of each processor core based on the fragment tensor type; and the code generation stage can generate corresponding cross-core synchronization code based on cross-core synchronization instructions.
[0102] Please see Figure 6 This disclosure also provides a processing apparatus 200 for intermediate representation of a model for a multi-core processor, comprising: The acquisition module 210 is configured to acquire an intermediate representation of the model to be compiled, the intermediate representation including tensors; The determining module 220 is configured to determine, at least based on the shape of the tensor in the intermediate representation, the fragment tensor type and the splitting dimension of the tensor in the intermediate representation; the fragment tensor type is configured to indicate how the tensor is held on the N processor cores; the splitting dimension is configured to indicate the dimension along which the tensor is divided; Setting module 230 is configured to set the fragment tensor type and the split dimension as the type information of the tensor in the intermediate representation; The storage allocation module 240 is configured to determine the storage allocation of the tensor on the N processor cores based on the type information of the tensor in the intermediate representation.
[0103] Optionally, the fragmented tensor type includes at least one of the following: copy type, distributed type, and synchronized buffer type; The replication type indicates that each processor core holds all the data of the tensor; The distributed type indicates that the N processor cores divide the tensor along the partitioning dimension, each processor core holds a portion of the tensor data, and the multiple portions of data held by the N processor cores do not overlap. The synchronization buffer type indicates that the data of the tensor held by each processor core needs to be converged across multiple processor cores.
[0104] Optionally, the tensors in the intermediate representation include an input tensor and tensors generated by operators, wherein the input tensor is the tensor in the intermediate representation corresponding to the input of the model to be compiled; the determining module 220 is further configured to determine the fragment tensor type and splitting dimension of the input tensor based on the shape of the input tensor itself; and to determine the fragment tensor type and splitting dimension of the tensor generated by the operators based on the determined fragment tensor type and splitting dimension of the input tensor, the connection relationship between the input tensor and the operators in the intermediate representation, and the category to which the operators belong.
[0105] Optionally, the determining module 220 is further configured to, in the event of a split dimension conflict in the intermediate representation, determine the tensor involved in the split dimension conflict as the synchronization buffer type; the split dimension conflict includes: the split dimensions of multiple tensors consumed by the same operator are inconsistent with each other, or the split dimension of the tensor is inconsistent with the split dimension required by the operator consuming the tensor.
[0106] Optionally, the processing apparatus 200 for the intermediate representation of the model for a multi-core processor further includes a synchronization instruction setting module configured to set cross-core synchronization instructions in the intermediate representation for the tensors involved in the split dimension conflict, the cross-core synchronization instructions being configured to aggregate the data of the tensors in the N processor cores into complete data.
[0107] Optionally, the determining module 220 is further configured to determine the number of dimensions in the entry tensor whose size is not 1; if the number is one, the fragment tensor type of the entry tensor is determined to be a replication type, the replication type indicating that each processor core holds all the data of the tensor; if the number is multiple, the fragment tensor type of the entry tensor is determined to be the distributed type, and the partitioning dimension is determined to be the highest dimension among the number of dimensions, the distributed type indicating that the N processor cores partition the tensor along the partitioning dimension, each processor core holds a portion of the tensor data, and the multiple portions of data held by the N processor cores do not overlap.
[0108] Optionally, the determining module 220 is further configured to retrieve ready operators one by one in a worksheet scheduling manner, wherein the ready operators are operators whose segmentation tensor type and segmentation dimension have been determined for all input operands; and determine the segmentation dimension of the output tensor of the ready operator according to the segmentation dimension transfer rule corresponding to the category to which the ready operator belongs.
[0109] Optionally, the rule for passing the segmentation dimension includes: For operators belonging to the computational type, the splitting dimension of the output tensor is taken as the dimension along which the parallel computation of the computational type operator proceeds; For operators belonging to the inherited type, the partitioning dimension of the output tensor inherits the partitioning dimension of the first operand of the inherited type operator, where the first operand is the input operand that is sorted first among the multiple input operands of the inherited type operator. For operators belonging to the dimension transformation category, the segmentation dimension of the output tensor is obtained by mapping the segmentation dimension of the input tensor according to the dimension transformation relationship of the dimension transformation category operator.
[0110] Optionally, the computational operators include matrix multiplication operators and convolution operators; the partitioning dimension of the output tensor of the matrix multiplication operator is the innermost dimension of the operands of the matrix multiplication operator; the partitioning dimension of the output tensor of the convolution operator is the batch dimension of the convolution operator.
[0111] Optionally, the dimension transformation operators include transpose operators, shape rearrangement operators, and slicing operators; the slicing dimension of the output tensor of the transpose operator is the dimension after the slicing dimension of the input tensor of the transpose operator is interchanged according to the dimensional order exchanged by the transpose operator; the slicing dimension of the output tensor of the shape rearrangement operator is the dimension after subtracting one from the shrunk dimension and adding one to the expanded dimension of the input tensor of the shape rearrangement operator; the slicing dimension of the output tensor of the slicing operator is the innermost dimension.
[0112] Optionally, the value range of the split dimension of the tensor is from 0 to the dimension of the tensor minus one; the storage allocation module 240 is further configured to allocate storage space configured to store all data of the tensor to each of the N processor cores if the split tensor type of the tensor is a copy type. If the tensor's fragmented tensor type is distributed, then the tensor is divided into partial data corresponding to each processor core according to the partitioning dimension, and storage space is allocated to each processor core to store the partial data corresponding to that processor core according to the size of the partial data corresponding to each processor core. If the tensor's fragmented tensor type is a synchronization buffer type, each processor core reserves buffer space for the tensor in its private storage. The tensor is divided into parts of data corresponding to each processor core according to the partitioning dimension. Each processor core is responsible for calculating the part of the tensor corresponding to its own processor core to obtain the calculation result. The calculation result is written into the buffer space of the corresponding processor core so that the cross-core synchronization instructions converge to form a tensor with a complete copy of the data for each processor core.
[0113] Optionally, the storage allocation module 240 is further configured such that, if the size of the tensor in the partition dimension is not divisible by N, the size of the portion held by the first part of the N processor cores is equal to the size obtained by rounding up the ratio of the size in the partition dimension to N, the size of the portion held by the second part of the N processor cores other than the first part of the processor cores is equal to the size obtained by rounding down the ratio of the size in the partition dimension to N, and the number of the first part of the processor cores is equal to the remainder obtained by dividing the size in the partition dimension by N.
[0114] Figure 7 This is a block diagram illustrating an electronic device 100 according to an exemplary embodiment. Figure 7 As shown, the electronic device 100 may include a processor 101 and a memory 102. The electronic device 100 may also include one or more of a multimedia component 103, an input / output (I / O) interface 104, and a communication component 105. Specifically, the electronic device may be a data processing device such as a server or terminal, and may be capable of running a processing method for intermediate representation models used in multi-core processors.
[0115] The processor 101 is configured to control the overall operation of the electronic device 100 to complete all or part of the steps in the processing method for the intermediate representation of a multi-core processor. The memory 102 is configured to store various types of data to support the operation of the electronic device 100. This data may include, for example, instructions for any application or method configured to operate on the electronic device 100, and application-related data. The memory 102 may be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as Static Random Access Memory (SRAM), Electrically Erasable Programmable Read-Only Memory (EEPROM), Erasable Programmable Read-Only Memory (EPROM), Programmable Read-Only Memory (PROM), Read-Only Memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. The multimedia component 103 may include a screen and audio components. The screen may be, for example, a touchscreen, and the audio component is configured to output and / or input audio signals. For example, the audio component may include a microphone configured to receive external audio signals. The received audio signals may be further stored in memory 102 or transmitted via communication component 105. The audio component also includes at least one speaker configured to output audio signals. I / O interface 104 provides an interface between processor 101 and other interface modules, such as a keyboard, mouse, buttons, etc. These buttons may be virtual or physical buttons. Communication component 105 is configured for wired or wireless communication between the electronic device 100 and other devices. Wireless communication, such as Wi-Fi, Bluetooth, Near Field Communication (NFC), 2G, 3G, 4G, NB-IoT, eMTC, or other 5G technologies, or combinations thereof, is not limited here. Therefore, the corresponding communication component 105 may include: a Wi-Fi module, a Bluetooth module, an NFC module, etc.
[0116] In an exemplary embodiment, the electronic device 100 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field-programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components, and is configured to perform the processing method for the intermediate representation of a model for a multi-core processor described above.
[0117] In another exemplary embodiment, a computer-readable storage medium including program instructions is also provided, which, when executed by a processor, implement the steps of the above-described processing method for intermediate model representation of a multi-core processor. For example, the computer-readable storage medium may be the memory 102 including program instructions, which may be executed by the processor 101 of the electronic device 100 to complete the above-described processing method for intermediate model representation of a multi-core processor.
[0118] In another exemplary embodiment, a computer program product is also provided, the computer program product comprising a computer program executable by a programmable device, the computer program having a code portion configured, when executed by the programmable device, to perform the above-described processing method for a model intermediate representation for a multi-core processor.
[0119] The preferred embodiments of this disclosure have been described in detail above with reference to the accompanying drawings. However, this disclosure is not limited to the specific details of the above embodiments. Within the scope of the technical concept of this disclosure, various simple modifications can be made to the technical solutions of this disclosure, and these simple modifications all fall within the protection scope of this disclosure.
[0120] It should also be noted that the various specific technical features described in the above embodiments can be combined in any suitable manner without contradiction. To avoid unnecessary repetition, this disclosure will not describe the various possible combinations separately.
[0121] Furthermore, various different embodiments of this disclosure can be combined in any way, as long as they do not violate the spirit of this disclosure, they should also be regarded as the content disclosed in this disclosure.
Claims
1. A method for processing intermediate representations of models for multi-core processors, characterized in that, The method is applied to a compiler configured to compile a model into instruction code for a multi-core processor, the multi-core processor comprising N processor cores, where N is an integer greater than 1; the method includes: Obtain the intermediate representation of the model to be compiled, wherein the intermediate representation includes tensors; Based at least on the shape of the tensor in the intermediate representation, the fragment tensor type and the split dimension of the tensor in the intermediate representation are determined; the fragment tensor type is configured to indicate how the tensor is held on the N processor cores; the split dimension is configured to indicate the dimension along which the tensor is divided; Set the fragment tensor type and the segmentation dimension as the type information of the tensor in the intermediate representation; Based on the type information of the tensor in the intermediate representation, the storage allocation of the tensor on the N processor cores is determined.
2. The method according to claim 1, characterized in that, The fragmented tensor type includes at least one of the following: copy type, distributed type, and synchronous buffer type; The replication type indicates that each processor core holds all the data of the tensor; The distributed type indicates that the N processor cores divide the tensor along the partitioning dimension, each processor core holds a portion of the tensor data, and the multiple portions of data held by the N processor cores do not overlap. The synchronization buffer type indicates that the data of the tensor held by each processor core needs to be converged across multiple processor cores.
3. The method according to claim 1 or 2, characterized in that, The tensors in the intermediate representation include an input tensor and tensors generated by operators. The input tensor is the tensor in the intermediate representation that corresponds to the input of the model to be compiled. Determining the fragment tensor type and splitting dimension of the tensors in the intermediate representation includes: Based on the shape of the entry tensor itself, determine the fragment tensor type and segmentation dimension of the entry tensor; Based on the determined fragment tensor type and splitting dimension of the entry tensor, the connection relationship between the entry tensor and the operator in the intermediate representation, and the category to which the operator belongs, the fragment tensor type and splitting dimension of the tensor generated by the operator are determined.
4. The method according to claim 3, characterized in that, The step of determining the fragment tensor type and splitting dimension of the tensors in the intermediate representation further includes: In the event of a split dimension conflict in the intermediate representation, the tensor involved in the split dimension conflict is determined to be of the synchronization buffer type; the split dimension conflict includes: the split dimensions of multiple tensors consumed by the same operator are inconsistent with each other, or the split dimension of the tensor is inconsistent with the split dimension required by the operator consuming the tensor.
5. The method according to claim 4, characterized in that, The method further includes: In the intermediate representation, cross-core synchronization instructions are set for the tensors involved in the split dimension conflict. The cross-core synchronization instructions are configured to merge the data of the tensors in the N processor cores into complete data.
6. The method according to claim 3, characterized in that, The step of determining the fragment tensor type and segmentation dimension of the entry tensor based on its own shape includes: Determine the number of dimensions in the entry tensor that are not equal to 1; When the number is one, the fragmented tensor type of the entry tensor is determined to be a copy type, which means that each processor core holds all the data of the tensor; When there are multiple such quantities, the fragmented tensor type of the entry tensor is determined to be a distributed type, and the partitioning dimension is determined to be the highest dimension among the multiple dimensions. The distributed type means that the N processor cores divide the tensor along the partitioning dimension, each processor core holds a portion of the tensor data, and the multiple portions of data held by the N processor cores do not overlap.
7. The method according to claim 3, characterized in that, The step of determining the fragment tensor type and partitioning dimension of the tensor generated by the operator based on the determined fragment tensor type and partitioning dimension of the input tensor, the connection relationship between the input tensor and the operator in the intermediate representation, and the category to which the operator belongs, includes: Ready operators are retrieved one by one using a worksheet scheduling method. The ready operators are those whose piecewise tensor type and splitting dimension have been determined for all input operands. The splitting dimension of the output tensor of the ready operator is determined according to the splitting dimension transfer rule corresponding to the category to which the ready operator belongs.
8. The method according to claim 7, characterized in that, The rules for passing the segmentation dimension include: For operators belonging to the computational type, the splitting dimension of the output tensor is taken as the dimension along which the parallel computation of the computational type operator proceeds; For operators belonging to the inherited type, the partitioning dimension of the output tensor inherits the partitioning dimension of the first operand of the inherited type operator, where the first operand is the input operand that is sorted first among the multiple input operands of the inherited type operator. For operators belonging to the dimension transformation category, the segmentation dimension of the output tensor is obtained by mapping the segmentation dimension of the input tensor according to the dimension transformation relationship of the dimension transformation category operator.
9. The method according to claim 8, characterized in that, The computational operators include matrix multiplication operators and / or convolution operators; the partitioning dimension of the output tensor of the matrix multiplication operator is the innermost dimension of the operands of the matrix multiplication operator; the partitioning dimension of the output tensor of the convolution operator is the batch dimension of the convolution operator; The dimension transformation operators include transpose operators, shape rearrangement operators, and / or slicing operators; the slicing dimension of the output tensor of the transpose operator is determined based on the slicing dimension of the input tensor of the transpose operator and the dimensions exchanged by the transpose operator; the slicing dimension of the output tensor of the shape rearrangement operator is the dimension obtained by subtracting one from the shrunk dimension and adding one to the expanded dimension of the input tensor of the shape rearrangement operator; the slicing dimension of the output tensor of the slicing operator is the innermost dimension.
10. The method according to claim 1 or 2, characterized in that, The value range of the tensor's partitioning dimension is 0 to the tensor's dimension minus one; determining the storage allocation of the tensor on the N processor cores based on the tensor's type information in the intermediate representation includes: If the tensor fragmentation tensor type is a copy type, then each of the N processor cores is allocated storage space configured to store all the data of the tensor; If the tensor's fragmented tensor type is distributed, then the tensor is divided into partial data corresponding to each processor core according to the partitioning dimension, and storage space is allocated to each processor core to store the partial data corresponding to that processor core according to the size of the partial data corresponding to each processor core. If the tensor fragmentation tensor type is a synchronous buffer type, each processor core reserves buffer space for the tensor in its private storage. The tensor is divided into parts of data corresponding to each processor core according to the partitioning dimension. Each processor core is responsible for calculating the part of the tensor corresponding to its own processor core to obtain the calculation result. The calculation result is written into the buffer space of the corresponding processor core, and cross-core synchronous instructions are used to ensure that each processor core holds a complete copy of the tensor.
11. The method according to claim 10, characterized in that, The tensor is divided into portions of data corresponding to each processor core according to the segmentation dimension, including: When the size of the tensor in the partition dimension is not divisible by N, the size of the portion held by the first part of the N processor cores is equal to the size obtained by rounding up the ratio of the size in the partition dimension to N. The size of the portion held by the second part of the N processor cores (excluding the first part of the processor cores) is equal to the size obtained by rounding down the ratio of the size in the partition dimension to N. The number of the first part of the processor cores is equal to the remainder obtained by dividing the size in the partition dimension by N.
12. A processing apparatus for intermediate representation of a model for a multi-core processor, characterized in that, An apparatus applied to a compiler configured to compile a model into instruction code for a multi-core processor, the multi-core processor comprising N processor cores, where N is an integer greater than 1; the apparatus comprises: The acquisition module is configured to acquire an intermediate representation of the model to be compiled, the intermediate representation including tensors; The determination module is configured to determine, at least based on the shape of the tensor in the intermediate representation, the fragment tensor type and the partitioning dimension of the tensor in the intermediate representation; the fragment tensor type is configured to indicate how the tensor is held on the N processor cores; the partitioning dimension is configured to indicate the dimension along which the tensor is partitioned; The setting module is configured to set the fragment tensor type and the splitting dimension as the type information of the tensor in the intermediate representation; The storage allocation module is configured to determine the storage allocation of the tensor on the N processor cores based on the type information of the tensor in the intermediate representation.
13. An electronic device, characterized in that, include: processor; as well as A memory storing a computer program or readable instructions, which, when executed by the processor, implement the method as described in any one of claims 1-11.
