Tensor processing method and device, electronic equipment, storage medium and program product

By introducing the first dimension conversion operator and the second dimension conversion operator into MHA, the problem of batch dimension order confusion when the length of the tensor sequence exceeds the limit is solved, ensuring the consistency and accuracy of data processing, and is suitable for processing ultra-large Long sequence data.

CN118674072APending Publication Date: 2024-09-20SHANGHAI BIREN TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410981048.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-19
Publication Date
2024-09-20

AI Technical Summary

Technical Problem

In deep learning, especially in NLP and image recognition tasks, when the sequence length of the tensor in the MHA mechanism exceeds the tensor dimension limit, direct dimension folding will cause the order of the data in the batch dimension to be disordered, affecting the performance and performance of the model. accuracy.

Method used

The first dimension conversion operator is introduced to fold the target sub-dimension in the original tensor into the first dimension. By rearranging the batch dimensions of the tensor, order confusion is avoided, and the output tensor is restored through the second dimension conversion operator. Ensure consistency and accuracy of data processing.

Benefits of technology

It effectively avoids errors and losses caused by confusing data dimensions, ensures the training effect of the model and the accuracy of the output, and is suitable for processing ultra-long sequence data.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118674072A
    Figure CN118674072A
Patent Text Reader

Abstract

The invention relates to the technical field of artificial intelligence, and provides a tensor processing method and device, electronic equipment, a storage medium and a program product, and the method comprises the steps: obtaining an original tensor; folding a to-be-folded target sub-dimension in the original tensor to a first dimension based on a first dimension conversion operator to obtain a target tensor; wherein the first dimension of the target tensor comprises an original first dimension, a target sub-dimension and a first sub-dimension, the target sub-dimension is located between the original first dimension and the first sub-dimension, the original first dimension is the first dimension of the original tensor, and the first sub-dimension is obtained by splitting a second dimension of the original tensor; the target sub-dimension is obtained by splitting the third dimension of the original tensor. According to the invention, the first dimension conversion operator is introduced, so that the problem that the first dimension of the folded tensor is disordered in order can be avoided.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a tensor processing method, device, electronic device, storage medium and program product. Background Art

[0002] In the field of deep learning, especially in tasks such as NLP (Natural Language Processing) and image recognition, MHA (Multi-Head Attention) has become a very important technology. MHA processes multiple attention "heads" in parallel, allowing the model to learn information in different representation subspaces, thereby enhancing the model's expressiveness and generalization capabilities. MHA is particularly widely used in model reasoning and training scenarios, especially in Transformer models.

[0003] In the implementation of MHA, the Q (Query), K (Key), and V (Value) tensors are usually divided into multiple heads, and each head performs self-attention calculations independently. In order to improve computational efficiency and memory utilization, the head_num (number of heads) dimension of the Q, K, and V tensors is usually automatically folded to the batch dimension. However, when the sequence length of the Q, K, and V tensors exceeds the tensor dimension limit, if the dimension is folded directly, the order of the data in the batch dimension will be confused, causing the model to produce prediction results different from the original order when processing the data, affecting the performance and accuracy of the model. Summary of the invention

[0004] The present invention provides a tensor processing method, device, electronic device, storage medium and program product, which are used to solve the problem of batch dimension order disorder caused by dimension folding of tensors when the sequence length of the tensor exceeds the tensor dimension limit in the related art.

[0005] The present invention provides a tensor processing method, comprising: Get the original tensor; Based on the first dimension conversion operator, fold the target sub-dimension to be folded in the original tensor onto the first dimension to obtain a target tensor; Among them, the first dimension of the target tensor includes the original first dimension, the target sub-dimension and the first sub-dimension, the target sub-dimension is located between the original first dimension and the first sub-dimension, the original first dimension is the first dimension of the original tensor, the first sub-dimension is obtained based on the splitting of the second dimension of the original tensor, and the target sub-dimension is obtained based on the splitting of the third dimension of the original tensor.

[0006] According to a tensor processing method provided by the present invention, based on the first dimension conversion operator, the target sub-dimension to be folded in the original tensor is folded onto the first dimension to obtain the target tensor, including: Splitting the second dimension of the original tensor into the first sub-dimension and the second sub-dimension, and folding the first sub-dimension onto the first dimension of the original tensor to obtain a first shape of the reshaped tensor, where the first shape is determined based on the size of each dimension of the original tensor after the dimension folding; Splitting the third dimension of the reshaped tensor into the target sub-dimension and the third sub-dimension, and folding the target sub-dimension onto the first dimension of the reshaped tensor to obtain a second shape of the target tensor, wherein the second shape is determined based on the size of each dimension of the reshaped tensor after the dimension folding; Based on the first dimensional conversion operator, the first shape and the second shape are applied to map the index of each data in the reshaped tensor to the second shape, so as to rearrange the position of each data in the reshaped tensor to obtain the target tensor.

[0007] According to a tensor processing method provided by the present invention, the applying the first shape and the second shape, mapping the index of each data in the reshaped tensor to the second shape, so as to rearrange the position of each data in the reshaped tensor to obtain the target tensor, includes: Based on the first shape, determining an index range of data in the reshaped tensor; Traversing the index range, and determining a corresponding index of the index of any data under the second shape based on the index of any data traversed, the first shape, and the second shape; Based on the corresponding index of the index of each data in the reshaped tensor in the second shape, each data in the reshaped tensor is mapped to the corresponding position of the target tensor to obtain the target tensor.

[0008] According to a tensor processing method provided by the present invention, the index of any data includes a first dimension index, a second dimension index, and a third dimension index, and the determining, based on the traversed index of any data, the first shape, and the second shape, a corresponding index of the index of any data in the second shape includes: Determine an original first dimension index and a first sub-dimension index of any data based on the first dimension index of any data and the size of the first sub-dimension in the first shape; Determine a target sub-dimension index and a third sub-dimension index of any data based on the third dimension index of any data and the size of the third dimension in the second shape; Determine a corresponding index of the first dimension index of any data in the second shape based on the original first dimension index, the target sub-dimension index and the first sub-dimension index of any data, the size of the first sub-dimension in the first shape and the size of the target sub-dimension in the second shape; Based on the second dimensional index of the any data, determining a corresponding index of the second dimensional index in the second shape; Based on the third sub-dimension index of the any data, a corresponding index of the third dimension index of the any data in the second shape is determined.

[0009] A tensor processing method provided by the present invention further includes: Based on the multi-head attention operator, the target tensor is calculated to obtain an initial output tensor; Based on the second dimension conversion operator, the target sub-dimension in the initial output tensor is restored to obtain a target output tensor.

[0010] A tensor processing method provided by the present invention further includes: The multi-head attention operator and the second dimension conversion operator are fused to obtain a fusion operator; Based on the fusion operator, the tensor is applied to perform calculation to obtain an initial output tensor, and the target sub-dimension in the initial output tensor is restored to obtain a target output tensor.

[0011] According to a tensor processing method provided by the present invention, the target sub-dimension in the initial output tensor is restored to obtain a target output tensor, including: Based on the shape of the initial output tensor and the shape of the target output tensor, the index of each data in the initial output tensor is mapped to the shape of the target output tensor to restore the target sub-dimension in the initial output tensor from the first dimension to the third dimension to obtain the target output tensor.

