// File: xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::UpdateWeightsAndBiasGradientsFC()
void ConstructLayer::UpdateWeightsAndBiasGradientsFC()
{
    //cout<<__func__<<" started."<<endl;
    // after finishing one batch, update weights and bias, for FC layer
    // get output from previous layer for current training sample
    std::vector<Images> a_images;
    if(/*__prevLayer->GetType() != LayerType::input && */__prevLayer->GetType() != LayerType::fullyConnected) // input layer has no W&B
    {
        // if previous layer is not an input/fullyConnected layer, then Vectorization operation is needed
        //      this is for cnn->fc or pooling->fc
        auto & _tmp = __prevLayer->GetImagesFullA(); // 'a' images from previous layer
        for(auto &i: _tmp)
            a_images.push_back(i.Vectorization());
    }
    else
    {
        // if previous layer is input/fc, no other operation is needed
        a_images = __prevLayer->GetImagesFullA(); // 'a' images from previous layer
    }
    auto & d_images = this->GetImagesFullDelta(); // delta images from current layer
    // NOTE: 'a' and 'delta' include value correpsonds to disabled neurons
    //       it's just these values have been set to 0 when updating __imagesA, __imagesDelta
    //       see Functions UpdateImagesA() and UpdateImagesDelta()
    //       since dC/dw_ij = a^{l-1}_j * delta^l_k, so if a = 0 or delta = 0, then dC/dw = 0, namely no change on w for inactive neurons
    if(a_images.size() != d_images.size()) {
        std::cout<<__func__<<" Error: batch size not equal..."<<std::endl;
        exit(0);
    }
    if(a_images.back().OutputImageFromKernel.size() != 1 ) {
        std::cout<<__func__<<" Error: updating weights and bias gradients for FC, dimension not match."<<std::endl;
        std::cout<<"                  kernel size: "<<a_images.back().OutputImageFromKernel.size()<<std::endl;
        exit(0);
    }

    // so directly work on __weightMatrix, not __weightMatrixActive
    // check layer type
    if(__weightMatrix.size() != 1) {
        std::cout<<__func__<<" Error: more than 1 weight matrix exist in fully connected layer."<<std::endl;
        exit(0);
    }

    //cout<<"sample size: "<<d_images.size()<<endl;
    //for(auto &i: d_images)
    //{
    //    cout<<i.OutputImageFromKernel[0].Dimension()<<endl;
    //    cout<<i.OutputImageFromKernel[0]<<endl;
    //}
    // loop for samples
    for(size_t i=0;i<a_images.size();i++)
    {
        Matrix & a_matrix = a_images[i].OutputImageFromKernel[0]; // 'a' image from previous layer
        Matrix & d_matrix = d_images[i].OutputImageFromKernel[0]; // 'd' image from current layer
        //cout<<"Layer id: "<<GetID()<<endl;
        //cout<<"a matrix: "<<endl<<a_matrix<<endl;
        //cout<<"delta matrix: "<<endl<<d_matrix<<endl;

        auto d1 = (__weightMatrix[0]).Dimension(), d2 = a_matrix.Dimension(), d3 = d_matrix.Dimension();
        if(d1.first != d3.first || d1.second != d2.first)
        {
            std::cout<<__func__<<"Error: updating weights and bias gradients for FC, dimension not match."<<std::endl;
            std::cout<<"          current layer 'w' matrix dimension: "<<d1<<std::endl;
            std::cout<<"          previous layer 'a' matrix dimension: "<<d2<<std::endl;
            std::cout<<"          current layer 'delta' matrix dimension: "<<d3<<std::endl;
            exit(0);
        }

        //cout<<"sample_id: "<<i<<endl;
        Matrix a_T = a_matrix.Transpose();
        Matrix dw = d_matrix * a_T;

        //cout<<a_T.Dimension()<<endl;
        //cout<<"a :"<<endl<<a_T<<endl;
        //cout<<d_matrix.Dimension()<<endl;
        //cout<<"d :"<<endl<<d_matrix<<endl;
        //cout<<"dw: "<<endl<<dw<<endl;
        //cout<<"dw matrix."<<endl;
        //getchar();

        // push bias gradient for current training sample
        Images tmp_w_gradients;
        tmp_w_gradients.OutputImageFromKernel.push_back(dw);
        __wGradient.push_back(tmp_w_gradients); // push weight gradient for current training sample

        Images tmp_b_gradients;
        tmp_b_gradients.OutputImageFromKernel.push_back(d_matrix);
        __bGradient.push_back(tmp_b_gradients); // bias gradient equals delta
    }

    //cout<<"INFO: "<<__func__<<" finished."<<endl;
}

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0x210(%rsp)
movq 0x210(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x1f060
movq 0xe8(%rsp), %rax
movq 0x3f0(%rax), %rdi
movq (%rdi), %rax
movq 0x250(%rax), %rax
callq *%rax
movl %eax, 0xf4(%rsp)
jmp 0x185a0
movl 0xf4(%rsp), %eax
cmpl $0x0, %eax
je 0x186dd
movq 0xe8(%rsp), %rax
movq 0x3f0(%rax), %rdi
movq (%rdi), %rax
movq 0xd0(%rax), %rax
callq *%rax
movq %rax, 0xe0(%rsp)
jmp 0x185d5
movq 0xe0(%rsp), %rax
movq %rax, 0x1e0(%rsp)
movq 0x1e0(%rsp), %rax
movq %rax, 0x1d8(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x1fc70
movq %rax, 0x1d0(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x1fca0
movq %rax, 0x1c8(%rsp)
leaq 0x1d0(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x1fcd0
testb $0x1, %al
jne 0x1863d
jmp 0x186db
leaq 0x1d0(%rsp), %rdi
callq 0x1fd10
movq %rax, 0x1c0(%rsp)
movq 0x1c0(%rsp), %rsi
leaq 0x1a8(%rsp), %rdi
callq 0x217c0
jmp 0x18669
leaq 0x1f8(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0x21790
jmp 0x18680
leaq 0x1a8(%rsp), %rdi
callq 0x1fde0
leaq 0x1d0(%rsp), %rdi
callq 0x1fd20
jmp 0x1861f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0x18d4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0x1a8(%rsp), %rdi
callq 0x1fde0
jmp 0x18d4c
jmp 0x1871b
movq 0xe8(%rsp), %rax
movq 0x3f0(%rax), %rdi
movq (%rdi), %rax
movq 0xd0(%rax), %rax
callq *%rax
movq %rax, 0xd8(%rsp)
jmp 0x18702
movq 0xd8(%rsp), %rsi
leaq 0x1f8(%rsp), %rdi
callq 0x218f0
jmp 0x18719
jmp 0x1871b
movq 0xe8(%rsp), %rdi
movq (%rdi), %rax
movq 0x100(%rax), %rax
callq *%rax
movq %rax, 0xd0(%rsp)
jmp 0x18739
movq 0xd0(%rsp), %rax
movq %rax, 0x1a0(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x1ff40
movq %rax, 0xc8(%rsp)
movq 0x1a0(%rsp), %rdi
callq 0x1ff40
movq %rax, %rcx
movq 0xc8(%rsp), %rax
cmpq %rcx, %rax
je 0x187d3
movq 0x52856(%rip), %rdi     # 0x6afd8
leaq 0x3ac86(%rip), %rsi     # 0x5340f
callq 0x4230
movq %rax, 0xc0(%rsp)
jmp 0x18798
movq 0xc0(%rsp), %rdi
leaq 0x3ac88(%rip), %rsi     # 0x5342f
callq 0x4230
movq %rax, 0xb8(%rsp)
jmp 0x187b6
movq 0xb8(%rsp), %rdi
movq 0x527eb(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x187cc
xorl %edi, %edi
callq 0x4290
leaq 0x1f8(%rsp), %rdi
callq 0x21bb0
movq %rax, %rdi
callq 0x1f9c0
cmpq $0x1, %rax
je 0x188ac
movq 0x527df(%rip), %rdi     # 0x6afd8
leaq 0x3ac0f(%rip), %rsi     # 0x5340f
callq 0x4230
movq %rax, 0xb0(%rsp)
jmp 0x1880f
movq 0xb0(%rsp), %rdi
leaq 0x3ac31(%rip), %rsi     # 0x5344f
callq 0x4230
movq %rax, 0xa8(%rsp)
jmp 0x1882d
movq 0xa8(%rsp), %rdi
movq 0x52774(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x18843
movq 0x5278e(%rip), %rdi     # 0x6afd8
leaq 0x3ac47(%rip), %rsi     # 0x53498
callq 0x4230
movq %rax, 0xa0(%rsp)
jmp 0x18860
leaq 0x1f8(%rsp), %rdi
callq 0x21bb0
movq %rax, %rdi
callq 0x1f9c0
movq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x4080
movq %rax, 0x98(%rsp)
jmp 0x1888f
movq 0x98(%rsp), %rdi
movq 0x52712(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x188a5
xorl %edi, %edi
callq 0x4290
movq 0xe8(%rsp), %rdi
addq $0x60, %rdi
callq 0x1f9c0
cmpq $0x1, %rax
je 0x1891b
movq 0x5270e(%rip), %rdi     # 0x6afd8
leaq 0x3ab3e(%rip), %rsi     # 0x5340f
callq 0x4230
movq %rax, 0x90(%rsp)
jmp 0x188e0
movq 0x90(%rsp), %rdi
leaq 0x3abc9(%rip), %rsi     # 0x534b8
callq 0x4230
movq %rax, 0x88(%rsp)
jmp 0x188fe
movq 0x88(%rsp), %rdi
movq 0x526a3(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x18914
xorl %edi, %edi
callq 0x4290
movq $0x0, 0x198(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x1ff40
movq %rax, %rcx
movq 0x80(%rsp), %rax
cmpq %rcx, %rax
jae 0x18d37
movq 0x198(%rsp), %rsi
leaq 0x1f8(%rsp), %rdi
callq 0x1fd40
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x68(%rsp)
callq 0x1f9e0
movq %rax, 0x190(%rsp)
movq 0x1a0(%rsp), %rdi
movq 0x198(%rsp), %rsi
callq 0x1fd40
movq 0x68(%rsp), %rsi
movq %rax, %rdi
callq 0x1f9e0
movq 0xe8(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq %rax, 0x188(%rsp)
addq $0x60, %rdi
callq 0x1f9e0
movq %rax, %rdi
callq 0x3bae0
movq %rdx, 0x70(%rsp)
movq %rax, 0x78(%rsp)
jmp 0x189da
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rcx
movq %rcx, 0x178(%rsp)
movq %rax, 0x180(%rsp)
movq 0x190(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x58(%rsp)
movq %rax, 0x60(%rsp)
jmp 0x18a0d
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rcx
movq %rcx, 0x168(%rsp)
movq %rax, 0x170(%rsp)
movq 0x188(%rsp), %rdi
callq 0x3bae0
movq %rdx, 0x48(%rsp)
movq %rax, 0x50(%rsp)
jmp 0x18a40
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, 0x158(%rsp)
movq %rax, 0x160(%rsp)
movq 0x178(%rsp), %rax
cmpq 0x158(%rsp), %rax
jne 0x18a82
movq 0x180(%rsp), %rax
cmpq 0x168(%rsp), %rax
je 0x18ba0
movq 0x5254f(%rip), %rdi     # 0x6afd8
leaq 0x3a97f(%rip), %rsi     # 0x5340f
callq 0x4230
movq %rax, 0x40(%rsp)
jmp 0x18a9c
movq 0x40(%rsp), %rdi
leaq 0x3a9a8(%rip), %rsi     # 0x53450
callq 0x4230
movq %rax, 0x38(%rsp)
jmp 0x18ab4
movq 0x38(%rsp), %rdi
movq 0x524f0(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x18ac7
movq 0x5250a(%rip), %rdi     # 0x6afd8
leaq 0x3aa25(%rip), %rsi     # 0x534fa
callq 0x4230
movq %rax, 0x30(%rsp)
jmp 0x18ae1
movq 0x30(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x3b200
movq %rax, 0x28(%rsp)
jmp 0x18afa
movq 0x28(%rsp), %rdi
movq 0x524aa(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x18b0d
movq 0x524c4(%rip), %rdi     # 0x6afd8
leaq 0x3aa0e(%rip), %rsi     # 0x53529
callq 0x4230
movq %rax, 0x20(%rsp)
jmp 0x18b27
movq 0x20(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x3b200
movq %rax, 0x18(%rsp)
jmp 0x18b40
movq 0x18(%rsp), %rdi
movq 0x52464(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x18b53
movq 0x5247e(%rip), %rdi     # 0x6afd8
leaq 0x3a9f8(%rip), %rsi     # 0x53559
callq 0x4230
movq %rax, 0x10(%rsp)
jmp 0x18b6d
movq 0x10(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x3b200
movq %rax, 0x8(%rsp)
jmp 0x18b86
movq 0x8(%rsp), %rdi
movq 0x5241e(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x18b99
xorl %edi, %edi
callq 0x4290
movq 0x190(%rsp), %rsi
leaq 0x140(%rsp), %rdi
callq 0x3e9f0
jmp 0x18bb7
movq 0x188(%rsp), %rsi
leaq 0x128(%rsp), %rdi
leaq 0x140(%rsp), %rdx
callq 0x3bbb0
jmp 0x18bd6
leaq 0x110(%rsp), %rdi
callq 0x1fda0
jmp 0x18be5
leaq 0x110(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x1fa50
jmp 0x18bfc
movq 0xe8(%rsp), %rdi
addq $0x90, %rdi
leaq 0x110(%rsp), %rsi
callq 0x1feb0
jmp 0x18c1a
leaq 0xf8(%rsp), %rdi
callq 0x1fda0
jmp 0x18c29
movq 0x188(%rsp), %rsi
leaq 0xf8(%rsp), %rdi
callq 0x1fa50
jmp 0x18c40
movq 0xe8(%rsp), %rdi
addq $0xa8, %rdi
leaq 0xf8(%rsp), %rsi
callq 0x1feb0
jmp 0x18c5e
leaq 0xf8(%rsp), %rdi
callq 0x1fde0
leaq 0x110(%rsp), %rdi
callq 0x1fde0
leaq 0x128(%rsp), %rdi
callq 0x3bb80
leaq 0x140(%rsp), %rdi
callq 0x3bb80
movq 0x198(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x198(%rsp)
jmp 0x18927
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0x18d28
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0x18d1b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
jmp 0x18d0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1f0(%rsp)
movl %eax, 0x1ec(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x1fde0
leaq 0x110(%rsp), %rdi
callq 0x1fde0
leaq 0x128(%rsp), %rdi
callq 0x3bb80
leaq 0x140(%rsp), %rdi
callq 0x3bb80
jmp 0x18d4c
leaq 0x1f8(%rsp), %rdi
callq 0x1f130
addq $0x218, %rsp            # imm = 0x218
retq
leaq 0x1f8(%rsp), %rdi
callq 0x1f130
movq 0x1f0(%rsp), %rdi
callq 0x43f0
nopw %cs:(%rax,%rax)