A method and system for template-based implementation and configuration generation of binary broadcast operators

By acquiring the data type information and physical layout attributes of tensors, identifying broadcast patterns, and selecting appropriate broadcast paths for data processing, the inefficiency and poor versatility of existing binary broadcast operator implementations are solved, thereby improving the performance and throughput of the operator.

CN123180246APending Publication Date: 2026-11-27ZHONGHAO XINYING (HANGZHOU) TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611688037.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-10-29
Publication Date
2026-11-27

AI Technical Summary

Technical Problem

Existing implementations of binary broadcast operators suffer from problems such as high address computation overhead, low memory access efficiency, poor versatility, and high maintenance costs, making it difficult to fully utilize the vectorization and parallelization capabilities of modern processors.

Method used

By obtaining the data type information of the input tensor, calculating the physical layout attributes and operational domain attributes, identifying the broadcast pattern, selecting the broadcast path, managing on-chip storage space and moving data, performing binary operations and packaging the results.

Benefits of technology

It improves the execution performance of binary broadcast operators, reduces memory overhead and bus data transfer overhead, increases computational throughput, adapts to different broadcast scenarios, and reduces performance loss in general scenarios.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123180246A_ABST
    Figure CN123180246A_ABST
Patent Text Reader

Abstract

The application discloses a template implementation and configuration generation method and system of a binary broadcast operator, and the method comprises the following steps: identifying the trailing padding gap of a tensor according to the physical layout attribute of a first input tensor and a second input tensor and the physical layout attribute of an output tensor; identifying the broadcast form according to the physical layout attribute of the first input tensor, the second input tensor and the output tensor, and selecting a broadcast path; analyzing the output tensor of each dimension according to the broadcast path; carrying the first input tensor and the second input tensor from an external memory to an on-chip memory according to the broadcast path, loading data into a register in the on-chip memory to perform binary operation to obtain an operation result, and packing the operation result according to the physical layout attribute of the output tensor and writing the operation result back to the external memory. The binary operation is used to realize the whole-process processing of tensor data carrying, register operation, result packing and data writing back of the broadcast path, the bus data transmission overhead is reduced, and the overall operation throughput speed is effectively improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of binary broadcast operators, and more particularly to a templated implementation and configuration generation method and system for binary broadcast operators. Background Technology

[0002] With the rapid development of deep learning and high-performance computing technologies, tensor operations have become a core computational mode for neural network training and inference. Among various tensor operations, the binary broadcast operator is a fundamental and frequently used operator, with typical forms including addition, subtraction, multiplication, division, maximum value extraction, and minimum value extraction. A common characteristic of these operators is that the two input tensors are not identical in shape. A broadcasting mechanism is needed to expand the smaller tensor along a specific dimension to align it with the larger tensor before performing the binary operation element-wise and outputting the result tensor.

[0003] The broadcast mechanism was first proposed and widely adopted by scientific computing libraries such as NumPy, and was subsequently inherited by mainstream deep learning frameworks such as PyTorch, TensorFlow, and PaddlePaddle. Broadcasting allows element-wise operations between tensors of different shapes without explicitly copying data, thus simplifying operator calls and improving expressive flexibility at the semantic level. However, at the underlying implementation level, broadcast operators often face complex address mapping, dimension alignment, and data transport issues, and their implementation quality directly affects the operator's execution performance and memory overhead.

[0004] Research revealed a scalar implementation based on general nested loops. This approach calculates the address of the corresponding input element for each element of the output tensor before performing scalar operations. Its advantages include simplicity and versatility, but its disadvantages include high address computation overhead, low memory access efficiency, and difficulty in fully utilizing the vectorization and parallelization capabilities of modern processors.

[0005] Secondly, there's the implementation based on Broadcast Expansion. This approach first logically or physically expands the smaller input tensor into the same shape as the output tensor before performing element-wise operations. While logical expansion avoids actual data copying, it still requires repeated address mapping for the broadcast dimension during each memory access. Physical expansion incurs additional storage and data transfer overhead, especially when the broadcast dimension is large, significantly increasing memory usage and bandwidth consumption. Simultaneously, there are dedicated implementations based on specific broadcast patterns. These methods write dedicated kernels for specific cases such as scalar broadcasting, continuous high-dimensional broadcasting, and zero-dimensional broadcasting to achieve higher execution efficiency. However, their versatility is poor; when the shape, dimension, or broadcast mode of the input tensor changes, the kernel often needs to be rewritten or recompiled, making it difficult to cover all broadcast scenarios and incurring high maintenance costs. Summary of the Invention

[0006] The purpose of this invention is to provide a templated implementation and configuration generation method and system for binary broadcast operators, which solves the above-mentioned technical problems pointed out in the prior art.

[0007] This invention provides a templated implementation and configuration generation method for a binary broadcast operator, the specific operation method including: Obtain the data type information of the first input tensor, the second input tensor, and the output tensor; determine the physical layout attributes and computation domain attributes of the first input tensor, the second input tensor, and the output tensor based on the data type information; Based on the physical layout attributes of the first input tensor and the second input tensor and the physical layout attributes of the output tensor, calculate the physical address offset and physical length of the first input tensor and the second input tensor in the reference coordinate system; further identify the line end padding gap of the output tensor based on the physical address offset and physical length; Based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, calculate the physical capacity of each tensor; based on the physical capacities of the first input tensor, the second input tensor, and the physical capacity of the output tensor, calculate the remaining on-chip storage space, and reclaim the remaining on-chip storage space into the buffer of the output tensor to fill the line end padding gap of the output tensor; Based on the physical layout attributes of the first input tensor, the second input tensor, and the padded output tensor, the broadcast pattern is identified and the broadcast path is selected. Analyze the output tensors of each dimension based on the broadcast path; The first input tensor and the second input tensor are transported from external memory to on-chip memory according to the broadcast path. Data is loaded into registers in the on-chip memory, binary operations are performed to obtain the operation result, and the operation result is packaged according to the physical layout attributes of the output tensor and written back to the external memory.

[0008] Preferably, the physical layout attributes and computation domain attributes of the first input tensor, the second input tensor, and the output tensor are determined according to the data type information, and the specific operation steps are as follows: Based on the data type information, determine the physical layout attributes and computation domain attributes in the first input tensor and the second input tensor; The physical layout attributes include the number of bytes occupied by a single logical element, the alignment granularity in the zero-dimensional direction, the alignment requirements for transmission granularity during data movement, the loading step size, and the storage step size; the computation domain attributes include the register data packet type, the expansion method after data loading, the instruction interface involved in the calculation, and the method for packaging the calculation results. If the physical layout attribute of the first input tensor is a first preset byte width and the operation domain attribute is a floating-point type, then a floating-point data packet with a second preset byte width is loaded using the extended method of loading the first input tensor into a register, and floating-point operations are performed using the floating-point data packet; if the physical layout attribute of the first input tensor is a first preset byte width and the operation domain attribute is an integer type, then an integer data packet with a second preset byte width is loaded using the extended method of loading the first input tensor into a register, and integer operations are performed using the integer data packet; the internal operation type is executed according to the data type information of the output tensor; If the binary operation is a comparison operation and the data type information of the output tensor is Boolean, then the internal operation type is determined to be the common promotion type of the first input tensor and the second input tensor.

[0009] Preferably, based on the physical layout attributes of the first input tensor and the second input tensor and the physical layout attributes of the output tensor, the physical address offset and physical length of the first input tensor and the second input tensor in the reference coordinate system are calculated. The specific operation steps are as follows: Obtain the number of bytes occupied by a single logical element in the physical layout attributes of the first input tensor and the number of bytes occupied by a single logical element in the physical layout attributes of the output tensor, and calculate the ratio of the first byte width; use the coordinate space of the logical elements of the output tensor as the reference coordinate system; calculate the first physical address offset and the first physical length of the first input tensor in the reference coordinate system based on the ratio of the first byte width; Obtain the number of bytes occupied by a single logical element in the physical layout attribute of the second input tensor and the number of bytes occupied by a single logical element in the physical layout attribute of the output tensor, and calculate the ratio of the second byte width; The second physical address offset and second physical length of the second input tensor in the reference coordinate system are calculated based on the ratio of the second byte width. If the broadcast relationship of the first input tensor is scalar broadcast, the physical length of the first input tensor in the reference coordinate system is determined to be the number of physical bytes corresponding to a single logical element, which is used as the physical length of the first input tensor in the reference coordinate system.

[0010] Preferably, the line end padding gap of the output tensor is further identified based on the physical address offset and physical length. The specific operation steps are as follows: If the broadcast relationship of the first input tensor is nonscalar broadcast, then the physical length of the current processing batch of the first input tensor is calculated based on the ratio of the logical length of the current processing batch of the output tensor to the first byte width of the first input tensor and the output tensor; and the physical length of the three tensors after alignment in the zero dimension direction is calculated based on the dimension zero alignment granularity in the physical layout attributes of the first input tensor, the second input tensor, and the output tensor respectively. If the physical length of any tensor after alignment in the zero-dimensional direction is greater than the product of the zero-dimensional length of the tensor and the width of a single logical element byte, then the output tensor is determined to have end-of-line padding gaps.

[0011] Preferably, the physical capacity is calculated based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor. The specific steps are as follows: Determine the number of bytes occupied by a single logical element in the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, and the number of logical elements in the current processing batch, and calculate the sum of the physically occupied bytes; calculate the first physical capacity of the first input tensor, the second input tensor, and the output tensor according to the ratio of the physically occupied bytes of the first input tensor, the second input tensor, and the output tensor to the allocated available on-chip storage capacity; Obtain the hardware address granularity; align the first physical capacity of the first input tensor, second input tensor, and output tensor downwards according to the hardware address granularity to obtain the aligned physical capacity of the first input tensor, second input tensor, and output tensor; calculate the number of logical elements that the first input tensor, second input tensor, and output tensor can hold by calculating the aligned physical capacity of the first input tensor, second input tensor, and output tensor and the byte width of the corresponding single logical element; take the minimum number of logical elements that can be held as the logical length that can be processed in the current batch; align the logical length downwards to the computing unit granularity and determine the actual number of logical elements processed in the current batch; recalculate the second physical capacity of the first input tensor, second input tensor, and output tensor using the actual number of logical elements processed in the current batch.

[0012] Preferably, the physical capacity of the first input tensor and the second input tensor is used to store the physical capacity of the output tensor. The specific operation steps are as follows: The remaining on-chip storage space is calculated using the second physical capacity and the first physical capacity, and the remaining on-chip storage space is reclaimed for a buffer of the first physical capacity that is used to store the physical number of bytes occupied by the output tensor.

[0013] Preferably, based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, the broadcast pattern is identified, and the broadcast path is selected. The specific operation steps are as follows: Obtain the logical lengths of the first input tensor, the second input tensor, and the output tensor in each dimension; compare the logical lengths of the first input tensor and the output tensor dimension by dimension. If the length of the first input tensor in that dimension is equal to 1 and the length of the output tensor in that dimension is greater than 1, then broadcasting occurs in that dimension, forming a first dimension broadcast set; compare the logical lengths of the second input tensor and the output tensor dimension by dimension. If the length of the second input tensor in that dimension is equal to 1 and the length of the output tensor in that dimension is greater than 1, then broadcasting occurs in that dimension, forming a second dimension broadcast set; If the logical length of all dimensions of the first input tensor and the second input tensor is 1, then the first input tensor and the second input tensor are identified as scalar broadcasting, and a scalar broadcasting path is selected. A high-dimensional interval is preset. If the broadcasting dimension of the first input tensor and the second input tensor is within a continuous interval of the high-dimensional interval, and the dimensions outside the high-dimensional interval are consistent with the output tensor, then it is identified as continuous high-dimensional broadcasting, and a continuous high-dimensional broadcasting path is selected. If the logical length of the first input tensor and the second input tensor is 1 only in the zero-dimensional direction, and the other dimensions are consistent with the output tensor, then it is identified as zero-dimensional broadcasting, and a zero-dimensional broadcasting path is selected. If the logical length of all dimensions of the first input tensor and the second input tensor are consistent with the output tensor, then it is identified as no broadcasting. If the broadcast does not belong to any of the above forms, then it is identified as general broadcasting, and a general broadcasting path is selected.