[0012] The present invention also provides a tensor processing device, comprising: The acquisition unit is used to obtain the original tensor; A conversion unit, configured to fold the target sub-dimension to be folded in the original tensor onto the first dimension based on the first dimension conversion operator to obtain a target tensor; Among them, the first dimension of the target tensor includes the original first dimension, the target sub-dimension and the first sub-dimension, the target sub-dimension is located between the original first dimension and the first sub-dimension, the original first dimension is the first dimension of the original tensor, the first sub-dimension is obtained based on the splitting of the second dimension of the original tensor, and the target sub-dimension is obtained based on the splitting of the third dimension of the original tensor.

[0013] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, any of the tensor processing methods described above is implemented.

[0014] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements any of the tensor processing methods described above.

[0015] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the tensor processing method described above is implemented.

[0016] The tensor processing method, device, electronic device, storage medium and program product provided by the present invention can fold the target sub-dimension in the original tensor onto the first dimension to obtain the target tensor by introducing the first dimension conversion operator. At the same time, the position of the target sub-dimension after folding in the target tensor can be ensured, that is, it is located between the original first dimension and the first sub-dimension, thereby realizing the rearrangement of the first dimension of the tensor and avoiding confusion in the order of the first dimension after folding, thereby helping to ensure the consistency and accuracy of data processing and reduce errors and losses caused by confusion in data dimensions. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] In order to more clearly illustrate the technical solutions in the present invention or related technologies, the following briefly introduces the drawings required for use in the embodiments or related technical descriptions. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0018] Figure 1 It is a flowchart of the tensor processing method provided by the present invention; Figure 2 It is a schematic diagram of the principle of the folding process of the first dimension conversion operator provided by the present invention; Figure 3 It is a schematic diagram of the fusion of the dimension conversion operator and the MMA operator provided by the present invention; Figure 4 It is a schematic diagram of the principle of the second dimension conversion operator restoration process provided by the present invention; Figure 5 It is a schematic diagram of processing Q, K, and V tensors provided by the present invention; Figure 6 is a structural schematic diagram of a tensor processing device provided by the present invention; Figure 7It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0019] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0020] MHA is an extended form of attention mechanism widely used in Transformer models. It runs multiple independent attention mechanisms in parallel to obtain the attention distribution of different subspaces of the input sequence, thereby more comprehensively capturing the potential multiple semantic associations in the sequence.

[0021] In the implementation of MHA, the input sequence (such as text sequence data or time series data) must first be converted into an embedding vector through an embedding layer. Subsequently, these embedding vectors are converted into Q, K, and V tensors through a linear transformation (i.e., through multiplication of the weight matrix). These tensors are usually three-dimensional, that is, the shape of the tensor is [batch_size, sequence_length, embedding_dim]. It should be understood that all of the above embedding vectors can be represented in the form of a two-dimensional array, where each row in the two-dimensional array represents an embedding vector for a time step, and each column represents a dimension in the embedding vector. Here, in sequence data processing, the data is arranged in sequence, and each such sequentially arranged element is called a time step. For example, for text sequence data, each time step represents a word or character; in time series data, each time step represents an observation at a point in time. When processing batch data (i.e., processing multiple sequences at the same time), the dimension of the embedding vector will increase by one dimension (i.e., batch dimension) to accommodate different sequences. Therefore, the shape of the embedding vector becomes [batch_size, sequence_length, embedding_dim], where batch_size (i.e., batch dimension) is the number of sequences in the batch, sequence_length is the length of each sequence, i.e., the number of time steps in the sequence, and embedding_dim is the length of the embedding vector for each time step. In MHA, these embedding vectors are converted to Q, K, and V tensors through linear transformations. Linear transformations do not change the dimensions of the tensors, but only linearly combine the embedding vectors through different weight matrices. Therefore, the Q, K, and V tensors also inherit the three-dimensional structure of the embedding vector, i.e., [batch_size, sequence_length, embedding_dim].

[0022] The core idea of ​​MHA is to divide the input Q, K, V tensors into multiple heads, each of which performs self-attention calculations independently, which allows the model to focus on different information at different locations, thereby improving the model's representation ability. In order to efficiently implement the calculation of the multi-head attention mechanism, the embedding_dim dimension is usually split into the head_num dimension and the dim_per_head dimension, and the head_num dimension is folded onto the batch dimension (that is, the head_num dimension is merged with the batch dimension) to form a larger batch dimension, so that the batch optimization in the deep learning framework can be used to increase the calculation speed. Here, dimension folding refers to a technique when processing multi-dimensional data (such as tensors), which involves merging one or more dimensions (or sub-dimensions) of multi-dimensional data into other dimensions to improve computational efficiency and optimize memory usage. It should be understood that the embedding_dim dimension is split into two dimensions: head_num (number of heads) and dim_per_head (size of each head). The head_num dimension determines the number of attention "heads" processed in parallel in the multi-head attention mechanism; the dim_per_head dimension indicates the size of each Q, K, V tensor processed by each attention head in the embedding_dim dimension. For example, embedding_dim = 320, head_num = 5, then dim_per_head = 320 / / 5 = 64 (where " / / " indicates integer division), indicating that in the multi-head attention mechanism, there will be 5 parallel attention heads, each of which will process a query, key, and value tensor with 64 element features in the embedding_dim dimension.

[0023] When MHA processes the output sequence data, if the input sequence is too long and exceeds the maximum sequence length that the model can handle, the tensor dimension will exceed the limit. In this case, the sequence_length dimension of the tensor needs to be split and folded into the batch dimension. However, since the length of the input sequence exceeds the tensor dimension limit, direct dimension folding will cause the order of the data on the batch dimension to become chaotic. For example, suppose there is a tensor with a shape of (4,9216, 320), which represents a batch with 4 samples, each sample has 9216 sequence elements (that is, the sequence length is 9216), and each element has 320 features. For this tensor, the expected correct splitting logic is: first split the embedding_dim dimension (that is, 320=5×64), and fold the split head_num dimension into the batch dimension, and the resulting tensor shape is (20, 9216, 64); because the sequence length 9216 of the tensor exceeds the tensor dimension limit, the dimension needs to be split (that is, 9216=9×1024) and then folded into the batch dimension, and the resulting tensor shape is (20×9, 1024,64). At this time, the order of the batch dimensions is 4, 5, 9.

[0024] However, when the sequence length of the tensor exceeds the tensor dimension limit, the related technology cannot correctly perform the above splitting. The actual splitting logic is to first split the sequence_length dimension and fold it into the batch dimension, and then fold the head_num dimension into the batch dimension. The resulting tensor shape is (36×5, 1024, 64). At this time, the order of the batch dimensions is 4, 9, 5. Obviously, the order of the batch dimensions is messed up. This will cause serious problems in practical applications. Because the order of samples in the batch has an important impact on the model training results, if the order is disrupted, the training effect of the model will be greatly reduced, and it may even fail to converge.

[0025] In this regard, the present invention provides a tensor processing method, which introduces a first dimension conversion operator to rearrange the batch dimension of the tensor when the sequence length of the input sequence exceeds the tensor dimension limit, so as to avoid the problem of disordered batch dimension order and meet the input and output requirements of MHA, thereby overcoming the above defects.

[0026] Figure 1 It is a flowchart of the tensor processing method provided by the present invention, such as Figure 1 As shown, the method includes: Step 110, obtaining the original tensor; It should be noted that the original tensor refers to a multidimensional array form into which the input sequence is converted. Before obtaining the original tensor, the input sequence can be obtained first. Here, the input sequence refers to a series of data points or elements, which are arranged in a certain order. These data points or elements can be words, characters or symbols in text sequence data, or time point records in time series data, or pixel values ​​in image processing. In fields such as deep learning and natural language processing, the input sequence can be the initial data for model training, which can be text sequence data or time series data.

