// File: xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::BatchNormalization_RestoreDelta_ForSample_2DLayer(int)
void ConstructLayer::BatchNormalization_RestoreDelta_ForSample_2DLayer(int sample_id)
{
    // refer to: https://arxiv.org/pdf/1502.03167v3.pdf
    // parallelization is implemented in upper level 

    size_t nK = __n_kernels_cnn;
    double epsilon = 1e-8;
    size_t batch_size = __p_data_interface->GetBatchSize();

    for(size_t k=0;k<nK;k++)
    {
        // part 1
        Matrix partial_C_over_partial_hat_x = __imageDelta[sample_id].OutputImageFromKernel[k];
        partial_C_over_partial_hat_x = partial_C_over_partial_hat_x * (__gamma_BN[k])[0][0];

        Matrix var = __imageVariance_BN[0].OutputImageFromKernel[k];
        var = var + epsilon;
        var(inverse_square_root);

        partial_C_over_partial_hat_x = partial_C_over_partial_hat_x * var[0][0];

        // part 2
        Matrix original_z = __imageZ[sample_id].OutputImageFromKernel[k];
        Matrix &mu = __imageAverage_BN[0].OutputImageFromKernel[k];
        original_z = original_z - mu[0][0];

        auto dim = __imageZ[sample_id].OutputImageFromKernel[k].Dimension();
        original_z = original_z *( 2. / ((double)batch_size) / ((double)dim.first) / ((double)dim.second) );
        Matrix partial_C_over_partial_sigma2 = __v_partial_C_over_partial_sigma_square[k];

        original_z = original_z * partial_C_over_partial_sigma2[0][0];

        // part 3
        Matrix partial_C_over_partial_mu = __v_partial_C_over_partial_mu[k];
        double part3 = partial_C_over_partial_mu[0][0];
        part3 = part3 * ( 2. / ((double)batch_size) / ((double)dim.first) / ((double)dim.second) );

        // result
        Matrix new_delta = partial_C_over_partial_hat_x + original_z;
        new_delta = new_delta + part3;

        __imageDelta_BN[sample_id].OutputImageFromKernel.push_back(new_delta);
    }
}

