[go: up one dir, main page]

CN114037074A - A model pruning method, device, electronic device and storage medium - Google Patents

A model pruning method, device, electronic device and storage medium Download PDF

Info

Publication number
CN114037074A
CN114037074A CN202111322412.3A CN202111322412A CN114037074A CN 114037074 A CN114037074 A CN 114037074A CN 202111322412 A CN202111322412 A CN 202111322412A CN 114037074 A CN114037074 A CN 114037074A
Authority
CN
China
Prior art keywords
matrix
pruning
mask
vector
sublayer
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202111322412.3A
Other languages
Chinese (zh)
Inventor
李建伟
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Baidu Netcom Science and Technology Co Ltd
Original Assignee
Beijing Baidu Netcom Science and Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Baidu Netcom Science and Technology Co Ltd filed Critical Beijing Baidu Netcom Science and Technology Co Ltd
Priority to CN202111322412.3A priority Critical patent/CN114037074A/en
Publication of CN114037074A publication Critical patent/CN114037074A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/08Learning methods
    • G06N3/082Learning methods modifying the architecture, e.g. adding, deleting or silencing nodes or connections
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F18/00Pattern recognition
    • G06F18/20Analysing
    • G06F18/24Classification techniques
    • G06F18/241Classification techniques relating to the classification model, e.g. parametric or non-parametric approaches
    • G06F18/2415Classification techniques relating to the classification model, e.g. parametric or non-parametric approaches based on parametric or probabilistic models, e.g. based on likelihood ratio or false acceptance rate versus a false rejection rate
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/04Architecture, e.g. interconnection topology
    • G06N3/045Combinations of networks

Landscapes

  • Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Theoretical Computer Science (AREA)
  • Data Mining & Analysis (AREA)
  • Evolutionary Computation (AREA)
  • Life Sciences & Earth Sciences (AREA)
  • Artificial Intelligence (AREA)
  • General Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Health & Medical Sciences (AREA)
  • Molecular Biology (AREA)
  • Software Systems (AREA)
  • Mathematical Physics (AREA)
  • Computing Systems (AREA)
  • Health & Medical Sciences (AREA)
  • Biomedical Technology (AREA)
  • Biophysics (AREA)
  • Computational Linguistics (AREA)
  • Probability & Statistics with Applications (AREA)
  • Evolutionary Biology (AREA)
  • Bioinformatics & Cheminformatics (AREA)
  • Bioinformatics & Computational Biology (AREA)
  • Computer Vision & Pattern Recognition (AREA)
  • Image Analysis (AREA)

Abstract

本公开提供了一种模型剪枝方法、装置、电子设备、可读存储介质以及计算机程序产品,涉及计算机视觉、深度学习领域。具体实现方案为:根据目标子层输出的图像特征向量矩阵,确定所述目标子层中的多头自注意力模块对应的剪枝掩码,所述目标子层为视觉转换器模型的子层,所述剪枝掩码用于标识所述多头自注意力模块中待剪枝的自注意力头;利用所述剪枝掩码,对所述多头自注意力模块进行剪枝。该方案能够确定用于标识多头自注意力模块中待剪枝的自注意力头的剪枝掩码,并利用剪枝掩码,对视觉转换器模型的目标子层的多头自注意力模块进行自注意力头的剪枝。从而能够降低视觉转换器模型部署和运算所需要消耗的资源。

Figure 202111322412

The present disclosure provides a model pruning method, apparatus, electronic device, readable storage medium and computer program product, which relate to the fields of computer vision and deep learning. The specific implementation scheme is: according to the image feature vector matrix output by the target sublayer, determine the pruning mask corresponding to the multi-head self-attention module in the target sublayer, and the target sublayer is a sublayer of the visual converter model, The pruning mask is used to identify the self-attention head to be pruned in the multi-head self-attention module; the multi-head self-attention module is pruned by using the pruning mask. This scheme can determine the pruning mask used to identify the self-attention head to be pruned in the multi-head self-attention module, and use the pruning mask to perform the multi-head self-attention module of the target sub-layer of the visual converter model. Self-attention head pruning. Therefore, the resources required for deployment and computation of the vision converter model can be reduced.

Figure 202111322412

Description

一种模型剪枝方法、装置、电子设备及存储介质A model pruning method, device, electronic device and storage medium

技术领域technical field

本公开涉及人工智能领域,具体涉及计算机视觉和深度学习技术,具体可用于计算机视觉、深度学习等场景。The present disclosure relates to the field of artificial intelligence, and in particular to computer vision and deep learning technologies, which can be specifically used in scenarios such as computer vision and deep learning.

背景技术Background technique

转换器(Transformer)模型是一种主要基于自注意力机制的深度神经网络模型,最初应用于自然语言处理(NLP,Natural Language Processing)。并且由于Transformer模型具有强大的表征能力,近年来Transformer模型在计算机视觉领域也得到了极大的发展。The Transformer model is a deep neural network model mainly based on the self-attention mechanism, which was originally applied to Natural Language Processing (NLP). And because the Transformer model has a strong representation ability, the Transformer model has also been greatly developed in the field of computer vision in recent years.

相对于计算机视觉领域常用的基于CNN的模型而言,视觉转换器(VisionTransformer)模型的部署和运算,一般需要耗费巨大的资源。因此,如何降低VisionTransformer模型部署和运算所需要消耗的资源,已经成为Vision Transformer模型应用时亟需解决的问题。Compared with the commonly used CNN-based models in the computer vision field, the deployment and operation of the Vision Transformer model generally consumes huge resources. Therefore, how to reduce the resources consumed by the deployment and operation of the Vision Transformer model has become an urgent problem to be solved in the application of the Vision Transformer model.

发明内容SUMMARY OF THE INVENTION

本公开提供了提供一种模型剪枝方法、装置、电子设备、可读存储介质以及计算机程序产品,以降低视觉转换器模型部署和运算所需要消耗的资源。The present disclosure provides a model pruning method, apparatus, electronic device, readable storage medium, and computer program product, so as to reduce the resources consumed by the deployment and operation of the vision converter model.

根据本公开的一方面,提供了一种模型剪枝方法,该方法可以包括以下步骤:According to an aspect of the present disclosure, a model pruning method is provided, and the method may include the following steps:

根据目标子层输出的图像特征向量矩阵,确定目标子层中的多头自注意力模块对应的剪枝掩码,目标子层为视觉转换器模型的子层,剪枝掩码用于标识多头自注意力模块中待剪枝的自注意力头;According to the image feature vector matrix output by the target sub-layer, determine the pruning mask corresponding to the multi-head self-attention module in the target sub-layer, the target sub-layer is the sub-layer of the visual converter model, and the pruning mask is used to identify the multi-head self-attention module. The self-attention head to be pruned in the attention module;

利用剪枝掩码,对多头自注意力模块进行剪枝。Using the pruning mask, the multi-head self-attention module is pruned.

根据本公开的第二方面,提供了一种模型剪枝装置,该装置可以包括:According to a second aspect of the present disclosure, there is provided a model pruning device, which may include:

剪枝掩码确定单元,用于根据目标子层输出的图像特征向量矩阵,确定目标子层中的多头自注意力模块对应的剪枝掩码,目标子层为视觉转换器模型的子层,剪枝掩码用于标识多头自注意力模块中待剪枝的自注意力头;The pruning mask determination unit is used to determine the pruning mask corresponding to the multi-head self-attention module in the target sublayer according to the image feature vector matrix output by the target sublayer, and the target sublayer is the sublayer of the visual converter model, The pruning mask is used to identify the self-attention head to be pruned in the multi-head self-attention module;

模块剪枝单元,用于利用剪枝掩码,对多头自注意力模块进行剪枝。The module pruning unit is used to prune the multi-head self-attention module using the pruning mask.

根据本公开的另一方面,提供了一种电子设备,包括:According to another aspect of the present disclosure, there is provided an electronic device, comprising:

至少一个处理器;以及at least one processor; and

与该至少一个处理器通信连接的存储器;其中,a memory communicatively coupled to the at least one processor; wherein,

该存储器存储有可被该至少一个处理器执行的指令,该指令被该至少一个处理器执行,以使该至少一个处理器能够执行本公开任一实施例中的方法。The memory stores instructions executable by the at least one processor to enable the at least one processor to perform the method in any of the embodiments of the present disclosure.

根据本公开的另一方面,提供了一种存储有计算机指令的非瞬时计算机可读存储介质,该计算机指令用于使计算机执行本公开任一实施例中的方法。According to another aspect of the present disclosure, there is provided a non-transitory computer-readable storage medium storing computer instructions for causing a computer to perform the method in any of the embodiments of the present disclosure.

根据本公开的另一方面,提供了一种计算机程序产品,包括计算机程序/指令,其特征在于,该计算机程序/指令被处理器执行时实现本公开任一实施例中的方法。According to another aspect of the present disclosure, a computer program product is provided, comprising a computer program/instruction, characterized in that, when the computer program/instruction is executed by a processor, the method in any of the embodiments of the present disclosure is implemented.

