// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
movq 0x18(%rdi), %rcx
testq %rcx, %rcx
je 0xa94e
movq %rdi, %rbx
movq 0x400(%rdi), %rax
movslq (%rax), %rax
movq %rax, %xmm0
punpckldq 0x1ca12(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1ca1a(%rip), %xmm0    # 0x27050
movslq %esi, %rax
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x1cf3b(%rip), %xmm0    # 0x27588
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
leaq (%rax,%rax,2), %rax
leaq 0xc0(%rsp), %r15
leaq 0x60(%rsp), %r12
movq %rax, (%rsp)
leaq (,%rax,8), %rax
movq %rax, 0x8(%rsp)
movq %rcx, 0x28(%rsp)
movq 0x180(%rbx), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rsi
addq %r14, %rsi
movq %r15, %rdi
callq 0x1aa3e
movq 0x308(%rbx), %rdi
addq %r14, %rdi
xorl %esi, %esi
callq 0x1adb2
leaq 0x78(%rsp), %r13
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r13, %rdi
movq %r15, %rsi
callq 0x1b70c
movq %r15, %rbp
movq %r15, %rdi
movq %r13, %rsi
callq 0x14e2c
movq %r13, %rdi
callq 0x1abf2
movq 0x2a8(%rbx), %rax
movq (%rax), %rsi
addq %r14, %rsi
movq %r13, %rdi
callq 0x1aa3e
movq %r12, %rdi
movq %r13, %rsi
movsd 0x1ce9c(%rip), %xmm0    # 0x27598
callq 0x1b3f2
movq %r13, %rdi
movq %r12, %rsi
callq 0x14e2c
movq %r12, %rdi
callq 0x1abf2
movq %r13, %rdi
leaq -0x443(%rip), %rsi      # 0xa2db
callq 0x1bf18
movq %r13, %rdi
xorl %esi, %esi
callq 0x1adb2
movq %rbp, %r15
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1b70c
movq %r15, %rdi
movq %r12, %rsi
callq 0x14e2c
movq %r12, %rdi
callq 0x1abf2
movq 0x150(%rbx), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rsi
addq %r14, %rsi
movq %r12, %rdi
callq 0x1aa3e
movq 0x278(%rbx), %rax
movq (%rax), %rdi
addq %r14, %rdi
xorl %esi, %esi
callq 0x1adb2
leaq 0x30(%rsp), %r15
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r15, %rdi
movq %r12, %rsi
callq 0x1b650
movq %r12, %rdi
movq %r15, %rsi
callq 0x14e2c
movq %r15, %rdi
callq 0x1abf2
movq 0x150(%rbx), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rdi
addq %r14, %rdi
callq 0x1abb8
movq %rax, %xmm0
movq 0x1c86f(%rip), %xmm2    # 0x27040
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x1c873(%rip), %xmm3    # 0x27050
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x10(%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, 0x20(%rsp)
callq 0x1b70c
movq %r12, %rdi
movq %r15, %rsi
callq 0x14e2c
movq %r15, %rdi
callq 0x1abf2
movq 0x338(%rbx), %rsi
addq %r14, %rsi
movq %r15, %rdi
callq 0x1aa3e
movq %r15, %rdi
xorl %esi, %esi
callq 0x1adb2
leaq 0x48(%rsp), %r15
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r15, %rdi
movq %r12, %rsi
callq 0x1b70c
movq %r12, %rdi
movq %r15, %rsi
callq 0x14e2c
movq %r15, %rdi
callq 0x1abf2
movq 0x350(%rbx), %rsi
addq %r14, %rsi
movq %r15, %rdi
callq 0x1aa3e
movq %r15, %rdi
xorl %esi, %esi
callq 0x1adb2
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x1ae34
movsd 0x20(%rsp), %xmm0
mulsd 0x18(%rsp), %xmm0
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x1b3f2
movq %r15, %rdi
movq %r13, %rsi
callq 0x14e2c
movq %r13, %rdi
callq 0x1abf2
movq 0x2d8(%rbx), %rdi
addq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x138a4
movq %r15, %rdi
callq 0x1abf2
leaq 0x48(%rsp), %rdi
callq 0x1abf2
leaq 0x30(%rsp), %rdi
callq 0x1abf2
movq %r12, %rdi
callq 0x1abf2
leaq 0x78(%rsp), %rdi
callq 0x1abf2
movq %rbp, %r15
movq %rbp, %rdi
callq 0x1abf2
addq $0x18, %r14
movq 0x28(%rsp), %rcx
decq %rcx
jne 0xa67c
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa9b3
jmp 0xa968
jmp 0xa9b8
jmp 0xa968
movq %rax, %rbx
jmp 0xa991
jmp 0xa981
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x1abf2
jmp 0xa984
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x1abf2
leaq 0x48(%rsp), %rdi
callq 0x1abf2
jmp 0xa9bb
jmp 0xa9d6
jmp 0xa9d1
jmp 0xa9d6
jmp 0xa9b3
jmp 0xa9d6
jmp 0xa9b3
jmp 0xa9d6
jmp 0xa9b8
jmp 0xa9b3
jmp 0xa9b3
jmp 0xa9b8
movq %rax, %rbx
jmp 0xa9c5
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1abf2
leaq 0x60(%rsp), %rdi
callq 0x1abf2
jmp 0xa9d9
movq %rax, %rbx
jmp 0xa9e3
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x1abf2
leaq 0xc0(%rsp), %rdi
callq 0x1abf2
movq %rbx, %rdi
callq 0x43e0