// File: /workspace/github_cpp_makefile_star2_O2/btbujiangjun[P]abcdl/src/fnn/FNN.cpp
// Function: abcdl::fnn::FNN::train(abcdl::algebra::Matrix<float> const&, abcdl::algebra::Matrix<float> const&)
void FNN::train(const abcdl::algebra::Mat& train_data,
                const abcdl::algebra::Mat& train_label){
    LOG(INFO) << "fnn start training...";

    size_t layer_size = _layers.size();
    size_t num_train_data = train_data.rows();

    CHECK(num_train_data == train_label.rows());
    CHECK(train_data.cols() == _layers[0]->get_input_dim());
    CHECK(train_label.cols() == _layers[_layers.size() -1]->get_output_dim());

    abcdl::algebra::Mat data;
    abcdl::algebra::Mat label;
    abcdl::utils::Shuffler shuffler(num_train_data);
    auto now = []{return std::chrono::system_clock::now();};
    auto start_time = now();
    real total_loss = 0;
    std::vector<std::pair<real, real>> auc_train_vec;

    shuffler.shuffle();
    for(size_t j = 0; j != num_train_data; j++){
        train_data.get_row(&data, shuffler.get(j));
        train_label.get_row(&label, shuffler.get(j));

        for(size_t k = 0; k != layer_size; k++){
            if(k == 0){
                ((InputLayer*)_layers[k])->set_x(data);
            }
            _layers[k]->forward(_layers[k-1]);
        }

        for(size_t k = layer_size - 1; k > 0; k--){
            if(k == layer_size - 1){
                ((OutputLayer*)_layers[k])->set_y(label);
                _layers[k]->backward(_layers[k-1], nullptr);
            }else{
                _layers[k]->backward(_layers[k-1], _layers[k+1]);
            }
               
            //mini_batch_update
            if(j % _batch_size == _batch_size - 1 || j == num_train_data - 1){
                _layers[k]->update_gradient(j % _batch_size + 1, _alpha);
            }
        }

        total_loss += _loss->loss(label, _layers[layer_size - 1]->get_activate_data());
        auc_train_vec.push_back(std::make_pair(label.argmax(), _layers[layer_size - 1]->get_activate_data().argmax()));

        if(j % 100 == 0){
            printf(" Train[%ld/%ld]\r", j, num_train_data);
        }
    }
        
    double auc_score = auc(auc_train_vec);
    double avg_loss = total_loss / num_train_data;
    long long int train_time = std::chrono::duration_cast<std::chrono::milliseconds>(now() - start_time).count();

    LOG(INFO) << "auc["<< auc_score <<"] loss["<< avg_loss <<"] training run time:["<< train_time <<"]ms";
    printf("auc[%lf] loss[%lf] training run time:[%lld]ms\n", auc_score, avg_loss, train_time);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %r15
leaq 0x1de57(%rip), %rsi     # 0x31fd0
leaq 0xe0(%rsp), %rbx
pushq $0x14
popq %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2ee18
leaq 0x1de4b(%rip), %rsi     # 0x31fe0
movq %rbx, %rdi
callq 0x9370
leaq 0xe0(%rsp), %rdi
callq 0x2f054
movq 0x30(%r15), %r14
movq 0x38(%r15), %r12
movq (%rbp), %rbx
movq %r14, %rax
cmpq (%r13), %rbx
movq %rbx, 0x18(%rsp)
jne 0x145f1
movq 0x8(%rbp), %rcx
movq (%rax), %rax
cmpq 0x8(%rax), %rcx
jne 0x14638
movq 0x8(%r13), %rax
movq 0x38(%r15), %rcx
movq -0x8(%rcx), %rcx
cmpq 0x10(%rcx), %rax
jne 0x1467b
leaq 0xb8(%rsp), %rdi
callq 0x17dac
leaq 0x90(%rsp), %rdi
callq 0x17dac
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x14074
movq %rbp, 0x68(%rsp)
movq %r13, 0x60(%rsp)
callq 0x9060
movq %rax, 0x50(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x140da
subq %r14, %r12
sarq $0x3, %r12
leaq -0x1(%r12), %rbp
leaq -0x1(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq -0x8(,%r12,8), %rax
movq %rax, 0x58(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
leaq (,%rbp,8), %rax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
cmpq %rbx, %r14
je 0x1448a
movq 0x78(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq 0x68(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
pushq $0x1
popq %rcx
callq 0x182c4
movq 0x78(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq 0x60(%rsp), %rdi
leaq 0x90(%rsp), %rsi
pushq $0x1
popq %rcx
callq 0x182c4
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x142fa
testq %rbx, %rbx
jne 0x142e2
movq 0x30(%r15), %rax
movq (%rax), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x1388c
movq 0x30(%r15), %rax
movq -0x8(%rax,%rbx,8), %rsi
movq (%rax,%rbx,8), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
incq %rbx
jmp 0x142c4
xorl %r13d, %r13d
movq 0x58(%rsp), %rbx
movq 0x30(%r15), %rax
cmpq %r13, %rbp
je 0x14395
testq %r13, %r13
je 0x1431e
movq 0x8(%rax,%rbx), %rdx
addq %rbx, %rax
jmp 0x14343
movq -0x8(%rax,%r12,8), %rdi
movl $0x178, %eax            # imm = 0x178
addq %rax, %rdi
leaq 0x90(%rsp), %rsi
callq 0x1dee2
movq 0x30(%r15), %rax
addq 0x10(%rsp), %rax
xorl %edx, %edx
movq -0x8(%rax), %rsi
movq (%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
cmpq 0x20(%rsp), %r14
setne %cl
movq 0x8(%r15), %rsi
movq %r14, %rax
xorl %edx, %edx
divq %rsi
decq %rsi
cmpq %rsi, %rdx
setne %al
testb %al, %cl
jne 0x14389
movq 0x30(%r15), %rax
movq (%rax,%rbx), %rdi
incq %rdx
movss (%r15), %xmm0
movq %rdx, %rsi
callq 0x1577e
addq $-0x8, %rbx
incq %r13
jmp 0x14302
movq 0x48(%r15), %rdi
movq -0x8(%rax,%r12,8), %rdx
movl $0xa0, %eax
addq %rax, %rdx
movq (%rdi), %rax
leaq 0x90(%rsp), %rsi
callq *0x10(%rax)
movss %xmm0, 0x2c(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x23ee8
movq 0x18(%rsp), %rbx
movq %rax, %r13
movq 0x30(%r15), %rax
movq -0x8(%rax,%r12,8), %rdi
movl $0xa0, %eax
addq %rax, %rdi
callq 0x23ee8
testq %r13, %r13
js 0x143f1
cvtsi2ss %r13, %xmm0
jmp 0x14407
movq %r13, %rcx
shrq %rcx
andl $0x1, %r13d
orq %rcx, %r13
cvtsi2ss %r13, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0xe0(%rsp)
testq %rax, %rax
js 0x1441c
cvtsi2ss %rax, %xmm0
jmp 0x14431
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0xe4(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x16328
movq %r14, %rax
xorl %edx, %edx
pushq $0x64
popq %rcx
divq %rcx
testq %rdx, %rdx
jne 0x14470
leaq 0x1dc56(%rip), %rdi     # 0x320b9
movq %r14, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x9050
movss 0xc(%rsp), %xmm0
addss 0x2c(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
incq %r14
jmp 0x1427d
leaq 0x30(%rsp), %rsi
callq 0x1473a
movsd %xmm0, 0x20(%rsp)
testq %rbx, %rbx
js 0x144a6
cvtsi2ss %rbx, %xmm0
jmp 0x144bb
movq %rbx, %rax
shrq %rax
andl $0x1, %ebx
orq %rax, %rbx
cvtsi2ss %rbx, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
callq 0x9060
subq 0x50(%rsp), %rax
movl $0xf4240, %ecx          # imm = 0xF4240
cqto
idivq %rcx
movq %rax, %rbx
leaq 0x1daf1(%rip), %rsi     # 0x31fd0
leaq 0xe0(%rsp), %rdi
pushq $0x4b
popq %rdx
xorl %ecx, %ecx
callq 0x2ee18
leaq 0x1dd12(%rip), %rsi     # 0x3220a
leaq 0xe0(%rsp), %rdi
callq 0x9370
leaq 0xe0(%rsp), %rdi
movsd 0x20(%rsp), %xmm0
callq 0x9550
leaq 0x1dbab(%rip), %rsi     # 0x320ca
leaq 0xe0(%rsp), %rdi
callq 0x9370
movss 0xc(%rsp), %xmm0
divss 0x10(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0xe0(%rsp), %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x9550
leaq 0x1db7c(%rip), %rsi     # 0x320d2
leaq 0xe0(%rsp), %rdi
callq 0x9370
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x90f0
leaq 0x1db6e(%rip), %rsi     # 0x320e8
leaq 0xe0(%rsp), %rdi
callq 0x9370
leaq 0xe0(%rsp), %rdi
callq 0x2f054
leaq 0x1db51(%rip), %rdi     # 0x320ec
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %rbx, %rsi
movb $0x2, %al
callq 0x9050
leaq 0x30(%rsp), %rdi
callq 0x1631a
leaq 0x70(%rsp), %rdi
callq 0x15866
leaq 0x90(%rsp), %rdi
callq 0x17fd6
leaq 0xb8(%rsp), %rdi
callq 0x17fd6
addq $0x268, %rsp            # imm = 0x268
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d9d8(%rip), %rsi     # 0x31fd0
leaq 0xe0(%rsp), %rbx
pushq $0x19
popq %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
callq 0x2ee18
leaq 0x1d9e1(%rip), %rsi     # 0x31ff6
movq %rbx, %rdi
callq 0x9370
leaq 0xe0(%rsp), %rdi
callq 0x2f054
movq 0x30(%r15), %rax
movq 0x18(%rsp), %rbx
jmp 0x141c8
leaq 0x1d991(%rip), %rsi     # 0x31fd0
leaq 0xe0(%rsp), %rbx
pushq $0x1a
popq %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
callq 0x2ee18
leaq 0x1d9cd(%rip), %rsi     # 0x32029
movq %rbx, %rdi
callq 0x9370
leaq 0xe0(%rsp), %rdi
callq 0x2f054
movq 0x18(%rsp), %rbx
jmp 0x141d9
leaq 0x1d94e(%rip), %rsi     # 0x31fd0
leaq 0xe0(%rsp), %rbx
pushq $0x1b
popq %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
callq 0x2ee18
leaq 0x1d9c9(%rip), %rsi     # 0x32068
movq %rbx, %rdi
callq 0x9370
leaq 0xe0(%rsp), %rdi
callq 0x2f054
movq 0x18(%rsp), %rbx
jmp 0x141ef
jmp 0x146d4
jmp 0x146d4
jmp 0x146d4
jmp 0x14700
jmp 0x14700
jmp 0x14700
movq %rax, %rbx
jmp 0x14717
movq %rax, %rbx
jmp 0x14724
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x2f054
jmp 0x14731
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x2f054
jmp 0x14703
jmp 0x14700
jmp 0x14700
jmp 0x14700
jmp 0x14700
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1631a
leaq 0x70(%rsp), %rdi
callq 0x15866
leaq 0x90(%rsp), %rdi
callq 0x17fd6
leaq 0xb8(%rsp), %rdi
callq 0x17fd6
movq %rbx, %rdi
callq 0x95e0
nop