// Assembly:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x1e0(%rsp)
movl %esi, 0x1dc(%rsp)
movq 0x1e0(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x18(%rax), %rcx
movq %rcx, 0x1d0(%rsp)
movsd 0x42a71(%rip), %xmm0    # 0x52038
movsd %xmm0, 0x1c8(%rsp)
movq 0x400(%rax), %rdi
callq 0x1f310
cltq
movq %rax, 0x1c0(%rsp)
movq $0x0, 0x1b8(%rsp)
movq 0x1b8(%rsp), %rax
cmpq 0x1d0(%rsp), %rax
jae 0xfdc0
movq 0x48(%rsp), %rdi
addq $0x180, %rdi            # imm = 0x180
movslq 0x1dc(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
movq 0x1b8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rsi
leaq 0x1a0(%rsp), %rdi
callq 0x3b810
movq 0x48(%rsp), %rdi
addq $0x308, %rdi            # imm = 0x308
movq 0x1b8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3be50
movq %rax, 0x40(%rsp)
jmp 0xf66d
movq 0x40(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1fd60
movsd (%rax), %xmm0
leaq 0x188(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x3cfd0
jmp 0xf696
leaq 0x1a0(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x1fa20
jmp 0xf6ad
leaq 0x188(%rsp), %rdi
callq 0x3bb80
movq 0x48(%rsp), %rdi
addq $0x2a8, %rdi            # imm = 0x2A8
xorl %eax, %eax
movl %eax, %esi
callq 0x1fd40
movq %rax, %rdi
movq 0x1b8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rsi
leaq 0x160(%rsp), %rdi
callq 0x3b810
jmp 0xf6f1
movsd 0x1c8(%rsp), %xmm0
leaq 0x148(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x3ca80
jmp 0xf711
leaq 0x160(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x1fa20
jmp 0xf728
leaq 0x148(%rsp), %rdi
callq 0x3bb80
leaq -0x7ac(%rip), %rsi      # 0xef90
leaq 0x160(%rsp), %rdi
callq 0x3dc20
jmp 0xf74b
xorl %eax, %eax
movl %eax, %esi
leaq 0x160(%rsp), %rdi
callq 0x3be50
movq %rax, 0x38(%rsp)
jmp 0xf763
movq 0x38(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1fd60
movsd (%rax), %xmm0
leaq 0x130(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x3cfd0
jmp 0xf78c
leaq 0x1a0(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x1fa20
jmp 0xf7a3
leaq 0x130(%rsp), %rdi
callq 0x3bb80
movq 0x48(%rsp), %rdi
addq $0x150, %rdi            # imm = 0x150
movslq 0x1dc(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
movq 0x1b8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rsi
leaq 0x118(%rsp), %rdi
callq 0x3b810
jmp 0xf7eb
movq 0x48(%rsp), %rdi
addq $0x278, %rdi            # imm = 0x278
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x28(%rsp)
callq 0x1fd40
movq %rax, %rdi
movq 0x1b8(%rsp), %rsi
callq 0x1f9e0
movq 0x28(%rsp), %rsi
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rdi
callq 0x3be50
movq %rax, 0x30(%rsp)
jmp 0xf836
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1fd60
movsd (%rax), %xmm0
leaq 0xf8(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x3ce60
jmp 0xf85f
leaq 0x118(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x1fa20
jmp 0xf876
leaq 0xf8(%rsp), %rdi
callq 0x3bb80
movq 0x48(%rsp), %rdi
addq $0x150, %rdi            # imm = 0x150
movslq 0x1dc(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
movq 0x1b8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rdi
callq 0x3bae0
movq %rdx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
jmp 0xf8c0
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0xe8(%rsp)
movq %rax, 0xf0(%rsp)
movaps 0x4277f(%rip), %xmm3    # 0x52060
movq 0x1c0(%rsp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
movapd 0x4277a(%rip), %xmm2    # 0x52070
subpd %xmm2, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movsd 0x4270b(%rip), %xmm0    # 0x52018
divsd %xmm1, %xmm0
movq 0xe8(%rsp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm4
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm4, %xmm1
divsd %xmm1, %xmm0
movq 0xf0(%rsp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
leaq 0xd0(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x3cfd0
jmp 0xf968
leaq 0x118(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x1fa20
jmp 0xf97f
leaq 0xd0(%rsp), %rdi
callq 0x3bb80
movq 0x48(%rsp), %rdi
addq $0x338, %rdi            # imm = 0x338
movq 0x1b8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rsi
leaq 0xb8(%rsp), %rdi
callq 0x3b810
jmp 0xf9b7
xorl %eax, %eax
movl %eax, %esi
leaq 0xb8(%rsp), %rdi
callq 0x3be50
movq %rax, 0x10(%rsp)
jmp 0xf9cf
movq 0x10(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1fd60
movsd (%rax), %xmm0
leaq 0xa0(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x3cfd0
jmp 0xf9f8
leaq 0x118(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1fa20
jmp 0xfa0f
leaq 0xa0(%rsp), %rdi
callq 0x3bb80
movq 0x48(%rsp), %rdi
addq $0x350, %rdi            # imm = 0x350
movq 0x1b8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rsi
leaq 0x88(%rsp), %rdi
callq 0x3b810
jmp 0xfa47
xorl %eax, %eax
movl %eax, %esi
leaq 0x88(%rsp), %rdi
callq 0x3be50
movq %rax, 0x8(%rsp)
jmp 0xfa5f
movq 0x8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1fd60
movsd (%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%rsp), %xmm0
movaps 0x425d6(%rip), %xmm4    # 0x52060
movq 0x1c0(%rsp), %xmm2
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
movapd 0x425d1(%rip), %xmm3    # 0x52070
subpd %xmm3, %xmm2
movaps %xmm2, %xmm1
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
addsd %xmm1, %xmm2
movsd 0x42562(%rip), %xmm1    # 0x52018
divsd %xmm2, %xmm1
movq 0xe8(%rsp), %xmm2
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
subpd %xmm3, %xmm2
movaps %xmm2, %xmm5
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
addsd %xmm5, %xmm2
divsd %xmm2, %xmm1
movq 0xf0(%rsp), %xmm2
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
subpd %xmm3, %xmm2
movaps %xmm2, %xmm3
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
addsd %xmm3, %xmm2
divsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
leaq 0x118(%rsp), %rdx
callq 0x3bf90
jmp 0xfb23
movsd 0x80(%rsp), %xmm0
leaq 0x50(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x3ca80
jmp 0xfb3d
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x1fa20
jmp 0xfb4e
leaq 0x50(%rsp), %rdi
callq 0x3bb80
movq 0x48(%rsp), %rdi
addq $0x2d8, %rdi            # imm = 0x2D8
movslq 0x1dc(%rsp), %rsi
callq 0x1fd40
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
callq 0x1fa50
jmp 0xfb80
leaq 0x68(%rsp), %rdi
callq 0x3bb80
leaq 0x88(%rsp), %rdi
callq 0x3bb80
leaq 0xb8(%rsp), %rdi
callq 0x3bb80
leaq 0x118(%rsp), %rdi
callq 0x3bb80
leaq 0x160(%rsp), %rdi
callq 0x3bb80
leaq 0x1a0(%rsp), %rdi
callq 0x3bb80
movq 0x1b8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x1b8(%rsp)
jmp 0xf5f2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0xfdb1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x3bb80
jmp 0xfdb1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0xfda4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x3bb80
jmp 0xfda4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x3bb80
jmp 0xfda4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0xfd97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x3bb80
jmp 0xfd97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x3bb80
jmp 0xfd97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0xfd8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x3bb80
jmp 0xfd8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0xfd7d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
jmp 0xfd73
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x180(%rsp)
movl %eax, 0x17c(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x3bb80
leaq 0x68(%rsp), %rdi
callq 0x3bb80
leaq 0x88(%rsp), %rdi
callq 0x3bb80
leaq 0xb8(%rsp), %rdi
callq 0x3bb80
leaq 0x118(%rsp), %rdi
callq 0x3bb80
leaq 0x160(%rsp), %rdi
callq 0x3bb80
leaq 0x1a0(%rsp), %rdi
callq 0x3bb80
jmp 0xfdc8
addq $0x1e8, %rsp            # imm = 0x1E8
retq
movq 0x180(%rsp), %rdi
callq 0x43f0
nopw %cs:(%rax,%rax)