// File: /workspace/github_cpp_makefile_star2_O2/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::BatchNormalization_UpdateZ_1DLayer()
void ConstructLayer::BatchNormalization_UpdateZ_1DLayer()
{
    // 1) get mu and sigma_square
    // part I) active
    Images mu, sigma_square;
    GetMu_BFor1DLayer(__imageZ, mu); // use active z image
    GetSigmaSquareFor1DLayer(__imageZ, mu, sigma_square);
    // part II) full
    Images mu_full, sigma_square_full;
    GetMu_BFor1DLayer(__imageZFull, mu_full);
    GetSigmaSquareFor1DLayer(__imageZFull, mu_full, sigma_square_full);

    // 2) save mu and sigma_square ---- Do I need to calculate both active and full? This is time consuming.
    // part I) active
    __imageAverage_BN[0] = mu;
    __imageVariance_BN[0] = sigma_square;
    // part II) full 
    __imageAverageFull_BN[0] = mu_full;
    __imageVarianceFull_BN[0] = sigma_square_full;

    // 3) get normalized \hat{x} and save it to __imageZ_BN
    size_t batch_size = __imageZ.size();
    assert(__imageZ[0].OutputImageFromKernel.size() == 1); // make sure 1D layer
    double epsilon = 1e-8; // avoid divide by zero issue; a hard-coded parameter from algorithm

    // part I) active mu and sigma matrix
    Matrix & average_m_active = mu.OutputImageFromKernel[0];
    Matrix variance_m_active = sigma_square.OutputImageFromKernel[0];
    variance_m_active = variance_m_active + epsilon;
    variance_m_active(sqrt);
    // part II) full mu and sigma matrix
    Matrix & average_m_full = mu_full.OutputImageFromKernel[0];
    Matrix variance_m_full = sigma_square_full.OutputImageFromKernel[0];
    variance_m_full = variance_m_full + epsilon;
    variance_m_full(sqrt);
    /*
    for(size_t i=0;i<batch_size;i++) // TO-DO: need to parallize
    {
        // part I) for active image
        Matrix m = __imageZ[i].OutputImageFromKernel[0]; // using copy assigment
        m = m - average_m_active;
        m = m / variance_m_active;
        Images tmp;
        tmp.OutputImageFromKernel.push_back(m);
        __imageZ_BN[i]=tmp;


        // part II) for full image
        Matrix m_full = __imageZFull[i].OutputImageFromKernel[0]; // using copy assigment
        m_full = m_full - average_m_full;
        m_full = m_full / variance_m_full;
        Images tmp_full;
        tmp_full.OutputImageFromKernel.push_back(m_full);
        __imageZFull_BN[i]=tmp_full;
    }
    */
    // parallelize
    auto fill_z = [&](size_t start, size_t end) 
    {
        for(size_t i=start;i<end;i++)
        {
            // part I) for active image
            Matrix m = __imageZ[i].OutputImageFromKernel[0]; // using copy assigment
            m = m - average_m_active;
            m = m / variance_m_active;
            Images tmp;
            tmp.OutputImageFromKernel.push_back(m);
            __imageZ_BN[i]=tmp;


            // part II) for full image
            Matrix m_full = __imageZFull[i].OutputImageFromKernel[0]; // using copy assigment
            m_full = m_full - average_m_full;
            m_full = m_full / variance_m_full;
            Images tmp_full;
            tmp_full.OutputImageFromKernel.push_back(m_full);
            __imageZFull_BN[i]=tmp_full;
        }
    };

#ifdef MULTI_THREAD
    std::vector<std::thread> vth;
    int Range[NTHREAD+1];
    for(int i=0;i<NTHREAD;i++)
        Range[i] = batch_size/NTHREAD * i;
    Range[NTHREAD] = batch_size;

    for(size_t n=0;n<NTHREAD;n++)
        vth.push_back(std::thread(fill_z, Range[n], Range[n+1]));

    for(auto &th: vth)
        th.join();
#else
    fill_z(0, batch_size);
#endif

    // 4) affine transformation to get y
    assert(__gamma_BN.size() == 1); // for 1D layer, size = 1; for 2D layer, size = nKernel
    assert(__beta_BN.size() == 1); 
    /*
    for(size_t i=0;i<batch_size;i++) // TO-DO: need to parallize
    {
        // part I) active
        Matrix m = __imageZ_BN[i].OutputImageFromKernel[0] ^ __gamma_BN[0];
        m = m + __beta_BN[0];
        Images tmp;
        tmp.OutputImageFromKernel.push_back(m);
        __imageY_BN[i]=tmp;

        // part II) full
        m = __imageZFull_BN[i].OutputImageFromKernel[0] ^ __gamma_BN[0];
        m = m + __beta_BN[0];
        tmp.OutputImageFromKernel.clear();
        tmp.OutputImageFromKernel.push_back(m);
        __imageYFull_BN[i]=tmp;
    }
    */
    // parallelize
    auto fill_y = [&](size_t start, size_t end)
    {
        for(size_t i=start;i<end;i++)
        {
            // part I) active
            Matrix m = __imageZ_BN[i].OutputImageFromKernel[0] ^ __gamma_BN[0];
            m = m + __beta_BN[0];
            Images tmp;
            tmp.OutputImageFromKernel.push_back(m);
            __imageY_BN[i]=tmp;

            // part II) full
            m = __imageZFull_BN[i].OutputImageFromKernel[0] ^ __gamma_BN[0];
            m = m + __beta_BN[0];
            tmp.OutputImageFromKernel.clear();
            tmp.OutputImageFromKernel.push_back(m);
            __imageYFull_BN[i]=tmp;
        }
    };

#ifdef MULTI_THREAD
    vth.clear();
    for(size_t n=0;n<NTHREAD;n++)
    {
        vth.push_back(std::thread(fill_y, Range[n], Range[n+1]));
    }
    for(auto &th: vth)
        th.join();
#else
    fill_y(0, batch_size);
#endif
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdi, %rbx
leaq 0xf0(%rsp), %rdx
andq $0x0, 0x10(%rdx)
pxor %xmm0, %xmm0
andq $0x0, 0x90(%rsp)
movdqa %xmm0, (%rdx)
movdqa %xmm0, 0x80(%rsp)
leaq 0x150(%rdi), %r14
movq (%rdi), %rax
movq %r14, %rsi
callq *0x1a8(%rax)
movq (%rbx), %rax
leaq 0xf0(%rsp), %rdx
leaq 0x80(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq *0x1b8(%rax)
leaq 0xd0(%rsp), %rdx
andq $0x0, 0x10(%rdx)
pxor %xmm0, %xmm0
andq $0x0, 0x70(%rsp)
movdqa %xmm0, (%rdx)
movdqa %xmm0, 0x60(%rsp)
leaq 0x1b0(%rbx), %r14
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
callq *0x1a8(%rax)
movq (%rbx), %rax
leaq 0xd0(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq *0x1b8(%rax)
movq 0x278(%rbx), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x11a9e
movq 0x2a8(%rbx), %rdi
leaq 0x80(%rsp), %rsi
callq 0x11a9e
movq 0x290(%rbx), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x11a9e
movq 0x2c0(%rbx), %rdi
leaq 0x60(%rsp), %rsi
callq 0x11a9e
movq 0x150(%rbx), %rcx
movq 0x158(%rbx), %rax
subq %rcx, %rax
pushq $0x18
popq %rsi
cqto
idivq %rsi
movq %rax, %r15
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cmpq $0x18, %rax
jne 0xb4c5
movq 0xf0(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x80(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x1796e
leaq 0x30(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movq 0x1632c(%rip), %xmm0    # 0x21528
callq 0x180cc
leaq 0xb8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x125b4
leaq 0x30(%rsp), %rdi
callq 0x17a9a
movq 0x27dac(%rip), %rsi     # 0x32fd0
leaq 0xb8(%rsp), %rdi
callq 0x188a2
movq 0xd0(%rsp), %r12
movq 0x60(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x1796e
leaq 0x30(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movq 0x162c8(%rip), %xmm0    # 0x21528
callq 0x180cc
leaq 0xa0(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x125b4
leaq 0x30(%rsp), %rdi
callq 0x17a9a
movq 0x27d48(%rip), %rsi     # 0x32fd0
leaq 0xa0(%rsp), %rdi
callq 0x188a2
pxor %xmm0, %xmm0
movdqa %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq %r15, %rax
shrq $0x3, %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq $0x8, %rdx
je 0xb2c1
movl %ecx, 0x30(%rsp,%rdx,4)
incq %rdx
addl %eax, %ecx
jmp 0xb2b0
movl %r15d, 0x50(%rsp)
xorl %r15d, %r15d
leaq 0x8(%rsp), %r13
movq %rsp, %rbp
leaq 0x10(%rsp), %r14
cmpq $0x8, %r15
je 0xb370
andq $0x0, 0x8(%rsp)
pushq $0x38
popq %rdi
callq 0x4250
leaq 0x276bb(%rip), %rcx     # 0x329b0
movq %rcx, (%rax)
movq 0x30(%rsp,%r15,4), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x8(%rax)
movq %rbx, 0x10(%rax)
movq 0x110(%rsp), %rcx
movq %rcx, 0x18(%rax)
leaq 0xb8(%rsp), %rcx
movq %rcx, 0x20(%rax)
movq %r12, 0x28(%rax)
leaq 0xa0(%rsp), %rcx
movq %rcx, 0x30(%rax)
movq %rax, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x4200
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb355
movq (%rdi), %rax
callq *0x8(%rax)
movq %r14, %rdi
movq %r13, %rsi
callq 0x1470a
movq %r13, %rdi
callq 0x113c8
incq %r15
jmp 0xb2d6
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r15
cmpq %r15, %r14
je 0xb38d
movq %r14, %rdi
callq 0x4390
addq $0x8, %r14
jmp 0xb37a
movq 0x310(%rbx), %rax
subq 0x308(%rbx), %rax
cmpq $0x18, %rax
jne 0xb4e4
movq 0x328(%rbx), %rax
subq 0x320(%rbx), %rax
cmpq $0x18, %rax
jne 0xb503
leaq 0x10(%rsp), %rdi
callq 0x113d6
xorl %ebp, %ebp
pushq $0x18
popq %r15
leaq 0x2761c(%rip), %r14     # 0x329f0
leaq 0x8(%rsp), %r12
movq %rsp, %r13
cmpq $0x8, %rbp
je 0xb441
andq $0x0, 0x8(%rsp)
movq %r15, %rdi
callq 0x4250
movq %r14, (%rax)
movq 0x30(%rsp,%rbp,4), %xmm0
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x8(%rax)
movq %rbx, 0x10(%rax)
movq %rax, (%rsp)
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x4200
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb427
movq (%rdi), %rax
callq *0x8(%rax)
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x1470a
movq %r12, %rdi
callq 0x113c8
incq %rbp
jmp 0xb3dc
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0xb45e
movq %rbx, %rdi
callq 0x4390
addq $0x8, %rbx
jmp 0xb44b
leaq 0x10(%rsp), %rdi
callq 0x113de
leaq 0xa0(%rsp), %rdi
callq 0x17a9a
leaq 0xb8(%rsp), %rdi
callq 0x17a9a
leaq 0x60(%rsp), %rdi
callq 0x110e4
leaq 0xd0(%rsp), %rdi
callq 0x110e4
leaq 0x80(%rsp), %rdi
callq 0x110e4
leaq 0xf0(%rsp), %rdi
callq 0x110e4
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x16b4f(%rip), %rdi     # 0x2201b
leaq 0x16207(%rip), %rsi     # 0x216da
leaq 0x16b6f(%rip), %rcx     # 0x22049
movl $0x801, %edx            # imm = 0x801
callq 0x4180
leaq 0x16ba0(%rip), %rdi     # 0x2208b
leaq 0x161e8(%rip), %rsi     # 0x216da
leaq 0x16b50(%rip), %rcx     # 0x22049
movl $0x84c, %edx            # imm = 0x84C
callq 0x4180
leaq 0x16b98(%rip), %rdi     # 0x220a2
leaq 0x161c9(%rip), %rsi     # 0x216da
leaq 0x16b31(%rip), %rcx     # 0x22049
movl $0x84d, %edx            # imm = 0x84D
callq 0x4180
jmp 0xb533
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x17a9a
jmp 0xb591
movq %rax, %rbx
jmp 0xb591
jmp 0xb546
jmp 0xb546
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0xb599
movq %rax, %rbx
jmp 0xb59e
jmp 0xb552
movq %rax, %rbx
jmp 0xb5c2
movq %rax, %rbx
jmp 0xb5ab
jmp 0xb561
jmp 0xb561
jmp 0xb566
jmp 0xb57a
jmp 0xb561
movq %rax, %rbx
jmp 0xb587
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xb587
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xb587
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x113c8
leaq 0x10(%rsp), %rdi
callq 0x113de
leaq 0xa0(%rsp), %rdi
callq 0x17a9a
leaq 0xb8(%rsp), %rdi
callq 0x17a9a
leaq 0x60(%rsp), %rdi
callq 0x110e4
leaq 0xd0(%rsp), %rdi
callq 0x110e4
leaq 0x80(%rsp), %rdi
callq 0x110e4
leaq 0xf0(%rsp), %rdi
callq 0x110e4
movq %rbx, %rdi
callq 0x43b0