// File: /workspace/github_cpp_makefile_star2_O3/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 0x23a78
testb %al, %al
je 0x2399a
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 0x23394
leaq 0x180(%rsp), %rdi
callq 0x267d8
leaq 0x158(%rsp), %rdi
callq 0x267d8
movq 0x70(%r15), %rsi
movq 0x78(%r15), %rdx
leaq 0x130(%rsp), %rdi
callq 0x267f6
movq 0xa8(%r15), %rsi
movq 0xb0(%r15), %rdx
leaq 0x108(%rsp), %rdi
callq 0x267f6
movq 0xe0(%r15), %rsi
movq 0xe8(%r15), %rdx
leaq 0xe0(%rsp), %rdi
callq 0x267f6
cmpq $0x0, 0x18(%r15)
je 0x23920
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 0x2341c
callq 0xa080
movq %rax, 0x80(%rsp)
movq 0x70(%rsp), %rax
cmpq 0x78(%rsp), %rax
je 0x23856
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 0x242d4
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 0x229f0
addq $0x10, %rsp
movq %rbx, %rdi
callq 0x26a2e
movq 0x118(%r12), %rbp
movq 0x50(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x242d4
movq 0x50(%rsp), %rax
movq (%rax,%r14,8), %rdx
movq %r15, %rdi
movq 0x40(%rsp), %rsi
callq 0x242d4
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 0x22cfa
addq $0x30, %rsp
movq %r15, %r13
movq %r15, %rdi
callq 0x26a2e
movq %rbx, %rdi
callq 0x26a2e
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 0x2381b
incq %rdx
js 0x23743
cvtsi2ss %rdx, %xmm1
jmp 0x23758
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 0x2f3d0
movq 0x38(%rsp), %rdi
movq %r12, %rsi
callq 0x2f0da
movq %r12, %rdi
callq 0x26a2e
movss 0x28(%r15), %xmm0
divss 0x4(%rsp), %xmm0
movss %xmm0, 0xb8(%rsp)
movq %r12, %rdi
leaq 0x108(%rsp), %rsi
movq %r13, %rdx
callq 0x2f3d0
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x2f0da
movq %r12, %rdi
callq 0x26a2e
movss 0x28(%r15), %xmm0
divss 0x4(%rsp), %xmm0
movss %xmm0, 0xb8(%rsp)
movq %r12, %rdi
leaq 0xe0(%rsp), %rsi
movq %r13, %rdx
callq 0x2f3d0
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x2f0da
movq %r12, %rdi
callq 0x26a2e
movq 0x18(%rsp), %rax
shrq $0x2, %rax
leaq (%rax,%rax,4), %rax
cmpq %r14, %rax
jne 0x23848
leaq 0x2640d(%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 0x235ec
movq 0x68(%rsp), %r14
movq %r14, %rdi
leaq 0x23999(%rip), %rsi     # 0x471fe
callq 0xa100
testl %eax, %eax
je 0x23879
movq %r15, %rdi
movq %r14, %rsi
callq 0x23ada
callq 0xa080
movq %rax, %rbx
movq %r15, %rdi
movq 0x20(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x23c40
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 0x2375e(%rip), %xmm1    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x23766(%rip), %xmm1    # 0x47040
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
callq 0xa380
movapd %xmm0, %xmm1
leaq 0x2636c(%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 0x235c2
leaq 0x26423(%rip), %rdi     # 0x49d4a
callq 0xa670
leaq 0xe0(%rsp), %rdi
callq 0x26a2e
leaq 0x108(%rsp), %rdi
callq 0x26a2e
leaq 0x130(%rsp), %rdi
callq 0x26a2e
leaq 0x158(%rsp), %rdi
callq 0x26a2e
leaq 0x180(%rsp), %rdi
callq 0x26a2e
movq 0x50(%rsp), %rdi
cmpq %rdi, 0x58(%rsp)
je 0x2397e
movq %rdi, 0x58(%rsp)
testq %rdi, %rdi
je 0x23988
callq 0xa440
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x26390(%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 0x23a20
movq %rax, %rbx
jmp 0x23a2d
movq %rax, %rbx
jmp 0x23a3a
movq %rax, %rbx
jmp 0x23a47
movq %rax, %rbx
jmp 0x23a54
jmp 0x239f1
jmp 0x239f1
jmp 0x239f1
jmp 0x239f1
jmp 0x239f1
jmp 0x239f1
jmp 0x239e5
jmp 0x239e5
jmp 0x239e5
movq %rax, %rbx
jmp 0x23a06
movq %rax, %rbx
jmp 0x23a06
jmp 0x239f1
movq %rax, %rbx
jmp 0x23a13
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x26a2e
leaq 0x90(%rsp), %rdi
callq 0x26a2e
leaq 0xe0(%rsp), %rdi
callq 0x26a2e
leaq 0x108(%rsp), %rdi
callq 0x26a2e
leaq 0x130(%rsp), %rdi
callq 0x26a2e
leaq 0x158(%rsp), %rdi
callq 0x26a2e
leaq 0x180(%rsp), %rdi
callq 0x26a2e
movq 0x50(%rsp), %rdi
cmpq %rdi, 0x58(%rsp)
je 0x23a65
movq %rdi, 0x58(%rsp)
testq %rdi, %rdi
je 0x23a6f
callq 0xa440
movq %rbx, %rdi
callq 0xa780
nop