[0027] Specifically, in NLP, the input text or time series data is first converted into embedded vectors through an embedding layer. Subsequently, in MHA, these embedded vectors are converted into tensors through linear transformation (i.e., multiplication of the weight matrix), that is, the original tensors are obtained. The original tensors here can be Q, K, V tensors. The purpose of converting the input sequence into a tensor is to enable the data to be processed by the deep learning model.

[0028] Step 120, based on the first dimension conversion operator, fold the target sub-dimension to be folded in the original tensor onto the first dimension to obtain a target tensor; Among them, the first dimension of the target tensor includes the original first dimension, the target sub-dimension and the first sub-dimension, the target sub-dimension is located between the original first dimension and the first sub-dimension, the original first dimension is the first dimension of the original tensor, the first sub-dimension is obtained based on the splitting of the second dimension of the original tensor, and the target sub-dimension is obtained based on the splitting of the third dimension of the original tensor.

[0029] Specifically, when the sequence length of the input sequence exceeds the preset length threshold, it indicates that the length of the input sequence is too long and exceeds the maximum sequence length that the model can handle. In this case, the first dimension conversion operator can be introduced to perform dimension folding processing on the original tensor to reduce the size of the tensor processed each time, and at the same time, the first dimension of the original tensor is rearranged to avoid the problem of disordered order in the first dimension. Here, the preset length threshold refers to a pre-set value used to determine whether the length of the input sequence (or the converted original tensor) is too long. If the length of the input sequence exceeds the threshold, some special processing measures need to be taken, such as folding certain dimensions of the tensor to reduce memory consumption during processing or improve processing efficiency. Here, the preset length threshold can be set according to the maximum sequence length that the model can handle.

[0030] The first dimension conversion operator is an operation or function that is used to fold a sub-dimension of a tensor (such as the head_num dimension) to the first dimension of the tensor (such as the batch dimension), thereby changing the shape of the tensor and avoiding the problem of disordered dimensional order of the tensor.

[0031] It can be understood that the shape of the original tensor includes the first dimension, the second dimension and the third dimension. Here, the shape of the original tensor refers to the size and arrangement of its dimensions. In the shape of the tensor, the first dimension has a specific meaning. For example, in sequence data, it can represent the batch dimension of the sequence (i.e., batch dimension); the second dimension also has a specific meaning. For example, in image processing, it can represent the height dimension of the image, and in sequence data, it can represent the sequence length or time step; the third dimension depends on the specific nature of the data. For example, in image processing, it can represent the width dimension of the image, and in sequence processing, it can represent the feature dimension in the sequence.

[0032] For example, the shape of the original tensor can be expressed as (N, H_large, W), where the first dimension N is the batch dimension, which represents the number of sequences in one processing (or one training iteration). The second dimension H_large is the original height dimension, which represents the length of the sequence or the number of time steps in the sequence. The third dimension W is the original width dimension, which represents the number of features per time step or element in sequence processing.

[0033] When the sequence length of the input sequence exceeds the preset length threshold, that is, the size of the second dimension H_large exceeds the preset length threshold, the second dimension of the original tensor can be split to obtain the first sub-dimension and the second sub-dimension. Here, the purpose of splitting the second dimension is to split the overly long sequence into shorter sub-sequences so that they can be input into the model in batches for processing. The first sub-dimension and the second sub-dimension are the dimensions obtained by splitting the second dimension of the original tensor. For example, the H_large dimension can be split into the sub_H dimension and the H_fold dimension, that is, H_large = sub_H ×H_fold, where sub_H is the first sub-dimension and H_fold is the second sub-dimension.

[0034] In addition, in the multi-head attention mechanism, in order to realize the multi-head attention calculation, the dimension of the embedding vector (that is, the third dimension of the original tensor) can be split into multiple heads, each head corresponds to a sub-target dimension, and the third sub-dimension is another sub-dimension obtained after splitting the third dimension of the original tensor in addition to the target sub-dimension. Here, by splitting the third dimension of the original tensor into multiple heads, the model can be allowed to learn different aspects of the input data in parallel in different representation subspaces. This parallelism not only improves computational efficiency, but also enhances the representation and generalization capabilities of the model. By combining outputs from different heads, the model can capture richer contextual information and better handle complex language tasks. Exemplarily, for an original tensor of shape (N, H_large, W), the W dimension can be split into head_num dimension and head_size dimension, that is, W = head_num × head_size, where head_num is the target sub-dimension and head_size is the third sub-dimension, which is used to represent the size of each head.

[0035] In the case where the sequence length of the input sequence exceeds the preset length threshold, in order to avoid the problem of disordered order of the batch dimension when the tensor is dimensionally folded, the embodiment of the present invention introduces a first dimension conversion operator. Through this operator, the target sub-dimension to be folded in the original tensor can be folded onto the first dimension, and the first dimension (i.e., the batch dimension) of the tensor can be rearranged and combined, thereby avoiding disordered order of the batch dimension. Here, it can be specifically achieved by reshaping and permute operations on the original tensor through the first dimension conversion operator. Specifically, the second and third dimensions of the original tensor can be split first, and according to the splitting results, the first dimension conversion operator is applied to rearrange the dimensions in the original tensor, and the tensor is reshaped according to the new dimension order to obtain the target tensor. It should be understood that when the sequence length of the input sequence does not exceed the preset length threshold, the H_large dimension can be split into the sub_H dimension (where the size of the sub_H dimension is 1) and the H_fold dimension, so that the original tensor can still be dimensionally folded through the first dimension conversion operator.

[0036] Figure 2 It is a schematic diagram of the principle of the folding process of the first dimension conversion operator provided by the present invention, such as Figure 2 As shown, using the first dimension conversion operator (i.e. Figure 2The principle steps of dimension folding of the original tensor using the stm1 operator shown in ) are as follows: First, for the original tensor with shape (N, H_large, W), it can be reshaped into (N, sub_H, H_fold, head_size, head_num), where H_large = sub_H × H_fold, W = head_size × head_num; then, the (N, sub_H, H_fold, head_size, head_num) in the previous step is rearranged into (N, head_num, sub_H, H_fold, head_size). It should be understood that, if Figure 2 As shown in the figure, before the tensor is processed by the stm1 operator, the sequence length of the tensor (i.e., the size of H_large) exceeds the tensor dimension limit, and the shape of the tensor obtained after splitting and folding is (N×sub_H×head_num, H_fold, head_size), which is equivalent to (N, sub_H, head_num, H_fold, head_size). Obviously, the order is disordered in the batch dimension. Therefore, in the specific implementation, the stm1 operator can be used to perform index calculation on the data of the third dimension (i.e., W dimension) of the original tensor to obtain the index coordinates of each data on the target sub-dimension (i.e., head_num dimension), and through coordinate mapping, the data on the target sub-dimension is mapped to the first dimension of the original tensor, and the data on the target sub-dimension is located between the data of the original first dimension (i.e., N dimension) and the data of the first sub-dimension (i.e., sub_H dimension), and the output target tensor data is obtained and stored, thereby completing the folding process.

[0037] Here, the target tensor refers to the tensor obtained by processing the original tensor by applying the first dimension conversion operator, which retains all the data of the original tensor, but the order of the dimensions has changed. In an embodiment of the present invention, the first dimension of the target tensor includes the first dimension in the original tensor (i.e., the original first dimension), the target sub-dimension, and the data of the first sub-dimension.

