CN112507173B - Tensor segmentation method, tensor segmentation device, chip and medium - Google Patents
Tensor segmentation method, tensor segmentation device, chip and medium Download PDFInfo
- Publication number
- CN112507173B CN112507173B CN202011479734.4A CN202011479734A CN112507173B CN 112507173 B CN112507173 B CN 112507173B CN 202011479734 A CN202011479734 A CN 202011479734A CN 112507173 B CN112507173 B CN 112507173B
- Authority
- CN
- China
- Prior art keywords
- segmentation
- dimension
- tensor
- segmented
- index value
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/90—Details of database functions independent of the retrieved data types
- G06F16/901—Indexing; Data structures therefor; Storage structures
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F18/00—Pattern recognition
- G06F18/20—Analysing
- G06F18/21—Design or setup of recognition systems or techniques; Extraction of features in feature space; Blind source separation
- G06F18/213—Feature extraction, e.g. by transforming the feature space; Summarisation; Mappings, e.g. subspace methods
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Data Mining & Analysis (AREA)
- Physics & Mathematics (AREA)
- Computer Vision & Pattern Recognition (AREA)
- Databases & Information Systems (AREA)
- General Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- Artificial Intelligence (AREA)
- Evolutionary Computation (AREA)
- Evolutionary Biology (AREA)
- Bioinformatics & Computational Biology (AREA)
- Bioinformatics & Cheminformatics (AREA)
- Software Systems (AREA)
- Life Sciences & Earth Sciences (AREA)
- Apparatus For Radiation Diagnosis (AREA)
- Image Analysis (AREA)
Abstract
The embodiment of the invention discloses a tensor segmentation method, a tensor segmentation device, a tensor segmentation chip and a tensor segmentation medium. The method comprises the following steps: acquiring an initial tensor and segmentation parameters; rearranging the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, so that the data to be reserved in the dimension to be segmented are adjacently arranged; and cutting the rearrangement tensor according to the cutting parameters to obtain a result tensor. The technical scheme can realize the stride segmentation of tensors through the chips which do not support the stride segmentation, and reduces the configuration quantity of registers.
Description
Technical Field
The embodiment of the invention relates to the technical field of data processing, in particular to a tensor segmentation method, a tensor segmentation device, a tensor segmentation chip and a tensor segmentation medium.
Background
The tensor operation includes: splicing, splitting, indexing and transforming. The stride cut (STRIDE SLICE) is a specific way of cutting, and refers to cutting the data in the tensor according to the cutting step (stride). For example, the tensor a is [0,1,2,3,4,5,6,7,8,9,10], and if the segmentation step size stride=2, the result of performing the stride segmentation on the tensor a is [0,2,4,6,8,10].
Currently, in order to realize the stride segmentation of tensors, the tensors can be realized by a chip supporting a stride segmentation function, but the hardware cost is higher; the method can also be realized by configuring a plurality of registers on the basis of a chip which does not support stride segmentation, but when the multi-dimensional tensor is segmented, one register needs to be configured once for data segmentation, the more the segmentation times are, the more the number of the configured registers is, so that the hardware cost is increased, and the hardware complexity is also improved.
Disclosure of Invention
The embodiment of the invention provides a tensor segmentation method, a tensor segmentation device, tensor segmentation equipment and a tensor segmentation medium, which are used for realizing tensor stride segmentation through a chip which does not support stride segmentation and reducing the configuration quantity of registers.
In a first aspect, an embodiment of the present invention provides a tensor segmentation method, including:
Acquiring an initial tensor and segmentation parameters;
rearranging the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, so that the data to be reserved in the dimension to be segmented are adjacently arranged;
and cutting the rearrangement tensor according to the cutting parameters to obtain a result tensor.
In a second aspect, an embodiment of the present invention further provides a tensor segmentation apparatus, including:
The tensor acquisition module is used for acquiring an initial tensor and segmentation parameters;
the tensor rearrangement module is arranged for rearranging the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, so that the data to be reserved in the dimension to be segmented are adjacently arranged;
And the tensor segmentation module is used for segmenting the rearrangement tensor according to the segmentation parameters to obtain a result tensor.
In a third aspect, an embodiment of the present invention further provides a chip, including a memory, a processor, and a computer program stored in the memory and capable of running on the processor, where the processor implements the tensor segmentation method according to any embodiment of the present invention when the processor executes the program.
In a fourth aspect, an embodiment of the present invention further provides a computer readable storage medium, where a computer program is stored, where the program is executed by a processor to implement the tensor segmentation method according to any embodiment of the present invention.
In the technical scheme of the embodiment of the invention, when the obtained initial tensor is subjected to stride segmentation, firstly, data on a dimension to be segmented is rearranged according to segmentation parameters to obtain a rearrangement tensor, wherein the data to be reserved on the dimension to be segmented in the rearrangement tensor are adjacently arranged, and then the rearrangement tensor is segmented according to the segmentation parameters to obtain a result tensor after the initial tensor is segmented. In the technical scheme, the segmentation of the rearrangement tensor can be realized through the chips which do not support the stride segmentation, and the data to be reserved in the dimension to be segmented in the rearrangement tensor are adjacently arranged, so that the segmentation times are reduced, and too many registers are not needed to be configured, thereby achieving the technical effects of realizing the stride segmentation of the tensor through the chips which do not support the stride segmentation, and reducing the configuration quantity of the registers.
Drawings
FIG. 1 is a flow chart of a tensor segmentation method in accordance with a first embodiment of the present invention;
FIG. 2 is a flow chart of a tensor segmentation method in a second embodiment of the present invention;
FIG. 3 is a schematic diagram of performing a stride segmentation on a one-dimensional tensor using the tensor segmentation method provided in the second embodiment;
FIG. 4 is a schematic diagram of a prior art tensor segmentation method based on configuration registers for performing a stride segmentation on a one-dimensional tensor;
FIG. 5 is a flow chart of a tensor segmentation method in accordance with a third embodiment of the present invention;
FIG. 6 is a flow chart of a tensor segmentation method in accordance with a fourth embodiment of the present invention;
fig. 7 is a schematic structural diagram of a tensor slicing device in a fifth embodiment of the present invention;
Fig. 8 is a schematic structural diagram of a chip in a sixth embodiment of the present invention.
Detailed Description
The invention is described in further detail below with reference to the drawings and examples. It is to be understood that the specific embodiments described herein are merely illustrative of the invention and are not limiting thereof. It should be further noted that, for convenience of description, only some, but not all of the structures related to the present invention are shown in the drawings.
Before discussing exemplary embodiments in more detail, it should be mentioned that some exemplary embodiments are described as processes or methods depicted as flowcharts. Although a flowchart depicts operations (or steps) as a sequential process, many of the operations can be performed in parallel, concurrently, or at the same time. Furthermore, the order of the operations may be rearranged. The process may be terminated when its operations are completed, but may have additional steps not included in the figures. The processes may correspond to methods, functions, procedures, subroutines, and the like.
Example 1
Fig. 1 is a flowchart of a tensor segmentation method according to a first embodiment of the present invention, where the method may be implemented by a tensor segmentation device according to the first embodiment of the present invention, where the device may be implemented in software and/or hardware, and may be generally integrated in a chip.
As shown in fig. 1, the tensor segmentation method provided in this embodiment is applied to stride segmentation, and includes:
s110, acquiring an initial tensor and segmentation parameters.
The initial tensor may be a one-dimensional tensor, a two-dimensional tensor, or a multidimensional tensor.
The segmentation parameters refer to segmentation parameters corresponding to the stride segmentation, and may include segmentation step sizes. Wherein, when the tensor is subjected to the step-by-step slicing, the slicing step length is larger than 1.
Alternatively, when the initial tensor is a multidimensional tensor, the dimension to be segmented may be one or more. When the dimension to be segmented of the initial tensor is one, the segmentation step length is larger than 1; when the number of the dimensions to be segmented of the initial tensor is multiple, the segmentation step length corresponding to at least one dimension to be segmented is larger than 1.
Further, the segmentation parameter may further include a segmentation start index value, or include a segmentation start index and a segmentation end index value, for indicating a segmentation element index range. The segmentation starting index value is an index value of a starting element corresponding to the segmentation element index range on the dimension to be segmented, and the segmentation ending index value is an index value of an ending element corresponding to the segmentation element index range on the dimension to be segmented.
S120, rearranging the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, so that the data to be reserved in the dimension to be segmented are adjacently arranged.
The data to be reserved refers to the data to be reserved for performing stride segmentation on tensors. The data to be reserved in the dimension to be segmented is determined according to the segmentation step length n of the dimension to be segmented, namely, one data is taken out every n data in the dimension to be segmented, namely, two adjacent data to be reserved are separated by (n-1) data in the dimension to be segmented.
And rearranging the data in the dimension to be segmented of the initial tensor to obtain a rearranged tensor corresponding to the initial tensor, wherein the data to be reserved in the dimension to be segmented are adjacently arranged in the rearranged tensor.
For example, the data in the dimension to be segmented of the initial tensor is 0,1,2,3,4,5,6,7,8,9,10, and if the segmentation step size is2, the data to be reserved in the dimension to be segmented is 0,2,4,6,8,10. In the initial tensor, the data to be retained in the dimension to be sliced is arranged at intervals, not adjacently.
In this step, the data in the dimension to be segmented of the initial tensor is rearranged, that is, 0,1,2,3,4,5,6,7,8,9,10 is rearranged, so that the data to be retained 0,2,4,6,8,10 in the rearranged tensor obtained after the data rearrangement is adjacently arranged, and in an exemplary embodiment, the data in the dimension to be segmented in the rearranged tensor is sorted to 0,2,4,6,8,10,1,3,5,7,9.
S130, cutting the rearrangement tensor according to the cutting parameters to obtain a result tensor.
And cutting the data to be retained, which are adjacently arranged in the rearrangement tensor, according to the cutting parameters, so as to obtain a result tensor corresponding to the initial tensor.
Continuing the previous example, slicing the data 0,2,4,6,8,10 to be retained in the data 0,2,4,6,8,10,1,3,5,7,9 on the dimension to be sliced of the rearrangement tensor according to the slicing parameters, so as to obtain a result tensor corresponding to the initial tensor, wherein the data on the dimension corresponding to the dimension to be sliced of the initial tensor in the result tensor is 0,2,4,6,8,10.
It should be noted that if the initial tensor is a multidimensional tensor and the initial tensor includes a plurality of dimensions to be segmented, tensor segmentation is sequentially performed for each dimension to be segmented, where when the dimension to be segmented is not the first dimension to be segmented, the initial tensor corresponding to the first dimension to be segmented is a result tensor obtained by performing tensor segmentation for the previous dimension to be segmented.
In the technical scheme of the embodiment of the invention, when the obtained initial tensor is subjected to stride segmentation, firstly, data on a dimension to be segmented is rearranged according to segmentation parameters to obtain a rearrangement tensor, wherein the data to be reserved on the dimension to be segmented in the rearrangement tensor are adjacently arranged, and then the rearrangement tensor is segmented according to the segmentation parameters to obtain a result tensor after the initial tensor is segmented. In the technical scheme, the segmentation of the rearrangement tensor can be realized through the chips which do not support the stride segmentation, and the data to be reserved in the dimension to be segmented in the rearrangement tensor are adjacently arranged, so that the segmentation times are reduced, and too many registers are not needed to be configured, thereby achieving the technical effects of realizing the stride segmentation of the tensor through the chips which do not support the stride segmentation and reducing the configuration quantity of the registers.
Example two
Fig. 2 is a flowchart of a tensor segmentation method according to a second embodiment of the present invention. The embodiment is embodied on the basis of the foregoing embodiment, where the rearrangement of the data on the dimension to be segmented according to the initial tensor and the segmentation parameter may be performed to obtain a rearrangement tensor, which is specifically:
Splitting the dimension to be split into a split sub-dimension and a step sub-dimension according to the dimension size and the splitting step length of the dimension to be split, and rearranging the data on the dimension to be split according to the splitting result to obtain a rearrangement tensor.
Further, the rearrangement tensor may be segmented according to the segmentation parameter to obtain a result tensor, specifically:
According to the segmentation initial index value of the dimension to be segmented, performing first segmentation on the rearrangement tensor in the step-length sub-dimension to obtain a primary segmentation tensor;
Performing second segmentation on the primary segmentation tensor on the segmentation dimension according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented to obtain a secondary segmentation tensor;
rearranging the secondary segmentation tensor, enabling the secondary segmentation tensor to be matched with the shape of the initial tensor, and taking the rearranged secondary segmentation tensor as a result tensor.
As shown in fig. 2, the tensor segmentation method provided in this embodiment is applied to stride segmentation, and includes:
s210, acquiring an initial tensor and segmentation parameters.
The segmentation parameters comprise a segmentation step length, a segmentation start index value and a segmentation end index value which correspond to the dimension to be segmented of the initial tensor.
The initial tensor may have one or more dimensions to be segmented, and an explanation is given below taking one dimension to be segmented as an example.
S220, splitting the dimension to be split into a split sub-dimension and a step sub-dimension according to the dimension size and the splitting step length of the dimension to be split, and rearranging data on the dimension to be split according to the splitting result to obtain a rearrangement tensor.
Where dimension size refers to the amount of data in a dimension. For example, if the data on the dimension to be segmented is 0,1,2,3,4,5,6,7, the dimension size of the dimension to be segmented is 8.
When the dimension to be segmented is split into a segmentation sub-dimension and a step sub-dimension according to the dimension of the dimension to be segmented and the segmentation step length, setting the dimension of the step sub-dimension to be equal to the segmentation step length, and setting the dimension of the segmentation sub-dimension to be the quotient of the dimension of the dimension to be segmented and the segmentation step length. Assuming that the splitting step length of the dimension to be split is2, the dimension to be split can be split into a splitting sub-dimension 4 and a step sub-dimension 2 according to the dimension size 8 and the splitting step length 2.
Optionally, splitting the dimension to be split into a split sub-dimension and a step sub-dimension according to the dimension size and the splitting step length of the dimension to be split may specifically be:
If the dimension size of the dimension to be segmented cannot be divided by the segmentation step length of the dimension to be segmented, the dimension size of the dimension to be segmented is increased, so that the increased dimension size can be divided by the segmentation step length of the dimension to be segmented; splitting the dimension to be split into a split sub-dimension and a step sub-dimension according to the increased dimension size and the splitting step length; the dimension size of the segmentation sub-dimension is the quotient of the increased dimension size and the segmentation step length, and the dimension size of the step length sub-dimension is the value of the segmentation step length.
Assuming that the data on the dimension to be segmented is 0,1,2,3,4,5 and 6, the dimension size of the dimension to be segmented is 7, if the segmentation step size of the dimension to be segmented is 2, the dimension size 7 of the dimension to be segmented cannot be divided by the segmentation step size 2 of the dimension to be segmented, at this time, the dimension size of the dimension to be segmented is increased, and the data on the dimension to be segmented can be subjected to bit filling, so that the increased dimension size can be divided by the segmentation step size of the dimension to be segmented, and the dimension to be segmented is further divided into a segmentation sub-dimension and a step sub-dimension according to the increased dimension size and the segmentation step size.
Optionally, calculating a remainder value of dividing the dimension size of the dimension to be segmented by the segmentation step size, and taking a difference value between the segmentation step size and the remainder value as a value for increasing the dimension size of the dimension to be segmented.
Continuing the previous example, calculating that the remainder value of dividing the dimension size 7 by the segmentation step size 2 on the dimension to be segmented is equal to 1, and the difference between the segmentation step size 2 and the remainder value 1 is equal to 1, so that the dimension size of the dimension to be segmented is increased by 1, namely 1 bit of data on the dimension to be segmented is complemented, for example, 0,1,2,3,4,5,6 and X are obtained by complementing the data on the dimension to be segmented. At this time, the dimension size 8 after being increased can be divided by the dividing step size 2 of the dimension to be divided, and then the dimension to be divided is divided into the dividing sub-dimension 4 and the step sub-dimension 2 according to the dimension size 8 after being increased and the dividing step size 2.
Then rearranging the data in the dimension to be segmented according to the splitting result, and continuing the former example, namely rearranging the data 0,1,2,3,4,5,6 and X in the dimension to be segmented according to the segmentation sub-dimension 4 and the step sub-dimension 2, namely rearranging the data according to 4 rows and 2 columns to obtain [0,1;2,3;4,5;6, X ].
Before data rearrangement, the data to be reserved are distributed at intervals in the dimension to be segmented; after data rearrangement, the data to be reserved are adjacently arranged in the dimension of the segmentation.
Optionally, when the data in the dimension to be segmented is rearranged according to the splitting result, in the segmentation sub-dimension and the step sub-dimension, the step sub-dimension may be used as a low dimension, so as to reduce memory overhead.
As an optional implementation manner, the rearrangement of the data in the dimension to be segmented according to the splitting result may be specifically:
Sequentially acquiring each data on the dimension to be segmented as current processing data; arranging the current processing data on the target element position; the index value of the target element position on the step sub-dimension is the remainder value of dividing the index value of the current processing data on the dimension to be segmented by the segmentation step; the index value of the target element position in the dimension of the segmentation is the quotient of the index value of the current processing data in the dimension to be segmented divided by the segmentation step length; and returning to execute the operation of sequentially acquiring one data in the dimension to be segmented as the current processing data until all the data in the dimension to be segmented are processed.
Continuing the previous example, wherein the index values of the data 0,1,2,3,4,5,6, x in the dimension to be sliced are sequentially 0,1,2,3,4,5,6, 7 (in this example, the index numbers start from 0). Firstly, taking data 0 as current processing data, wherein the index value of the arranged target element position in the step size sub-dimension is the index value 0 of the arranged target element position in the dimension to be segmented divided by the remainder value 0 of the segmentation step size 2, the index value of the arranged target element position in the segmentation sub-dimension is the index value 0 of the current processing data in the dimension to be segmented divided by the quotient value 0 of the segmentation step size 2, namely when the data are rearranged according to 4 rows and 2 columns, the index of the element position of the data 0 is (0, 0); secondly, taking the data 1 as current processing data, wherein the index value of the arranged target element position in the step length sub-dimension is the index value 1 of the arranged target element position in the dimension to be segmented divided by the remainder value 1 of the segmentation step length 2, the index value of the arranged target element position in the segmentation sub-dimension is the index value 1 of the current processing data in the dimension to be segmented divided by the quotient value 0 of the segmentation step length 2, namely when the data are rearranged according to 4 rows and 2 columns, the index of the element position of the data 1 is (0, 1); and the like, until all the data in the dimension to be segmented are processed, when the data are rearranged according to 4 rows and 2 columns, the index of the element position of the data 2 is (1, 0), the index of the element position of the data 3 is (1, 1), the index of the element position of the data 4 is (2, 0), the index of the element position of the data 5 is (2, 1), the index of the element position of the data 6 is (3, 0), and the index of the element position of the data X is (3, 1).
S230, performing first segmentation on the rearrangement tensor in the step sub-dimension according to the segmentation start index value of the dimension to be segmented to obtain a primary segmentation tensor.
Judging which column of data belongs to the data to be reserved on the step sub-dimension according to the segmentation starting index value of the dimension to be segmented, and then segmenting and reserving which column of data to be used as a primary segmentation tensor.
Continuing the previous example, if the segmentation starting index value of the dimension to be segmented is 0 and the data with the segmentation starting index value of 0 is 0, a row of data containing the data 0 on the step-length sub-dimension belongs to the data to be reserved, and the data to be reserved is needed to be segmented, namely the data reserved in the primary segmentation tensor are 0,2,4 and 6; if the segmentation starting index value of the dimension to be segmented is 3 and the data with the segmentation starting index value of 3 is 3, a row of data containing the data 3 in the step sub-dimension belongs to the data to be preserved, and segmentation preservation is needed, namely the data preserved in one segmentation tensor is 1,3,5 and X.
It should be noted that when the first division of the re-row tensor is performed in the step sub-dimension, no change occurs in the data in the other dimensions of the initial tensor.
As an optional implementation manner, according to the segmentation start index value of the dimension to be segmented, performing first segmentation on the rearrangement tensor in the step-length sub-dimension to obtain a primary segmentation tensor, and obtaining the primary segmentation tensor may specifically be:
if the segmentation starting index value of the dimension to be segmented is smaller than the segmentation step length of the dimension, segmenting the tensor with the index value of the step length sub-dimension as the tensor of the segmentation starting index value in the rearrangement tensor as the primary segmentation tensor;
If the segmentation initial index value of the dimension to be segmented is larger than or equal to the segmentation step length, the tensor with the index value of the dimension of the step length being the target value is segmented in the rearrangement tensor to be used as a primary segmentation tensor; the target value is the remainder value of dividing the segmentation starting index value by the segmentation step length.
Continuing the previous example, if the segmentation starting index value of the dimension to be segmented is 0, the segmentation starting index value 0 is smaller than the segmentation step length 2 of the dimension, and a tensor with the index value of 0 on the step length sub-dimension is segmented in the rearrangement tensor to serve as a primary segmentation tensor, namely, data 0,2,4 and 6 with the index value of 0 on the step length sub-dimension are reserved in the rearrangement tensor; if the segmentation starting index value of the dimension to be segmented is 3, the segmentation starting index value 3 is smaller than the segmentation step length 2 of the dimension, and a tensor with the index value of the segmentation starting index value 3 divided by the remainder value of the segmentation step length 2 of 1 in the segmentation step length sub-dimension is segmented in the rearrangement tensor to serve as a primary segmentation tensor, namely, data 1,3,5 and X with the index value of 1 in the segmentation step length sub-dimension are reserved in the rearrangement tensor.
S240, performing second segmentation on the primary segmentation tensor on the segmentation dimension according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented to obtain a secondary segmentation tensor.
When the cut end index value of the dimension to be cut is larger than the dimension size of the dimension to be cut, saturation processing may be performed on the cut end index value first, for example, the cut end index value is reset to the dimension size of the dimension to be cut. When the cut end index value is not set, the cut end index value may be defaulted to be equal to the dimension size of the dimension to be cut
When the segmentation ending index value of the dimension to be segmented is smaller than or equal to the dimension size of the dimension to be segmented, performing second segmentation on the primary segmentation tensor in the dimension to be segmented, and reserving data, of which the index value is between the segmentation starting index value and the segmentation ending index value in the dimension to be segmented, to obtain the secondary segmentation tensor.
If the first segmentation is performed on the rearrangement tensor in the step sub-dimension to obtain a first segmentation tensor, data 0,2,4 and 6 with index values of 0 in the step sub-dimension are reserved, namely, the data reserved in the segmentation sub-dimension is 0,2,4 and 6, and the data between the segmentation start index value and the segmentation end index value in the dimension to be segmented is reserved in the second segmentation if the segmentation start index value is 0 and the segmentation end index value is 5, wherein the data reserved in the segmentation sub-dimension is 0,2,4 and 6, the index values in the corresponding dimension to be segmented are 0,2,4 and 6 in sequence, and the data reserved in the second segmentation is 0,2 and 4 as a second segmentation tensor, namely, the data reserved after the step segmentation is completed in the dimension to be segmented is 0,2 and 4.
As an optional implementation manner, according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented, the first segmentation tensor is subjected to the second segmentation on the segmentation dimension to obtain the second segmentation tensor, which may be specifically:
calculating the segmentation size in the dimension of the segment according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented; dividing the segmentation starting index value by the quotient of the segmentation step length to be used as the segmentation starting index value on the segmentation dimension; and according to the segmentation starting index value and the segmentation size in the segmentation dimension, segmenting the primary segmentation tensor in the segmentation dimension to obtain a secondary segmentation tensor.
Optionally, calculating the segmentation size in the segmentation dimension according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented includes: calculating the difference value between the segmentation ending index value and the segmentation starting index value of the dimension to be segmented, and rounding up the result of dividing the difference value by the segmentation step length to obtain the segmentation dimension in the segmentation dimension.
Continuing the previous example, calculating the difference between the segmentation ending index value 5 and the segmentation starting index value 0 of the dimension to be segmented as 5, and dividing the difference 5 by the segmentation step length 2 to obtain a result 2.5, and rounding upwards to obtain 3, wherein 3 is used as the segmentation dimension in the segmentation sub dimension.
And calculating a segmentation starting index value on the segmentation sub-dimension, wherein the quotient value of the segmentation starting index value 0 divided by the segmentation step length 2 is 0, and the segmentation starting index value on the segmentation sub-dimension is 0.
And according to the segmentation starting index value and the segmentation size in the segmentation dimension, segmenting the primary segmentation tensor in the segmentation dimension, namely starting from the data corresponding to the segmentation starting index value in the segmentation dimension, segmenting the data with the same quantity as the segmentation size, and obtaining the secondary segmentation tensor. For example, after 3 data are segmented from the corresponding data 0 of the segmentation start index value 0 in the segmentation dimension, that is, the segmentation remains 0,2,4, a second segmentation tensor is obtained.
S250, rearranging the secondary segmentation tensor to enable the secondary segmentation tensor to be matched with the shape of the initial tensor, and taking the rearranged secondary segmentation tensor as a result tensor.
In the quadratic cut tensor, the cut-retained data is two-dimensional in shape, e.g. "0,2,4" is present in 3 rows and 1 column. In this step, the data reserved in the stride cut in the dimension to be cut is rearranged, and is adjusted from a two-dimensional form to a one-dimensional form, and still the data is used as the data in one dimension (the dimension is the dimension to be cut before the cut is performed) in the initial tensor, and the tensor obtained at this time is the result tensor corresponding to the initial tensor after the stride cut is completed for the dimension to be cut.
The shape of the result tensor is matched with the shape of the initial tensor, for example, if the shape of the initial tensor is (7,6,8,9), and if the dimension size is 7, the dimension to be segmented is the dimension to be segmented, after the step segmentation is completed for the dimension, the shape of the result tensor is (3,6,8,9), that is, the segmentation on the dimension with the original dimension size of 7 reserves 3 data.
The present embodiment is not explained in detail herein, and reference is made to the foregoing embodiments.
In a specific example, taking an initial tensor as a one-dimensional tensor [0,1,2,3,4,5,6] as an example, if the segmentation step length is 2, the segmentation start index value is 0, and the segmentation end index value is 5, a schematic diagram of performing the stride segmentation on the one-dimensional tensor by adopting the tensor segmentation method provided by the embodiment may be shown in fig. 3, only two times of the stride segmentation are required to complete, two registers are configured, a schematic diagram of performing the stride segmentation on the one-dimensional tensor by adopting a tensor segmentation method based on configuration registers in the prior art may be shown in fig. 4, three times of the stride segmentation is required to complete, and three registers are configured.
Along with the increase of the dimension size of the one-dimensional tensor, the technical scheme of the embodiment has the advantages that if the dimension size of the one-dimensional tensor is 2048 and the segmentation step length is 2, 1024 times of segmentation are needed for performing the stride segmentation on the one-dimensional tensor by adopting a method based on a configuration register in the prior art, 1024 registers are configured, and the stride segmentation is still needed for the one-dimensional tensor by adopting the tensor segmentation method provided by the embodiment, and two registers are configured, so that the stride segmentation of the tensor is realized through a chip which does not support the stride segmentation, the technical effect of reducing the configuration quantity of the registers is achieved, the running time of the stride segmentation of the tensor is reduced, the multiplexing of chip modules is increased, and the development cost of chip hardware is reduced.
Example III
Fig. 5 is a flowchart of a tensor segmentation method according to a third embodiment of the present invention. The embodiment is embodied on the basis of the foregoing embodiment, wherein the number of dimensions to be segmented is a plurality of; after the step of slicing the rearrangement tensor according to the slicing parameters to obtain the result tensor, the method further comprises the following steps:
judging whether a dimension to be segmented exists, which is not subjected to segmentation processing, if so, returning to a step of rearranging data in the dimension to be segmented according to the initial tensor and the segmentation parameters, rearranging the data in a target dimension to be segmented, which is subjected to segmentation processing currently, wherein the initial tensor when the target dimension to be segmented is subjected to segmentation processing is a result tensor obtained when the segmentation processing is finished for a previous dimension to be segmented; if not, the result tensor obtained when the segmentation processing is completed for the previous dimension to be segmented is taken as the final segmentation result tensor (at this time, the previous dimension to be segmented is the last tensor to be segmented which needs to be subjected to step segmentation in the initial tensor).
As shown in fig. 5, the tensor segmentation method provided in this embodiment is applied to stride segmentation, and includes:
s310, acquiring an initial tensor and segmentation parameters.
The segmentation parameters comprise segmentation step length, segmentation start index value and segmentation end index value corresponding to each dimension to be segmented of the initial tensor.
S320, rearranging the data in the current dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, so that the data to be reserved in the current dimension to be segmented are adjacently arranged.
When S320 is executed for the first time, the current dimension to be segmented is the first dimension to be segmented that needs to be subjected to stride segmentation.
S330, cutting the rearrangement tensor according to the cutting parameters to obtain a result tensor.
S340, judging whether the dimension to be segmented exists in the incomplete segmentation process, if yes, executing S350, and if not, executing S360.
S350, taking a result tensor corresponding to the current dimension to be segmented as an initial tensor, sequentially taking a dimension to be segmented which is not subjected to segmentation processing as the current dimension to be segmented again, and returning to execute S320.
S360, taking a result tensor corresponding to the current dimension to be segmented as a final segmentation result tensor.
When the dimension to be segmented which does not have incomplete segmentation processing exists, the current dimension to be segmented is the last dimension to be segmented which needs to be subjected to stride segmentation, and the result tensor obtained after the stride segmentation is completed for the dimension to be segmented is the final segmentation result tensor.
The present embodiment is not explained in detail herein, and reference is made to the foregoing embodiments.
According to the technical scheme, the stride segmentation is sequentially carried out on the plurality of dimensions to be segmented of the initial tensor, so that the technical effects of realizing the stride segmentation of the multidimensional tensor through the chip which does not support the stride segmentation and reducing the configuration quantity of the registers are achieved.
Further, as an alternative embodiment, when the slicing process is performed for each dimension to be sliced, if the dimension of the initial tensor is not three-dimensional, the initial tensor is rearranged into an initial tensor in a three-dimensional form retaining the dimension to be sliced.
If the dimension of the initial tensor is smaller than three dimensions, firstly expanding the dimension of the initial tensor and adjusting the dimension of the initial tensor into three dimensions; if the dimension of the initial tensor is greater than three dimensions, the dimension of the initial tensor can be reduced first, the dimension of the initial tensor is adjusted to be three dimensions, and one dimension is the dimension to be segmented currently.
Furthermore, when the segmentation processing is performed for each dimension to be segmented, the rearrangement tensor can be adjusted to be a three-dimensional tensor when the rearrangement tensor is performed for the rearrangement, so that each segmentation is performed for the three-dimensional tensor.
In the technical scheme, the tensor dimension is unified into three dimensions and then segmented, so that the difficulty in processing the tensor by a chip is reduced, the difficulty in developing chip hardware is also reduced, and the reusability of the chip hardware is improved.
Optionally, when the slicing process is completed for each dimension to be sliced to obtain a result tensor, the result tensor may be restored to a shape matching the initial tensor obtained in the original, where the dimensions of the two are equal. For example, when the initial tensor obtained in the original is 5-dimensional tensor and the stride cutting process is performed on each dimension to be cut, the initial tensor corresponding to the current dimension to be cut is adjusted to be a three-dimensional tensor for retaining the current dimension to be cut, when the stride cutting process is completed on each dimension to be cut, the result tensor corresponding to the current dimension to be cut is restored to be 5-dimensional tensor, and then the stride cutting process is performed on the next dimension to be cut.
Optionally, when the segmentation processing is completed for all the dimensions to be segmented, restoring the result tensor corresponding to the last dimension to be segmented into a shape matched with the initial tensor obtained originally, wherein the dimensions of the two are equal, and taking the result tensor after the shape restoration as the final segmentation result tensor. For example, when the initial tensor obtained in the original is a 5-dimensional tensor and the stride segmentation processing is performed on each dimension to be segmented, the initial tensor corresponding to the current dimension to be segmented is adjusted to a three-dimensional tensor retaining the current dimension to be segmented, until the segmentation processing is completed on all the dimensions to be segmented, the result tensor corresponding to the last dimension to be segmented is restored to be a 5-dimensional tensor, and the 5-dimensional tensor is the final segmentation result tensor.
Example IV
Fig. 6 is a flowchart of a tensor segmentation method according to a fourth embodiment of the present invention. The present embodiment provides a specific implementation manner based on the foregoing embodiment. The tensor to be strided and segmented is a multidimensional tensor, and the multidimensional tensor comprises a plurality of dimensions to be segmented.
As shown in fig. 6, the tensor segmentation method provided in this embodiment is applied to stride segmentation, and includes:
s410, acquiring a target initial tensor and segmentation parameters.
The segmentation parameters comprise a segmentation start index, a segmentation end index and a segmentation step length corresponding to each dimension to be segmented corresponding to the target initial tensor.
S420, taking the target initial tensor as the current initial tensor.
And S430, sequentially selecting one dimension to be segmented as the current dimension to be segmented, and adjusting the current initial tensor to be the three-dimensional tensor for retaining the current dimension to be segmented when the target initial tensor is not the three-dimensional tensor.
S440, splitting the current dimension to be split into a split sub-dimension and a step sub-dimension according to the dimension size and the splitting step length of the current dimension to be split, and rearranging data on the current dimension to be split according to a splitting result to obtain a rearranging tensor.
The dimension size of the step sub-dimension is set to be equal to the value of the segmentation step, and the dimension size of the segmentation sub-dimension is set to be the quotient of the dimension size of the dimension to be segmented and the segmentation step.
Optionally, if the dimension size of the dimension to be segmented cannot be divided by the segmentation step size of the dimension to be segmented, increasing the dimension size of the dimension to be segmented, so that the increased dimension size can be divided by the segmentation step size of the dimension to be segmented; splitting the dimension to be split into a split sub-dimension and a step sub-dimension according to the increased dimension size and the splitting step length; the dimension size of the segmentation sub-dimension is the quotient of the increased dimension size and the segmentation step length, and the dimension size of the step length sub-dimension is the value of the segmentation step length.
S450, performing first segmentation on the rearrangement tensor on the step sub-dimension according to the segmentation start index value of the current dimension to be segmented to obtain a primary segmentation tensor.
If the segmentation starting index value of the dimension to be segmented is smaller than the segmentation step length of the dimension, segmenting the tensor with the index value of the step length sub-dimension as the tensor of the segmentation starting index value in the rearrangement tensor as the primary segmentation tensor;
If the segmentation initial index value of the dimension to be segmented is larger than or equal to the segmentation step length, the tensor with the index value of the dimension of the step length being the target value is segmented in the rearrangement tensor to be used as a primary segmentation tensor; the target value is the remainder value of dividing the segmentation starting index value by the segmentation step length.
S460, performing second segmentation on the primary segmentation tensor on the segmentation dimension according to the segmentation start index value, the segmentation end index value and the segmentation step length of the current dimension to be segmented to obtain a secondary segmentation tensor.
Specifically, calculating the segmentation size in the dimension of the segment according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented; dividing the segmentation starting index value by the quotient of the segmentation step length to be used as the segmentation starting index value on the segmentation dimension; and according to the segmentation starting index value and the segmentation size in the segmentation dimension, segmenting the primary segmentation tensor in the segmentation dimension to obtain a secondary segmentation tensor.
S470, rearranging the secondary segmentation tensor to enable the secondary segmentation tensor to be matched with the shape of the target initial tensor, and taking the rearranged secondary segmentation tensor as the current result tensor.
S480, judging whether the dimension to be segmented exists in the incomplete segmentation process, if yes, executing S490, and if not, executing S4100.
S490, the current result tensor is taken as the current initial tensor, and the process returns to S430.
S4100, regarding the current result tensor as a final segmentation result tensor corresponding to the target initial tensor.
Illustratively, the target initial tensor has a shape of (1024,512,2048,256), each dimension is the dimension to be segmented, the corresponding segmentation start index is (5, 6,7, 8), the segmentation end index is (512,2048,256,1024), and the segmentation step size is (4,3,6,4).
When performing a stride cut for a first dimension (dimension 1024), the current initial tensor has a shape (1024,512,2048,256) that is adjusted to a three-dimensional tensor (1024, 512,2048 x 256) that preserves the current dimension to be cut (dimension 1024). Splitting the current dimension 1024 to be split into a split sub-dimension 256 and a step sub-dimension 4, and rearranging the data in the current dimension to be split according to the splitting result to obtain a rearranged tensor, wherein the rearranged tensor is also adjusted to be a three-dimensional tensor for reserving the split sub-dimension and the step sub-dimension, and the rearranged tensor is (256, 4,512×2048×256). Since the segmentation start index value of the first dimension is 5, the segmentation start index value 5 is greater than the segmentation step length 4, and the remainder value of the segmentation start index value 5 divided by the segmentation step length 4 is 1, the tensor with the index value of 1 in the sub-dimension of the segmentation step length is segmented in the rearrangement tensor, and is used as a primary segmentation tensor, and the shape of the tensor is (256, 1,512×2048×256). Since the segmentation starting index value of the first dimension is 5, the segmentation ending index value is 512, the segmentation step length is 4, the segmentation size in the segmentation sub-dimension is 127 (the difference between the segmentation ending index value 512 and the segmentation starting index value 5 is calculated, the value obtained by dividing the difference by the segmentation step length 4 and rounding up is the segmentation size), the segmentation starting index value in the segmentation sub-dimension is 1 (the quotient value obtained by dividing the segmentation starting index value 5 in the first dimension by the segmentation step length 4 is the segmentation starting index value in the segmentation sub-dimension), and then the primary segmentation tensor can be segmented in the segmentation sub-dimension according to the segmentation starting index value and the segmentation size, so that the secondary segmentation tensor is obtained, the shape of which is (127, 1,512×2048×256), and the secondary segmentation tensor is rearranged (127,512,2048,256) as the current result tensor.
When performing a stride cut for the second dimension (dimension 512), the current initial tensor has a shape (127,512,2048,256) that is adjusted to preserve the three-dimensional tensor (127, 512,2048 x 256) of the current dimension to be cut (dimension 512). Splitting the current dimension 512 to be split into a split sub-dimension 171 and a step sub-dimension 3, and rearranging the data in the current dimension to be split according to the splitting result to obtain a rearrangement tensor, wherein the rearrangement tensor is also adjusted to be a three-dimensional tensor for reserving the split sub-dimension (the split sub-dimension is combined with the high dimension thereof, and can be still called as the split sub-dimension without influencing the secondary split) and the step sub-dimension, and the shape of the rearrangement tensor is (127×171,3,2048×256). Since the segmentation start index value of the second dimension is 6, the segmentation start index value 6 is greater than the segmentation step length 3, and the remainder value of the segmentation start index value 6 divided by the segmentation step length 3 is 0, then a tensor with the index value of 0 in the sub-dimension of the segmentation step length is segmented in the rearrangement tensor, and is shaped as a primary segmentation tensor (127×171,1,2048×256). Since the segmentation start index value of the second dimension is 6, the segmentation end index value is 512 (the segmentation end index value 2048 is greater than the dimension size 512, and the saturation processing dimension 512 is performed), the segmentation step length is 3, the segmentation size in the segmentation sub-dimension is 169 (the difference between the segmentation end index value 512 and the segmentation start index value 6 is calculated, the value obtained by dividing the difference by the segmentation step length 3 and rounding up is the segmentation size), the segmentation start index value in the segmentation sub-dimension is 2 (the quotient value obtained by dividing the segmentation start index value 6 in the second dimension by the segmentation step length 3 is the segmentation start index value in the segmentation sub-dimension), and then the primary segmentation tensor can be segmented in the segmentation sub-dimension according to the segmentation start index value and the segmentation size, so as to obtain a secondary segmentation tensor, the shape of which is (127×169,1, 8×256), and the secondary segmentation tensor is rearranged (127,169,2048,256) as the current result tensor.
And so on, processing continues in the third and fourth dimensions. And finishing the stride segmentation aiming at the fourth dimension, wherein the obtained current result tensor is the final segmentation result tensor corresponding to the target initial tensor.
It should be noted that when the current dimension to be segmented is split into a segmentation sub-dimension and a step sub-dimension, the data in the current dimension to be segmented is rearranged according to the splitting result to obtain a rearrangement tensor, and the rearrangement tensor is adjusted to a three-dimensional tensor for retaining the segmentation sub-dimension and the step sub-dimension, the segmentation sub-dimension is arranged in front, and the step sub-dimension is arranged in back, so that memory overhead is avoided. Specifically, if the current dimension to be segmented is the highest dimension of the current initial tensor, the sub-dimension, the step sub-dimension and the merging dimension of all the other dimensions except the highest dimension can be respectively and sequentially used as three dimensions of the rearrangement tensor; if the current dimension to be segmented is the lowest dimension of the current initial tensor, combining dimensions, segmentation sub-dimensions and step sub-dimensions of all the other dimensions except the lowest dimension can be respectively used as three dimensions of the rearrangement tensor in sequence; if the current dimension to be segmented is not the highest dimension of the current initial tensor and is not the lowest dimension of the current initial tensor, the dimension higher than each dimension of the current dimension to be segmented and the merging dimension and the step sub-dimension of the segmentation sub-dimension, and the dimension lower than each dimension of the current dimension to be segmented can be respectively used as three dimensions of the rearrangement tensor in sequence.
The present embodiment is not explained in detail herein, and reference is made to the foregoing embodiments.
Example five
Fig. 7 is a schematic structural diagram of a tensor segmentation device according to a fifth embodiment of the present invention, where the embodiment is applicable to a case of performing stride segmentation on a multidimensional tensor, and the device may be implemented in a software and/or hardware manner and may be generally integrated in a chip. As shown in fig. 7, the apparatus includes: a tensor acquisition module 510, a tensor rearrangement module 520, and a tensor segmentation module 530, wherein,
A tensor obtaining module 510 configured to obtain an initial tensor and a segmentation parameter;
The tensor rearrangement module 520 is configured to rearrange the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearrangement tensor, so that the data to be reserved in the dimension to be segmented are adjacently arranged;
the tensor segmentation module 530 is configured to segment the rearrangement tensor according to the segmentation parameter to obtain a result tensor.
In the technical scheme of the embodiment of the invention, when the obtained initial tensor is subjected to stride segmentation, firstly, data on a dimension to be segmented is rearranged according to segmentation parameters to obtain a rearrangement tensor, wherein the data to be reserved on the dimension to be segmented in the rearrangement tensor are adjacently arranged, and then the rearrangement tensor is segmented according to the segmentation parameters to obtain a result tensor after the initial tensor is segmented. In the technical scheme, the segmentation of the rearrangement tensor can be realized through the chips which do not support the stride segmentation, and the data to be reserved in the dimension to be segmented in the rearrangement tensor are adjacently arranged, so that the segmentation times are reduced, and too many registers are not needed to be configured, thereby achieving the technical effects of realizing the stride segmentation of the tensor through the chips which do not support the stride segmentation and reducing the configuration quantity of the registers.
Optionally, the slicing parameters include: cutting step length; the tensor rearrangement module 520 is specifically configured to split the dimension to be segmented into a segmentation sub-dimension and a step sub-dimension according to the dimension size and the segmentation step length of the dimension to be segmented, and rearrange the data in the dimension to be segmented according to the splitting result, so as to obtain a rearranged tensor.
Further, the tensor rearrangement module 520 is specifically configured to sequentially obtain one data in the dimension to be segmented as current processing data; arranging the current processing data on a target element position; the index value of the target element position on the step-length sub-dimension is the index value of the current processing data on the dimension to be segmented divided by the remainder value of the segmentation step length; the index value of the target element position in the dimension of the segmentation is the quotient of the index value of the current processing data in the dimension to be segmented divided by the segmentation step length; and returning to execute the operation of sequentially acquiring one data in the dimension to be segmented as the current processing data until all the data in the dimension to be segmented are processed.
Optionally, the segmentation parameter further includes a segmentation start index value and a segmentation end index value; the tensor slicing module 530 includes: the device comprises a first segmentation unit, a second segmentation unit and a rearrangement unit. Wherein,
The first segmentation unit is arranged for carrying out first segmentation on the rearrangement tensor on the step-length sub-dimension according to the segmentation start index value of the dimension to be segmented to obtain a primary segmentation tensor;
The second segmentation unit is arranged for carrying out second segmentation on the primary segmentation tensor on the segmentation dimension according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented to obtain a secondary segmentation tensor;
And the rearrangement unit is used for rearranging the secondary segmentation tensor, enabling the secondary segmentation tensor to be matched with the shape of the initial tensor, and taking the rearranged secondary segmentation tensor as the result tensor.
Further, the first segmentation unit is specifically configured to, if the segmentation start index value of the dimension to be segmented is smaller than the segmentation step length of the dimension, segment a tensor with the index value of the step length sub-dimension being the segmentation start index value in the rearrangement tensor, as the primary segmentation tensor; if the segmentation initial index value of the dimension to be segmented is larger than or equal to the segmentation step length of the dimension, the tensor with the index value of the step length sub-dimension being the target value is segmented in the rearrangement tensor to be used as the primary segmentation tensor; the target value is the remainder value of the segmentation starting index value divided by the segmentation step length.
Further, the second segmentation unit is specifically configured to calculate a segmentation size in the segmentation dimension according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented; dividing the segmentation starting index value by the quotient value of the segmentation step length to be used as the segmentation starting index value on the segmentation dimension; and according to the segmentation starting index value and the segmentation size in the segmentation dimension, segmenting the primary segmentation tensor in the segmentation dimension to obtain a secondary segmentation tensor.
Optionally, the tensor rearrangement module 520 is specifically configured to increase the dimension size of the dimension to be segmented if the dimension size of the dimension to be segmented cannot be divided by the segmentation step size of the dimension to be segmented, so that the increased dimension size can be divided by the segmentation step size of the dimension to be segmented; splitting the dimension to be split into a split sub-dimension and a step sub-dimension according to the increased dimension size and the splitting step length; the dimension size of the step sub-dimension is the quotient of the increased dimension size and the step length, and the dimension size of the step sub-dimension is the value of the step length.
Further, the number of the dimensions to be segmented is a plurality of; the device further comprises: the cyclic segmentation module is configured to determine whether a dimension to be segmented which is not subjected to segmentation processing exists after the step of segmenting the rearrangement tensor according to the segmentation parameters to obtain a result tensor, if yes, return the step of rearranging data on the dimension to be segmented according to the initial tensor and the segmentation parameters, and rearrange the data on a target dimension to be segmented which is currently subjected to segmentation processing, wherein the initial tensor when the segmentation processing is performed on the target dimension to be segmented is the result tensor obtained when the segmentation processing is completed on a previous dimension to be segmented; if not, the result tensor obtained when the segmentation processing is completed for the previous dimension to be segmented is taken as the final segmentation result tensor.
Further, the device further comprises: and the tensor dimension adjustment module is configured to rearrange the initial tensor into an initial tensor which keeps the three-dimensional form of the dimension to be segmented if the dimension of the initial tensor is not three-dimensional when the segmentation processing is performed on each dimension to be segmented.
The tensor segmentation device can execute the tensor segmentation method provided by any embodiment of the invention, and has the corresponding functional modules and beneficial effects of the executed tensor segmentation method.
Example six
Fig. 8 is a schematic diagram of a hardware structure of a chip according to a sixth embodiment of the present invention, as shown in fig. 8, where the chip includes:
One or more processors 610, one processor 610 being illustrated in fig. 8;
a memory 620;
The processor 610 and the memory 620 in the chip may be connected by a bus or otherwise, for example in fig. 8.
The memory 620, as a non-transitory computer readable storage medium, may be used to store a software program, a computer executable program, and program instructions corresponding to a tensor segmentation method according to an embodiment of the present invention, including:
Acquiring an initial tensor and segmentation parameters;
rearranging the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, so that the data to be reserved in the dimension to be segmented are adjacently arranged;
and cutting the rearrangement tensor according to the cutting parameters to obtain a result tensor.
The processor 610 executes various functional applications of the chip and data processing by running software program instructions stored in the memory 620, i.e. implements a tensor slicing method as in any of the embodiments of the method described above.
Memory 620 may include a storage program area that may store an operating system, at least one application program required for functionality, and a storage data area; the storage data area may store data created according to the use of the chip, etc. In addition, memory 620 may include high-speed random access memory, and may also include non-transitory memory, such as at least one magnetic disk storage device, flash memory device, or other non-transitory solid state storage device.
Example seven
A seventh embodiment of the present invention also provides a storage medium containing computer-executable instructions, which when executed by a processor, are for performing a tensor slicing method, the method comprising:
Acquiring an initial tensor and segmentation parameters;
rearranging the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, so that the data to be reserved in the dimension to be segmented are adjacently arranged;
and cutting the rearrangement tensor according to the cutting parameters to obtain a result tensor.
Optionally, the computer executable instructions may also be used to perform a technical solution of a tensor slicing method provided by any embodiment of the present invention when executed by a processor.
From the above description of embodiments, it will be clear to a person skilled in the art that the present invention may be implemented by means of software and necessary general purpose hardware, but of course also by means of hardware, although in many cases the former is a preferred embodiment. Based on such understanding, the technical solution of the present invention may be embodied essentially or in a part contributing to the prior art in the form of a software product, which may be stored in a computer readable storage medium, such as a floppy disk, a Read-Only Memory (ROM), a random access Memory (Random Access Memory, RAM), a FLASH Memory (FLASH), a hard disk or an optical disk of a computer, etc., including several instructions for causing the chip to perform the method according to the embodiments of the present invention.
It should be noted that, in the embodiment of the tensor slicing apparatus, each unit and module included are only divided according to the functional logic, but not limited to the above division, so long as the corresponding function can be implemented; in addition, the specific names of the functional units are also only for distinguishing from each other, and are not used to limit the protection scope of the present invention.
Note that the above is only a preferred embodiment of the present invention and the technical principle applied. It will be understood by those skilled in the art that the present invention is not limited to the particular embodiments described herein, but is capable of various obvious changes, rearrangements and substitutions as will now become apparent to those skilled in the art without departing from the scope of the invention. Therefore, while the invention has been described in connection with the above embodiments, the invention is not limited to the embodiments, but may be embodied in many other equivalent forms without departing from the spirit or scope of the invention, which is set forth in the following claims.
Claims (10)
1. A tensor segmentation method for performing tensor stride segmentation on a chip that does not support stride segmentation, where the chip is configured with a register for each segmentation, the method comprising:
Acquiring an initial tensor and segmentation parameters;
rearranging the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, so that the data to be reserved in the dimension to be segmented are adjacently arranged;
cutting the rearrangement tensor according to the cutting parameters to obtain a result tensor;
Wherein the slicing parameters include: cutting step length;
rearranging the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, wherein the rearranging tensor comprises the following steps:
splitting the dimension to be split into a split sub-dimension and a step sub-dimension according to the dimension size and the splitting step length of the dimension to be split, and rearranging data on the dimension to be split according to a splitting result to obtain a rearranging tensor;
The rearranging the data on the dimension to be segmented according to the splitting result comprises the following steps:
Sequentially acquiring each data on the dimension to be segmented as current processing data;
Arranging the current processing data on a target element position; the index value of the target element position on the step-length sub-dimension is the index value of the current processing data on the dimension to be segmented divided by the remainder value of the segmentation step length; the index value of the target element position in the dimension of the segmentation is the quotient of the index value of the current processing data in the dimension to be segmented divided by the segmentation step length;
And returning to execute the operation of sequentially acquiring one data in the dimension to be segmented as the current processing data until all the data in the dimension to be segmented are processed.
2. The method of claim 1, wherein the segmentation parameters further comprise a segmentation start index value and a segmentation end index value;
And cutting the rearrangement tensor according to the cutting parameter to obtain a result tensor, wherein the cutting comprises the following steps:
According to the segmentation initial index value of the dimension to be segmented, performing first segmentation on the rearrangement tensor on the step-length sub-dimension to obtain a primary segmentation tensor;
Performing second segmentation on the primary segmentation tensor on the segmentation dimension according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented to obtain a secondary segmentation tensor;
rearranging the quadratic segmentation tensor, enabling the quadratic segmentation tensor to be matched with the shape of the initial tensor, and taking the rearranged quadratic segmentation tensor as the result tensor.
3. The method according to claim 2, wherein performing a first segmentation on the reorder tensor in the step-size sub-dimension according to the segmentation start index value of the dimension to be segmented to obtain a primary segmentation tensor comprises:
If the segmentation starting index value of the dimension to be segmented is smaller than the segmentation step length, the tensor with the index value of the step length sub-dimension being the segmentation starting index value is segmented in the rearrangement tensor to be used as the primary segmentation tensor;
If the segmentation initial index value of the dimension to be segmented is larger than or equal to the segmentation step length, the tensor with the index value of the step length sub-dimension as the target value is segmented in the rearrangement tensor to be used as the primary segmentation tensor; the target value is the remainder value of the segmentation starting index value divided by the segmentation step length.
4. A method according to claim 3, wherein performing a second segmentation on the primary segmentation tensor in the segmentation dimension according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented to obtain a secondary segmentation tensor comprises:
Calculating the segmentation size on the segmentation dimension according to the segmentation start index value, the segmentation end index value and the segmentation step length of the dimension to be segmented;
dividing the segmentation starting index value by the quotient value of the segmentation step length to be used as the segmentation starting index value on the segmentation dimension;
and according to the segmentation starting index value and the segmentation size in the segmentation dimension, segmenting the primary segmentation tensor in the segmentation dimension to obtain a secondary segmentation tensor.
5. The method of claim 1, wherein splitting the dimension to be split into a split sub-dimension and a step sub-dimension according to the dimension size and the split step size of the dimension to be split, comprising:
If the dimension size of the dimension to be segmented cannot be divided by the segmentation step length of the dimension to be segmented, the dimension size of the dimension to be segmented is increased, so that the increased dimension size can be divided by the segmentation step length of the dimension to be segmented;
splitting the dimension to be split into a split sub-dimension and a step sub-dimension according to the increased dimension size and the splitting step length;
the dimension size of the step sub-dimension is the quotient of the increased dimension size and the step length, and the dimension size of the step sub-dimension is the value of the step length.
6. The method according to any of claims 1-5, wherein the number of dimensions to be sliced is a plurality;
After the step of segmenting the rearrangement tensor according to the segmentation parameters to obtain a result tensor, the method further comprises the following steps:
Judging whether a dimension to be segmented which is not subjected to segmentation processing is existed, if yes, returning to the step of rearranging the data on the dimension to be segmented according to the initial tensor and the segmentation parameters, rearranging the data on a target dimension to be segmented which is subjected to segmentation processing at present, wherein the initial tensor when the segmentation processing is performed on the target dimension to be segmented is a result tensor obtained when the segmentation processing is completed on the previous dimension to be segmented; if not, the result tensor obtained when the segmentation processing is completed for the previous dimension to be segmented is taken as the final segmentation result tensor.
7. The method of claim 6, wherein when performing the slicing process for each dimension to be sliced, if the dimension of the initial tensor is not three-dimensional, the initial tensor is rearranged into an initial tensor that retains the three-dimensional form of the dimension to be sliced.
8. A tensor slicing apparatus for performing tensor stride slicing on a chip that does not support stride slicing, the chip being configured with a register for each slice, the apparatus comprising:
The tensor acquisition module is used for acquiring an initial tensor and segmentation parameters;
the tensor rearrangement module is arranged for rearranging the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, so that the data to be reserved in the dimension to be segmented are adjacently arranged;
the tensor segmentation module is used for segmenting the rearrangement tensor according to the segmentation parameters to obtain a result tensor;
Wherein the slicing parameters include: cutting step length;
rearranging the data in the dimension to be segmented according to the initial tensor and the segmentation parameter to obtain a rearranging tensor, wherein the rearranging tensor comprises the following steps:
splitting the dimension to be split into a split sub-dimension and a step sub-dimension according to the dimension size and the splitting step length of the dimension to be split, and rearranging data on the dimension to be split according to a splitting result to obtain a rearranging tensor;
The rearranging the data on the dimension to be segmented according to the splitting result comprises the following steps:
Sequentially acquiring each data on the dimension to be segmented as current processing data;
Arranging the current processing data on a target element position; the index value of the target element position on the step-length sub-dimension is the index value of the current processing data on the dimension to be segmented divided by the remainder value of the segmentation step length; the index value of the target element position in the dimension of the segmentation is the quotient of the index value of the current processing data in the dimension to be segmented divided by the segmentation step length;
And returning to execute the operation of sequentially acquiring one data in the dimension to be segmented as the current processing data until all the data in the dimension to be segmented are processed.
9. A chip comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the method of any of claims 1-7 when executing the computer program.
10. A computer readable storage medium, on which a computer program is stored, characterized in that the computer program, when being executed by a processor, implements the method according to any of claims 1-7.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202011479734.4A CN112507173B (en) | 2020-12-15 | 2020-12-15 | Tensor segmentation method, tensor segmentation device, chip and medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202011479734.4A CN112507173B (en) | 2020-12-15 | 2020-12-15 | Tensor segmentation method, tensor segmentation device, chip and medium |
Publications (2)
Publication Number | Publication Date |
---|---|
CN112507173A CN112507173A (en) | 2021-03-16 |
CN112507173B true CN112507173B (en) | 2024-05-31 |
Family
ID=74972114
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202011479734.4A Active CN112507173B (en) | 2020-12-15 | 2020-12-15 | Tensor segmentation method, tensor segmentation device, chip and medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112507173B (en) |
Families Citing this family (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN114968612B (en) * | 2021-07-14 | 2023-04-11 | 华为技术有限公司 | Data processing method, system and related equipment |
CN113722269B (en) * | 2021-08-26 | 2023-01-24 | 北京大学 | A strided slice operator processing method and device based on Ascend AI processor |
CN116802646A (en) * | 2021-12-27 | 2023-09-22 | 华为技术有限公司 | Data processing methods and devices |
CN118277711A (en) * | 2022-12-31 | 2024-07-02 | 北京希姆计算科技有限公司 | Operator segmentation method and device and operator compiling system |
CN117634711B (en) * | 2024-01-25 | 2024-05-14 | 北京壁仞科技开发有限公司 | Tensor dimension segmentation method, system, device and medium |
Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
EP0374297A1 (en) * | 1988-12-23 | 1990-06-27 | ANT Nachrichtentechnik GmbH | Method for performing a direct or reverse bidimensional spectral transform |
CN110263923A (en) * | 2019-08-12 | 2019-09-20 | 上海燧原智能科技有限公司 | Tensor convolutional calculation method and system |
CN110837483A (en) * | 2019-09-30 | 2020-02-25 | 厦门星宸科技有限公司 | Tensor dimension transformation method and device |
CN111291240A (en) * | 2018-12-06 | 2020-06-16 | 华为技术有限公司 | Method for processing data and data processing device |
CN111428879A (en) * | 2020-03-04 | 2020-07-17 | 深圳芯英科技有限公司 | Data processing method, device, chip and computer readable storage medium |
Family Cites Families (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US10776110B2 (en) * | 2018-09-29 | 2020-09-15 | Intel Corporation | Apparatus and method for adaptable and efficient lane-wise tensor processing |
-
2020
- 2020-12-15 CN CN202011479734.4A patent/CN112507173B/en active Active
Patent Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
EP0374297A1 (en) * | 1988-12-23 | 1990-06-27 | ANT Nachrichtentechnik GmbH | Method for performing a direct or reverse bidimensional spectral transform |
CN111291240A (en) * | 2018-12-06 | 2020-06-16 | 华为技术有限公司 | Method for processing data and data processing device |
CN110263923A (en) * | 2019-08-12 | 2019-09-20 | 上海燧原智能科技有限公司 | Tensor convolutional calculation method and system |
CN110837483A (en) * | 2019-09-30 | 2020-02-25 | 厦门星宸科技有限公司 | Tensor dimension transformation method and device |
CN111428879A (en) * | 2020-03-04 | 2020-07-17 | 深圳芯英科技有限公司 | Data processing method, device, chip and computer readable storage medium |
Also Published As
Publication number | Publication date |
---|---|
CN112507173A (en) | 2021-03-16 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN112507173B (en) | Tensor segmentation method, tensor segmentation device, chip and medium | |
EP3477556A1 (en) | Method and apparatus for performing operations in convolutional neural network | |
EP3489862A1 (en) | Method and apparatus for performing operation of convolutional layers in convolutional neural network | |
CN111902813A (en) | Apparatus and method for convolution operation | |
EP3489864A1 (en) | Method and apparatus for performing convolution operation on folded feature data | |
JP5344774B2 (en) | Test sequence error correction method, corresponding system and gene assembly apparatus | |
US20190129811A1 (en) | Query Fault Processing Method and Processing Apparatus | |
WO2021042594A1 (en) | Method and apparatus for data caching | |
TWI857493B (en) | Computer-implemented method, system and non-transitory computer-readable storage medium for neural network computations | |
CN113342886A (en) | Data exchange method and device | |
KR20160113826A (en) | A Method on Initial Seeds Selection of K-Means for Big Data Clustering | |
CN108205472B (en) | Memory release method, release device, computer device and readable storage medium | |
CN110807286A (en) | Structural grid identification method | |
US20210383499A1 (en) | Computer-readable recording medium recording appearance frequency calculation program, information processing apparatus, and appearance frequency calculation method | |
CN111190912B (en) | Large-transaction-oriented sharding execution method and device based on line change | |
CN112329426A (en) | Header and footer identification method, apparatus, device and medium for electronic file | |
CN107451154B (en) | Data table processing method, device and system | |
CN113778518B (en) | Data processing method, device, computer equipment and storage medium | |
US10013393B2 (en) | Parallel computer system, parallel computing method, and program storage medium | |
CN113077344B (en) | Block chain-based transaction method, device, electronic equipment and storage medium | |
CN107203550B (en) | Data processing method and database server | |
CN112597424B (en) | Tensor calculation method, device, chip and medium based on broadcast mechanism | |
US9760372B2 (en) | Parallel processing in plural processors with result register each performing associative operation on respective column data | |
CN111447269A (en) | Deserializing method and deserializing device for high-concurrency scenes in block chain transaction | |
CN106844601B (en) | Data storage method and data storage device |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
CB02 | Change of applicant information |
Address after: 214028 room 1701-1710, Jinqian block, No. 10, Hongyi Road, Xinwu District, Wuxi City, Jiangsu Province Applicant after: Wuxi Lingxi brain Technology Co.,Ltd. Address before: 202-03, conference center of Wuxi IOT industrial base, CLP Haikang, 78 Jinghui East Road, Xinwu District, Wuxi City, Jiangsu Province, 214000 Applicant before: Wuxi Lingxi brain Technology Co.,Ltd. |
|
CB02 | Change of applicant information | ||
GR01 | Patent grant | ||
GR01 | Patent grant |