// File: /workspace/github_cpp_makefile_star2_O1/btbujiangjun[P]abcdl/src/rnn/RNN.cpp
// Function: abcdl::rnn::RNN::train(abcdl::algebra::MatrixSet<float> const&, abcdl::algebra::MatrixSet<float> const&)
void RNN::train(const abcdl::algebra::MatSet& train_seq_data,
                const abcdl::algebra::MatSet& train_seq_label){
	if(!check_data(train_seq_data, train_seq_label)){
        printf("RNN::sgd Data dim Error.\n");
		return ;
	}
	
	size_t num_train_data = train_seq_data.size();
    abcdl::utils::Shuffler shuffler(num_train_data);
	auto now = []{return std::chrono::system_clock::now();};

	abcdl::algebra::Mat state;
	abcdl::algebra::Mat activation;
	
	abcdl::algebra::Mat batch_derivate_weight(_U.rows(), _U.cols());
	abcdl::algebra::Mat batch_derivate_pre_weight(_W.rows(), _W.cols());
	abcdl::algebra::Mat batch_derivate_act_weight(_V.rows(), _V.cols());

	for(size_t i = 0; i != _epoch; i++){
        shuffler.shuffle();
		auto start_time = now();

		for(size_t j = 0; j != num_train_data; j++){
			_layer->farward(train_seq_data[shuffler.get(j)], _U, _W, _V, state, activation);
			_layer->backward(train_seq_data[shuffler.get(j)], train_seq_label[shuffler.get(j)], _U, _W, _V, state, activation, batch_derivate_weight, batch_derivate_pre_weight, batch_derivate_act_weight);

            if( j % _mini_batch_size == (_mini_batch_size - 1) || j == (num_train_data - 1)){
				size_t n = j % _mini_batch_size + 1;
    			_U -= batch_derivate_weight * (_alpha / n);
			    _W -= batch_derivate_pre_weight * (_alpha / n);
			    _V -= batch_derivate_act_weight * (_alpha / n);

			}

            if( j % 5 == 0){
                printf("Epoch[%ld][%ld/%ld] training...\r", i, j, num_train_data);
            }
		}

        if(_path != ""){
            write_model(_path);
        }

        printf("Epoch[%ld] training run time: %lld ms, loss[%f] base loss[%f]\n", i, (long long int)std::chrono::duration_cast<std::chrono::milliseconds>(now() - start_time).count(), loss(train_seq_data, train_seq_label), std::log(_feature_dim));
	}

	printf("training finished.\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rsi, %rbp
movq %rdi, %r15
movq %rdx, 0x40(%rsp)
callq 0x23e00
testb %al, %al
je 0x23d22
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
movq %rcx, 0x70(%rsp)
movq %rcx, %rsi
movq %rax, 0x78(%rsp)
subq %rax, %rsi
sarq $0x3, %rsi
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rsi
leaq 0x48(%rsp), %rdi
movq %rsi, 0x10(%rsp)
callq 0x2371c
leaq 0x180(%rsp), %rdi
callq 0x26b6c
leaq 0x158(%rsp), %rdi
callq 0x26b6c
movq 0x70(%r15), %rsi
movq 0x78(%r15), %rdx
leaq 0x130(%rsp), %rdi
callq 0x26b8a
movq 0xa8(%r15), %rsi
movq 0xb0(%r15), %rdx
leaq 0x108(%rsp), %rdi
callq 0x26b8a
movq 0xe0(%r15), %rsi
movq 0xe8(%r15), %rdx
leaq 0xe0(%rsp), %rdi
callq 0x26b8a
cmpq $0x0, 0x18(%r15)
je 0x23ca8
leaq 0x70(%r15), %rax
movq %rax, 0x38(%rsp)
leaq 0xa8(%r15), %rax
movq %rax, 0x30(%rsp)
leaq 0xe0(%r15), %rax
movq %rax, 0x28(%rsp)
leaq 0x30(%r15), %r14
movq 0x10(%rsp), %rax
decq %rax
movq %rax, 0x88(%rsp)
leaq 0xb8(%rsp), %r13
movq $0x0, 0x8(%rsp)
movq %r14, 0x68(%rsp)
movq %rbp, 0x20(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x237a4
callq 0xa080
movq %rax, 0x80(%rsp)
movq 0x70(%rsp), %rax
cmpq 0x78(%rsp), %rax
je 0x23bde
xorl %r14d, %r14d
movq %r15, %rsi
movq %r13, %r15
movq %r14, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rcx
movq %rdx, 0x18(%rsp)
movq %rsi, %r12
movq 0x118(%rsi), %r13
movq 0x50(%rsp), %rax
movq (%rax,%r14,8), %rdx
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x2465a
subq $0x8, %rsp
movq %r13, %rdi
movq %rbx, %rsi
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %r8
leaq 0x188(%rsp), %r9
leaq 0x160(%rsp), %rax
pushq %rax
callq 0x22d70
addq $0x10, %rsp
movq %rbx, %rdi
callq 0x26dc2
movq 0x118(%r12), %rbp
movq 0x50(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x2465a
movq 0x50(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq %r15, %rdi
movq 0x40(%rsp), %rsi
callq 0x2465a
subq $0x8, %rsp
movq %rbp, %rdi
leaq 0x98(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x30(%rsp), %r9
leaq 0xe8(%rsp), %rax
pushq %rax
leaq 0x118(%rsp), %rax
pushq %rax
leaq 0x148(%rsp), %rax
pushq %rax
leaq 0x178(%rsp), %rax
pushq %rax
leaq 0x1a8(%rsp), %rax
pushq %rax
callq 0x23082
addq $0x30, %rsp
movq %r15, %r13
movq %r15, %rdi
callq 0x26dc2
movq %rbx, %rdi
callq 0x26dc2
movq %r12, %r15
movq 0x20(%r12), %rcx
movq %r14, %rax
xorl %edx, %edx
divq %rcx
decq %rcx
cmpq %rcx, %rdx
setne %al
cmpq %r14, 0x88(%rsp)
setne %cl
testb %al, %cl
jne 0x23ba3
incq %rdx
js 0x23acb
cvtsi2ss %rdx, %xmm1
jmp 0x23ae0
movq %rdx, %rax
shrq %rax
andl $0x1, %edx
orq %rax, %rdx
cvtsi2ss %rdx, %xmm1
addss %xmm1, %xmm1
leaq 0x90(%rsp), %r12
movss 0x28(%r15), %xmm0
movss %xmm1, 0x4(%rsp)
divss %xmm1, %xmm0
movss %xmm0, 0xb8(%rsp)
movq %r12, %rdi
leaq 0x130(%rsp), %rsi
movq %r13, %rdx
callq 0x2f786
movq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x2f4a8
movq %r12, %rdi
callq 0x26dc2
movss 0x28(%r15), %xmm0
divss 0x4(%rsp), %xmm0
movss %xmm0, 0xb8(%rsp)
movq %r12, %rdi
leaq 0x108(%rsp), %rsi
movq %r13, %rdx
callq 0x2f786
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x2f4a8
movq %r12, %rdi
callq 0x26dc2
movss 0x28(%r15), %xmm0
divss 0x4(%rsp), %xmm0
movss %xmm0, 0xb8(%rsp)
movq %r12, %rdi
leaq 0xe0(%rsp), %rsi
movq %r13, %rdx
callq 0x2f786
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x2f4a8
movq %r12, %rdi
callq 0x26dc2
movq 0x18(%rsp), %rax
shrq $0x2, %rax
leaq (%rax,%rax,4), %rax
cmpq %r14, %rax
jne 0x23bd0
leaq 0x26085(%rip), %rdi     # 0x49c41
movq 0x8(%rsp), %rsi
movq %r14, %rdx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0xa070
incq %r14
cmpq %r14, 0x10(%rsp)
jne 0x23974
movq 0x68(%rsp), %r14
movq %r14, %rdi
leaq 0x23611(%rip), %rsi     # 0x471fe
callq 0xa100
testl %eax, %eax
je 0x23c01
movq %r15, %rdi
movq %r14, %rsi
callq 0x23e62
callq 0xa080
movq %rax, %rbx
movq %r15, %rdi
movq 0x20(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x23fd8
subq 0x80(%rsp), %rbx
movq %rbx, %rax
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rbx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rbx
addq %rax, %rbx
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd (%r15), %xmm1
unpcklps 0x233d6(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x233de(%rip), %xmm1    # 0x47040
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0xa380
movapd %xmm0, %xmm1
leaq 0x25fe4(%rip), %rdi     # 0x49c62
movq 0x8(%rsp), %r12
movq %r12, %rsi
movq %rbx, %rdx
movsd 0x18(%rsp), %xmm0
movb $0x2, %al
callq 0xa070
incq %r12
movq %r12, 0x8(%rsp)
cmpq 0x18(%r15), %r12
jne 0x2394a
leaq 0x2609b(%rip), %rdi     # 0x49d4a
callq 0xa670
leaq 0xe0(%rsp), %rdi
callq 0x26dc2
leaq 0x108(%rsp), %rdi
callq 0x26dc2
leaq 0x130(%rsp), %rdi
callq 0x26dc2
leaq 0x158(%rsp), %rdi
callq 0x26dc2
leaq 0x180(%rsp), %rdi
callq 0x26dc2
movq 0x50(%rsp), %rdi
cmpq %rdi, 0x58(%rsp)
je 0x23d06
movq %rdi, 0x58(%rsp)
testq %rdi, %rdi
je 0x23d10
callq 0xa440
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26008(%rip), %rdi     # 0x49d31
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa670
movq %rax, %rbx
jmp 0x23da8
movq %rax, %rbx
jmp 0x23db5
movq %rax, %rbx
jmp 0x23dc2
movq %rax, %rbx
jmp 0x23dcf
movq %rax, %rbx
jmp 0x23ddc
jmp 0x23d79
jmp 0x23d79
jmp 0x23d79
jmp 0x23d79
jmp 0x23d79
jmp 0x23d79
jmp 0x23d6d
jmp 0x23d6d
jmp 0x23d6d
movq %rax, %rbx
jmp 0x23d8e
movq %rax, %rbx
jmp 0x23d8e
jmp 0x23d79
movq %rax, %rbx
jmp 0x23d9b
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x26dc2
leaq 0x90(%rsp), %rdi
callq 0x26dc2
leaq 0xe0(%rsp), %rdi
callq 0x26dc2
leaq 0x108(%rsp), %rdi
callq 0x26dc2
leaq 0x130(%rsp), %rdi
callq 0x26dc2
leaq 0x158(%rsp), %rdi
callq 0x26dc2
leaq 0x180(%rsp), %rdi
callq 0x26dc2
movq 0x50(%rsp), %rdi
cmpq %rdi, 0x58(%rsp)
je 0x23ded
movq %rdi, 0x58(%rsp)
testq %rdi, %rdi
je 0x23df7
callq 0xa440
movq %rbx, %rdi
callq 0xa780
nop