[0014] Preferably, the output tensors of each dimension are analyzed based on the broadcast path, and the specific operation steps are as follows: If a continuous high-dimensional broadcast path is selected, the prefix dimension, broadcast dimension, and suffix dimension are divided according to the logical length of each dimension of the output tensor; the broadcast-side input tensor is obtained; the number of logical elements corresponding to the prefix dimension of the broadcast-side input tensor is loaded as resident data and stored in on-chip memory; the resident data is expanded in on-chip memory according to the logical length of the broadcast dimension; the non-broadcast-side input tensor is continuously loaded into on-chip memory according to row units; the number of rows that can be processed in the current batch is calculated based on the remaining on-chip storage capacity after the resident data is occupied; double buffers are allocated for the non-broadcast-side input tensor, the broadcast-side input tensor after the resident data is expanded, and the output tensor. If the logical length of the suffix dimension is greater than 1, then the range of the suffix dimension is determined, the output tensor is divided along the suffix dimension to form multiple output sub-intervals, and a computing core is allocated; if the logical length of the suffix dimension is equal to 1, then the range of the broadcast dimension is determined, the output tensor is divided along the broadcast dimension to form multiple output sub-intervals, and a computing core is allocated, and the resident data is reused locally on each computing core.

[0015] Preferably, the first input tensor and the second input tensor are transported from external memory to on-chip memory according to the broadcast path, and data is loaded into registers in the on-chip memory. The specific operation steps are as follows: Double buffers are allocated for the first input tensor, the second input tensor, and the output tensor. The double buffers for the first and second input tensors include a current buffer and a preload buffer. The double buffers for the output tensor include a current output buffer and an output multiplexing buffer. The preload buffer is used for the next batch of the first and second input tensors, and the output multiplexing buffer is used for the next batch of the output tensor. If the preload buffer and the output multiplexing buffer for the next batch are safe, then asynchronous preloading for the next batch is started.

[0016] Preferably, a binary operation is performed to obtain the operation result, and the operation result is packaged according to the physical layout attributes of the output tensor and written back to the external memory. The specific operation steps are as follows: The first and second input tensors of the current batch are loaded from the current buffer into a register to perform a binary operation, and the calculation result is written to the current output buffer of the output tensor. The current output buffer is asynchronously written back to external memory. After confirming that the write-back event of the current output buffer has been completed and that the preload buffer and output multiplexed buffer of the next batch are both in a safe state, the current buffer pointer and preload buffer pointer of each tensor are swapped. If the first and second input tensors are broadcast-side input tensors and the physical address of the next batch is the same as the physical address of the current batch, the data transfer of the broadcast-side input tensors is skipped, and only the buffer swapping and data transfer are performed on the non-broadcast-side input tensors and the output tensor. The operation result is transformed according to the physical layout attributes of the output tensor, the physical layout is output from the register data packet of the internal operation type, and written back to the buffer of the output tensor.

[0017] Compared with the prior art, the embodiments of the present invention have at least the following technical advantages: Analysis of the templated implementation and configuration generation method and system of the binary broadcast operator provided by the present invention shows that, in specific applications, it completes the reading and parsing of three types of tensor data type information, accurately distinguishes the logical data type, physical layout attribute and operation domain attribute of tensors, and clarifies the storage byte width and register expansion method of different data types. It can clarify the data conversion correspondence between input and output tensors, and also avoids operation format errors caused by data type confusion. Furthermore, based on the tensor physical layout attributes, the physical address offset and physical length in the reference coordinate system are calculated, the range of tensor row end filling gaps is accurately identified, the correspondence between logical elements and physical storage is clarified, the address calculation deviation caused by filling gaps is eliminated, and the tensor address resolution accuracy is improved. Furthermore, this invention combines three types of tensor physical layout attributes to complete the physical capacity calculation; then, by inputting the tensor physical capacity, it completes the output tensor line end gap filling process, aligns the number of logical elements with the physical storage byte capacity, eliminates invalid random data generated by line end blank bytes, and regularizes the output tensor physical storage structure. Furthermore, this invention identifies various broadcast patterns through tensor physical layout attributes, covering scalar broadcast, continuous high-dimensional broadcast, zero-dimensional broadcast, no broadcast, and general broadcast scenarios. It matches corresponding data processing paths for different broadcast patterns, abandons the processing method of adapting a single data path to all scenarios, and effectively reduces the unnecessary performance loss caused by general scenarios. Furthermore, this invention parses the output tensor dimension based on the selected broadcast path, divides it into prefix dimension, broadcast dimension, and suffix dimension, distinguishes between broadcast and non-broadcast input tensors, and completes on-chip storage space calculation, resident data processing, row cell partitioning, and double buffer allocation; then, it generates multiple output sub-intervals through dimension partitioning and matches them with corresponding computing cores to complete the reasonable allocation of on-chip storage resources and computing resources. Furthermore, this invention completes the entire process of tensor data transfer, register operation, result packaging, and data write-back through a matched broadcast path; it also adopts a double-buffered structure to achieve parallel execution of data preloading and operation, skips repeated data transfer for broadcast tensors with unchanged addresses, reduces bus data transmission overhead, and strictly follows the physical layout attributes of the output tensor to complete the packaging and write-back of operation results, ensuring the stable and efficient operation of the binary broadcast operator throughout the entire process and effectively improving the overall operation throughput speed. Attached Figure Description

[0018] Figure 1 This is a flowchart of the main steps of a templated implementation and configuration generation method for a binary broadcast operator in Example 1.

[0019] Figure 2 This is a flowchart illustrating the attributes of a templated implementation and configuration generation method for a binary broadcast operator in Example 1.

[0020] Figure 3 This is a flowchart illustrating the templated implementation and configuration generation method of a binary broadcast operator in Example 1.

[0021] Figure 4 This is a storage flowchart of a templated implementation and configuration generation method for a binary broadcast operator in Embodiment 1;

[0022] Figure 5 This is a flowchart illustrating the templated implementation and configuration generation method of a binary broadcast operator in Embodiment 1.

[0023] Figure 6This is a flowchart showing the final output of a templated implementation and configuration generation method for a binary broadcast operator in Example 1.

[0024] Figure 7 This is a flowchart of a templated implementation and configuration generation system for a binary broadcast operator in Embodiment 2;

[0025] Labels: Acquisition module 10; Analysis module 20; Identification module 30. Detailed Implementation

[0026] The technical solution of the present invention will now be clearly and completely described with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0027] The present invention will now be described in further detail with reference to specific embodiments and accompanying drawings.

[0028] Example 1 like Figure 1 As shown, this invention provides a templated implementation and configuration generation method for a binary broadcast operator, the specific operation method including:

[0029] S10: Obtain the data type information of the first input tensor, the second input tensor, and the output tensor; determine the physical layout attributes and computation domain attributes of the first input tensor, the second input tensor, and the output tensor respectively based on the data type information; It should be noted that in this step, both the first and second input tensors will output a data type information. Each data type information determines the logical data type, physical layout attribute, and computation domain attribute of the first and second input tensors. In other words, the physical layout attribute and computation domain attribute are the properties of the first and second input tensors, while the logical data type is the output type of the first and second input tensors. For example, the first input tensor is represented as logical type BF16, physical layout as 2 bytes, and computation domain extension as FP32 operation; the second input tensor is represented as logical type BF16, physical layout as 2 bytes, and computation domain extension as FP32 operation; and the output tensor is represented as logical type FP32, physical layout as 4 bytes, and computation domain as FP32 output packing (represented as output of the same type combination).

[0030] S20: Based on the physical layout attributes of the first input tensor and the second input tensor and the physical layout attributes of the output tensor, calculate the physical address offset and physical length of the first input tensor and the second input tensor in the reference coordinate system; further identify the line end padding gap of the output tensor based on the physical address offset and physical length; It should be noted that the above steps involve reading the physical layout attributes of the first input tensor, the second input tensor, and the output tensor to calculate the physical address offset and physical length of the first input tensor and the second input tensor in the reference coordinate system; then, the tensor row padding gap is identified by the physical address offset and physical length, and the actual range of the row padding gap is analyzed; finally, by clarifying the difference between logical elements and physical storage, the address calculation deviation caused by the row padding gap is eliminated, providing an address-level basis for subsequent physical capacity calculation.

[0031] S30: Calculate the physical capacity of each tensor based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor; calculate the remaining on-chip storage space based on the physical capacity of the first input tensor, the second input tensor, and the physical capacity of the output tensor; and reclaim the remaining on-chip storage space into the buffer of the output tensor to fill the line end padding gap of the output tensor. It should be noted that the above steps calculate the physical capacity by utilizing the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, and then combine the physical capacities of the first input tensor and the second input tensor to complete the physical capacity processing of the output tensor, thereby realizing the filling operation of the line end gaps; aligning the number of logical elements with the actual physical storage bytes, ensuring that the line end gaps of the output tensor are completely filled, avoiding invalid random data generated by unfilled bytes at the line end, and improving the spatial structure of the physical storage of the output tensor;

[0032] S40: Based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor (i.e., the output tensor is the padded output tensor), identify the broadcast pattern and select the broadcast path; It should be noted that the above steps read the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, complete the broadcast pattern recognition and select the corresponding broadcast path, distinguish between various cases such as scalar broadcast, continuous high-dimensional broadcast, zero-dimensional broadcast, no broadcast, and general broadcast, match their respective data paths for different broadcast patterns, avoid the performance loss caused by using a single path for all broadcast scenarios, and select an appropriate processing path for subsequent dimension parsing and on-chip transfer.

[0033] S50: Analyze the output tensors of each dimension based on the broadcast path; It should be noted that the above steps parse the output tensor according to the selected broadcast path, divide it into prefix dimension, broadcast dimension, and suffix dimension, identify the broadcast-side input tensor and the non-broadcast-side input tensor, calculate the available on-chip storage space, and complete resident data processing, row cell partitioning, and double buffer allocation. Then, the output sub-interval is divided and the computing core is allocated through the suffix dimension or broadcast dimension, completing on-chip resource planning and multi-core task partitioning, and preparing resources for data transfer from external memory to on-chip memory.

[0034] S60: Transport the first input tensor and the second input tensor from the external memory to the on-chip memory according to the broadcast path, load data into the register in the on-chip memory, perform binary operations to obtain the operation result, and pack the operation result according to the physical layout attributes of the output tensor and write it back to the external memory.

[0035] It should be noted that the above steps involve selecting a broadcast path to move the first and second input tensors from external memory to on-chip memory; then, the data in the on-chip memory is further loaded into registers, and binary operations are performed to obtain the results; the results are then packaged using the physical layout attributes of the output tensor, and the packaged data is written back to external memory. Double buffering is introduced to complete preloading and parallel computation. The batch physical address changes of the broadcast input tensors are identified, and repeated transport is skipped when the physical address remains unchanged. This completes the entire execution process of the binary broadcast operator, from data transport, register computation, result packaging to writing back to external memory.

[0036] like Figure 2 As shown, specifically, in step S10, the physical layout attributes and computation domain attributes of the first input tensor, the second input tensor, and the output tensor are determined according to the data type information. The specific operation steps are as follows:

[0037] S11: Determine the physical layout attributes (i.e., determining the physical layout attributes is to determine how the tensor is represented, aligned, moved, loaded, and stored at the physical storage level) and the operation domain attributes (i.e., after the data represented as a tensor is loaded into the register, what register data packet is used to represent it, how it is expanded from the physical layout into the operation domain, what instruction interface is used to calculate it, and how the calculation result is packaged back to the output layout) based on the data type information. The physical layout attributes include the number of bytes occupied by a single logical element (e.g., a single logical element of BOOL, UINT8, or INT8 occupies 1 byte; a single logical element of BF16 or INT16 occupies 2 bytes, etc.; note that although BF16 and INT16 both occupy 2 bytes, their subsequent operational domains are different, so only the physical width is determined here), and the alignment granularity in the zero-dimensional direction (i.e., the zero-dimensional direction is dim0; hardware usually requires dim0 (i.e., the 0th dimension of the tensor, which is usually referred to as the innermost dimension or row direction dimension) to be aligned with a certain granularity, such as aligned with N elements, a 1-byte layout might be aligned with 4N, and a 2-byte layout might be aligned with 2... N-alignment), data transfer granularity alignment requirements (i.e., when the data transfer engine moves data from external main memory to on-chip high-speed storage, it usually requires the transfer length to be aligned at the hardware granularity; this is expressed as the minimum length of the tensor transferred each time, whether the actual transfer length needs to be aligned upwards, and how to handle the non-aligned part), load step size and storage step size (i.e., the load step size refers to the physical address interval between adjacent logical elements when loading from on-chip high-speed storage to registers; while the storage step size refers to the physical address interval between adjacent logical elements when writing from registers back to on-chip high-speed storage; these two steps are related to physical layout width, dim0 alignment, and whether it is related to broadcasting).

[0038] The operational domain attributes include the register data packet type (i.e., determining which data packet to use after loading into the register based on the data type and operational requirements; such as expanding BF16 into an FP32 data packet after loading, and sign-expanding INT16 into an INT32 data packet after loading), the expansion method after data loading (i.e., expansion may be required after loading from the physical layout into the register; expansion methods include floating-point expansion, such as expanding BF16 into FP32, and integer sign expansion, such as expanding INT16 into INT32), the instruction interface involved in the calculation (i.e., determining which calculation interface to use based on the internal operation type; such as a 32-bit integer interface, an unsigned 32-bit integer interface, and a 64-bit integer interface), and the packaging method of the calculation result (i.e., indicating that after the calculation is completed, the result in the register is a data packet of the internal operation type; before writing back to the output tensor, it needs to be packaged according to the physical layout attributes of the output tensor; packaging methods include truncation (e.g., truncation of FP32 results into BF16), conversion (e.g., conversion of INT32 results into INT16), rearrangement (e.g., alignment according to the dim0 of the output layout and arrangement by the step size), and output data packet construction (constructing a register data packet that conforms to the output physical layout)).

[0039] It should be noted that the above steps parse the physical layout attributes and computation domain attributes of the first and second input tensors through data type information. The physical layout attributes describe the actual characteristics of the tensor at the hardware storage level; the computation domain attributes describe the processing behavior after the data is sent to the register. This step defines the physical layout attributes and computation domain attributes separately, separating the tensor storage characteristics and register operation characteristics. The physical layout attributes serve address calculation, block processing, data movement, and row padding judgment; the computation domain attributes serve data loading, data expansion, instruction interface selection, and operation result packaging, eliminating configuration errors caused by the mutual confusion between storage parameters and operation parameters.

[0040] S12: If the physical layout attribute of the first input tensor is a first preset byte width (e.g., 2 bytes, meaning whether the number of bytes occupied by a single logical element of the first input tensor is equal to the first preset byte width; let the first preset byte width be W1, with values ​​of 1 byte, 2 bytes, 4 bytes, or 8 bytes; the second preset byte width be W2, with values ​​of 4 bytes or 8 bytes), and the operation domain attribute is floating-point type (i.e., whether the register data packet type, extension method, and instruction interface in the operation domain attribute of the tensor belong to floating-point semantics), then the extension method setting for loading the first input tensor into the register is set to the second... A floating-point data packet with a preset byte width is used to perform floating-point operations (i.e., according to the physical layout attributes of the first input tensor, the data is loaded into a register, and the second preset byte width is set using the expansion method after the data is loaded into the register (e.g., 4 bytes, which means the expansion method is to expand the floating-point data of the first preset byte width into a floating-point data packet of the second preset byte width; for example, BF16 is 2 bytes, which is expanded into FP32, i.e., a 4-byte floating-point data packet). When performing binary operations subsequently, the first input tensor uses the expanded floating-point data packet to perform operations with the corresponding data packet of the second input tensor). If the physical layout attribute of the first input tensor is a first preset byte width, and the operation domain attribute is an integer type (i.e., determining whether the number of bytes occupied by a single logical element is equal to the first preset byte width, and determining whether the operation domain attribute of the tensor belongs to integer semantics), then the expansion method of loading the first input tensor into the register sets an integer data packet of a second preset byte width, and uses the integer data packet to perform integer operations (i.e., the physical layout attribute of the first input tensor is used to load the data into the register; the expansion method is to expand the integer data of the first preset byte width into an integer data packet of the second preset byte width, such as INT16 being 2 bytes, and sign-extending it to INT32, i.e., a 4-byte integer data packet; when performing binary operations subsequently, the first input tensor uses the expanded integer data packet).

[0041] The internal operation type is executed based on the data type information of the output tensor (i.e., if the output is FP32, the default internal operation type may be FP32; if the output is INT32, the default internal operation type may be INT32; if the output is INT64, the default internal operation type may be INT64; the derived internal operation type is used as the basis for subsequent calculation interface selection, that is, the corresponding calculation interface is called according to the internal operation type; if the operator explicitly specifies the internal operation type, the explicitly specified type shall prevail).

[0042] If the binary operation is a comparison operation (i.e., comparison operations include equal to, not equal to, greater than, less than, greater than or equal to, less than or equal to, etc.), and the data type information of the output tensor is Boolean, then the internal operation type is determined to be the common promotion type of the first input tensor and the second input tensor (i.e., the type to which the first input tensor and the second input tensor are jointly promoted during the operation; i.e., the internal operation type is deduced by default based on the data type of the output tensor; if it is a comparison operation and the output is Boolean, then the internal operation type is determined to be the common promotion type of the two input tensors, the comparison is performed on the common promotion type, and the Boolean result is packaged according to the output layout); for example, if the first input is INT32 and the second input is FP32, the common promotion type may be FP32; if the first input is BF16 and the second input is FP32, the common promotion type may be FP32. The common promotion type may be FP32, the first input is INT16, the second input is INT32, and the common promotion type may be INT32. The physical layout attributes mentioned above are used for address calculation, block partitioning, transport, and row padding judgment; the operation domain attributes are used for loading, expansion, computation interface selection, and result packaging; the internal operation type is used to select the specific computation interface, ultimately performing binary operations and writing back the output tensor; subsequently, comparison operations are performed on the common promotion type to obtain Boolean results; according to the physical layout attributes of the output tensor, the Boolean results are packaged into Boolean type data packets, where each Boolean logic element in the Boolean type data packet occupies 1 byte; according to the alignment granularity of the output tensor in the zero-dimensional direction, the packaged Boolean results are padded at the end of rows and written back to the output tensor's buffer;

[0043] It should be noted that the above steps use the first input tensor for branch determination; when the number of bytes occupied by a single logical element of the first input tensor is equal to the first preset byte width, the operation domain attribute belongs to floating-point semantics; the expansion method of loading the register is configured as a floating-point data packet with a second preset byte width; the original floating-point data is expanded before floating-point binary operations are performed; when the number of bytes occupied by a single logical element of the first input tensor is equal to the first preset byte width, the operation domain attribute belongs to integer semantics; the expansion method of loading the register is configured as an integer data packet with a second preset byte width; the original integer data is sign-extended before integer binary operations are performed; in the scenario of this invention, the internal operation type is derived from the data type information of the output tensor, and the derived internal operation type is used to select the corresponding calculation interface, operator band When there is an explicit configuration of the internal operation type, the internal operation type explicitly specified by the operator is directly adopted. In the case where the binary operation is a comparison operation and the output tensor data type is Boolean, the method of deriving the internal operation type from the output tensor is abandoned. Instead, the common promotion type of the first input tensor and the second input tensor is used as the internal operation type. The comparison operation is performed on the common promotion type. After obtaining the Boolean result, the result is packaged according to the physical layout of the output tensor. This step distinguishes between ordinary binary operation scenarios and special scenarios of comparing the output Boolean. It completes data expansion, internal operation type determination, and instruction interface selection. After performing the binary operation, the operation result is written back to the output tensor, reducing the probability of type promotion logic errors in scenarios of mixed data types and unifying the hardware register data packet transformation behavior.

[0044] like Figure 3 As shown, specifically, in step S20, based on the physical layout attributes of the first input tensor and the second input tensor and the physical layout attributes of the output tensor, the physical address offset and physical length of the first input tensor and the second input tensor in the reference coordinate system are calculated; based on the physical address offset and physical length, the line ending padding gap of the output tensor is further identified. The specific operation steps are as follows: S21: Obtain the number of bytes occupied by a single logical element in the physical layout attributes of the first input tensor (e.g., if the first input tensor is INT64, the byte width is 8; if the first input tensor is UINT8, the byte width is 1) and the number of bytes occupied by a single logical element in the physical layout attributes of the output tensor (e.g., if the output tensor is INT64, the byte width is 8; if the output tensor is UINT8, the byte width is 1), and calculate the ratio of the first byte widths (that is, one logical element of the first input tensor is physically equivalent to the physical width of several logical elements of the output tensor; or one logical element of the output tensor is physically equivalent to the physical width of several logical elements of the first input tensor; for example, if the first input is INT64 and the output is UINT8, the ratio is 8:1; if the first input is BF16 and the output is FP32, the ratio is 2:4, i.e., 1:2).

[0045] The logical element coordinate space of the output tensor is used as the reference coordinate system (i.e., the reference coordinate system is the logical element coordinate space of the output tensor; that is, a set of coordinate references is temporarily established based on the logical element coordinates of the output tensor, which is used to uniformly convert the physical addresses of the first input tensor and the second input tensor; the reference coordinate system is composed of the logical coordinates (d0, d1, ..., dD-1) of each dimension of the output tensor, where D is the total number of dimensions of the output tensor).

[0046] The first physical address offset of the first input tensor in the reference coordinate system is calculated based on the ratio of the first byte width. (That is, a logical position in the reference coordinate system corresponds to a byte starting address in the physical storage space of the first input tensor, and this byte starting address is the physical address offset. The calculation needs to consider the output logical coordinates, the byte width of the first input tensor, the byte width of the output tensor, and the physical layout rules of the first input tensor itself, such as dim0 alignment, transport alignment, etc.) and the first physical length (that is, the first physical length of the first input tensor in the reference coordinate system is calculated based on the ratio of the first byte width). The physical length in the coordinate system; the output tensor has a logical length for the current processing batch, which corresponds to the first input tensor. The actual physical byte length needs to be calculated. The calculation needs to consider the logical length of the current processing batch, the byte width ratio of the first input tensor to the output tensor, and the physical layout rules of the first input tensor itself (e.g., dim0 alignment, transport alignment, etc.). The two values ​​mentioned above are used by the data transport engine to move data from external main memory to the first input tensor, the load instruction to load data from on-chip high-speed storage, and to determine how much data the current batch of the first input tensor needs to process.