[0038] The method provided by the embodiment of the present invention can fold the target sub-dimension in the original tensor onto the first dimension to obtain the target tensor by introducing the first dimension conversion operator. At the same time, it can ensure the position of the target sub-dimension in the target tensor after folding, that is, between the original first dimension and the first sub-dimension, thereby realizing the rearrangement of the first dimension of the tensor and avoiding confusion in the order of the first dimension after folding, thereby helping to ensure the consistency and accuracy of data processing and reduce errors and losses caused by confusion in data dimensions.

[0039] Based on the above embodiment, step 120 specifically includes: Step 121, splitting the second dimension of the original tensor into the first sub-dimension and the second sub-dimension, and folding the first sub-dimension onto the first dimension of the original tensor to obtain a first shape of the reshaped tensor, wherein the first shape is determined based on the size of each dimension of the original tensor after the dimension folding; Specifically, when the sequence length of the input sequence (i.e., the second dimension of the original tensor) exceeds the preset length threshold, the second dimension of the original tensor can be split into a first sub-dimension and a second sub-dimension, and the first sub-dimension can be folded onto the first dimension of the original tensor to obtain the first shape of the reshaped tensor, thereby achieving the division of the overly long input sequence into shorter sub-sequences so that they can be input into the model in batches for processing. Here, reshaping a tensor refers to the process of changing the shape of a tensor (i.e., the size and order of the dimensions) without changing its data itself. The reshaped tensor (i.e., the reshaped tensor) has different dimensional sizes but contains the same data. It should be understood that the reshape operation generally does not change the number of dimensions, but only the size of each dimension.

[0040] The first shape of the reshaped tensor mentioned above is the size and order of the dimensions of the reshaped tensor, which is determined according to the size of each dimension of the tensor after dimension folding. Here, the size of each dimension of the tensor after dimension folding refers to the length or size of the original tensor in each dimension after dimension folding. For example, for the original tensor with shape (N, H_large, W), the second dimension (i.e., H_large dimension) is first split into the first sub-dimension (i.e., sub_H dimension) and the second sub-dimension (i.e., H_fold dimension), and then the first sub-dimension is folded onto the first dimension to form a new shape (N×sub_H, H_fold, W). The shape (N×sub_H, H_fold, W) here is the first shape of the reshaped tensor.

[0041] Step 122, splitting the third dimension of the reshaped tensor into the target sub-dimension and the third sub-dimension, and folding the target sub-dimension onto the first dimension of the reshaped tensor to obtain a second shape of the target tensor, wherein the second shape is determined based on the size of each dimension of the reshaped tensor after the dimension folding; Specifically, in order to implement multi-head attention calculation, the third dimension of the reshaped tensor can be split into multiple heads, that is, the target sub-dimension and the third sub-dimension are obtained, and the target dimension is folded onto the first dimension of the reshaped tensor, further changing the shape of the tensor, thereby obtaining the second shape of the target tensor. It should be understood that the target tensor also contains the same data as the original tensor, but has different dimensional sizes.

[0042] The second shape of the target tensor is the size and order of the tensor dimensions after the target sub-dimension is folded to the first dimension, which is determined according to the size of each dimension of the reshaped tensor after the dimension folding. The size of each dimension of the reshaped tensor here refers to the length or size of the reshaped tensor in each dimension after the dimension folding. For example, the first shape of the reshaped tensor is (N×sub_H, H_fold, W), first split the third dimension (i.e., W dimension) into the target sub-dimension (i.e., head_num dimension) and the third sub-dimension (i.e., head_size dimension), and then fold the target sub-dimension to the first dimension to form a new shape (N×head_num×sub_H, H_fold, head_size), where (N×head_num×sub_H, H_fold, head_size) is the second shape of the target tensor.

[0043] Step 123, based on the first dimensional conversion operator, apply the first shape and the second shape, map the index of each data in the reshaped tensor to the second shape, so as to rearrange the position of each data in the reshaped tensor to obtain the target tensor.

[0044] It should be noted that, when the sequence length of the input sequence exceeds the preset length threshold, directly performing dimension splitting and folding processing on the original tensor will cause the data of the output target tensor to be out of order in the first dimension (i.e., batch dimension). Therefore, the embodiment of the present invention maps the index of each data in the reshaped tensor by applying the first shape determined in step 121 and the second shape determined in step 122 based on the first dimension conversion operator, so as to rearrange the order of the data in the first dimension of the reshaped tensor in the process of folding the target sub-dimension into the first dimension, thereby solving the problem of data order disorder in the first dimension.

[0045] Specifically, the data in the reshaped tensor refers to the specific values ​​or elements stored in the tensor, which are arranged in memory according to a specific dimensional order and size. Reshaping the tensor itself does not change the values ​​of these data, but only changes their arrangement or dimensional structure in memory. The index of each data in the reshaped tensor (or index coordinate, position index) refers to the identifier used to locate or access the location of a specific data in the tensor. In a multidimensional tensor, each data element can be uniquely identified by a set of indices that correspond to the position of the data in each dimension. For example, in a tensor of shape (N, H_large, W), the index of a data element can be represented as (i, j, k), where i is the index on the N dimension (i.e., batch dimension), j is the index on the H_large dimension (i.e., sequence length dimension), and k is the index on the W dimension (i.e., feature dimension).

[0046] It can be understood that mapping the index of each data in the reshaped tensor to the shape of the target tensor (i.e., the second shape) means recalculating the index position of each data element in the target tensor according to the relationship between the shape of the reshaped tensor and the shape of the target tensor. Since the reshaped tensor and the target tensor have different dimensional sizes and orders, it is necessary to convert the original index (i.e., the index of each data in the reshaped tensor) into a new index that adapts to the new shape (i.e., the index that adapts to the shape of the target tensor), so that each data element in the reshaped tensor can be mapped to the second shape according to the new index to obtain the target tensor.

[0047] The specific mapping process depends on the specific shapes of the reshaped tensor and the target tensor. For example, the first shape of the reshaped tensor is (N×sub_H, H_fold, W), and the second shape of the target tensor is (N×head_num×sub_H, H_fold,head_size). Assuming that the index of a data element of the reshaped tensor is (in_n, in_h, in_w), then it is necessary to find the corresponding index (out_n, out_h, out_w) of the index in the second shape (N×head_num×sub_H, H_fold, head_size) of the target tensor. Specifically, it can be achieved by the following steps: First, for each data element in the reshaped tensor, its original index is (in_n, in_h, in_w), and the original index can be mathematically transformed according to the shape of the target tensor, such as integer division, remainder, etc., to calculate the new index of the element in the target tensor; then, according to the calculated new index, the data elements in the reshaped tensor are copied to the corresponding positions of the target tensor, that is, the data are rearranged in memory so that they conform to the shape of the target tensor.

[0048] In the embodiment of the present invention, through index mapping, the position of data in memory can be rearranged by only changing its index without changing the data value, so as to meet specific computing or processing requirements. This is because the index itself represents the location information of the data in memory, and by changing the calculation method of the index, the order of data in memory can be controlled. In the deep learning framework, index mapping can be implemented through underlying array operations, which can efficiently process large amounts of data and change its logical structure without copying the data itself.