本公开的技术,能够确定用于标识多头自注意力模块中待剪枝的自注意力头的剪枝掩码,并利用剪枝掩码,对视觉转换器模型的目标子层的多头自注意力模块进行自注意力头的剪枝。从而能够降低视觉转换器模型部署和运算所需要消耗的资源。The technology of the present disclosure can determine the pruning mask for identifying the self-attention head to be pruned in the multi-head self-attention module, and use the pruning mask to perform multi-head self-attention on the target sub-layer of the visual converter model. The force module performs self-attention head pruning. Therefore, the resources required for deployment and computation of the vision converter model can be reduced.

应当理解,本部分所描述的内容并非旨在标识本公开的实施例的关键或重要特征,也不用于限制本公开的范围。本公开的其它特征将通过以下的说明书而变得容易理解。It should be understood that what is described in this section is not intended to identify key or critical features of embodiments of the disclosure, nor is it intended to limit the scope of the disclosure. Other features of the present disclosure will become readily understood from the following description.

附图说明Description of drawings

附图用于更好地理解本方案,不构成对本公开的限定。其中:The accompanying drawings are used for better understanding of the present solution, and do not constitute a limitation to the present disclosure. in:

图1为本公开的实施例中提供一种模型剪枝方法的流程图;1 provides a flowchart of a model pruning method in an embodiment of the present disclosure;

图2本公开的实施例中提供的一种Vision Transformer模型结构示意图;2 is a schematic structural diagram of a Vision Transformer model provided in an embodiment of the present disclosure;

图3为本公开的实施例中提供的一种自注意头剪枝过程的示意图;3 is a schematic diagram of a self-attention head pruning process provided in an embodiment of the present disclosure;

图4为本公开的实施例中提供的一种剪枝掩码确定方法的流程图;4 is a flowchart of a method for determining a pruning mask provided in an embodiment of the present disclosure;

图5为本公开的实施例提供一种模型剪枝装置的示意图;FIG. 5 provides a schematic diagram of a model pruning device according to an embodiment of the present disclosure;

图6为本公开的实施例提供的一种电子设备的示意图。FIG. 6 is a schematic diagram of an electronic device according to an embodiment of the present disclosure.

具体实施方式Detailed ways

以下结合附图对本公开的示范性实施例做出说明,其中包括本公开实施例的各种细节以助于理解,应当将它们认为仅仅是示范性的。因此,本领域普通技术人员应当认识到,可以对这里描述的实施例做出各种改变和修改,而不会背离本公开的范围和精神。同样,为了清楚和简明,以下的描述中省略了对公知功能和结构的描述。Exemplary embodiments of the present disclosure are described below with reference to the accompanying drawings, which include various details of the embodiments of the present disclosure to facilitate understanding and should be considered as exemplary only. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the present disclosure. Also, descriptions of well-known functions and constructions are omitted from the following description for clarity and conciseness.

本公开的实施例提供一种模型剪枝方法,具体请参照图1,其为本公开的实施例中提供一种模型剪枝方法的流程图。该方法可以包括以下步骤:An embodiment of the present disclosure provides a method for model pruning. For details, please refer to FIG. 1 , which is a flowchart of a method for model pruning in an embodiment of the present disclosure. The method may include the following steps:

步骤S101:根据目标子层输出的图像特征向量矩阵,确定目标子层中的多头自注意力模块对应的剪枝掩码,目标子层为视觉转换器模型的子层,剪枝掩码用于标识多头自注意力模块中待剪枝的自注意力头。Step S101: According to the image feature vector matrix output by the target sublayer, determine the pruning mask corresponding to the multi-head self-attention module in the target sublayer, the target sublayer is the sublayer of the visual converter model, and the pruning mask is used for Identifies the self-attention head to be pruned in the multi-head self-attention module.

步骤S102:利用剪枝掩码,对多头自注意力模块进行剪枝。Step S102: Use the pruning mask to prune the multi-head self-attention module.

本公开的实施例提供的模型剪枝方法,能够确定用于标识多头自注意力模块中待剪枝的自注意力头的剪枝掩码,并利用剪枝掩码,对视觉转换器模型的目标子层的多头自注意力模块进行自注意力头的剪枝。从而能够降低视觉转换器模型部署和运算所需要消耗的资源。The model pruning method provided by the embodiments of the present disclosure can determine the pruning mask used to identify the self-attention head to be pruned in the multi-head self-attention module, and use the pruning mask to prune the visual converter model. The multi-head self-attention module of the target sublayer performs self-attention head pruning. Therefore, the resources required for deployment and computation of the vision converter model can be reduced.

对于Vision Transformer模型而言,该模型一般包括图像向量转换层、编码层和解码层(统称为Transformer Layer),编码层和解码层分别包括多个编码子层和解码子层。具体的,编码子层为视觉转换器模型的编码层中的子层,解码子层为视觉转换器模型的解码层中的子层。For the Vision Transformer model, the model generally includes an image vector conversion layer, an encoding layer and a decoding layer (collectively referred to as Transformer Layer), and the encoding layer and the decoding layer respectively include a plurality of encoding sub-layers and decoding sub-layers. Specifically, the encoding sublayer is a sublayer in the encoding layer of the visual converter model, and the decoding sublayer is a sublayer in the decoding layer of the visual converter model.

本公开的实施例提供的模型剪枝方法针对的对象为该Vision Transformer模型中的任意一个或者多个编码子层和解码子层。也就是说,可以在多个编码子层和解码子层任意选择至少一个子层作为目标子层。具体的,可以按照预设的资源消耗,以及视觉转换器模型要求的模型输出的精确度,在多个编码子层和解码子层有选择的确定出目标子层。The target object of the model pruning method provided by the embodiments of the present disclosure is any one or more encoding sublayers and decoding sublayers in the Vision Transformer model. That is, at least one sublayer may be arbitrarily selected as the target sublayer among the plurality of encoding sublayers and decoding sublayers. Specifically, the target sub-layer can be selectively determined from multiple encoding sub-layers and decoding sub-layers according to preset resource consumption and accuracy of model output required by the visual converter model.

以下结合图2,对于对多头自注意力模块中的自注意头进行剪枝,以降低视觉转换器模型部署和运算所需要消耗的资源的原理进行详细的说明。图2为本公开的实施例中提供的一种Vision Transformer模型结构示意图。需要说明的是,图2中仅示出了Visiontransformer模型中的图像向量转换层和多个编码子层。The principle of pruning the self-attention heads in the multi-head self-attention module to reduce the resources consumed by the deployment and operation of the visual converter model will be described in detail below with reference to FIG. 2 . FIG. 2 is a schematic structural diagram of a Vision Transformer model provided in an embodiment of the present disclosure. It should be noted that only the image vector conversion layer and multiple coding sub-layers in the Visiontransformer model are shown in FIG. 2 .

图像向量转换层主要是对输入的图像做线性变换和/或像素压平排列,将输入的图像重塑成一个向量,并将该向量输入到编码层;编码层中每个编码子层都对应于一个编码器,该编码器依次由标准模块、多头注意力模块(Multi-Head Attention)、多层感知模块(MLP,也叫多层感知机,一般有两层)组成。The image vector conversion layer mainly performs linear transformation and/or pixel flattening arrangement on the input image, reshapes the input image into a vector, and inputs the vector into the coding layer; each coding sublayer in the coding layer corresponds to For an encoder, the encoder consists of a standard module, a multi-head attention module (Multi-Head Attention), and a multi-layer perceptron module (MLP, also called a multi-layer perceptron, generally has two layers).

在实际使用场景中,一般将图像分成多个小块(patch),每个小块的分辨率相等。每个小块对应模型的一个输入位置,经过图像向量转换层之后,生成与小块个数相等的特征向量。然后该特征向量依次输入至进入多个编码子层,最后一个编码层输出的特征向量会被输入至分类器,然后得到分类结果。该分类结果可以是一个概率值,比如识别出该输入图像是一只狗的概率是90%,是一只猫的概率是10%。In an actual usage scenario, an image is generally divided into multiple patches, and the resolutions of each patch are equal. Each small block corresponds to an input position of the model, and after passing through the image vector conversion layer, a feature vector equal to the number of small blocks is generated. Then the feature vector is input to multiple coding sub-layers in turn, and the feature vector output by the last coding layer will be input to the classifier, and then the classification result will be obtained. The classification result may be a probability value, for example, the probability of recognizing that the input image is a dog is 90%, and the probability of being a cat is 10%.

