// File: /workspace/github_cpp_makefile_star2_O3/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 $0x278, %rsp            # imm = 0x278
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r15
leaq 0x26c3b(%rip), %rsi     # 0x3bff0
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movl $0x14, %edx
xorl %ecx, %ecx
callq 0x387b6
leaq 0x26c2d(%rip), %rsi     # 0x3c000
movl $0x15, %edx
movq %rbx, %rdi
callq 0x93b0
leaq 0xf0(%rsp), %rdi
callq 0x389de
movq 0x30(%r15), %rax
movq 0x38(%r15), %rcx
movq %rcx, 0x48(%rsp)
movq (%r14), %r12
movq %rax, 0x50(%rsp)
cmpq (%rbp), %r12
jne 0x15935
movq 0x8(%r14), %rcx
movq (%rax), %rax
cmpq 0x8(%rax), %rcx
jne 0x15980
movq 0x8(%rbp), %rax
movq 0x38(%r15), %rcx
movq -0x8(%rcx), %rcx
cmpq 0x10(%rcx), %rax
jne 0x159c7
leaq 0xc8(%rsp), %rdi
callq 0x197e0
leaq 0xa0(%rsp), %rdi
callq 0x197e0
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0x15288
callq 0x9070
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x15310
movq %rbx, 0x78(%rsp)
testq %r12, %r12
movq %r12, 0x20(%rsp)
je 0x1577a
movq %r14, 0x90(%rsp)
movq 0x48(%rsp), %rax
subq 0x50(%rsp), %rax
sarq $0x3, %rax
leaq -0x1(%rax), %r13
leaq -0x1(%r12), %rcx
movq %rcx, 0x18(%rsp)
movq %rax, %r12
negq %r12
movq %rax, 0x10(%rsp)
leaq -0x8(,%rax,8), %rax
movq %rax, 0x80(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
leaq (,%r13,8), %rax
movq %rax, 0x98(%rsp)
xorl %ebx, %ebx
movq %rbp, 0x88(%rsp)
movq 0x60(%rsp), %rax
movq (%rax,%rbx,8), %rdx
movl $0x1, %ecx
movq 0x90(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x19e38
movq 0x60(%rsp), %rax
movq (%rax,%rbx,8), %rdx
movl $0x1, %ecx
movq %rbp, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x19e38
movq 0x48(%rsp), %rax
cmpq 0x50(%rsp), %rax
je 0x15582
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x30(%r15), %rax
subq $0x1, %rbp
jae 0x15567
movq (%rax), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x14a1c
movq 0x30(%r15), %rax
movq %rax, %rcx
jmp 0x1556b
leaq (%rax,%r14), %rcx
movq (%rcx), %rdi
movq -0x8(%rax,%r14), %rsi
movq (%rdi), %rax
callq *0x10(%rax)
addq $0x8, %r14
cmpq %rbp, %r12
jne 0x15544
testq %r13, %r13
je 0x1562b
movq 0x80(%rsp), %r14
xorl %ebp, %ebp
movq 0x30(%r15), %rax
testq %rbp, %rbp
je 0x155a8
movq 0x8(%rax,%r14), %rdx
addq %r14, %rax
jmp 0x155d5
movq 0x10(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %rdi
movl $0x178, %eax            # imm = 0x178
addq %rax, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x21422
movq 0x30(%r15), %rax
addq 0x98(%rsp), %rax
xorl %edx, %edx
movq -0x8(%rax), %rsi
movq (%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
cmpq 0x18(%rsp), %rbx
setne %cl
movq 0x8(%r15), %rsi
movq %rbx, %rax
xorl %edx, %edx
divq %rsi
decq %rsi
cmpq %rsi, %rdx
setne %al
testb %al, %cl
jne 0x1561b
movq 0x30(%r15), %rax
movq (%rax,%r14), %rdi
incq %rdx
movss (%r15), %xmm0
movq %rdx, %rsi
callq 0x16d74
incq %rbp
addq $-0x8, %r14
cmpq %rbp, %r13
jne 0x15595
movq 0x30(%r15), %rax
movq 0x48(%r15), %rdi
movq 0x10(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %rdx
movl $0xa0, %eax
addq %rax, %rdx
movq (%rdi), %rax
leaq 0xa0(%rsp), %rsi
callq *0x10(%rax)
movss %xmm0, 0x2c(%rsp)
movq 0x88(%rsp), %rbp
leaq 0xa0(%rsp), %rdi
callq 0x2ab7a
movq %rax, %r14
movq 0x30(%r15), %rax
movq 0x10(%rsp), %rcx
movq -0x8(%rax,%rcx,8), %rdi
movl $0xa0, %eax
addq %rax, %rdi
callq 0x2ab7a
testq %r14, %r14
js 0x1569b
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
jmp 0x156b4
movq %r14, %rcx
shrq %rcx
andl $0x1, %r14d
orq %rcx, %r14
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0xf0(%rsp)
testq %rax, %rax
js 0x156cc
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x156e4
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0xf4(%rsp)
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0x1570c
movq 0xf0(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x38(%rsp)
jmp 0x1571e
leaq 0x30(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
callq 0x17c6c
movq %rbx, %rax
movabsq $-0x70a3d70a3d70a3d7, %rcx # imm = 0x8F5C28F5C28F5C29
imulq %rcx, %rax
rorq $0x2, %rax
movabsq $0x28f5c28f5c28f5c, %rcx # imm = 0x28F5C28F5C28F5C
cmpq %rcx, %rax
ja 0x15758
leaq 0x26990(%rip), %rdi     # 0x3c0d9
movq %rbx, %rsi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x9060
movss 0xc(%rsp), %xmm0
addss 0x2c(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
incq %rbx
cmpq 0x20(%rsp), %rbx
jne 0x154f2
jmp 0x15783
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
leaq 0x30(%rsp), %rsi
callq 0x15a9e
movsd %xmm0, 0x18(%rsp)
movq 0x20(%rsp), %rcx
testq %rcx, %rcx
movq 0x78(%rsp), %rbx
js 0x157ac
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
jmp 0x157c4
movq %rcx, %rax
shrq %rax
andl $0x1, %ecx
orq %rax, %rcx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
addss %xmm0, %xmm0
movss %xmm0, 0x10(%rsp)
callq 0x9070
subq %rbx, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rbx
leaq 0x26807(%rip), %rsi     # 0x3bff0
leaq 0xf0(%rsp), %rdi
movl $0x4b, %edx
xorl %ecx, %ecx
callq 0x387b6
leaq 0x26a26(%rip), %rsi     # 0x3c22a
leaq 0xf0(%rsp), %rdi
movl $0x4, %edx
callq 0x93b0
leaq 0xf0(%rsp), %rdi
movsd 0x18(%rsp), %xmm0
callq 0x9540
leaq 0x268ba(%rip), %rsi     # 0x3c0ea
leaq 0xf0(%rsp), %rdi
movl $0x7, %edx
callq 0x93b0
movss 0xc(%rsp), %xmm0
divss 0x10(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0xf0(%rsp), %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x9540
leaq 0x26886(%rip), %rsi     # 0x3c0f2
leaq 0xf0(%rsp), %rdi
movl $0x15, %edx
callq 0x93b0
movq %rbx, %rax
shrq $0x3f, %rax
sarq $0x12, %rbx
addq %rax, %rbx
leaq 0xf0(%rsp), %rdi
movq %rbx, %rsi
callq 0x9120
leaq 0x26865(%rip), %rsi     # 0x3c108
leaq 0xf0(%rsp), %rdi
movl $0x3, %edx
callq 0x93b0
leaq 0xf0(%rsp), %rdi
callq 0x389de
leaq 0x26843(%rip), %rdi     # 0x3c10c
movsd 0x18(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %rbx, %rsi
movb $0x2, %al
callq 0x9060
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x158ee
callq 0x9350
movq 0x60(%rsp), %rdi
cmpq %rdi, 0x68(%rsp)
je 0x158ff
movq %rdi, 0x68(%rsp)
testq %rdi, %rdi
je 0x15909
callq 0x9350
leaq 0xa0(%rsp), %rdi
callq 0x19a36
leaq 0xc8(%rsp), %rdi
callq 0x19a36
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x266b4(%rip), %rsi     # 0x3bff0
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movl $0x19, %edx
movl $0x3, %ecx
callq 0x387b6
leaq 0x266b9(%rip), %rsi     # 0x3c016
movl $0x32, %edx
movq %rbx, %rdi
callq 0x93b0
leaq 0xf0(%rsp), %rdi
callq 0x389de
movq 0x30(%r15), %rax
jmp 0x1540c
leaq 0x26669(%rip), %rsi     # 0x3bff0
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movl $0x1a, %edx
movl $0x3, %ecx
callq 0x387b6
leaq 0x266a1(%rip), %rsi     # 0x3c049
movl $0x3e, %edx
movq %rbx, %rdi
callq 0x93b0
leaq 0xf0(%rsp), %rdi
callq 0x389de
jmp 0x1541d
leaq 0x26622(%rip), %rsi     # 0x3bff0
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movl $0x1b, %edx
movl $0x3, %ecx
callq 0x387b6
leaq 0x26699(%rip), %rsi     # 0x3c088
movl $0x50, %edx
movq %rbx, %rdi
callq 0x93b0
leaq 0xf0(%rsp), %rdi
callq 0x389de
jmp 0x15433
jmp 0x15a22
jmp 0x15a22
jmp 0x15a22
jmp 0x15a4e
jmp 0x15a4e
movq %rax, %rbx
jmp 0x15a7b
movq %rax, %rbx
jmp 0x15a88
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x389de
jmp 0x15a95
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x389de
jmp 0x15a51
jmp 0x15a4e
jmp 0x15a4e
jmp 0x15a4e
jmp 0x15a4e
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x15a60
callq 0x9350
movq 0x60(%rsp), %rdi
cmpq %rdi, 0x68(%rsp)
je 0x15a71
movq %rdi, 0x68(%rsp)
testq %rdi, %rdi
je 0x15a7b
callq 0x9350
leaq 0xa0(%rsp), %rdi
callq 0x19a36
leaq 0xc8(%rsp), %rdi
callq 0x19a36
movq %rbx, %rdi
callq 0x95d0
nop