// File: /workspace/github_cpp_makefile_star2_O1/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, 0x3c(%rsp)
movq (%r14), %rax
movq %r14, %rdi
callq *0x258(%rax)
movl %eax, 0x38(%rsp)
leaq 0x240(%rsp), %rbx
movq %rbx, %rdi
callq 0x42c0
leaq 0x177a0(%rip), %rsi     # 0x29130
movl $0x21, %edx
movq %rbx, %rdi
callq 0x4280
leaq 0x240(%rsp), %rdi
movl %ebp, %esi
callq 0x43c0
leaq 0x240(%rsp), %rdi
leaq 0x3c(%rsp), %rsi
callq 0x1a124
leaq 0x240(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x1a202
leaq 0x1777b(%rip), %rsi     # 0x29152
leaq 0x240(%rsp), %rdi
movl $0x15, %edx
callq 0x4280
leaq 0x248(%rsp), %rbx
leaq 0xc0(%rsp), %rdi
movq %rbx, %rsi
callq 0x4330
movq %r14, 0x60(%rsp)
leaq 0x3b8(%rsp), %r13
leaq 0xc0(%rsp), %rsi
movq %r13, %rdi
movl $0x8, %edx
callq 0x4380
leaq 0xd0(%rsp), %r12
movq -0x10(%r12), %rdi
cmpq %r12, %rdi
je 0x11a3a
callq 0x4230
movq 0x25597(%rip), %rdi     # 0x36fd8
leaq 0x17808(%rip), %rsi     # 0x29250
movl $0x19, %edx
callq 0x4280
movq 0x2557f(%rip), %rdi     # 0x36fd8
leaq 0x1780a(%rip), %rsi     # 0x2926a
movl $0x30, %edx
callq 0x4280
movq 0x25567(%rip), %rdi     # 0x36fd8
movl %ebp, %esi
callq 0x43c0
movq %rax, %r15
leaq 0x17819(%rip), %rsi     # 0x2929b
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 0x11f44
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)
movq 0x60(%rsp), %rax
leaq 0x78(%rax), %rcx
movq %rcx, 0x68(%rsp)
addq $0x60, %rax
movq %rax, 0x90(%rsp)
leaq 0x98(%rsp), %rbp
leaq 0x177df(%rip), %r14     # 0x292f5
leaq 0x70(%rsp), %r15
leaq 0xc0(%rsp), %rbx
leaq 0xb8(%rsp), %r12
movq 0x3b8(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4250
movsbl %al, %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x4410
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x11dd4
xorps %xmm0, %xmm0
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
movl $0x6, %ecx
movq %rbp, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x43d0
cmpq $-0x1, %rax
je 0x11bfd
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x11c8b
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x1aa86
movq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x13a14
movq (%rsp), %r14
movq 0x8(%rsp), %r15
cmpq %r14, %r15
je 0x11be9
movq %r14, %r13
movq (%r13), %rdi
testq %rdi, %rdi
je 0x11bd3
callq 0x4230
addq $0x18, %r13
cmpq %r15, %r13
jne 0x11bc5
movq %r14, 0x8(%rsp)
leaq 0x3b8(%rsp), %r13
movq %rbx, %rdi
callq 0x1acb6
leaq 0x176fd(%rip), %r14     # 0x292f5
jmp 0x11c86
movl $0x4, %ecx
movq %rbp, %rdi
leaq 0x176f0(%rip), %rsi     # 0x292fc
xorl %edx, %edx
callq 0x43d0
cmpq $-0x1, %rax
je 0x11ca3
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
je 0x11c8b
movq %rbx, %rdi
movq %rsp, %rsi
callq 0x1aa86
movq 0x90(%rsp), %rdi
movq %rbx, %rsi
callq 0x13a14
movq (%rsp), %r13
movq 0x8(%rsp), %r15
cmpq %r13, %r15
je 0x11c76
movq %r13, %r14
movq (%r14), %rdi
testq %rdi, %rdi
je 0x11c61
callq 0x4230
addq $0x18, %r14
cmpq %r15, %r14
jne 0x11c54
movq %r13, 0x8(%rsp)
leaq 0x1767f(%rip), %r14     # 0x292f5
movq %rbx, %rdi
callq 0x1acb6
leaq 0x3b8(%rsp), %r13
leaq 0x70(%rsp), %r15
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x11b2b
callq 0x4230
jmp 0x11b2b
movq 0xa0(%rsp), %rdx
testq %rdx, %rdx
je 0x11c8b
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x98(%rsp), %rsi
addq %rsi, %rdx
movq %r15, %rdi
callq 0x15ca8
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 %r12, %rsi
callq 0x4170
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x11d3d
movq 0x28(%rsp), %rsi
cmpq 0x30(%rsp), %rsi
je 0x11d2e
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x28(%rsp)
jmp 0x11cf1
leaq 0x20(%rsp), %rdi
movq %r12, %rdx
callq 0x17770
jmp 0x11cf1
movq %rbx, %rdi
movq 0x25281(%rip), %rsi     # 0x36fc8
callq 0x42f0
leaq 0x138(%rsp), %rdi
callq 0x40b0
movq 0x40(%rsp), %rdi
movaps 0x20(%rsp), %xmm0
movq 0x30(%rsp), %rax
movq %rdi, 0x20(%rsp)
movups 0x48(%rsp), %xmm1
movaps %xmm0, 0x40(%rsp)
movq %rax, 0x50(%rsp)
movups %xmm1, 0x28(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testq %rdi, %rdi
je 0x11d9c
callq 0x4230
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x11dab
callq 0x4230
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x11dc2
callq 0x4230
movq %rsp, %rdi
leaq 0x40(%rsp), %rsi
callq 0x1468c
jmp 0x11c8b
movq 0x8(%rsp), %rax
cmpq (%rsp), %rax
movq 0x60(%rsp), %r12
je 0x11e3f
leaq 0xc0(%rsp), %rdi
movq %rsp, %rsi
callq 0x1aa86
leaq 0xc0(%rsp), %rsi
movq 0x68(%rsp), %rdi
callq 0x13a14
movq (%rsp), %rbx
movq 0x8(%rsp), %r14
cmpq %rbx, %r14
je 0x11e32
movq %rbx, %r15
movq (%r15), %rdi
testq %rdi, %rdi
je 0x11e24
callq 0x4230
addq $0x18, %r15
cmpq %r14, %r15
jne 0x11e17
movq %rbx, 0x8(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x1acb6
movq 0x60(%r12), %rdi
movq 0x68(%r12), %rax
subq %rdi, %rax
movq 0x80(%r12), %rcx
subq 0x78(%r12), %rcx
cmpq %rcx, %rax
jne 0x11ffc
testl $0xfffffffb, 0xc(%r12)  # imm = 0xFFFFFFFB
jne 0x11e87
cmpq $0x18, %rax
jne 0x1201b
callq 0x1ac7c
cmpq 0x10(%r12), %rax
jne 0x1203a
movl 0xc(%r12), %eax
decl %eax
cmpl $0x1, %eax
ja 0x11ee2
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 0x12059
callq 0x1ac7c
movq 0x20(%r12), %r8
movq 0x28(%r12), %rcx
movq %rax, %rdi
movq %rdx, %rsi
movq %r8, %rdx
callq 0x1a680
testb %al, %al
je 0x12078
movq %rsp, %rdi
callq 0x146b8
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x11f04
callq 0x4230
leaq 0x3b8(%rsp), %rdi
callq 0x4070
movq 0x250b8(%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 0x2508d(%rip), %rdi     # 0x36fd8
leaq 0x172fe(%rip), %rsi     # 0x29250
movl $0x19, %edx
callq 0x4280
movq 0x25075(%rip), %rdi     # 0x36fd8
leaq 0x17254(%rip), %rsi     # 0x291be
movl $0x1a, %edx
callq 0x4280
leaq 0xc0(%rsp), %rdi
movq %rbx, %rsi
callq 0x4330
movq 0xc0(%rsp), %rsi
movq 0xc8(%rsp), %rdx
movq 0x2503d(%rip), %rdi     # 0x36fd8
callq 0x4280
movq %rax, %rbx
leaq 0x172fe(%rip), %rsi     # 0x292a8
movl $0x1a, %edx
movq %rax, %rdi
callq 0x4280
movq %rbx, %rdi
callq 0x4100
movq 0xc0(%rsp), %rdi
cmpq %r12, %rdi
je 0x11fd1
callq 0x4230
movq 0x25000(%rip), %rdi     # 0x36fd8
leaq 0x172e4(%rip), %rsi     # 0x292c3
movl $0x31, %edx
callq 0x4280
movq 0x24fe8(%rip), %rdi     # 0x36fd8
callq 0x4100
xorl %edi, %edi
callq 0x4290
leaq 0x172fe(%rip), %rdi     # 0x29301
leaq 0x15740(%rip), %rsi     # 0x2774a
leaq 0x1731d(%rip), %rcx     # 0x2932e
movl $0x100e, %edx           # imm = 0x100E
callq 0x4160
leaq 0x17345(%rip), %rdi     # 0x29367
leaq 0x15721(%rip), %rsi     # 0x2774a
leaq 0x172fe(%rip), %rcx     # 0x2932e
movl $0x1011, %edx           # imm = 0x1011
callq 0x4160
leaq 0x17341(%rip), %rdi     # 0x29382
leaq 0x15702(%rip), %rsi     # 0x2774a
leaq 0x172df(%rip), %rcx     # 0x2932e
movl $0x1015, %edx           # imm = 0x1015
callq 0x4160
leaq 0x1733e(%rip), %rdi     # 0x2939e
leaq 0x156e3(%rip), %rsi     # 0x2774a
leaq 0x172c0(%rip), %rcx     # 0x2932e
movl $0x1019, %edx           # imm = 0x1019
callq 0x4160
leaq 0x17348(%rip), %rdi     # 0x293c7
leaq 0x156c4(%rip), %rsi     # 0x2774a
leaq 0x172a1(%rip), %rcx     # 0x2932e
movl $0x101d, %edx           # imm = 0x101D
callq 0x4160
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
cmpq %r12, %rdi
jne 0x121a0
jmp 0x121a5
jmp 0x12106
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x1acb6
jmp 0x12183
jmp 0x12125
jmp 0x12125
jmp 0x12125
movq %rax, %rbx
leaq 0xd0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x121b2
callq 0x4230
jmp 0x121b2
jmp 0x120fe
jmp 0x1210e
movq %rax, %rbx
jmp 0x1214e
jmp 0x1210e
jmp 0x1210e
jmp 0x12113
movq %rax, %rbx
jmp 0x121b2
movq %rax, %rbx
jmp 0x121a5
movq %rax, %rbx
jmp 0x12174
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x1acb6
jmp 0x12174
movq %rax, %rbx
jmp 0x12183
movq %rax, %rbx
movq 0x24e94(%rip), %rsi     # 0x36fc8
leaq 0xc0(%rsp), %rdi
callq 0x42f0
leaq 0x138(%rsp), %rdi
callq 0x40b0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x1215d
callq 0x4230
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x12174
callq 0x4230
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x12183
callq 0x4230
movq %rsp, %rdi
callq 0x146b8
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0x121a5
callq 0x4230
leaq 0x3b8(%rsp), %rdi
callq 0x4070
movq 0x24e17(%rip), %rsi     # 0x36fd0
leaq 0x240(%rsp), %rdi
callq 0x4130
leaq 0x2b0(%rsp), %rdi
callq 0x40b0
movq %rbx, %rdi
callq 0x43e0
nop