// File: /workspace/github_c_makefile_star2_O2/SimpleDoger[P]Darknet_CPU_Only/iseg_layer.c
// Function: forward_iseg_layer
void forward_iseg_layer(const layer l, network net)
{

    double time = what_time_is_it_now();
    int i,b,j,k;
    int ids = l.extra;
    memcpy(l.output, net.input, l.outputs*l.batch*sizeof(float));
    memset(l.delta, 0, l.outputs * l.batch * sizeof(float));

    for (b = 0; b < l.batch; ++b){
        int index = b*l.outputs;
        activate_array(l.output + index, l.classes*l.w*l.h, LOGISTIC);
    }

    for (b = 0; b < l.batch; ++b){
        // a priori, each pixel has no class
        for(i = 0; i < l.classes; ++i){
            for(k = 0; k < l.w*l.h; ++k){
                int index = b*l.outputs + i*l.w*l.h + k;
                l.delta[index] = 0 - l.output[index];
            }
        }

        // a priori, embedding should be small magnitude
        for(i = 0; i < ids; ++i){
            for(k = 0; k < l.w*l.h; ++k){
                int index = b*l.outputs + (i+l.classes)*l.w*l.h + k;
                l.delta[index] = .1 * (0 - l.output[index]);
            }
        }


        memset(l.counts, 0, 90*sizeof(int));
        for(i = 0; i < 90; ++i){
            fill_cpu(ids, 0, l.sums[i], 1);
            
            int c = net.truth[b*l.truths + i*(l.w*l.h+1)];
            if(c < 0) break;
            // add up metric embeddings for each instance
            for(k = 0; k < l.w*l.h; ++k){
                int index = b*l.outputs + c*l.w*l.h + k;
                float v = net.truth[b*l.truths + i*(l.w*l.h + 1) + 1 + k];
                if(v){
                    l.delta[index] = v - l.output[index];
                    axpy_cpu(ids, 1, l.output + b*l.outputs + l.classes*l.w*l.h + k, l.w*l.h, l.sums[i], 1);
                    ++l.counts[i];
                }
            }
        }

        float *mse = calloc(90, sizeof(float));
        for(i = 0; i < 90; ++i){
            int c = net.truth[b*l.truths + i*(l.w*l.h+1)];
            if(c < 0) break;
            for(k = 0; k < l.w*l.h; ++k){
                float v = net.truth[b*l.truths + i*(l.w*l.h + 1) + 1 + k];
                if(v){
                    int z;
                    float sum = 0;
                    for(z = 0; z < ids; ++z){
                        int index = b*l.outputs + (l.classes + z)*l.w*l.h + k;
                        sum += pow(l.sums[i][z]/l.counts[i] - l.output[index], 2);
                    }
                    mse[i] += sum;
                }
            }
            mse[i] /= l.counts[i];
        }

        // Calculate average embedding
        for(i = 0; i < 90; ++i){
            if(!l.counts[i]) continue;
            scal_cpu(ids, 1.f/l.counts[i], l.sums[i], 1);
            if(b == 0 && net.gpu_index == 0){
                printf("%4d, %6.3f, ", l.counts[i], mse[i]);
                for(j = 0; j < ids; ++j){
                    printf("%6.3f,", l.sums[i][j]);
                }
                printf("\n");
            }
        }
        free(mse);

        // Calculate embedding loss
        for(i = 0; i < 90; ++i){
            if(!l.counts[i]) continue;
            for(k = 0; k < l.w*l.h; ++k){
                float v = net.truth[b*l.truths + i*(l.w*l.h + 1) + 1 + k];
                if(v){
                    for(j = 0; j < 90; ++j){
                        if(!l.counts[j])continue;
                        int z;
                        for(z = 0; z < ids; ++z){
                            int index = b*l.outputs + (l.classes + z)*l.w*l.h + k;
                            float diff = l.sums[j][z] - l.output[index];
                            if (j == i) l.delta[index] +=   diff < 0? -.1 : .1;
                            else        l.delta[index] += -(diff < 0? -.1 : .1);
                        }
                    }
                }
            }
        }

        for(i = 0; i < ids; ++i){
            for(k = 0; k < l.w*l.h; ++k){
                int index = b*l.outputs + (i+l.classes)*l.w*l.h + k;
                l.delta[index] *= .01;
            }
        }
    }

    *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);
    printf("took %lf sec\n", what_time_is_it_now() - time);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
