// File: /workspace/github_cpp_makefile_star2_O2/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 $0x198, %rsp            # imm = 0x198
movq %rsi, %rbp
movq %rdi, %r15
movq %rdx, 0x48(%rsp)
callq 0x2105e
testb %al, %al
je 0x20f29
movq 0x8(%rbp), %rax
subq (%rbp), %rax
pushq $0x28
popq %rcx
cqto
idivq %rcx
leaq 0x88(%rsp), %rdi
movq %rax, 0x8(%rsp)
movq %rax, %rsi
callq 0x20a2c
leaq 0x170(%rsp), %rdi
callq 0x23808
leaq 0x148(%rsp), %rdi
callq 0x23808
movq 0x70(%r15), %rsi
movq 0x78(%r15), %rdx
leaq 0x120(%rsp), %rdi
callq 0x23822
movq 0xa8(%r15), %rsi
movq 0xb0(%r15), %rdx
leaq 0xf8(%rsp), %rdi
callq 0x23822
movq 0xe0(%r15), %rsi
movq 0xe8(%r15), %rdx
leaq 0xd0(%rsp), %rdi
callq 0x23822
leaq 0x70(%r15), %rax
movq %rax, 0x30(%rsp)
leaq 0xa8(%r15), %rax
movq %rax, 0x28(%rsp)
leaq 0xe0(%r15), %rax
movq %rax, 0x20(%rsp)
leaq 0x30(%r15), %rax
movq %rax, 0x38(%rsp)
movq 0x8(%rsp), %rax
decq %rax
movq %rax, 0x58(%rsp)
leaq 0x60(%rsp), %r12
leaq 0xa8(%rsp), %r13
xorl %r14d, %r14d
movq %rbp, 0x18(%rsp)
cmpq 0x18(%r15), %r14
je 0x20f46
movq %r14, 0x40(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x20a92
callq 0xa070
movq %rax, 0x50(%rsp)
xorl %r14d, %r14d
movq %r14, %rax
xorl %edx, %edx
pushq $0x5
popq %rcx
divq %rcx
cmpq %r14, 0x8(%rsp)
je 0x20e9c
movq %rax, 0x10(%rsp)
movq %r15, %rax
movq %r13, %r15
movq %r12, %rbx
movq %rax, %r12
movq 0x118(%rax), %r13
movq 0x90(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x2170c
subq $0x8, %rsp
movq %r13, %rdi
movq %rbx, %rsi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %r8
leaq 0x178(%rsp), %r9
leaq 0x150(%rsp), %rax
pushq %rax
callq 0x201d8
popq %rax
popq %rcx
movq %rbx, %rdi
callq 0x23a32
movq 0x118(%r12), %rbp
movq 0x90(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x2170c
movq 0x90(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq %r15, %rdi
movq 0x48(%rsp), %rsi
callq 0x2170c
subq $0x8, %rsp
movq %rbp, %rdi
leaq 0x68(%rsp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
movq 0x38(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r9
leaq 0xd8(%rsp), %rax
pushq %rax
leaq 0x108(%rsp), %rax
pushq %rax
leaq 0x138(%rsp), %rax
pushq %rax
leaq 0x168(%rsp), %rax
pushq %rax
leaq 0x198(%rsp), %rax
pushq %rax
callq 0x204d2
addq $0x30, %rsp
movq %r15, %r13
movq %r15, %rdi
callq 0x23a32
movq %rbx, %rdi
callq 0x23a32
movq %r12, %r15
movq 0x20(%r12), %rcx
movq %r14, %rax
xorl %edx, %edx
divq %rcx
decq %rcx
cmpq %rcx, %rdx
setne %al
cmpq %r14, 0x58(%rsp)
setne %cl
testb %al, %cl
jne 0x20e66
incq %rdx
js 0x20d91
cvtsi2ss %rdx, %xmm1
jmp 0x20da6
movq %rdx, %rax
shrq %rax
andl $0x1, %edx
orq %rax, %rdx
cvtsi2ss %rdx, %xmm1
addss %xmm1, %xmm1
leaq 0x60(%rsp), %r12
movss 0x28(%r15), %xmm0
movss %xmm1, 0x4(%rsp)
divss %xmm1, %xmm0
movss %xmm0, 0xa8(%rsp)
movq %r12, %rdi
leaq 0x120(%rsp), %rsi
movq %r13, %rdx
callq 0x2a004
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x29e72
movq %r12, %rdi
callq 0x23a32
movss 0x28(%r15), %xmm0
divss 0x4(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
movq %r12, %rdi
leaq 0xf8(%rsp), %rsi
movq %r13, %rdx
callq 0x2a004
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x29e72
movq %r12, %rdi
callq 0x23a32
movss 0x28(%r15), %xmm0
divss 0x4(%rsp), %xmm0
movss %xmm0, 0xa8(%rsp)
movq %r12, %rdi
leaq 0xd0(%rsp), %rsi
movq %r13, %rdx
callq 0x2a004
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x29e72
movq %r12, %rdi
callq 0x23a32
movq 0x10(%rsp), %rax
leaq (%rax,%rax,4), %rax
cmpq %r14, %rax
jne 0x20e8f
leaq 0x1ddc6(%rip), %rdi     # 0x3ec41
movq 0x40(%rsp), %rsi
movq %r14, %rdx
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0xa060
incq %r14
leaq 0x60(%rsp), %r12
jmp 0x20c38
movq 0x38(%rsp), %rdi
leaq 0x1b356(%rip), %rsi     # 0x3c1fe
callq 0x21724
testb %al, %al
je 0x20ebe
movq %r15, %rdi
movq 0x38(%rsp), %rsi
callq 0x210c0
callq 0xa070
movq %rax, %rbx
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movq 0x48(%rsp), %rdx
callq 0x211c4
movq 0x40(%rsp), %r14
subq 0x50(%rsp), %rbx
movq %rbx, %rax
cqto
movl $0xf4240, %ecx          # imm = 0xF4240
idivq %rcx
movq %rax, %rbx
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movq (%r15), %rdi
callq 0x2172e
movaps %xmm0, %xmm1
leaq 0x1dd54(%rip), %rdi     # 0x3ec62
movq %r14, %rsi
movq %rbx, %rdx
movsd 0x10(%rsp), %xmm0
movb $0x2, %al
callq 0xa060
incq %r14
jmp 0x20c0f
leaq 0x1de23(%rip), %rdi     # 0x3ed53
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xa6a0
leaq 0x1de1f(%rip), %rdi     # 0x3ed6c
callq 0xa6a0
leaq 0xd0(%rsp), %rdi
callq 0x23a32
leaq 0xf8(%rsp), %rdi
callq 0x23a32
leaq 0x120(%rsp), %rdi
callq 0x23a32
leaq 0x148(%rsp), %rdi
callq 0x23a32
leaq 0x170(%rsp), %rdi
callq 0x23a32
leaq 0x88(%rsp), %rdi
callq 0x21754
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x21015
movq %rax, %rbx
jmp 0x21022
movq %rax, %rbx
jmp 0x2102f
movq %rax, %rbx
jmp 0x2103c
movq %rax, %rbx
jmp 0x21049
jmp 0x20ff6
jmp 0x20ff6
jmp 0x20ff6
jmp 0x20ff6
jmp 0x20ff6
jmp 0x20ff6
jmp 0x20ff1
jmp 0x20ff1
jmp 0x20ff1
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x23a32
jmp 0x20ffe
jmp 0x20ff6
movq %rax, %rbx
jmp 0x20ffe
movq %rax, %rbx
jmp 0x21008
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x23a32
leaq 0xd0(%rsp), %rdi
callq 0x23a32
leaq 0xf8(%rsp), %rdi
callq 0x23a32
leaq 0x120(%rsp), %rdi
callq 0x23a32
leaq 0x148(%rsp), %rdi
callq 0x23a32
leaq 0x170(%rsp), %rdi
callq 0x23a32
leaq 0x88(%rsp), %rdi
callq 0x21754
movq %rbx, %rdi
callq 0xa7a0