[0047] It should be noted that the above steps calculate the ratio of the first byte width by reading the number of bytes occupied by a single logical element within the physical layout attributes of the first input tensor and the output tensor. This ratio represents the proportional relationship between the physical width of a single logical element in the first input tensor and the physical width of a logical element in the output tensor. Using the coordinate space of the output tensor logical elements as the reference coordinate system, the first physical address offset and the first physical length are calculated using the ratio of the first byte widths. The conversion process incorporates the output logical coordinates, the byte width of the first input tensor, the byte width of the output tensor, and the physical layout rules of the first input tensor itself, including dim0 alignment. Then, the alignment constraints are transferred, and the resulting first physical address offset and first physical length are delivered to the data transfer engine and loading instructions for transferring the first input tensor data from external main memory to on-chip high-speed storage. The on-chip high-speed storage is then used to perform data loading to determine the total amount of data to be processed in the current batch. This step establishes a unified coordinate conversion benchmark, completes the address and length conversion between tensors with different byte widths, and eliminates address resolution errors caused by inconsistencies in the byte widths of different input and output tensors.

[0048] S22: Obtain the number of bytes occupied by a single logical element in the physical layout attribute of the second input tensor and the number of bytes occupied by a single logical element in the physical layout attribute of the output tensor, and calculate the ratio of the second byte width; Calculate the second physical address offset and the second physical length of the second input tensor in the reference coordinate system based on the ratio of the width of the second byte;

[0049] If the broadcast relationship of the first input tensor is scalar broadcast, then the physical length of the first input tensor in the reference coordinate system is determined to be the number of physical bytes corresponding to a single logical element, which is taken as the physical length of the first input tensor in the reference coordinate system (i.e., scalar broadcast is represented as all dimensions of the first input tensor having a length of 1; for example, the shape of the first input tensor is [1,1,1,1,1]; the shape of the output tensor is [8,16,32,64,128], in which case the first input tensor is scalar broadcast; if it is determined to be scalar broadcast, then the first input tensor only needs one logical element, because all dimensions are 1). Therefore, the entire first input tensor has only one logical element. Based on the physical layout properties of the first input tensor, the number of bytes occupied by a single logical element is determined. For example, if the first input tensor is FP32, a single logical element occupies 4 bytes; if the first input tensor is BF16, a single logical element occupies 2 bytes; if the first input tensor is INT64, a single logical element occupies 8 bytes. This number of physical bytes is then used as the physical length of the first input tensor in the reference coordinate system. Because during scalar broadcasting, the entire output range reuses the same logical element, the physical length of the first input tensor is simply the number of physical bytes occupied by a single logical element.

[0050] If the broadcast relationship of the first input tensor is nonscalar broadcast, then the physical length of the current processing batch of the first input tensor is calculated based on the ratio of the logical length of the current processing batch of the output tensor to the first byte width of the first input tensor and the output tensor (i.e., nonscalar broadcast in this step includes continuous high-dimensional broadcast, dim0-row broadcast, shape-merging broadcast, general broadcast, no broadcast, etc.); the logical length of the current processing batch of the output tensor is obtained. Since the current processing batch is determined by the data flow scheduling layer based on factors such as on-chip storage capacity and tile granularity, the current processing batch of the output tensor has a logical length, indicating how many logical elements to process in this batch; then, the physical length of the current processing batch of the first input tensor is calculated based on the ratio of the logical length of the current processing batch of the output tensor to the first byte width; the calculation requires... To determine the physical length of the current processing batch of the first input tensor, consider the logical length of the current batch, the ratio of the first byte width of the first input tensor to the first byte width of the output tensor, the dim0 alignment rule of the first input tensor itself, and the data transport alignment requirements (all of which are included in the physical layout properties and will not be elaborated further). This physical length is used by the data transport engine to transport the data of the current batch of the first input tensor, determine how much on-chip storage space the current batch of the first input tensor requires, and determine whether the current batch of the first input tensor matches the output batch. The difference between scalar broadcast and non-scalar broadcast in the above steps is that scalar broadcast indicates that the physical length is the number of physical bytes of a single logical element; non-scalar broadcast indicates that the physical length is calculated based on the ratio of the logical length and byte width of the current batch of output, avoiding unnecessary repeated transport and buffer expansion in scalar broadcast scenarios.

[0051] Based on the zero-dimensional alignment granularity (i.e., the alignment granularity of the 0th dimension) in the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, calculate the physical length of the three tensors after alignment in the zero-dimensional direction. Specifically, obtain the logical zero-dimensional length of the first input tensor, the second input tensor, and the output tensor. The logical dim0 length refers to the actual number of logical elements in the dim0 direction of the tensor. Calculate the physical length after alignment in the zero-dimensional direction based on the zero-dimensional alignment granularity and the logical zero-dimensional length. The aligned physical length may be greater than the logical dim0 length. If the logical dim0 length is not an integer multiple of the alignment granularity, upward alignment is required, resulting in padding (i.e., adding invalid data beyond the valid data). Padding occurs because the hardware has alignment requirements for data length. If the logical length of a certain dimension of the tensor does not meet the alignment granularity required by the hardware, some invalid data needs to be added in that dimension to make the physical length meet the alignment requirements. This added invalid data is the padding.

[0052] If the physical length of any tensor after alignment in the zero-dimensional direction is greater than the product of the logical dimension zero length and the width of a single logical element byte, then the output tensor is determined to have a line-end padding gap (i.e., taking the output tensor as an example, this means the output tensor has padding in the dim0 direction, i.e., there is a line-end padding gap; if the aligned physical length is less than or equal to the logical dim0 length, it means the output tensor has no padding in the dim0 direction), and the first input tensor, the second input tensor, and the output tensor are switched to a line-by-line path (that is, if any tensor has a line-end padding gap, it is forbidden to flatten the tensor into one dimension for processing; it must be switched to a line-by-line path). The specific operation is the logical dimension zero length. single-element byte Dimensional zero-alignment granularity Physical length after alignment ;like Then there is a line end fill gap;

[0053] If the aligned physical lengths are all less than or equal to the logical dimension zero of the tensor, then the first input tensor, the second input tensor, and the output tensor are determined to enter a continuous flattening path.

[0054] It should be noted that the above steps calculate the ratio of the second byte width by reading the number of bytes occupied by a single logical element within the physical layout attributes of the second input tensor and the output tensor; then, the second physical address offset and the second physical length are obtained by converting the second byte width ratio; then, the first input tensor is configured to distinguish between scalar broadcast and non-scalar broadcast. In the case of scalar broadcast, the number of physical bytes corresponding to a single logical element is directly used as the physical length of the first input tensor in the reference coordinate system; in the scalar broadcast scenario, the same logical element is reused in all output intervals, eliminating the need for length conversion based on the logical length of the output batch, thus avoiding unnecessary data duplication and unnecessary buffer expansion in the scalar broadcast scenario; in the case of non-scalar broadcast, non-scalar broadcast covers continuous high-dimensional broadcast; the logical length of the current processing batch of the output tensor is taken, combined with the ratio of the first byte width; at the same time, the dim0 alignment rule of the first input tensor itself and the data transport alignment requirements are substituted to calculate the physical length of the current processing batch of the first input tensor. The calculation result is used by the data transport engine to execute the data transport of this batch, evaluate the on-chip storage space required for this batch, and verify the output. The system checks if the input and output batches match. It then reads the zero-alignment granularity of the dimensions in the physical layout attributes of the first, second, and output tensors, and calculates the physical length of each tensor after zero-alignment. The logical `dim0` length represents the actual number of logical elements in the tensor's `dim0` direction. If the logical `dim0` length is not divisible by the alignment granularity, it performs an upward alignment operation, generating invalid padding data. If the physical length of any tensor after alignment is greater than the corresponding tensor's logical dimension zero length, it determines that the tensor has a row-end padding gap, and switches the first, second, and output tensors to a progressive path. It prohibits the tensor from being flattened into one dimension for processing. If the physical length of all tensors after alignment is less than or equal to the corresponding tensor's logical dimension zero length, it determines that the first, second, and output tensors enter a continuous flattening path. This step completes the broadcast type branching process, identifying row-end padding gaps in the `dim0` direction. Then, based on the existence of padding gaps, it selects either a progressive path or a continuous flattening path to reduce calculation errors caused by invalid row-end padding data participating in the calculation.

[0055] like Figure 4 As shown, specifically, in step S30, the physical capacity of each tensor is calculated based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor; the remaining on-chip storage space is calculated based on the physical capacities of the first and second input tensors and the physical capacity of the output tensor, and the remaining on-chip storage space is reclaimed into the buffer of the output tensor to fill the line ending gaps of the output tensor. The specific operation steps are as follows:

[0056] S31: Determine the number of bytes occupied by a single logical element in the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, and the number of logical elements in the current processing batch (i.e., determine the number of logical elements that the three tensors need to process in the current processing batch; when there is no broadcast, the number of logical elements of the three tensors is the same, and the number of logical elements is expressed as the total number of logical elements of the tensor or the number of logical elements in the current batch to be allocated), and calculate the sum of the physical bytes occupied (using the physical bytes occupied by the first input tensor, which is equal to its number of logical elements multiplied by the byte width of a single logical element; the physical bytes occupied by the second input tensor and the output tensor are calculated in the same way; then add them together to obtain the sum of the physical bytes occupied, which represents how many physical bytes of on-chip storage are needed if the three tensors process the same number of logical elements; for example, if the first input tensor is INT64 with a byte width of 8; the second input tensor is UINT8 with a byte width of 1; and the output tensor is INT64 with a byte width of 8; if the current batch needs to process the same number of logical elements, then the physical occupancy ratio of the three is first input: second input: output = 8:1). : 8, the ratio corresponding to the sum of the physical bytes occupied is 8 + 1 + 8 = 17).

[0057] The first physical capacity of the first input tensor, the second input tensor, and the output tensor is calculated based on the proportion of the physical bytes occupied by the first input tensor, the second input tensor, and the output tensor to the sum of the total number of bytes, and the allocated available on-chip storage capacity, respectively. This is equivalent to calculating the allocation ratio of the first input tensor (equal to the physical bytes occupied by the first input tensor divided by the sum of the physical bytes occupied by the three tensors); the allocation ratio of the second input tensor (equal to the physical bytes occupied by the second input tensor divided by the sum of the physical bytes occupied by the three tensors); and the allocation ratio of the output tensor (equal to the physical bytes occupied by the output tensor divided by the physical bytes occupied by the three tensors). The sum of the number of bytes; that is, the physical capacity of the first input tensor, the second input tensor, and the output tensor, which determines how many bytes can be stored; the physical capacity allocated to the first input tensor is equal to the available on-chip storage capacity multiplied by the allocation ratio of the first input tensor; the physical capacity of the second input tensor and the output tensor is calculated similarly). For example, if the available on-chip storage capacity is 1000 bytes, the first input is INT64 with a physical utilization ratio of 8 / 17, the second input is UINT8 with a physical utilization ratio of 1 / 17, and the output is INT64 with a physical utilization ratio of 8 / 17; then the physical capacity allocated to the first input is approximately 1000 × 8 / 17 bytes, the physical capacity allocated to the second input is approximately 1000 × 1 / 17 bytes, and the physical capacity allocated to the output is approximately 1000 × 8 / 17 bytes. In this way, although the number of logical elements is the same for all three, the physical capacity is allocated according to the width ratio to avoid insufficient wide types and waste of narrow types; where the first physical capacity is calculated as the number of logical elements in the current batch. Width of each tensor element in bytes Physical occupancy in bytes Then calculate the total usage. Finally, the first physical capacity is calculated. ;in, This represents the allocated available on-chip storage capacity;

