add model-security & fix eqlabel and eqref in model-deployment (#39)

Co-authored-by: hangangqiang <hangangqiang2@huawei.com>
This commit is contained in:
AssassinG
2022-03-07 09:49:08 +08:00
committed by GitHub
parent 817c00053e
commit 1cf49258bc
6 changed files with 76 additions and 28 deletions

View File

@@ -125,21 +125,21 @@ ARMv8系列的CPU上有32个NEON寄存器v0-v31如 :numref:`ch08-fig-register
卷积计算归根到底是矩阵乘法,两个二维矩阵相乘的时间复杂度是$O(n^3)$。我们可以使用Winograd来降低矩阵乘法的复杂度。
以一维卷积运算为例记为F(mr)其中m代表输出的个数r为卷积核的个数。输入为$d=[d_0 \ d_0 \ d_2 \ d_3]$,卷积核为$g=[g_0 \ g_0 \ g_2]^T$,该卷积计算可以写成矩阵形式如公式 :numref:`ch08-equ-conv_matmul_one_dimension`所示需要6次乘法和4次加法。
以一维卷积运算为例记为F(mr)其中m代表输出的个数r为卷积核的个数。输入为$d=[d_0 \ d_0 \ d_2 \ d_3]$,卷积核为$g=[g_0 \ g_0 \ g_2]^T$,该卷积计算可以写成矩阵形式如公式 :eqref:`ch08-equ-conv_matmul_one_dimension`所示需要6次乘法和4次加法。
$$F(2, 3)=
\left[ \begin{matrix} d_0 & d_0 & d_2 \\ d_1 & d_2 & d_3 \end{matrix} \right] \left[ \begin{matrix} g_0 \\ g_1 \\ g_2 \end{matrix} \right]=
\left[ \begin{matrix} y_0 \\ y_1 \end{matrix} \right]$$
:label:`ch08-equ-conv_matmul_one_dimension`
:eqlabel:`ch08-equ-conv_matmul_one_dimension`
可以观察到,卷积运算转换为矩阵乘法时输入矩阵中存在着重复元素$d_1$和$d_2$,因此,卷积转换的矩阵乘法相对一般的矩阵乘有了优化空间。可以通过计算中间变量$m_0-m_3$得到矩阵乘的结果,见公式 :numref:`ch08-equ-conv-2-winograd`
可以观察到,卷积运算转换为矩阵乘法时输入矩阵中存在着重复元素$d_1$和$d_2$,因此,卷积转换的矩阵乘法相对一般的矩阵乘有了优化空间。可以通过计算中间变量$m_0-m_3$得到矩阵乘的结果,见公式 :eqref:`ch08-equ-conv-2-winograd`
$$F(2, 3)=
\left[ \begin{matrix} d_0 & d_0 & d_2 \\ d_1 & d_2 & d_3 \end{matrix} \right] \left[ \begin{matrix} g_0 \\ g_1 \\ g_2 \end{matrix} \right]=
\left[ \begin{matrix} m_0+m_1+m_2 \\ m_1-m_2+m_3 \end{matrix} \right]$$
:label:`ch08-equ-conv-2-winograd`
:eqlabel:`ch08-equ-conv-2-winograd`
其中,$m_0-m_3$的分别见公式 :numref:`ch08-equ-winograd-param`
其中,$m_0-m_3$的分别见公式 :eqref:`ch08-equ-winograd-param`
$$\begin{aligned}
m_0=(d_0-d_2)*g_0 \\
@@ -147,33 +147,33 @@ m_1=(d_1+d_2)*(\frac{g_0+g_1+g_2}{2}) \\
m_2=(d_0-d_2)*(\frac{g_0-g_1+g_2}{2}) \\
m_2=(d_1-d_3)*g_2
\end{aligned}$$
:label:`ch08-equ-winograd-param`
:eqlabel:`ch08-equ-winograd-param`
通过$m_0-m_3$间接计算r1r2需要的运算次数包括输入d的4次加法输出m的4次乘法和4次加法。在推理阶段权重的数值是常量因此卷积核上的运算可以在图编译阶段计算不计入在线的run时间。所以总的运算次数为4次乘法和8次加法与直接运算的6次乘法和4次加法相比乘法次数减少加法次数增加。在计算机中乘法一般比加法慢通过减少乘法次数增加少量加法可以实现加速。
计算过程写成矩阵形式如公式 :numref:`ch08-equ-winograd-matrix`所示其中⊙为对应位置相乘A、B、G都是常量矩阵。这里写成矩阵计算是为了表达清晰实际使用时按照公式 :numref:`ch08-equ-winograd-param`手写展开的计算速度更快。
计算过程写成矩阵形式如公式 :eqref:`ch08-equ-winograd-matrix`所示其中⊙为对应位置相乘A、B、G都是常量矩阵。这里写成矩阵计算是为了表达清晰实际使用时按照公式 :eqref:`ch08-equ-winograd-param`手写展开的计算速度更快。
$$\mathbf{Y}=\mathbf{A^T}(\mathbf{G}g)*(\mathbf{B^T}d)$$
:label:`ch08-equ-winograd-matrix`
$$\mathbf{B^T}=
\left[ \begin{matrix} 1 & 0 & -1 & 0 \\ 0 & 1 & 1 & 0 \\ 0 & -1 & 1 & 0 \\ 0 & 1 & 0 & -1 \end{matrix} \right]$$
:label:`ch08-equ-winograd-matrix-bt`
:eqlabel:`ch08-equ-winograd-matrix-bt`
$$\mathbf{G}=
\left[ \begin{matrix} 1 & 0 & 0 \\ 0.5 & 0.5 & 0.5 \\ 0.5 & -0.5 & 0.5 \\ 0 & 0 & 1 \end{matrix} \right]$$
:label:`ch08-equ-winograd-matrix-g`
:eqlabel:`ch08-equ-winograd-matrix-g`
$$\mathbf{A^T}=
\left[ \begin{matrix} 1 & 1 & -1 & 0 \\ 0 & 1 & -1 & -1 \end{matrix} \right] \\$$
:label:`ch08-equ-winograd-matrix-at`
:eqlabel:`ch08-equ-winograd-matrix-at`
通常深度学习领域通常使用的都是2D卷积将F(23)扩展到F(2x23x3),可以写成矩阵形式,如公式 :numref:`ch08-equ-winograd-two-dimension-matrix`所示。此时Winograd算法的乘法次数为16而直接卷积的乘法次数为36降低了2.25倍的乘法计算复杂度。
通常深度学习领域通常使用的都是2D卷积将F(23)扩展到F(2x23x3),可以写成矩阵形式,如公式 :eqref:`ch08-equ-winograd-two-dimension-matrix`所示。此时Winograd算法的乘法次数为16而直接卷积的乘法次数为36降低了2.25倍的乘法计算复杂度。
$$\mathbf{Y}=\mathbf{A^T}(\mathbf{G}g\mathbf{G^T})*(\mathbf{B^T}d\mathbf{B})\mathbf{A}$$
:label:`ch08-equ-winograd-two-dimension-matrix`
:eqlabel:`ch08-equ-winograd-two-dimension-matrix`
Winograd算法的整个计算过程在逻辑上可以分为4步如 :numref:`ch08-fig-winograd`所示: