Computing device for performing complex Einstein summation operation, method for performing complex Einstein summation operation on a computing device, computer-readable storage medium, and computer program product
By introducing central processing units, graphics processing units and computing scheduling components into computing devices, and utilizing real-virtual decoupling and dimensional fusion of complex tensors, the complex Einstein summation operation is optimized, which solves the computational performance bottleneck of high-dimensional complex Einstein summation operations and achieves efficient, low-cost hardware compatibility and versatility.
Patent Information
- Application Number
- CN202511062236.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-31
- Publication Date
- 2025-09-30
- Estimated Expiration
- 2045-07-31
AI Technical Summary
Existing computing hardware lacks efficient support for high-dimensional complex Einstein summation operations, resulting in computing performance bottlenecks, high development costs for different hardware platforms, and poor portability.
By introducing central processing units, graphics processing units and computing scheduling components into computing devices, and utilizing real-imaginary decoupling and dimensional fusion of complex tensors, the complex Einstein summation operation is optimized, including the decoupling of the real and imaginary parts, dimensional expansion and sorting, to realize real matrix multiplication operations.
It improves computing efficiency and hardware compatibility, reduces development costs, and improves the efficiency and versatility of complex Einstein summation operations.
Smart Images

Figure CN120560604B_ABST
Abstract
Description
Technical Field
[0001] The present invention generally relates to processing digital data on a computing device, and more particularly to a computing device for performing a complex Einstein summation operation, a method for performing a complex Einstein summation operation on a computing device, a computer-readable storage medium, and a computer program product. Background Art
[0002] The Einstein Summation Convention is an efficient method for representing tensor operations. The Einstein summation operation (einsum, also known as the Einstein product) is widely used in scientific computing, such as quantum mechanics, tensor network contraction, quantum computing (such as quantum gate operations), and high-order tensor operations in machine learning. It is of great value in fields such as physical simulation, quantum chemistry calculations, and artificial intelligence for science.
[0003] With the prevalence of floating-point computations (e.g., half-precision floating-point FP16 / BF16) in artificial intelligence (AI) acceleration chips, the demand for complex operations (e.g., complex half-precision FP16) has grown, particularly in quantum computing simulations and tensor network optimization. However, existing computing hardware (e.g., graphics processing units (GPUs), tensor processing units (TPUs), and AI accelerator chips) is primarily optimized for general-purpose computational patterns (e.g., matrix multiplication, convolution, and element-by-element operations) and lacks efficient native support for high-dimensional complex Einstein summations (e.g., input / output dimensions greater than 30). Therefore, as tensor network algorithms become increasingly complex and data sizes continue to expand, efficiently executing large-scale Einstein summations has become a critical bottleneck in computing performance.
[0004] The following solutions are all insufficient for performing complex Einstein sums:
[0005] NVIDIA's cuTENSOR is a GPU-accelerated tensor linear algebra library that can fully utilize the dedicated tensor cores of NVIDIA's GPUs to accelerate high-performance tensor calculations. However, its application has obvious hardware limitations and only supports NVIDIA series GPUs, and cannot be used on general-purpose chip platforms.
[0006] The open-source PyTorch framework can implement basic Einstein summation functions through automatic complex number decomposition. However, its implementation, which decomposes a single complex operation into four real operations, suffers from inherent efficiency flaws, resulting in a more than 50% drop in computational throughput and a doubling of video memory accesses. More critically, this design relies heavily on the underlying hardware's native support for high-dimensional reduction operators. Consequently, even when calling PyTorch through the framework's interface on general-purpose chips lacking corresponding hardware acceleration, its actual computational functionality cannot be effectively guaranteed, requiring specialized personnel to optimize the underlying hardware.
[0007] Domestic chips also offer open-source operator libraries, but these have significant functional limitations. For one thing, they lack support for the Einstein summation of complex floating-point numbers, and their support for arithmetic formulas is also limited. For example, chip manufacturers only pre-install around 20 specific calculation modes, severely restricting their flexibility in complex scientific computing scenarios.
[0008] Furthermore, due to differences in computational characteristics across different hardware architectures (e.g., Single Instruction Multiple Data (SIMD) vectorization, matrix computation units, and tensor cores), directly implementing high-dimensional complex Einstein summations typically requires hardware-specific customization and optimization, resulting in high development costs and poor portability. Furthermore, developing and optimizing computational processes for different hardware platforms results in high development costs and difficulty in generalization.
[0009] None of the above solutions can take into account both hardware versatility and high efficiency of high-dimensional complex tensor calculations.
[0010] There is a need in the art for techniques for performing complex Einstein summation operations on computing devices that are improved in at least one of the aspects discussed above. Summary of the Invention
[0011] The present invention is provided to provide a technology for further improving the execution of complex Einstein summation operations on computing devices by utilizing real-virtual decoupling and expansion of complex tensors and dimension fusion.
[0012] One aspect of the present invention provides a computing device for performing a complex Einstein summation operation, comprising: a central processing unit for performing general-purpose computing; a graphics processing unit for performing high-performance general-purpose computing or graphics computing, wherein the graphics processing unit includes a video memory; and a computing scheduling component coupled to the central processing unit and coupled to the graphics processing unit, wherein the computing scheduling component is configured to: enable the central processing unit to receive a string indicating a complex Einstein summation operation including a first tensor dimension, a second tensor dimension, and an output dimension; enable the graphics processing unit to receive a string indicating a complex Einstein summation operation including a first tensor dimension, a second tensor dimension, and an output dimension; and enable the graphics processing unit to receive a string indicating a complex Einstein summation operation including a first tensor dimension, a second tensor dimension, and an output dimension. Collect a first complex tensor associated with the first tensor dimension and a second complex tensor associated with the second tensor dimension, and store the first complex tensor and the second complex tensor in the video memory; compare the video memory space occupied by the first complex tensor and the video memory space occupied by the second complex tensor; determine a large complex tensor and a small complex tensor based on the result of the comparison; decouple the real part and the imaginary part of each data element of the large complex tensor to obtain a large real tensor; expand and decouple the real part and the imaginary part of each data element of the small complex tensor into a real part, a negative imaginary part, an imaginary part and a negative imaginary part. a real part, thereby obtaining a small real tensor; adding a first additional dimension to the tail of the tensor dimension associated with the large complex tensor in the string, adding a second additional dimension and the first additional dimension to the head and tail of the tensor dimension associated with the small complex tensor in the string, respectively, and adding the second additional dimension to the tail of the output dimension in the string, thereby obtaining an updated string, wherein the first additional dimension is associated with the decoupling of the real part and the imaginary part of each data element of the large complex tensor, and the first additional dimension and the second additional dimension are associated with the decoupling of the real part and the imaginary part of each data element of the small complex tensor. The invention relates to a method for performing a real number tensor and a small real number tensor, wherein the real number tensor is associated with the expansion and decoupling of the real part and the imaginary part of each data element of the real number tensor; detecting a contraction dimension in the updated string; arranging the dimensions associated with the contraction dimension in the updated string together to obtain a sorted string; applying a permutation vector associated with the permutation to the large real number tensor and the small real number tensor to obtain a permuted large real number tensor and a permuted small real number tensor; and causing the graphics processing unit to calculate a real matrix multiplication operation associated with the permuted large real number tensor, the permuted small real number tensor and the updated string.
[0013] In the computing device as described above, the computing scheduling component is further used to: detect a reduction dimension in the string; and determine a reduced first complex tensor, a reduced second complex tensor, and a reduced string based on the reduction dimension, wherein the comparison of the occupied video memory space is performed based on the video memory space occupied by the reduced first complex tensor and the video memory space occupied by the reduced second complex tensor, and wherein the updated string is determined based on the reduced string.
[0014] In the computing device as described above, the first complex tensor is stored in a first area of the video memory, and the second complex tensor is stored in a second area of the video memory, a first pointer associated with the first complex tensor is set to point to the first area, and a second pointer associated with the second complex tensor is set to point to the second area, wherein the computing scheduling component is also used to perform the following steps in response to the result of the comparison indicating that the capacity of the first area is less than the capacity of the second area: making the first pointer point to the second area, and making the second pointer point to the first area; and exchanging the positions of the first tensor dimension and the second tensor dimension in the string, wherein the area pointed to by the first pointer is associated with the large complex tensor, and the area pointed to by the second pointer is associated with the small complex tensor.
[0015] In the computing device as described above, the sizes of the first additional dimension and the second additional dimension are both 2, the first position in the first additional dimension and the second additional dimension is used to store the real part of the corresponding tensor, and the second position in the first additional dimension and the second additional dimension is used to store the imaginary part of the corresponding tensor.
[0016] In the computing device as described above, the computing scheduling component is used to: arrange the dimensions associated with the contraction dimension in the tensor dimensions associated with the large real number tensor at the tail; and arrange the dimensions associated with the contraction dimension in the tensor dimensions associated with the small real number tensor at the head in the same order as the tensor dimensions associated with the large real number tensor, thereby obtaining the sorted string.
[0017] In the computing device as described above, the computing scheduling component is configured to query the index position of each character in the sorted character string in the updated character string to obtain the permutation vector.
[0018] In the computing device as described above, the computing scheduling component is used to: enable the graphics processing unit to use the permuted large real number tensor as the first real number matrix and the permuted small real number tensor as the second real number matrix to calculate the real number matrix multiplication operation, wherein the first dimension of the first real number matrix corresponds to the remaining dimension of the permuted large real number tensor relative to the contraction dimension, the second dimension of the first real number matrix corresponds to the contraction dimension of the permuted large real number tensor, and wherein the first dimension of the second real number matrix corresponds to the contraction dimension of the permuted small real number tensor, and the second dimension of the second real number matrix corresponds to the remaining dimension of the permuted small real number tensor relative to the contraction dimension.
[0019] In the computing device as described above, each data element in the first complex tensor and the second complex tensor includes a half-precision real part and a half-precision imaginary part.
[0020] In the computing device as described above, the complex Einstein summation operation is used for quantum computing or tensor network contraction.
[0021] Another aspect of the present invention provides a method for performing a complex Einstein summation operation on a computing device, the computing device comprising a central processing unit and a graphics processing unit, the method comprising the following steps: S101: causing the central processing unit to receive a string indicating a complex Einstein summation operation comprising a first tensor dimension, a second tensor dimension, and an output dimension; S102: causing the graphics processing unit to receive a first complex tensor associated with the first tensor dimension and a second complex tensor associated with the second tensor dimension, and to perform the complex Einstein summation operation on the first complex tensor and the second complex tensor. The second complex tensor is stored in the video memory of the graphics processing unit; S103: comparing the video memory space occupied by the first complex tensor and the video memory space occupied by the second complex tensor; S104: determining a large complex tensor and a small complex tensor based on the result of the comparison; S105: decoupling the real part and the imaginary part of each data element of the large complex tensor, thereby obtaining a large real tensor; S106: expanding and decoupling the real part and the imaginary part of each data element of the small complex tensor into a real part, a negative imaginary part, an imaginary part and a real part, thereby obtaining a small real tensor; S107: A first additional dimension is added to the tail of the tensor dimension associated with the large complex tensor, a second additional dimension and the first additional dimension are added to the head and tail of the tensor dimension associated with the small complex tensor in the string, respectively, and the second additional dimension is added to the tail of the output dimension in the string, thereby obtaining an updated string, wherein the first additional dimension is associated with the decoupling of the real part and the imaginary part of each data element of the large complex tensor, and the first additional dimension and the second additional dimension are associated with the expansion and decoupling of the real part and the imaginary part of each data element of the small complex tensor. coupling association; S108: detecting the contraction dimension in the updated string; S109: arranging the dimensions associated with the contraction dimension in the updated string together to obtain a sorted string; S110: applying the permutation vector associated with the permutation to the large real number tensor and the small real number tensor to obtain a permuted large real number tensor and a permuted small real number tensor; and S111: causing the graphics processing unit to calculate the real matrix multiplication operation associated with the permuted large real number tensor, the permuted small real number tensor and the updated string.
[0022] As described above, the method further includes the following steps: S112: detecting a reduction dimension in the string; and S113: determining a reduced first complex tensor, a reduced second complex tensor, and a reduced string based on the reduction dimension, wherein the comparison of the occupied memory space is performed based on the memory space occupied by the reduced first complex tensor and the memory space occupied by the reduced second complex tensor, and wherein the updated string is determined based on the reduced string.
[0023] According to the method described above, the first complex tensor is stored in the first area of the video memory, and the second complex tensor is stored in the second area of the video memory, the first pointer associated with the first complex tensor is set to point to the first area, and the second pointer associated with the second complex tensor is set to point to the second area, wherein, in response to the result of the comparison indicating that the capacity of the first area is less than the capacity of the second area, the method also includes the following steps: S114: make the first pointer point to the second area, and make the second pointer point to the first area; and S115: swap the positions of the first tensor dimension and the second tensor dimension in the string, wherein the area pointed to by the first pointer is associated with the large complex tensor, and the area pointed to by the second pointer is associated with the small complex tensor.
[0024] According to the method described above, the sizes of the first additional dimension and the second additional dimension are both 2, the first position in the first additional dimension and the second additional dimension is used to store the real part of the corresponding tensor, and the second position in the first additional dimension and the second additional dimension is used to store the imaginary part of the corresponding tensor.
[0025] As described above, the S109 includes: S1091: arranging the dimensions associated with the contraction dimension in the tensor dimensions associated with the large real number tensor at the tail; and S1092: arranging the dimensions associated with the contraction dimension in the tensor dimensions associated with the small real number tensor at the head in the same order as the tensor dimensions associated with the large real number tensor, thereby obtaining the sorted string.
[0026] In the method described above, S110 includes: querying the index position of each character in the sorted character string in the updated character string to obtain the replacement vector.
[0027] As described above, the S111 includes: causing the graphics processing unit to use the permuted large real number tensor as the first real number matrix and the permuted small real number tensor as the second real number matrix to calculate the real number matrix multiplication operation, wherein the first dimension of the first real number matrix corresponds to the remaining dimension of the permuted large real number tensor relative to the contraction dimension, the second dimension of the first real number matrix corresponds to the contraction dimension of the permuted large real number tensor, and wherein the first dimension of the second real number matrix corresponds to the contraction dimension of the permuted small real number tensor, and the second dimension of the second real number matrix corresponds to the remaining dimension of the permuted small real number tensor relative to the contraction dimension.
[0028] According to the method described above, each data element in the first complex tensor and the second complex tensor includes a half-precision real part and a half-precision imaginary part.
[0029] According to the method described above, the complex Einstein summation operation is used for quantum computing or tensor network contraction.
[0030] Another aspect of the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program implements the steps of any of the above methods when executed by a processor.
[0031] Another aspect of the present invention provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the steps of any one of the above methods are implemented.
[0032] The computing device and method according to the present invention overcome the limitations of performing traditional complex Einstein summation operations on hardware. By utilizing the real-virtual decoupling and expansion of complex tensors and dimensional fusion, the computational efficiency and hardware compatibility of performing complex Einstein summation operations on computing devices are improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] Embodiments of the present invention are described with reference to the accompanying drawings.
[0034] Figure 1 is a block diagram of a computing device according to some embodiments of the present invention.
[0035] Figure 2 is a schematic diagram of a process of performing a complex Einstein summation operation by a computing device according to some embodiments of the present invention.
[0036] Figure 3 3 is a schematic diagram of real-virtual decoupling and expansion of tensors according to some embodiments of the present invention.
[0037] Figure 4 is a schematic diagram of performing dimension permutation on a tensor according to some embodiments of the present invention.
[0038] Figure 5 is a schematic diagram of reducing a tensor according to some embodiments of the present invention.
[0039] Figures 6 and 7 is a flow chart of a first method for performing a complex Einstein summation operation on a computing device according to some embodiments of the present invention.
[0040] Figure 8 is a flow diagram of a first process associated with a first method for performing a complex Einstein summation operation on a computing device, according to some embodiments of the present invention.
[0041] Figure 9 is a block diagram of a computer-readable storage medium according to some embodiments of the present invention.
[0042] Figure 10 is a block diagram of a computer program product according to some embodiments of the present invention. DETAILED DESCRIPTION
[0043] In this application, ordinal numbers such as "first," "second," and "third" are used to distinguish different instances of the same object. The ordinal numbers such as "first," "second," and "third" do not indicate the relative order of the objects in time, space, ranking, or other aspects.
[0044] According to one aspect of the present invention, a computing device for performing a complex Einstein summation operation is provided.
[0045] Figure 1 is a block diagram of a computing device 100 according to some embodiments of the present invention.
[0046] The computing device 100 may include a central processing unit (CPU) 110, a graphics processing unit (GPU) 120, and a computation scheduling component 130. The CPU 110 is configured to perform general-purpose computing. The GPU 120 is configured to perform high-performance general-purpose computing or graphics computing. The GPU 120 may include video memory 122. The computation scheduling component 130 is coupled to the CPU 110 and the GPU 120. The computation scheduling component 130 may be configured to perform scheduling between the CPU 110 and the GPU 120. For example, the computation scheduling component 130 may send data, signals, and / or commands to the CPU 110 and / or receive data, signals, and / or commands from the CPU 110. The computation scheduling component 130 may also send data, signals, and / or commands to the GPU 120 and / or receive data, signals, and / or commands from the GPU 120.
[0047] Figure 2 FIG is a schematic diagram of a process for performing a complex Einstein summation operation by a computing device according to some embodiments of the present invention. Figure 2 The computing device 100 is described.
[0048] At a first block 201 , the computation scheduling component 130 causes the central processing unit 110 to receive a string indicating a complex Einstein summation operation including a first tensor dimension, a second tensor dimension, and an output dimension. The computation scheduling component 130 causes the graphics processing unit 120 to receive a first complex tensor associated with the first tensor dimension and a second complex tensor associated with the second tensor dimension, and to store the first complex tensor and the second complex tensor in the graphics memory 122 .
[0049] In some embodiments, each data element in the first complex tensor and the second complex tensor may include a half-precision real part and a half-precision imaginary part.
[0050] In some embodiments, the complex Einstein summation operation can be used for quantum computing or tensor network contraction.
[0051] The following is an example of a general expression for the sum operation of two tensors:
[0052] C il = ∑ k A ijk ⋅ B kl (1)
[0053] In the above expression, the tensor dimension of input tensor A is i*j*k, the tensor dimension of input tensor B is k*l, tensors A and B are summed with respect to dimension k, and the output dimension of output result C is i1. The tensors and tensor dimensions in the above expression are merely examples, and the scope of the present invention is not limited thereto.
[0054] The following describes the Einstein summation operation.
[0055] To simplify the above expression, we can use Einstein summation strings to represent the dimensions of the input tensors and output results. Einstein summation strings are a method of expressing high-dimensional Einstein summation operations using the Einstein summation convention. In the Einstein summation convention, the expression is simplified by using subscripts to mark the dimensions of the input tensors and output results. The Einstein summation string for the above summation operation can be expressed as follows:
[0056] ijk,kl->il (2)
[0057] Among them, the left side of “->” represents the input, the character on the left side of “,” represents the tensor dimension of the input tensor A, the character on the right side of “,” represents the tensor dimension of the input tensor B, and the right side of “->” represents the output dimension of the output result C.
[0058] By convention, the reduced dimension, contracted dimension, and remaining dimension are defined in the Einstein summation operation:
[0059] Reduction dimension: If a dimension appears only in the subscript of a single input tensor and does not appear in the subscripts of other input tensors or the output tensor, then this dimension is called the reduction dimension, and the sum operation is performed along this dimension.
[0060] Contraction dimension: If a dimension appears in the subscripts of both input tensors but does not appear in the subscript of the output tensor, it is called a contraction dimension, and element-wise products are performed along this dimension before summing.
[0061] Residual dimensions: The dimensions of the output tensor are called residual dimensions. These residual dimensions are also the dimensions that remain after removing the reduction and contraction dimensions from the two input tensors.
[0062] In conjunction with the above expression (2), specifically, only the input tensor A has dimension j, which does not exist in the dimensions of the input tensor B and the output result C. This dimension j is the reduction dimension of the input tensor A, and calculations along this reduction dimension j will perform an addition operation on the input tensor A. The dimension k shared by the input tensors A and B but not in the output result C is called the contraction dimension. Calculations along this contraction dimension k will perform an element-by-element product and sum operation on the input tensors A and B. The dimension il of the output result C is the remaining dimension, which is also the sum of the remaining dimension i in the input tensor A and the remaining dimension l in the input tensor B.
[0063] Therefore, to facilitate the description of the operation of the computing device, for the first complex tensor A a1a2…aNA , the second complex tensor B b1b2…bNB And the output result C c1c2…cNC , the following uses the general Einstein summation string to represent the complex Einstein summation operation:
[0064] a1a2…a NA , b1b2…b NB -> c1c2…c NC (3)
[0065] Among them, a1a2…a NA Represents the first tensor dimension of the first complex tensor A, whose dimensions are from a1, a2 to a NA In order, NA indicates the number of dimensions of the first complex tensor A. b1b2…b NB Represents the second tensor dimension of the second complex tensor B, NB represents the number of dimensions of the second complex tensor B, c1c2…c NC represents the output dimension of the output result C, and NC represents the number of dimensions of the output result C.
[0066] The corresponding summation formula is:
[0067] C = Σ 𝛿1 …Σ 𝛿𝑁contract A a1a2…a𝑁𝐴 * B b1b2…b𝑁 (4)
[0068] Among them, 𝛿1 continues to 𝛿𝑁 contract Indicates the contraction dimension.
[0069] Computational complexity = (byte size required by the data format) × 2 × m × n × k (5)
[0070] In the computational complexity formula (5), 2 represents one multiplication and one addition operation. m represents the one-dimensional dimension formed by flattening the remaining dimensions in the first complex tensor A, whose size is equal to the total number of elements contained in the remaining dimensions. n represents the one-dimensional dimension formed by flattening the remaining dimensions in the second complex tensor B, whose size is equal to the total number of elements contained in the remaining dimensions. k represents the one-dimensional dimension formed by flattening the contraction dimension involved in the contraction operation, whose size is equal to the total number of elements contained in the contraction dimension. Taking complex half-precision floating-point as an example, the data formats involved in the calculation process include complex half-precision (4 bytes) and half-precision (2 bytes). According to the definition of the Einstein summation operation, the computational complexity is only related to the contraction dimension and the remaining dimension, and is not related to the reduction dimension.
[0071] To optimize space complexity and increase computational speed, for example, for complex half-precision data, an intuitive approach is to convert the complex half-precision data into a half-precision real number pair consisting of a half-precision real part and a half-precision imaginary part, so that the real and imaginary parts of the complex number are each stored using a half-precision floating-point number. However, commonly used high-performance computing libraries lack support for complex number contraction operations. Libraries such as PyTorch implement complex number calculations by splitting complex numbers into real and imaginary parts. The following describes PyTorch's calculation method.
[0072] For the Einstein summation operation of complex tensors A and B, the expression is as follows, where the subscripts real and imag represent the real and imaginary parts of the tensor elements, respectively:
[0073] C = einsum(A, B) = einsum(A real +i A imag , B real +i B imag ) (6)
[0074] Since Einstein summation is actually a multiplication and addition calculation and obeys the distributive law, complex number multiplication can be expanded as:
[0075] C = einsum(A real , B real ) + (i*i) einsum(A imag , B imag ) + i einsum(A real ,B imag ) + i einsum(A imag , B real )
[0076] = einsum(A real , B real ) - einsum(Aimag , B imag ) + i (einsum(A real , B imag )+ einsum(A imag , B real ))
[0077] = Σ 𝛿1 …Σ 𝛿𝑁contract (A real * B real - A imag * B imag ) + i Σ 𝛿1 …Σ 𝛿𝑁contract (A real * B imag + A imag * B real ) (7)
[0078] Therefore, taking the complex half-precision floating point as an example, the byte size required for the data format is 4, and the original computational complexity is 4×2×m×n×k=8mnk. However, the PyTorch method simply decomposes the real and imaginary parts of the complex number, and the byte size required for the data format is 2, but it will result in four independent Einstein summation operations, so the computational complexity is 2×4×2×m×n×k=16mnk; after obtaining these four Einstein summation results, two more summations are calculated, namely einsum(A real , B real ) - einsum(A imag , B imag ) and einsum(A real , B imag ) + einsum(A imag ,B real ), the corresponding computational complexity is 4mn. The total computational complexity of the PyTorch method is 16mnk + 4mn, which is at least twice the original computational complexity and causes repeated memory access, thereby increasing unnecessary computational overhead and memory bandwidth pressure.
[0079] On GPUs, complex data is stored sequentially in the order [real part + imaginary part, real part + imaginary part, ...]. Therefore, at the byte level, the real and imaginary parts of each data element are closely connected. When using the simple splitting of real and imaginary parts described above, each calculation only reads the real or imaginary part of the data element, resulting in discontinuous memory accesses. Because GPU global memory accesses typically use 32-byte, 64-byte, or 128-byte units, even if only 1 byte of data is required, the GPU reads the entire memory block, automatically loading adjacent data. This means that even though each calculation actually requires only half the total amount of data, the amount of data read from the GPU's shared memory still corresponds to a complete tensor. The following further illustrates the read and write complexity of GPUs using the Einstein summation formula.
[0080] First, for the four Einstein summation operations mentioned above, einsum(A real, B real ), einsum(A imag, B imag ), (einsum(A real, B imag ), einsum(A imag, B real )), the memory read complexity of tensors A and B is 2 (half-precision bytes) * 4 (times) * (mk+kn) = 8(mk+kn), and the write complexity is 2 (half-precision bytes) * 4 (times) * (mn) = 8mn.
[0081] Secondly, after obtaining the four Einstein summation results, we need to calculate two summations, namely einsum(A real ,B real ) - einsum(A imag , B imag ) and einsum(A real , B imag ) + einsum(A imag , B real ), the read complexity is 8mn and the write complexity is 4mn.
[0082] Therefore, the total data read complexity of this access method is 8(mk+kn+mn), and the total data write complexity is 12mn.
[0083] The following describes the decoupling of the real and imaginary parts of a complex tensor and the dimension expansion.
[0084] In order to reduce the complexity of GPU reading and writing data to video memory, the real and imaginary parts of complex tensors can be decoupled.
[0085] At the first determination block 202, the computation scheduling component 130 compares the video memory space occupied by the first complex tensor with the video memory space occupied by the second complex tensor, and then determines the large complex tensor and the small complex tensor based on the comparison result. If the video memory space occupied by the first complex tensor is greater than the video memory space occupied by the second complex tensor, then the first complex tensor is a large complex tensor and the second complex tensor is a small complex tensor. In this case, the read and write operations of the small complex tensor are often negligible compared to the large complex tensor and the output tensor, so the data access of the large complex tensor and the output tensor is mainly focused on.
[0086] At block 203 , the computation scheduling component 130 decouples the real and imaginary parts of each data element of the large complex tensor, thereby obtaining a large real tensor. Furthermore, the computation scheduling component 130 expands and decouples the real and imaginary parts of each data element of the small complex tensor into a real part, a negative imaginary part, an imaginary part, and a real part, thereby obtaining a small real tensor. Next, the computation scheduling component 130 updates the string.
[0087] In specific applications, since the number of elements n in the remaining dimensions of the small complex tensor B is much smaller than the number of elements m in the remaining dimensions of the large complex tensor A, that is, the size of the small complex tensor B is much smaller than the large complex tensor A, it is not desirable to modify the large complex tensor A for reasons of video memory and copy volume. To this end, an embodiment of the present invention proposes a new complex tensor real-virtual decoupling and reconstruction method for small complex tensors, which expands and decouples the small complex tensor B. That is, a new GPU space is opened up, and the small complex tensor B = [B real + i B imag ] is expanded and decoupled into a real tensor B r = [B real ,- B imag , B imag , B real ], corresponding to the real part, negative number of the imaginary part, imaginary part and real part of the data element of the original complex tensor, where B r Represents the small real tensor corresponding to the small complex tensor B, and converts the small real tensor B r Stored in the new GPU space. Although the small real tensor B r The amount of data for the small complex tensor B is doubled, but since n << m, its impact on the entire Einstein summation operation is negligible.
[0088] For a large complex tensor A, the real and imaginary parts can be directly decoupled, that is, the large complex tensor A = [A real + i A imag ] decoupled into a real tensor A r =[Areal , A imag ], corresponding to the real and imaginary parts of the data elements of the original complex tensor, where A r Represents the large real tensor corresponding to the large complex tensor A. In a computing device, the conversion between the complex view and the real view of the large complex tensor A only involves a change in interpretation, that is, instructing the computing device how to interpret the bit values without actually performing calculations or making video memory copies.
[0089] Figure 3 is a schematic diagram of real-imaginary decoupling and expansion of tensors according to some embodiments of the present invention. For a large complex tensor A, each data element thereof includes a real part indicated by the subscript real and an imaginary part indicated by the subscript imag. Taking a tensor of dimension 2x2 as an example, the first data element of the first row and first column of the large complex tensor A is represented by A 11,real +iA 11,imag After directly decoupling the real and imaginary parts of the large complex tensor A, a large real tensor A of dimension 2x2x2 is obtained. r As indicated by the arrow, the real part A in the first data element of the large complex tensor A 11,real Form a large real number tensor A r The data element of the first row and first column of the large complex tensor A is the imaginary part of the first data element 11,imag Form a large real number tensor A r The data elements of the first row and second column of . Similarly, we can get the large complex tensor A and the large real tensor A r The correspondence between other data elements.
[0090] For a small complex tensor B, each data element includes a real part indicated by the subscript real and an imaginary part indicated by the subscript imag. Taking a tensor of dimension 2x2 as an example, the first data element of the first row and first column of the small complex tensor B is represented as B 11,real +iB 11,imag After expanding and decoupling the small complex tensor B, a small real tensor B with dimension 4*4 in the new GPU space is obtained. r , since the small real tensor B r The amount of data in the small complex tensor B is doubled, so its space size is twice the space occupied by the small complex tensor B. As indicated by the arrow, the real part B in the first data element of the small complex tensor B 11,real Form a small real number tensor B r The data element of the first row and first column is the imaginary part of the first data element of the small complex tensor B 11,imag The negative of forms a small real tensor B r The data element of the first row and second column is the imaginary part of the first data element of the small complex tensor B 11,imag Form a small real number tensor Br The data element of the third row and first column of the real part of the first data element of the small complex tensor B 11,real Form a small real number tensor B r The data elements of the third row and second column of . Similarly, we can get the small complex tensor B and the small real tensor B r The correspondence between other data elements.
[0091] Although Figure 3 Only the decoupling of the large complex tensor A of dimension 2x2 and the expansion and decoupling of the small complex tensor B of dimension 2x2 are shown, but the scope of the present invention is not limited to this, and the decoupling operation can be performed on large complex tensors and small complex tensors of other dimensions according to the same principle.
[0092] To adapt to the real tensor, the computation scheduling component 130 adds a first additional dimension to the end of the tensor dimension associated with the large complex tensor in the string, adds a second additional dimension and a first additional dimension to the beginning and end of the tensor dimension associated with the small complex tensor in the string, respectively, and adds the second additional dimension to the end of the output dimension in the string, thereby obtaining an updated string. The first additional dimension is associated with the decoupling of the real and imaginary parts of each data element of the large complex tensor, and the first additional dimension and the second additional dimension are associated with the expansion and decoupling of the real and imaginary parts of each data element of the small complex tensor.
[0093] The Einstein string expressed by formula (3) is updated as:
[0094] a1a2…a 𝑁𝐴 π, µb1b2… b 𝑁𝐵 π -> c1c2…c NC µ (8)
[0095] The first additional dimension π is added to the end of the tensor dimensions associated with the large complex tensor A. The second additional dimension µ and the first additional dimension π are added to the beginning and end of the tensor dimensions associated with the small complex tensor B, respectively. The second additional dimension µ is added to the end of the output dimensions. The first additional dimension π is considered a contraction dimension in the operation, while the second additional dimension µ is considered a remaining dimension in the operation.
[0096] In some embodiments, the size of the first additional dimension and the second additional dimension may both be 2, the first position in the first additional dimension and the second additional dimension may be used to store the real part of the corresponding tensor, and the second position in the first additional dimension and the second additional dimension may be used to store the imaginary part of the corresponding tensor.
[0097] The following is the decoupled real tensor A r and B rThe consistency between the calculation result of the Einstein summation operation represented by the updated character string and the calculation result in formula (7) is explained.
[0098] When µ = 1, π = 1, corresponding to A r π =1 = [A real ], B r µ = 1, π =1 = [B real ], and the real part of C.
[0099] Since both π and µ have specific values, the Einstein summation string is consistent with the original formula (3), and the corresponding mathematical formula is consistent with formula (4).
[0100] Result 1 = Σ 𝛿1 …Σ 𝛿𝑁contract A real * B real (9)
[0101] When µ = 1, π = 2, corresponding to A r π =2 = [A imag ], B r µ = 1, π =2 = [- B imag ], and the real part of C.
[0102] Result 2 = Σ 𝛿1 …Σ 𝛿𝑁contract A imag * (- B imag ) (10)
[0103] Since π belongs to the contracted dimension, we need to sum the results 1 and 2, and the sum is:
[0104] Σ 𝛿1 …Σ 𝛿𝑁contract (A real * B real - A imag * B imag ) = C real (11)
[0105] Similarly, when µ = 2, π = 1; µ = 2, π = 2, the sum of the calculation results is:
[0106] Σ 𝛿1 …Σ 𝛿𝑁contract (A real * B imag + A imag * B real ) = C imag (12)
[0107] The calculation result of the Einstein string expressed by formula (8) is [C real , C imag ]. Converting the real view to the complex view in the computing device is the output result C. In the computing device, the conversion between the real view and the complex view of the output result C only involves a change in the interpretation method, that is, instructing the computing device how to interpret the bit value without actually performing the calculation or making a memory copy.
[0108] The contraction adds a first additional dimension π of size 2, increasing the number of elements in the contraction from k to 2k. The number of elements in the remaining dimensions of the first complex tensor A remains at m, while the second complex tensor B adds a second additional dimension µ of size 2, increasing the number of elements in the remaining dimensions from n to 2n. Substituting these parameters into the computational complexity formula, the final computational complexity is 2 (half-precision bytes) × 2 (multiply-add) × m × 2n × 2k = 16mnk. The data read complexity is 4mk + 8kn, and the data write complexity is 4mn. Considering n << m, compared to the PyTorch method's computational complexity of (16mnk + 4mn), data read complexity of 8 (mk + kn + mn), and data write complexity of 12mn, while maintaining or even reducing the computational complexity, some embodiments of the present invention reduce the data read complexity by at least half and the data write complexity by one-third.
[0109] Therefore, the embodiments of the present invention utilize the real-imaginary decoupling and reconstruction technology of complex tensors, decompose the complex tensor into real tensor representations of the real part and the imaginary part through mathematical transformation, reorganize the data in the form of real number pairs, and expand the complex dimension using auxiliary dimensions, thereby achieving hardware compatibility while ensuring operational equivalence. The embodiments of the present invention avoid the redundant memory access and calculation of four real number operations required for traditional complex number calculations by decoupling and reconstructing the real-imaginary complex tensors, greatly reducing the complexity of data reading and writing, saving computing resources of computing equipment, and improving computing speed and efficiency.
[0110] The following describes real tensor dimension permutations and substitutions.
[0111] At the third block 204, the computation scheduling component 130 detects the contracted dimension in the updated string. The computation scheduling component 130 then arranges the dimensions associated with the contracted dimension in the updated string to obtain a sorted string. The computation scheduling component 130 then applies the permutation vector associated with the permutation to the large real number tensor and the small real number tensor to obtain a permuted large real number tensor and a permuted small real number tensor.
[0112] In order to convert tensor operations into matrix multiplication, the contraction dimensions need to be arranged in order. First, the dimension structure of the tensor is analyzed by string analysis. For example, in the string "ikhp, koi -> hpo", the contraction dimension ik and the remaining dimension hpo can be identified. In some embodiments, the tensor A can be r and B r The corresponding tensor dimensions are compared to identify the contraction dimension, for example, you can traverse the tensor A r and B r The associated tensor dimensions are filtered out with the same characters as the contraction dimensions. The dimensions associated with the output tensor C are the remaining dimensions.
[0113] Next, the order of the dimensions in the string is adjusted. In some embodiments, the computation scheduling component 130 may be configured to arrange the dimensions associated with the contraction dimension in the tensor dimensions associated with the large real number tensor at the end. Furthermore, the dimensions associated with the contraction dimension in the tensor dimensions associated with the small real number tensor are arranged at the beginning in the same order as the tensor dimensions associated with the large real number tensor, thereby obtaining a sorted string.
[0114] In the adjusted string, the contraction dimension is the same as the real tensor A r and B r The associated tensor dimensions are continuous and in the same order, and are consistent with the tensor A r The associated tensor dimension is at the end, in the tensor B r For example, modify the string to "hpik, iko -> hpo" so that the order of the contraction dimension ik in the tensor dimensions associated with the two input tensors remains consistent, rather than "kihp, iko -> hpo", where the contraction dimensions are continuous but the order is inconsistent.
[0115] After arranging the dimensions, the string represented by formula (8) is adjusted to:
[0116] a 𝛾1 a 𝛾2 …a 𝛾NAleft a 𝛿1 a 𝛿2 ... a𝛿𝑁contract π, b 𝛿1 … b 𝛿𝑁contract π b 𝛾1 b 𝛾2 …b 𝛾NBleft µ -> c 𝛾1 c 𝛾2 …c 𝛾NC µ (13)
[0117] where 𝛾 i Represents the remaining dimensions of the tensor, N Aleft Represents a large real number tensor A r The number of remaining dimensions, N Bleft Represents a small real number tensor B r The number of remaining dimensions, a 𝛿1 a 𝛿2 ... a 𝛿𝑁contract with b 𝛿1 … b 𝛿𝑁contract The same is the contraction of dimensions.
[0118] Then, the large real number tensor and the small real number tensor are permuted according to the permutation vector associated with the dimensional arrangement in the string. In some embodiments, the computation scheduling component 130 can be used to query the index position of each character in the sorted string in the updated string to obtain the permutation vector. For example, in a computing device, the sorted string can be traversed, and the index position of each character in the dimensional order in the original updated string can be sequentially queried to obtain the permutation vector of each tensor. Finally, the real number tensor A is respectively permuted according to the permutation vector. r and B r Perform dimension permutation to obtain the permuted large real number tensor A SORT and the permuted small real tensor B SORT At this time, tensor A SORT and B SORT Make the video memory layout suitable for matrix multiplication.
[0119] The above dimension arrangement and tensor permutation process ensures the continuity and sequential consistency of the contraction dimensions, so that it can be efficiently mapped to matrix multiplication operations.
[0120] Figure 4This is a schematic diagram of dimensional permutation of a tensor according to some embodiments of the present invention. As shown in the figure, for example, a permutation vector for a large real number tensor is obtained according to the change in dimensional order from ikhp to hpik. The original dimensional order is i(0), k(1), h(2), p(3). After permutation, the dimensional order is h(2), p(3), i(0), k(1). Therefore, according to the change in dimensional order, the index position of the permuted dimension in the original dimensional order can be obtained, thereby obtaining a permutation vector. For a 2x2x2x2 four-dimensional large real number tensor A1 with a tensor dimensional order of ikhp, after applying the permutation vector to the large real number tensor A1, a permuted large real number tensor A2 can be obtained. The arrows show the corresponding relationship between some data elements in tensors A1 and A2.
[0121] The permutation vector for the small real tensor is obtained by changing the dimension order from koi to iko. The original dimension order is k(0), o(1), i(2). After permutation, the dimension order is i(2), k(0), o(1). Therefore, according to the change in dimension order, the index position of the permuted dimension in the original dimension order can be obtained, thereby obtaining the permutation vector. For a 2x2x2 three-dimensional small real tensor B1 with a tensor dimension order of koi, after applying the permutation vector to the small real tensor B1, the permuted small real tensor B2 can be obtained. The arrows show the correspondence between some data elements between tensors B1 and B2.
[0122] Although Figure 4 Only the dimension permutation operation of a large real number tensor of dimension 2x2x2x2 and a small real number tensor of dimension 2x2x2 is shown, but the scope of the present invention is not limited to this, and the dimension permutation operation can be performed on large real number tensors and small real number tensors of other dimensions according to the same principle.
[0123] The following describes the real matrix multiplication operation.
[0124] At a fourth block 205 , the computation scheduling component 130 causes the graphics processing unit 120 to compute a real matrix multiplication operation associated with the permuted large real tensor, the permuted small real tensor, and the updated string.
[0125] In some embodiments, the computation scheduling component 130 may be configured to cause the graphics processing unit to compute a real matrix multiplication operation using the permuted large real tensor as a first real matrix and the permuted small real tensor as a second real matrix, wherein a first dimension of the first real matrix corresponds to a remaining dimension of the permuted large real tensor relative to a contraction dimension, and a second dimension of the first real matrix corresponds to a contraction dimension of the permuted large real tensor. wherein a first dimension of the second real matrix corresponds to a contraction dimension of the permuted small real tensor, and a second dimension of the second real matrix corresponds to a remaining dimension of the permuted small real tensor relative to a contraction dimension.
[0126] Since the permuted large real tensor A SORT The second half of the permuted small real number tensor B SORT The first half of are all contracted dimensions, where the order of dimensions is consistent, so they can be flattened into one dimension, whose dimension is k. Then, the permuted large real number tensor A SORT The first half (i.e., the remaining dimension) is flattened into one dimension, whose dimension is m. SORT The second half of (i.e., the remaining dimensions) is flattened into one dimension, whose dimension is n.
[0127] Therefore, the permuted large real tensor A SORT The first real matrix A can be considered as a two-dimensional 2D , the first real matrix A 2D The first dimension m corresponds to the permuted large real tensor A SORT The remaining dimension relative to the contraction dimension, the second dimension k of the first real matrix corresponds to the permuted large real tensor A SORT The contracted dimension, that is, the first real matrix A 2D is a two-dimensional matrix of dimension m*k.
[0128] permuted small real number tensor B SORT The second real matrix B can be considered as a two-dimensional 2D , the second real matrix B 2D The first dimension corresponds to the permuted small real tensor B SORT The contraction dimension k, the second real matrix B 2D The second dimension corresponds to the permuted small real number tensor B SORT The remaining dimension n relative to the contraction dimension, that is, the second real number matrix B 2D is a two-dimensional matrix of dimension k*n.
[0129] Correspondingly, the dimensions of the output tensor C come from the permuted large real number tensor A SORT The first half of the permuted small real number tensor B SORT Therefore, the output real number matrix corresponding to the output tensor C is a two-dimensional matrix of dimension m*n.
[0130] Therefore, the string represented by formula (13) can be simplified as follows:
[0131] mk,kn->mn (14)
[0132] In a computing device, the conversion between a multi-dimensional view and a one-dimensional view only involves a change in interpretation, that is, instructing the computing device on how to parse bit values without actually performing calculations or making video memory copies.
[0133] Therefore, the GPU 120 can calculate the first real number matrix A 2D , the second real matrix B 2D Real matrix multiplication associated with the simplified string.
[0134] Therefore, embodiments of the present invention divide the dimensions involved in the operation into contracted dimensions and remaining dimensions. Through dimensional fusion, high-dimensional tensor operations are converted into standard two-dimensional matrix multiplications. This fully utilizes the matrix acceleration units in the computing hardware while fully preserving the mathematical meaning of the original operation, maximizing computational efficiency. This converted two-dimensional matrix multiplication is suitable for matrix multiplication acceleration units on various hardware platforms, achieving a leap in versatility.
[0135] Therefore, the method for performing complex Einstein summation operations on a computing device proposed in an embodiment of the present invention converts complex operations that are not originally supported by the hardware into real matrix multiplication that is natively supported by the hardware through a complex number and real number conversion mechanism, thereby significantly improving the efficiency of general computing devices in performing complex Einstein summation operations, reducing the video memory capacity required by the computing device, and realizing the saving of computing resources.
[0136] In some embodiments, the first complex tensor may be stored in a first region of the video memory 122, and the second complex tensor may be stored in a second region of the video memory 122. A first pointer associated with the first complex tensor may be set to point to the first region, and a second pointer associated with the second complex tensor may be set to point to the second region.
[0137] In some embodiments, additionally, at block 5 206 , in response to the result of the comparison indicating that the capacity of the first region is less than the capacity of the second region, the computation scheduling component 130 may further be configured to: cause the first pointer to point to the second region, cause the second pointer to point to the first region, and swap positions of the first tensor dimension and the second tensor dimension in the string, wherein the region pointed to by the first pointer is associated with the large complex tensor, and the region pointed to by the second pointer is associated with the small complex tensor.
[0138] After determining that the video memory space occupied by the first complex tensor A is smaller than the video memory space occupied by the second complex tensor B, the first complex tensor A is designated as a small complex tensor, and the second complex tensor B is designated as a large complex tensor. To ensure that only the small complex tensor is expanded and decoupled during the decoupling step, the first complex tensor (small complex tensor) A and the second complex tensor (large complex tensor) B are swapped. Specifically, the first pointer pointing to the first region storing the first complex tensor (small complex tensor) A is swapped with the second pointer pointing to the second region storing the second complex tensor (large complex tensor) B. No actual tensor copies are made in video memory.
[0139] Accordingly, the positions of the first tensor dimension associated with the first complex tensor (small complex tensor) A and the second tensor dimension associated with the second complex tensor (large complex tensor) B in the string are swapped, so that the string represented by formula (3) is updated as follows:
[0140] b1b2…b 𝑁𝐵 , a1a2…a 𝑁𝐴 -> c1c2…c NC (15)
[0141] By exchanging pointers with strings, it is ensured that the first tensor dimension in the string always corresponds to the large complex tensor, and the second tensor dimension in the string always corresponds to the small complex tensor.
[0142] Therefore, by comparing the memory usage of input tensors, the embodiments of the present invention dynamically select the input tensor with the smallest footprint for expansion and decoupling, ensuring that the smaller tensor is always used as the operand for introducing auxiliary dimensions. This significantly reduces memory requirements and eliminates unnecessary data transfer operations. This optimization reduces memory copy overhead by up to 90% and increases computation speed by up to 50%.
[0143] In some embodiments, in addition, at block 6 207 , the computation scheduling component 130 may further be configured to detect a reduction dimension in the string, and determine a reduced first complex tensor, a reduced second complex tensor, and a reduced string based on the reduction dimension. The comparison of the occupied video memory space is performed based on the video memory space occupied by the reduced first complex tensor and the video memory space occupied by the reduced second complex tensor. The updated string is determined based on the reduced string.
[0144] Based on the definition of the reduction dimension mentioned above, the computation performs sum operations along the reduction dimension for a single input tensor. The distribution ratio shows that eliminating the reduction dimension in the input tensor is mathematically equivalent to eliminating the reduction dimension in the output tensor. For tensor reduction scenarios where the reduction dimension exists, such as quantum chemistry calculations and attention mechanisms in deep learning, the reduction dimension is often large. To save video memory and computation time, if the reduction dimension exists, pre-adding the input tensor can be used to eliminate the reduction dimension in the input tensor.
[0145] For the first complex tensor A and the second complex tensor B, the reduction dimension can be identified from the string. Assuming that the first complex tensor A and the second complex tensor B both have the reduction dimension, the following calculation is performed for the first complex tensor A and the second complex tensor B:
[0146] A rdc = Σε1…Σε NArdc A a1a2…aNA (16)
[0147] B rdc = Σε1…Σε NBrdc B b1b2…bNB (17)
[0148] Among them, A rdc represents the first complex tensor after reduction, ε1 to ε NArdc Indicates the reduced dimension of the first complex tensor A, NA rdc Indicates the number of reduced dimensions of the first complex tensor A, B rdc represents the reduced second complex tensor, ε1 to ε NBrdc Represents the reduced dimension of the second complex tensor B, NB rdc Indicates the number of reduced dimensions of the second complex tensor B.
[0149] Figure 5 is a schematic diagram of reducing a tensor according to some embodiments of the present invention.
[0150] Taking NA = 3 as an example, the number of elements in each dimension is 2, where the second dimension a2 is the reduction dimension, and the first complex tensor A is summed along dimension a2. After reduction, there are only two dimensions, and the number of elements in each dimension is 2. The calculation process for each data element is as follows:
[0151] A rdc a11, a21 = A a11, a21, a31 + A a11, a22, a31
[0152] A rdc a12, a21 = A a12, a21, a31 + A a12, a22, a31
[0153] A rdc a11, a22 = A a11, a21, a32 + A a11, a22, a32
[0154] A rdc a12, a22 = A a12, a21, a32 + A a12, a22, a32
[0155] The first complex tensor A after reduction rdc can be stored in the new GPU storage space, and the total number of elements after reduction is reduced from 8 to 4.
[0156] When the second complex tensor B has a reduction dimension, the second complex tensor B is summed with respect to the reduction dimension in a similar manner, which is not described in detail here.
[0157] After the reduction, the folded dimensions and the remaining dimensions can be expressed as:
[0158] 𝛿1,2,...,𝛿𝑁 contract = (a1,a2,...,a 𝑁𝐴-NArdc ) ∩ (b1,b2,...,b 𝑁𝐵-NBrdc ) (18)
[0159] 𝛾1,..., = (a1,...,a 𝑁𝐴-NArdc ) ∪ (b1,...,b 𝑁𝐵-NBrdc ) / (𝛿1,...,𝛿𝑁contract) (19)
[0160] The string represented by formula (3) can be replaced by the reduced string:
[0161] a1a2…a 𝑁𝐴-NArdc , b1b2…b 𝑁𝐵-NBrdc -> c1c2…c NC (20)
[0162] The corresponding summation formula is:
[0163] C = Σ 𝛿1 …Σ 𝛿𝑁contract A rdc a1a2…a𝑁𝐴-NArdc * B rdc b1b2…b𝑁-NBrdc (twenty one)
[0164] Although the above example uses the reduction of both the first complex tensor A and the second complex tensor B as an example, when only one of the complex tensors A or B has a reduction dimension, the reduction is performed only on the complex tensor with the reduction dimension.
[0165] After the reduction process is performed, the first complex tensor A can be reduced to rdc and the reduced second complex tensor B rdc To compare the occupied memory space, and the updated string obtained according to the decoupling can be updated based on the reduced string.
[0166] Therefore, the embodiments of the present invention reduce the input tensor to eliminate the reduction dimension in the input tensor, thereby saving video memory space and computing time, and further improving computing efficiency.
[0167] By utilizing real-virtual decoupling and auxiliary dimensional expansion methods, the embodiments of the present invention avoid the redundant computational mode of four real operations required by traditional solutions, achieving a significant improvement in complex number computation efficiency. Dimensional fusion and matrix conversion are achieved through dimensional permutation and tensor permutation, converting complex Einstein summations into real matrix multiplications. This approach is compatible with matrix multiplication accelerators on various hardware platforms (e.g., GPU tensor cores, TPU matrix units, etc.). Therefore, by converting complex Einstein summations into real matrix multiplications, the embodiments of the present invention fully utilize the high-performance computing units of general-purpose hardware, reducing the development complexity of hardware adaptation while maintaining computational efficiency. Even on platforms lacking dedicated Einstein summation hardware, the embodiments of the present invention can still maintain nearly 90% of peak computational performance, achieving a significant leap in versatility. The embodiments of the present invention optimize the complex Einstein summation in terms of hardware compatibility, computational efficiency, and video memory optimization, providing a high-performance solution for cutting-edge scientific computing fields such as quantum computing simulation and tensor network optimization.
[0168] According to another aspect of the present invention, a method for performing a complex Einstein summation operation on a computing device is provided.
[0169] Figures 6 and 7 is a flow chart of a first method for performing a complex Einstein summation operation on a computing device according to some embodiments of the present invention. The computing device includes a central processing unit and a graphics processing unit. The first method may be performed on Figure 1 The present invention is executed on the computing device 100 in the embodiment of the present invention, but the scope of the present invention is not limited in this regard.
[0170] The first method may include step S101: enabling a central processing unit to receive a character string, where the character string is used to indicate a complex Einstein summation operation including a first tensor dimension, a second tensor dimension, and an output dimension.
[0171] The first method may include step S102: enabling a graphics processing unit to receive a first complex tensor associated with a first tensor dimension and a second complex tensor associated with a second tensor dimension, and storing the first complex tensor and the second complex tensor in a video memory of the graphics processing unit.
[0172] The first method may include step S103: comparing the video memory space occupied by the first complex tensor with the video memory space occupied by the second complex tensor.
[0173] The first method may include step S104: determining a large complex tensor and a small complex tensor based on a result of the comparison.
[0174] The first method may include step S105: decoupling the real part and the imaginary part of each data element of the large complex tensor, thereby obtaining a large real tensor.
[0175] The first method may include step S106: expanding and decoupling the real part and the imaginary part of each data element of the small complex tensor into a real part, a negative imaginary part, an imaginary part, and a real part, thereby obtaining a small real tensor.
[0176] The first method may include step S107 of adding a first additional dimension to the tail of a tensor dimension associated with the large complex tensor in the string, adding a second additional dimension and the first additional dimension to the head and tail of a tensor dimension associated with the small complex tensor in the string, respectively, and adding the second additional dimension to the tail of an output dimension in the string, thereby obtaining an updated string, wherein the first additional dimension is associated with decoupling the real part and the imaginary part of each data element of the large complex tensor, and the first additional dimension and the second additional dimension are associated with expanding and decoupling the real part and the imaginary part of each data element of the small complex tensor.
[0177] The first method may include step S108: detecting a contraction dimension in the updated character string.
[0178] The first method may include step S109: arranging the dimensions associated with the contraction dimension in the updated character string together, thereby obtaining a sorted character string.
[0179] The first method may include step S110 : applying a permutation vector associated with the arrangement to the large real number tensor and the small real number tensor to obtain a permuted large real number tensor and a permuted small real number tensor.
[0180] The first method may include step S111 : causing a graphics processing unit to calculate a real matrix multiplication operation associated with the permuted large real number tensor, the permuted small real number tensor, and the updated string.
[0181] In some embodiments, the size of the first additional dimension and the second additional dimension may both be 2, the first position in the first additional dimension and the second additional dimension may be used to store the real part of the corresponding tensor, and the second position in the first additional dimension and the second additional dimension may be used to store the imaginary part of the corresponding tensor.
[0182] In some embodiments, step S110 may include: querying the index position of each character in the sorted character string in the updated character string to obtain a permutation vector.
[0183] In some embodiments, step S111 may include causing a graphics processing unit to calculate a real matrix multiplication operation using the permuted large real tensor as a first real matrix and the permuted small real tensor as a second real matrix, wherein a first dimension of the first real matrix corresponds to a remaining dimension of the permuted large real tensor relative to a contraction dimension, and a second dimension of the first real matrix corresponds to a contraction dimension of the permuted large real tensor. Furthermore, a first dimension of the second real matrix corresponds to a contraction dimension of the permuted small real tensor, and a second dimension of the second real matrix corresponds to a remaining dimension of the permuted small real tensor relative to the contraction dimension.
[0184] In some embodiments, each data element in the first complex tensor and the second complex tensor may include a half-precision real part and a half-precision imaginary part.
[0185] In some embodiments, the complex Einstein summation operation can be used for quantum computing or tensor network contraction.
[0186] Optionally, in some embodiments, the first method may further include step S112: detecting a reduction dimension in the character string.
[0187] In some embodiments, the second method may further include step S113: determining a reduced first complex tensor, a reduced second complex tensor, and a reduced string based on the reduction dimension. The comparison of video memory space occupied is performed based on the video memory space occupied by the reduced first complex tensor and the video memory space occupied by the reduced second complex tensor. Furthermore, the updated string is determined based on the reduced string.
[0188] After executing step S113, in step S103, the memory space occupied by the reduced first complex tensor is compared with the memory space occupied by the reduced second complex tensor. In step S107, the first additional dimension is added to the tail of the tensor dimension associated with the large complex tensor in the reduced string, the second additional dimension and the first additional dimension are added to the head and tail of the tensor dimension associated with the small complex tensor in the reduced string, respectively, and the second additional dimension is added to the tail of the output dimension in the reduced string, thereby obtaining an updated string.
[0189] Optionally, in some embodiments, the first complex tensor is stored in a first region of the video memory, and the second complex tensor is stored in a second region of the video memory. A first pointer associated with the first complex tensor is set to point to the first region, and a second pointer associated with the second complex tensor is set to point to the second region.
[0190] In some embodiments, in response to the comparison result indicating that the capacity of the first region is smaller than the capacity of the second region, the first method may further include step S114: making the first pointer point to the second region, and making the second pointer point to the first region.
[0191] In some embodiments, the first method may further include step S115: swapping the positions of the first tensor dimension and the second tensor dimension in the string, wherein the region pointed to by the first pointer is associated with the large complex tensor, and the region pointed to by the second pointer is associated with the small complex tensor.
[0192] After step S115, in step S105, the real part and the imaginary part of each data element of the large complex tensor pointed to by the first pointer are decoupled to obtain a large real tensor. In step S106, the real part and the imaginary part of each data element of the small complex tensor pointed to by the second pointer are expanded and decoupled into a real part, a negative imaginary part, an imaginary part and a real part, thereby obtaining a small real tensor. In step S107, the first additional dimension is added to the tail of the tensor dimension associated with the large complex tensor in the dimension-swapped string, the second additional dimension and the first additional dimension are added to the head and tail of the tensor dimension associated with the small complex tensor in the dimension-swapped string, respectively, and the second additional dimension is added to the tail of the output dimension in the dimension-swapped string, thereby obtaining an updated string.
[0193] Figure 8 is a flow chart of a first process associated with a first method for performing a complex Einstein summation operation on a computing device according to some embodiments of the present invention. Figure 1 is executed on the computing device 100 in Figures 6 and 7 The specific implementation of step S109 in the first method in FIG. 1 is described above, but the scope of the present invention is not limited thereto.
[0194] The first process may include step S1091 : arranging dimensions associated with the contraction dimension at the end of the tensor dimensions associated with the large real number tensor.
[0195] The first process may include step S1092: arranging the dimensions associated with the contraction dimensions in the tensor dimensions associated with the small real number tensor at the head in the same order as the tensor dimensions associated with the large real number tensor, thereby obtaining a sorted string.
[0196] According to another aspect of the present invention, a computer-readable storage medium is provided.
[0197] Figure 9 is a block diagram of a computer-readable storage medium 900 according to some embodiments of the present invention.
[0198] The computer readable storage medium 900 stores a computer program 950. When the computer program 950 is executed by the processor, the computer program 950 realizes the above combination Figure 6-Figure 8 The steps of each method or process are described.
[0199] According to another aspect of the present invention, a computer program product is provided.
[0200] Figure 10 is a block diagram of a computer program product 1000 according to some embodiments of the present invention.
[0201] The computer program product 1000 may include a computer program 950. When the computer program 950 is executed by a processor, the computer program 950 implements the above-mentioned Figure 6-Figure 8 The steps of each method or process are described.
[0202] The embodiments of the present invention have been described with reference to the accompanying drawings, which are intended to be illustrative rather than restrictive.
Claims
1. A computing device for performing a complex Einstein summation operation, characterized in that include: a central processing unit, used to perform general-purpose computations; A graphics processing unit, configured to perform high-performance general-purpose computing or graphics computing, wherein the graphics processing unit includes a video memory; as well as A computing scheduling component is coupled to the central processing unit and the graphics processing unit, and is configured to: causing the central processing unit to receive a character string indicating a complex Einstein summation operation including a first tensor dimension, a second tensor dimension, and an output dimension; causing the graphics processing unit to receive a first complex tensor associated with the first tensor dimension and a second complex tensor associated with the second tensor dimension, and to store the first complex tensor and the second complex tensor in the graphics memory; Comparing the video memory space occupied by the first complex tensor and the video memory space occupied by the second complex tensor; determining a large complex tensor and a small complex tensor based on a result of the comparison; Decoupling the real part and the imaginary part of each data element of the large complex tensor to obtain a large real tensor; Expanding and decoupling the real part and the imaginary part of each data element of the small complex tensor into a real part, a negative imaginary part, an imaginary part, and a real part, thereby obtaining a small real tensor; adding a first additional dimension to the end of the tensor dimension associated with the large complex tensor in the string, adding a second additional dimension and the first additional dimension to the beginning and end of the tensor dimension associated with the small complex tensor in the string, respectively, and adding the second additional dimension to the end of the output dimension in the string, thereby obtaining an updated string, wherein the first additional dimension is associated with the decoupling of the real part and the imaginary part of each data element of the large complex tensor, and the first additional dimension and the second additional dimension are associated with the expansion and decoupling of the real part and the imaginary part of each data element of the small complex tensor; detecting a contraction dimension in the updated character string; Arranging the dimensions in the updated character string that are associated with the contraction dimension together, thereby obtaining a sorted character string; applying a permutation vector associated with the arrangement to the large real number tensor and the small real number tensor to obtain a permuted large real number tensor and a permuted small real number tensor; and The graphics processing unit is caused to compute a real matrix multiplication operation associated with the permuted large real tensor, the permuted small real tensor, and the updated string.
2. The computing device of claim 1, wherein: The computing scheduling component is also used to: detecting a reduced dimension in the string; and determining a reduced first complex tensor, a reduced second complex tensor, and a reduced string based on the reduction dimension, The comparison of the occupied video memory space is performed based on the video memory space occupied by the reduced first complex tensor and the video memory space occupied by the reduced second complex tensor, and The updated character string is determined based on the reduced character string.
3. The computing device of claim 1, wherein: The first complex tensor is stored in a first region of the video memory, and the second complex tensor is stored in a second region of the video memory, a first pointer associated with the first complex tensor is set to point to the first region, and a second pointer associated with the second complex tensor is set to point to the second region, The computing and scheduling component is further configured to, in response to a result of the comparison indicating that the capacity of the first region is less than the capacity of the second region, perform the following steps: making the first pointer point to the second area, and making the second pointer point to the first area; and swapping the positions of the first tensor dimension and the second tensor dimension in the string, The area pointed to by the first pointer is associated with the large complex tensor, and the area pointed to by the second pointer is associated with the small complex tensor.
4. The computing device of claim 1, wherein: The sizes of the first additional dimension and the second additional dimension are both 2, the first position in the first additional dimension and the second additional dimension is used to store the real part of the corresponding tensor, and the second position in the first additional dimension and the second additional dimension is used to store the imaginary part of the corresponding tensor.
5. The computing device of claim 1, wherein: The computing scheduling component is used to: Arrange the dimensions associated with the contraction dimension at the end of the tensor dimensions associated with the large real number tensor; and Dimensions associated with the folded dimensions in the tensor dimensions associated with the small real number tensor are arranged at the head in the same order as the tensor dimensions associated with the large real number tensor, thereby obtaining the sorted string.
6. The computing device of claim 1, wherein: The computing scheduling component is used to: The index position of each character in the sorted character string in the updated character string is queried to obtain the permutation vector.
7. The computing device of claim 1, wherein: The computing scheduling component is used to: causing the graphics processing unit to calculate a real matrix multiplication operation using the permuted large real tensor as a first real matrix and the permuted small real tensor as a second real matrix, wherein the first dimension of the first real number matrix corresponds to the remaining dimension of the permuted large real number tensor relative to the contraction dimension, the second dimension of the first real number matrix corresponds to the contraction dimension of the permuted large real number tensor, and The first dimension of the second real number matrix corresponds to the contraction dimension of the permuted small real number tensor, and the second dimension of the second real number matrix corresponds to the remaining dimension of the permuted small real number tensor relative to the contraction dimension.
8. The computing device of claim 1, wherein: Each data element in the first complex tensor and the second complex tensor includes a half-precision real part and a half-precision imaginary part.
9. The computing device of claim 1, wherein: The complex Einstein summation operation is used for quantum computing or tensor network contraction.
10. A method for performing a complex Einstein summation operation on a computing device comprising a central processing unit and a graphics processing unit, characterized in that: The method comprises the following steps: S101: enabling the central processing unit to receive a character string, where the character string is used to indicate a complex Einstein summation operation including a first tensor dimension, a second tensor dimension, and an output dimension; S102: enabling the graphics processing unit to receive a first complex tensor associated with the first tensor dimension and a second complex tensor associated with the second tensor dimension, and storing the first complex tensor and the second complex tensor in a video memory of the graphics processing unit; S103: Compare the video memory space occupied by the first complex tensor and the video memory space occupied by the second complex tensor; S104: Determine a large complex tensor and a small complex tensor based on the comparison result; S105: Decoupling the real part and the imaginary part of each data element of the large complex tensor, thereby obtaining a large real tensor; S106: Expanding and decoupling the real part and the imaginary part of each data element of the small complex number tensor into a real part, a negative imaginary part, an imaginary part, and a real part, thereby obtaining a small real number tensor; S107: adding a first additional dimension to the tail of the tensor dimension associated with the large complex tensor in the string, adding a second additional dimension and the first additional dimension to the head and tail of the tensor dimension associated with the small complex tensor in the string, respectively, and adding the second additional dimension to the tail of the output dimension in the string, thereby obtaining an updated string, wherein the first additional dimension is associated with the decoupling of the real part and the imaginary part of each data element of the large complex tensor, and the first additional dimension and the second additional dimension are associated with the expansion and decoupling of the real part and the imaginary part of each data element of the small complex tensor; S108: Detecting the contraction dimension in the updated character string; S109: Arrange the dimensions in the updated character string that are associated with the contraction dimension, thereby obtaining a sorted character string; S110: applying a permutation vector associated with the arrangement to the large real number tensor and the small real number tensor to obtain a permuted large real number tensor and a permuted small real number tensor; and S111 : Activate the graphics processing unit to calculate a real matrix multiplication operation associated with the permuted large real number tensor, the permuted small real number tensor, and the updated character string.
11. The method according to claim 10, wherein The method further comprises the following steps: S112: Detecting the reduced dimension in the character string; and S113: Determine a reduced first complex tensor, a reduced second complex tensor, and a reduced string based on the reduction dimension. The comparison of the occupied video memory space is performed based on the video memory space occupied by the reduced first complex tensor and the video memory space occupied by the reduced second complex tensor, and The updated character string is determined based on the reduced character string.
12. The method according to claim 10, wherein The first complex tensor is stored in a first region of the video memory, and the second complex tensor is stored in a second region of the video memory, a first pointer associated with the first complex tensor is set to point to the first region, and a second pointer associated with the second complex tensor is set to point to the second region, In response to the comparison result indicating that the capacity of the first area is smaller than the capacity of the second area, the method further includes the following steps: S114: Make the first pointer point to the second area, and make the second pointer point to the first area; and S115: swapping the positions of the first tensor dimension and the second tensor dimension in the string, The area pointed to by the first pointer is associated with the large complex tensor, and the area pointed to by the second pointer is associated with the small complex tensor.
13. The method according to claim 10, wherein The sizes of the first additional dimension and the second additional dimension are both 2, the first position in the first additional dimension and the second additional dimension is used to store the real part of the corresponding tensor, and the second position in the first additional dimension and the second additional dimension is used to store the imaginary part of the corresponding tensor.
14. The method according to claim 10, wherein The S109 includes: S1091: Arrange the dimensions associated with the contraction dimension in the tensor dimensions associated with the large real number tensor at the end; and S1092: Arrange the dimensions associated with the condensed dimensions in the tensor dimensions associated with the small real number tensor at the head in the same order as the tensor dimensions associated with the large real number tensor, thereby obtaining the sorted string.
15. The method according to claim 10, wherein The S110 includes: The index position of each character in the sorted character string in the updated character string is queried to obtain the permutation vector.
16. The method according to claim 10, wherein The S111 includes: causing the graphics processing unit to calculate a real matrix multiplication operation using the permuted large real tensor as a first real matrix and the permuted small real tensor as a second real matrix, wherein the first dimension of the first real number matrix corresponds to the remaining dimension of the permuted large real number tensor relative to the contraction dimension, the second dimension of the first real number matrix corresponds to the contraction dimension of the permuted large real number tensor, and The first dimension of the second real number matrix corresponds to the contraction dimension of the permuted small real number tensor, and the second dimension of the second real number matrix corresponds to the remaining dimension of the permuted small real number tensor relative to the contraction dimension.
17. The method according to claim 10, wherein Each data element in the first complex tensor and the second complex tensor includes a half-precision real part and a half-precision imaginary part.
18. The method according to claim 10, wherein The complex Einstein summation operation is used for quantum computing or tensor network contraction.
19. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 10 to 18 are implemented.
20. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 10 to 18 are implemented.
Citation Information
Patent Citations
Native tensor processor
CN108875956A
Representation and fusion method of tensor reference operator in deep learning compiler
CN115186813A