Data processing method and apparatus
By transforming the self-attention computation of the Transformer model from the time domain to the frequency domain and using low-rank and sparse matrices during model fine-tuning, the problem of high computational complexity of the Transformer model is solved, achieving efficient computation in resource-constrained environments.
Patent Information
- Application Number
- PCT/CN2025/071227
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2024-03-29
- Filing Date
- 2025-01-08
- Publication Date
- 2025-10-02
AI Technical Summary
The Transformer architecture in large language models has high computational complexity in self-attention computation, especially when processing long sequences, which requires huge computational resources and limits its application in resource-constrained environments.
By performing a linear transformation on the input data and converting it to the frequency domain, and then performing key-value operations in the frequency domain, the softmax operation in self-attention computation is reduced, thus lowering the computational complexity. Furthermore, low-rank and sparse matrices are used to reduce computational load during model fine-tuning.
It significantly reduces the computational complexity of self-attention from O(n²) to O(n), thereby reducing computational resource requirements and improving the computational efficiency and storage space utilization of the model in resource-constrained environments.
Smart Images

Figure CN2025071227_02102025_PF_FP_ABST
Abstract
Description
A data processing method and device thereof
[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office on March 29, 2024, with application number 202410384576.6 and application name “A data processing method and device thereof”, the entire contents of which are incorporated by reference into this application. Technical Field
[0002] The present application relates to the field of artificial intelligence, and in particular to a data processing method and device thereof. Background Art
[0003] Large language models are increasingly being used in a wide range of applications due to their outstanding performance in natural language processing tasks, such as text translation, sentiment analysis, and question-answering systems. Among these models, the Transformer architecture has become one of the most critical technologies due to its unique self-attention mechanism. The traditional self-attention calculation is: Attention(Q,K,V) = softmax(Q T K)V;
[0004] The size of query data (Q data), key data (K data), and value data (V data) is n*d, where n is the length of the token and d is the feature dimension. n is much larger than d, for example, N is much larger than the square of d.
[0005] In the above self-attention calculation process, Q data and K data need to interact first (through product operation), and then perform softmax operation. The result of softmax operation can interact with V data (through product operation). However, when Q data and K data interact, it is the transpose of the matrix of size n*d and size d*n (the matrix of size n*d, that is, K T ) data, the complexity of this operation is n 2 d, since n is much larger than d, the complexity can be approximated to n 2 , the size of the result of the softmax operation is n*n. When the result of the softmax operation interacts with the V data, it is an operation between data of size n*n and data of size n*d. The complexity of this operation is n 2 d, since n is much larger than d, the complexity can be approximated to n 2 , that is to say, the overall computational complexity is O(n 2 ). Since n is large and the square of n is even larger, the overall computational complexity is high. Summary of the Invention
[0006] In the first aspect, the present application provides a data processing method, which is applied to the attention layer in a machine learning model. The method includes: when the attention layer performs self-attention operation on the input data, it can perform linear transformation on the input data to obtain key data, query data and value data respectively; perform frequency domain transformation on the key data and the query data respectively to obtain key data in the frequency domain and query data in the frequency domain. Due to the frequency domain conversion, unlike the existing technology of first performing operations between Q data and K data, the present application obtains a first operation result by performing operations on the key data and the value data in the frequency domain, and then obtains a second operation result by performing operations on the query data in the frequency domain and the first operation result. The second operation result can be a self-attention operation result.
[0007] In the embodiment of the present application, the Q data and the K data are converted to the frequency domain, and the softmax of the traditional attention is removed. It is equivalent to not having to interact between the Q data and the K data first, but to first interact between the K data and the V data. The size of the K data converted to the frequency domain can be n*d, and the size of the V data can be n*d. Referring to the right side of Figure 4, when the interaction between the K data and the V data converted to the frequency domain is performed, the size is d*n (the transpose of the matrix of size n*d, that is, K T ) and data of size n*d, the complexity of this operation is nd 2 Since n is much larger than d, the complexity can be approximated to n. When the interaction between the K data and V data converted to the frequency domain and the interaction between the Q data converted to the frequency domain is performed, it is an operation between data of size n*d and data of size d*d. The complexity of this operation is nd 2 , since n is much larger than d, the complexity can be approximated to n, that is, the overall computational complexity is O(n). Compared with the existing technology, n 2 The overall computational complexity of this application is relatively low.
[0008] In one possible implementation, the machine learning model also includes a first network connected before the attention layer, and the input data is a feature representation obtained by processing at least one of images, text, and audio through the first network.
[0009] In one possible implementation, the machine learning model also includes a second network connected after the attention layer, and the method also includes: obtaining a data processing result of the machine learning model through the second network based on the second operation result.
[0010] In one possible implementation, the size of the key data in the frequency domain is: d*n, the size of the query data in the frequency domain is: n*d, and the size of the value data is: n*d, where n is the length of the input data and d is the number of feature dimensions of the input data.
[0011] In one possible implementation, obtaining a first operation result by operating on the key data and the value data in the frequency domain includes: processing the key data and the value data in the frequency domain based on a preset function to obtain processed key data; fusing the processed key data and the value data to obtain the first operation result; obtaining a second operation result by operating on the query data in the frequency domain and the first operation result includes: processing the query data in the frequency domain based on the preset function to obtain processed query data; fusing the processed query data and the first operation result to obtain the second operation result.
[0012] In one possible implementation, the preset function is an exponential function, a cosine function, or a sine function. That is, the first operation result is obtained by operating the key data and the value data in the frequency domain, including: processing the key data in the frequency domain by an exponential function, a cosine function, or a sine function (for example, the key data in the frequency domain can be a vector or a matrix and include multiple elements, and each element can be processed by an exponential function, a cosine function, or a sine function) to obtain processed key data; using the fusion result of the processed key data and the value data as the first operation result; the second operation result is obtained by operating the query data in the frequency domain and the first operation result, including: processing the query data in the frequency domain by an exponential function, a cosine function, or a sine function to obtain processed query data; using the fusion result of the processed query data and the first operation result as the second operation result.
[0013] In a possible implementation, performing frequency domain transformation on the key data and the query data respectively includes: performing frequency domain transformation on the feature dimension of the key data; and performing frequency domain transformation on the feature dimension of the query data.
[0014] When fine-tuning a pre-trained model, especially for the transformation matrix used in linear transformation, retraining not only increases the consumption of computing resources, but also because the existing pre-trained model has encoded a lot of knowledge and rules, retraining may also cause these valuable information to be lost. In the embodiment of the present application, considering the high cost of full fine-tuning, we choose to superimpose low-rank matrices and / or sparse matrices on the original parameter matrix of the model. These newly added matrices have a size much smaller than the original parameter matrix, so the computing resources required for fine-tuning are greatly reduced.
[0015] Specifically, in one possible implementation, the input data of the attention layer can be linearly transformed by a first transformation matrix to obtain first data; and the input data of the attention layer can be linearly transformed by a second transformation matrix to obtain second data; wherein the second transformation matrix is a sparse matrix or a low-rank matrix. The first data and the second data are fused to obtain key data, query data or value data, and when fine-tuning the machine learning model, the second transformation matrix is updated while keeping the first transformation matrix unchanged. The first transformation matrix can be one of a K transformation matrix, a Q transformation matrix or a V transformation matrix.
[0016] Among them, a matrix with a rank less than 1 / 2 of the matrix dimension can be considered a low-rank matrix, and a matrix with a percentage of non-zero elements less than 50% can be considered a sparse matrix.
[0017] Since the number of parameters that need to be updated in the second transformation matrix (low-rank matrix or sparse matrix) is less than that in the first transformation matrix, and when fine-tuning the model, the parameters of the first transformation matrix remain fixed, while the parameters of the second transformation matrix are updated, the overall amount of computation is reduced. Specifically, during the fine-tuning process, only parameters of these small-sized low-rank matrices and sparse matrices are updated. The low-rank matrix allows the core information of the original transformation matrix to be captured and modified through a small number of parameters, while the sparse matrix further enhances the model's focus on key information on this basis, ensuring that important features are not ignored. In this way, the computational cost caused by fine-tuning can be significantly reduced while maintaining model performance.
[0018] In one possible implementation, the second transformation matrix is the product of the first submatrix and the second submatrix, the size of the first submatrix is d*r, the size of the second submatrix is r*d, and d is greater than r. Since d is greater than r (for example, d is much greater than r, and the total number of parameters of the first submatrix and the second submatrix is less than that of the first transformation matrix), the first submatrix and the second submatrix are long strip matrices, and therefore the number of parameters contained is much smaller than that of the first transformation matrix, and the product of the first submatrix and the second submatrix is a low-rank matrix. Therefore, when fine-tuning the model, the first submatrix and the second submatrix can be updated.
[0019] In a second aspect, the present application provides a data processing device for use in an attention layer in a machine learning model, the device comprising:
[0020] A processing module is used to perform a linear transformation on the input data of the attention layer to obtain key data, query data and value data respectively; perform a frequency domain transformation on the key data and the query data respectively to obtain key data in the frequency domain and query data in the frequency domain; obtain a first operation result by performing an operation on the key data and the value data in the frequency domain; and obtain a second operation result by performing an operation on the query data in the frequency domain and the first operation result.
[0021] In one possible implementation, the machine learning model also includes a first network connected before the attention layer, and the input data is a feature representation obtained by processing at least one of images, text, and audio through the first network.
[0022] In one possible implementation, the machine learning model further includes a second network connected after the attention layer, and the processing module is further configured to:
[0023] According to the second operation result, the data processing result of the machine learning model is obtained through the second network.
[0024] In one possible implementation, the size of the key data in the frequency domain is: d*n, the size of the query data in the frequency domain is: n*d, and the size of the value data is: n*d, where n is the length of the input data and d is the number of feature dimensions of the input data.
[0025] In a possible implementation, the processing module is specifically configured to:
[0026] Based on a preset function, processing the key data and the value data in the frequency domain to obtain processed key data;
[0027] fusing the processed key data and the value data to obtain the first operation result;
[0028] Based on the preset function, the query data in the frequency domain is processed to obtain processed query data;
[0029] The processed query data and the first operation result are fused to obtain the second operation result.
[0030] In a possible implementation, the preset function is an exponential function, a cosine function, or a sine function.
[0031] In a possible implementation, the processing module is specifically configured to: perform frequency domain transformation on the feature dimension of the key data;
[0032] Perform frequency domain transformation on the feature dimension of the query data.
[0033] In a possible implementation, the processing module is specifically configured to:
[0034] Performing a linear transformation on the input data of the attention layer using a first transformation matrix to obtain first data;
[0035] Performing a linear transformation on the input data of the attention layer using a second transformation matrix to obtain second data; wherein the second transformation matrix is a sparse matrix or a low-rank matrix;
[0036] Merging the first data and the second data to obtain key data, query data, or value data;
[0037] The processing module is further configured to:
[0038] When fine-tuning the machine learning model, the second transformation matrix is updated while keeping the first transformation matrix unchanged.
[0039] In a possible implementation, the second transformation matrix is the product of a first submatrix and a second submatrix, the size of the first submatrix is d*r, the size of the second submatrix is r*d, and d is greater than r;
[0040] The processing module is specifically used to:
[0041] The first sub-matrix and the second sub-matrix are updated.
[0042] In a third aspect, an embodiment of the present application provides a data processing device, which may include a memory, a processor, and a bus system, wherein the memory is used to store programs, and the processor is used to execute the programs in the memory to perform the first aspect and any optional method thereof.
[0043] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which a computer program is stored. When the computer-readable storage medium is run on a computer, the computer executes the above-mentioned first aspect and any optional method thereof.
[0044] In a fifth aspect, an embodiment of the present application provides a computer program, which, when executed on a computer, enables the computer to execute the above-mentioned first aspect and any optional method thereof.
[0045] In a sixth aspect, the present application provides a chip system comprising a processor for supporting an execution device or a training device in implementing the functions described in the aforementioned aspects, such as transmitting or processing data or information described in the aforementioned methods. In one possible design, the chip system further comprises a memory for storing program instructions and data necessary for the execution device or the training device. The chip system may consist of a single chip or may include a chip and other discrete components. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] Figure 1 is a schematic diagram of the structure of the artificial intelligence main framework;
[0047] FIG2 is a schematic diagram of a system architecture of the present application;
[0048] FIG3 is a flowchart of a data processing method provided in an embodiment of the present application;
[0049] FIG4 is a flowchart of a data processing method provided in an embodiment of the present application;
[0050] FIG5 is a flowchart of a data processing method provided in an embodiment of the present application;
[0051] FIG6 is a flowchart of a data processing method provided in an embodiment of the present application;
[0052] FIG7 is a schematic structural diagram of a data processing device provided in an embodiment of the present application;
[0053] FIG8 is a schematic structural diagram of a terminal device provided in an embodiment of the present application;
[0054] FIG9 is a schematic diagram of a structure of a server provided in an embodiment of the present application;
[0055] FIG10 is a schematic diagram of the structure of a chip provided in an embodiment of the present application. DETAILED DESCRIPTION
[0056] The following describes the embodiments of the present invention in conjunction with the accompanying drawings. The terms used in the embodiments of the present invention are only used to explain the specific embodiments of the present invention, and are not intended to limit the present invention.
[0057] The embodiments of the present application are described below in conjunction with the accompanying drawings. Those skilled in the art will appreciate that, with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0058] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequential order. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances, and this is merely a way of distinguishing the objects of the same attributes when describing them in the embodiments of the present application. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, so that the process, method, system, product or equipment comprising a series of units need not be limited to those units, but may include other units that are not clearly listed or inherent to these processes, methods, products or equipment.
[0059] As used herein, the terms "substantially," "about," and similar terms are used as terms of approximation, not as terms of degree, and are intended to take into account the inherent variations in measurements or calculations that one of ordinary skill in the art would recognize. Furthermore, the use of "may" when describing embodiments of the present invention refers to "one or more possible embodiments." As used herein, the terms "use," "using," and "used" may be considered synonymous with the terms "utilize," "utilizing," and "utilized," respectively. Additionally, the term "exemplary" is intended to refer to an example or illustration.
[0060] First, let's describe the overall workflow of an AI system. See Figure 1, which shows a schematic diagram of the AI framework. This framework will be explained from two perspectives: the "intelligent information chain" (horizontal axis) and the "IT value chain" (vertical axis). The "intelligent information chain" reflects the entire process from data acquisition to processing. For example, it could be the general process of intelligent information perception, intelligent information representation and formation, intelligent reasoning, intelligent decision-making, and intelligent execution and output. Throughout this process, data undergoes a condensed journey from "data-information-knowledge-wisdom." The "IT value chain," spanning the underlying infrastructure of human intelligence, information (provided and processed by technology), and the system's industrial ecosystem, reflects the value that AI brings to the information technology industry.
[0061] (1) Infrastructure
[0062] Infrastructure provides computing power for AI systems, enabling communication with the outside world and supporting this through a foundational platform. External communication occurs through sensors; computing power is provided by intelligent chips (CPUs, NPUs, GPUs, ASICs, FPGAs, and other hardware accelerators). The foundational platform includes a distributed computing framework and network-related platform guarantees and support, including cloud storage and computing, and interconnected networks. For example, sensors communicate with the outside world to acquire data, which is then fed into the intelligent chips within the distributed computing system provided by the foundational platform for computation.
[0063] (2) Data
[0064] Data above the infrastructure layer represents data sources for AI. This data includes graphics, images, voice, and text, as well as IoT data from traditional devices. This includes business data from existing systems and sensor data such as force, displacement, liquid level, temperature, and humidity.
[0065] (3) Data processing
[0066] Data processing generally includes data training, machine learning, deep learning, search, reasoning, decision-making, etc.
[0067] Among them, machine learning and deep learning can symbolize and formalize data for intelligent information modeling, extraction, preprocessing, and training.
[0068] Reasoning refers to the process of simulating human intelligent reasoning in computers or intelligent systems, using formalized information to perform machine thinking and solve problems based on reasoning control strategies. Typical functions are search and matching.
[0069] Decision-making refers to the process of making decisions after intelligent information is reasoned, and usually provides functions such as classification, sorting, and prediction.
[0070] (4) General ability
[0071] After the data has undergone the data processing mentioned above, some general capabilities can be further formed based on the results of the data processing, such as algorithms or a general system, for example, translation, text analysis, computer vision processing, speech recognition, image recognition, etc.
[0072] (5) Smart products and industry applications
[0073] Smart products and industry applications refer to the products and applications of artificial intelligence systems in various fields. They are the encapsulation of the overall artificial intelligence solution, which productizes intelligent information decision-making and realizes practical application. Its application areas mainly include: smart terminals, smart transportation, smart medical care, autonomous driving, smart cities, etc.
[0074] It should be understood that the steps related to the model reasoning process in the embodiments of this application involve AI-related operations. When performing AI operations, the instruction execution architecture of the terminal device and server is not limited to the processor-memory architecture described above. The system architecture provided by the embodiments of this application is described in detail below with reference to Figure 2.
[0075] FIG2 is a schematic diagram of the system architecture provided by an embodiment of the present application. As shown in FIG2 , the system architecture 500 includes an execution device 510 , a training device 520 , a database 530 , a client device 540 , a data storage system 550 , and a data acquisition system 560 .
[0076] The execution device 510 includes a calculation module 511, an I / O interface 512, a pre-processing module 513, and a post-processing module 514. The calculation module 511 may include the target model / rule 501, and the pre-processing module 513 and the post-processing module 514 are optional.
[0077] The execution device 510 may be a terminal device or a server that runs the aforementioned composite application.
[0078] The data acquisition device 560 is used to collect training samples. The training samples can be program files (including program codes and program input data), etc. After collecting the training samples, the data acquisition device 560 stores them in the database 530.
[0079] The training device 520 can train the neural network based on the training samples maintained in the database 530 to obtain the target model / rule 501 (such as the machine learning model in the embodiment of the present application).
[0080] It should be noted that, in actual applications, the training samples maintained in the database 530 may not all be collected by the data acquisition device 560, but may also be received from other devices. It should also be noted that the training device 520 may not train the target model / rule 501 entirely based on the training samples maintained in the database 530, but may also obtain training samples from the cloud or other places for model training. The above description should not be used as a limitation on the embodiments of the present application.
[0081] The target model / rule 501 obtained through training with the training device 520 can be applied to different systems or devices, such as the execution device 510 shown in FIG2 . The execution device 510 can be a terminal, such as a mobile phone terminal, a tablet computer, a laptop computer, an augmented reality (AR) / virtual reality (VR) device, an in-vehicle terminal, etc., or a server, etc.
[0082] Specifically, the training device 520 may transfer the trained model to the execution device 510 .
[0083] In Figure 2, the execution device 510 is configured with an input / output (I / O) interface 512 for data interaction with external devices. The user can input data into the I / O interface 512 through the client device 540 (for example, data input into the machine learning model in the embodiment of the present application, etc.).
[0084] Preprocessing module 513 and preprocessing module 514 are used to preprocess the input data received by I / O interface 512. It should be understood that preprocessing module 513 and preprocessing module 514 may be absent or only one preprocessing module may be present. If preprocessing module 513 and preprocessing module 514 are absent, computing module 511 may be used directly to process the input data.
[0085] When the execution device 510 preprocesses the input data, or when the computing module 511 of the execution device 510 performs calculations and other related processing, the execution device 510 can call the data, code, etc. in the data storage system 550 for corresponding processing, and can also store the data, instructions, etc. obtained from the corresponding processing in the data storage system 550.
[0086] Finally, the I / O interface 512 provides the processed results to the client device 540 and thus to the user.
[0087] In the scenario shown in FIG. 2 , the user can manually input data, and this "manual input data" can be operated through the interface provided by I / O interface 512. Alternatively, client device 540 can automatically send input data to I / O interface 512. If user authorization is required for client device 540 to automatically send input data, the user can set the corresponding permissions in client device 540. The user can view the output of execution device 510 on client device 540, which can be presented in a display, sound, action, or other specific form. Client device 540 can also serve as a data acquisition terminal, collecting input data and output results from I / O interface 512 as new sample data and storing them in database 530. Of course, collection can also be performed without client device 540, with I / O interface 512 directly storing the input data and output results from I / O interface 512 as new sample data in database 530.
[0088] It is worth noting that FIG2 is merely a schematic diagram of a system architecture provided by an embodiment of the present application, and the positional relationships between the devices, components, modules, etc. shown in the figure do not constitute any limitation. For example, in FIG2 , the data storage system 550 is an external memory relative to the execution device 510. In other cases, the data storage system 550 can also be placed in the execution device 510. It should be understood that the execution device 510 can be deployed in the client device 540.
[0089] From the inference side of the model:
[0090] In the embodiment of the present application, the computing module 511 of the above-mentioned execution device 510 can obtain the code stored in the data storage system 550 to implement the steps related to the model reasoning process in the embodiment of the present application.
[0091] In an embodiment of the present application, the computing module 511 of the execution device 510 may include a hardware circuit (such as an application specific integrated circuit (ASIC), a field-programmable gate array (FPGA), a general-purpose processor, a digital signal processor (DSP), a microprocessor or a microcontroller, etc.), or a combination of these hardware circuits. For example, the training device 520 may be a hardware system with an instruction execution function, such as a CPU, DSP, etc., or a hardware system without an instruction execution function, such as an ASIC, FPGA, etc., or a combination of the above-mentioned hardware systems without an instruction execution function and hardware systems with an instruction execution function.
[0092] Specifically, the computing module 511 of the execution device 510 can be a hardware system with an execution instruction function, and the steps related to the model reasoning process provided in the embodiment of the present application can be software codes stored in the memory. The computing module 511 of the execution device 510 can obtain the software code from the memory and execute the obtained software code to implement the steps related to the model reasoning process provided in the embodiment of the present application.
[0093] It should be understood that the computing module 511 of the execution device 510 can be a combination of a hardware system that does not have the function of executing instructions and a hardware system that has the function of executing instructions. Some of the steps related to the model reasoning process provided in the embodiment of the present application can also be implemented by the hardware system that does not have the function of executing instructions in the computing module 511 of the execution device 510, which is not limited here.
[0094] From the training side of the model:
[0095] In an embodiment of the present application, the above-mentioned training device 520 can obtain the code stored in the memory (not shown in Figure 2, which can be integrated into the training device 520 or deployed separately from the training device 520) to implement the steps related to model training in the embodiment of the present application.
[0096] In an embodiment of the present application, the training device 520 may include a hardware circuit (such as an application specific integrated circuit (ASIC), a field-programmable gate array (FPGA), a general-purpose processor, a digital signal processor (DSP), a microprocessor or a microcontroller, etc.), or a combination of these hardware circuits. For example, the training device 520 may be a hardware system with an instruction execution function, such as a CPU, DSP, etc., or a hardware system without an instruction execution function, such as an ASIC, FPGA, etc., or a combination of the above-mentioned hardware systems without an instruction execution function and hardware systems with an instruction execution function.
[0097] It should be understood that the training device 520 can be a combination of a hardware system that does not have the function of executing instructions and a hardware system that has the function of executing instructions. Some of the steps related to model training provided in the embodiments of the present application can also be implemented by the hardware system in the training device 520 that does not have the function of executing instructions, which is not limited here.
[0098] The embodiments of the present application can be applied to scenarios such as text processing, image processing, audio processing, or multimodal data processing.
[0099] For example, text processing can be text classification, sentiment analysis, dialogue generation, text entity recognition, keyword extraction, summary extraction, text translation, code completion, etc.
[0100] For example, image processing can be image classification, object detection, image segmentation, image generation, image restoration, image enhancement, image style transfer, etc.
[0101] For example, audio processing can be audio enhancement, speech recognition, speech synthesis, voiceprint recognition, etc.
[0102] For example, multimodal data processing can be text-based image generation, image-text retrieval, etc.
[0103] Since the embodiments of the present application involve the application of a large number of neural networks, in order to facilitate understanding, the relevant terms and related concepts such as neural networks involved in the embodiments of the present application are first introduced below.
[0104] (1) Neural Network
[0105] A neural network can be composed of neural units. A neural unit can refer to an operation unit that takes xs (i.e., input data) and intercept 1 as input. The output of the operation unit can be:
[0106] Where s = 1, 2, ... n, n is a natural number greater than 1, Ws is the weight of xs, and b is the bias of the neural unit. f is the activation function of the neural unit, which is used to introduce nonlinear characteristics into the neural network to convert the input signal of the neural unit into the output signal. The output signal of the activation function can be used as the input of the next convolutional layer, and the activation function can be a sigmoid function. A neural network is a network formed by connecting multiple single neural units mentioned above, that is, the output of one neural unit can be the input of another neural unit. The input of each neural unit can be connected to the local receptive field of the previous layer to extract the features of the local receptive field. The local receptive field can be an area composed of several neural units.
[0107] (2) Transformer Network
[0108] The transformer network may include an embedding layer and at least one transformer layer, wherein the at least one transformer layer may be N transformer layers (N is an integer greater than 0), wherein each transformer layer includes an attention layer, an add&norm layer, a feedforward layer, and an add&norm layer that are adjacent in sequence. In the embedding layer, the current input is embedded to obtain multiple feature vectors; in the attention layer, P input vectors are obtained from the previous layer of the transformer layer, and with any first input vector among the P input vectors as the center, based on the correlation between each input vector within a preset attention window and the first input vector, an intermediate vector corresponding to the first input vector is obtained, thereby determining the P intermediate vectors corresponding to the P input vectors; in the pooling layer, the P intermediate vectors are merged into Q output vectors, wherein the multiple output vectors obtained by the last transformer layer in at least one transformer layer are used as feature representations of the current input.
[0109] Next, the above steps are introduced in detail with reference to specific examples.
[0110] First, in the embedding layer, the current input is embedded to obtain multiple feature vectors.
[0111] The embedding layer can be called the input embedding layer. The current input can be a text input, for example, a paragraph of text or a sentence. The text can be Chinese text, English text, or text in other languages. After obtaining the current input, the embedding layer can embed each word in the current input to obtain a feature vector for each word. In some embodiments, as shown in Figure 4, the embedding layer includes an input embedding layer and a positional encoding layer. In the input embedding layer, word embedding can be performed on each word in the current input to obtain a word embedding vector for each word. In the positional encoding layer, the position of each word in the current input can be obtained, and then the position vector of each word can be processed. In some examples, the position of each word can be the absolute position of each word in the current input. Taking the current input "How many numbers should I return Huabei" as an example, the position of "how many" can be represented as the first position, the position of "number" can be represented as the second position, and so on. In some examples, the position of each word can be the relative position between each word. Still taking the current input of "On which date should I repay" as an example, the position of "on which date" can be expressed as before "number", the position of "number" can be expressed as after "on which date" and before "should", etc. When the word embedding vector and position vector of each word in the current input are obtained, the position vector of each word and the corresponding word embedding vector can be combined to obtain the feature vector of each word, that is, to obtain multiple feature vectors corresponding to the current input. Multiple feature vectors can be represented as an embedding matrix with a preset dimension. The number of feature vectors in the multiple feature vectors can be set to M, and the preset dimension can be H, so that the multiple feature vectors can be represented as an M×H embedding matrix.
[0112] (3) Attention mechanism
[0113] The attention mechanism mimics the internal process of biological observation behavior, namely, a mechanism that aligns internal experience and external sensations to increase the observation precision of certain areas. It can quickly filter out high-value information from a large amount of information using limited attention resources. The attention mechanism can quickly extract important features from sparse data and is therefore widely used in natural language processing tasks, especially machine translation. The self-attention mechanism is an improvement on the attention mechanism, which reduces dependence on external information and is better at capturing the internal correlation of data or features. The essential idea of the attention mechanism can be rewritten as the following formula:
[0114] Here, Lx = ||Source|| represents the length of the Source. This formula implies that the elements in the Source are imagined to consist of a series of data pairs. Given a Query element in the target, the similarity or correlation between the Query and each Key is calculated to obtain the weight coefficient for each Key's corresponding Value. The weighted sum of the Values is then taken to obtain the final Attention value. Essentially, the Attention mechanism performs a weighted sum of the Values of the Source elements, with the Query and Key used to calculate the weight coefficient for the corresponding Value. Conceptually, Attention can be understood as selectively filtering out a small amount of important information from a large amount of information and focusing on this important information, while ignoring the majority of less important information. This focusing process is reflected in the calculation of the weight coefficients: the larger the weight, the more focus is placed on the corresponding Value. In other words, the weight represents the importance of the information, while the Value represents the corresponding information. The self-attention mechanism can be understood as internal attention. The attention mechanism occurs between the Query element of the Target and all elements of the Source. The self-attention mechanism refers to the attention mechanism that occurs between the internal elements of the Source or the internal elements of the Target. It can also be understood as the attention calculation mechanism in the special case of Target = Source. The specific calculation process is the same, only the calculation object has changed.
[0115] About Query (hereinafter referred to as Q), Key (hereinafter referred to as K) and Value (hereinafter referred to as V):
[0116] Q, K, and V are all derived from the input features themselves, representing vectors generated from them. V can be considered a vector representing a single input feature. Directly inputting a set of V into a network for training is equivalent to not incorporating attention. However, if attention is introduced, the set of V is multiplied by a set of weights W(Q, K), allowing attention to be focused on local input features. W(Q, K) calculates the similarity between Q and K. Common approaches include dot-product attention and additive attention. The former is a dot product operation, while the latter is calculated through a hidden layer. Specifically, V is a vector representing the input features, and Q and K are feature vectors for calculating the attention weights. Both are derived from the input features. Attention(Q, K, V) multiplies V by the corresponding weights based on the degree of attention. In the attention mechanism, Q, K, and V are calculated by calculating the similarity between the current query and all keys. This similarity is then passed through a softmax layer to obtain a set of weights. The sum of these weights and the corresponding values yields the value under attention.
[0117] (4) Pre-trained language model
[0118] A pretrained language model is a natural language sequence encoder that encodes each word in a natural language sequence into a vector representation for prediction. Its training consists of two phases. In the pre-training phase, the model is trained on a large amount of unsupervised text for language modeling tasks, thereby learning a word representation. In the fine-tuning phase, the model is initialized using the parameters learned in the pre-training phase and trained in a relatively short number of steps on downstream tasks such as text classification and sequence labeling. This allows the semantic information gained from pre-training to be successfully transferred to downstream tasks.
[0119] (5) Backpropagation algorithm
[0120] Convolutional neural networks can use the back propagation (BP) algorithm to correct the size of the parameters in the initial super-resolution model during training, reducing the reconstruction error loss of the super-resolution model. Specifically, the forward propagation of the input signal to the output generates an error loss. This error loss information is then backpropagated to update the parameters of the initial super-resolution model, thereby converging the error loss. The BP algorithm is a backward propagation movement dominated by the error loss, aiming to obtain the optimal super-resolution model parameters, such as the transformation matrix.
[0121] (6) Loss function
[0122] During the training of a deep neural network, because we want the output of the deep neural network to be as close as possible to the desired predicted value, we can compare the current network's predicted value with the desired target value and then update the weight vector of each layer of the neural network based on the difference between the two. (Of course, there is usually an initialization process before the first update, which is to pre-configure the parameters for each layer in the deep neural network.) For example, if the network's predicted value is too high, the weight vector is adjusted to make it predict a lower value. This adjustment is continued until the deep neural network can predict the desired target value or a value very close to the desired target value. Therefore, it is necessary to predefine "how to compare the difference between the predicted value and the target value." This is the loss function (or objective function), which is an important equation used to measure the difference between the predicted value and the target value. For example, the loss function output value (loss) indicates a greater difference, so training a deep neural network becomes a process of minimizing this loss as much as possible.
[0123] (7) Frequency domain transformation:
[0124] Frequency domain transforms are mathematical tools used to analyze the frequency components of a signal (or data sequence). The most common frequency domain transforms include the Fourier transform and the discrete cosine transform (DCT). Frequency domain transforms can be used to convert a signal from the time domain (or spatial domain) to the frequency domain, allowing the signal's characteristics and patterns to be more intuitively expressed in terms of frequency. Frequency domain transforms have a wide range of applications in fields such as signal processing, image processing, and speech analysis, including in tasks such as data compression, denoising, and feature extraction.
[0125] Large language models, due to their outstanding performance in natural language processing tasks, are increasingly being used in a wide range of applications, such as text translation, sentiment analysis, and question-answering systems. Among these models, the Transformer architecture, with its unique self-attention mechanism, has become one of the most critical technologies. This architecture is able to capture long-range data dependencies, which is crucial for understanding and generating natural language text. However, Transformer models often require significant computational resources to handle complex tasks. In particular, the computational complexity of their self-attention mechanism is proportional to the square of the input length, which means that the required computational resources and processing time increase dramatically when processing long sequences. This high computational complexity limits the application of Transformer models in resource-constrained environments, such as on-device inference in mobile devices and embedded systems. Running large Transformer models on on-device devices requires both high computational efficiency and minimal storage space.
[0126] In order to solve the above problems, the present invention provides a data processing method. The model training method of the present invention is described in detail below with reference to the accompanying drawings.
[0127] Refer to Figure 3, which is a flow chart of a data processing method provided in an embodiment of the present application. As shown in Figure 3, a data processing method provided in an embodiment of the present application may include steps 301 to 303, and these steps are described in detail below.
[0128] 301. Perform a linear transformation on the input data of the attention layer to obtain key data, query data, and value data respectively.
[0129] The embodiments of the present application can be applied to the processing of a machine learning model, wherein the machine learning model may include a first network, an attention layer, and a second network.
[0130] The attention layer can be a network layer in a transformer layer of a machine learning model.
[0131] The first network layer may be a network connected before the attention layer in the machine learning model, and the second network layer may be a network connected after the attention layer in the machine learning model. For example, the first network may include the input projection layer of the machine learning model and some network layers in the feature extraction network, and the second network may include some network layers in the feature extraction network of the machine learning model (when the attention layer is the last layer of the feature extraction network, the second network may not include the network layers in the feature extraction network of the machine learning model) and the task network including the output projection layer.
[0132] In one possible implementation, the target data may be data input into a machine learning model.
[0133] For example, the target data is image data, and the machine learning model can be used to perform image processing tasks, and the processing result obtained by processing the target data through the machine learning model is the processing result of the image processing task.
[0134] For example, the target data is audio data, and the machine learning model can be used to perform an audio processing task, and the processing result obtained by processing the target data through the machine learning model is the processing result of the audio processing task.
[0135] For example, the target data is text data, and the machine learning model can be used to perform a text processing task, and the processing result obtained by processing the target data through the machine learning model is the processing result of the text processing task.
[0136] For example, image processing tasks may include image classification, target recognition, image enhancement (such as but not limited to super-resolution, denoising, rain removal, deblurring, demosaicing, etc.), image generation, and the like.
[0137] For example, audio processing tasks can include speech-to-text, audio enhancement, audio synthesis, and the like.
[0138] For example, text processing tasks can be summary generation, dialogue response, text generation and other tasks.
[0139] In one possible implementation, the attention layer may be a network layer based on the attention mechanism. For example, the attention layer may belong to a transformer layer, and the transformer layer may include an attention layer, an add and normalization (add&norm) layer, a feed forward net (FFN), and an add and normalization layer that are adjacent in sequence.
[0140] Among them, the attention layer obtains N input vectors Xl from the layer above it, and the N input vectors Xl can be expressed as a matrix X. The attention layer adopts a self-attention mechanism to transform each vector based on the correlation between vectors to obtain N output vectors, which can be expressed as a matrix Y. It can be understood that when the attention layer is a layer directly connected to the embedding layer, the input vector it obtains (or it can be called input data) is the embedding vector output by the embedding layer; when the attention layer is an attention layer included in the subsequent transformer layer, the input vector it obtains (or it can be called input data) is the output vector of the previous transformer layer. The attention layer can include multiple attention heads.
[0141] In an embodiment of the present application, a linear transformation may be performed on the input data of the attention layer to obtain key data, query data, and value data, respectively. The linear transformation may be performed on at least one of the multiple attention heads in the attention layer.
[0142] The attention head can use the transformation matrix Q, transformation matrix K and transformation matrix V to N input vectors respectively<X1,X2,…,XN> Each input vector Xi is transformed to obtain the q vector (that is, query data), k vector (that is, key data) and v vector (that is, value data) corresponding to each input vector.
[0143] In terms of operation, the input matrix X composed of N input vectors can be linearly transformed using transformation matrix Q, transformation matrix K and transformation matrix V respectively to obtain the Q matrix, K matrix and V matrix of the input matrix respectively. Then the matrix can be split respectively to obtain the q vector, k vector and v vector corresponding to each input vector.
[0144] 302. Perform frequency domain transformation on the key data and the query data respectively to obtain key data in the frequency domain and query data in the frequency domain.
[0145] The frequency domain transform may be, but is not limited to, Fourier transform and discrete cosine transform (DCT). Through the frequency domain transform, a signal can be converted from the time domain (or spatial domain) to the frequency domain.
[0146] In one possible implementation, the frequency domain transformation is specifically a frequency domain transformation performed on the feature dimension of the data. For example, the size of the key data can be n*d, the size of the query data can be d*n, and the size of the value data can be n*d, where n can be the length of the input data (e.g., the input token vector) and d is the length on the feature dimension.
[0147] In a possible implementation, the frequency domain transformation does not change the size of the data. The size of the key data in the frequency domain obtained by the frequency domain transformation is: d*n, and the size of the query data in the frequency domain obtained by the frequency domain transformation is: n*d.
[0148] 303. Obtain a first operation result by performing an operation on the key data and the value data in the frequency domain.
[0149] 304. Obtain a second operation result by performing an operation on the query data in the frequency domain and the first operation result.
[0150] The traditional self-attention calculation is: Attention(Q,K,V)=softmax(Q T K)V;
[0151] The size of Q data, K data, and V data is n*d, where n is the length of the token and d is the feature dimension. N is much larger than d, for example, N is much larger than the square of d.
[0152] In the above self-attention calculation process, Q data and K data need to interact first (through product operation), and then perform softmax operation. The result of softmax operation can interact with V data (through product operation). However, referring to the left side of Figure 4, when Q data and K data interact, the size is n*d and the size is d*n (the transpose of the matrix of size n*d, that is, K T ) data, the complexity of this operation is n 2 d, since n is much larger than d, the complexity can be approximated to n 2 , the size of the result of the softmax operation is n*n. When the result of the softmax operation interacts with the V data, it is an operation between data of size n*n and data of size n*d. The complexity of this operation is n 2 d, since n is much larger than d, the complexity can be approximated to n 2 , that is to say, the overall computational complexity is O(n 2 ). Since n is large and the square of n is even larger, the overall computational complexity is high.
[0153] In the embodiment of the present application, the Q data and the K data are converted to the frequency domain, and the softmax of the traditional attention is removed. It is equivalent to not having to interact between the Q data and the K data first, but to first interact between the K data and the V data. The size of the K data converted to the frequency domain can be n*d, and the size of the V data can be n*d. Referring to the right side of Figure 4, when the interaction between the K data and the V data converted to the frequency domain is performed, the size is d*n (the transpose of the matrix of size n*d, that is, K T ) and data of size n*d, the complexity of this operation is nd 2 Since n is much larger than d, the complexity can be approximated to n. When the interaction between the K data and V data converted to the frequency domain and the interaction between the Q data converted to the frequency domain is performed, it is an operation between data of size n*d and data of size d*d. The complexity of this operation is nd 2 , since n is much larger than d, the complexity can be approximated to n, that is, the overall computational complexity is O(n). Compared with the existing technology, n 2The overall computational complexity of this application is relatively low.
[0154] Among them, taking the attention layer belonging to the transformer layer as an example, the embodiment of the present application uses frequency domain transformation to perform equivalent changes on the feature dimensions of Attention in the transformer, so that the softmax operation in the attention can be eliminated. The attention mechanism in the transformer model is transformed into the frequency domain. Through this transformation, the softmax operation with high complexity in the attention calculation is removed, thereby significantly reducing the amount of calculation of the model in long sequence processing. The core of this method is to use the linear characteristics of the frequency domain to perform equivalent transformation on the feature dimensions of the attention, realizing the computational complexity from O(n 2 ) is reduced to O(n).
[0155] In one possible implementation, the key data and the value data in the frequency domain can be processed based on a preset function to obtain processed key data; the processed key data and the value data are fused to obtain the first operation result; and the second operation result is obtained by performing an operation on the query data in the frequency domain and the first operation result, including: processing the query data in the frequency domain based on the preset function to obtain processed query data; and fusing the processed query data and the first operation result to obtain the second operation result.
[0156] In one possible implementation, the preset function is an exponential function, a cosine function, or a sine function. That is, the first operation result is obtained by operating the key data and the value data in the frequency domain, including: processing the key data in the frequency domain by an exponential function, a cosine function, or a sine function (for example, the key data in the frequency domain can be a vector or a matrix and include multiple elements, and each element can be processed by an exponential function, a cosine function, or a sine function) to obtain processed key data; using the fusion result of the processed key data and the value data as the first operation result; the second operation result is obtained by operating the query data in the frequency domain and the first operation result, including: processing the query data in the frequency domain by an exponential function, a cosine function, or a sine function to obtain processed query data; using the fusion result of the processed query data and the first operation result as the second operation result.
[0157] Taking the exponential function as an example, the self-attention based on frequency domain transformation in the embodiment of the present application can be in the form of: Frequency Attention(Q,K,V)=exp(f(Q T))exp(f(K))V;
[0158] Where f can represent the frequency domain transform, which can act on the frequency domain dimension d. The form of f can be any frequency domain transform, including fast Fourier transform FFT and discrete cosine transform DCT, and exp represents the exponential function. In theory, using this transform for calculation can approximate the original traditional self-attention calculation. After performing such a conversion, the softmax of the traditional attention can be removed, thereby accelerating the complexity of the self-attention calculation from O(n 2 ) is reduced to O(n).
[0159] Referring to Figure 5, Figure 5 introduces the comparison between the self-attention calculation process implemented based on the embodiment of the present application and the self-attention calculation process in the existing technology by taking the transformer layer as an example. The middle of Figure 5 shows the self-attention calculation process in the existing technology, and the right side of Figure 5 shows the self-attention calculation process implemented based on the embodiment of the present application.
[0160] In addition, when fine-tuning the pre-trained model, especially for the transformation matrix used in linear transformation, retraining not only increases the consumption of computing resources, but also because the existing pre-trained model has encoded a lot of knowledge and rules, retraining may also cause these valuable information to be lost. In the embodiment of the present application, considering the high cost of full fine-tuning, we choose to superimpose low-rank matrices and / or sparse matrices on the original parameter matrix of the model. These newly added matrices have a size much smaller than the original parameter matrix, so the computing resources required for fine-tuning are greatly reduced.
[0161] Specifically, in one possible implementation, the input data of the attention layer can be linearly transformed by a first transformation matrix to obtain first data; and the input data of the attention layer can be linearly transformed by a second transformation matrix to obtain second data; wherein the second transformation matrix is a sparse matrix or a low-rank matrix. The first data and the second data are fused to obtain key data, query data or value data, and when fine-tuning the machine learning model, the second transformation matrix is updated while keeping the first transformation matrix unchanged. The first transformation matrix can be one of a K transformation matrix, a Q transformation matrix or a V transformation matrix.
[0162] Among them, a matrix with a rank less than 1 / 2 of the matrix dimension can be considered a low-rank matrix, and a matrix with a percentage of non-zero elements less than 50% can be considered a sparse matrix.
[0163] Since the number of parameters that need to be updated in the second transformation matrix (low-rank matrix or sparse matrix) is less than that in the first transformation matrix, and when fine-tuning the model, the parameters of the first transformation matrix remain fixed, while the parameters of the second transformation matrix are updated, the overall amount of computation is reduced. Specifically, during the fine-tuning process, only parameters of these small-sized low-rank matrices and sparse matrices are updated. The low-rank matrix allows the core information of the original transformation matrix to be captured and modified through a small number of parameters, while the sparse matrix further enhances the model's focus on key information on this basis, ensuring that important features are not ignored. In this way, the computational cost caused by fine-tuning can be significantly reduced while maintaining model performance.
[0164] In one possible implementation, the second transformation matrix is the product of the first submatrix and the second submatrix, the size of the first submatrix is d*r, the size of the second submatrix is r*d, and d is greater than r. Since d is greater than r (for example, d is much greater than r, and the total number of parameters of the first submatrix and the second submatrix is less than that of the first transformation matrix), the first submatrix and the second submatrix are long strip matrices, and therefore the number of parameters contained is much smaller than that of the first transformation matrix, and the product of the first submatrix and the second submatrix is a low-rank matrix. Therefore, when fine-tuning the model, the first submatrix and the second submatrix can be updated.
[0165] Specifically, the original calculation in the model is: h = Wx;
[0166] Where h is the output, x is the input, and W is the parameter matrix (d*d) that needs to be optimized. h = (W+AB+S)x;
[0167] Initialize the A, B, and S matrices, where A and B are of d*r and r*d dimensions, with r being much smaller than d, and S is a sparse matrix (only a few parameters have values, the rest are 0). We can quickly fine-tune by optimizing A, B, and S without optimizing W. After the optimization is completed, we can get the final weight: W f =W+AB+S;
[0168] Taking the self-attention module as an example, Q, K, and V are all obtained by: Q = W q x,K=W K x,V=W V x;
[0169] For example, referring to FIG6 , the leftmost branch in FIG6 is a processing branch performed by the first transformation matrix, the middle branch is a processing branch performed by the first sub-matrix and the second sub-matrix, and the right branch is a processing branch performed by the sparse matrix.
[0170] The embodiment of the present application combines a low-rank matrix with a sparse matrix obtained by frequency domain transformation to efficiently fine-tune the pre-trained model. This fine-tuning scheme not only significantly reduces the required training calculations, but also maintains model accuracy, providing technical support for the rapid deployment and application of large-scale Transformer models. After training on the server side using training data, the frequency domain self-attention mechanism Transformer can be used on the user side for fast inference.
[0171] Next, the beneficial effects of the embodiments of the present application are introduced in combination with experiments. Referring to Table 1, Pythia is the original Transformer architecture, and the second row FLAT is the solution of the embodiment of the present application. It can be seen that the solution of the embodiment of the present application only requires 1 / 16 of the training cost to losslessly accelerate the Transformer architecture by more than 20%.
[0172] Table 1
[0173] Referring to Table 2, Table 2 is a schematic diagram of the beneficial effects corresponding to the self-attention operation method in the embodiment of the present application. By comparing the embodiment of the present application with the existing linear Transformer solution, it can be seen that the embodiment of the present application has achieved the best effect.
[0174] Table 2
[0175] Table 3
[0176] Refer to Table 3, which is a schematic diagram of the beneficial effects corresponding to the fast fine-tuning scheme based on frequency domain low-rank sparsity in the embodiment of the present application. LoSA is a fast fine-tuning scheme based on frequency domain low-rank sparsity proposed by the present invention. Compared with the LoRA scheme that only uses low-rank fine-tuning, the embodiment of the present application has better effect.
[0177] 7 , which is a schematic diagram of the structure of a data processing device provided in an embodiment of the present application. As shown in FIG7 , a data processing device 700 provided in an embodiment of the present application includes:
[0178] Processing module 701 is used to perform linear transformation on the input data of the attention layer to obtain key data, query data and value data respectively; perform frequency domain transformation on the key data and the query data respectively to obtain key data in the frequency domain and query data in the frequency domain; obtain a first operation result by performing operations on the key data and the value data in the frequency domain; obtain a second operation result by performing operations on the query data in the frequency domain and the first operation result.
[0179] For a detailed description of the processing module 701 , reference may be made to the description of the embodiment corresponding to FIG. 3 in the above embodiment, and similarities will not be repeated here.
[0180] In one possible implementation, the machine learning model also includes a first network connected before the attention layer, and the input data is a feature representation obtained by processing at least one of images, text, and audio through the first network.
[0181] In one possible implementation, the machine learning model further includes a second network connected after the attention layer, and the processing module 701 is further configured to:
[0182] According to the second operation result, the data processing result of the machine learning model is obtained through the second network.
[0183] In one possible implementation, the size of the key data in the frequency domain is: d*n, the size of the query data in the frequency domain is: n*d, and the size of the value data is: n*d, where n is the length of the input data and d is the number of feature dimensions of the input data.
[0184] In a possible implementation, the processing module 701 is specifically configured to:
[0185] Based on a preset function, processing the key data and the value data in the frequency domain to obtain processed key data;
[0186] fusing the processed key data and the value data to obtain the first operation result;
[0187] Based on the preset function, the query data in the frequency domain is processed to obtain processed query data;
[0188] The processed query data and the first operation result are fused to obtain the second operation result.
[0189] In a possible implementation, the preset function is an exponential function, a cosine function, or a sine function.
[0190] In a possible implementation, the processing module 701 is specifically configured to:
[0191] Performing frequency domain transformation on the characteristic dimension of the key data;
[0192] Perform frequency domain transformation on the feature dimension of the query data.
[0193] In a possible implementation, the processing module 701 is specifically configured to:
[0194] Performing a linear transformation on the input data of the attention layer using a first transformation matrix to obtain first data;
[0195] Performing a linear transformation on the input data of the attention layer using a second transformation matrix to obtain second data; wherein the second transformation matrix is a sparse matrix or a low-rank matrix;
[0196] Merging the first data and the second data to obtain key data, query data, or value data;
[0197] The processing module 701 is further configured to:
[0198] When fine-tuning the machine learning model, the second transformation matrix is updated while keeping the first transformation matrix unchanged.
[0199] In a possible implementation, the second transformation matrix is the product of a first submatrix and a second submatrix, the size of the first submatrix is d*r, the size of the second submatrix is r*d, and d is greater than r;
[0200] The processing module 701 is specifically configured to:
[0201] The first sub-matrix and the second sub-matrix are updated.
[0202] Next, an execution device provided in an embodiment of the present application is introduced. Please refer to Figure 8. Figure 8 is a structural diagram of an execution device provided in an embodiment of the present application. The execution device 800 can be specifically manifested as a virtual reality VR device, a mobile phone, a tablet, a laptop computer, a smart wearable device, a monitoring data processing device or a server, etc., which is not limited here. Specifically, the execution device 800 includes: a receiver 801, a transmitter 802, a processor 803 and a memory 804 (wherein the number of processors 803 in the execution device 800 can be one or more, and Figure 8 takes one processor as an example), wherein the processor 803 may include an application processor 8031 and a communication processor 8032. In some embodiments of the present application, the receiver 801, the transmitter 802, the processor 803 and the memory 804 may be connected via a bus or other means.
[0203] The memory 804 may include a read-only memory and a random access memory, and provides instructions and data to the processor 803. A portion of the memory 804 may also include non-volatile random access memory (NVRAM). The memory 804 stores processor and operation instructions, executable modules, or data structures, or subsets or extended sets thereof. The operation instructions may include various operation instructions for implementing various operations.
[0204] Processor 803 controls the operation of the execution device. In specific applications, the various components of the execution device are coupled together via a bus system. In addition to a data bus, the bus system may also include a power bus, a control bus, and a status signal bus. However, for clarity, all bus systems are referred to as a bus system in the figure.
[0205] The methods disclosed in the above embodiments of the present application can be applied to or implemented by the processor 803. The processor 803 can be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be completed by hardware integrated logic circuits in the processor 803 or by software instructions. The above processor 803 can be a general-purpose processor, a digital signal processor (DSP), a microprocessor, or a microcontroller, and can further include an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. The processor 803 can implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of the present application. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of the present application can be directly implemented as being executed by a hardware decoding processor, or can be executed by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium known in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, or registers. The storage medium is located in memory 804, and processor 803 reads information from memory 804 and, in conjunction with its hardware, completes the steps involved in the model inference process in the above method.
[0206] Receiver 801 can be used to receive input digital or character information and generate signal input related to executing device-related settings and function control. Transmitter 802 can be used to output digital or character information through the first interface. Transmitter 802 can also be used to send instructions to the disk pack through the first interface to modify data in the disk pack. Transmitter 802 can also include a display device such as a display screen.
[0207] The embodiment of the present application also provides a server device. Please refer to Figure 9, which is a schematic diagram of the structure of a server provided by the embodiment of the present application. Specifically, the server 900 is implemented by one or more servers. The server 900 may have relatively large differences due to different configurations or performance. It may include one or more central processing units (CPU) 99 (for example, one or more processors) and memory 932, and one or more storage media 930 (for example, one or more mass storage devices) for storing application programs 942 or data 944. Among them, the memory 932 and the storage medium 930 can be temporary storage or permanent storage. The program stored in the storage medium 930 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations on the server. Furthermore, the central processing unit 99 can be configured to communicate with the storage medium 930 to execute a series of instruction operations in the storage medium 930 on the server 900.
[0208] The server 900 may also include one or more power supplies 926, one or more wired or wireless network interfaces 950, one or more input and output interfaces 958; or, one or more operating systems 941, such as Windows Server™, Mac OS X™, Unix™, Linux™, FreeBSD™, etc.
[0209] In the embodiment of the present application, the central processing unit 99 is used to execute the data processing method in the above embodiment.
[0210] An embodiment of the present application also provides a computer program product, which, when running on a computer, enables the computer to execute the steps executed by the aforementioned execution device, or enables the computer to execute the steps executed by the aforementioned training device.
[0211] A computer-readable storage medium is also provided in an embodiment of the present application, which stores a program for signal processing. When the computer-readable storage medium is run on a computer, it enables the computer to execute the steps executed by the aforementioned execution device, or enables the computer to execute the steps executed by the aforementioned training device.
[0212] The execution device, training device or terminal device provided in the embodiments of the present application can specifically be a chip, and the chip includes: a processing unit and a communication unit, the processing unit can be, for example, a processor, and the communication unit can be, for example, an input / output interface, a pin or a circuit, etc. The processing unit can execute the computer execution instructions stored in the storage unit, so that the chip in the execution device executes the data processing method described in the above embodiment, or so that the chip in the training device executes the data processing method described in the above embodiment. Optionally, the storage unit is a storage unit in the chip, such as a register, a cache, etc. The storage unit can also be a storage unit located outside the chip in the wireless access device, such as a read-only memory (ROM) or other types of static storage devices that can store static information and instructions, a random access memory (RAM), etc.
[0213] Specifically, see Figure 10, which is a schematic diagram of the structure of a chip provided in an embodiment of the present application. The chip can be represented as a neural network processor NPU 1000. NPU 1000 is mounted on the host CPU (host CPU) as a coprocessor and assigned tasks by the host CPU. The core of the NPU is arithmetic circuit 1003, which is controlled by controller 1004 to extract matrix data from memory and perform multiplication operations.
[0214] In some implementations, the arithmetic circuit 1003 includes multiple processing units (PEs). In some implementations, the arithmetic circuit 1003 is a two-dimensional systolic array. The arithmetic circuit 1003 can also be a one-dimensional systolic array or other electronic circuit capable of performing mathematical operations such as multiplication and addition. In some implementations, the arithmetic circuit 1003 is a general-purpose matrix processor.
[0215] For example, assume there is an input matrix A, a transformation matrix B, and an output matrix C. The computation circuit retrieves the corresponding data of matrix B from weight memory 1002 and caches it on each PE in the computation circuit. The computation circuit then retrieves the data of matrix A from input memory 1001 and performs a matrix operation on matrix B. The partial or final matrix result is stored in accumulator 1008.
[0216] Unified memory 1006 is used to store input and output data. Weight data is directly transferred to weight memory 1002 through the Direct Memory Access Controller (DMAC) 1005. Input data is also transferred to unified memory 1006 through the DMAC.
[0217] BIU stands for Bus Interface Unit, i.e., bus interface unit 1010 , which is used for interaction between the AXI bus, DMAC, and instruction fetch buffer (IFB) 1009 .
[0218] The bus interface unit 1010 (BIU) is used for the instruction fetch memory 1009 to obtain instructions from the external memory, and is also used for the storage unit access controller 1005 to obtain the original data of the input matrix A or the transformation matrix B from the external memory.
[0219] DMAC is mainly used to transfer input data in the external memory DDR to the unified memory 1006 or transfer weight data to the weight memory 1002 or transfer input data to the input memory 1001.
[0220] The vector calculation unit 1007 includes multiple operation processing units. When necessary, it further processes the output of the operation circuit 1003, such as vector multiplication, vector addition, exponential operation, logarithmic operation, size comparison, etc. It is mainly used for non-convolutional / fully connected layer network calculations in neural networks, such as batch normalization, pixel-level summation, and upsampling of feature planes.
[0221] In some implementations, the vector calculation unit 1007 can store the processed output vector to the unified memory 1006. For example, the vector calculation unit 1007 can apply a linear function or a nonlinear function to the output of the operation circuit 1003, such as linear interpolation of the feature plane extracted by the convolution layer, or accumulate a vector of values to generate an activation value. In some implementations, the vector calculation unit 1007 generates a normalized value, a pixel-level summed value, or both. In some implementations, the processed output vector can be used as an activation input to the operation circuit 1003, for example, for use in subsequent layers in a neural network.
[0222] An instruction fetch buffer 1009 connected to the controller 1004 is used to store instructions used by the controller 1004;
[0223] Unified memory 1006, input memory 1001, weight memory 1002, and instruction fetch memory 1009 are all on-chip memories. External memories are private to the NPU hardware architecture.
[0224] The processor mentioned in any of the above places can be a general-purpose central processing unit, a microprocessor, an ASIC, or one or more integrated circuits for controlling the execution of the above program.
[0225] It should also be noted that the device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separate, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed across multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present embodiment. In addition, in the drawings of the device embodiments provided in this application, the connection relationship between the modules indicates that there is a communication connection between them, which can be specifically implemented as one or more communication buses or signal lines.
[0226] Through the description of the above embodiments, those skilled in the art can clearly understand that the present application can be implemented by means of software plus necessary general hardware, and of course can also be implemented by special hardware including application-specific integrated circuits, special CPUs, special memories, special components, etc. In general, all functions performed by computer programs can be easily implemented with corresponding hardware, and the specific hardware structures used to implement the same function can also be diverse, such as analog circuits, digital circuits or special circuits, etc. However, for the present application, software program implementation is a better implementation method in most cases. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a readable storage medium, such as a computer's floppy disk, USB flash drive, mobile hard disk, ROM, RAM, magnetic disk or optical disk, etc., and includes a number of instructions to enable a computer device (which can be a personal computer, training equipment, or network equipment, etc.) to execute the methods described in each embodiment of the present application.
[0227] In the above embodiments, all or part of the embodiments may be implemented by software, hardware, firmware, or any combination thereof. When implemented by software, all or part of the embodiments may be implemented in the form of a computer program product.
[0228] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website, a computer, a training device or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website, a computer, a training device or a data center. The computer-readable storage medium can be any available medium that a computer can store or a data storage device such as a training device, a data center, etc. that includes one or more available media integrations. The available medium can be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)).
Claims
1. A data processing method, characterized in that: Applied to an attention layer in a machine learning model, the method comprises: Performing a linear transformation on the input data of the attention layer to obtain key data, query data, and value data respectively; Performing frequency domain transformation on the key data and the query data respectively to obtain key data in the frequency domain and query data in the frequency domain; Obtaining a first operation result by operating the key data and the value data in the frequency domain; A second operation result is obtained by performing an operation on the query data in the frequency domain and the first operation result.
2. The method according to claim 1, characterized in that The machine learning model also includes a first network connected before the attention layer, and the input data is a feature representation obtained by processing at least one of image, text, and audio data through the first network.
3. The method according to claim 1 or 2, characterized in that The machine learning model further includes a second network connected after the attention layer, and the method further includes: According to the second operation result, the data processing result of the machine learning model is obtained through the second network.
4. The method according to any one of claims 1 to 3, characterized in that: The size of the key data in the frequency domain is d*n, the size of the query data in the frequency domain is n*d, and the size of the value data is n*d, where n is the length of the input data and d is the number of feature dimensions of the input data.
5. The method according to any one of claims 1 to 4, characterized in that: The step of obtaining a first operation result by operating the key data and the value data in the frequency domain includes: Based on a preset function, processing the key data and the value data in the frequency domain to obtain processed key data; fusing the processed key data and the value data to obtain the first operation result; The obtaining of a second operation result by operating the query data in the frequency domain and the first operation result includes: Based on the preset function, the query data in the frequency domain is processed to obtain processed query data; The processed query data and the first operation result are fused to obtain the second operation result.
6. The method according to claim 5, characterized in that The preset function is an exponential function, a cosine function or a sine function.
7. The method according to any one of claims 1 to 6, characterized in that: The performing frequency domain transformation on the key data and the query data respectively includes: Performing frequency domain transformation on the characteristic dimension of the key data; Perform frequency domain transformation on the feature dimension of the query data.
8. The method according to any one of claims 1 to 7, characterized in that: The linear transformation of the input data of the attention layer includes: Performing a linear transformation on the input data of the attention layer using a first transformation matrix to obtain first data; Performing a linear transformation on the input data of the attention layer using a second transformation matrix to obtain second data; wherein the second transformation matrix is a sparse matrix or a low-rank matrix; Merging the first data and the second data to obtain key data, query data, or value data; The method further comprises: When fine-tuning the machine learning model, the second transformation matrix is updated while keeping the first transformation matrix unchanged.
9. The method according to claim 8, characterized in that The second transformation matrix is the product of the first submatrix and the second submatrix, the size of the first submatrix is d*r, the size of the second submatrix is r*d, d is greater than r, and the total number of parameters of the first submatrix and the second submatrix is less than that of the first transformation matrix; The updating of the second transformation matrix comprises: The first sub-matrix and the second sub-matrix are updated.
10. A data processing device, characterized in that: An attention layer applied to a machine learning model, comprising: A processing module is used to perform a linear transformation on the input data of the attention layer to obtain key data, query data and value data respectively; perform a frequency domain transformation on the key data and the query data respectively to obtain key data in the frequency domain and query data in the frequency domain; obtain a first operation result by performing an operation on the key data and the value data in the frequency domain; and obtain a second operation result by performing an operation on the query data in the frequency domain and the first operation result.
11. The device according to claim 10, characterized in that The machine learning model also includes a first network connected before the attention layer, and the input data is a feature representation obtained by processing at least one of images, text, and audio through the first network.
12. The device according to claim 10 or 11, characterized in that The machine learning model further includes a second network connected after the attention layer, and the processing module is further configured to: According to the second operation result, the data processing result of the machine learning model is obtained through the second network.
13. The device according to any one of claims 10 to 12, characterized in that The size of the key data in the frequency domain is d*n, the size of the query data in the frequency domain is n*d, and the size of the value data is n*d, where n is the length of the input data and d is the number of feature dimensions of the input data.
14. The device according to any one of claims 10 to 13, characterized in that The processing module is specifically used to: Based on a preset function, processing the key data and the value data in the frequency domain to obtain processed key data; fusing the processed key data and the value data to obtain the first operation result; Based on the preset function, the query data in the frequency domain is processed to obtain processed query data; The processed query data and the first operation result are fused to obtain the second operation result.
15. The device according to any one of claims 10 to 14, characterized in that The processing module is specifically configured to: perform frequency domain transformation on the feature dimension of the key data; Perform frequency domain transformation on the feature dimension of the query data.
16. The device according to any one of claims 10 to 15, characterized in that The processing module is specifically used to: Performing a linear transformation on the input data of the attention layer using a first transformation matrix to obtain first data; Performing a linear transformation on the input data of the attention layer using a second transformation matrix to obtain second data; wherein the second transformation matrix is a sparse matrix or a low-rank matrix; Merging the first data and the second data to obtain key data, query data, or value data; The processing module is further configured to: When fine-tuning the machine learning model, the second transformation matrix is updated while keeping the first transformation matrix unchanged.
17. The device according to claim 16, characterized in that The second transformation matrix is the product of the first submatrix and the second submatrix, the size of the first submatrix is d*r, the size of the second submatrix is r*d, and d is greater than r; The processing module is specifically used to: The first sub-matrix and the second sub-matrix are updated.
18. A computer storage medium, characterized in that The computer storage medium stores one or more instructions that, when executed by one or more computers, cause the one or more computers to perform the operations of the method of any one of claims 1 to 9.
19. A computer program product, characterized in that The method comprises computer-readable instructions, which, when executed on a computer device, cause the computer device to execute the method according to any one of claims 1 to 9.
20. A system, characterized in that It includes at least one processor and at least one memory; the processor and the memory are connected via a communication bus and communicate with each other; The at least one memory is used to store code; The at least one processor is configured to execute the code to perform the method according to any one of claims 1 to 9.
21. A chip, characterized in that: The system comprises at least one processing unit and an interface circuit, wherein the interface circuit is used to provide program instructions or data to the at least one processing unit, and the at least one processing unit is used to execute the program instructions to implement the method according to any one of claims 1 to 9.
Citation Information
Patent Citations
Data processing method and device, electronic equipment and storage medium
CN114372457A
Data processing method and device, equipment and storage medium
CN116958578A
Data processing method and device based on self-attention, medium and terminal
CN117688287A
Attention neural networks with talking heads attention
US20210279576A1