[0058] Obtain the hardware address granularity (i.e., the hardware address granularity is the basic alignment requirement of the hardware for on-chip memory addresses and transport lengths; such as alignment by N elements, alignment by a fixed number of bytes, or alignment by the minimum transmission granularity of the data transport engine, preferably 16 bytes, 32 bytes, 64 bytes, or 128 bytes, which is read from the hardware specification parameters or determined by the address alignment requirements of the on-chip memory).

[0059] The first physical capacities of the first input tensor, the second input tensor, and the output tensor are aligned downwards according to the hardware address granularity (i.e., the first physical capacities of each tensor are aligned according to the hardware address granularity). Align down ;in, Represented as the aligned physical capacity, it is the actual usable buffer byte capacity of the i-th tensor after meeting hardware alignment requirements. The calculation result is at the hardware address granularity. Integer multiples of; This refers to the hardware address granularity, which represents the basic alignment requirements of the hardware for on-chip memory addresses and transport lengths (e.g., 16 bytes, 32 bytes, 64 bytes, or 128 bytes). This is expressed as an alignment factor, used to calculate how many complete hardware address granularities are included in the initial physical capacity; This represents the alignment multiple after rounding down, removing any fractions less than one. For the remainder, only retain the complete multiples; This is represented as restoring the byte capacity by multiplying the alignment multiple by the hardware address granularity to convert it back to the actual number of bytes. The aligned physical capacities of the first input tensor, the second input tensor, and the output tensor are obtained (i.e., the physical capacity allocated to the first input tensor is aligned downwards according to the hardware address granularity; downward alignment means that if the allocated physical capacity is not an integer multiple of the hardware address granularity, then it is taken as no more than its maximum integer multiple; the physical capacity allocated to the second input tensor is aligned downwards according to the hardware address granularity; the physical capacity allocated to the output tensor is aligned downwards according to the hardware address granularity, resulting in the aligned physical capacities of each of the three tensors; the aligned physical capacities may be less than or equal to the allocated physical capacities).

[0060] Calculate the physical capacity of the first input tensor, the second input tensor, and the output tensor after alignment, and the byte width of the corresponding single logical element, respectively, to obtain the number of logical elements that the first input tensor, the second input tensor, and the output tensor can hold (i.e., the formula for the number of logical elements that can be held is...). ,in The width of a single element in the tensor is given by the number of logical elements. The number of logical elements that the first input tensor can hold is equal to its aligned physical capacity divided by the width of a single logical element in bytes. The number of logical elements indicates how many logical elements the buffer of the first input tensor can hold at most. The number of logical elements that the second input tensor can hold is calculated similarly to the output tensor. These three numbers of logical elements may be different. For example, if the first input is INT64, the allocated physical capacity may only hold a few logical elements; if the second input is UINT8, the allocated physical capacity may hold a lot of logical elements; the output is INT64, and the allocated physical capacity is similar to that of the first input. This step converts the physical capacity back to the number of logical elements, so that the three tensors can be compared on the same logical dimension.

[0061] The minimum number of logical elements that can be accommodated is taken as the logical length that can be processed in the current batch (the logical length represents the maximum number of logical elements that can be processed in the current batch, and the buffers of the three tensors can all accommodate this number of logical elements).

[0062] The logic length is down-aligned to the computation unit granularity (i.e., this is a hardware constraint, meaning that a vector unit or a computation tile must process a fixed number of elements at a time; for example, if the computation unit granularity is 8, each computation must process multiples of 8, 16, 24, etc., and cannot process multiples of 7, 9, 13, etc., which are not multiples of 8). Down-alignment means taking the largest integer multiple of the computation unit granularity, provided it does not exceed the logic length that the current batch can process; that is, first checking how many complete computation unit granularities are in the current logic length (specifically, the designed computation unit granularity is...). The unit is the number of elements; The value should be a power of 2, preferably Or 64; The number of elements that can be processed is determined by calculating the number of elements in a single operation using a vector unit; the logical length that can be processed for the current batch is also determined. Align down to computational cell granularity ;in, This represents the actual number of logical elements processed in the current batch (in terms of element count). Any fractions less than a complete computational unit granularity are discarded, leaving only the actual number of logical elements that can be processed. The actual number of logical elements processed in the current batch is determined as follows: (e.g., logical length = 100, computational unit granularity = 8; first, check how many complete 8s are in 100. There are 8 × 1 = 8, 8 × 2 = 16, ..., 8 × 12 = 96, 8 × 13 = 104 complete 8s. 104 exceeds 100, so 13 cannot be used. The maximum possible integer multiple is 12. Then calculate the actual processing quantity: 8 × 12 = 96. Therefore, the actual number of logical elements processed in the current batch is 96. The remaining 4 logical elements (100 - 96 = 4) are not processed in this batch but are carried over to the next batch or processed through other methods.)

[0063] The second physical capacity of the first input tensor, the second input tensor, and the output tensor is recalculated based on the actual number of logical elements processed in the current batch (this second physical capacity is the capacity under the actual number of logical elements in the current batch, and the formula is...). );

[0064] It should be noted that the above steps calculate the physical byte count of each of the three tensors by reading the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, as well as the number of logical elements corresponding to the current processing batch. These physical byte counts are then summed. Next, the first physical capacity of each tensor is calculated by combining the proportion of the physical byte counts of the first, second, and output tensors to the total, along with the available on-chip storage capacity. Initial storage allocation is performed according to the byte width ratio, improving the situation of insufficient storage capacity for wide types and wasted storage resources for narrow types. Finally, the hardware address granularity is obtained, and the first physical capacity of each tensor is down-aligned according to the hardware address granularity to obtain the aligned physical capacity. The physical capacity is calculated by dividing the aligned physical capacity by the byte width of a single logical element of the corresponding tensor, thus determining the number of logical elements that the buffers of the first input tensor, second input tensor, and output tensor can hold. Then, the minimum value among the three sets of accommodating logical elements is selected as the logical length that the current batch can process. This logical length is then aligned down to the granularity of the computing unit, discarding any fractional parts that are less than a complete computing unit granularity, to determine the actual number of logical elements processed in the current batch. The second physical capacity of the first input tensor, second input tensor, and output tensor is then recalculated using this actual number of processed logical elements. This step sequentially completes the on-chip storage allocation by byte proportion, hardware address granularity alignment, buffer logical capacity conversion, and determines the actual batch processing scale under multiple hardware constraints, avoiding batch execution abnormalities caused by insufficient buffer capacity of a certain tensor.

[0065] S32: Calculate the remaining on-chip storage space using the second physical capacity and the first physical capacity (the formula for calculating the remaining on-chip storage space is...). The remaining on-chip storage space is reclaimed for a buffer of the first physical capacity (i.e., a buffer is a physical storage area; the physical capacity is the size of this buffer in bytes). Specifically, it is prioritized for output tensors, which typically need to be written back to external main memory, so their buffer requirements may be large. If the output tensor buffer is still insufficient, it can continue to be reclaimed for input tensors. If the output tensor is not needed, it is reclaimed for input tensors with a larger physical size, which are usually wide-type inputs, such as INT64 or FP64. The buffer capacities of the three tensors and the logic processing length of subsequent batches are updated. After reclamation, subsequent batches may be able to process more logic elements.

[0066] It should be noted that the above steps obtain the remaining on-chip storage space by comparing the second physical capacity with the first physical capacity. The remaining on-chip storage space is then reclaimed. The reclaimed storage space is prioritized for use in the buffer corresponding to the output tensor, which is responsible for writing data back to external main memory and thus has a greater buffer requirement. If the output tensor buffer resources are still insufficient, the reclaimed storage space is then used for the input tensor. If the output tensor does not require expansion, the reclaimed storage space is used for the input tensor with a larger physical size (typically a wide input type like INT64 or FP64). This updates the buffer capacity of the three tensors and simultaneously updates the logic processing length of subsequent batches. The reclaimed storage space allows subsequent batches to process more logic elements, fully utilizing the free bytes of on-chip storage and reducing on-chip storage resource idleness.

[0067] like Figure 5 As shown, specifically, in step S40, the broadcast pattern is identified and the broadcast path is selected based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor. The specific operation steps are as follows: S41: Obtain the logical lengths of the first input tensor, the second input tensor, and the output tensor in each dimension; compare the logical lengths of the first input tensor and the output tensor dimension by dimension. If the length of the first input tensor in that dimension is equal to 1 and the length of the output tensor in that dimension is greater than 1, then broadcasting occurs in that dimension to form the first dimension broadcast set.

[0068] Compare the logical lengths of the second input tensor and the output tensor dimension by dimension. If the length of the second input tensor in that dimension is equal to 1 and the length of the output tensor in that dimension is greater than 1, then that dimension is broadcast, forming a second dimension broadcast set.

[0069] If the logical length of all dimensions of the first input tensor and the second input tensor is 1, then the first input tensor and the second input tensor are identified as scalar broadcast form, and the scalar broadcast path is selected;

[0070] It should be noted that the above process involves traversing each dimension to obtain the logical lengths of the first input tensor, the second input tensor, and the output tensor; comparing the lengths of the input and output tensors dimension by dimension to collect the dimensions that generate broadcasts, thus obtaining the first dimension broadcast set and the second dimension broadcast set; if the logical length of all dimensions is equal to 1, the scalar broadcast pattern is determined and the scalar broadcast path is selected; this step completes the preliminary screening of broadcast dimensions, distinguishes scalar broadcast scenarios, and provides the dimension set input for subsequent discrimination of various broadcast patterns;

[0071] S42: Preset the range of the high-dimensional interval (i.e., the high-dimensional interval refers to the continuous interval from dim1 to dim4). If the broadcast dimensions of the first input tensor and the second input tensor (at least one) are within a continuous interval of the high-dimensional interval, and the dimensions outside the high-dimensional interval are consistent with the output tensor, then it is identified as a continuous high-dimensional broadcast pattern. A continuous high-dimensional broadcast path is selected (i.e., checking whether the broadcast dimensions of the first or second input tensor are concentrated within a continuous interval from dim1 to dim4; for example, if the first input tensor broadcasts in dim2, dim3, and dim4, these three dimensions are continuous, satisfying the continuous high-dimensional broadcast condition; then determining whether the dimensions outside this interval are consistent with the output tensor; continuous high-dimensional broadcast also requires that the dimensions before the broadcast interval are consistent with the output tensor, and the dimensions after the broadcast interval are consistent with the output tensor). The output tensors must be consistent; for example, prefix dimensions dim0 and dim1 must be consistent with the output, broadcast dimensions dim2, dim3, and dim4 must be broadcast, and suffix dimensions must be absent or consistent with the output. If these conditions are met, it is identified as a continuous high-dimensional broadcast pattern, and a continuous high-dimensional broadcast path is selected. The characteristic of a continuous high-dimensional broadcast path is that the tensor dimensions are divided into three parts: prefix dimension, broadcast dimension, and suffix dimension. The prefix data on the broadcast side is used as a resident block and loaded once within the processing range of the current processing batch or the current computing core. It is expanded and reused in on-chip storage according to the broadcast dimension. The non-broadcast side is used as streaming input and loaded continuously by row tiles (representing the logical data blocks that actually participate in the handling, loading, calculation, and writing back within a batch). After the calculation is completed, it is written back by row tiles in the same way.

