A large model-based multilingual offensive detection method
Patent Information
- Application Number
- CN202410769976.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-14
- Publication Date
- 2026-11-03
- Estimated Expiration
- 2044-06-14
AI Technical Summary
Traditional machine learning algorithms suffer from insufficient understanding, flexibility, and update speed in web content detection, making it difficult to effectively identify and filter cyberbullying.
A multilingual attack detection method using a large model is proposed. By constructing and cleaning a multilingual attack detection dataset, loading and modifying the Llama2 model and pre-training it, and using KMeans clustering and LoRA fine-tuning methods to group and fuse attention heads to form the SQA attention mechanism, thereby improving the performance and flexibility of the model.
It achieves higher accuracy and understanding of offensive language detection, is suitable for large-scale corpora and real-world application scenarios, reduces the number of model parameters, and improves the flexibility and efficiency of the model in actual deployment.
Smart Images

Figure CN118760749B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of large-scale NLP (Natural Language Processing) models, specifically involving a multilingual attack detection method based on large-scale models. Background Technology
[0002] Research into using AI (Artificial Intelligence) content detection to prevent cyberbullying has been ongoing for several years. As early as 2015, it was proposed that there is a significant correlation between an individual's emotional state and malicious intent, and using machine learning to detect aggressive behavior on social media is considered a good indicator of cyberbullying detection. In 2017, Google's Jigsaw created Conversation AI to detect toxic online comments. Many tech giants have been incorporating algorithms into their content moderation for years, possessing methods for identifying and filtering online information. However, it is clear that these AI detection algorithms have not eradicated cyberbullying. One reason is that traditional machine learning algorithms cannot meet the requirements of online content moderation, and the influencing factors include the following:
[0003] 1. Insufficient comprehension: Harmful language is very difficult to distinguish, and the semantic understanding ability of AI algorithms is not strong enough. It often gives the same score to harmful and harmless comments, failing to truly filter out disrespectful comments, or giving lower scores to neutral sentences, filtering out normal comments that should not be filtered, thus hindering communication between bloggers and fans.
[0004] 2. Lack of flexibility: Some websites may require detection of offensive language but not rumors, while other websites may have the opposite requirement. Traditional AI detection tools often use a generic "toxicity score," which is not flexible enough and cannot meet the different needs of different platforms.
[0005] 3. Insufficient update speed: Many detection algorithms work using APIs, and these models are trained on large amounts of network data. They perform well on examples similar to the training data, but are likely to fail when encountering unfamiliar toxic language or constantly coined words. Therefore, AI models lack efficient and real-time human feedback, making it impossible to quickly fine-tune and iterate the model, resulting in poor automated detection performance.
[0006] Large language models possess features such as intelligent emergence, pre-training, and reinforcement learning based on human feedback, which greatly enhance traditional methods. They offer stronger language understanding capabilities, and custom models with higher accuracy can be quickly trained using general models. Furthermore, human feedback helps identify and correct errors, resulting in better and faster detection performance.
[0007] The Transformer is a standard seq2seq (encoder-decoder) architecture, consisting of a stack of encoders and decoders. Transformers are widely used in NLP. Typical tasks such as text classification and sentiment analysis only require an encoder, while generative tasks like machine translation, dialogue analysis, and reading comprehension require a decoder to generate sequences of the same size. Each encoder module in a Transformer model consists of a self-attention layer and a feedforward neural network, while the decoder has an additional encoder-decoder attention module to establish the connection between the encoder and decoder. These self-attention mechanisms are the core of the Transformer's success.
[0008] Autoregressive decoder inference is a significant bottleneck in Transformer models due to the substantial memory bandwidth overhead of loading decoder weights and all attention keys and values at each decoding step. This memory bandwidth overhead of loading keys and values can be drastically reduced using multi-query attention. However, multi-query attention can lead to a decrease in inference quality and model training instability. Furthermore, training a single model solely for accelerating inference is not ideal. This paper addresses this by uptraining an existing model using a pre-trained model, converting a multi-head attention model to a multi-query attention model with lower computational cost, and introducing a generalized approach combining grouped query attention, multi-query attention, and multi-head attention. This achieves a balance between performance and speed.
[0009] MHA (Multi-Head-Attention) is a standard multi-head attention mechanism that includes H queries, key and value matrices. The weights of the key and value matrices are not shared among all attention heads.
[0010] MQA (Multi-Query Attention) is a variant of multi-query attention and an attention mechanism used for autoregressive decoding. Unlike MHA, MQA allows all headers to share the same key and value matrix, while each header retains only one set of query parameters, thus greatly reducing the number of parameters in the key and value matrices.
[0011] Grouped Query Attention (GQA) divides the query headers into G groups, each sharing a key-value matrix. GQA-G refers to grouped query attention with G groups. GQA-1 has a single group, thus a single key-value pair, equivalent to MQA. If GQA-H has the same number of groups as the number of headers, it is equivalent to MHA.
[0012] GQA employs a compromise between MHA and MQA. After training, GQA achieves a balance between performance and speed, reaching near-MHA quality while maintaining comparable speed to MQA. However, GQA's fusion of attention heads from different semantic spaces can lead to semantic space distortion, thus affecting the model's inference performance. Summary of the Invention
[0013] To address the limitations of traditional machine learning algorithms in terms of understanding, flexibility, and update speed, which prevent them from meeting the requirements of online content censorship, this invention proposes a multilingual attack detection method based on a large model. Furthermore, this invention introduces SQA, a method for grouping model attention heads based on attention head similarity. SQA calculates the similarity between attention heads, fusing attention heads with similar semantic spaces to obtain a more focused attention, thereby improving model performance. Simultaneously, this method reduces the number of model parameters, making the model more flexible and efficient in practical system deployment.
[0014] To achieve the above objectives, the technical solution adopted by the present invention is as follows:
[0015] A multilingual attack detection method based on a large model, the method comprising:
[0016] Step 1: Construct and clean the dataset for multilingual attack detection;
[0017] Step 2: Load the Llama2 model for modification, and then pre-train it to obtain a pre-trained language model. The modification process is as follows:
[0018] Step 2.1: Load the parameters of the Llama2 model and obtain the parameters of all attention heads of the Llama2 model. The parameters of the attention heads include the weights of the key-value head and the query head.
[0019] Step 2.2: Convert the weight of each key-value header into a tensor of a preset dimension;
[0020] Step 2.3: Perform KMeans clustering on the key-value headers based on tensors;
[0021] Step 2.4: Encapsulate the KMeans clustering results into a dictionary. The key of the dictionary is the index value corresponding to each attention head, and the value is the clustering label corresponding to each attention head after clustering.
[0022] Step 2.5: Based on the dictionary obtained in Step 2.4, perform average pooling to merge the key-value headers in the attention headers of the same cluster label into a single key-value header;
[0023] Step 2.6: Concatenate all the key-value headers obtained after average pooling to obtain the key-value header combination result;
[0024] Step 2.7: Based on the number of key-value headers before average pooling, perform a repeat operation on the key-value header combination result obtained in Step 2.6 to expand it to the original number of key-value headers of the Llama2 model, and combine the corresponding query headers to obtain new attention headers, thus completing the transformation;
[0025] Step 3: Based on the dataset, fine-tune the pre-trained language model using the LoRA fine-tuning method to obtain the fine-tuned model;
[0026] Step 4: Use a fine-tuned model for multilingual attack detection.
[0027] Several alternative methods are provided below, but they are not intended as additional limitations on the overall solution above. They are merely further additions or optimizations. Provided there are no technical or logical contradictions, each alternative method can be combined individually with respect to the overall solution above, or multiple alternative methods can be combined with each other.
[0028] Preferably, the step of converting the weight of each key-value header into a tensor of a preset dimension includes:
[0029] The weight of each key-value header is increased in dimensionality through the tensor's unsqueeze operation, and then transformed into a tensor shape with the final preset dimensions through two view operations and one transpose operation.
[0030] As a preferred method, 5% of the total data from the Bookcorpus and Wikipedia datasets is used for pre-training, with the following pre-training parameters: number of epochs (train_epoch = 3), number of samples used per iteration during training (train_batch_size_each_device = 64), number of samples used during testing (eval_batch_size_each_device = 64), and gradient accumulation (grad_acc = 4).
[0031] Preferably, the step of repeating the key-value header combination result obtained in step 2.6 to expand it to the original number of key-value headers in the Llama2 model includes:
[0032] If a key value in the key value header combination result is derived from the average pooling of key value headers in N attention headers with the same cluster label, then the key value header in the key value header combination result is repeated to obtain the key value header in the N key value header combination result.
[0033] Preferably, the process of combining the corresponding query headers to obtain a new attention header includes:
[0034] For a key-value header in the key-value header combination result, if the key-value header is derived from the average pooling of the key-value headers of N attention headers with the same cluster label, and the key-value header generates N key-value headers by performing a repeat operation, then the query headers of the N attention headers with the same cluster label are combined with a key-value header generated after the repeat operation to obtain a new attention header.
[0035] The present invention provides a multilingual attack detection method based on a large model, which has the following advantages compared with the prior art:
[0036] (1) High accuracy: By fine-tuning the large language model, more accurate results of attack language detection can be obtained, thereby improving the system's judgment accuracy.
[0037] (2) Powerful language understanding capabilities: Large-scale language models based on deep learning have powerful language understanding and generation capabilities, enabling them to better capture and understand the semantics and context of offensive language.
[0038] (3) Scalability: The method of the present invention is applicable to large-scale corpora and practical application scenarios, and can cope with different fields and various types of offensive language.
[0039] (4) The KMeans clustering method is used to group the attention heads, resulting in a more focused attention, which improves the model's performance while reducing the number of model parameters. This solves the problem that the model is difficult to deploy in practical applications due to the large number of parameters. Attached Figure Description
[0040] Figure 1 This is a flowchart of a multilingual attack detection method based on a large model according to the present invention;
[0041] Figure 2 This is a flowchart illustrating the modification of the Llama2 model in this invention;
[0042] Figure 3This is a schematic diagram illustrating how the grouped key-value headers are averaged and pooled into a single group according to the present invention.
[0043] Figure 4 This is a diagram illustrating the method of attention head grouping using KMeans clustering in this invention;
[0044] Figure 5 The schematic diagram is for fine-tuning using LoRA. Detailed Implementation
[0045] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0046] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. The terminology used herein in the description of the invention is for the purpose of describing particular embodiments only and is not intended to limit the invention.
[0047] like Figure 1 As shown, this embodiment provides a multilingual attack detection method based on a large model, including the following steps:
[0048] Step 1: Construct and clean the multilingual attack detection dataset. In this embodiment, Chinese, English, and Korean are selected as the instruction-supervised fine-tuning dataset. First, a large-scale corpus is collected, containing example texts of both offensive and non-offensive languages. Then, these texts are preprocessed, including word segmentation, stop word removal, and tokenization, and an instruction-supervised prompt is constructed to prepare the dataset for model fine-tuning.
[0049] Step 2: Select a large-scale model as the base model: In this embodiment, the Llama2 model is selected as the large-scale model that requires fine-tuning of instructions for this task, as it has powerful language understanding and generation capabilities. Then, the base model is initialized and configured for fine-tuning. Simultaneously, its model structure is modified, such as... Figure 2 As shown, the modification process is as follows:
[0050] Step 2.1: Load the parameters of the original Llama2 model and obtain the weights of the attention key-value pairs in each layer of the Llama2 model. The attention mechanism in the original Llama2 model is MHA. Therefore, in this embodiment, the weights of the key-value headers and query headers in the 40 layers of the Llama2 model are specifically obtained.
[0051] Step 2.2: Transform the weights of the key-value pairs in each layer. Increase the dimensionality of the weights using the tensor's unsqueeze operation, then perform two view operations and one transpose operation to transform them into the final required tensor shape. For example, in this embodiment, the original two-dimensional tensor is converted into a four-bit tensor to facilitate subsequent grouping operations.
[0052] Step 2.3: Perform KMeans clustering on the attention head based on the weights of the transformed key-value pairs, where the parameter n_clusters of KMeans clustering is a hyperparameter.
[0053] Step 2.4: Process the KMeans clustering results and encapsulate them into a dictionary. The key of the dictionary is the index value corresponding to each attention head, and the value is the clustering label corresponding to each attention head after clustering.
[0054] Step 2.5: Based on the dictionary obtained in Step 2.4, perform average pooling to merge the key-value heads in the attention heads with the same label into a single key-value head (for ease of description, this embodiment refers to it as a merged key-value head), and obtain the grouping and fusion result of the attention heads.
[0055] This embodiment uses the key as an example to illustrate average pooling, such as... Figure 3 As shown, K1, K2, ..., Kn represent the keys of n attention heads, and d h d represents the feature dimension of the key. model The sequence length is represented by the Mean Pool operation, which merges n keys into one key. The merging process for the values is similar.
[0056] Step 2.6: Concatenate the fused key value headers obtained after average pooling to obtain the final key value header combination result.
[0057] Step 2.7: Perform a repeat operation on the key-value header combination result obtained in Step 2.6 to expand it to the original dimension, and then perform attention calculation to obtain a more focused attention.
[0058] In order to maintain the structure of the original attention mechanism, this embodiment performs a repeat operation on the key-value head combination result obtained in step 2.6 based on the number of key-value heads before average pooling, expands it to the original number of key-value heads of the Llama2 model, and combines the corresponding query heads to obtain a new attention head.
[0059] During the repeat operation, for a fused key-value header in the key-value header combination result, if the fused key-value header is derived from the average pooling of the key-value headers in N attention headers with the same cluster label, then the fused key-value header in the key-value header combination result is repeated to obtain the fused key-value header in the N key-value header combination result.
[0060] During the combination, for a fused key-value header in the key-value header combination result, if the fused key-value header is derived from the average pooling of the key-value headers of N attention headers with the same cluster label, and the fused key-value header generates N fused key-value headers by performing a repeat operation, then the query headers of the N attention headers with the same cluster label are combined with a fused key-value header generated after the repeat operation to obtain a new attention header. Figure 4 This indicates the grouping results obtained by using the KMeans algorithm to group attention heads with similar semantic spaces. For example, 0, 3, and 4 indicate that these three key-value heads have similar semantic spaces, so they are grouped together. Other key-value heads are grouped similarly.
[0061] In this embodiment, the attention mechanism with the new attention head is named SQA (Similarity Query Attention), and the attention calculation formula is as follows:
[0062]
[0063] In the formula, Attention(Q,K,V) is the attention value, softmax is the softmax function, Q is the query, K is the key, V is the value, and d k The dimension of the input information.
[0064] To adapt the improved Llama2 model from step 2.7 to the new model structure, this invention uses 5% of the total data from the Bookcorpus and Wikipedia datasets for pre-training. This achieves model transformation with relatively low computational cost while also improving model performance. The pre-training parameters are as follows: train_epoch = 3, train_batch_size_each_device = 64 samples used in one forward and backward propagation, eval_batch_size_each_device = 64 samples used in each evaluation, and grad_acc = 4 gradients. The total number of steps in the entire training process is 11667, and the time is approximately 22 hours.
[0065] Step 3: Fine-tuning the model. In this method, the preprocessed dataset is input into the Llama2 model obtained in Step 2.8, and LoRA is used for fine-tuning. During fine-tuning, the model's weights and parameters are adjusted based on the sample label information to enable it to better identify and classify offensive language. Figure 5 As shown, the principle of LoRA is as follows:
[0066] (1) Add a bypass next to the original PLM (Pre-trained Language Model) to perform a dimensionality reduction and then dimensionality increase operation to simulate intrinsic rank.
[0067] (2) During training, the parameters of PLM are fixed, and only the dimension reduction matrix A and the dimension increase matrix B are trained. The input and output dimensions of the model remain unchanged, and the parameters of BA and PLM are superimposed during output.
[0068] (3) Initialize A with a random Gaussian distribution and B with a 0 matrix to ensure that the bypass matrix is still a 0 matrix at the beginning of training.
[0069] When fine-tuning the pre-trained language model for downstream tasks, the pre-trained model parameters need to be updated, as shown in the following formula:
[0070] W0+ΔW
[0071] W0 represents the parameters of the pre-trained language model after pre-training, and ΔW represents the parameters that need to be updated. If it's full parameter fine-tuning, the number of parameters is W0. However, for LoRa, only ΔW needs to be fine-tuned.
[0072] Specifically, assuming the pre-trained matrix is W0∈R d×k Its update can be represented as:
[0073] W0 + ΔW = W0 + BA, B ∈ R d×r ,A∈R r×k
[0074] Wherein the rank:
[0075] r << min(d,k)
[0076] Where d represents the size of the first dimension of the original parameter matrix W0, which represents the number of rows, and k represents the size of the second dimension of the original parameter matrix W0, which represents the number of columns.
[0077] During LoRA training, W0 is fixed, and only A and B are training parameters.
[0078] During the forward pass, both W0 and ΔW are multiplied by the same input x and then summed, as shown in the following formula:
[0079] h = W0x + ΔWx = W0x + BAx
[0080] Where h represents the output of the forward process.
[0081] Step 4: Model Evaluation and Optimization. Evaluate and optimize the fine-tuned model. Evaluate the model using a separate test set, calculating performance metrics such as accuracy, recall, and F1 score on the offensive language detection task. Based on the evaluation results, the model can be adjusted and optimized to further improve its performance.
[0082] Step 5: System Implementation. Apply the fine-tuned model to a practical offensive language detection system. Integrate the fine-tuned model into a usable software or hardware system that can receive input text and output a judgment result on whether the text contains offensive language. The system can be customized and optimized according to actual needs to improve real-time performance and adaptability.
[0083] This invention discloses a method for multilingual aggression detection based on a large model. This method fine-tunes a large-scale deep learning-based language model, enabling it to accurately identify and classify aggression in different languages, thus providing an efficient and accurate multilingual aggression detection solution. Simultaneously, it combines this method with KMeans clustering to group attention heads, fusing similar attention heads to achieve a more focused attention, improving model performance while reducing model parameters, making the model more flexible for deployment in practical application systems.
[0084] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0085] The embodiments described above are merely illustrative of several implementations of the present invention, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of the invention. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of the present invention, and these modifications and improvements all fall within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the appended claims.
Claims
1. A multilingual attack detection method based on a large model, characterized in that, The multilingual attack detection method based on a large model includes: Step 1: Construct and clean the dataset for multilingual attack detection; Step 2: Load the Llama2 model for modification, and then pre-train it to obtain a pre-trained language model. The modification process is as follows: Step 2.1: Load the parameters of the Llama2 model and obtain the parameters of all attention heads of the Llama2 model. The parameters of the attention heads include the weights of the key-value head and the query head. Step 2.2: Convert the weight of each key-value header into a tensor of a preset dimension; Step 2.3: Perform KMeans clustering on the key-value headers based on tensors; Step 2.4: Encapsulate the KMeans clustering results into a dictionary. The key of the dictionary is the index value corresponding to each attention head, and the value is the clustering label corresponding to each attention head after clustering. Step 2.5: Based on the dictionary obtained in Step 2.4, perform average pooling to merge the key-value headers in the attention headers of the same cluster label into a single key-value header; Step 2.6: Concatenate all the key-value headers obtained after average pooling to obtain the key-value header combination result; Step 2.7: Based on the number of key-value headers before average pooling, perform a repeat operation on the key-value header combination result obtained in Step 2.6 to expand it to the original number of key-value headers of the Llama2 model, and combine the corresponding query headers to obtain new attention headers, thus completing the transformation; Step 3: Based on the dataset, fine-tune the pre-trained language model using the LoRA fine-tuning method to obtain the fine-tuned model; Step 4: Use a fine-tuned model for multilingual attack detection.
2. The multilingual attack detection method based on a large model according to claim 1, characterized in that, The step of converting the weight of each key-value header into a tensor of a preset dimension includes: The weight of each key-value header is increased in dimensionality through the tensor's unsqueeze operation, and then transformed into a tensor shape with the final preset dimensions through two view operations and one transpose operation.
3. The multilingual attack detection method based on a large model according to claim 1, characterized in that, Pre-training was performed using 5% of the total data from the Bookcorpus and Wikipedia datasets, with the following pre-training parameters: train_epoch = 3 epochs, train_batch_size_each_device = 64 samples per iteration during training, eval_batch_size_each_device = 64 samples per test, and grad_acc = 4 gradients.
4. The multilingual attack detection method based on a large model according to claim 1, characterized in that, The step of repeating the key-value header combination result obtained in step 2.6 to expand it to the original number of key-value headers in the Llama2 model includes: If a key value in the key value header combination result is derived from the average pooling of key value headers in N attention headers with the same cluster label, then the key value header in the key value header combination result is repeated to obtain the key value header in the N key value header combination result.
5. The multilingual attack detection method based on a large model according to claim 1, characterized in that, The process of combining the corresponding query headers to obtain a new attention header includes: For a key-value header in the key-value header combination result, if the key-value header is derived from the average pooling of the key-value headers of N attention headers with the same cluster label, and the key-value header generates N key-value headers by performing a repeat operation, then the query headers of the N attention headers with the same cluster label are combined with a key-value header generated after the repeat operation to obtain a new attention header.
Citation Information
Patent Citations
Fine adjustment method and system of pre-training language model based on attention guidance mechanism
CN114925814A
Instruction data mining method and system based on LoRA embedding
CN117556251A
