// File: 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:
subq $0x5b8, %rsp            # imm = 0x5B8
movq %rdi, 0x5b0(%rsp)
movq 0x5b0(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
movq (%rdi), %rax
movq 0x298(%rax), %rax
callq *%rax
movq 0xa0(%rsp), %rdi
movl %eax, 0x5ac(%rsp)
movq (%rdi), %rax
movq 0x250(%rax), %rax
callq *%rax
movq 0xa0(%rsp), %rdi
movl %eax, 0x5a8(%rsp)
movq (%rdi), %rax
movq 0x258(%rax), %rax
callq *%rax
movl %eax, 0x5a4(%rsp)
leaq 0x428(%rsp), %rdi
movq %rdi, 0xa8(%rsp)
callq 0x42d0
movq 0xa8(%rsp), %rdi
leaq 0x38b7c(%rip), %rsi     # 0x53bb8
callq 0x4230
jmp 0x1b043
movl 0x5ac(%rsp), %esi
leaq 0x428(%rsp), %rdi
callq 0x43e0
jmp 0x1b059
leaq 0x428(%rsp), %rdi
leaq 0x5a8(%rsp), %rsi
callq 0x3aa10
jmp 0x1b070
leaq 0x428(%rsp), %rdi
leaq 0x5a4(%rsp), %rsi
callq 0x3aba0
jmp 0x1b087
leaq 0x38b4c(%rip), %rsi     # 0x53bda
leaq 0x428(%rsp), %rdi
callq 0x4230
jmp 0x1b09d
leaq 0x1e8(%rsp), %rdi
leaq 0x428(%rsp), %rsi
callq 0x4250
jmp 0x1b0b4
leaq 0x208(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
movl $0x8, %edx
callq 0x4390
jmp 0x1b0d0
leaq 0x1e8(%rsp), %rdi
callq 0x4160
movq 0x4fef4(%rip), %rdi     # 0x6afd8
leaq 0x38bed(%rip), %rsi     # 0x53cd8
callq 0x4230
movq %rax, 0x98(%rsp)
jmp 0x1b0fa
movq 0x98(%rsp), %rdi
leaq 0x38be9(%rip), %rsi     # 0x53cf2
callq 0x4230
movq %rax, 0x90(%rsp)
jmp 0x1b118
movq 0x90(%rsp), %rdi
movl 0x5ac(%rsp), %esi
callq 0x43e0
movq %rax, 0x88(%rsp)
jmp 0x1b136
movq 0x88(%rsp), %rdi
leaq 0x38bde(%rip), %rsi     # 0x53d23
callq 0x4230
movq %rax, 0x80(%rsp)
jmp 0x1b154
movq 0x80(%rsp), %rdi
movq 0x4fe4d(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x1b16a
leaq 0x208(%rsp), %rdi
callq 0x43d0
movb %al, 0x7f(%rsp)
jmp 0x1b17d
movb 0x7f(%rsp), %al
testb $0x1, %al
jne 0x1b2d7
jmp 0x1b18b
movq 0x4fe46(%rip), %rdi     # 0x6afd8
leaq 0x38b3f(%rip), %rsi     # 0x53cd8
callq 0x4230
movq %rax, 0x70(%rsp)
jmp 0x1b1a5
movq 0x70(%rsp), %rdi
leaq 0x38a95(%rip), %rsi     # 0x53c46
callq 0x4230
movq %rax, 0x68(%rsp)
jmp 0x1b1bd
leaq 0x1c8(%rsp), %rdi
leaq 0x428(%rsp), %rsi
callq 0x4250
jmp 0x1b1d4
movq 0x68(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x41f0
movq %rax, 0x60(%rsp)
jmp 0x1b1ed
movq 0x60(%rsp), %rdi
leaq 0x38b37(%rip), %rsi     # 0x53d30
callq 0x4230
movq %rax, 0x58(%rsp)
jmp 0x1b205
movq 0x58(%rsp), %rdi
movq 0x4fd9f(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x1b218
leaq 0x1c8(%rsp), %rdi
callq 0x4160
movq 0x4fdac(%rip), %rdi     # 0x6afd8
leaq 0x38b18(%rip), %rsi     # 0x53d4b
callq 0x4230
movq %rax, 0x50(%rsp)
jmp 0x1b23f
movq 0x50(%rsp), %rdi
movq 0x4fd65(%rip), %rsi     # 0x6afb0
callq 0x4260
jmp 0x1b252
xorl %edi, %edi
callq 0x4290
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x420(%rsp)
movl %eax, 0x41c(%rsp)
jmp 0x1b8e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x420(%rsp)
movl %eax, 0x41c(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0x4160
jmp 0x1b8e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x420(%rsp)
movl %eax, 0x41c(%rsp)
jmp 0x1b8dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x420(%rsp)
movl %eax, 0x41c(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x4160
jmp 0x1b8dc
leaq 0x1a0(%rsp), %rdi
callq 0x42f0
leaq 0x188(%rsp), %rdi
callq 0x21e70
leaq 0x208(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x4060
movq %rax, 0x48(%rsp)
jmp 0x1b30d
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x42a0
movb %al, 0x47(%rsp)
jmp 0x1b327
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x1b334
jmp 0x1b5b7
leaq 0x170(%rsp), %rdi
callq 0x1f090
leaq 0x1a0(%rsp), %rdi
leaq 0x38a2d(%rip), %rsi     # 0x53d7d
xorl %eax, %eax
movl %eax, %edx
callq 0x4450
cmpq $-0x1, %rax
je 0x1b425
leaq 0x188(%rsp), %rdi
callq 0x21e80
cmpq $0x0, %rax
jbe 0x1b420
leaq 0x158(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x3b770
jmp 0x1b391
movq 0xa0(%rsp), %rdi
addq $0x78, %rdi
leaq 0x158(%rsp), %rsi
callq 0x1fa50
jmp 0x1b3ac
leaq 0x188(%rsp), %rdi
callq 0x21ea0
leaq 0x158(%rsp), %rdi
callq 0x3bb80
jmp 0x1b420
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x420(%rsp)
movl %eax, 0x41c(%rsp)
jmp 0x1b8c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x420(%rsp)
movl %eax, 0x41c(%rsp)
jmp 0x1b5a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x420(%rsp)
movl %eax, 0x41c(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x3bb80
jmp 0x1b5a5
jmp 0x1b593
leaq 0x1a0(%rsp), %rdi
leaq 0x38950(%rip), %rsi     # 0x53d84
xorl %eax, %eax
movl %eax, %edx
callq 0x4450
cmpq $-0x1, %rax
je 0x1b4d3
leaq 0x188(%rsp), %rdi
callq 0x21e80
cmpq $0x0, %rax
jbe 0x1b4ce
leaq 0x140(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x3b770
jmp 0x1b471
movq 0xa0(%rsp), %rdi
addq $0x60, %rdi
leaq 0x140(%rsp), %rsi
callq 0x1fa50
jmp 0x1b48c
leaq 0x188(%rsp), %rdi
callq 0x21ea0
leaq 0x140(%rsp), %rdi
callq 0x3bb80
jmp 0x1b4ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x420(%rsp)
movl %eax, 0x41c(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x3bb80
jmp 0x1b5a5
jmp 0x1b591
leaq 0x1a0(%rsp), %rdi
callq 0x4180
cmpq $0x0, %rax
jbe 0x1b58f
leaq 0x108(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x40f0
jmp 0x1b501
leaq 0x128(%rsp), %rdi
leaq 0x1c7(%rsp), %rsi
leaq 0x108(%rsp), %rdx
callq 0x1b910
jmp 0x1b520
leaq 0x170(%rsp), %rdi
movq %rdi, 0x38(%rsp)
leaq 0x128(%rsp), %rsi
movq %rsi, 0x30(%rsp)
callq 0x21ec0
movq 0x30(%rsp), %rdi
callq 0x1f190
leaq 0x108(%rsp), %rdi
callq 0x4160
movq 0x38(%rsp), %rsi
leaq 0x188(%rsp), %rdi
callq 0x21f00
jmp 0x1b56a
jmp 0x1b58f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x420(%rsp)
movl %eax, 0x41c(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x4160
jmp 0x1b5a5
jmp 0x1b591
jmp 0x1b593
leaq 0x170(%rsp), %rdi
callq 0x1f190
jmp 0x1b2f1
leaq 0x170(%rsp), %rdi
callq 0x1f190
jmp 0x1b8c2
leaq 0x188(%rsp), %rdi
callq 0x21e80
cmpq $0x0, %rax
jbe 0x1b63e
leaq 0xf0(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x3b770
jmp 0x1b5e1
movq 0xa0(%rsp), %rdi
addq $0x78, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x1fa50
jmp 0x1b5fc
leaq 0x188(%rsp), %rdi
callq 0x21ea0
leaq 0xf0(%rsp), %rdi
callq 0x3bb80
jmp 0x1b63e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x420(%rsp)
movl %eax, 0x41c(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x3bb80
jmp 0x1b8c2
movq 0xa0(%rsp), %rdi
addq $0x60, %rdi
callq 0x1f9c0
movq 0xa0(%rsp), %rdi
movq %rax, 0x28(%rsp)
addq $0x78, %rdi
callq 0x1f9c0
movq %rax, %rcx
movq 0x28(%rsp), %rax
cmpq %rcx, %rax
jne 0x1b674
jmp 0x1b693
leaq 0x3870e(%rip), %rdi     # 0x53d89
leaq 0x36b50(%rip), %rsi     # 0x521d2
movl $0x100e, %edx           # imm = 0x100E
leaq 0x38728(%rip), %rcx     # 0x53db6
callq 0x4170
movq 0xa0(%rsp), %rax
cmpl $0x0, 0xc(%rax)
je 0x1b6b3
movq 0xa0(%rsp), %rax
cmpl $0x4, 0xc(%rax)
jne 0x1b767
movq 0xa0(%rsp), %rdi
addq $0x60, %rdi
callq 0x1f9c0
cmpq $0x1, %rax
jne 0x1b6cc
jmp 0x1b6eb
leaq 0x3871c(%rip), %rdi     # 0x53def
leaq 0x36af8(%rip), %rsi     # 0x521d2
movl $0x1011, %edx           # imm = 0x1011
leaq 0x386d0(%rip), %rcx     # 0x53db6
callq 0x4170
movq 0xa0(%rsp), %rdi
addq $0x60, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f9e0
movq %rax, %rdi
callq 0x3bae0
movq %rdx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
jmp 0x1b714
movq 0xa0(%rsp), %rcx
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rdx
movq %rdx, 0xe0(%rsp)
movq %rax, 0xe8(%rsp)
movq 0xe0(%rsp), %rax
cmpq 0x10(%rcx), %rax
jne 0x1b746
jmp 0x1b765
leaq 0x386bd(%rip), %rdi     # 0x53e0a
leaq 0x36a7e(%rip), %rsi     # 0x521d2
movl $0x1015, %edx           # imm = 0x1015
leaq 0x38656(%rip), %rcx     # 0x53db6
callq 0x4170
jmp 0x1b767
movq 0xa0(%rsp), %rax
cmpl $0x1, 0xc(%rax)
je 0x1b787
movq 0xa0(%rsp), %rax
cmpl $0x2, 0xc(%rax)
jne 0x1b886
movq 0xa0(%rsp), %rdi
addq $0x60, %rdi
callq 0x1f9c0
movq 0xa0(%rsp), %rcx
cmpq 0x18(%rcx), %rax
jne 0x1b7a8
jmp 0x1b7c7
leaq 0x38677(%rip), %rdi     # 0x53e26
leaq 0x36a1c(%rip), %rsi     # 0x521d2
movl $0x1019, %edx           # imm = 0x1019
leaq 0x385f4(%rip), %rcx     # 0x53db6
callq 0x4170
movq 0xa0(%rsp), %rdi
addq $0x60, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f9e0
movq %rax, %rdi
callq 0x3bae0
movq %rdx, 0x8(%rsp)
movq %rax, 0x10(%rsp)
jmp 0x1b7f0
movq 0xa0(%rsp), %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdx
movq %rdx, 0xd0(%rsp)
movq %rcx, 0xd8(%rsp)
movups 0xd0(%rsp), %xmm0
movaps %xmm0, 0xc0(%rsp)
movups 0x20(%rax), %xmm0
movaps %xmm0, 0xb0(%rsp)
movq 0xc0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
movq 0xb8(%rsp), %rcx
callq 0x3b180
movb %al, 0x7(%rsp)
jmp 0x1b859
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x1b863
jmp 0x1b865
jmp 0x1b884
leaq 0x385e3(%rip), %rdi     # 0x53e4f
leaq 0x3695f(%rip), %rsi     # 0x521d2
movl $0x101d, %edx           # imm = 0x101D
leaq 0x38537(%rip), %rcx     # 0x53db6
callq 0x4170
jmp 0x1b886
leaq 0x188(%rsp), %rdi
callq 0x21f70
leaq 0x1a0(%rsp), %rdi
callq 0x4160
leaq 0x208(%rsp), %rdi
callq 0x4070
leaq 0x428(%rsp), %rdi
callq 0x4090
addq $0x5b8, %rsp            # imm = 0x5B8
retq
leaq 0x188(%rsp), %rdi
callq 0x21f70
leaq 0x1a0(%rsp), %rdi
callq 0x4160
leaq 0x208(%rsp), %rdi
callq 0x4070
leaq 0x428(%rsp), %rdi
callq 0x4090
movq 0x420(%rsp), %rdi
callq 0x43f0
nopw %cs:(%rax,%rax)