[0072] It should be noted that the above steps define a high-dimensional interval range, determine whether the broadcast dimension falls within this continuous interval, and verify whether the dimensions outside the interval are consistent with the output tensor. If the conditions are met, the continuous high-dimensional broadcast pattern is determined and a continuous high-dimensional broadcast path is selected. This step splits the tensor dimension into prefix dimension, broadcast dimension, and suffix dimension. The broadcast-side prefix data is loaded only once in a calculation batch. Then, the on-chip storage is used to complete the data expansion and reuse of the broadcast dimension. Non-broadcast-side data is continuously transported and loaded in row tile form to participate in the calculation. After the calculation is completed, the results are also written back in row tile form. This adapts to business scenarios where the broadcast dimension is concentrated in a high-dimensional continuous interval, reducing the data transport overhead caused by repeated loading.

[0073] S43: If the first input tensor and the second input tensor have a logical length of 1 only in the zero-dimensional direction (i.e., only one of the first input tensor and the second input tensor has a logical length of 1 in the zero-dimensional direction), and the other dimensions are consistent with the output tensor, then it is identified as a zero-dimensional broadcast pattern, and a zero-dimensional broadcast path is selected (i.e., zero-dimensional is dim0; check if the length of the first input tensor or the second input tensor in the dim0 direction is 1, and the length of the output tensor in the dim0 direction is greater than 1; if so, broadcasting occurs in the dim0 direction; determine if other dimensions are consistent with the output tensor, check if the length of the first input tensor or the second input tensor in the dim1 to dim4 directions is consistent with the output tensor; if consistent, it means that only dim0 is broadcasting; if the conditions are met, it is identified as a zero-dimensional broadcast pattern, and the zero-dimensional broadcast path is selected, which means that only one element needs to be loaded per row on the broadcast side; copy it to all positions of the current dim0 tile in on-chip storage or registers, and process the input and output on the other side according to the continuous row layout).

[0074] It should be noted that the above check is to determine whether only dim0 is broadcast, and whether all other dimensions and output tensors are consistent. If the condition is met, the zero-dimensional broadcast pattern is determined and the zero-dimensional broadcast path is selected. In this scenario, only a single element is loaded in each row on the broadcast side. Then, it is copied and filled to all positions of the current dim0 tile in on-chip storage or register. On the other side, the input and output maintain a continuous row layout for read and write processing. This is specifically designed for the case where only the 0th dimension is broadcast, avoiding repeated loading of the same data element by element and reducing the frequency of data reading.

[0075] S44: If the logical lengths of all dimensions of the first input tensor and the second input tensor are consistent with the output tensor, then it is identified as a non-broadcast form (i.e., for each dimension, the length of that dimension of the first input tensor is equal to the length of that dimension of the output tensor; if all dimensions satisfy this condition, then the first input tensor has no broadcast; if all dimensions of the second input tensor also satisfy this condition, then the second input tensor has no broadcast; if neither input tensor has broadcast, then it is identified as a non-broadcast form; in the non-broadcast form, the three tensors have the same number of logical elements, but their physical occupancy may be different, and on-chip storage needs to be allocated in a weighted manner according to the physical layout width; if there is no row padding, a continuous flattening path can be entered; if there is row padding, a row-by-row path needs to be entered).

[0076] It should be noted that the above comparison involves all dimensions of logical length; when the lengths of each dimension of the two input tensors are consistent with those of the output tensor, it is determined to be a non-broadcast state; in this state, the number of logical elements of the three tensors is equal, but the physical bytes occupied can be different; on-chip storage space needs to be weighted and allocated based on the physical layout width; if there is no row padding, it switches to a continuous flattening path; if there is row padding, it switches to a row-by-row path to handle ordinary tensor operations without broadcasting, distinguishing between the presence and absence of row padding to select different data paths, and adapting to non-broadcast tensor calculations with inconsistent physical layouts;

[0077] S45: If the broadcast does not belong to any of the above forms, it is identified as a general broadcast form, and a general broadcast path is selected (i.e., it is determined whether the broadcast relationship does not belong to any of the following: scalar broadcast, continuous high-dimensional broadcast, zero-dimensional broadcast, or no broadcast; if none of them are included, it enters the general broadcast form; the general broadcast form includes broadcasting in multiple discontinuous dimensions, broadcasting in different dimensions of two input tensors, cross broadcast dimensions, and other complex shapes; the general broadcast path is represented as selecting the highest non-1 dimension in the output space as the initial splitting candidate, accumulating the physical occupancy of three tensors dimension by dimension from dim0 using the capacity planning function, determining the actual splitting dimension and the length of each batch, if the length of an input in the splitting dimension is 1, then the buffer of that input does not expand with the size of the output batch, achieving data reuse by setting stride to 0, traversing the outer dimension and dim0 tile within each batch, dim0 broadcasting is achieved by loading a single element and copying it to all positions within the tile, and broadcasting in other dimensions is achieved by setting stride to 0).

[0078] It should be noted that when the above steps determine that the scenario does not belong to any of the previous types, it is identified as a general broadcast pattern and a general broadcast path is selected. This pattern covers broadcast dimensions that are discontinuous, two input tensors with different broadcast dimensions, broadcast dimensions that overlap, and other complex broadcast shapes. Then, the highest non-1 dimension of the output space is selected as the initial splitting candidate, and the capacity planning function is called to accumulate the physical occupancy of three tensors dimension by dimension starting from dim0 to determine the actual splitting dimension and the processing length of each batch. If the length of a certain input is equal to 1 in the splitting dimension, the input buffer does not expand with the output batch size. Data reuse is achieved by setting the step size to 0. Each batch traverses the outer dimension and dim0tile. The dim0 broadcast is filled to all positions of the tile by loading a single element and copying it. The broadcasts of other dimensions are also reused by setting the step size to 0. This is compatible with various complex broadcast situations that cannot be classified into dedicated paths, and completes the batch splitting and data reuse processing in complex broadcast scenarios.

[0079] Specifically, in step S50, the output tensors of each dimension are analyzed according to the broadcast path. The specific operation steps are as follows: If a continuous high-dimensional broadcast path is selected, the output tensor is divided into prefix dimension, broadcast dimension, and suffix dimension according to the logical length of each dimension (that is, on the side where broadcasting occurs in the continuous high-dimensional interval, the dimensions of the output tensor are divided into three parts: prefix dimension, dimension before the broadcast dimension interval, broadcast dimension, continuous high-dimensional interval where broadcasting occurs, suffix dimension, and dimension after the broadcast dimension interval).

[0080] Obtain the broadcast-side input tensor (i.e., the broadcast-side input tensor refers to the side of the first or second input tensor from which the broadcast occurs in a continuous high-dimensional interval).

[0081] The number of logical elements corresponding to the broadcast input tensor in the prefix dimension (i.e., the data of the broadcast input tensor in the prefix dimension, which is the data that needs to be resident; because the length of the broadcast input tensor in the broadcast dimension is 1, each logical position of the broadcast input tensor in the prefix dimension corresponds to a logical element in the broadcast dimension) is loaded as resident data and stored in on-chip memory. The resident data is expanded in on-chip memory according to the logical length of the broadcast dimension (i.e., the resident data remains unchanged within the current processing batch, does not need to be repeatedly transferred between multiple batches, and can be expanded and reused in on-chip memory according to the broadcast dimension); specifically, the resident data refers to the logical element data corresponding to the broadcast input tensor in the prefix dimension; let the number of logical elements corresponding to the prefix dimension be... The unit is the number of elements; let the number of bytes occupied by a single logical element of the broadcast-side input tensor be... The unit is bytes / element; therefore, the on-chip storage capacity occupied by resident data is... The resident data remains unchanged within the current processing batch and does not need to be repeatedly transferred between multiple batches; in the on-chip memory, the resident data is stored according to the broadcast dimension of logical length. When expanded, the on-chip storage capacity occupied is... ;

[0082] The non-broadcast side (i.e., the non-broadcast input tensor refers to the input tensor on the other side, which has not undergone continuous high-dimensional broadcasting or has a different broadcasting relationship) is continuously loaded into on-chip memory according to row units (i.e., one or more rows in the dim0 direction are used as the basic transport unit; in continuous high-dimensional broadcasting paths, non-broadcast input is loaded continuously by row tiles). Based on the remaining on-chip memory capacity after resident data occupancy, the number of rows that can be processed in the current batch is calculated (i.e., resident data is the data corresponding to the broadcast side input tensor in the prefix dimension; the capacity occupied by resident data is equal to the number of logical elements of resident data multiplied by the byte width of the broadcast side input tensor; the expanded broadcast side input data). Multiple copies are made along the broadcast dimension; the expanded data capacity is equal to the number of logical elements in the resident data multiplied by the length of the broadcast dimension, and then multiplied by the byte width of the broadcast-side input tensor; the remaining on-chip storage capacity is obtained by subtracting the capacity occupied by the resident data and the expanded data from the total available on-chip storage capacity; the total double buffering usage for each row of non-broadcast input and output is determined, as both non-broadcast input and output require double buffering; the total double buffering usage for each row is equal to the physical occupancy of each row of non-broadcast input multiplied by 2, plus the physical occupancy of each row of output multiplied by 2; the number of rows that can be processed in the current batch is equal to the remaining on-chip storage capacity divided by the total double buffering usage per row).

[0083] Double buffers are allocated for the non-broadcast input tensor, the broadcast input tensor after the resident data is expanded, and the output tensor, respectively. (That is, double buffers mean that each tensor has a current buffer and a preload buffer. The current buffer is used for the calculation of the current batch, and the preload buffer is used for the preloading of the next batch.) The three types of data requiring double buffering are determined: non-broadcast input tensors, broadcast input tensors after the resident data is expanded, and output tensors. A current buffer and a preload buffer are allocated for each type of data. The current buffer for non-broadcast input is used for the calculation of the current batch, and the preload buffer is used for the streaming loading of the next batch. The current buffer for the expanded broadcast input is used for the calculation of the current batch, and the preload buffer is used for the expanded data of the next batch. The current buffer for the output tensor is used for the calculation results of the current batch, and the preload buffer is used for the output reuse of the next batch. It is ensured that all three types of data have sufficient capacity, and the buffer capacity of the three types of data needs to meet the number of rows that can be processed in the current batch.

[0084] If the logical length of the suffix dimension is greater than 1, then the range of the suffix dimension is determined, and the output tensor is divided along the suffix dimension to form multiple output sub-intervals. Computational cores are then assigned (i.e., the range of the suffix dimension is determined; the suffix dimension is the dimension following the broadcast dimension interval, such as an output tensor shape of [dim0, dim1, dim2, dim3, dim4], broadcast dimensions of dim2 and dim3, and suffix dimension of dim4; the output tensor is divided into multiple non-overlapping sub-intervals along the suffix dimension, and each sub-interval is assigned to a computational core. Each computational core processes its own output sub-interval; the resident data corresponding to the prefix dimension input on the broadcast side is reused locally in each computational core, because the suffix dimension division does not change the data in the prefix dimension and broadcast dimension, so the resident data can be reused locally in each core).

[0085] If the logical length of the suffix dimension is equal to 1, then the range of the broadcast dimension is determined, the output tensor is divided along the broadcast dimension to form multiple output sub-intervals, and computational cores are allocated. The resident data is reused locally in each computational core (i.e., resident data is reused locally in each computational core; the resident data corresponding to the prefix dimension input on the broadcast side is reused locally in each computational core, because the broadcast dimension division does not change the data in the prefix dimension, so resident data can be reused locally in each core; different computational cores each have non-overlapping output intervals to avoid write conflicts). Specifically, let the logical length of the output tensor in the suffix dimension be... The unit is the number of elements; let the number of available computational cores be... ; Divide the output tensor along the suffix dimension into The nth non-overlapping output sub-intervals; The logical length of each output sub-interval in the suffix dimension is: ;like Cannot be If the result is divisible, the remainder is distributed among the first few output sub-intervals; each output sub-interval is assigned a computational core; each computational core processes the logical elements within its respective output sub-interval and reuses the resident data corresponding to the prefix dimension; the remainder calculation is as follows: Distribute the remainders sequentially to the first few. ;