对于某一编码子层而言,其同时处理多个patch计算量可以由公式(1)-(4)推导出来,具体地,公式(1)-(3)分别估算的是编码子层计算过程中三个主要步骤的计算量,公式(4)代表着整个编码子层的计算量。其中,N代表着输入patch的个数或输入特征向量的个数,D是一个嵌入维度的大小(embedding size/embedding dim),是训练过程中特征向量中头(head,也被称为自注意力头、单个自注意力计算头)的个数以及每个特征向量的维度(dim,也称为特征向量的长度)的乘积,[N,D]代表维度是(N,D)的矩阵,[D,D]代表维度是(D,D)的矩阵,[N,D]、[N,N]类似,此处不再赘述。For a certain coding sublayer, the calculation amount of processing multiple patches at the same time can be derived from formulas (1)-(4). Specifically, formulas (1)-(3) respectively estimate the calculation process of the coding sublayer The calculation amount of the three main steps in the formula (4) represents the calculation amount of the entire coding sub-layer. Among them, N represents the number of input patches or the number of input feature vectors, and D is the size of an embedding dimension (embedding size/embedding dim), which is the head (also called self-attention) in the feature vector during the training process. The product of the number of force heads, a single self-attention computing head) and the dimension of each feature vector (dim, also known as the length of the feature vector), [N, D] represents a matrix of dimension (N, D), [D, D] represents a matrix whose dimension is (D, D). [N, D] and [N, N] are similar, and will not be repeated here.

4×([N,D]×[D,D])=>4ND2——(1)4×([N, D]×[D, D])=>4ND 2 ——(1)

[N,D]×[D,N]+[N,N]×[N,D]=>2N2 D——(2)[N,D]×[D,N]+[N,N]×[N,D]=>2N 2 D——(2)

[N,D]×[D,4D]+[N,4D]×[4D,D]=>8ND2——(3)[N, D]×[D, 4D]+[N, 4D]×[4D, D]=>8ND 2 ——(3)

12ND2+2N2 D——(4)12ND 2 +2N 2 D——(4)

由公式(1)-(4)可知,通过降低多头自注意模块中的自注意力头的数量,能够降低编码子层的计算量。并且对于多头自注意模块而言,重要性存在差异,例如,重要性较低的自注意力头关注的图像区域其实对最后的输出并没有用。因此,将重要性较低的的自注意力头剪枝,能够在确保输出的准确性的前提下,能够降低编码子层的计算量,进而能够降低视觉转换器模型部署和运算所需要消耗的资源。From formulas (1)-(4), it can be known that by reducing the number of self-attention heads in the multi-head self-attention module, the calculation amount of the encoding sub-layer can be reduced. And for the multi-head self-attention module, there are differences in importance, for example, the image regions that the less important self-attention heads pay attention to are actually not useful for the final output. Therefore, pruning the less important self-attention heads can reduce the calculation amount of the encoding sub-layer under the premise of ensuring the accuracy of the output, and thus can reduce the consumption of the vision converter model deployment and operation. resource.

另外,由公式(1)-(4)可知,对于压缩embedding dim(也被称为feature dim),也可降低视觉转换器模型部署和运算所需要消耗的资源。但对于多头自注意模块而言,每个自注意力头是有物理意义的,如果只对embedding(向量)维度进行降维,很有可能将信息比较丰富的自注意力头剪枝。因此,对视觉转换器模型的目标子层的多头自注意力模块进行自注意力头的剪枝,能够在降低视觉转换器模型部署和运算所需要消耗的资源的情况下,确保模型输出精准度。In addition, it can be known from equations (1)-(4) that for compressing the embedding dim (also called feature dim), the resources required for the deployment and operation of the visual converter model can also be reduced. However, for the multi-head self-attention module, each self-attention head has physical meaning. If only the dimension of embedding (vector) is reduced, it is very possible to prune the self-attention head with rich information. Therefore, pruning the multi-head self-attention module of the target sub-layer of the vision converter model can ensure the accuracy of the model output while reducing the resources consumed by the deployment and operation of the vision converter model. .

为了能够精准的确定出要剪枝的自注意力头,本公开的实施例中具体采用如下方式来获得剪枝掩码,具体请参见图3,图3为本公开的实施例中提供的一种自注意头剪枝过程的示意图。In order to accurately determine the self-attention head to be pruned, the pruning mask is obtained in the following manner in the embodiment of the present disclosure. For details, please refer to FIG. 3 , which is a A schematic diagram of the self-attention head pruning process.

假设将一张图像分为9个patch,经过图像向量转换层对着9个patch进行线性映射或者压平操作(linear projection or flatten)后,生成9个token,假定token(标识)的size(规模)为384,token组成的矩阵维度为:[9,384]。将token输入到Transformer Layer,token在经过Transformer Layer后,矩阵维度不改变,直至到达最终的输出层。即,Transformer Layer输出对应的图像特征向量矩阵。在Transformer Layer i(第i子层)输出对应的图像特征向量矩阵后,即可针对图像特征向量矩阵,确定剪枝掩码,具体过程如下:Suppose an image is divided into 9 patches, and after the image vector conversion layer performs linear mapping or flattening operation (linear projection or flatten) on the 9 patches, 9 tokens are generated, and it is assumed that the size (scale) of the token (identity) ) is 384, and the matrix dimension composed of tokens is: [9, 384]. Input the token into the Transformer Layer. After the token passes through the Transformer Layer, the matrix dimension does not change until it reaches the final output layer. That is, the Transformer Layer outputs the corresponding image feature vector matrix. After Transformer Layer i (the i-th sublayer) outputs the corresponding image feature vector matrix, the pruning mask can be determined for the image feature vector matrix. The specific process is as follows:

第一,在颜色通道维度对图像特征矩阵进行分离操作后得到第一向量矩阵。即,第一向量矩阵为在颜色通道维度对图像特征矩阵进行分离操作后得到的向量矩阵。具体的,图像特征向量矩阵的矩阵维度为[9,384],经过reshape(矩阵变换)变换成矩阵维度为[9,6,64]的向量矩阵,再经过split(分离)操作后得到两个矩阵维度为[9,6,32]的第一向量矩阵。具体的,图像特征向量矩阵[N,H,C]的N=9,H=6,C=32。其中,N代表着输入patch的个数或输入特征向量的个数,H代表多头自注意模块中的多注意力头的数目,C代表图像的颜色通道。First, a first vector matrix is obtained after the image feature matrix is separated in the color channel dimension. That is, the first vector matrix is a vector matrix obtained by performing a separation operation on the image feature matrix in the color channel dimension. Specifically, the matrix dimension of the image feature vector matrix is [9, 384], which is transformed into a vector matrix with the matrix dimension [9, 6, 64] after reshape (matrix transformation), and then the split (separation) operation is performed to obtain two The first vector matrix with matrix dimensions [9, 6, 32]. Specifically, N=9, H=6, and C=32 of the image feature vector matrix [N, H, C]. Among them, N represents the number of input patches or the number of input feature vectors, H represents the number of multi-attention heads in the multi-head self-attention module, and C represents the color channel of the image.

第二,对第一向量矩阵在颜色通道维度取均值,得到第二向量矩阵以及第三向量矩阵。具体的,针对两个特征向量矩阵执行取均值操作,得到两个矩阵维度为[6,32]的矩阵,分别为第二向量矩阵以及第三向量矩阵。Second, the first vector matrix is averaged in the color channel dimension to obtain the second vector matrix and the third vector matrix. Specifically, the mean value operation is performed on the two eigenvector matrices to obtain two matrices with matrix dimensions [6, 32], which are the second vector matrix and the third vector matrix, respectively.

第三,对第二向量矩阵在多头自注意力模块对应的多头维度取均值,得到第四向量矩阵。即,对第二向量矩阵在多头维度取均值执行进一步的取均值操作,得到矩阵维度为[1,32]的第四向量矩阵,该第四向量矩阵全局为向量。Third, take the mean value of the second vector matrix in the multi-head dimension corresponding to the multi-head self-attention module to obtain a fourth vector matrix. That is, a further mean value operation is performed on the mean value of the second vector matrix in the multi-head dimension to obtain a fourth vector matrix whose matrix dimension is [1, 32], and the fourth vector matrix is a vector globally.

第四,对第四向量矩阵进行拓展操作得到第五向量矩阵。具体的,利用expand(扩展)操作得到矩阵维度为[6,32]的第五向量矩阵。其中,所谓扩展操作的具体实现方式可以为对矩阵维度为[1,32]的第四向量矩阵进行复制操作,得到矩阵维度为[6,32]的第五向量矩阵。Fourth, an expansion operation is performed on the fourth vector matrix to obtain a fifth vector matrix. Specifically, an expand (expand) operation is used to obtain a fifth vector matrix whose matrix dimension is [6, 32]. The specific implementation manner of the so-called expansion operation may be to perform a copy operation on the fourth vector matrix whose matrix dimension is [1, 32] to obtain a fifth vector matrix whose matrix dimension is [6, 32].

