// File: /workspace/github_c_makefile_star2_O1/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 $0xf8, %rsp
leaq 0x130(%rsp), %r12
xorl %eax, %eax
callq 0xdfdc
movsd %xmm0, 0xc0(%rsp)
movl 0x64(%r12), %ebx
movq 0x268(%r12), %r14
movq 0x6a8(%rsp), %rsi
movslq 0x58(%r12), %rax
movslq 0x48(%r12), %rdx
imulq %rax, %rdx
shlq $0x2, %rdx
movq %r14, %rdi
callq 0x32c0
movq 0x260(%r12), %r15
movl 0x58(%r12), %ecx
movslq 0x48(%r12), %r13
movl %r13d, %eax
movl %ecx, 0x44(%rsp)
imull %ecx, %eax
movslq %eax, %rdx
shlq $0x2, %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x31d0
testq %r13, %r13
jle 0x400ad
leaq 0x130(%rsp), %rax
movslq 0x58(%rax), %rbp
movl 0x70(%rax), %ecx
imull 0xfc(%rax), %ecx
movq 0x268(%rax), %r12
imull 0x6c(%rax), %ecx
movl %ecx, 0x8(%rsp)
shlq $0x2, %rbp
movq %r12, %rdi
movl 0x8(%rsp), %esi
xorl %edx, %edx
callq 0x275e5
addq %rbp, %r12
decq %r13
jne 0x40097
leaq 0x130(%rsp), %rax
movslq 0x48(%rax), %rax
movq %rax, 0x98(%rsp)
testq %rax, %rax
jle 0x407d2
leaq 0x5d0(%rsp), %rax
leaq 0x130(%rsp), %rcx
movl 0xfc(%rcx), %edi
movl 0x6c(%rcx), %r13d
imull 0x70(%rcx), %r13d
movl 0x68(%rcx), %edx
movslq 0x58(%rcx), %rsi
movq 0x268(%rcx), %r8
movq 0x260(%rcx), %r9
movq 0x1c0(%rcx), %r10
movq %r10, 0x8(%rsp)
movq 0x1c8(%rcx), %r10
movq 0xe0(%rax), %rcx
leal 0x1(%r13), %r11d
movl %r13d, %ebp
imull %edi, %ebp
movslq %ebp, %rax
leaq (%r8,%rax,4), %r12
movq %r12, 0xa0(%rsp)
leaq 0x5d0(%rsp), %r12
movl 0xc8(%r12), %r12d
movl %r12d, 0x8c(%rsp)
movq %rsi, 0xd8(%rsp)
shlq $0x2, %rsi
movq %rsi, 0xd0(%rsp)
movsd 0x1644b(%rip), %xmm1    # 0x565b0
movl %r11d, 0x58(%rsp)
movslq %r11d, %rsi
movq %rsi, 0xa8(%rsp)
movl %ebp, %r11d
movl %edx, 0x50(%rsp)
movslq %edx, %rdx
movq %rdx, 0xc8(%rsp)
movq %rcx, 0x60(%rsp)
leaq 0x4(%rcx), %rcx
movq %rcx, 0xe0(%rsp)
movq $0x0, 0x68(%rsp)
movl $0x1, 0x18(%rsp)
leaq 0x17ca8(%rip), %rbp     # 0x57e58
leaq 0x17c91(%rip), %r12     # 0x57e48
movl %eax, 0x54(%rsp)
movq %rax, 0x70(%rsp)
movl %eax, 0x4c(%rsp)
movl $0x0, 0x48(%rsp)
movq $0x0, 0x28(%rsp)
movl %edi, 0x90(%rsp)
movq %r8, 0x78(%rsp)
movq %r9, 0xb0(%rsp)
movq %r10, 0x10(%rsp)
testl %edi, %edi
jle 0x40224
xorl %eax, %eax
movl 0x48(%rsp), %ecx
testl %r13d, %r13d
jle 0x4021b
movq %r13, %rdx
movl %ecx, %esi
movslq %esi, %rsi
xorps %xmm0, %xmm0
subss (%r8,%rsi,4), %xmm0
movss %xmm0, (%r9,%rsi,4)
incl %esi
decq %rdx
jne 0x40202
incl %eax
addl %r13d, %ecx
cmpl %edi, %eax
jne 0x401f8
testl %ebx, %ebx
jle 0x40266
xorl %eax, %eax
movl 0x4c(%rsp), %ecx
testl %r13d, %r13d
jle 0x4025d
movq %r13, %rdx
movl %ecx, %esi
movslq %esi, %rsi
xorps %xmm0, %xmm0
subss (%r8,%rsi,4), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r9,%rsi,4)
incl %esi
decq %rdx
jne 0x40238
incl %eax
addl %r13d, %ecx
cmpl %ebx, %eax
jne 0x4022e
movl %r11d, 0x94(%rsp)
movl $0x168, %edx            # imm = 0x168
movq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x31d0
movq 0x28(%rsp), %rcx
imulq 0xc8(%rsp), %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0xf0(%rsp)
leaq (%rax,%rcx,4), %rax
movq %rax, 0xe8(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x1c(%rsp)
xorl %ecx, %ecx
movq %rcx, 0xb8(%rsp)
movq 0x10(%rsp), %rax
movq (%rax,%rcx,8), %rsi
xorps %xmm0, %xmm0
movl %ebx, %edi
movl $0x1, %edx
callq 0x2863b
movq 0xb8(%rsp), %rdx
movq %rdx, %rax
imulq 0xa8(%rsp), %rax
movq 0xe8(%rsp), %rcx
cvttss2si (%rcx,%rax,4), %eax
movl %eax, 0x5c(%rsp)
testl %eax, %eax
js 0x403eb
testl %r13d, %r13d
xorpd %xmm1, %xmm1
jle 0x403c7
movl %r13d, %r9d
imull 0x5c(%rsp), %r9d
addl 0x68(%rsp), %r9d
movl 0x1c(%rsp), %edi
movq %r13, %r8
movq 0xa0(%rsp), %rsi
movslq %edi, %rax
movq 0x60(%rsp), %rcx
movss (%rcx,%rax,4), %xmm0
ucomiss %xmm1, %xmm0
jne 0x4033b
jnp 0x403b5
movslq %r9d, %rax
movq 0x78(%rsp), %rcx
subss (%rcx,%rax,4), %xmm0
movq 0xb0(%rsp), %rcx
movss %xmm0, (%rcx,%rax,4)
movq 0x10(%rsp), %rax
movq (%rax,%rdx,8), %rcx
movl %edi, 0x30(%rsp)
movl %ebx, %edi
movss 0x12c9c(%rip), %xmm0    # 0x53008
movl %r9d, 0x38(%rsp)
movl %r13d, %edx
movq %r8, 0x20(%rsp)
movl $0x1, %r8d
movq %rsi, 0x80(%rsp)
callq 0x285e9
movq 0x80(%rsp), %rsi
movq 0x20(%rsp), %r8
movl 0x30(%rsp), %edi
movl 0x38(%rsp), %r9d
movq 0xb8(%rsp), %rdx
xorps %xmm1, %xmm1
movq 0x8(%rsp), %rax
incl (%rax,%rdx,4)
addq $0x4, %rsi
incl %r9d
incl %edi
decq %r8
jne 0x40327
cmpl $0x0, 0x5c(%rsp)
js 0x403eb
leaq 0x1(%rdx), %rax
movl 0x1c(%rsp), %ecx
addl 0x58(%rsp), %ecx
movl %ecx, 0x1c(%rsp)
cmpq $0x59, %rdx
movq %rax, %rcx
jb 0x402b0
movl $0x5a, %edi
movl $0x4, %esi
callq 0x3260
movq %rax, 0x20(%rsp)
xorl %eax, %eax
movq 0x78(%rsp), %r11
xorps %xmm3, %xmm3
movq %rax, %rdx
imulq 0xa8(%rsp), %rdx
addq 0xf0(%rsp), %rdx
movq 0x60(%rsp), %rcx
cvttss2si (%rcx,%rdx,4), %ecx
testl %ecx, %ecx
js 0x40505
testl %r13d, %r13d
jle 0x404d0
movq 0xe0(%rsp), %rsi
leaq (%rsi,%rdx,4), %rdx
movl 0x54(%rsp), %esi
xorl %edi, %edi
movss (%rdx,%rdi,4), %xmm0
ucomiss %xmm3, %xmm0
jne 0x40456
jnp 0x404c2
testl %ebx, %ebx
jle 0x404ae
movq 0x8(%rsp), %r8
xorps %xmm1, %xmm1
cvtsi2ssl (%r8,%rax,4), %xmm1
movq 0x10(%rsp), %r8
movq (%r8,%rax,8), %r8
xorps %xmm0, %xmm0
movl %esi, %r9d
xorl %r10d, %r10d
movss (%r8,%r10,4), %xmm2
divss %xmm1, %xmm2
movslq %r9d, %r9
subss (%r11,%r9,4), %xmm2
cvtss2sd %xmm2, %xmm2
mulsd %xmm2, %xmm2
cvtss2sd %xmm0, %xmm0
addsd %xmm2, %xmm0
cvtsd2ss %xmm0, %xmm0
incq %r10
addl %r13d, %r9d
cmpq %r10, %rbx
jne 0x4047a
jmp 0x404b1
xorps %xmm0, %xmm0
movq 0x20(%rsp), %r8
addss (%r8,%rax,4), %xmm0
movss %xmm0, (%r8,%rax,4)
incq %rdi
incl %esi
cmpq %r13, %rdi
jne 0x4044a
movq 0x8(%rsp), %rdx
xorps %xmm0, %xmm0
cvtsi2ssl (%rdx,%rax,4), %xmm0
movq 0x20(%rsp), %rdx
movss (%rdx,%rax,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%rdx,%rax,4)
testl %ecx, %ecx
js 0x40505
leaq 0x1(%rax), %rcx
cmpq $0x59, %rax
movq %rcx, %rax
jb 0x40409
cmpl $0x0, 0x8c(%rsp)
sete %al
cmpq $0x0, 0x28(%rsp)
sete %cl
andb %al, %cl
movb %cl, 0x80(%rsp)
xorl %ecx, %ecx
movq 0x8(%rsp), %rax
movl (%rax,%rcx,4), %eax
testl %eax, %eax
je 0x405e3
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movss 0x12ac5(%rip), %xmm0    # 0x53008
divss %xmm1, %xmm0
movq 0x10(%rsp), %rax
movq (%rax,%rcx,8), %rsi
movl %ebx, %edi
movl $0x1, %edx
movq %rcx, 0x30(%rsp)
callq 0x28619
movq 0x30(%rsp), %rcx
cmpb $0x0, 0x80(%rsp)
je 0x405e3
movq 0x8(%rsp), %rax
movl (%rax,%rcx,4), %esi
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rcx,4), %xmm0
leaq 0x178e2(%rip), %rdi     # 0x57e6e
movb $0x1, %al
callq 0x3170
testl %ebx, %ebx
movq 0x10(%rsp), %rcx
jle 0x405d4
xorl %edx, %edx
movq %rdx, 0x38(%rsp)
movq 0x30(%rsp), %rax
movq (%rcx,%rax,8), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rdx,4), %xmm0
leaq 0x178c0(%rip), %rdi     # 0x57e7b
movb $0x1, %al
callq 0x3170
movq 0x38(%rsp), %rdx
movq 0x10(%rsp), %rcx
incq %rdx
cmpq %rdx, %rbx
jne 0x4059e
movl $0xa, %edi
callq 0x3060
movq 0x30(%rsp), %rcx
incq %rcx
cmpq $0x5a, %rcx
jne 0x40524
movq 0x20(%rsp), %rdi
callq 0x3040
movl 0x50(%rsp), %eax
imull 0x28(%rsp), %eax
movl %eax, 0x38(%rsp)
xorl %ecx, %ecx
xorpd %xmm2, %xmm2
movq 0x8(%rsp), %rax
cmpl $0x0, (%rax,%rcx,4)
je 0x406f4
testl %r13d, %r13d
jle 0x406f4
movl 0x58(%rsp), %eax
movl %eax, %edx
imull %ecx, %edx
movq 0x70(%rsp), %rax
movl %eax, %esi
xorl %edi, %edi
incq %rdi
leal (%rdx,%rdi), %r8d
addl 0x38(%rsp), %r8d
movslq %r8d, %r8
movq 0x60(%rsp), %rax
movss (%rax,%r8,4), %xmm0
ucomiss %xmm2, %xmm0
jne 0x4065c
jnp 0x406e9
xorl %r8d, %r8d
testl %ebx, %ebx
setle %r9b
movq 0x8(%rsp), %rax
cmpl $0x0, (%rax,%r8,4)
sete %r10b
orb %r9b, %r10b
jne 0x406dc
movq 0x10(%rsp), %rax
movq (%rax,%r8,8), %r9
movl %esi, %r10d
xorl %r11d, %r11d
movss (%r9,%r11,4), %xmm0
movslq %r10d, %r10
movss (%r14,%r10,4), %xmm1
xorl %eax, %eax
cmpq %rcx, %r8
jne 0x406ab
ucomiss %xmm0, %xmm1
seta %al
movsd (%rbp,%rax,8), %xmm0
jmp 0x406b7
ucomiss %xmm0, %xmm1
seta %al
movsd (%r12,%rax,8), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r15,%r10,4), %xmm1
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, (%r15,%r10,4)
incq %r11
addl %r13d, %r10d
cmpq %r11, %rbx
jne 0x40687
incq %r8
cmpq $0x5a, %r8
jne 0x4065f
incl %esi
cmpq %r13, %rdi
jne 0x40637
incq %rcx
cmpq $0x5a, %rcx
jne 0x4060d
testl %ebx, %ebx
movl 0x90(%rsp), %edi
movq 0x78(%rsp), %r8
movq 0xb0(%rsp), %r9
movl 0x94(%rsp), %r11d
movsd 0x15e89(%rip), %xmm1    # 0x565b0
movsd 0x15e71(%rip), %xmm2    # 0x565a0
jle 0x4076a
xorl %eax, %eax
movl %r11d, %ecx
testl %r13d, %r13d
jle 0x40761
movq %r13, %rdx
movl %ecx, %esi
movslq %esi, %rsi
xorps %xmm0, %xmm0
cvtss2sd (%r15,%rsi,4), %xmm0
mulsd %xmm2, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r15,%rsi,4)
incl %esi
decq %rdx
jne 0x40740
incl %eax
addl %r13d, %ecx
cmpl %ebx, %eax
jne 0x40736
movq 0x28(%rsp), %rdx
incq %rdx
movq 0xd8(%rsp), %rax
addl %eax, 0x48(%rsp)
addl %eax, 0x4c(%rsp)
movq 0xd0(%rsp), %rcx
addq %rcx, 0xa0(%rsp)
movq 0x68(%rsp), %rcx
addl %eax, %ecx
movq %rcx, 0x68(%rsp)
movl 0x18(%rsp), %ecx
addl 0x50(%rsp), %ecx
movl %ecx, 0x18(%rsp)
addl %eax, 0x54(%rsp)
movq 0x70(%rsp), %rcx
addl %eax, %ecx
movq %rcx, 0x70(%rsp)
addl 0x44(%rsp), %r11d
movq %rdx, 0x28(%rsp)
cmpq 0x98(%rsp), %rdx
jne 0x401ee
movq 0x98(%rsp), %rsi
imull 0x44(%rsp), %esi
movq %r15, %rdi
callq 0xf121
mulss %xmm0, %xmm0
leaq 0x130(%rsp), %rax
movq 0x1d8(%rax), %rax
movss %xmm0, (%rax)
xorl %eax, %eax
callq 0xdfdc
subsd 0xc0(%rsp), %xmm0
leaq 0x1766d(%rip), %rdi     # 0x57e82
movb $0x1, %al
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3170