[0049] Based on any of the above embodiments, step 123 specifically includes: Step 1231, based on the first shape, determine the index range of the data in the reshaped tensor; Specifically, when dealing with tensors, especially in the field of deep learning, the index range of data refers to the boundaries of the data inside the tensor that can be accessed through the index. For a tensor with a specific shape, its index range is determined by the size of each dimension. For example, for a reshaped tensor with a shape of (N×sub_H, H_fold, W), assuming that the index of a data element of the reshaped tensor is (in_n, in_h, in_w), then the range of in_n is [0, N×sub_H-1], representing the index after the original batch dimension and the sub_H dimension are merged; the range of in_h is [0, H_fold-1], which remains unchanged during the target sub-dimension folding process; the range of in_w is [0, W-1], but in the target tensor shape, this dimension is split into head_num and head_size.

[0050] Step 1232, traversing the index range, and determining a corresponding index of the index of any data under the second shape based on the traversed index, the first shape, and the second shape; Step 1233: Map each data in the reshaped tensor to a corresponding position of the target tensor based on the corresponding index of each data in the reshaped tensor in the second shape to obtain the target tensor.

[0051] Specifically, according to the first shape of the reshaped tensor, after determining the index range of the data in the reshaped tensor, the index range can be traversed, and for each data in the traversed reshaped tensor, the corresponding index of the data in the target tensor shape can be calculated according to the index of the data in its current shape (i.e., the first shape), and combined with the first shape of the reshaped tensor and the second shape of the target tensor. Then, after calculating the corresponding index of each data in the reshaped tensor in the target tensor, each data in the reshaped tensor can be mapped to the corresponding position of the target tensor according to these corresponding indexes, so that the data in the reshaped tensor is rearranged or reshaped into the shape of the target tensor. It should be understood that the data in the reshaped tensor and the data in the target tensor are the same as the data in the original tensor, but the three have different dimensional sizes and the order of data arrangement is different.

[0052] Based on any of the above embodiments, the index of any data includes a first dimension index, a second dimension index, and a third dimension index. Accordingly, step 1232 specifically includes: Determine an original first dimension index and a first sub-dimension index of any data based on the first dimension index of any data and the size of the first sub-dimension in the first shape; Determine a target sub-dimension index and a third sub-dimension index of any data based on the third dimension index of any data and the size of the third dimension in the second shape; Determine a corresponding index of the first dimension index of any data in the second shape based on the original first dimension index, the target sub-dimension index and the first sub-dimension index of any data, the size of the first sub-dimension in the first shape and the size of the target sub-dimension in the second shape; Based on the second dimensional index of the any data, determining a corresponding index of the second dimensional index in the second shape; Based on the third sub-dimension index of the any data, a corresponding index of the third dimension index of the any data in the second shape is determined.

[0053] It should be noted that in a multidimensional tensor, each data element can be uniquely identified by its position (i.e., index) in each dimension. For example, for a three-dimensional tensor with a shape of (N, H, W), each data in the tensor can be specified by an index of three dimensions, that is, the index of each data includes a first dimension index, a second dimension index, and a third dimension index. Among them, the first dimension index is the position index in the N-dimensional direction, the second dimension index is the position index in the H-dimensional direction, and the third dimension index is the position index in the W dimension.

[0054] Specifically, for each data in the reshaped tensor, the corresponding index of the data in the target tensor can be obtained by applying the index of the data in each dimension and combining the first shape of the reshaped tensor and the second shape of the target tensor to perform index calculation.

[0055] Exemplarily, assuming that the first shape of the reshaped tensor is (N×sub_H, H_fold, W), and the second shape of the target tensor is (N×head_num×sub_H, H_fold, head_size), where W = head_size × head_num, in order to convert the reshaped tensor into the target tensor, it can be implemented by index calculation and mapping. Assuming that the index of any data in the reshaped tensor in (N×sub_H, H_fold, W) is (in_n, in_h, in_w), through calculation, the corresponding index (out_n, out_h, out_w) of the index in the target tensor shape (N×head_num×sub_H, H_fold, head_size) can be found.

[0056] Specifically, the first dimension index in_n and the size of the first sub-dimension in the first shape (i.e., sub_H) are first calculated to decompose the first dimension index in_n into the original first dimension index and the first sub-dimension index. Here, the original first dimension index is the index of the batch dimension in the original tensor, and the first sub-dimension index is the index of the sub_H dimension. The original first dimension index is represented as n, and the first sub-dimension index is represented as sub_h_idx, then n = in_n / / sub_H, sub_h_idx = in_n % sub_H. It should be understood that " / / " represents an integer division operation, and the result is an integer; "%" represents a remainder operation, and the result is an integer.

[0057] Subsequently, the third dimension index in_w and the size of the third dimension in the second shape (i.e., head_size) can be used for calculation, and the third dimension index can be decomposed into the target sub-dimension index and the third sub-dimension index. Here, the size of the third dimension in the second shape refers to the width dimension size of the target tensor after the head_num dimension is folded into the batch dimension; the target sub-dimension index is the index in the head_num dimension; and the third sub-dimension index is the index in the head_size dimension, which is the remaining part after the in_w split. The target sub-dimension index is represented as head_num_idx, and the third sub-dimension index is represented as head_size_idx, then head_num_idx = in_w / / head_size, head_size_idx = in_w %head_size.

[0058] Finally, by combining the original first dimension index, the target sub-dimension index, and the first sub-dimension index, a new batch index, out_n, can be obtained, i.e., the first dimension index in the target tensor. Here, in order to ensure that the correct batch dimension order can be obtained according to the index mapping, the target sub-dimension index (i.e., head_num_idx) can be inserted between the original first dimension index (i.e., the original batch dimension index n) and the first sub-dimension index (i.e., sub_h_idx), and the new batch index is: out_n = n × head_num × sub_H + head_num_idx × sub_H + sub_h_idx. In the process of dimension folding of the reshaped tensor, since the second dimension remains unchanged, the second dimension index of each data in the target tensor is: out_h = in_h. Since the third dimension of the target tensor is the third sub-dimension after the third dimension of the reshaped tensor is split, the third dimension index of the data in the target tensor is: out_w = head_size_idx. According to the calculated index (out_n, out_h, out_w), the data can be mapped to the corresponding position of the target vector.

[0059] Based on any of the above embodiments, Figure 3 is a schematic diagram of the fusion of the dimension conversion operator and the MMA operator provided by the present invention, such as Figure 3 As shown, considering that in MHA, the input sequence is first converted into embedded vectors through the embedding layer, and then these embedded vectors are converted into tensors through linear transformation (i.e., matrix multiplication). In order to reduce the impact of the newly added first-dimensional conversion operator on the performance of MHA, the first-dimensional conversion operator can be fused with the MMA (Matrix Multiply and Accumulate) operator. Here, fusion calculation refers to merging two or more consecutive and interrelated operators into a new, larger operator to reduce the number of memory reads and writes, reduce computational complexity, and improve execution efficiency. Operator fusion is an optimization technique that reduces computational and memory overhead by merging multiple operations or operators.

[0060] Specifically, if Figure 3 As shown in Figure 1, after the embedding vectors are processed by the MMA operator and converted into tensors, these tensors are stored in the memory module. Figure 3), if no fusion calculation is performed, the output of the original MMA operator needs to be stored in the memory module first, and then loaded from the memory module to the stm operator. After being processed by the stm operator, the result output by the stm operator needs to be written to the memory module. Such frequent memory access not only increases the memory access overhead, but also reduces the computing efficiency, which affects the performance of MHA. In this regard, the embodiment of the present invention fuses the stm operator and the MMA operator for calculation, so that the output of the MMA operator does not need to be written to the memory module, and the stm operator can directly apply the result output by the MMA operator for calculation. Finally, the output of the stm operator only needs to be written to the memory module. This can reduce the storage and reloading of intermediate results, thereby reducing the memory access overhead, improving the computing efficiency, and reducing the impact on the performance of MHA.