第五,将第三向量矩阵与具有相同维度的第五向量矩阵进行拼接,得到第六向量矩阵。具体的,将矩阵维度为[6,32]的第五向量矩阵与另一矩阵维度为[6,32]的第三向量矩阵进行拼接,得到矩阵维度为[6,64]的第六向量矩阵。所谓矩阵的拼接是指对行数相同的矩阵进行列合并(左右合并),对列数相同的矩阵进行行合并(上下合并)。例如对于矩阵a=np.array([1,2]),矩阵b=np.array([2,4]),进行左右合并a=np.hstack((a,b)),得到输出print(a)=[1,2,2,4]。Fifth, the third vector matrix is spliced with the fifth vector matrix having the same dimension to obtain the sixth vector matrix. Specifically, a fifth vector matrix whose matrix dimension is [6, 32] is spliced with another third vector matrix whose matrix dimension is [6, 32] to obtain a sixth vector matrix whose matrix dimension is [6, 64] . The so-called splicing of matrices refers to column merging (left and right merging) for matrices with the same number of rows, and row merging (up and down merging) for matrices with the same number of columns. For example, for matrix a=np.array([1, 2]), matrix b=np.array([2,4]), perform left and right merging a=np.hstack((a,b)), get the output print( a)=[1, 2, 2, 4].

第六,在所述多头维度对第六向量矩阵进行线性变化,得到矩阵维度为二的第七向量矩阵。具体的,将第六向量矩阵输入到线性映射层(linear projection)得到矩阵维度为[6,2]的第七向量矩阵。Sixthly, linearly change the sixth vector matrix in the multi-head dimension to obtain a seventh vector matrix with a matrix dimension of two. Specifically, the sixth vector matrix is input to a linear projection layer to obtain a seventh vector matrix whose matrix dimension is [6, 2].

第七,基于第七向量矩阵,确定剪枝掩码。Seventh, based on the seventh vector matrix, a pruning mask is determined.

本公开的实施例基于第七向量矩阵,确定剪枝掩码的具体实现方式参照图4,图4为本公开的实施例中提供的一种剪枝掩码确定方法的流程图。Referring to FIG. 4 for a specific implementation manner of determining a pruning mask based on the seventh vector matrix in an embodiment of the present disclosure, FIG. 4 is a flowchart of a method for determining a pruning mask provided in an embodiment of the present disclosure.

步骤S401:对第七向量矩阵进行归一化处理,得到第七向量矩阵在不同维度的分布概率。Step S401: Normalize the seventh vector matrix to obtain distribution probabilities of the seventh vector matrix in different dimensions.

步骤S402:根据分布概率,将指定维度的向量作为剪枝掩码。Step S402: According to the distribution probability, the vector of the specified dimension is used as the pruning mask.

本公开的实施例中,根据分布概率,将指定维度的向量作为剪枝掩码,能够确保掩码矩阵准确的标识多头自注意力模块中待剪枝的自注意力头。In the embodiment of the present disclosure, according to the distribution probability, the vector of the specified dimension is used as the pruning mask, which can ensure that the mask matrix accurately identifies the self-attention head to be pruned in the multi-head self-attention module.

请再参照图3,本公开的实施例中,可以将矩阵维度为[6,2]的第七向量矩阵经过归一化子层(log-softmax层)获取2个维度的分布概率。其中,在多头维度的维度值为6(H=6),另一个维度的维度值为2。在得到分布概率后,可以针对第二维度的概率分布,使用argsort操作得到指定维度(如图3中下标为1)的向量作为剪枝掩码。Referring to FIG. 3 again, in an embodiment of the present disclosure, a seventh vector matrix with a matrix dimension of [6, 2] can be passed through a normalization sublayer (log-softmax layer) to obtain a distribution probability of two dimensions. Among them, the dimension value of the multi-head dimension is 6 (H=6), and the dimension value of the other dimension is 2. After obtaining the distribution probability, for the probability distribution of the second dimension, the argsort operation can be used to obtain a vector of a specified dimension (subscript 1 in Figure 3) as a pruning mask.

为了能够将重要性低的自注意力头剪枝,以在确保输出的准确性的前提下,降低目标子层的计算量,本公开的实施例中,在确定剪枝掩码后,可以将按照如下方式来对自注意力头进行剪枝,具体的:首先,确定剪枝掩码中的掩码值与多头自注意力模块中的自注意力头之间的对应关系。然后,在剪枝掩码中包括掩码值为零的掩码的情况下,基于对应关系,将掩码值为零的掩码对应的自注意力头进行剪枝。其中,掩码值与多头自注意力模块中的各个自注意力头一一对应。In order to prune the self-attention heads with low importance to reduce the calculation amount of the target sub-layer on the premise of ensuring the accuracy of the output, in the embodiment of the present disclosure, after the pruning mask is determined, the The self-attention head is pruned in the following manner, specifically: first, the corresponding relationship between the mask value in the pruning mask and the self-attention head in the multi-head self-attention module is determined. Then, in the case where the mask with the mask value of zero is included in the pruning mask, based on the correspondence, the self-attention head corresponding to the mask with the mask value of zero is pruned. Among them, the mask value corresponds to each self-attention head in the multi-head self-attention module one-to-one.

具体的,在剪枝掩码为[1,0,1,1,1,1]的情况下,即将多头自注意力模块中的第二个自注意力头剪枝(将剪枝掩码与图像特征向量矩阵相乘)。如果剪枝掩码为[0,1,0,1,1,1],即将多头自注意力模块中的第一和三个自注意力头剪枝,而将其余的自注意力头保留。Specifically, when the pruning mask is [1, 0, 1, 1, 1, 1], the second self-attention head in the multi-head self-attention module is pruned (combining the pruning mask with image feature vector matrix multiplication). If the pruning mask is [0, 1, 0, 1, 1, 1], the first and third self-attention heads in the multi-head self-attention module are pruned, and the rest of the self-attention heads are kept.

为了能够方便、精准的确定剪枝掩码,本公开的实施例可以将图像特征向量矩阵输入到掩码推导模块中,得到剪枝掩码,掩码推导模块为在视觉转换器模型训练过程中同时训练的用于确定剪枝掩码的模块。In order to conveniently and accurately determine the pruning mask, the embodiment of the present disclosure can input the image feature vector matrix into the mask derivation module to obtain the pruning mask. The mask derivation module is used in the training process of the visual converter model. A concurrently trained module for determining pruning masks.

该掩码推导模块是在训练视觉转换器模型时同步进行训练的模块。具体的,视觉转换器模型的训练过程为:将样本图像以及对应的标注输入到待训练的视觉转换器模型,获得待训练的视觉转换器模型输出的结果。根据结果与标注对应的损失函数值,不断调整待训练的视觉转换器模型中的参数,直至训练次数满足预设次数,从而获得视觉转换器模型。The mask derivation module is a module that is trained synchronously when training the visual transformer model. Specifically, the training process of the visual converter model is as follows: input the sample images and corresponding annotations into the visual converter model to be trained, and obtain the output result of the visual converter model to be trained. According to the loss function value corresponding to the result and the annotation, the parameters in the visual converter model to be trained are continuously adjusted until the number of training times meets the preset number of times, thereby obtaining the visual converter model.

在待训练的视觉转换器模型训练阶段,掩码推导模块也在执行相应的步骤,掩码推导模块的参数也在不断地被调整。但是,在模型训练阶段,为了确保数据的一致性,不会对多头自注意力模块进行剪枝,而是会将掩码值为零的掩码对应的自注意力头的输出置零,如剪枝掩码为[0,1,0,1,1,1],即将多头自注意力模块中的第一和三个自注意力头的输出置零。During the training phase of the visual converter model to be trained, the mask derivation module is also performing corresponding steps, and the parameters of the mask derivation module are constantly being adjusted. However, in the model training stage, in order to ensure the consistency of the data, the multi-head self-attention module will not be pruned, but the output of the self-attention head corresponding to the mask whose mask value is zero will be set to zero, such as The pruning mask is [0, 1, 0, 1, 1, 1], i.e. zeroing the outputs of the first and third self-attention heads in the multi-head self-attention module.

另外,在模型训练阶段,在得到分布概率后,并非针对第二维度的概率分布,使用argsort操作得到下标为1的向量作为剪枝掩码。而是先将第二维度的概率分布输入到Gumbel-softmax层,将该概率分布变成one-hot编码,再获取通道1上的向量作为剪枝掩码。In addition, in the model training stage, after obtaining the distribution probability, instead of the probability distribution of the second dimension, the argsort operation is used to obtain a vector with a subscript of 1 as the pruning mask. Instead, first input the probability distribution of the second dimension into the Gumbel-softmax layer, turn the probability distribution into one-hot encoding, and then obtain the vector on channel 1 as the pruning mask.

如图5所示,本公开的实施例提供一种模型剪枝装置,该装置包括:As shown in FIG. 5 , an embodiment of the present disclosure provides a model pruning device, which includes:

剪枝掩码确定单元501,用于根据目标子层输出的图像特征向量矩阵,确定目标子层中的多头自注意力模块对应的剪枝掩码,目标子层为视觉转换器模型的子层,剪枝掩码用于标识多头自注意力模块中待剪枝的自注意力头;The pruning mask determining unit 501 is used to determine the pruning mask corresponding to the multi-head self-attention module in the target sublayer according to the image feature vector matrix output by the target sublayer, and the target sublayer is the sublayer of the visual converter model , the pruning mask is used to identify the self-attention head to be pruned in the multi-head self-attention module;

