// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movq (%rdi), %rax
callq *0x298(%rax)
movl %eax, %ebx
movq (%r12), %rax
movq %r12, %rdi
callq *0x250(%rax)
movl %eax, 0x24(%rsp)
movq (%r12), %rax
movq %r12, %rdi
callq *0x258(%rax)
movl %eax, 0x20(%rsp)
leaq 0x240(%rsp), %r14
movq %r14, %rdi
callq 0x4290
leaq 0x1347f(%rip), %rsi     # 0x23081
movq %r14, %rdi
callq 0x4240
leaq 0x240(%rsp), %rdi
movl %ebx, %esi
callq 0x43a0
leaq 0x240(%rsp), %rdi
leaq 0x24(%rsp), %rsi
callq 0x172e0
leaq 0x240(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x17337
leaq 0x1345f(%rip), %rsi     # 0x230a3
leaq 0x240(%rsp), %rdi
callq 0x4240
leaq 0x248(%rsp), %r14
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0x4310
leaq 0x3b8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x4360
leaq 0xc0(%rsp), %rdi
callq 0x4100
movq 0x23343(%rip), %rdi     # 0x32fd8
leaq 0x13505(%rip), %rsi     # 0x231a1
callq 0x4240
leaq 0x13513(%rip), %rsi     # 0x231bb
movq %rax, %rdi
callq 0x4240
movq %rax, %rdi
movl %ebx, %esi
callq 0x43a0
leaq 0x1352b(%rip), %rsi     # 0x231ec
movq %rax, %rdi
callq 0x4240
movq %rax, %rdi
callq 0x4130
leaq 0x438(%rsp), %rdi
callq 0x4080
testb %al, %al
je 0xffad
leaq 0x88(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movq %rsp, %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x78(%r12), %rax
movq %rax, 0x28(%rsp)
movq %r12, 0x30(%rsp)
leaq 0x60(%r12), %rax
movq %rax, 0x38(%rsp)
leaq 0x78(%rsp), %rbp
leaq 0xc0(%rsp), %rbx
leaq 0x98(%rsp), %r14
leaq 0x40(%rsp), %r12
leaq 0x60(%rsp), %r13
leaq 0x3b8(%rsp), %rdi
movq %rbp, %rsi
callq 0x4060
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xfe9b
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
movq %rbp, %rdi
leaq 0x134cc(%rip), %rsi     # 0x23246
xorl %edx, %edx
callq 0x43e0
cmpq $-0x1, %rax
je 0xfdac
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0xfdf6
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x17906
movq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x1139c
jmp 0xfde6
movq %rbp, %rdi
leaq 0x13497(%rip), %rsi     # 0x2324d
xorl %edx, %edx
callq 0x43e0
cmpq $-0x1, %rax
je 0xfe03
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0xfdf6
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x17906
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x1139c
movq %rsp, %rdi
callq 0x11d2a
movq %rbx, %rdi
callq 0x17a9a
movq %r13, %rdi
callq 0x127dc
jmp 0xfd40
cmpq $0x0, 0x80(%rsp)
je 0xfdf6
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x42a0
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
pushq $0x8
popq %rdx
callq 0x4370
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4190
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xfe63
movq %r12, %rdi
movq %r14, %rsi
callq 0x11d08
jmp 0xfe3d
movq %rbx, %rdi
callq 0x4120
movq %r13, %rdi
movq %r12, %rsi
callq 0x1637e
movq %r12, %rdi
callq 0x127dc
leaq 0xa0(%rsp), %rdi
callq 0x4100
movq %rsp, %rdi
movq %r13, %rsi
callq 0x11d32
jmp 0xfdf6
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
movq 0x30(%rsp), %rbx
je 0xfee2
leaq 0xc0(%rsp), %rdi
movq %rsp, %rsi
callq 0x17906
leaq 0xc0(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x1139c
movq %rsp, %rdi
callq 0x11d2a
leaq 0xc0(%rsp), %rdi
callq 0x17a9a
movq 0x60(%rbx), %rdi
movq 0x68(%rbx), %rax
subq %rdi, %rax
movq 0x80(%rbx), %rcx
subq 0x78(%rbx), %rcx
cmpq %rcx, %rax
jne 0x10038
movl 0xc(%rbx), %ecx
testl $0xfffffffb, %ecx       # imm = 0xFFFFFFFB
jne 0xff28
cmpq $0x18, %rax
jne 0x10057
callq 0x17a6a
cmpq 0x10(%rbx), %rax
jne 0x10076
movl 0xc(%rbx), %ecx
decl %ecx
cmpl $0x1, %ecx
ja 0xff6f
movq 0x60(%rbx), %rdi
movq 0x68(%rbx), %rax
subq %rdi, %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
cmpq 0x18(%rbx), %rax
jne 0x10095
callq 0x17a6a
movq 0x20(%rbx), %r8
movq 0x28(%rbx), %rcx
movq %rax, %rdi
movq %rdx, %rsi
movq %r8, %rdx
callq 0x175e8
testb %al, %al
je 0x100b4
movq %rsp, %rdi
callq 0x11d5e
leaq 0x78(%rsp), %rdi
callq 0x4100
leaq 0x3b8(%rsp), %rdi
callq 0x4070
leaq 0x240(%rsp), %rdi
callq 0x40a0
addq $0x5c8, %rsp            # imm = 0x5C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x23024(%rip), %rdi     # 0x32fd8
leaq 0x131e6(%rip), %rsi     # 0x231a1
callq 0x4240
leaq 0x13148(%rip), %rsi     # 0x2310f
movq %rax, %rdi
callq 0x4240
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0x4310
leaq 0xc0(%rsp), %rsi
movq %rbx, %rdi
callq 0x41f0
leaq 0x13200(%rip), %rsi     # 0x231f9
movq %rax, %rdi
callq 0x4240
movq %rax, %rdi
callq 0x4130
leaq 0xc0(%rsp), %rdi
callq 0x4100
movq 0x22fbb(%rip), %rdi     # 0x32fd8
leaq 0x131f0(%rip), %rsi     # 0x23214
callq 0x4240
movq %rax, %rdi
callq 0x4130
xorl %edi, %edi
callq 0x4260
leaq 0x13213(%rip), %rdi     # 0x23252
leaq 0x11694(%rip), %rsi     # 0x216da
leaq 0x13232(%rip), %rcx     # 0x2327f
movl $0x100e, %edx           # imm = 0x100E
callq 0x4180
leaq 0x1325a(%rip), %rdi     # 0x232b8
leaq 0x11675(%rip), %rsi     # 0x216da
leaq 0x13213(%rip), %rcx     # 0x2327f
movl $0x1011, %edx           # imm = 0x1011
callq 0x4180
leaq 0x13256(%rip), %rdi     # 0x232d3
leaq 0x11656(%rip), %rsi     # 0x216da
leaq 0x131f4(%rip), %rcx     # 0x2327f
movl $0x1015, %edx           # imm = 0x1015
callq 0x4180
leaq 0x13253(%rip), %rdi     # 0x232ef
leaq 0x11637(%rip), %rsi     # 0x216da
leaq 0x131d5(%rip), %rcx     # 0x2327f
movl $0x1019, %edx           # imm = 0x1019
callq 0x4180
leaq 0x1325d(%rip), %rdi     # 0x23318
leaq 0x11618(%rip), %rsi     # 0x216da
leaq 0x131b6(%rip), %rcx     # 0x2327f
movl $0x101d, %edx           # imm = 0x101D
callq 0x4180
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
jmp 0x10188
jmp 0x10129
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x17a9a
jmp 0x1017b
jmp 0x10145
jmp 0x10145
jmp 0x10145
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x4100
jmp 0x1019a
jmp 0x10124
jmp 0x10140
movq %rax, %rbx
jmp 0x1015a
jmp 0x10140
jmp 0x1012e
jmp 0x10140
movq %rax, %rbx
jmp 0x1019a
movq %rax, %rbx
jmp 0x1018d
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x17a9a
jmp 0x10171
movq %rax, %rbx
jmp 0x10171
movq %rax, %rbx
jmp 0x1017b
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x4120
leaq 0x40(%rsp), %rdi
callq 0x127dc
leaq 0xa0(%rsp), %rdi
callq 0x4100
leaq 0x60(%rsp), %rdi
callq 0x127dc
movq %rsp, %rdi
callq 0x11d5e
leaq 0x78(%rsp), %rdi
callq 0x4100
leaq 0x3b8(%rsp), %rdi
callq 0x4070
leaq 0x240(%rsp), %rdi
callq 0x40a0
movq %rbx, %rdi
callq 0x43b0
nop