// File: /workspace/github_cpp_makefile_star2_O3/xbai0624[P]dnn/src/ConstructLayer.cpp
// Function: ConstructLayer::ComputeCostInOutputLayerForCurrentSample(int)
void ConstructLayer::ComputeCostInOutputLayerForCurrentSample(int sample_index)
{
    if(__type != LayerType::output)
    {
        std::cout<<"Error: ComputeCostInOutputLayerForCurrentSample() only works for output layer."
            <<std::endl;
        std::exit(0);
    }

    // --- 2) compute the cost function C(a_i, y_i)
    Images & sample_image = __imageA[sample_index]; // output layer drop out is not used for sure, so use __imageA is OK.
    assert(sample_image.GetNumberOfKernels() == 1); // output layer must be a fully connected layer, so one kernel
    // now get a_i
    Matrix & sample_A = sample_image.OutputImageFromKernel[0];
    assert(sample_A.Dimension().second == 1); // must be a collum matrix
    //cout<<sample_A<<endl;

    //assert(sample_number >= 1); // obsolete
    //Matrix sample_label = (__p_data_interface->GetCurrentBatchLabel())[sample_number-1];
    Matrix & sample_label = (__p_data_interface->GetCurrentBatchLabel())[sample_index];
    assert(sample_label.Dimension()  == sample_A.Dimension());

    double cost = 0.;
    if(__cost_func_type == CostFuncType::cross_entropy)
    {
        cost = cross_entropy(sample_A, sample_label);
    }
    else if(__cost_func_type == CostFuncType::log_likelihood)
    {
        cost = log_likelihood(sample_A, sample_label);
    }
    else if(__cost_func_type == CostFuncType::quadratic_sum)
    {
        cost = quadratic_sum(sample_A, sample_label);
    }
    else {
        std::cout<<"Error: cost function only supports cross_entropy, loglikelihood, quadratic_sum"
            <<std::endl;
        exit(0);
    }
    // push cost for current sample to memory
    //__outputLayerCost.push_back(cost);
    __outputLayerCost[sample_index]= cost;
    //cout<<"sample cost: "<<cost<<endl;
    //getchar();


    // --- 3) then calculate delta: delta = delta(a_i, y_i) for this sample
    // -------- please note: this function only calculate \delta for current sample, 
    // ----------- when doing back propagation for hidden layers, only delta with the same sample_index should be updated 
    // ----------- the overall delta for this batch should be an average of all deltas in this batch
    // ----------- this is due to the characteristic of Cost function, which is an averaged sum over all samples

    Matrix delta = Matrix(sample_A.Dimension());
    if(__cost_func_type == CostFuncType::cross_entropy)
    {
        delta = sample_A - sample_label; // softmax and sigmoid all have this form
    }
    else if(__cost_func_type == CostFuncType::log_likelihood)
    {
        delta = sample_A - sample_label;
    }
    else if(__cost_func_type == CostFuncType::quadratic_sum)
    {
        delta = sample_A - sample_label; // this is a place holder, not used
    }
    else {
        std::cout<<"Error: cost function only supports cross_entropy, loglikelihood, quadratic_sum"
            <<std::endl;
        exit(0);
    }
    // push cost for current sample to memory
    Images images_delta_from_current_sample;
    images_delta_from_current_sample.OutputImageFromKernel.push_back(delta); // only one kernel in fc layer
    __imageDelta[sample_index]=images_delta_from_current_sample;
    __imageDeltaFull[sample_index]=images_delta_from_current_sample; // in output layer, dropout is not used for sure
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
cmpl $0x4, 0xc(%rdi)
jne 0xad80
movq %rdi, %rbx
movslq %esi, %rcx
movq 0x138(%rdi), %rax
leaq (%rcx,%rcx,2), %r14
movq (%rax,%r14,8), %r15
movq 0x8(%rax,%r14,8), %rax
subq %r15, %rax
cmpq $0x18, %rax
jne 0xad90
movq %rcx, 0x48(%rsp)
movq %r15, %rdi
callq 0x1abb8
cmpq $0x1, %rdx
jne 0xadaf
movq 0x400(%rbx), %rax
shlq $0x3, %r14
movq 0x28(%rax), %r12
addq %r14, %r12
movq %r12, %rdi
callq 0x1abb8
movq %rax, %r13
movq %rdx, %rbp
movq %r15, %rdi
callq 0x1abb8
movq %rdx, %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x1a5e0
testb %al, %al
je 0xadce
movl 0x3e8(%rbx), %eax
xorpd %xmm0, %xmm0
cmpl $0x2, %eax
je 0xab9d
cmpl $0x1, %eax
je 0xac6d
testl %eax, %eax
jne 0xaded
movq %r15, %rdi
callq 0x1abb8
movq %rax, %r13
movq %rdx, %rbp
movq %r12, %rdi
callq 0x1abb8
movq %rdx, %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x1a5e0
testb %al, %al
je 0xae35
cmpq $0x1, %rbp
xorpd %xmm0, %xmm0
jne 0xae73
testq %r13, %r13
je 0xac6d
xorl %ebp, %ebp
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1adb2
movq (%rax), %rax
movsd 0x1caa1(%rip), %xmm0    # 0x275b8
ucomisd (%rax), %xmm0
jae 0xab7e
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1adb2
movq (%rax), %rax
movsd (%rax), %xmm0
ucomisd 0x1ca89(%rip), %xmm0    # 0x275c0
jae 0xab7e
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1adb2
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1adb2
movq (%rax), %rax
movsd (%rax), %xmm0
callq 0x41e0
mulsd 0x8(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
jmp 0xab8c
movsd 0x28(%rsp), %xmm0
addsd 0x1ca04(%rip), %xmm0    # 0x27590
incq %rbp
cmpq %rbp, %r13
jne 0xaafb
jmp 0xac6d
movq %r15, %rdi
callq 0x1abb8
movq %rax, %r13
movq %rdx, %rbp
movq %r12, %rdi
callq 0x1abb8
movq %rdx, %rcx
movq %r13, %rdi
movq %rbp, %rsi
movq %rax, %rdx
callq 0x1a5e0
testb %al, %al
je 0xae54
cmpq $0x1, %rbp
jne 0xae92
testq %r13, %r13
xorpd %xmm0, %xmm0
je 0xac6d
xorl %ebp, %ebp
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1adb2
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1adb2
movq (%rax), %rax
movsd 0x8(%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1adb2
movq (%rax), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1adb2
movsd 0x28(%rsp), %xmm0
movq (%rax), %rax
movsd 0x50(%rsp), %xmm1
subsd (%rax), %xmm1
mulsd 0x8(%rsp), %xmm1
addsd %xmm1, %xmm0
incq %rbp
cmpq %rbp, %r13
jne 0xabe5
movq 0x1f8(%rbx), %rax
movq 0x48(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq %r15, %rdi
callq 0x1abb8
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x1a800
movl 0x3e8(%rbx), %eax
cmpl $0x2, %eax
je 0xaced
cmpl $0x1, %eax
je 0xaccc
testl %eax, %eax
jne 0xae0f
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1b0ec
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14e2c
jmp 0xad0c
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1b0ec
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14e2c
jmp 0xad0c
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x1b0ec
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x14e2c
leaq 0x10(%rsp), %rdi
callq 0x1abf2
xorpd %xmm0, %xmm0
leaq 0x10(%rsp), %rdi
movapd %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
leaq 0x30(%rsp), %rsi
callq 0x138a4
movq 0x180(%rbx), %rdi
addq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x14112
addq 0x1e0(%rbx), %r14
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x14112
leaq 0x10(%rsp), %rdi
callq 0x1353a
leaq 0x30(%rsp), %rdi
callq 0x1abf2
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x2c251(%rip), %rdi     # 0x36fd8
leaq 0x1cda0(%rip), %rsi     # 0x27b2e
jmp 0xadfb
leaq 0x1cde6(%rip), %rdi     # 0x27b7d
leaq 0x1c9ac(%rip), %rsi     # 0x2774a
leaq 0x1cdff(%rip), %rcx     # 0x27ba4
movl $0x591, %edx            # imm = 0x591
callq 0x4160
leaq 0x1ce39(%rip), %rdi     # 0x27bef
leaq 0x1c98d(%rip), %rsi     # 0x2774a
leaq 0x1cde0(%rip), %rcx     # 0x27ba4
movl $0x594, %edx            # imm = 0x594
callq 0x4160
leaq 0x1ce3b(%rip), %rdi     # 0x27c10
leaq 0x1c96e(%rip), %rsi     # 0x2774a
leaq 0x1cdc1(%rip), %rcx     # 0x27ba4
movl $0x59a, %edx            # imm = 0x59A
callq 0x4160
movq 0x2c1e4(%rip), %rdi     # 0x36fd8
leaq 0x1ce46(%rip), %rsi     # 0x27c41
callq 0x4260
movq %rax, %rdi
callq 0x4100
xorl %edi, %edi
callq 0x4290
movq 0x2c1c2(%rip), %rdi     # 0x36fd8
leaq 0x1ce24(%rip), %rsi     # 0x27c41
movl $0x4e, %edx
callq 0x4280
movq 0x2c1aa(%rip), %rdi     # 0x36fd8
callq 0x4100
jmp 0xae08
leaq 0x1e73b(%rip), %rdi     # 0x29577
leaq 0x1c907(%rip), %rsi     # 0x2774a
leaq 0x1e742(%rip), %rcx     # 0x2958c
movl $0x55a, %edx            # imm = 0x55A
callq 0x4160
leaq 0x1e71c(%rip), %rdi     # 0x29577
leaq 0x1c8e8(%rip), %rsi     # 0x2774a
leaq 0x1e74c(%rip), %rcx     # 0x295b5
movl $0x579, %edx            # imm = 0x579
callq 0x4160
leaq 0x1d201(%rip), %rdi     # 0x2807b
leaq 0x1c8c9(%rip), %rsi     # 0x2774a
leaq 0x1e704(%rip), %rcx     # 0x2958c
movl $0x55b, %edx            # imm = 0x55B
callq 0x4160
leaq 0x1d1e2(%rip), %rdi     # 0x2807b
leaq 0x1c8aa(%rip), %rsi     # 0x2774a
leaq 0x1e70e(%rip), %rcx     # 0x295b5
movl $0x57a, %edx            # imm = 0x57A
callq 0x4160
jmp 0xaeca
jmp 0xaeb7
jmp 0xaeb7
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x1abf2
jmp 0xaedc
jmp 0xaeca
jmp 0xaeca
movq %rax, %rbx
jmp 0xaedc
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x1353a
leaq 0x30(%rsp), %rdi
callq 0x1abf2
movq %rbx, %rdi
callq 0x43e0