// File: 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:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x100(%rsp)
movq 0x100(%rsp), %rdi
movq %rdi, 0x38(%rsp)
movq (%rdi), %rax
callq *0x250(%rax)
cmpl $0x1, %eax
je 0x7425
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
callq *0x250(%rax)
cmpl $0x0, %eax
je 0x7425
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
callq *0x250(%rax)
cmpl $0x4, %eax
je 0x7425
jmp 0x777a
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
callq *0x258(%rax)
cmpl $0x0, %eax
jne 0x75b9
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
callq *0x270(%rax)
movq %rax, 0xf0(%rsp)
movq %rdx, 0xf8(%rsp)
cmpq $0x1, 0xf0(%rsp)
jb 0x7467
jmp 0x7486
leaq 0x4b468(%rip), %rdi     # 0x528d6
leaq 0x4ad5d(%rip), %rsi     # 0x521d2
movl $0x754, %edx            # imm = 0x754
leaq 0x4b464(%rip), %rcx     # 0x528e5
callq 0x4170
cmpq $0x1, 0xf8(%rsp)
jb 0x7493
jmp 0x74b2
leaq 0x4b483(%rip), %rdi     # 0x5291d
leaq 0x4ad31(%rip), %rsi     # 0x521d2
movl $0x755, %edx            # imm = 0x755
leaq 0x4b438(%rip), %rcx     # 0x528e5
callq 0x4170
movups 0xf0(%rsp), %xmm0
movaps %xmm0, 0xc0(%rsp)
movq 0xc0(%rsp), %rsi
movq 0xc8(%rsp), %rdx
leaq 0xd8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movsd 0x4ab21(%rip), %xmm0    # 0x52008
callq 0x3b680
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
addq $0x308, %rdi            # imm = 0x308
callq 0x1fa50
jmp 0x7504
movups 0xf0(%rsp), %xmm0
movaps %xmm0, 0x80(%rsp)
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x98(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x3b680
jmp 0x7536
movq 0x38(%rsp), %rdi
addq $0x320, %rdi            # imm = 0x320
leaq 0x98(%rsp), %rsi
callq 0x1fa50
jmp 0x7551
leaq 0x98(%rsp), %rdi
callq 0x3bb80
leaq 0xd8(%rsp), %rdi
callq 0x3bb80
jmp 0x777a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x75a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x3bb80
leaq 0xd8(%rsp), %rdi
callq 0x3bb80
jmp 0x7782
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
callq *0x258(%rax)
cmpl $0x1, %eax
jne 0x774f
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
callq *0x288(%rax)
movq 0x38(%rsp), %rdi
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
addq $0x60, %rdi
callq 0x1f9c0
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jne 0x760a
jmp 0x7629
leaq 0x4b31c(%rip), %rdi     # 0x5292d
leaq 0x4abba(%rip), %rsi     # 0x521d2
movl $0x762, %edx            # imm = 0x762
leaq 0x4b2c1(%rip), %rcx     # 0x528e5
callq 0x4170
cmpq $0x1, 0x78(%rsp)
jb 0x7633
jmp 0x7652
leaq 0x4b30e(%rip), %rdi     # 0x52948
leaq 0x4ab91(%rip), %rsi     # 0x521d2
movl $0x763, %edx            # imm = 0x763
leaq 0x4b298(%rip), %rcx     # 0x528e5
callq 0x4170
movq $0x0, 0x70(%rsp)
movq 0x70(%rsp), %rax
cmpq 0x78(%rsp), %rax
jae 0x774d
movq 0x38(%rsp), %rax
addq $0x308, %rax            # imm = 0x308
movq %rax, 0x18(%rsp)
leaq 0x58(%rsp), %rdi
movq %rdi, 0x20(%rsp)
movl $0x1, %edx
movsd 0x4a976(%rip), %xmm0    # 0x52008
movq %rdx, %rsi
callq 0x3b590
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x1ffa0
jmp 0x76ab
leaq 0x58(%rsp), %rdi
callq 0x3bb80
movq 0x38(%rsp), %rax
addq $0x320, %rax            # imm = 0x320
movq %rax, 0x8(%rsp)
leaq 0x40(%rsp), %rdi
movq %rdi, 0x10(%rsp)
movl $0x1, %edx
xorps %xmm0, %xmm0
movq %rdx, %rsi
callq 0x3b590
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x1ffa0
jmp 0x76f0
leaq 0x40(%rsp), %rdi
callq 0x3bb80
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x765b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x3bb80
jmp 0x7782
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x3bb80
jmp 0x7782
jmp 0x7778
movq 0x63882(%rip), %rdi     # 0x6afd8
leaq 0x4b1f2(%rip), %rsi     # 0x5294f
callq 0x4230
movq %rax, %rdi
movq 0x63844(%rip), %rsi     # 0x6afb0
callq 0x4260
xorl %edi, %edi
callq 0x4290
jmp 0x777a
addq $0x108, %rsp            # imm = 0x108
retq
movq 0xb8(%rsp), %rdi
callq 0x43f0
nop