模块剪枝单元502,用于利用剪枝掩码,对多头自注意力模块进行剪枝。The module pruning unit 502 is used for pruning the multi-head self-attention module by using the pruning mask.

在一种实施方式中,剪枝掩码确定单元501,可以包括:In one embodiment, the pruning mask determining unit 501 may include:

第一向量矩阵处理子单元,用于对图像特征向量矩阵对应的第一向量矩阵在颜色通道维度取均值,得到第二向量矩阵以及第三向量矩阵,第一向量矩阵为在颜色通道维度对图像特征矩阵进行分离操作后得到的向量矩阵;The first vector-matrix processing subunit is used to average the first vector-matrix corresponding to the image feature vector matrix in the color channel dimension to obtain the second vector-matrix and the third vector-matrix, where the first vector-matrix is the image in the color channel dimension. The vector matrix obtained after the feature matrix is separated;

第二向量矩阵处理子单元,用于对第二向量矩阵在多头自注意力模块对应的多头维度取均值,得到第四向量矩阵;The second vector-matrix processing subunit is used to average the second vector-matrix in the multi-head dimension corresponding to the multi-head self-attention module to obtain the fourth vector-matrix;

第三向量矩阵处理子单元,用于将第三向量矩阵与具有相同维度的第五向量矩阵进行拼接,得到第六向量矩阵,第五向量矩阵为对第四向量矩阵进行拓展操作得到的向量矩阵;The third vector-matrix processing subunit is used for splicing the third vector-matrix and the fifth vector-matrix with the same dimension to obtain a sixth vector-matrix, and the fifth vector-matrix is a vector-matrix obtained by extending the fourth vector-matrix ;

第四向量矩阵处理子单元,用于在所述多头维度对第六向量矩阵进行线性变化,得到矩阵维度为二的第七向量矩阵;The fourth vector-matrix processing subunit is used to linearly change the sixth vector-matrix in the multi-head dimension to obtain the seventh vector-matrix with a matrix dimension of two;

剪枝掩码确定子单元,用于基于第七向量矩阵,确定剪枝掩码。The pruning mask determining subunit is used to determine the pruning mask based on the seventh vector matrix.

在一种实施方式中,剪枝掩码确定子单元,可以包括:In one embodiment, the pruning mask determines the subunit, which may include:

矩阵归一化子单元,用于对第七向量矩阵进行归一化处理,得到第七向量矩阵的在不同维度的分布概率;The matrix normalization subunit is used to normalize the seventh vector matrix to obtain the distribution probability of the seventh vector matrix in different dimensions;

剪枝掩码获得子单元,用于根据分布概率,将指定维度的向量作为剪枝掩码。The pruning mask obtains the subunit, which is used to use the vector of the specified dimension as the pruning mask according to the distribution probability.

在一种实施方式中,模块剪枝单元502,可以包括:In one embodiment, the modular pruning unit 502 may include:

对应关系确定子单元,用于确定剪枝掩码中的掩码值与多头自注意力模块中的自注意力头之间的对应关系;The correspondence determination subunit is used to determine the correspondence between the mask value in the pruning mask and the self-attention head in the multi-head self-attention module;

模块剪枝子单元,用于在剪枝掩码中包括掩码值为零的掩码的情况下,基于对应关系,将掩码值为零的掩码对应的自注意力头进行剪枝。The module pruning subunit is used to prune the self-attention head corresponding to the mask with the mask value of zero based on the correspondence when the pruning mask includes a mask with a mask value of zero.

在一种实施方式中,剪枝掩码确定单元501,可以包括:In one embodiment, the pruning mask determining unit 501 may include:

掩码推导模块子单元,用于将图像特征向量矩阵输入到掩码推导模块中,得到剪枝掩码,掩码推导模块为在视觉转换器模型训练过程中同时训练的用于确定剪枝掩码的模块。The mask derivation module subunit is used to input the image feature vector matrix into the mask derivation module to obtain the pruning mask. The mask derivation module is simultaneously trained during the training of the visual converter model and is used to determine the pruning mask. code module.

在一种实施方式中,目标子层包括编码子层以及解码子层中的至少一个子层,编码子层为视觉转换器模型的编码层中的子层,解码子层为视觉转换器模型的解码层中的子层。In one embodiment, the target sublayer includes at least one of an encoding sublayer and a decoding sublayer, the encoding sublayer is a sublayer in the encoding layer of the visual translator model, and the decoding sublayer is a sublayer of the visual translator model. Sublayers in the decoding layer.

本公开的技术方案中,所涉及的用户个人信息的获取,存储和应用等,均符合相关法律法规的规定,且不违背公序良俗。In the technical solution of the present disclosure, the acquisition, storage and application of the user's personal information involved are all in compliance with the provisions of relevant laws and regulations, and do not violate public order and good customs.

根据本公开实施例,本公开还提供了一种电子设备和一种可读存储介质。According to an embodiment of the present disclosure, the present disclosure also provides an electronic device and a readable storage medium.

图6示出了可以用来实施本公开的实施例的示例电子设备600的示意性框图。电子设备旨在表示各种形式的数字计算机,诸如,膝上型计算机、台式计算机、工作台、个人数字助理、服务器、刀片式服务器、大型计算机、和其它适合的计算机。电子设备还可以表示各种形式的移动装置,诸如,个人数字处理、蜂窝电话、智能电话、可穿戴设备和其它类似的计算装置。本文所示的部件、它们的连接和关系、以及它们的功能仅仅作为示例,并且不意在限制本文中描述的和/或者要求的本公开的实现。FIG. 6 shows a schematic block diagram of an example electronic device 600 that may be used to implement embodiments of the present disclosure. Electronic devices are intended to represent various forms of digital computers, such as laptops, desktops, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. Electronic devices may also represent various forms of mobile devices, such as personal digital processors, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are by way of example only, and are not intended to limit implementations of the disclosure described and/or claimed herein.

如图6所示,设备600包括计算单元601,其可以根据存储在只读存储器(ROM)602中的计算机程序或者从存储单元608加载到随机访问存储器(RAM)603中的计算机程序,来执行各种适当的动作和处理。在RAM 603中,还可存储设备600操作所需的各种程序和数据。计算单元601、ROM 602以及RAM 603通过总线604彼此相连。输入/输出(I/O)接口605也连接至总线604。As shown in FIG. 6 , the device 600 includes a computing unit 601 that can be executed according to a computer program stored in a read only memory (ROM) 602 or a computer program loaded from a storage unit 608 into a random access memory (RAM) 603 Various appropriate actions and handling. In the RAM 603, various programs and data necessary for the operation of the device 600 can also be stored. The computing unit 601 , the ROM 602 , and the RAM 603 are connected to each other through a bus 604 . An input/output (I/O) interface 605 is also connected to bus 604 .

设备600中的多个部件连接至I/O接口605,包括:输入单元606,例如键盘、鼠标等;输出单元607,例如各种类型的显示器、扬声器等;存储单元608,例如磁盘、光盘等;以及通信单元609,例如网卡、调制解调器、无线通信收发机等。通信单元609允许设备600通过诸如因特网的计算机网络和/或各种电信网络与其他设备交换信息/数据。Various components in the device 600 are connected to the I/O interface 605, including: an input unit 606, such as a keyboard, mouse, etc.; an output unit 607, such as various types of displays, speakers, etc.; a storage unit 608, such as a magnetic disk, an optical disk, etc. ; and a communication unit 609, such as a network card, a modem, a wireless communication transceiver, and the like. The communication unit 609 allows the device 600 to exchange information/data with other devices through a computer network such as the Internet and/or various telecommunication networks.

计算单元601可以是各种具有处理和计算能力的通用和/或专用处理组件。计算单元601的一些示例包括但不限于中央处理单元(CPU)、图形处理单元(GPU)、各种专用的人工智能(AI)计算芯片、各种运行机器学习模型算法的计算单元、数字信号处理器(DSP)、以及任何适当的处理器、控制器、微控制器等。计算单元601执行上文所描述的各个方法和处理,例如模型剪枝方法。例如,在一些实施例中,模型剪枝方法可被实现为计算机软件程序,其被有形地包含于机器可读介质,例如存储单元608。在一些实施例中,计算机程序的部分或者全部可以经由ROM 602和/或通信单元609而被载入和/或安装到设备600上。当计算机程序加载到RAM 603并由计算单元601执行时,可以执行上文描述的模型剪枝方法的一个或多个步骤。备选地,在其他实施例中,计算单元601可以通过其他任何适当的方式(例如,借助于固件)而被配置为执行模型剪枝方法。Computing unit 601 may be various general-purpose and/or special-purpose processing components with processing and computing capabilities. Some examples of computing units 601 include, but are not limited to, central processing units (CPUs), graphics processing units (GPUs), various specialized artificial intelligence (AI) computing chips, various computing units that run machine learning model algorithms, digital signal processing processor (DSP), and any suitable processor, controller, microcontroller, etc. The computing unit 601 performs the various methods and processes described above, such as the model pruning method. For example, in some embodiments, the model pruning method may be implemented as a computer software program tangibly embodied on a machine-readable medium, such as storage unit 608 . In some embodiments, part or all of the computer program may be loaded and/or installed on device 600 via ROM 602 and/or communication unit 609 . When the computer program is loaded into RAM 603 and executed by computing unit 601, one or more steps of the model pruning method described above may be performed. Alternatively, in other embodiments, the computing unit 601 may be configured to perform the model pruning method by any other suitable means (eg, by means of firmware).

