// File: /workspace/github_cpp_makefile_star2_O3/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::LoadTrainedWeightsAndBias()
void ConstructLayer::LoadTrainedWeightsAndBias()
{
    int layer_id = GetID();
    LayerType layer_type = GetType();
    LayerDimension layer_dimension = GetLayerDimension();

    // parse file name
    std::ostringstream oss;
    oss<<"weights_and_bias_trained/LayerID=";
    oss<<layer_id;
    oss<<layer_type;
    oss<<layer_dimension;
    oss<<"_weights_and_bias.txt";

    // save weights and bias
    std::fstream ff(oss.str(), std::ios::in);
    std::cout<<__func__<<"(): Loading trained weights and bias for layer: "<<layer_id<<" from files."<<std::endl;

    if(!ff.is_open())
    {
        std::cout<<__func__<<" Error: cannot open file: "<<oss.str()<<" to load weights and bias."
            <<std::endl;
        std::cout<<"           please make sure you have files exsit."
            <<std::endl;
        exit(0);
    }

    // a helper
    auto parseLine = [&](std::string line) -> std::vector<double>
    {
        std::vector<double> tmp;
        std::istringstream iss(line);
        double v;
        while(iss>>v)
        {
            tmp.push_back(v);
        }
        return tmp;
    };

    std::string line;
    std::vector<std::vector<double>> vv;
    while(getline(ff, line))
    {
        std::vector<double> v_line;
        if(line.find("weight") != std::string::npos)
        {
            if(vv.size() > 0) // last read is bias matrix
            {
                Matrix M(vv);
                __biasVector.push_back(M);
                vv.clear();
            }
        } 
        else if(line.find("bias") != std::string::npos)
        {
            if(vv.size() > 0) // last read is weight matrix
            {
                Matrix M(vv);
                __weightMatrix.push_back(M);
                vv.clear();
            }
        }
        else if(line.size() > 0)
        {
            v_line = parseLine(line);
            vv.push_back(v_line);
        }
    }

    // save last bias matrix
    if(vv.size() > 0)
    {
        Matrix M(vv);
        __biasVector.push_back(M);
        vv.clear();
    }

    assert(__weightMatrix.size() == __biasVector.size());
    if(__type == LayerType::fullyConnected || __type == LayerType::output)
    {
        assert(__weightMatrix.size() == 1);
        auto dim = __weightMatrix[0].Dimension();
        //cout<<"fc: load file dim:"<<dim<<endl;
        //cout<<"fc neurons: "<<__n_neurons_fc<<endl;
        assert(dim.first == __n_neurons_fc);
    }
    if(__type == LayerType::cnn || __type == LayerType::pooling)
    {
        assert(__weightMatrix.size() == __n_kernels_cnn);
        auto dim = __weightMatrix[0].Dimension();
        //cout<<"cnn: load file dim: "<<dim<<endl;
        //cout<<"cnn kernel dim: "<<__kernelDim<<endl;
        assert(dim == __kernelDim);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5c8, %rsp            # imm = 0x5C8
movq %rdi, %r14
movq (%rdi), %rax
callq *0x298(%rax)
movl %eax, %ebp
movq (%r14), %rax
movq %r14, %rdi
callq *0x250(%rax)
movl %eax, 0x44(%rsp)
movq (%r14), %rax
movq %r14, %rdi
callq *0x258(%rax)
movl %eax, 0x40(%rsp)
leaq 0x240(%rsp), %rbx
movq %rbx, %rdi
callq 0x42c0
leaq 0x178a9(%rip), %rsi     # 0x290f1
movl $0x21, %edx
movq %rbx, %rdi
callq 0x4280
leaq 0x240(%rsp), %rdi
movl %ebp, %esi
callq 0x43c0
leaq 0x240(%rsp), %rdi
leaq 0x44(%rsp), %rsi
callq 0x1a0a8
leaq 0x240(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x1a186
leaq 0x17884(%rip), %rsi     # 0x29113
leaq 0x240(%rsp), %rdi
movl $0x15, %edx
callq 0x4280
leaq 0x248(%rsp), %rbx
leaq 0xc0(%rsp), %rdi
movq %rbx, %rsi
callq 0x4330
leaq 0x3b8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movl $0x8, %edx
callq 0x4380
leaq 0xd0(%rsp), %r12
movq -0x10(%r12), %rdi
cmpq %r12, %rdi
je 0x118ea
callq 0x4230
movq 0x256e7(%rip), %rdi     # 0x36fd8
leaq 0x17919(%rip), %rsi     # 0x29211
movl $0x19, %edx
callq 0x4280
movq 0x256cf(%rip), %rdi     # 0x36fd8
leaq 0x1791b(%rip), %rsi     # 0x2922b
movl $0x30, %edx
callq 0x4280
movq 0x256b7(%rip), %rdi     # 0x36fd8
movl %ebp, %esi
callq 0x43c0
movq %rax, %r15
leaq 0x1792a(%rip), %rsi     # 0x2925c
movl $0xc, %edx
movq %rax, %rdi
callq 0x4280
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4250
movsbl %al, %esi
movq %r15, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4190
leaq 0x438(%rsp), %rdi
callq 0x4080
testb %al, %al
je 0x11def
leaq 0xa8(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
movq %rsp, %rcx
movq %rax, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx)
leaq 0x78(%r14), %rax
movq %rax, 0x48(%rsp)
movq %r14, 0x88(%rsp)
leaq 0x60(%r14), %rax
movq %rax, 0x90(%rsp)
leaq 0x98(%rsp), %r12
leaq 0x178ed(%rip), %rbp     # 0x292b6
leaq 0x68(%rsp), %r15
leaq 0xc0(%rsp), %rbx
leaq 0xb8(%rsp), %r13
leaq 0x3b8(%rsp), %r14
movq 0x3b8(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4250
movsbl %al, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x4410
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x11c7a
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movq $0x0, 0x60(%rsp)
movl $0x6, %ecx
movq %r12, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x43d0
cmpq $-0x1, %rax
je 0x11ab8
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x11b46
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x1a9c6
movq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x138a4
movq (%rsp), %r14
movq 0x8(%rsp), %r15
cmpq %r14, %r15
je 0x11aa3
movq %r14, %rbp
movq (%rbp), %rdi
testq %rdi, %rdi
je 0x11a8e
callq 0x4230
addq $0x18, %rbp
cmpq %r15, %rbp
jne 0x11a80
movq %r14, 0x8(%rsp)
leaq 0x17813(%rip), %rbp     # 0x292b6
movq %rbx, %rdi
callq 0x1abf2
leaq 0x3b8(%rsp), %r14
jmp 0x11b41
movl $0x4, %ecx
movq %r12, %rdi
leaq 0x177f6(%rip), %rsi     # 0x292bd
xorl %edx, %edx
callq 0x43d0
cmpq $-0x1, %rax
je 0x11b5e
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x11b46
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x1a9c6
movq 0x90(%rsp), %rdi
movq %rbx, %rsi
callq 0x138a4
movq (%rsp), %rbp
movq 0x8(%rsp), %r15
cmpq %rbp, %r15
je 0x11b32
movq %rbp, %r14
movq (%r14), %rdi
testq %rdi, %rdi
je 0x11b1c
callq 0x4230
addq $0x18, %r14
cmpq %r15, %r14
jne 0x11b0f
movq %rbp, 0x8(%rsp)
leaq 0x3b8(%rsp), %r14
movq %rbx, %rdi
callq 0x1abf2
leaq 0x17775(%rip), %rbp     # 0x292b6
leaq 0x68(%rsp), %r15
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x119e6
callq 0x4230
jmp 0x119e6
movq 0xa0(%rsp), %rdx
testq %rdx, %rdx
je 0x11b46
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x98(%rsp), %rsi
addq %rsi, %rdx
movq %r15, %rdi
callq 0x15b72
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movl $0x8, %edx
callq 0x4390
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4170
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x11bf5
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x11be6
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x11ba9
leaq 0x20(%rsp), %rdi
movq %r13, %rdx
callq 0x17694
jmp 0x11ba9
movq %rbx, %rdi
movq 0x253c9(%rip), %rsi     # 0x36fc8
callq 0x42f0
leaq 0x138(%rsp), %rdi
callq 0x40b0
movq 0x50(%rsp), %rdi
movaps 0x20(%rsp), %xmm0
movaps %xmm0, 0x50(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x60(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testq %rdi, %rdi
je 0x11c54
callq 0x4230
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x11c54
callq 0x4230
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x11c68
callq 0x4230
movq %rsp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x1454c
jmp 0x11b46
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
movq 0x88(%rsp), %r12
je 0x11ce8
leaq 0xc0(%rsp), %rdi
movq %rsp, %rsi
callq 0x1a9c6
leaq 0xc0(%rsp), %rsi
movq 0x48(%rsp), %rdi
callq 0x138a4
movq (%rsp), %rbx
movq 0x8(%rsp), %r14
cmpq %rbx, %r14
je 0x11cdb
movq %rbx, %r15
movq (%r15), %rdi
testq %rdi, %rdi
je 0x11ccd
callq 0x4230
addq $0x18, %r15
cmpq %r14, %r15
jne 0x11cc0
movq %rbx, 0x8(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x1abf2
movq 0x60(%r12), %rdi
movq 0x68(%r12), %rax
subq %rdi, %rax
movq 0x80(%r12), %rcx
subq 0x78(%r12), %rcx
cmpq %rcx, %rax
jne 0x11ea7
movl 0xc(%r12), %ecx
testl $0xfffffffb, %ecx       # imm = 0xFFFFFFFB
jne 0x11d37
cmpq $0x18, %rax
jne 0x11ec6
callq 0x1abb8
cmpq 0x10(%r12), %rax
jne 0x11ee5
movl 0xc(%r12), %ecx
decl %ecx
cmpl $0x1, %ecx
ja 0x11d8d
movq 0x60(%r12), %rdi
movq 0x68(%r12), %rax
subq %rdi, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
cmpq 0x18(%r12), %rcx
jne 0x11f04
callq 0x1abb8
movq 0x20(%r12), %r8
movq 0x28(%r12), %rcx
movq %rax, %rdi
movq %rdx, %rsi
movq %r8, %rdx
callq 0x1a5e0
testb %al, %al
je 0x11f23
movq %rsp, %rdi
callq 0x14578
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x11daf
callq 0x4230
leaq 0x3b8(%rsp), %rdi
callq 0x4070
movq 0x2520d(%rip), %rsi     # 0x36fd0
leaq 0x240(%rsp), %rdi
callq 0x4130
leaq 0x2b0(%rsp), %rdi
callq 0x40b0
addq $0x5c8, %rsp            # imm = 0x5C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x251e2(%rip), %rdi     # 0x36fd8
leaq 0x17414(%rip), %rsi     # 0x29211
movl $0x19, %edx
callq 0x4280
movq 0x251ca(%rip), %rdi     # 0x36fd8
leaq 0x1736a(%rip), %rsi     # 0x2917f
movl $0x1a, %edx
callq 0x4280
leaq 0xc0(%rsp), %rdi
movq %rbx, %rsi
callq 0x4330
movq 0xc0(%rsp), %rsi
movq 0xc8(%rsp), %rdx
movq 0x25192(%rip), %rdi     # 0x36fd8
callq 0x4280
movq %rax, %rbx
leaq 0x17414(%rip), %rsi     # 0x29269
movl $0x1a, %edx
movq %rax, %rdi
callq 0x4280
movq %rbx, %rdi
callq 0x4100
movq 0xc0(%rsp), %rdi
cmpq %r12, %rdi
je 0x11e7c
callq 0x4230
movq 0x25155(%rip), %rdi     # 0x36fd8
leaq 0x173fa(%rip), %rsi     # 0x29284
movl $0x31, %edx
callq 0x4280
movq 0x2513d(%rip), %rdi     # 0x36fd8
callq 0x4100
xorl %edi, %edi
callq 0x4290
leaq 0x17414(%rip), %rdi     # 0x292c2
leaq 0x15895(%rip), %rsi     # 0x2774a
leaq 0x17433(%rip), %rcx     # 0x292ef
movl $0x100e, %edx           # imm = 0x100E
callq 0x4160
leaq 0x1745b(%rip), %rdi     # 0x29328
leaq 0x15876(%rip), %rsi     # 0x2774a
leaq 0x17414(%rip), %rcx     # 0x292ef
movl $0x1011, %edx           # imm = 0x1011
callq 0x4160
leaq 0x17457(%rip), %rdi     # 0x29343
leaq 0x15857(%rip), %rsi     # 0x2774a
leaq 0x173f5(%rip), %rcx     # 0x292ef
movl $0x1015, %edx           # imm = 0x1015
callq 0x4160
leaq 0x17454(%rip), %rdi     # 0x2935f
leaq 0x15838(%rip), %rsi     # 0x2774a
leaq 0x173d6(%rip), %rcx     # 0x292ef
movl $0x1019, %edx           # imm = 0x1019
callq 0x4160
leaq 0x1745e(%rip), %rdi     # 0x29388
leaq 0x15819(%rip), %rsi     # 0x2774a
leaq 0x173b7(%rip), %rcx     # 0x292ef
movl $0x101d, %edx           # imm = 0x101D
callq 0x4160
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
cmpq %r12, %rdi
jne 0x12048
jmp 0x1204d
jmp 0x11fb1
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x1abf2
jmp 0x1202b
jmp 0x11fd0
jmp 0x11fd0
jmp 0x11fd0
movq %rax, %rbx
leaq 0xd0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1205a
callq 0x4230
jmp 0x1205a
jmp 0x11fa9
jmp 0x11fb9
movq %rax, %rbx
jmp 0x12008
jmp 0x11fb9
jmp 0x11fb9
jmp 0x11fbe
movq %rax, %rbx
jmp 0x1205a
movq %rax, %rbx
jmp 0x1204d
movq %rax, %rbx
jmp 0x1201c
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x1abf2
jmp 0x1201c
movq %rax, %rbx
jmp 0x1202b
movq %rax, %rbx
movq 0x24fe9(%rip), %rsi     # 0x36fc8
leaq 0xc0(%rsp), %rdi
callq 0x42f0
leaq 0x138(%rsp), %rdi
callq 0x40b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x12008
callq 0x4230
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x1201c
callq 0x4230
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x1202b
callq 0x4230
movq %rsp, %rdi
callq 0x14578
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x1204d
callq 0x4230
leaq 0x3b8(%rsp), %rdi
callq 0x4070
movq 0x24f6f(%rip), %rsi     # 0x36fd0
leaq 0x240(%rsp), %rdi
callq 0x4130
leaq 0x2b0(%rsp), %rdi
callq 0x40b0
movq %rbx, %rdi
callq 0x43e0
nop