// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rdi, %rbx
movq (%rdi), %rax
callq *0x250(%rax)
cmpl $0x1, %eax
je 0x5b23
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x250(%rax)
testl %eax, %eax
je 0x5b23
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x250(%rax)
cmpl $0x4, %eax
jne 0x5c6a
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x258(%rax)
movq (%rbx), %rcx
movq %rbx, %rdi
testl %eax, %eax
je 0x5beb
callq *0x258(%rcx)
cmpl $0x1, %eax
jne 0x5c78
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x288(%rax)
movq %rax, %r14
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
cmpq %rax, %r14
jne 0x5c9a
testq %r14, %r14
je 0x5cb9
leaq 0x308(%rbx), %r15
addq $0x320, %rbx            # imm = 0x320
movq %rsp, %r12
pushq $0x1
popq %r13
subq $0x1, %r14
jb 0x5c6a
movq %r12, %rdi
movq %r13, %rsi
movq %r13, %rdx
movsd 0x1b963(%rip), %xmm0    # 0x21510
callq 0x177ee
movq %r15, %rdi
movq %r12, %rsi
callq 0x14ed2
movq %r12, %rdi
callq 0x17a9a
xorps %xmm0, %xmm0
movq %r12, %rdi
movq %r13, %rsi
movq %r13, %rdx
callq 0x177ee
movq %rbx, %rdi
movq %r12, %rsi
callq 0x14ed2
movq %r12, %rdi
callq 0x17a9a
jmp 0x5b92
callq *0x270(%rcx)
testq %rax, %rax
je 0x5cd8
movq %rdx, %r15
testq %rdx, %rdx
je 0x5cf7
movq %rax, %r14
movq %rsp, %r12
movsd 0x1b8fc(%rip), %xmm0    # 0x21510
movq %r12, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x1787a
leaq 0x308(%rbx), %rdi
movq %r12, %rsi
callq 0x1139c
leaq 0x18(%rsp), %rdi
xorps %xmm0, %xmm0
movq %r14, %rsi
movq %r15, %rdx
callq 0x1787a
addq $0x320, %rbx            # imm = 0x320
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x1139c
leaq 0x18(%rsp), %rdi
callq 0x17a9a
movq %rsp, %rdi
callq 0x17a9a
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0x2d359(%rip), %rdi     # 0x32fd8
leaq 0x1c192(%rip), %rsi     # 0x21e18
callq 0x4240
movq %rax, %rdi
callq 0x4130
xorl %edi, %edi
callq 0x4260
leaq 0x1c155(%rip), %rdi     # 0x21df6
leaq 0x1ba32(%rip), %rsi     # 0x216da
leaq 0x1c0ff(%rip), %rcx     # 0x21dae
movl $0x762, %edx            # imm = 0x762
callq 0x4180
leaq 0x1c151(%rip), %rdi     # 0x21e11
leaq 0x1ba13(%rip), %rsi     # 0x216da
leaq 0x1c0e0(%rip), %rcx     # 0x21dae
movl $0x763, %edx            # imm = 0x763
callq 0x4180
leaq 0x1c0c0(%rip), %rdi     # 0x21d9f
leaq 0x1b9f4(%rip), %rsi     # 0x216da
leaq 0x1c0c1(%rip), %rcx     # 0x21dae
movl $0x754, %edx            # imm = 0x754
callq 0x4180
leaq 0x1c0e8(%rip), %rdi     # 0x21de6
leaq 0x1b9d5(%rip), %rsi     # 0x216da
leaq 0x1c0a2(%rip), %rcx     # 0x21dae
movl $0x755, %edx            # imm = 0x755
callq 0x4180
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x17a9a
jmp 0x5d2e
jmp 0x5d2b
jmp 0x5d2b
jmp 0x5d2b
movq %rax, %rbx
movq %rsp, %rdi
callq 0x17a9a
movq %rbx, %rdi
callq 0x43b0