本文中以上描述的系统和技术的各种实施方式可以在数字电子电路系统、集成电路系统、场可编程门阵列(FPGA)、专用集成电路(ASIC)、专用标准产品(ASSP)、芯片上系统的系统(SOC)、负载可编程逻辑设备(CPLD)、计算机硬件、固件、软件、和/或它们的组合中实现。这些各种实施方式可以包括:实施在一个或者多个计算机程序中,该一个或者多个计算机程序可在包括至少一个可编程处理器的可编程系统上执行和/或解释,该可编程处理器可以是专用或者通用可编程处理器,可以从存储系统、至少一个输入装置、和至少一个输出装置接收数据和指令,并且将数据和指令传输至该存储系统、该至少一个输入装置、和该至少一个输出装置。Various implementations of the systems and techniques described herein above may be implemented in digital electronic circuitry, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips system (SOC), load programmable logic device (CPLD), computer hardware, firmware, software, and/or combinations thereof. These various embodiments may include being implemented in one or more computer programs executable and/or interpretable on a programmable system including at least one programmable processor that The processor, which may be a special purpose or general-purpose programmable processor, may receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device an output device.

用于实施本公开的方法的程序代码可以采用一个或多个编程语言的任何组合来编写。这些程序代码可以提供给通用计算机、专用计算机或其他可编程模型剪枝装置的处理器或控制器,使得程序代码当由处理器或控制器执行时使流程图和/或框图中所规定的功能/操作被实施。程序代码可以完全在机器上执行、部分地在机器上执行,作为独立软件包部分地在机器上执行且部分地在远程机器上执行或完全在远程机器或服务器上执行。Program code for implementing the methods of the present disclosure may be written in any combination of one or more programming languages. Such program codes may be provided to a processor or controller of a general purpose computer, special purpose computer, or other programmable model pruning device, such that the program codes, when executed by the processor or controller, perform the functions specified in the flowcharts and/or block diagrams /Operation is implemented. The program code may execute entirely on the machine, partly on the machine, partly on the machine and partly on a remote machine as a stand-alone software package or entirely on the remote machine or server.

在本公开的上下文中,机器可读介质可以是有形的介质,其可以包含或存储以供指令执行系统、装置或设备使用或与指令执行系统、装置或设备结合地使用的程序。机器可读介质可以是机器可读信号介质或机器可读储存介质。机器可读介质可以包括但不限于电子的、磁性的、光学的、电磁的、红外的、或半导体系统、装置或设备,或者上述内容的任何合适组合。机器可读存储介质的更具体示例会包括基于一个或多个线的电气连接、便携式计算机盘、硬盘、随机存取存储器(RAM)、只读存储器(ROM)、可擦除可编程只读存储器(EPROM或快闪存储器)、光纤、便捷式紧凑盘只读存储器(CD-ROM)、光学储存设备、磁储存设备、或上述内容的任何合适组合。In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in connection with the instruction execution system, apparatus or device. The machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. Machine-readable media may include, but are not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media would include one or more wire-based electrical connections, portable computer disks, hard disks, random access memory (RAM), read only memory (ROM), erasable programmable read only memory (EPROM or flash memory), fiber optics, compact disk read only memory (CD-ROM), optical storage, magnetic storage, or any suitable combination of the foregoing.

为了提供与用户的交互,可以在计算机上实施此处描述的系统和技术,该计算机具有:用于向用户显示信息的显示装置(例如,CRT(阴极射线管)或者LCD(液晶显示器)监视器);以及键盘和指向装置(例如,鼠标或者轨迹球),用户可以通过该键盘和该指向装置来将输入提供给计算机。其它种类的装置还可以用于提供与用户的交互;例如,提供给用户的反馈可以是任何形式的传感反馈(例如,视觉反馈、听觉反馈、或者触觉反馈);并且可以用任何形式(包括声输入、语音输入或者、触觉输入)来接收来自用户的输入。To provide interaction with a user, the systems and techniques described herein may be implemented on a computer having a display device (eg, a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user ); and a keyboard and pointing device (eg, a mouse or trackball) through which a user can provide input to the computer. Other kinds of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (eg, visual feedback, auditory feedback, or tactile feedback); and can be in any form (including acoustic input, voice input, or tactile input) to receive input from the user.