[0086] It should be noted that the above steps involve parsing the various dimensions of the output tensor by selecting the broadcast path; dividing the output tensor dimensions into prefix dimension, broadcast dimension, and suffix dimension; identifying the broadcast-side input tensor and non-broadcast-side input tensor in continuous high-dimensional broadcast scenarios; then extracting the number of logical elements corresponding to the prefix dimension of the broadcast-side input tensor as resident data and writing it into the on-chip memory; and completing the resident data expansion according to the logical length of the broadcast dimension within the on-chip memory; this part of the resident data remains unchanged within the same processing batch and does not need to be repeatedly transferred across batches, thus realizing the on-chip reuse of broadcast-side data;

[0087] Then, one or more rows in the dim0 direction are used as row units as the basic transport unit; continuous loading is performed on the non-broadcast input tensor, and the capacity occupied by the resident data itself and the capacity occupied by the expanded data are deducted to obtain the remaining on-chip storage capacity; combined with the total double buffering of each row of non-broadcast input and output, the number of rows that can be processed in the current batch is calculated, the on-chip storage capacity is calculated, the maximum processing scale of a single batch is limited, and on-chip storage overflow is prevented;

[0088] Double buffers are allocated for non-broadcast input tensors, broadcast input tensors after expansion of resident data, and output tensors. A current buffer and a preload buffer are set for each type of data. The current buffer is used for the current batch of calculations, and the preload buffer is used for the preloading of the next batch. Non-broadcast inputs are preloaded in a streaming manner. Broadcast inputs are expanded for the next batch of data, and output tensors are reserved with the results. The double buffering setting enables the calculation process and the data transfer process to be executed in parallel, improving the overall data throughput.

[0089] If the logical length of the suffix dimension is greater than 1, the range of the suffix dimension is locked, and the output tensor is split along the suffix dimension to obtain several output sub-intervals. Then, each output sub-interval is assigned to different computing cores. The resident data corresponding to the prefix dimension of the broadcast input can be reused locally in each computing core. The suffix dimension split will not change the data of the prefix dimension and the broadcast dimension, reducing the repeated loading of broadcast data in multi-core scenarios.

[0090] If the logical length of the suffix dimension is determined to be equal to 1, the broadcast dimension range is locked, and the output tensor is divided along the broadcast dimension to obtain several output sub-intervals. Then, each output sub-interval is allocated to different computing cores, and the resident data is reused locally on each computing core. Each computing core holds non-overlapping output intervals, avoiding the write conflict problem caused by multi-core parallel operation, and completing the division of output intervals and the distribution of multi-computing core tasks.

[0091] like Figure 6As shown, specifically, in step S60, the first input tensor and the second input tensor are transported from external memory to on-chip memory according to the broadcast path; data is loaded into registers in the on-chip memory; a binary operation is performed to obtain the operation result; the operation result is packaged according to the physical layout attributes of the output tensor and written back to the external memory. The specific operation steps are as follows:

[0092] S61: Allocate double buffers for the first input tensor, the second input tensor, and the output tensor respectively. The double buffers for the first input tensor and the second input tensor include a current buffer and a preloaded buffer. The double buffers for the output tensor include a current output buffer and an output multiplexing buffer.

[0093] A preload buffer is used for the next batch of the first input tensor and the second input tensor, and an output multiplexing buffer is used for the next batch of the output tensor;

[0094] If the preload buffer and output multiplexed buffer for the next batch are safe, then the asynchronous preloading for the next batch is initiated (i.e., confirming whether the next preload buffer is safe, which includes that the preload buffer is not currently being used by an ongoing computation or write-back operation, that the old data in the preload buffer has been consumed, and that it is safe to write the next batch of data; confirming whether the output multiplexed buffer is safe, which means that the buffer is not currently being used by an ongoing computation operation, that the buffer is not currently being used by an ongoing write-back operation, that the old data in the buffer has been consumed or written back to external memory, that the asynchronous transport event or write-back event related to the buffer has been set, and that there are no incomplete memory-dependent operations; after confirming safety, the asynchronous preloading of the next batch of input data is initiated, which means that the first and second input tensor data of the next batch are transported from external main memory to their respective preload buffers through the data transport engine, and the transport process is executed in parallel with the computation of the current batch).

[0095] It should be noted that the above steps configure dual buffers for the first input tensor, the second input tensor, and the output tensor; set the current buffer and the preload buffer for the first input tensor and the second input tensor; set the current output buffer and the output multiplexed buffer for the output tensor; then, it is stipulated that the next batch of inputs will use the preloaded buffer, and the next batch of outputs will use the output multiplexed buffer; then, it is determined whether the preloaded buffer and the output multiplexed buffer are in a safe and available state. A safe state means that the buffer is not occupied by running computation or write-back operations, and the old data inside the buffer has been consumed, so the next batch of data can be written. After confirming safety, the next batch of asynchronous preloading is started; then, the data of the next batch of first input tensors and second input tensors is moved from external main memory to the preloaded buffer with the help of the data transfer engine. The data transfer action is performed synchronously with the current batch of computation, realizing the overlapping execution of data transfer and computation tasks, reducing the overall waiting latency;

[0096] S62: The first and second input tensors of the current batch are loaded from the current buffer into the register to perform a binary operation (i.e., according to the physical layout attributes and operation domain attributes of the first input tensor, the data is loaded from the on-chip high-speed storage into the vector register; after loading, it is converted into an internal operation type according to the operation domain attributes; the same applies to the second input tensor). The calculation result is written to the current output buffer of the output tensor (i.e., the operation result is in the register and represented as a data packet of the internal operation type; the operation result is written to the current output buffer of the output tensor, and the operation result is packaged according to the physical layout attributes of the output tensor. After packaging, it is written to the current output buffer of the output tensor; the current output buffer is prepared to be written back to the external main memory, and the data in the current output buffer will be asynchronously written back to the external main memory later).

