// File: /workspace/github_cpp_makefile_star2_O3/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::UpdateWeightsAndBiasGradientsCNN()
void ConstructLayer::UpdateWeightsAndBiasGradientsCNN()
{
    // after finishing one batch, update weights and bias gradient, (CNN layer)
    // cnn layer is different with FC layer. For FC layer, different 
    // neurons have different weights, however, for CNN layer, different
    // neurons in one image share the same weights and bias (kernel).
    // So one need to loop for kernels

    // get 'a' matrix from previous layer for current training sample
    //         !!! cnn->fc connection is allowed, however, 
    //         !!1 fc->cnn connection is not implemented for the moment
    //         !!! so no vectorization or tensorization is needed here
    auto & aVec = __prevLayer -> GetImagesFullA();
    // get 'delta' matrix for current layer
    auto & deltaVec = this -> GetImagesFullDelta();

    // get kernel number
    size_t nKernel = __weightMatrixActive.size();

    // loop for batch
    for(size_t nbatch = 0;nbatch<aVec.size();nbatch++)
    {
        Images &a_image =  aVec[nbatch];
        std::vector<Matrix> &a_matrix = a_image.OutputImageFromKernel;
        Images &d_image = deltaVec[nbatch];
        std::vector<Matrix> &d_matrix = d_image.OutputImageFromKernel;
        if(d_matrix.size() != nKernel) {
            std::cout<<"Error: updateing cnn w gradients, number of kernels not match."<<std::endl;
            exit(0);
        }

        // tmp image for saving weight and bias gradients
        Images tmp_w_gradients;
        Images tmp_b_gradients;

        // loop for kernel
        for(size_t k = 0;k<nKernel;k++)
        {
            // get 'delta' matrix for current kernel
            Matrix &delta = d_matrix[k];

            // update current kernel
            auto dimKernel = __weightMatrixActive[k].Dimension();

            // weight gradient
            Matrix dw(dimKernel);

            for(size_t i=0;i<dimKernel.first;i++)
            {
                for(size_t j=0;j<dimKernel.second;j++)
                {
                    // It is a convolution value with 'a_previous_layer' as the input image and 'delta_this_layer' as the "kernel"
                    // step 1) (i, j) is the w(i, j) of current layer weight matrix -> need to find the corresponding coord in 'a_previous_layer'
                    //size_t i_a_prev = i*__cnnStride; // now __cnnStride is set to 1, and is not adjustable
                    //size_t j_a_prev = j*__cnnStride; // need to derive a detailed expression for __cnnStride > 1
                    // step 2) gradient descent part
                    double _tmp = 0;
                    for(auto &_ap: a_matrix){
                        _tmp += Matrix::GetCorrelationValue(_ap, delta, i, j);
                    }
                    dw[i][j] = _tmp;
                }
            }
            tmp_w_gradients.OutputImageFromKernel.push_back(dw); // push weight gradient for current training sample

            // update bias gradient
            Matrix db(1, 1);
            auto dim = delta.Dimension();
            double b_gradient = delta.SumInSection(0, dim.first, 0, dim.second);
            db[0][0] = b_gradient;
            tmp_b_gradients.OutputImageFromKernel.push_back(db);
        }
        __wGradient.push_back(tmp_w_gradients);
        __bGradient.push_back(tmp_b_gradients);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %rbx
movq 0x3f0(%rdi), %rdi
movq (%rdi), %rax
callq *0xd0(%rax)
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq *0x100(%rax)
movq %rax, 0x68(%rsp)
movq (%r14), %rax
movq %r14, 0x48(%rsp)
movq %rax, 0x8(%rsp)
cmpq %rax, 0x8(%r14)
je 0x1074a
movq 0x10(%rsp), %rax
movq 0xd8(%rax), %rcx
movq 0xe0(%rax), %rdx
movq %rdx, 0x50(%rsp)
movq %rcx, 0x58(%rsp)
subq %rcx, %rdx
movq %rdx, 0x60(%rsp)
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
leaq 0x90(%rax), %rcx
movq %rcx, 0x40(%rsp)
cmpq $0x1, %rdx
adcq $0x0, %rdx
movq %rdx, 0x78(%rsp)
addq $0xa8, %rax
movq %rax, 0x38(%rsp)
leaq 0x90(%rsp), %rbx
xorl %eax, %eax
movq %rax, 0x70(%rsp)
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rax
movq 0x68(%rsp), %rcx
movq (%rcx), %rdx
movq 0x8(%rdx,%rax), %rcx
movq %rdx, 0x18(%rsp)
subq (%rdx,%rax), %rcx
cmpq 0x60(%rsp), %rcx
jne 0x1075c
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x30(%rsp)
movq %rcx, 0xa0(%rsp)
movapd %xmm0, 0x90(%rsp)
movq 0x50(%rsp), %rcx
cmpq 0x58(%rsp), %rcx
je 0x106e2
addq %rax, 0x8(%rsp)
addq %rax, 0x18(%rsp)
xorl %eax, %eax
movq %rax, 0x80(%rsp)
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rdi
movq 0x18(%rsp), %rax
movq (%rax), %r14
addq %rdi, %r14
movq 0x10(%rsp), %rax
addq 0xd8(%rax), %rdi
callq 0x1abb8
movq %rax, %r15
movq %rdx, %r12
leaq 0xa8(%rsp), %rdi
movq %rax, %rsi
callq 0x1a800
testq %r15, %r15
je 0x10643
xorl %ebx, %ebx
movq %r15, 0x88(%rsp)
testq %r12, %r12
je 0x1062f
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
movq (%rax), %rbp
movq 0x8(%rax), %r13
xorpd %xmm0, %xmm0
cmpq %r13, %rbp
je 0x10604
movsd %xmm0, (%rsp)
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x1e0fe
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
addq $0x18, %rbp
cmpq %r13, %rbp
jne 0x105da
jmp 0x10609
movsd %xmm0, (%rsp)
leaq 0xa8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1adb2
movq (%rax), %rax
movsd (%rsp), %xmm0
movsd %xmm0, (%rax,%r15,8)
incq %r15
cmpq %r12, %r15
jne 0x105c0
incq %rbx
movq 0x88(%rsp), %r15
cmpq %r15, %rbx
jne 0x105b8
leaq 0x20(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x138a4
movl $0x1, %esi
movl $0x1, %edx
leaq 0xc0(%rsp), %r15
movq %r15, %rdi
callq 0x1a76e
leaq 0x90(%rsp), %rbx
movq %r14, %rdi
callq 0x1abb8
movq %rdx, %r8
movq %r14, %rdi
xorl %esi, %esi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x1d774
movsd %xmm0, (%rsp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x1adb2
movq (%rax), %rax
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x138a4
movq %r15, %rdi
callq 0x1abf2
leaq 0xa8(%rsp), %rdi
callq 0x1abf2
movq 0x80(%rsp), %rax
incq %rax
cmpq 0x78(%rsp), %rax
jne 0x1055f
movq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x13904
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x13904
movq %rbx, %rdi
callq 0x1353a
leaq 0x20(%rsp), %rdi
callq 0x1353a
movq 0x70(%rsp), %rdx
incq %rdx
movq 0x48(%rsp), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rcx, 0x8(%rsp)
subq %rcx, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
cmpq %rax, %rdx
movq %rdx, %rax
jb 0x104ef
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x26875(%rip), %rdi     # 0x36fd8
leaq 0x185c8(%rip), %rsi     # 0x28d32
callq 0x4260
movq %rax, %rdi
callq 0x4100
xorl %edi, %edi
callq 0x4290
jmp 0x10788
jmp 0x1078d
jmp 0x107a1
jmp 0x107a1
jmp 0x10788
movq %rax, %rbx
jmp 0x107b1
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x1abf2
jmp 0x107a4
jmp 0x107a1
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x1abf2
leaq 0x90(%rsp), %rdi
callq 0x1353a
leaq 0x20(%rsp), %rdi
callq 0x1353a
movq %rbx, %rdi
callq 0x43e0