可以将此处描述的系统和技术实施在包括后台部件的计算系统(例如,作为数据服务器)、或者包括中间件部件的计算系统(例如,应用服务器)、或者包括前端部件的计算系统(例如,具有图形用户界面或者网络浏览器的用户计算机,用户可以通过该图形用户界面或者该网络浏览器来与此处描述的系统和技术的实施方式交互)、或者包括这种后台部件、中间件部件、或者前端部件的任何组合的计算系统中。可以通过任何形式或者介质的数字数据通信(例如,通信网络)来将系统的部件相互连接。通信网络的示例包括:局域网(LAN)、广域网(WAN)和互联网。The systems and techniques described herein may be implemented on a computing system that includes back-end components (eg, as a data server), or a computing system that includes middleware components (eg, an application server), or a computing system that includes front-end components (eg, a user's computer having a graphical user interface or web browser through which a user may interact with implementations of the systems and techniques described herein), or including such backend components, middleware components, Or any combination of front-end components in a computing system. The components of the system may be interconnected by any form or medium of digital data communication (eg, a communication network). Examples of communication networks include: Local Area Networks (LANs), Wide Area Networks (WANs), and the Internet.

计算机系统可以包括客户端和服务器。客户端和服务器一般远离彼此并且通常通过通信网络进行交互。通过在相应的计算机上运行并且彼此具有客户端-服务器关系的计算机程序来产生客户端和服务器的关系。服务器可以是云服务器,也可以为分布式系统的服务器,或者是结合了区块链的服务器。A computer system can include clients and servers. Clients and servers are generally remote from each other and usually interact through a communication network. The relationship of client and server arises by computer programs running on the respective computers and having a client-server relationship to each other. The server can be a cloud server, a distributed system server, or a server combined with blockchain.

应该理解,可以使用上面所示的各种形式的流程,重新排序、增加或删除步骤。例如,本发公开中记载的各步骤可以并行地执行也可以顺序地执行也可以不同的次序执行,只要能够实现本公开公开的技术方案所期望的结果,本文在此不进行限制。It should be understood that steps may be reordered, added or deleted using the various forms of flow shown above. For example, the steps described in the present disclosure can be executed in parallel, sequentially, or in different orders. As long as the desired results of the technical solutions disclosed in the present disclosure can be achieved, there is no limitation herein.

上述具体实施方式,并不构成对本公开保护范围的限制。本领域技术人员应该明白的是,根据设计要求和其他因素,可以进行各种修改、组合、子组合和替代。任何在本公开的精神和原则之内所作的修改、等同替换和改进等,均应包含在本公开保护范围之内。The above-mentioned specific embodiments do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modifications, equivalent replacements and improvements made within the spirit and principles of the present disclosure should be included within the protection scope of the present disclosure.

Claims (15)

1.一种模型剪枝方法,包括:1. A model pruning method, comprising: 根据目标子层输出的图像特征向量矩阵,确定所述目标子层中的多头自注意力模块对应的剪枝掩码,所述目标子层为视觉转换器模型的子层,所述剪枝掩码用于标识所述多头自注意力模块中待剪枝的自注意力头;According to the image feature vector matrix output by the target sublayer, determine the pruning mask corresponding to the multi-head self-attention module in the target sublayer, the target sublayer is a sublayer of the visual converter model, and the pruning mask code is used to identify the self-attention head to be pruned in the multi-head self-attention module; 利用所述剪枝掩码,对所述多头自注意力模块进行剪枝。Using the pruning mask, the multi-head self-attention module is pruned. 2.根据权利要求1的方法,其中,所述根据目标子层输出的图像特征向量矩阵,确定所述目标子层中的多头自注意力模块对应的剪枝掩码,包括:2. The method according to claim 1, wherein, determining the pruning mask corresponding to the multi-head self-attention module in the target sublayer according to the image feature vector matrix output by the target sublayer, comprising: 对所述图像特征向量矩阵对应的第一向量矩阵在颜色通道维度取均值,得到第二向量矩阵以及第三向量矩阵,所述第一向量矩阵为在颜色通道维度对所述图像特征矩阵进行分离操作得到的向量矩阵;The first vector matrix corresponding to the image feature vector matrix is averaged in the color channel dimension to obtain a second vector matrix and a third vector matrix, and the first vector matrix is to separate the image feature matrix in the color channel dimension. The vector matrix obtained by the operation; 对所述第二向量矩阵在所述多头自注意力模块对应的多头维度取均值,得到第四向量矩阵;The second vector matrix is averaged in the multi-head dimension corresponding to the multi-head self-attention module to obtain a fourth vector matrix; 将所述第三向量矩阵与具有相同维度的第五向量矩阵进行拼接,得到第六向量矩阵,所述第五向量矩阵为对所述第四向量矩阵进行拓展操作得到的向量矩阵;The third vector matrix is spliced with the fifth vector matrix with the same dimension, and the sixth vector matrix is obtained, and the fifth vector matrix is the vector matrix obtained by expanding the fourth vector matrix; 在所述多头维度对所述第六向量矩阵进行线性变化,得到矩阵维度为二的第七向量矩阵;Linearly changing the sixth vector matrix in the multi-head dimension to obtain a seventh vector matrix with a matrix dimension of two; 基于所述第七向量矩阵,确定所述剪枝掩码。The pruning mask is determined based on the seventh vector matrix. 3.根据权利要求2的方法,其中,所述基于所述第七向量矩阵,确定所述剪枝掩码,包括:3. The method according to claim 2, wherein the determining the pruning mask based on the seventh vector matrix comprises: 对所述第七向量矩阵进行归一化处理,得到第七向量矩阵的在不同维度的分布概率;Normalizing the seventh vector matrix to obtain the distribution probabilities of the seventh vector matrix in different dimensions; 根据所述分布概率,将指定维度的向量作为所述剪枝掩码。According to the distribution probability, a vector of a specified dimension is used as the pruning mask. 4.根据权利要求1至3任意一项所述的方法,其中,所述利用所述剪枝掩码,对所述多头自注意力模块进行剪枝,包括:4. The method according to any one of claims 1 to 3, wherein, using the pruning mask to prune the multi-head self-attention module, comprising: 确定所述剪枝掩码中的掩码值与所述多头自注意力模块中的自注意力头之间的对应关系;determining the correspondence between the mask value in the pruning mask and the self-attention head in the multi-head self-attention module; 在所述剪枝掩码中包括掩码值为零的掩码的情况下,基于所述对应关系,将所述掩码值为零的掩码对应的所述自注意力头进行剪枝。When the pruning mask includes a mask with a mask value of zero, based on the correspondence, the self-attention head corresponding to the mask with the mask value of zero is pruned. 5.根据权利要求1的方法,其中,所述根据目标子层输出的图像特征向量矩阵,确定所述目标子层中的多头自注意力模块对应的剪枝掩码,包括:5. The method according to claim 1, wherein, determining the pruning mask corresponding to the multi-head self-attention module in the target sublayer according to the image feature vector matrix output by the target sublayer, comprising: 将所述图像特征向量矩阵输入到掩码推导模块中,得到所述剪枝掩码,所述掩码推导模块为在所述视觉转换器模型训练过程中同时训练的用于确定所述剪枝掩码的模块。Inputting the image feature vector matrix into a mask derivation module to obtain the pruning mask, and the mask derivation module is simultaneously trained during the training process of the visual converter model for determining the pruning masked module. 6.根据权利要求1的方法,其中,所述目标子层包括编码子层以及解码子层中的至少一个子层,所述编码子层为所述视觉转换器模型的编码层中的子层,所述解码子层为所述视觉转换器模型的解码层中的子层。6. The method of claim 1, wherein the target sublayer comprises at least one of an encoding sublayer and a decoding sublayer, the encoding sublayer being a sublayer in an encoding layer of the visual translator model , the decoding sublayer is a sublayer in the decoding layer of the visual converter model. 7.一种模型剪枝装置,包括:7. A model pruning device, comprising: 剪枝掩码确定单元,用于根据目标子层输出的图像特征向量矩阵,确定所述目标子层中的多头自注意力模块对应的剪枝掩码,所述目标子层为视觉转换器模型的子层,所述剪枝掩码用于标识所述多头自注意力模块中待剪枝的自注意力头;A pruning mask determining unit, used for determining the pruning mask corresponding to the multi-head self-attention module in the target sublayer according to the image feature vector matrix output by the target sublayer, where the target sublayer is a visual converter model The pruning mask is used to identify the self-attention head to be pruned in the multi-head self-attention module; 模块剪枝单元,用于利用所述剪枝掩码,对所述多头自注意力模块进行剪枝。a module pruning unit for pruning the multi-head self-attention module by using the pruning mask. 8.根据权利要求7的装置,其中,所述剪枝掩码确定单元,包括:8. The apparatus according to claim 7, wherein the pruning mask determining unit comprises: 第一向量矩阵处理子单元,用于对所述图像特征向量矩阵对应的第一向量矩阵在颜色通道维度取均值,得到第二向量矩阵以及第三向量矩阵,所述第一向量矩阵为在颜色通道维度对所述图像特征矩阵进行分离操作后得到的向量矩阵;The first vector-matrix processing subunit is used to average the first vector-matrix corresponding to the image feature vector matrix in the color channel dimension to obtain the second vector-matrix and the third vector-matrix, where the first vector-matrix is in the color channel. The vector matrix obtained after the channel dimension performs the separation operation on the image feature matrix; 第二向量矩阵处理子单元,用于对所述第二向量矩阵在所述多头自注意力模块对应的多头维度取均值,得到第四向量矩阵;A second vector-matrix processing subunit, configured to average the second vector-matrix in the multi-head dimension corresponding to the multi-head self-attention module to obtain a fourth vector-matrix; 第三向量矩阵处理子单元,用于将所述第三向量矩阵与具有相同维度的第五向量矩阵进行拼接,得到第六向量矩阵,所述第五向量矩阵为对所述第四向量矩阵进行拓展操作得到的向量矩阵;The third vector-matrix processing subunit is used for splicing the third vector-matrix and the fifth vector-matrix having the same dimension to obtain a sixth vector-matrix, where the fifth vector-matrix is the The vector matrix obtained by the expansion operation; 第四向量矩阵处理子单元,用于在所述多头维度对所述第六向量矩阵进行线性变化,得到矩阵维度为二的第七向量矩阵;a fourth vector-matrix processing subunit, used for linearly changing the sixth vector-matrix in the multi-head dimension to obtain a seventh vector-matrix with a matrix dimension of two; 剪枝掩码确定子单元,用于基于所述第七向量矩阵,确定所述剪枝掩码。A pruning mask determining subunit, configured to determine the pruning mask based on the seventh vector matrix. 9.根据权利要求8的装置,其中,所述剪枝掩码确定子单元,包括:9. The apparatus according to claim 8, wherein the pruning mask determination subunit comprises: 矩阵归一化子单元,用于对所述第七向量矩阵进行归一化处理,得到第七向量矩阵的在不同维度的分布概率;a matrix normalization subunit, used for normalizing the seventh vector matrix to obtain the distribution probabilities of the seventh vector matrix in different dimensions; 剪枝掩码获得子单元,用于根据所述分布概率,将指定维度的向量作为所述剪枝掩码。The pruning mask obtaining subunit is used to use a vector of a specified dimension as the pruning mask according to the distribution probability. 10.根据权利要求7至9任意一项所述的装置,其中,所述模块剪枝单元,包括:10. The device according to any one of claims 7 to 9, wherein the modular pruning unit comprises: 对应关系确定子单元,用于确定所述剪枝掩码中的掩码值与所述多头自注意力模块中的自注意力头之间的对应关系;Correspondence determination subunit for determining the correspondence between the mask value in the pruning mask and the self-attention head in the multi-head self-attention module; 模块剪枝子单元,用于在所述剪枝掩码中包括掩码值为零的掩码的情况下,基于所述对应关系,将所述掩码值为零的掩码对应的所述自注意力头进行剪枝。The module pruning subunit is configured to, in the case that the pruning mask includes a mask with a mask value of zero, based on the correspondence, Attention head for pruning. 11.根据权利要求8的装置,其中,所述剪枝掩码确定单元,包括:11. The apparatus according to claim 8, wherein the pruning mask determining unit comprises: 掩码推导模块子单元,用于将所述图像特征向量矩阵输入到掩码推导模块中,得到所述剪枝掩码,所述掩码推导模块为在所述视觉转换器模型训练过程中同时训练的用于确定所述剪枝掩码的模块。A mask derivation module subunit, used for inputting the image feature vector matrix into a mask derivation module to obtain the pruning mask, and the mask derivation module is simultaneously in the training process of the visual converter model A trained module for determining the pruning mask. 12.根据权利要求7的装置,其中,所述目标子层包括编码子层以及解码子层中的至少一个子层,所述编码子层为所述视觉转换器模型的编码层中的子层,所述解码子层为所述视觉转换器模型的解码层中的子层。12. The apparatus of claim 7, wherein the target sublayer comprises at least one of an encoding sublayer and a decoding sublayer, the encoding sublayer being a sublayer of an encoding layer of the visual translator model , the decoding sublayer is a sublayer in the decoding layer of the visual converter model. 13.一种电子设备,包括:13. An electronic device comprising: 至少一个处理器;以及at least one processor; and 与所述至少一个处理器通信连接的存储器;其中,a memory communicatively coupled to the at least one processor; wherein, 所述存储器存储有可被所述至少一个处理器执行的指令,所述指令被所述至少一个处理器执行,以使所述至少一个处理器能够执行权利要求1至6中任意一项所述的方法。The memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to perform the execution of any one of claims 1 to 6 Methods. 14.一种存储有计算机指令的非瞬时计算机可读存储介质,其中,所述计算机指令用于使计算机执行权利要求1至6中任意一项所述的方法。14. A non-transitory computer readable storage medium storing computer instructions for causing a computer to perform the method of any one of claims 1 to 6. 15.一种计算机程序产品,包括计算机程序/指令,其特征在于,该计算机程序/指令被处理器执行时实现权利要求1至6所述方法的步骤。15. A computer program product comprising computer programs/instructions, characterized in that, when the computer program/instructions are executed by a processor, the steps of the methods of claims 1 to 6 are implemented.
CN202111322412.3A 2021-11-09 2021-11-09 A model pruning method, device, electronic device and storage medium Pending CN114037074A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202111322412.3A CN114037074A (en) 2021-11-09 2021-11-09 A model pruning method, device, electronic device and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111322412.3A CN114037074A (en) 2021-11-09 2021-11-09 A model pruning method, device, electronic device and storage medium

Publications (1)

Publication Number Publication Date
CN114037074A true CN114037074A (en) 2022-02-11

Family

ID=80136971

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111322412.3A Pending CN114037074A (en) 2021-11-09 2021-11-09 A model pruning method, device, electronic device and storage medium

Country Status (1)

Country Link
CN (1) CN114037074A (en)

Cited By (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114819140A (en) * 2022-03-31 2022-07-29 支付宝(杭州)信息技术有限公司 Model pruning method and device and computer equipment
CN115147669A (en) * 2022-06-24 2022-10-04 北京百度网讯科技有限公司 Image processing method, training method and device based on visual converter model
CN115170917A (en) * 2022-06-20 2022-10-11 美的集团(上海)有限公司 Image processing method, electronic device, and storage medium
US20230073835A1 (en) * 2021-09-08 2023-03-09 Samsung Electronics Co., Ltd. Structured Pruning of Vision Transformer
CN116108891A (en) * 2022-12-15 2023-05-12 南京邮电大学 A Transformer-based lightweight pruning method, device, terminal and computer-readable storage medium
CN116527897A (en) * 2023-04-28 2023-08-01 中国电信股份有限公司 Image encoding method, image decoding method, image encoding device, image decoding device, electronic equipment and storage medium
CN116611477A (en) * 2023-05-31 2023-08-18 北京百度网讯科技有限公司 Training method, device, equipment and medium for data pruning method and sequence model
CN118520849A (en) * 2024-07-25 2024-08-20 广东浪潮智慧计算技术有限公司 Natural language processing task execution method, device, equipment, system and medium
US12299960B2 (en) 2022-05-10 2025-05-13 Samsung Electronics Co., Ltd. Efficiency of vision transformers with adaptive token pruning

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2021023202A1 (en) * 2019-08-07 2021-02-11 交叉信息核心技术研究院(西安)有限公司 Self-distillation training method and device for convolutional neural network, and scalable dynamic prediction method
US20210158206A1 (en) * 2019-11-25 2021-05-27 Sap Se Attention mechanism for natural language processing
CN112949840A (en) * 2021-04-20 2021-06-11 中国人民解放军国防科技大学 Channel attention guided convolutional neural network dynamic channel pruning method and device
CN113361575A (en) * 2021-05-28 2021-09-07 北京百度网讯科技有限公司 Model training method and device and electronic equipment

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2021023202A1 (en) * 2019-08-07 2021-02-11 交叉信息核心技术研究院(西安)有限公司 Self-distillation training method and device for convolutional neural network, and scalable dynamic prediction method
US20210158206A1 (en) * 2019-11-25 2021-05-27 Sap Se Attention mechanism for natural language processing
CN112949840A (en) * 2021-04-20 2021-06-11 中国人民解放军国防科技大学 Channel attention guided convolutional neural network dynamic channel pruning method and device
CN113361575A (en) * 2021-05-28 2021-09-07 北京百度网讯科技有限公司 Model training method and device and electronic equipment

Non-Patent Citations (3)

* Cited by examiner, † Cited by third party
Title
G. ROTH: "Registering two overlapping range images", SECOND INTERNATIONAL CONFERENCE ON 3-D DIGITAL IMAGING AND MODELING (CAT. NO.PR00062), 6 August 2002 (2002-08-06) *
孙显;梁伟;刁文辉;曹志颖;冯瑛超;王冰;付琨;: "遥感边缘智能技术研究进展及挑战", 中国图象图形学报, no. 09, 16 September 2020 (2020-09-16) *
邵伟平;王兴;曹昭睿;白帆;: "基于MobileNet与YOLOv3的轻量化卷积神经网络设计", 计算机应用, no. 1, 10 July 2020 (2020-07-10) *

Cited By (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20230073835A1 (en) * 2021-09-08 2023-03-09 Samsung Electronics Co., Ltd. Structured Pruning of Vision Transformer
CN114819140A (en) * 2022-03-31 2022-07-29 支付宝(杭州)信息技术有限公司 Model pruning method and device and computer equipment
WO2023185209A1 (en) * 2022-03-31 2023-10-05 支付宝(杭州)信息技术有限公司 Model pruning
CN114819140B (en) * 2022-03-31 2025-05-13 支付宝(杭州)信息技术有限公司 Model pruning method, device and computer equipment
US12299960B2 (en) 2022-05-10 2025-05-13 Samsung Electronics Co., Ltd. Efficiency of vision transformers with adaptive token pruning
CN115170917A (en) * 2022-06-20 2022-10-11 美的集团(上海)有限公司 Image processing method, electronic device, and storage medium
CN115170917B (en) * 2022-06-20 2023-11-07 美的集团(上海)有限公司 Image processing method, electronic device and storage medium
CN115147669A (en) * 2022-06-24 2022-10-04 北京百度网讯科技有限公司 Image processing method, training method and device based on visual converter model
CN116108891A (en) * 2022-12-15 2023-05-12 南京邮电大学 A Transformer-based lightweight pruning method, device, terminal and computer-readable storage medium
CN116527897A (en) * 2023-04-28 2023-08-01 中国电信股份有限公司 Image encoding method, image decoding method, image encoding device, image decoding device, electronic equipment and storage medium
CN116611477A (en) * 2023-05-31 2023-08-18 北京百度网讯科技有限公司 Training method, device, equipment and medium for data pruning method and sequence model
CN116611477B (en) * 2023-05-31 2024-05-17 北京百度网讯科技有限公司 Data pruning method and training method, device, equipment and medium for sequence model
CN118520849A (en) * 2024-07-25 2024-08-20 广东浪潮智慧计算技术有限公司 Natural language processing task execution method, device, equipment, system and medium

Similar Documents

Publication Publication Date Title
CN114037074A (en) A model pruning method, device, electronic device and storage medium
CN113326764B (en) Method and device for training image recognition model and image recognition
CN113657390B (en) Text detection model training method and text detection method, device and device
JP2023547010A (en) Model training methods, equipment, and electronics based on knowledge distillation
CN114201242B (en) Method, device, device and storage medium for processing data
CN114863229A (en) Image classification method and training method and device for image classification model
CN114549840A (en) Training method of semantic segmentation model and semantic segmentation method and device
CN113361536B (en) Image semantic segmentation model training, image semantic segmentation method and related device
CN114282670A (en) Compression method, device and storage medium for neural network model
US12424010B2 (en) Character recognition model training method and apparatus, character recognition method and apparatus, device and storage medium
CN114067099B (en) Training method of student image recognition network and image recognition method
CN114817476A (en) Language model training method and device, electronic equipment and storage medium
CN114861758A (en) Multi-modal data processing method and device, electronic equipment and readable storage medium
CN118196556A (en) Model training method, open set classification method, device, equipment and medium
CN113240082A (en) A method and device for transfer learning
CN116721217A (en) Training method of three-dimensional prediction model, three-dimensional reconstruction method and device
CN116468985A (en) Model training method, quality detection device, electronic equipment and medium
CN114419327A (en) Image detection method and training method and device of image detection model
CN114898742A (en) Method, device, equipment and storage medium for training streaming voice recognition model
CN114842541A (en) Model training and face recognition method, device, equipment and storage medium
CN114511743A (en) Detection model training method, target detection method, device, equipment, medium and product
CN117746125A (en) Training method and device of image processing model and electronic equipment
CN113642654B (en) Image feature fusion method and device, electronic equipment and storage medium
CN116385775A (en) Image tag adding method and device, electronic equipment and storage medium
CN113361575B (en) Model training method and device and electronic equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
AD01 Patent right deemed abandoned
AD01 Patent right deemed abandoned

Effective date of abandoning: 20250314