[0097] The current output buffer is asynchronously written back to external memory. After confirming that the write-back event of the current output buffer has been completed and that the preload buffer and output multiplexed buffer of the next batch are both in a safe state, the current buffer pointer and preload buffer pointer of each tensor are swapped. The safe state means that the buffer is not currently occupied by the ongoing computation or write-back operation, and the old data in the buffer has been consumed or written back to external memory. (That is, during the write-back, the current buffer pointer and preload buffer pointer of the three tensors are swapped; for the first input tensor, the current buffer pointer points to the original preload buffer, and the preload buffer pointer points to the original current buffer; for the second input tensor, the current buffer pointer points to the original preload buffer, and the preload buffer pointer points to the original current buffer; for the third input tensor, the current buffer pointer points to the original preload buffer, and the preload buffer pointer points to the original current buffer; for the third input tensor, the current buffer pointer points to the original preload buffer, and the preload buffer pointer points to the original current buffer; for the third input tensor, the preload buffer pointer points to the original preload buffer, and the preload buffer pointer points to the original current buffer; for the third input tensor, the preload buffer pointer points to the original preload buffer, and the preload buffer pointer points to the original preload ... The input tensor's current buffer pointer points to the original preload buffer, and the preload buffer pointer points to the original current buffer; for the output tensor, the current output buffer pointer points to the original output multiplexed buffer, and the output multiplexed buffer pointer points to the original current output buffer. After the swap, the next batch of data is already in the new current buffer. Because preloading has been completed, the next batch can directly load data from the new current buffer to perform calculations. The current output buffer is used to store the calculation results of the current batch; the output multiplexed buffer is used to store the calculation results of the next batch, and its pointer is swapped with the current output buffer after being written back to external memory.

[0098] If the first input tensor and the second input tensor are broadcast-side input tensors, and the physical address of the next batch is the same as the physical address of the current batch, then skip the data transfer of the broadcast-side input tensor, and only perform buffer swapping and data transfer on the non-broadcast-side input tensor and the output tensor (i.e., keep the resident data of the broadcast-side input tensor unchanged; swap the current buffer and preload buffer of the non-broadcast-side input tensor, and start the asynchronous preload of the next batch; swap the current output buffer and output multiplexing buffer of the output tensor, and start the output write-back of the next batch; if they are different, then process the broadcast-side input tensor in the normal double-buffering manner; if the physical address of the next batch is different from that of the current batch, it means that the broadcast-side input needs new data; at this time, it is necessary to swap the double buffer of the broadcast-side input tensor, start the asynchronous preload of the broadcast-side input tensor, and update the resident data).

[0099] The operation result is transformed according to the physical layout attribute of the output tensor, the physical layout is output from the register data packet of the internal operation type, and written back to the buffer of the output tensor;

[0100] It should be noted that the above steps load the first and second input tensors of the current batch from the current buffer into the register for binary operations; then, based on the physical layout and operation domain attributes of the first input tensor, the data is loaded from on-chip storage to the vector register, completing the format conversion to the internal operation type. The same processing is performed on the second input tensor; the operation result is generated in the register as a data packet of the internal operation type; then, the operation result is packaged according to the physical layout attributes of the output tensor and written to the current output buffer of the output tensor; the data in the current output buffer awaits asynchronous writing back to external main memory; while performing the write-back operation, the buffer pointers of the first input tensor, the second input tensor, and the output tensor are switched; the current buffer pointer and the preload buffer pointer of the first and second input tensors are swapped; the current output buffer pointer and the output multiplexed buffer pointer of the output tensor are swapped. After the pointer swap is completed, the data that has been preloaded in the next batch is located in the new current buffer, and the next batch can directly read the data in the new current buffer to perform calculations and identify whether the first and second input tensors belong to the broadcast side input tensors.

[0101] When the physical addresses of the next batch are the same as those of the current batch, the data transfer operation of the broadcast-side input tensor is skipped, and the resident data of the broadcast-side input tensor remains unchanged. Only the buffer exchange and data transfer of the non-broadcast-side input tensor and the output tensor are completed. When the physical addresses of the next batch are different from those of the current batch, the broadcast-side input tensor performs a normal double-buffering process, swaps its own double buffer, and starts asynchronous preloading to update the resident data. Then, through the internal operation type data packet in the output tensor physical layout attribute conversion register, it is converted into the output physical layout format and written to the output tensor buffer, completing the operation result format conversion and buffer data processing.

[0102] Example 2 like Figure 7 As shown, the present invention also provides a templated implementation and configuration generation system for a binary broadcast operator, including: a data acquisition module 10, an analysis module 20, and an identification module 30;

[0103] The acquisition module 10 is used to acquire data type information of the first input tensor, the second input tensor, and the output tensor; and to determine the physical layout attributes and computation domain attributes of the first input tensor, the second input tensor, and the output tensor based on the data type information.

[0104] The analysis module 20 is used to calculate the physical address offset and physical length of the first input tensor and the second input tensor in the reference coordinate system based on the physical layout attributes of the first input tensor and the second input tensor and the physical layout attributes of the output tensor; and to further identify the line end padding gap of the output tensor based on the physical address offset and physical length. Based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, calculate the physical capacity of each tensor; based on the physical capacities of the first input tensor, the second input tensor, and the physical capacity of the output tensor, calculate the remaining on-chip storage space, and reclaim the remaining on-chip storage space into the buffer of the output tensor to fill the line end padding gap of the output tensor; Based on the physical layout attributes of the first input tensor, the second input tensor, and the padded output tensor, the broadcast pattern is identified and the broadcast path is selected. Analyze the output tensors of each dimension based on the broadcast path; The identification module 30 is used to transport the first input tensor and the second input tensor from the external memory to the on-chip memory according to the broadcast path, load data into the register in the on-chip memory, perform binary operations to obtain the operation result, and pack the operation result according to the physical layout attributes of the output tensor and write it back to the external memory.

[0105] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; those skilled in the art can modify the technical solutions described in the foregoing embodiments, or make equivalent substitutions for some or all of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.

Claims

1. A templated implementation and configuration generation method for a binary broadcast operator, characterized in that, Specific operating methods include: Obtain the data type information of the first input tensor, the second input tensor, and the output tensor; determine the physical layout attributes and computation domain attributes of the first input tensor, the second input tensor, and the output tensor based on the data type information; Based on the physical layout attributes of the first input tensor and the second input tensor and the physical layout attributes of the output tensor, calculate the physical address offset and physical length of the first input tensor and the second input tensor in the reference coordinate system; further identify the line end padding gap of the output tensor based on the physical address offset and physical length; Based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, calculate the physical capacity of each tensor; based on the physical capacities of the first input tensor, the second input tensor, and the physical capacity of the output tensor, calculate the remaining on-chip storage space, and reclaim the remaining on-chip storage space into the buffer of the output tensor to fill the line end padding gap of the output tensor; Based on the physical layout attributes of the first input tensor, the second input tensor, and the padded output tensor, the broadcast pattern is identified and the broadcast path is selected. Analyze the output tensors of each dimension based on the broadcast path; The first input tensor and the second input tensor are transported from external memory to on-chip memory according to the broadcast path. Data is loaded into registers in the on-chip memory, binary operations are performed to obtain the operation result, and the operation result is packaged according to the physical layout attributes of the output tensor and written back to the external memory.

2. The method for templated implementation and configuration generation of a binary broadcast operator according to claim 1, characterized in that, Based on the data type information, the physical layout attributes and computation domain attributes of the first input tensor, the second input tensor, and the output tensor are determined respectively. The specific operation steps are as follows: Based on the data type information, determine the physical layout attributes and computation domain attributes in the first input tensor and the second input tensor; The physical layout attributes include the number of bytes occupied by a single logical element, the alignment granularity in the zero-dimensional direction, the alignment requirements for transmission granularity during data movement, the loading step size, and the storage step size; the computation domain attributes include the register data packet type, the expansion method after data loading, the instruction interface involved in the calculation, and the method for packaging the calculation results. If the physical layout attribute of the first input tensor is a first preset byte width and the operation domain attribute is a floating-point type, then a floating-point data packet with a second preset byte width is loaded using the extended method of loading the first input tensor into a register, and floating-point operations are performed using the floating-point data packet; if the physical layout attribute of the first input tensor is a first preset byte width and the operation domain attribute is an integer type, then an integer data packet with a second preset byte width is loaded using the extended method of loading the first input tensor into a register, and integer operations are performed using the integer data packet; the internal operation type is executed according to the data type information of the output tensor; If the binary operation is a comparison operation and the data type information of the output tensor is Boolean, then the internal operation type is determined to be the common promotion type of the first input tensor and the second input tensor.

3. The method for templated implementation and configuration generation of a binary broadcast operator according to claim 2, characterized in that, Based on the physical layout attributes of the first input tensor and the second input tensor, and the physical layout attributes of the output tensor, the physical address offset and physical length of the first input tensor and the second input tensor in the reference coordinate system are calculated. The specific operation steps are as follows: Obtain the number of bytes occupied by a single logical element in the physical layout attributes of the first input tensor and the number of bytes occupied by a single logical element in the physical layout attributes of the output tensor, and calculate the ratio of the first byte width; The coordinate space of the logical elements of the output tensor is used as the reference coordinate system; Calculate the first physical address offset and the first physical length of the first input tensor in the reference coordinate system based on the ratio of the width of the first byte; Obtain the number of bytes occupied by a single logical element in the physical layout attribute of the second input tensor and the number of bytes occupied by a single logical element in the physical layout attribute of the output tensor, and calculate the ratio of the second byte width; The second physical address offset and second physical length of the second input tensor in the reference coordinate system are calculated based on the ratio of the second byte width. If the broadcast relationship of the first input tensor is scalar broadcast, the physical length of the first input tensor in the reference coordinate system is determined to be the number of physical bytes corresponding to a single logical element, which is used as the physical length of the first input tensor in the reference coordinate system.

4. The method for templated implementation and configuration generation of a binary broadcast operator according to claim 3, characterized in that, Based on the physical address offset and physical length, the line end padding gap of the output tensor is further identified. The specific operation steps are as follows: If the broadcast relationship of the first input tensor is nonscalar broadcast, then the physical length of the current processing batch of the first input tensor is calculated based on the ratio of the logical length of the current processing batch of the output tensor to the first byte width of the first input tensor and the output tensor; and the physical length of the three tensors after alignment in the zero dimension direction is calculated based on the dimension zero alignment granularity in the physical layout attributes of the first input tensor, the second input tensor, and the output tensor respectively. If the physical length of any tensor after alignment in the zero-dimensional direction is greater than the product of the zero-dimensional length of the tensor and the width of a single logical element byte, then the output tensor is determined to have end-of-line padding gaps.

5. The method for templated implementation and configuration generation of a binary broadcast operator according to claim 4, characterized in that, The physical capacity is calculated based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor. The specific steps are as follows: Determine the number of bytes occupied by a single logical element in the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, and the number of logical elements in the current processing batch, and calculate the sum of the physically occupied bytes; calculate the first physical capacity of the first input tensor, the second input tensor, and the output tensor according to the ratio of the physically occupied bytes of the first input tensor, the second input tensor, and the output tensor to the allocated available on-chip storage capacity; Obtain the hardware address granularity; align the first physical capacity of the first input tensor, second input tensor, and output tensor downwards according to the hardware address granularity to obtain the aligned physical capacity of the first input tensor, second input tensor, and output tensor; calculate the number of logical elements that the first input tensor, second input tensor, and output tensor can hold by calculating the aligned physical capacity of the first input tensor, second input tensor, and output tensor and the byte width of the corresponding single logical element; take the minimum number of logical elements that can be held as the logical length that can be processed in the current batch; The logical length is aligned downwards to the computational unit granularity, and the actual number of logical elements processed in the current batch is determined. The second physical capacity of the first input tensor, the second input tensor, and the output tensor is recalculated using the number of logical elements actually processed in the current batch.

6. The method for templated implementation and configuration generation of a binary broadcast operator according to claim 5, characterized in that, The physical capacity of the first input tensor and the second input tensor is used to store the physical capacity of the output tensor. The specific operation steps are as follows: The remaining on-chip storage space is calculated using the second physical capacity and the first physical capacity, and the remaining on-chip storage space is reclaimed for a buffer of the first physical capacity that is used to store the physical number of bytes occupied by the output tensor.

7. The method for templated implementation and configuration generation of a binary broadcast operator according to claim 6, characterized in that, Based on the physical layout attributes of the first input tensor, the second input tensor, and the output tensor, the broadcast pattern is identified, and the broadcast path is selected. The specific operation steps are as follows: Obtain the logical lengths of the first input tensor, the second input tensor, and the output tensor in each dimension; compare the logical lengths of the first input tensor and the output tensor dimension by dimension. If the length of the first input tensor in that dimension is equal to 1 and the length of the output tensor in that dimension is greater than 1, then broadcasting occurs in that dimension, forming a first dimension broadcast set; compare the logical lengths of the second input tensor and the output tensor dimension by dimension. If the length of the second input tensor in that dimension is equal to 1 and the length of the output tensor in that dimension is greater than 1, then broadcasting occurs in that dimension, forming a second dimension broadcast set; If the logical length of all dimensions of the first input tensor and the second input tensor is 1, then the first input tensor and the second input tensor are identified as scalar broadcasting, and a scalar broadcasting path is selected; a high-dimensional interval is preset; if the broadcasting dimension of the first input tensor and the second input tensor is within a continuous interval of the high-dimensional interval, and the dimensions outside the high-dimensional interval are consistent with the output tensor, then it is identified as continuous high-dimensional broadcasting, and a continuous high-dimensional broadcasting path is selected; if the logical length of the first input tensor and the second input tensor is 1 only in the zero-dimensional direction, and the other dimensions are consistent with the output tensor, then it is identified as zero-dimensional broadcasting, and a zero-dimensional broadcasting path is selected; if the logical length of all dimensions of the first input tensor and the second input tensor are consistent with the output tensor, then it is identified as no broadcasting. If the broadcast does not belong to any of the above forms, it is identified as a general broadcast form, and a general broadcast path is selected.

8. The method for templated implementation and configuration generation of a binary broadcast operator according to claim 7, characterized in that, The specific steps for analyzing the output tensors of each dimension based on the broadcast path are as follows: If a continuous high-dimensional broadcast path is selected, the prefix dimension, broadcast dimension, and suffix dimension are divided according to the logical length of each dimension of the output tensor; the broadcast-side input tensor is obtained; the number of logical elements corresponding to the prefix dimension of the broadcast-side input tensor is loaded as resident data and stored in on-chip memory; the resident data is expanded in on-chip memory according to the logical length of the broadcast dimension; the non-broadcast-side input tensor is continuously loaded into on-chip memory according to row units; the number of rows that can be processed in the current batch is calculated based on the remaining on-chip storage capacity after the resident data is occupied; double buffers are allocated for the non-broadcast-side input tensor, the broadcast-side input tensor after the resident data is expanded, and the output tensor. If the logical length of the suffix dimension is greater than 1, then the range of the suffix dimension is determined, the output tensor is divided along the suffix dimension to form multiple output sub-intervals, and a computing core is allocated; if the logical length of the suffix dimension is equal to 1, then the range of the broadcast dimension is determined, the output tensor is divided along the broadcast dimension to form multiple output sub-intervals, and a computing core is allocated, and the resident data is reused locally on each computing core.

9. The method for templated implementation and configuration generation of a binary broadcast operator according to claim 8, characterized in that, The first input tensor and the second input tensor are moved from external memory to on-chip memory according to the broadcast path, and data is loaded into registers in the on-chip memory. The specific operation steps are as follows: Double buffers are allocated for the first input tensor, the second input tensor, and the output tensor. The double buffers for the first and second input tensors include a current buffer and a preload buffer. The double buffers for the output tensor include a current output buffer and an output multiplexing buffer. The preload buffer is used for the next batch of the first and second input tensors, and the output multiplexing buffer is used for the next batch of the output tensor. If the preload buffer and the output multiplexing buffer for the next batch are safe, then asynchronous preloading for the next batch is started.

10. The method for templated implementation and configuration generation of a binary broadcast operator according to claim 9, characterized in that, The binary operation is performed to obtain the operation result. The operation result is then packaged according to the physical layout attributes of the output tensor and written back to the external memory. The specific operation steps are as follows: The first and second input tensors of the current batch are loaded from the current buffer into a register to perform a binary operation, and the calculation result is written to the current output buffer of the output tensor. The current output buffer is asynchronously written back to external memory. After confirming that the write-back event of the current output buffer has been completed and that the preload buffer and output multiplexed buffer of the next batch are both in a safe state, the current buffer pointer and preload buffer pointer of each tensor are swapped. If the first and second input tensors are broadcast-side input tensors and the physical address of the next batch is the same as the physical address of the current batch, the data transfer of the broadcast-side input tensors is skipped, and only the buffer swapping and data transfer are performed on the non-broadcast-side input tensors and the output tensor. The operation result is transformed according to the physical layout attributes of the output tensor, the physical layout is output from the register data packet of the internal operation type, and written back to the buffer of the output tensor.