// File: /workspace/github_cpp_makefile_star2_O1/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 0xa9ae
movq %rdi, %rbx
movq 0x400(%rdi), %rax
movslq (%rax), %rax
movq %rax, %xmm0
punpckldq 0x1c9b2(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1c9ba(%rip), %xmm0    # 0x27050
movslq %esi, %rax
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x1cedb(%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 0x1aafe
movq 0x308(%rbx), %rdi
addq %r14, %rdi
xorl %esi, %esi
callq 0x1ae76
leaq 0x78(%rsp), %r13
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r13, %rdi
movq %r15, %rsi
callq 0x1b7d0
movq %r15, %rbp
movq %r15, %rdi
movq %r13, %rsi
callq 0x14f80
movq %r13, %rdi
callq 0x1acb6
movq 0x2a8(%rbx), %rax
movq (%rax), %rsi
addq %r14, %rsi
movq %r13, %rdi
callq 0x1aafe
movq %r12, %rdi
movq %r13, %rsi
movsd 0x1ce3c(%rip), %xmm0    # 0x27598
callq 0x1b4b6
movq %r13, %rdi
movq %r12, %rsi
callq 0x14f80
movq %r12, %rdi
callq 0x1acb6
movq %r13, %rdi
leaq -0x443(%rip), %rsi      # 0xa33b
callq 0x1bfe6
movq %r13, %rdi
xorl %esi, %esi
callq 0x1ae76
movq %rbp, %r15
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1b7d0
movq %r15, %rdi
movq %r12, %rsi
callq 0x14f80
movq %r12, %rdi
callq 0x1acb6
movq 0x150(%rbx), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rsi
addq %r14, %rsi
movq %r12, %rdi
callq 0x1aafe
movq 0x278(%rbx), %rax
movq (%rax), %rdi
addq %r14, %rdi
xorl %esi, %esi
callq 0x1ae76
leaq 0x30(%rsp), %r15
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r15, %rdi
movq %r12, %rsi
callq 0x1b714
movq %r12, %rdi
movq %r15, %rsi
callq 0x14f80
movq %r15, %rdi
callq 0x1acb6
movq 0x150(%rbx), %rax
movq (%rsp), %rcx
movq (%rax,%rcx,8), %rdi
addq %r14, %rdi
callq 0x1ac7c
movq %rax, %xmm0
movq 0x1c80f(%rip), %xmm2    # 0x27040
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movapd 0x1c813(%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 0x1b7d0
movq %r12, %rdi
movq %r15, %rsi
callq 0x14f80
movq %r15, %rdi
callq 0x1acb6
movq 0x338(%rbx), %rsi
addq %r14, %rsi
movq %r15, %rdi
callq 0x1aafe
movq %r15, %rdi
xorl %esi, %esi
callq 0x1ae76
leaq 0x48(%rsp), %r15
movq (%rax), %rax
movsd (%rax), %xmm0
movq %r15, %rdi
movq %r12, %rsi
callq 0x1b7d0
movq %r12, %rdi
movq %r15, %rsi
callq 0x14f80
movq %r15, %rdi
callq 0x1acb6
movq 0x350(%rbx), %rsi
addq %r14, %rsi
movq %r15, %rdi
callq 0x1aafe
movq %r15, %rdi
xorl %esi, %esi
callq 0x1ae76
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x1aef8
movsd 0x20(%rsp), %xmm0
mulsd 0x18(%rsp), %xmm0
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x1b4b6
movq %r15, %rdi
movq %r13, %rsi
callq 0x14f80
movq %r13, %rdi
callq 0x1acb6
movq 0x2d8(%rbx), %rdi
addq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x13a14
movq %r15, %rdi
callq 0x1acb6
leaq 0x48(%rsp), %rdi
callq 0x1acb6
leaq 0x30(%rsp), %rdi
callq 0x1acb6
movq %r12, %rdi
callq 0x1acb6
leaq 0x78(%rsp), %rdi
callq 0x1acb6
movq %rbp, %r15
movq %rbp, %rdi
callq 0x1acb6
addq $0x18, %r14
movq 0x28(%rsp), %rcx
decq %rcx
jne 0xa6dc
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xaa13
jmp 0xa9c8
jmp 0xaa18
jmp 0xa9c8
movq %rax, %rbx
jmp 0xa9f1
jmp 0xa9e1
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x1acb6
jmp 0xa9e4
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x1acb6
leaq 0x48(%rsp), %rdi
callq 0x1acb6
jmp 0xaa1b
jmp 0xaa36
jmp 0xaa31
jmp 0xaa36
jmp 0xaa13
jmp 0xaa36
jmp 0xaa13
jmp 0xaa36
jmp 0xaa18
jmp 0xaa13
jmp 0xaa13
jmp 0xaa18
movq %rax, %rbx
jmp 0xaa25
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1acb6
leaq 0x60(%rsp), %rdi
callq 0x1acb6
jmp 0xaa39
movq %rax, %rbx
jmp 0xaa43
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x1acb6
leaq 0xc0(%rsp), %rdi
callq 0x1acb6
movq %rbx, %rdi
callq 0x43e0