leaq 0x5c0(%rsp), %rbx
leaq 0x120(%rsp), %rbp
xorl %eax, %eax
callq 0x3bc0
movsd %xmm0, 0xb0(%rsp)
movl 0x64(%rbp), %eax
movl %eax, 0x14(%rsp)
movq 0x268(%rbp), %r14
movq 0xd8(%rbx), %rsi
movslq 0x58(%rbp), %rax
movq %rax, 0x80(%rsp)
movslq 0x48(%rbp), %rbx
movq %rbx, %r15
imulq %rax, %r15
movq %r15, 0xa8(%rsp)
shlq $0x2, %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0x32b0
movq 0x260(%rbp), %r12
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x31c0
movl 0xfc(%rbp), %eax
movl 0x6c(%rbp), %r13d
movl 0x70(%rbp), %ecx
movl %ecx, 0x18(%rsp)
movl %eax, %ebp
imull %eax, %ecx
imull %r13d, %ecx
movl %ecx, 0x8(%rsp)
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
movq 0x80(%rsp), %rax
leaq (,%rax,4), %rax
movq %rax, 0x88(%rsp)
movq %r14, %r15
movq %rcx, 0xc8(%rsp)
movq %rcx, %rbx
subq $0x1, %rbx
jb 0x36049
movq %r15, %rdi
movl 0x8(%rsp), %esi
xorl %edx, %edx
callq 0x1a4f8
addq 0x88(%rsp), %r15
jmp 0x3602b
movabsq $0x100000000, %rcx      # imm = 0x100000000
imull 0x18(%rsp), %r13d
leaq 0x120(%rsp), %rax
movq 0x1c0(%rax), %rdx
movq %rdx, 0x8(%rsp)
movq 0x1c8(%rax), %rbx
leaq 0x5c0(%rsp), %r9
movq 0xe0(%r9), %rdx
movslq 0x68(%rax), %rsi
leal 0x1(%r13), %edi
movl %r13d, %eax
imull %ebp, %eax
movl %eax, 0x4(%rsp)
cltq
leaq (%r14,%rax,4), %rax
movq %rax, 0x50(%rsp)
xorl %r8d, %r8d
testl %r13d, %r13d
movl $0x0, %eax
cmovgl %r13d, %eax
movq %rax, 0x18(%rsp)
movl 0xc8(%r9), %eax
movl %eax, 0x74(%rsp)
testl %ebp, %ebp
movl $0x0, %eax
movq %rax, 0x28(%rsp)
cmovlel %r8d, %ebp
movl %ebp, 0x7c(%rsp)
movl 0x14(%rsp), %eax
testl %eax, %eax
movl $0x0, %r15d
cmovgl %eax, %r15d
movl %edi, 0x78(%rsp)
movslq %edi, %rax
movq %rsi, 0xc0(%rsp)
shlq $0x20, %rsi
movq %rsi, 0xb8(%rsp)
movq %rax, 0x90(%rsp)
shlq $0x20, %rax
movq %rax, 0xd8(%rsp)
movsd 0x194b6(%rip), %xmm1    # 0x4f5d0
movq %rdx, 0x38(%rsp)
leaq 0x4(%rdx), %rax
movq %rax, 0xd0(%rsp)
leaq 0x1ad4e(%rip), %rbp     # 0x50e80
movq %rcx, 0x40(%rsp)
xorl %eax, %eax
cmpq 0xc8(%rsp), %rax
je 0x36671
movq %rax, 0x48(%rsp)
xorl %eax, %eax
movq 0x28(%rsp), %rcx
cmpl 0x7c(%rsp), %eax
je 0x36183
movl %ecx, %edx
movq 0x18(%rsp), %rsi
subq $0x1, %rsi
jb 0x3617c
movslq %edx, %rdx
xorps %xmm0, %xmm0
subss (%r14,%rdx,4), %xmm0
movss %xmm0, (%r12,%rdx,4)
incl %edx
jmp 0x36160
incl %eax
addl %r13d, %ecx
jmp 0x36153
xorl %eax, %eax
movl 0x4(%rsp), %ecx
cmpl %r15d, %eax
je 0x361c4
movl %ecx, %edx
movq 0x18(%rsp), %rsi
subq $0x1, %rsi
jb 0x361bd
movslq %edx, %rdx
xorps %xmm0, %xmm0
subss (%r14,%rdx,4), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r12,%rdx,4)
incl %edx
jmp 0x36195
incl %eax
addl %r13d, %ecx
jmp 0x36189
movl $0x168, %edx            # imm = 0x168
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x31c0
movq 0x48(%rsp), %rcx
imulq 0xc0(%rsp), %rcx
movq 0x38(%rsp), %rax
movq %rcx, 0xa0(%rsp)
leaq (%rax,%rcx,4), %rax
movq %rax, 0xe0(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x58(%rsp)
xorl %edx, %edx
cmpq $0x5a, %rdx
je 0x3632f
movq (%rbx,%rdx,8), %rsi
xorps %xmm0, %xmm0
movl 0x14(%rsp), %edi
movq %rdx, 0x98(%rsp)
pushq $0x1
popq %rdx
callq 0x1b449
movq 0x98(%rsp), %rdx
movq %rdx, %rax
imulq 0x90(%rsp), %rax
movq 0xe0(%rsp), %rcx
cvttss2si (%rcx,%rax,4), %r8d
testl %r8d, %r8d
js 0x3632f
imull %r13d, %r8d
addl 0x28(%rsp), %r8d
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq 0x18(%rsp), %r9
xorpd %xmm1, %xmm1
subq $0x1, %r9
jb 0x36315
movq %rdi, %rax
sarq $0x1e, %rax
movq 0x38(%rsp), %rcx
movss (%rcx,%rax), %xmm0
ucomiss %xmm1, %xmm0
jne 0x36296
jnp 0x362fc
movslq %r8d, %rax
subss (%r14,%rax,4), %xmm0
movss %xmm0, (%r12,%rax,4)
movq (%rbx,%rdx,8), %rcx
movq %rdi, 0x20(%rsp)
movl 0x14(%rsp), %edi
movss 0x15d4e(%rip), %xmm0    # 0x4c008
movl %r8d, 0x68(%rsp)
movl %r13d, %edx
movq %rsi, 0x30(%rsp)
pushq $0x1
popq %r8
movq %r9, 0x60(%rsp)
callq 0x1b3ed
movq 0x60(%rsp), %r9
movq 0x30(%rsp), %rsi
movq 0x20(%rsp), %rdi
movl 0x68(%rsp), %r8d
movq 0x98(%rsp), %rdx
xorps %xmm1, %xmm1
movq 0x8(%rsp), %rax
incl (%rax,%rdx,4)
incl %r8d
addq $0x4, %rsi
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %rdi
jmp 0x36274
incq %rdx
movq 0x58(%rsp), %rax
addq 0xd8(%rsp), %rax
movq %rax, 0x58(%rsp)
jmp 0x36208
pushq $0x5a
popq %rdi
pushq $0x4
popq %rsi
callq 0x3250
movq %rax, %r10
xorl %eax, %eax
xorps %xmm3, %xmm3
cmpq $0x5a, %rax
je 0x36417
movq %rax, %rcx
imulq 0x90(%rsp), %rcx
addq 0xa0(%rsp), %rcx
movq 0x38(%rsp), %rdx
cvttss2si (%rdx,%rcx,4), %edx
testl %edx, %edx
js 0x36417
movq 0xd0(%rsp), %rdx
leaq (%rdx,%rcx,4), %rcx
movl 0x4(%rsp), %edx
xorl %esi, %esi
cmpq 0x18(%rsp), %rsi
je 0x363f5
movss (%rcx,%rsi,4), %xmm0
ucomiss %xmm3, %xmm0
jne 0x36397
jnp 0x363ee
xorps %xmm0, %xmm0
movl %edx, %edi
xorl %r8d, %r8d
cmpq %r8, %r15
je 0x363e2
movq (%rbx,%rax,8), %r9
movss (%r9,%r8,4), %xmm1
movq 0x8(%rsp), %r9
cvtsi2ssl (%r9,%rax,4), %xmm2
divss %xmm2, %xmm1
movslq %edi, %rdi
subss (%r14,%rdi,4), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm1, %xmm1
cvtss2sd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
incq %r8
addl %r13d, %edi
jmp 0x3639f
addss (%r10,%rax,4), %xmm0
movss %xmm0, (%r10,%rax,4)
incq %rsi
incl %edx
jmp 0x36384
movq 0x8(%rsp), %rcx
cvtsi2ssl (%rcx,%rax,4), %xmm0
movss (%r10,%rax,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%r10,%rax,4)
incq %rax
jmp 0x36342
cmpl $0x0, 0x74(%rsp)
sete %al
cmpq $0x0, 0x48(%rsp)
sete %cl
andb %al, %cl
movb %cl, 0x60(%rsp)
xorl %ecx, %ecx
movq %r10, 0x30(%rsp)
cmpq $0x5a, %rcx
je 0x364ed
movq 0x8(%rsp), %rax
movl (%rax,%rcx,4), %eax
testl %eax, %eax
je 0x364e5
cvtsi2ss %eax, %xmm1
movss 0x15bad(%rip), %xmm0    # 0x4c008
divss %xmm1, %xmm0
movq (%rbx,%rcx,8), %rsi
movl 0x14(%rsp), %edi
pushq $0x1
popq %rdx
movq %rcx, 0x20(%rsp)
callq 0x1b423
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %r10
cmpb $0x0, 0x60(%rsp)
je 0x364e5
movq 0x8(%rsp), %rax
movl (%rax,%rcx,4), %esi
cvtss2sd (%r10,%rcx,4), %xmm0
leaq 0x1aa0c(%rip), %rdi     # 0x50ea6
movb $0x1, %al
callq 0x3160
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x364d3
movq 0x20(%rsp), %rax
movq (%rbx,%rax,8), %rax
cvtss2sd (%rax,%rcx,4), %xmm0
leaq 0x1a9f6(%rip), %rdi     # 0x50eb3
movb $0x1, %al
movq %rcx, 0x68(%rsp)
callq 0x3160
movq 0x68(%rsp), %rcx
incq %rcx
jmp 0x364a3
pushq $0xa
popq %rdi
callq 0x3060
movq 0x30(%rsp), %r10
movq 0x20(%rsp), %rcx
incq %rcx
jmp 0x36435
movq %r10, %rdi
callq 0x3040
xorps %xmm3, %xmm3
xorl %eax, %eax
movsd 0x190be(%rip), %xmm2    # 0x4f5c0
cmpq $0x5a, %rax
je 0x365e3
movq 0x8(%rsp), %rcx
cmpl $0x0, (%rcx,%rax,4)
je 0x365db
movl 0x78(%rsp), %ecx
imull %eax, %ecx
movl 0x4(%rsp), %edx
xorl %esi, %esi
cmpq 0x18(%rsp), %rsi
je 0x365db
incq %rsi
leal (%rcx,%rsi), %edi
addl 0xa0(%rsp), %edi
movslq %edi, %rdi
movq 0x38(%rsp), %r8
movss (%r8,%rdi,4), %xmm0
ucomiss %xmm3, %xmm0
jne 0x36555
jnp 0x365d4
xorl %edi, %edi
cmpq $0x5a, %rdi
je 0x365d4
movq 0x8(%rsp), %r8
cmpl $0x0, (%r8,%rdi,4)
je 0x365cf
movl %edx, %r8d
xorl %r9d, %r9d
cmpq %r9, %r15
je 0x365cf
movq (%rbx,%rdi,8), %r10
movss (%r10,%r9,4), %xmm0
movslq %r8d, %r8
movss (%r14,%r8,4), %xmm1
xorl %r10d, %r10d
cmpq %rax, %rdi
jne 0x365a5
ucomiss %xmm0, %xmm1
seta %r10b
leaq 0x1a8f3(%rip), %r11     # 0x50e90
movsd (%r11,%r10,8), %xmm0
jmp 0x365b3
ucomiss %xmm0, %xmm1
seta %r10b
movsd (%rbp,%r10,8), %xmm0
cvtss2sd (%r12,%r8,4), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movss %xmm0, (%r12,%r8,4)
incq %r9
addl %r13d, %r8d
jmp 0x3656f
incq %rdi
jmp 0x36557
incl %edx
jmp 0x36528
incq %rax
jmp 0x36502
xorl %eax, %eax
movl 0x4(%rsp), %ecx
cmpl %r15d, %eax
je 0x3661d
movl %ecx, %edx
movq 0x18(%rsp), %rsi
subq $0x1, %rsi
jb 0x36616
movslq %edx, %rdx
cvtss2sd (%r12,%rdx,4), %xmm0
mulsd %xmm2, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r12,%rdx,4)
incl %edx
jmp 0x365f5
incl %eax
addl %r13d, %ecx
jmp 0x365e9
movq 0x48(%rsp), %rdx
incq %rdx
movq 0x80(%rsp), %rax
movq 0x28(%rsp), %rcx
addl %eax, %ecx
movq %rcx, 0x28(%rsp)
addl %eax, 0x4(%rsp)
movq 0x50(%rsp), %rax
addq 0x88(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x40(%rsp), %rax
addq 0xb8(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rdx, %rax
movsd 0x18f64(%rip), %xmm1    # 0x4f5d0
jmp 0x36139
movq %r12, %rdi
movq 0xa8(%rsp), %rsi
callq 0x4b4c
mulss %xmm0, %xmm0
leaq 0x120(%rsp), %rax
movq 0x1d8(%rax), %rax
movss %xmm0, (%rax)
xorl %eax, %eax
callq 0x3bc0
subsd 0xb0(%rsp), %xmm0
leaq 0x1a80b(%rip), %rdi     # 0x50eba
movb $0x1, %al
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3160