// File: 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:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpl $0x1, 0xc(%rax)
je 0x1819d
movq 0x20(%rsp), %rax
cmpl $0x0, 0xc(%rax)
je 0x1819d
movq 0x20(%rsp), %rax
cmpl $0x4, 0xc(%rax)
je 0x1819d
jmp 0x18534
movq 0x20(%rsp), %rdi
movq $0x1, 0xb8(%rsp)
movq (%rdi), %rax
callq *0x258(%rax)
cmpl $0x1, %eax
jne 0x181cd
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0x20(%rsp), %rdi
addq $0x308, %rdi            # imm = 0x308
callq 0x1f9c0
cmpq 0xb8(%rsp), %rax
jne 0x181ea
jmp 0x18209
leaq 0x3b750(%rip), %rdi     # 0x53941
leaq 0x39fda(%rip), %rsi     # 0x521d2
movl $0xe7d, %edx            # imm = 0xE7D
leaq 0x3b755(%rip), %rcx     # 0x53959
callq 0x4170
movq 0x20(%rsp), %rdi
addq $0x308, %rdi            # imm = 0x308
callq 0x1f9c0
movq 0x20(%rsp), %rdi
movq %rax, 0x18(%rsp)
addq $0x368, %rdi            # imm = 0x368
callq 0x1f9c0
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jne 0x1823f
jmp 0x1825e
leaq 0x3b74d(%rip), %rdi     # 0x53993
leaq 0x39f85(%rip), %rsi     # 0x521d2
movl $0xe7e, %edx            # imm = 0xE7E
leaq 0x3b700(%rip), %rcx     # 0x53959
callq 0x4170
movq 0x20(%rsp), %rdi
addq $0x320, %rdi            # imm = 0x320
callq 0x1f9c0
movq 0x20(%rsp), %rdi
movq %rax, 0x10(%rsp)
addq $0x380, %rdi            # imm = 0x380
callq 0x1f9c0
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jne 0x18294
jmp 0x182b3
leaq 0x3b735(%rip), %rdi     # 0x539d0
leaq 0x39f30(%rip), %rsi     # 0x521d2
movl $0xe7f, %edx            # imm = 0xE7F
leaq 0x3b6ab(%rip), %rcx     # 0x53959
callq 0x4170
movq 0x20(%rsp), %rax
movq 0x400(%rax), %rdi
callq 0x1f310
cltq
movq %rax, 0xb0(%rsp)
movq $0x0, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
cmpq 0xb8(%rsp), %rax
jae 0x18534
movq 0x20(%rsp), %rdi
addq $0x368, %rdi            # imm = 0x368
movq 0xa8(%rsp), %rsi
callq 0x1f9e0
movq 0x20(%rsp), %rdi
movq %rax, 0xa0(%rsp)
addq $0x380, %rdi            # imm = 0x380
movq 0xa8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x98(%rsp)
movq 0xa0(%rsp), %rsi
movsd 0xc0(%rax), %xmm0
movaps 0x39d0f(%rip), %xmm2    # 0x52060
movq 0xb0(%rsp), %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x39d0a(%rip), %xmm2    # 0x52070
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
leaq 0x80(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x3cfd0
movq 0x20(%rsp), %rdi
addq $0x308, %rdi            # imm = 0x308
movq 0xa8(%rsp), %rsi
callq 0x1f9e0
movq 0x8(%rsp), %rdx
movq %rax, %rsi
leaq 0x68(%rsp), %rdi
callq 0x3c480
jmp 0x183b8
movq 0x20(%rsp), %rdi
addq $0x308, %rdi            # imm = 0x308
movq 0xa8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
callq 0x1fa20
jmp 0x183e0
leaq 0x68(%rsp), %rdi
callq 0x3bb80
movq 0x20(%rsp), %rax
movq 0x98(%rsp), %rsi
movsd 0xc0(%rax), %xmm0
movaps 0x39c5a(%rip), %xmm2    # 0x52060
movq 0xb0(%rsp), %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0x39c55(%rip), %xmm2    # 0x52070
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
leaq 0x40(%rsp), %rdi
callq 0x3cfd0
jmp 0x1843a
movq 0x20(%rsp), %rdi
addq $0x320, %rdi            # imm = 0x320
movq 0xa8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rsi
leaq 0x28(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x3c480
jmp 0x18467
movq 0x20(%rsp), %rdi
addq $0x320, %rdi            # imm = 0x320
movq 0xa8(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x1fa20
jmp 0x1848f
leaq 0x28(%rsp), %rdi
callq 0x3bb80
leaq 0x40(%rsp), %rdi
callq 0x3bb80
leaq 0x80(%rsp), %rdi
callq 0x3bb80
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0x182da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
jmp 0x18525
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x3bb80
jmp 0x18525
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
jmp 0x1851b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
leaq 0x28(%rsp), %rdi
callq 0x3bb80
leaq 0x40(%rsp), %rdi
callq 0x3bb80
leaq 0x80(%rsp), %rdi
callq 0x3bb80
jmp 0x1853c
addq $0xc8, %rsp
retq
movq 0x60(%rsp), %rdi
callq 0x43f0
nopw %cs:(%rax,%rax)