[0061] Based on any of the above embodiments, the method further includes: Step 130, calculating the target tensor based on a multi-head attention operator to obtain an initial output tensor; Step 140: Based on the second dimension conversion operator, restore the target sub-dimension in the initial output tensor to obtain a target output tensor.

[0062] It should be noted that after the original tensor is converted by the first dimension conversion operator to obtain the target tensor with the correct batch dimension order, the target tensor can be applied to perform multi-head attention calculation to obtain the initial output tensor. After the multi-head attention calculation, it is also necessary to restore the target sub-dimension in the initial output tensor to the last dimension. Therefore, the embodiment of the present invention implements the inverse conversion process by introducing the second dimension conversion operator, that is, restoring the target sub-dimension in the initial output tensor to obtain the final target output tensor.

[0063] Specifically, when calculating the target tensor based on the multi-head attention operator, the attention mechanism can be applied independently to each head in the target tensor, which includes calculating the linear transformation of Q, K, and V, and then using Q and K to calculate the attention score, and then using these scores to weighted sum V. Finally, the outputs of all heads are concatenated and fused through a linear transformation (such as a fully connected layer) to obtain the initial output tensor. Here, the initial output tensor is the result of the multi-head attention operator. It is calculated by the multi-head attention mechanism of the target tensor and contains the dependency information between the elements in the input sequence. It should be understood that by performing multi-head attention calculation on the target tensor, the shape of the initial output tensor obtained is the same as the shape of the target tensor, but contains different data.

[0064] After obtaining the initial output tensor, the tensor can be dimensionally restored through the second dimension conversion operator to obtain the final target output tensor. Here, the second dimension conversion operator is an operation or function used to restore the target sub-dimension in the tensor to its original position. This conversion is the inverse operation of the dimension folding in step 120 of the above embodiment, and is intended to restore the original structure or layout of the tensor for subsequent processing or to meet the requirements of a specific output format. It should be understood that the target output tensor refers to the output tensor after dimension restoration, which restores certain dimensional characteristics of the original input sequence for use in subsequent processing steps (such as classification, regression, decoding, etc.) or as the output of the final model.

