// File: /workspace/github_cpp_makefile_star2_O1/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::InitBatchNormalizationParameters()
void ConstructLayer::InitBatchNormalizationParameters()
{
    // only apply for cnn, fc and output layer, no need for pooling and input layers
    if( (GetType() != LayerType::cnn) &&
            ( GetType() != LayerType::fullyConnected) &&
            (GetType() != LayerType::output))
    {
        return; 
    }

    if(GetLayerDimension() == LayerDimension::_1D)
    {
        auto dim = GetOutputImageSize();
        assert(dim.first >= 1);
        assert(dim.second >= 1);

        // for 1D layer, each channel (or feature) has one pair of (\gamma, \beta)
        //   so the matrix dimension equals the output image dimension
        //   and __gamma_BN should have only one matrix
        Matrix tmp1(dim, 1.0); // initialize gamma parameters with 1.0
        __gamma_BN.push_back(tmp1);
        Matrix tmp2(dim, 0);   // initialize beta parameters with 0
        __beta_BN.push_back(tmp2);
    }
    else if(GetLayerDimension() == LayerDimension::_2D)
    {
        size_t n = GetNumberOfKernelsCNN();
        assert(n == __weightMatrix.size());  // one-on-one mapping to vector of kernels
        assert(n >= 1);

        // for 2D layer, each kernel has one pair of (\gamma, \beta)
        //   so the matrix dimension should be (1, 1)
        //   and __gamma_BN should have the same length with number of kernels
        for(size_t i=0;i<n;i++)
        {
            __gamma_BN.push_back(Matrix(1, 1, 1.0)); // initialize gamma with 1.
            __beta_BN.push_back(Matrix(1, 1, 0.0)); // initialize beta with 0.
        }
    }
    else {
        std::cout<<"Error: InitBatchNormalizationParameters(): unsupported layer dimension."<<std::endl;
        exit(0);
    }

    // Above we initilize parameter \gamma with 1. and paramter \beta with 0
    // instead of random normalization
    // ---- this needs to be tested
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq (%rdi), %rax
callq *0x250(%rax)
cmpl $0x1, %eax
je 0x5d57
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x250(%rax)
testl %eax, %eax
je 0x5d57
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x250(%rax)
cmpl $0x4, %eax
jne 0x5eb0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x258(%rax)
movq (%rbx), %rcx
movq %rbx, %rdi
testl %eax, %eax
je 0x5e2d
callq *0x258(%rcx)
cmpl $0x1, %eax
jne 0x5ebc
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x288(%rax)
movq %rax, %r14
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
cmpq %rcx, %r14
jne 0x5ede
testq %r14, %r14
je 0x5efd
leaq 0x308(%rbx), %r15
addq $0x320, %rbx            # imm = 0x320
leaq 0x8(%rsp), %r12
movl $0x1, %esi
movl $0x1, %edx
movq %r12, %rdi
movsd 0x2179d(%rip), %xmm0    # 0x27580
callq 0x1a952
movq %r15, %rdi
movq %r12, %rsi
callq 0x17a00
movq %r12, %rdi
callq 0x1acb6
movl $0x1, %esi
movl $0x1, %edx
xorps %xmm0, %xmm0
movq %r12, %rdi
callq 0x1a952
movq %rbx, %rdi
movq %r12, %rsi
callq 0x17a00
movq %r12, %rdi
callq 0x1acb6
decq %r14
jne 0x5dce
jmp 0x5eb0
callq *0x270(%rcx)
testq %rax, %rax
je 0x5f1c
movq %rdx, %r15
testq %rdx, %rdx
je 0x5f3b
movq %rax, %r14
leaq 0x8(%rsp), %r12
movsd 0x21728(%rip), %xmm0    # 0x27580
movq %r12, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x1a9ec
leaq 0x308(%rbx), %rdi
movq %r12, %rsi
callq 0x13a14
leaq 0x20(%rsp), %rdi
xorps %xmm0, %xmm0
movq %r14, %rsi
movq %r15, %rdx
callq 0x1a9ec
addq $0x320, %rbx            # imm = 0x320
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x13a14
leaq 0x20(%rsp), %rdi
callq 0x1acb6
leaq 0x8(%rsp), %rdi
callq 0x1acb6
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq 0x31115(%rip), %rdi     # 0x36fd8
leaq 0x21ffd(%rip), %rsi     # 0x27ec7
callq 0x4260
movq %rax, %rdi
callq 0x4100
xorl %edi, %edi
callq 0x4290
leaq 0x21fc0(%rip), %rdi     # 0x27ea5
leaq 0x2185e(%rip), %rsi     # 0x2774a
leaq 0x21f6a(%rip), %rcx     # 0x27e5d
movl $0x762, %edx            # imm = 0x762
callq 0x4160
leaq 0x21fbc(%rip), %rdi     # 0x27ec0
leaq 0x2183f(%rip), %rsi     # 0x2774a
leaq 0x21f4b(%rip), %rcx     # 0x27e5d
movl $0x763, %edx            # imm = 0x763
callq 0x4160
leaq 0x21f2b(%rip), %rdi     # 0x27e4e
leaq 0x21820(%rip), %rsi     # 0x2774a
leaq 0x21f2c(%rip), %rcx     # 0x27e5d
movl $0x754, %edx            # imm = 0x754
callq 0x4160
leaq 0x21f53(%rip), %rdi     # 0x27e95
leaq 0x21801(%rip), %rsi     # 0x2774a
leaq 0x21f0d(%rip), %rcx     # 0x27e5d
movl $0x755, %edx            # imm = 0x755
callq 0x4160
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x1acb6
jmp 0x5f72
jmp 0x5f6f
jmp 0x5f6f
jmp 0x5f6f
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x1acb6
movq %rbx, %rdi
callq 0x43e0