// File: /workspace/github_cpp_makefile_star2_O3/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::BatchNormalization_UpdateGammaBeta()
void ConstructLayer::BatchNormalization_UpdateGammaBeta()
{
    // for batch normalization, update beta and gamma during back propagation

    // only apply for cnn, fc and output layers
    if(__type != LayerType::cnn && __type != LayerType::fullyConnected && __type != LayerType::output)
        return;

    size_t nK = 1;
    if(GetLayerDimension() == LayerDimension::_2D)
        nK = __n_kernels_cnn;

    assert(__gamma_BN.size() == nK);
    assert(__gamma_BN.size() == __v_partial_C_over_partial_gamma.size());
    assert(__beta_BN.size() == __v_partial_C_over_partial_beta.size());

    size_t batch_size = __p_data_interface->GetBatchSize();

    for(size_t i=0;i<nK;i++)
    {
        Matrix & gradient_gamma = __v_partial_C_over_partial_gamma[i];
        Matrix & gradient_beta = __v_partial_C_over_partial_beta[i];

        Matrix d_gamma =  gradient_gamma *  (__learningRate/(double)batch_size);
        __gamma_BN[i] = __gamma_BN[i] - d_gamma;
        Matrix d_beta =  gradient_beta * (__learningRate/(double)batch_size);
        __beta_BN[i] = __beta_BN[i] - d_beta;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl 0xc(%rdi), %eax
cmpl $0x4, %eax
ja 0xfe88
movl $0x13, %ecx
btl %eax, %ecx
jae 0xfe88
movq %rdi, %rbp
movq (%rdi), %rax
callq *0x258(%rax)
movl $0x1, %esi
cmpl $0x1, %eax
jne 0xfd0c
movq 0x18(%rbp), %rsi
movq 0x310(%rbp), %rax
subq 0x308(%rbp), %rax
movq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %rsi, %rdx
jne 0xfe97
movq 0x370(%rbp), %rcx
subq 0x368(%rbp), %rcx
cmpq %rcx, %rax
jne 0xfeb6
movq 0x328(%rbp), %rax
subq 0x320(%rbp), %rax
movq 0x388(%rbp), %rcx
subq 0x380(%rbp), %rcx
cmpq %rcx, %rax
jne 0xfed5
testq %rsi, %rsi
je 0xfe88
movq 0x400(%rbp), %rax
movslq (%rax), %rax
movq %rax, %xmm0
punpckldq 0x172ac(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x172b4(%rip), %xmm0    # 0x27050
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
xorl %r14d, %r14d
leaq 0x50(%rsp), %rbx
leaq 0x20(%rsp), %r12
movq %rsi, 0x8(%rsp)
movq 0x368(%rbp), %rsi
addq %r14, %rsi
movq 0x380(%rbp), %r13
addq %r14, %r13
movsd 0xc0(%rbp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %rbx, %rdi
callq 0x1b70c
movq 0x308(%rbp), %rsi
addq %r14, %rsi
movq %r12, %rdi
movq %rbx, %rdx
callq 0x1b0ec
movq 0x308(%rbp), %rdi
addq %r14, %rdi
movq %r12, %rsi
callq 0x14e2c
movq %r12, %rdi
callq 0x1abf2
movsd 0xc0(%rbp), %xmm0
divsd 0x10(%rsp), %xmm0
movq %r12, %rdi
movq %r13, %rsi
callq 0x1b70c
leaq 0x38(%rsp), %r15
movq 0x320(%rbp), %rsi
addq %r14, %rsi
movq %r15, %rdi
movq %r12, %rdx
callq 0x1b0ec
movq 0x320(%rbp), %rdi
addq %r14, %rdi
movq %r15, %rsi
callq 0x14e2c
movq %r15, %rdi
callq 0x1abf2
movq %r12, %rdi
callq 0x1abf2
movq %rbx, %rdi
callq 0x1abf2
addq $0x18, %r14
movq 0x8(%rsp), %rsi
decq %rsi
jne 0xfdbb
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18fdc(%rip), %rdi     # 0x28e7a
leaq 0x178a5(%rip), %rsi     # 0x2774a
leaq 0x18fe6(%rip), %rcx     # 0x28e92
movl $0xe7d, %edx            # imm = 0xE7D
callq 0x4160
leaq 0x1900f(%rip), %rdi     # 0x28ecc
leaq 0x17886(%rip), %rsi     # 0x2774a
leaq 0x18fc7(%rip), %rcx     # 0x28e92
movl $0xe7e, %edx            # imm = 0xE7E
callq 0x4160
leaq 0x1902d(%rip), %rdi     # 0x28f09
leaq 0x17867(%rip), %rsi     # 0x2774a
leaq 0x18fa8(%rip), %rcx     # 0x28e92
movl $0xe7f, %edx            # imm = 0xE7F
callq 0x4160
jmp 0xfef8
jmp 0xfefd
movq %rax, %rbx
jmp 0xff19
movq %rax, %rbx
jmp 0xff0f
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x1abf2
leaq 0x20(%rsp), %rdi
callq 0x1abf2
leaq 0x50(%rsp), %rdi
callq 0x1abf2
movq %rbx, %rdi
callq 0x43e0
nop