[0065] Figure 4 is a schematic diagram of the principle of the second dimension conversion operator restoration process provided by the present invention, such as Figure 4 As shown, using the second dimension conversion operator (i.e. Figure 4 The principle steps of dimension reduction of the initial output tensor by using the stm2 operator shown in (shown in ) are as follows: Since the shape of the initial output tensor is the same as that of the target tensor, the shape of the initial output tensor can be expressed as (N×head_num×sub_H, H_fold, head_size), which is equivalent to (N, head_num, sub_H, H_fold, head_size). By rearranging the shape into (N, sub_H, H_fold, head_size×head_num), the dimension of head_num can be reduced to the last problem. In the specific implementation, the stm2 operator can be used to perform index calculation on the data of each dimension of the initial output tensor to obtain the corresponding index position of the data of each dimension in the initial output tensor in the target output tensor, so that the data in the initial output tensor can be mapped to the target output tensor through these corresponding index positions to restore the target sub-dimension of the initial output tensor to the last dimension.

[0066] Based on any of the above embodiments, the method further includes: Step 150, fusing the multi-head attention operator and the second dimension conversion operator to obtain a fusion operator; Step 160: Based on the fusion operator, apply the tensor to perform calculation to obtain an initial output tensor, and restore the target sub-dimension in the initial output tensor to obtain a target output tensor.

[0067] Specifically, in order to reduce the impact of the newly added second dimension conversion operator on the MHA performance, the second dimension conversion operator can be fused with the MHA operator (i.e., multi-head attention operator) for calculation. That is, the second dimension conversion operator and the MHA operator can be fused first, and after obtaining the fused operator, the fused operator can be applied to perform calculations and dimensionality restoration. Here, the process of fusing the second dimension conversion operator with the MHA operator is similar to the process of fusing the stm operator with the MMA operator in the above embodiment, and will not be repeated here.

[0068] Based on any of the above embodiments, in step 140 or step 160, restoring the target sub-dimension in the initial output tensor to obtain the target output tensor includes: Based on the shape of the initial output tensor and the shape of the target output tensor, the index of each data in the initial output tensor is mapped to the shape of the target output tensor to restore the target sub-dimension in the initial output tensor from the first dimension to the third dimension to obtain the target output tensor.

[0069] It should be noted that the shape of the initial output tensor is the same as the shape of the target tensor. The target tensor is obtained by folding the target sub-dimension of the reshaped tensor, and the target output tensor is obtained by restoring the target sub-dimension of the initial output tensor. Therefore, the shape of the target output tensor is the same as the shape of the reshaped tensor. After determining the shape of the initial output tensor and the shape of the target tensor, the index of each data in the initial output tensor can be mapped to the shape of the target output tensor according to these shapes, thereby restoring the target sub-dimension in the initial output tensor from the first dimension to the third dimension (i.e., the last dimension).

[0070] Specifically, according to the shape of the initial output tensor, the index range of the data in the tensor can be determined. For example, for an initial output tensor with a shape of (N×head_num×sub_H, H_fold, head_size), assuming that the index of a data element of the tensor is (in_n, in_h, in_w), the range of in_n is [0, N×head_num×sub_H-1], which represents the index after the original batch dimension and the head_num dimension and sub_H dimension are combined; the range of in_h is [0, H_fold-1], which remains unchanged during the target sub-dimension restoration process; the range of in_w is [0, head_size -1].

[0071] Subsequently, the above index range can be traversed, and for each data in the traversed initial output tensor, the corresponding index of the data in the target output tensor can be calculated according to the index of the data in its current shape, combined with the shape of the initial output tensor and the shape of the target output tensor. After calculating the corresponding index of each data in the initial output tensor in the target output tensor, each data in the initial output tensor can be mapped to the corresponding position of the target output tensor according to these corresponding indexes, so that the data in the initial output tensor is rearranged or reshaped into the shape of the target output tensor. It should be understood that the data in the initial output tensor is the same as the data in the target output tensor, but the two have different dimensional sizes and the order of data arrangement is different.

[0072] Exemplarily, assuming that the shape of the initial output tensor is (N×head_num×sub_H, H_fold, head_size), and the shape of the target output tensor is (N×sub_H, H_fold, W), where W = head_size × head_num, in order to convert the initial output tensor into the target output tensor, it can be implemented by index calculation and mapping. Assuming that the index of any data in the initial output tensor in (N×head_num×sub_H, H_fold, head_size) is (in_n, in_h, in_w), through calculation, the corresponding index (out_n, out_h, out_w) of the index in the shape of the target output tensor (N×sub_H, H_fold, W) can be found.

[0073] Specifically, the first step can calculate the number of blocks into which the initial output tensor is decomposed in the batch dimension by the following formula, that is, original_batch_size = head_num × sub_H, which is used to indicate the size of the initial output tensor as a whole in the batch dimension, that is, how many blocks N×head_num×sub_H is decomposed into, and each block consists of sub_H sub-batches and head_num "heads".

[0074] In the second step, output_original_n can be calculated by the formula output_original_n = in_n / / original_batch_size, which represents the corresponding "original" batch index in the target output tensor (that is, ignoring the batch index before head_num and sub_H expansion).

[0075] In the third step, output_head_size_idx can be calculated by the formula output_head_size_idx = in_n %original_batch_size / / sub_H, which is used to indicate which "head" the current element in_n belongs to in the target output tensor.

[0076] In the fourth step, output_sub_H_idx can be calculated by the formula output_sub_H_idx = in_n % original_batch_size %sub_H, which is used to indicate which sub_H subbatch the current element in_n belongs to in the target output tensor.

[0077] Step 5. After the calculations in the above four steps, the first dimension index of the target output tensor (i.e., the batch dimension index) is obtained by multiplying output_original_n by sub_H (because each "original" batch is expanded into sub_H sub-batches in the target output tensor) and then adding output_sub_H_idx. The specific calculation formula is: out_n = output_original_n × sub_H + output_sub_H_idx.

[0078] Step 6. Since the H_fold dimension remains unchanged during the transformation, out_h is directly set to in_h, that is, out_h = in_h.

[0079] Step 7. Calculate the index of the data on the last dimension (i.e., width dimension) of the target output tensor. Since the width of the target output tensor is head_num times the width of the initial output tensor, the index needs to be adjusted according to output_head_size_idx. This can be achieved by adding in_w (the width index of the initial output tensor) plus output_head_size_idx multiplied by the width of the initial output tensor (i.e., head_size), and the calculation formula is: out_w = in_w + output_head_size_idx × head_size.

[0080] Based on any of the above embodiments, an embodiment of the present invention provides a tensor processing method, which can be applied to CPU (Central Processing Unit), GPU (Graphics Processing Unit), GPGPU (General-purpose computing on graphics processing units), etc., and can support the scenario of dimension folding by super-dimensional MHA on GPU and GPGPU. Specifically, the embodiment of the present invention can fold the head_num dimension of the tensor into the batch dimension by using the first dimension conversion operator while ensuring a certain order. After the MHA operation, the head_num dimension of the MHA output tensor is restored to the last dimension by using the second dimension conversion operator, thereby avoiding the problem of disordered batch dimension data, and also supporting the scenario of folding head_num into the batch dimension and large shape folding (i.e., the sequence length is too long, and the sequence length dimension is split and folded) in MHA.

[0081] Figure 5 It is a schematic diagram of processing Q, K, and V tensors provided by the present invention, such as Figure 5 As shown in the figure, taking the shape of Q, K, V tensors (4, 9216, 320) as an example, the processing of Q, K, V tensors and MHA output tensors by the first dimension operator and the second dimension operator is introduced respectively. In the implementation of MHA, the input sequence is first converted into an embedding vector through the embedding layer, and then transformed by linear transformation (such as Figure 5The MMA0, MMA1, and MMA2 operators shown in the figure are used to convert these embedding vectors into Q, K, and V tensors. The shape of the Q, K, and V tensors is (4, 9216, 320). Since the sequence length of the tensor (i.e., 9216) exceeds the preset length threshold, the dimension can be split and folded onto the batch dimension, resulting in a tensor shape of (4×9, 1024, 320), which can also be expressed as (4, 9, 1024, 320). In order to implement multi-head attention calculation, the third dimension (i.e., width dimension) of the tensor can be split by the stm1 operator to obtain the head_num dimension, which is folded onto the batch dimension, resulting in a tensor shape of (20, 9, 1024, 64). This can achieve dimension folding while avoiding batch dimension disorder. Subsequently, the folded Q, K, and V tensors can be multi-headed for attention calculation. After MHA calculation, the shape of the MHA output tensor is still (20, 9, 1024, 64). At this point, the stm2 operator can be applied to restore the MHA output tensor to restore the head_num dimension to the last dimension, thereby obtaining the final output tensor with a shape of (4, 9, 1024, 320), thereby realizing the processing of the MHA input and output tensors.

[0082] Based on any of the above embodiments, Figure 6 is a schematic diagram of the structure of the tensor processing device provided by the present invention, such as Figure 6 As shown, the device comprises: An acquisition unit 610, used to acquire an original tensor; A conversion unit 620 is used to fold the target sub-dimension to be folded in the original tensor onto the first dimension based on the first dimension conversion operator to obtain a target tensor; Among them, the first dimension of the target tensor includes the original first dimension, the target sub-dimension and the first sub-dimension, the target sub-dimension is located between the original first dimension and the first sub-dimension, the original first dimension is the first dimension of the original tensor, the first sub-dimension is obtained based on the splitting of the second dimension of the original tensor, and the target sub-dimension is obtained based on the splitting of the third dimension of the original tensor.

[0083] The device provided by the embodiment of the present invention can fold the target sub-dimension in the original tensor onto the first dimension to obtain the target tensor by introducing the first dimension conversion operator. At the same time, it can ensure the position of the target sub-dimension in the target tensor after folding, that is, between the original first dimension and the first sub-dimension, thereby realizing the rearrangement of the first dimension of the tensor and avoiding confusion in the order of the first dimension after folding, thereby helping to ensure the consistency and accuracy of data processing and reduce errors and losses caused by confusion in data dimensions.

[0084] Based on any of the above embodiments, the conversion unit 620 includes: A first shape determining unit is used to split the second dimension of the original tensor into the first sub-dimension and the second sub-dimension, and fold the first sub-dimension onto the first dimension of the original tensor to obtain a first shape of the reshaped tensor, wherein the first shape is determined based on the size of each dimension of the original tensor after the dimension folding; A second shape determining unit is used to split the third dimension of the reshaped tensor into the target sub-dimension and the third sub-dimension, and fold the target sub-dimension onto the first dimension of the reshaped tensor to obtain a second shape of the target tensor, wherein the second shape is determined based on the size of each dimension of the reshaped tensor after the dimension folding; An index mapping unit is used to apply the first shape and the second shape based on the first dimensional conversion operator to map the index of each data in the reshaped tensor to the second shape, so as to rearrange the position of each data in the reshaped tensor to obtain the target tensor.

[0085] Based on any of the above embodiments, the index mapping unit includes: a determining subunit, configured to determine an index range of data in the reshaped tensor based on the first shape; A traversal subunit, configured to traverse the index range, and determine a corresponding index of the index of any data under the second shape based on the index of any data traversed, the first shape, and the second shape; A mapping subunit is used to map each data in the reshaped tensor to a corresponding position of the target tensor based on a corresponding index of the index of each data in the reshaped tensor in the second shape, so as to obtain the target tensor.

[0086] Based on any of the foregoing embodiments, the index of any of the data includes a first dimension index, a second dimension index, and a third dimension index. Accordingly, the traversal subunit is specifically used for: Determine an original first dimension index and a first sub-dimension index of any data based on the first dimension index of any data and the size of the first sub-dimension in the first shape; Determine a target sub-dimension index and a third sub-dimension index of any data based on the third dimension index of any data and the size of the third dimension in the second shape; Determine a corresponding index of the first dimension index of any data in the second shape based on the original first dimension index, the target sub-dimension index and the first sub-dimension index of any data, the size of the first sub-dimension in the first shape and the size of the target sub-dimension in the second shape; Based on the second dimensional index of the any data, determining a corresponding index of the second dimensional index in the second shape; Based on the third sub-dimension index of the any data, a corresponding index of the third dimension index of the any data in the second shape is determined.

[0087] Based on any of the above embodiments, the device further includes: A computing unit, configured to compute the target tensor based on a multi-head attention operator to obtain an initial output tensor; A dimension restoration unit is used to restore the target sub-dimension in the initial output tensor based on the second dimension conversion operator to obtain a target output tensor.

[0088] Based on any of the above embodiments, the device further includes: A fusion unit is used to fuse the multi-head attention operator and the second dimension conversion operator to obtain a fusion operator; A processing unit is used to apply the tensor to perform calculation based on the fusion operator to obtain an initial output tensor, and restore the target sub-dimension in the initial output tensor to obtain a target output tensor.

[0089] Based on any of the above embodiments, the dimension restoration unit or the processing unit is specifically used for: Based on the shape of the initial output tensor and the shape of the target output tensor, the index of each data in the initial output tensor is mapped to the shape of the target output tensor to restore the target sub-dimension in the initial output tensor from the first dimension to the third dimension to obtain the target output tensor.

[0090] Figure 7 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 7As shown, the electronic device may include: a processor 710, a communication interface 720, a memory 730 and a communication bus 740, wherein the processor 710, the communication interface 720 and the memory 730 complete mutual communication through the communication bus 740. The processor 710 may call the logic instructions in the memory 730 to execute the tensor processing method, which includes: obtaining the original tensor; based on the first dimension conversion operator, folding the target sub-dimension to be folded in the original tensor to the first dimension to obtain the target tensor; wherein the first dimension of the target tensor includes the original first dimension, the target sub-dimension and the first sub-dimension, the target sub-dimension is located between the original first dimension and the first sub-dimension, the original first dimension is the first dimension of the original tensor, the first sub-dimension is obtained based on the splitting of the second dimension of the original tensor, and the target sub-dimension is obtained based on the splitting of the third dimension of the original tensor.

[0091] In addition, the logic instructions in the above-mentioned memory 730 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the relevant technology or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.

[0092] On the other hand, the present invention also provides a computer program product, which includes a computer program. The computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the tensor processing method provided by the above methods, the method including: obtaining an original tensor; based on a first dimension conversion operator, folding a target sub-dimension to be folded in the original tensor onto a first dimension to obtain a target tensor; wherein the first dimension of the target tensor includes an original first dimension, the target sub-dimension and a first sub-dimension, the target sub-dimension is located between the original first dimension and the first sub-dimension, the original first dimension is the first dimension of the original tensor, the first sub-dimension is obtained based on splitting the second dimension of the original tensor, and the target sub-dimension is obtained based on splitting the third dimension of the original tensor.

[0093] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the tensor processing method provided by the above-mentioned methods, the method comprising: obtaining an original tensor; based on a first dimension conversion operator, folding a target sub-dimension to be folded in the original tensor onto a first dimension to obtain a target tensor; wherein the first dimension of the target tensor comprises an original first dimension, the target sub-dimension and a first sub-dimension, the target sub-dimension is located between the original first dimension and the first sub-dimension, the original first dimension is the first dimension of the original tensor, the first sub-dimension is obtained based on splitting the second dimension of the original tensor, and the target sub-dimension is obtained based on splitting the third dimension of the original tensor.

[0094] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0095] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the relevant technology can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiment.

[0096] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A tensor processing method, characterized in that: include: Get the original tensor; Based on the first dimension conversion operator, fold the target sub-dimension to be folded in the original tensor onto the first dimension to obtain a target tensor; Among them, the first dimension of the target tensor includes the original first dimension, the target sub-dimension and the first sub-dimension, the target sub-dimension is located between the original first dimension and the first sub-dimension, the original first dimension is the first dimension of the original tensor, the first sub-dimension is obtained based on the splitting of the second dimension of the original tensor, and the target sub-dimension is obtained based on the splitting of the third dimension of the original tensor.

2. The tensor processing method according to claim 1, characterized in that: The method of folding the target sub-dimension to be folded in the original tensor onto the first dimension based on the first dimension conversion operator to obtain the target tensor includes: Splitting the second dimension of the original tensor into the first sub-dimension and the second sub-dimension, and folding the first sub-dimension onto the first dimension of the original tensor to obtain a first shape of the reshaped tensor, where the first shape is determined based on the size of each dimension of the original tensor after the dimension folding; Splitting the third dimension of the reshaped tensor into the target sub-dimension and the third sub-dimension, and folding the target sub-dimension onto the first dimension of the reshaped tensor to obtain a second shape of the target tensor, wherein the second shape is determined based on the size of each dimension of the reshaped tensor after the dimension folding; Based on the first dimensional conversion operator, the first shape and the second shape are applied to map the index of each data in the reshaped tensor to the second shape, so as to rearrange the position of each data in the reshaped tensor to obtain the target tensor.

3. The tensor processing method according to claim 2, characterized in that: The applying the first shape and the second shape to map the index of each data in the reshaped tensor to the second shape to rearrange the position of each data in the reshaped tensor to obtain the target tensor includes: Based on the first shape, determining an index range of data in the reshaped tensor; Traversing the index range, and determining a corresponding index of the index of any data under the second shape based on the index of any data traversed, the first shape, and the second shape; Based on the corresponding index of the index of each data in the reshaped tensor in the second shape, each data in the reshaped tensor is mapped to the corresponding position of the target tensor to obtain the target tensor.

4. The tensor processing method according to claim 3, characterized in that: The index of any data includes a first dimension index, a second dimension index, and a third dimension index, and determining a corresponding index of the index of any data under the second shape based on the traversed index of any data, the first shape, and the second shape includes: Determine an original first dimension index and a first sub-dimension index of any data based on the first dimension index of any data and the size of the first sub-dimension in the first shape; Determine a target sub-dimension index and a third sub-dimension index of any data based on the third dimension index of any data and the size of the third dimension in the second shape; Determine a corresponding index of the first dimension index of any data in the second shape based on the original first dimension index, the target sub-dimension index and the first sub-dimension index of any data, the size of the first sub-dimension in the first shape and the size of the target sub-dimension in the second shape; Based on the second dimensional index of the any data, determining a corresponding index of the second dimensional index in the second shape; Based on the third sub-dimension index of the any data, a corresponding index of the third dimension index of the any data in the second shape is determined.

5. The tensor processing method according to claim 1, characterized in that: Also includes: Based on the multi-head attention operator, the target tensor is calculated to obtain an initial output tensor; Based on the second dimension conversion operator, the target sub-dimension in the initial output tensor is restored to obtain a target output tensor.

6. The tensor processing method according to claim 1, characterized in that: Also includes: The multi-head attention operator and the second dimension conversion operator are fused to obtain a fusion operator; Based on the fusion operator, the tensor is applied to perform calculation to obtain an initial output tensor, and the target sub-dimension in the initial output tensor is restored to obtain a target output tensor.

7. The tensor processing method according to claim 5 or 6, characterized in that: The restoring the target sub-dimension in the initial output tensor to obtain a target output tensor includes: Based on the shape of the initial output tensor and the shape of the target output tensor, the index of each data in the initial output tensor is mapped to the shape of the target output tensor to restore the target sub-dimension in the initial output tensor from the first dimension to the third dimension to obtain the target output tensor.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the tensor processing method according to any one of claims 1 to 7 is implemented.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the tensor processing method according to any one of claims 1 to 7 is implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the tensor processing method according to any one of claims 1 to 7 is implemented.

Citation Information

Cited By

  • Code generation method and device, electronic equipment and storage medium

    CN120276719A