// File: /workspace/github_cpp_makefile_star2_O1/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 0x2693f(%rip), %rsi     # 0x3bff0
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movl $0x14, %edx
xorl %ecx, %ecx
callq 0x3894e
leaq 0x26931(%rip), %rsi     # 0x3c000
movl $0x15, %edx
movq %rbx, %rdi
callq 0x93b0
leaq 0xf0(%rsp), %rdi
callq 0x38b76
movq 0x30(%r15), %rax
movq %rax, 0x58(%rsp)
movq 0x38(%r15), %rax
movq %rax, 0x50(%rsp)
movq (%r14), %r13
cmpq (%rbp), %r13
jne 0x15c17
movq 0x8(%r14), %rax
movq 0x30(%r15), %rcx
movq (%rcx), %rcx
cmpq 0x8(%rcx), %rax
jne 0x15c5e
movq 0x8(%rbp), %rax
movq 0x38(%r15), %rcx
movq -0x8(%rcx), %rcx
cmpq 0x10(%rcx), %rax
jne 0x15ca5
leaq 0xc8(%rsp), %rdi
callq 0x19a38
leaq 0xa0(%rsp), %rdi
callq 0x19a38
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
callq 0x15584
callq 0x9070
movq %rax, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x1560c
movq %rbx, 0x80(%rsp)
testq %r13, %r13
movq %r13, 0x20(%rsp)
je 0x15a59
movq %r14, 0x98(%rsp)
movq 0x50(%rsp), %r12
subq 0x58(%rsp), %r12
sarq $0x3, %r12
leaq -0x1(%r12), %rax
movq %rax, 0x90(%rsp)
leaq -0x1(%r13), %rax
movq %rax, 0x18(%rsp)
leaq (,%r12,8), %r13
movl $0x1, %eax
subq %r12, %rax
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
xorl %ebx, %ebx
movq %rbp, 0x88(%rsp)
movq 0x68(%rsp), %rax
movq (%rax,%rbx,8), %rdx
movl $0x1, %ecx
movq 0x98(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x1a090
movq 0x68(%rsp), %rax
movq (%rax,%rbx,8), %rdx
movl $0x1, %ecx
movq %rbp, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x1a090
movq 0x50(%rsp), %rax
cmpq 0x58(%rsp), %rax
je 0x1586b
xorl %r14d, %r14d
testq %r14, %r14
jne 0x15850
movq 0x30(%r15), %rax
movq (%rax), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x14d18
movq 0x30(%r15), %rax
movq -0x8(%rax,%r14,8), %rsi
movq (%rax,%r14,8), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
incq %r14
cmpq %r14, %r12
jne 0x15837
cmpq $0x0, 0x90(%rsp)
je 0x15914
xorl %r14d, %r14d
xorl %ebp, %ebp
movq 0x30(%r15), %rax
leaq (%rax,%r13), %rcx
movq -0x8(%rcx,%r14,8), %rdi
subq $0x1, %rbp
jae 0x158b7
addq $0x178, %rdi            # imm = 0x178
leaq 0xa0(%rsp), %rsi
callq 0x216ea
movq 0x30(%r15), %rax
leaq (%rax,%r13), %rcx
movq -0x8(%rcx,%r14,8), %rdi
xorl %edx, %edx
jmp 0x158bb
movq (%rcx,%r14,8), %rdx
addq %r13, %rax
movq -0x10(%rax,%r14,8), %rsi
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 0x15906
movq 0x30(%r15), %rax
addq %r13, %rax
movq -0x8(%rax,%r14,8), %rdi
incq %rdx
movss (%r15), %xmm0
movq %rdx, %rsi
callq 0x16fc0
movq %rbp, %r14
cmpq %rbp, 0x10(%rsp)
jne 0x1587f
movq 0x30(%r15), %rax
movq 0x48(%r15), %rdi
movq -0x8(%rax,%r12,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 0x2acd4
movq %rax, %r14
movq 0x30(%r15), %rax
movq -0x8(%rax,%r12,8), %rdi
movl $0xa0, %eax
addq %rax, %rdi
callq 0x2acd4
testq %r14, %r14
js 0x1597a
xorps %xmm0, %xmm0
cvtsi2ss %r14, %xmm0
jmp 0x15993
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 0x159ab
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
jmp 0x159c3
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 0x159eb
movq 0xf0(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x38(%rsp)
jmp 0x159fd
leaq 0x30(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
callq 0x17ec6
movq %rbx, %rax
movabsq $-0x70a3d70a3d70a3d7, %rcx # imm = 0x8F5C28F5C28F5C29
imulq %rcx, %rax
rorq $0x2, %rax
movabsq $0x28f5c28f5c28f5c, %rcx # imm = 0x28F5C28F5C28F5C
cmpq %rcx, %rax
ja 0x15a37
leaq 0x266b1(%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 0x157e7
jmp 0x15a62
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
leaq 0x30(%rsp), %rsi
callq 0x15d7c
movsd %xmm0, 0x18(%rsp)
movq 0x20(%rsp), %rcx
testq %rcx, %rcx
movq 0x80(%rsp), %rbx
js 0x15a8e
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
jmp 0x15aa6
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 0x26525(%rip), %rsi     # 0x3bff0
leaq 0xf0(%rsp), %rdi
movl $0x4b, %edx
xorl %ecx, %ecx
callq 0x3894e
leaq 0x26744(%rip), %rsi     # 0x3c22a
leaq 0xf0(%rsp), %rdi
movl $0x4, %edx
callq 0x93b0
leaq 0xf0(%rsp), %rdi
movsd 0x18(%rsp), %xmm0
callq 0x9540
leaq 0x265d8(%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 0x265a4(%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 0x26583(%rip), %rsi     # 0x3c108
leaq 0xf0(%rsp), %rdi
movl $0x3, %edx
callq 0x93b0
leaq 0xf0(%rsp), %rdi
callq 0x38b76
leaq 0x26561(%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 0x15bd0
callq 0x9350
movq 0x68(%rsp), %rdi
cmpq %rdi, 0x70(%rsp)
je 0x15be1
movq %rdi, 0x70(%rsp)
testq %rdi, %rdi
je 0x15beb
callq 0x9350
leaq 0xa0(%rsp), %rdi
callq 0x19c8e
leaq 0xc8(%rsp), %rdi
callq 0x19c8e
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x263d2(%rip), %rsi     # 0x3bff0
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movl $0x19, %edx
movl $0x3, %ecx
callq 0x3894e
leaq 0x263d7(%rip), %rsi     # 0x3c016
movl $0x32, %edx
movq %rbx, %rdi
callq 0x93b0
leaq 0xf0(%rsp), %rdi
callq 0x38b76
jmp 0x15708
leaq 0x2638b(%rip), %rsi     # 0x3bff0
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movl $0x1a, %edx
movl $0x3, %ecx
callq 0x3894e
leaq 0x263c3(%rip), %rsi     # 0x3c049
movl $0x3e, %edx
movq %rbx, %rdi
callq 0x93b0
leaq 0xf0(%rsp), %rdi
callq 0x38b76
jmp 0x1571d
leaq 0x26344(%rip), %rsi     # 0x3bff0
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
movl $0x1b, %edx
movl $0x3, %ecx
callq 0x3894e
leaq 0x263bb(%rip), %rsi     # 0x3c088
movl $0x50, %edx
movq %rbx, %rdi
callq 0x93b0
leaq 0xf0(%rsp), %rdi
callq 0x38b76
jmp 0x15733
jmp 0x15d00
jmp 0x15d00
jmp 0x15d00
jmp 0x15d2c
jmp 0x15d2c
movq %rax, %rbx
jmp 0x15d59
movq %rax, %rbx
jmp 0x15d66
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x38b76
jmp 0x15d73
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x38b76
jmp 0x15d2f
jmp 0x15d2c
jmp 0x15d2c
jmp 0x15d2c
jmp 0x15d2c
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x15d3e
callq 0x9350
movq 0x68(%rsp), %rdi
cmpq %rdi, 0x70(%rsp)
je 0x15d4f
movq %rdi, 0x70(%rsp)
testq %rdi, %rdi
je 0x15d59
callq 0x9350
leaq 0xa0(%rsp), %rdi
callq 0x19c8e
leaq 0xc8(%rsp), %rdi
callq 0x19c8e
movq %rbx, %rdi
callq 0x95d0
nop