// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %r13
movq 0x400(%rdi), %rax
movslq (%rax), %rax
movslq %esi, %rcx
movq %rax, %xmm0
punpckldq 0x175cc(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x175d4(%rip), %xmm0    # 0x21030
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x17aa8(%rip), %xmm0    # 0x21518
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r14d, %r14d
imulq $0x18, %rcx, %rax
movq %rax, (%rsp)
leaq 0x68(%rsp), %r12
subq $0x1, %r13
jb 0x9d62
movq 0x180(%rbx), %rax
movq (%rsp), %rcx
movq (%rax,%rcx), %rsi
addq %r14, %rsi
leaq 0x38(%rsp), %r15
movq %r15, %rdi
callq 0x1796e
movq 0x308(%rbx), %rdi
addq %r14, %rdi
xorl %esi, %esi
callq 0x17c46
leaq 0x80(%rsp), %rbp
movq (%rax), %rax
movsd (%rax), %xmm0
movq %rbp, %rdi
movq %r15, %rsi
callq 0x182dc
movq %r15, %rdi
movq %rbp, %rsi
callq 0x125b4
movq %rbp, %rdi
callq 0x17a9a
movq 0x2a8(%rbx), %rax
movq (%rax), %rsi
addq %r14, %rsi
movq %rbp, %rdi
callq 0x1796e
movq %r12, %rdi
movq %rbp, %rsi
movsd 0x17a14(%rip), %xmm0    # 0x21528
callq 0x180cc
movq %rbp, %rdi
movq %r12, %rsi
callq 0x125b4
movq %r12, %rdi
callq 0x17a9a
movq %rbp, %rdi
leaq -0x42c(%rip), %rsi      # 0x970a
callq 0x188a2
movq %rbp, %rdi
xorl %esi, %esi
callq 0x17c46
leaq 0x38(%rsp), %r15
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r12, %rdi
movq %r15, %rsi
callq 0x182dc
movq %r15, %rdi
movq %r12, %rsi
callq 0x125b4
movq %r12, %rdi
callq 0x17a9a
movq 0x150(%rbx), %rax
movq (%rsp), %rcx
movq (%rax,%rcx), %rsi
addq %r14, %rsi
movq %r12, %rdi
callq 0x1796e
movq 0x278(%rbx), %rax
movq (%rax), %rdi
addq %r14, %rdi
xorl %esi, %esi
callq 0x17c46
leaq 0x20(%rsp), %r15
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r15, %rdi
movq %r12, %rsi
callq 0x18270
movq %r12, %rdi
movq %r15, %rsi
callq 0x125b4
movq %r15, %rdi
callq 0x17a9a
movq 0x150(%rbx), %rax
movq (%rsp), %rcx
movq (%rax,%rcx), %rdi
addq %r14, %rdi
callq 0x17a6a
movq %rax, %xmm0
movq 0x17435(%rip), %xmm2    # 0x21020
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x17439(%rip), %xmm3    # 0x21030
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
movq %rdx, %xmm4
punpckldq %xmm2, %xmm4    # xmm4 = xmm4[0],xmm2[0],xmm4[1],xmm2[1]
subpd %xmm3, %xmm4
movapd %xmm4, %xmm1
unpckhpd %xmm4, %xmm1            # xmm1 = xmm1[1],xmm4[1]
addsd %xmm4, %xmm1
divsd %xmm1, %xmm0
movq %r15, %rdi
movq %r12, %rsi
movsd %xmm0, 0x18(%rsp)
callq 0x182dc
movq %r12, %rdi
movq %r15, %rsi
callq 0x125b4
movq %r15, %rdi
callq 0x17a9a
movq 0x338(%rbx), %rsi
addq %r14, %rsi
movq %r15, %rdi
callq 0x1796e
movq %r15, %rdi
xorl %esi, %esi
callq 0x17c46
leaq 0x50(%rsp), %r15
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r15, %rdi
movq %r12, %rsi
callq 0x182dc
movq %r12, %rdi
movq %r15, %rsi
callq 0x125b4
movq %r15, %rdi
callq 0x17a9a
movq 0x350(%rbx), %rsi
addq %r14, %rsi
movq %r15, %rdi
callq 0x1796e
movq %r15, %rdi
xorl %esi, %esi
callq 0x17c46
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
leaq 0x38(%rsp), %rsi
movq %r12, %rdx
callq 0x17cc4
movsd 0x18(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x180cc
movq %r15, %rdi
movq %rbp, %rsi
callq 0x125b4
movq %rbp, %rdi
callq 0x17a9a
movq 0x2d8(%rbx), %rdi
addq (%rsp), %rdi
movq %r15, %rsi
callq 0x1139c
movq %r15, %rdi
callq 0x17a9a
leaq 0x50(%rsp), %rdi
callq 0x17a9a
leaq 0x20(%rsp), %rdi
callq 0x17a9a
movq %r12, %rdi
callq 0x17a9a
leaq 0x80(%rsp), %rdi
callq 0x17a9a
leaq 0x38(%rsp), %rdi
callq 0x17a9a
addq $0x18, %r14
jmp 0x9a8a
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9da2
jmp 0x9d8c
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x17a9a
jmp 0x9d8f
jmp 0x9dd6
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x17a9a
jmp 0x9da5
jmp 0x9da2
jmp 0x9dc7
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x17a9a
jmp 0x9dca
jmp 0x9dd6
jmp 0x9dc7
jmp 0x9dd6
jmp 0x9de5
jmp 0x9de5
jmp 0x9e07
jmp 0x9de5
jmp 0x9dd6
jmp 0x9dc7
jmp 0x9dd6
jmp 0x9de5
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x17a9a
jmp 0x9dd9
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x17a9a
jmp 0x9de8
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x17a9a
leaq 0x38(%rsp), %rdi
callq 0x17a9a
movq %rbx, %rdi
callq 0x43b0
movq %rax, %rbx
jmp 0x9df5