// File: /workspace/github_cpp_makefile_star2_O2/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 $0xc8, %rsp
movq %rdi, %rbx
movq 0x3f0(%rdi), %rdi
movq (%rdi), %rax
callq *0xd0(%rax)
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x100(%rax)
movq %rax, 0x48(%rsp)
movq 0xe0(%rbx), %rax
subq 0xd8(%rbx), %rax
pushq $0x18
popq %rcx
movq %rax, 0x40(%rsp)
cqto
idivq %rcx
movq %rax, 0x68(%rsp)
leaq 0x90(%rbx), %rax
movq %rax, 0x38(%rsp)
movq %rbx, 0x58(%rsp)
leaq 0xa8(%rbx), %rax
movq %rax, 0x30(%rsp)
xorl %esi, %esi
leaq 0x80(%rsp), %rbx
movq %r14, 0x28(%rsp)
movq (%r14), %rdx
movq 0x8(%r14), %rax
movq %rdx, 0x8(%rsp)
subq %rdx, %rax
cqto
idivq %rcx
cmpq %rax, %rsi
jae 0xee2a
movq %rsi, 0x50(%rsp)
imulq $0x18, %rsi, %rax
movq 0x48(%rsp), %rcx
movq (%rcx), %rdx
movq 0x8(%rdx,%rax), %rcx
subq (%rdx,%rax), %rcx
cmpq 0x40(%rsp), %rcx
jne 0xee3c
addq %rax, 0x8(%rsp)
addq %rax, %rdx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movapd %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
xorl %eax, %eax
movq %rdx, 0x60(%rsp)
cmpq 0x68(%rsp), %rax
je 0xede7
movq %rax, 0x70(%rsp)
imulq $0x18, %rax, %rdi
movq (%rdx), %r15
addq %rdi, %r15
movq 0x58(%rsp), %rax
addq 0xd8(%rax), %rdi
callq 0x17a6a
movq %rax, %r14
movq %rdx, %r13
leaq 0x98(%rsp), %rdi
movq %rax, %rsi
callq 0x1776a
xorl %ebx, %ebx
movq %r14, 0x78(%rsp)
cmpq %r14, %rbx
je 0xed50
xorl %r12d, %r12d
cmpq %r13, %r12
je 0xed46
movq 0x8(%rsp), %rax
movq (%rax), %rbp
movq 0x8(%rax), %r14
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
cmpq %r14, %rbp
je 0xed23
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x1a27c
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
addq $0x18, %rbp
jmp 0xecf9
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0x17c46
movq (%rax), %rax
movsd (%rsp), %xmm0
movsd %xmm0, (%rax,%r12,8)
incq %r12
jmp 0xecdf
incq %rbx
movq 0x78(%rsp), %r14
jmp 0xecd7
leaq 0x10(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x1139c
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
pushq $0x1
popq %rdx
movq %rdx, %rsi
callq 0x176e6
leaq 0x80(%rsp), %rbx
movq %r15, %rdi
callq 0x17a6a
movq %rdx, %r8
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x19b90
movsd %xmm0, (%rsp)
movq %r14, %rdi
xorl %esi, %esi
callq 0x17c46
movq (%rax), %rax
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1139c
movq %r14, %rdi
callq 0x17a9a
leaq 0x98(%rsp), %rdi
callq 0x17a9a
movq 0x70(%rsp), %rax
incq %rax
movq 0x60(%rsp), %rdx
jmp 0xec8f
movq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x11400
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x11400
movq %rbx, %rdi
callq 0x110e4
leaq 0x10(%rsp), %rdi
callq 0x110e4
movq 0x50(%rsp), %rsi
incq %rsi
movq 0x28(%rsp), %r14
pushq $0x18
popq %rcx
jmp 0xec1c
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x24195(%rip), %rdi     # 0x32fd8
leaq 0x13e78(%rip), %rsi     # 0x22cc2
callq 0x4240
movq %rax, %rdi
callq 0x4130
xorl %edi, %edi
callq 0x4260
jmp 0xee68
jmp 0xee6d
jmp 0xee81
jmp 0xee81
jmp 0xee68
movq %rax, %rbx
jmp 0xee91
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x17a9a
jmp 0xee84
jmp 0xee81
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x17a9a
leaq 0x80(%rsp), %rdi
callq 0x110e4
leaq 0x10(%rsp), %rdi
callq 0x110e4
movq %rbx, %rdi
callq 0x43b0