// File: 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:
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x1a0(%rsp)
movq 0x1a0(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x3f0(%rax), %rdi
movq (%rdi), %rax
callq *0xd0(%rax)
movq 0x60(%rsp), %rdi
movq %rax, 0x198(%rsp)
movq (%rdi), %rax
callq *0x100(%rax)
movq 0x60(%rsp), %rdi
movq %rax, 0x190(%rsp)
addq $0xd8, %rdi
callq 0x1f9c0
movq %rax, 0x188(%rsp)
movq $0x0, 0x180(%rsp)
movq 0x180(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x198(%rsp), %rdi
callq 0x1ff40
movq %rax, %rcx
movq 0x58(%rsp), %rax
cmpq %rcx, %rax
jae 0x19317
movq 0x198(%rsp), %rdi
movq 0x180(%rsp), %rsi
callq 0x1fd40
movq %rax, 0x178(%rsp)
movq 0x178(%rsp), %rax
movq %rax, 0x170(%rsp)
movq 0x190(%rsp), %rdi
movq 0x180(%rsp), %rsi
callq 0x1fd40
movq %rax, 0x168(%rsp)
movq 0x168(%rsp), %rax
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rdi
callq 0x1f9c0
cmpq 0x188(%rsp), %rax
je 0x18ea4
movq 0x52156(%rip), %rdi     # 0x6afd8
leaq 0x3a970(%rip), %rsi     # 0x537f9
callq 0x4230
movq %rax, %rdi
movq 0x52118(%rip), %rsi     # 0x6afb0
callq 0x4260
xorl %edi, %edi
callq 0x4290
leaq 0x148(%rsp), %rdi
callq 0x1fda0
leaq 0x130(%rsp), %rdi
callq 0x1fda0
jmp 0x18ec0
movq $0x0, 0x118(%rsp)
movq 0x118(%rsp), %rax
cmpq 0x188(%rsp), %rax
jae 0x19292
movq 0x160(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1f9e0
movq 0x60(%rsp), %rdi
movq %rax, 0x110(%rsp)
addq $0xd8, %rdi
movq 0x118(%rsp), %rsi
callq 0x1f9e0
movq %rax, %rdi
callq 0x3bae0
movq %rdx, 0x48(%rsp)
movq %rax, 0x50(%rsp)
jmp 0x18f2c
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, 0x100(%rsp)
movq %rax, 0x108(%rsp)
movups 0x100(%rsp), %xmm0
movaps %xmm0, 0xd0(%rsp)
movq 0xd0(%rsp), %rsi
movq 0xd8(%rsp), %rdx
leaq 0xe8(%rsp), %rdi
callq 0x3b4a0
jmp 0x18f75
movq $0x0, 0xc8(%rsp)
movq 0xc8(%rsp), %rax
cmpq 0x100(%rsp), %rax
jae 0x1914f
movq $0x0, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
cmpq 0x108(%rsp), %rax
jae 0x19134
xorps %xmm0, %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0x170(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x1fe00
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x1fe30
movq %rax, 0xa0(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1fe60
testb $0x1, %al
jne 0x1901d
jmp 0x190d4
leaq 0xa8(%rsp), %rdi
callq 0x1fea0
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rdi
movq 0x110(%rsp), %rsi
movq 0xc8(%rsp), %rdx
movq 0xc0(%rsp), %rcx
callq 0x416e0
movsd %xmm0, 0x40(%rsp)
jmp 0x1905f
movsd 0x40(%rsp), %xmm0
addsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x1ff20
jmp 0x18fff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
jmp 0x19308
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
jmp 0x192fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
jmp 0x19283
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0xc8(%rsp), %rsi
leaq 0xe8(%rsp), %rdi
callq 0x3be50
movq %rax, 0x38(%rsp)
jmp 0x190ff
movq 0x38(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x1fd60
movsd 0x30(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0xc0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc0(%rsp)
jmp 0x18fa3
jmp 0x19136
movq 0xc8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc8(%rsp)
jmp 0x18f81
leaq 0x148(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x1fa50
jmp 0x19166
leaq 0x80(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x3b3b0
jmp 0x1917d
movq 0x110(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x20(%rsp)
movq %rax, 0x28(%rsp)
jmp 0x19196
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x70(%rsp)
movq %rax, 0x78(%rsp)
movq 0x110(%rsp), %rdi
movq 0x70(%rsp), %rdx
movq 0x78(%rsp), %r8
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
callq 0x407f0
movsd %xmm0, 0x18(%rsp)
jmp 0x191d0
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0x80(%rsp), %rdi
callq 0x3be50
movq %rax, 0x10(%rsp)
jmp 0x19200
movq 0x10(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1fd60
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x130(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x1fa50
jmp 0x1922f
leaq 0x80(%rsp), %rdi
callq 0x3bb80
leaq 0xe8(%rsp), %rdi
callq 0x3bb80
movq 0x118(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x118(%rsp)
jmp 0x18ecc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x128(%rsp)
movl %eax, 0x124(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x3bb80
leaq 0xe8(%rsp), %rdi
callq 0x3bb80
jmp 0x192fb
movq 0x60(%rsp), %rdi
addq $0x90, %rdi
leaq 0x148(%rsp), %rsi
callq 0x1feb0
jmp 0x192ad
movq 0x60(%rsp), %rdi
addq $0xa8, %rdi
leaq 0x130(%rsp), %rsi
callq 0x1feb0
jmp 0x192c8
leaq 0x130(%rsp), %rdi
callq 0x1fde0
leaq 0x148(%rsp), %rdi
callq 0x1fde0
movq 0x180(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x180(%rsp)
jmp 0x18ddf
leaq 0x130(%rsp), %rdi
callq 0x1fde0
leaq 0x148(%rsp), %rdi
callq 0x1fde0
jmp 0x1931f
addq $0x1a8, %rsp            # imm = 0x1A8
retq
movq 0x128(%rsp), %rdi
callq 0x43f0
nopl (%rax)