// File: /workspace/github_c_makefile_star2_O3/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 0x120(%rsp), %r12
xorl %eax, %eax
callq 0xe79c
movsd %xmm0, 0xb8(%rsp)
movl 0x64(%r12), %ebx
movq 0x268(%r12), %r14
movq 0x698(%rsp), %rsi
movslq 0x58(%r12), %rdx
movslq 0x48(%r12), %r13
movq %r13, %rax
movq %rdx, 0x88(%rsp)
imulq %rdx, %rax
movq %rax, 0xb0(%rsp)
leaq (,%rax,4), %r15
movq %r14, %rdi
movq %r15, %rdx
callq 0x32b0
movq 0x260(%r12), %r12
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x31c0
movq %r13, 0x48(%rsp)
testq %r13, %r13
jle 0x429e0
leaq 0x120(%rsp), %rax
movl 0x70(%rax), %ebp
imull 0xfc(%rax), %ebp
imull 0x6c(%rax), %ebp
movq 0x88(%rsp), %rax
leaq (,%rax,4), %rax
movq 0x48(%rsp), %r13
movq %r14, %r15
movq %rax, 0x90(%rsp)
movq %r15, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x28c99
movq 0x90(%rsp), %rax
addq %rax, %r15
decq %r13
jne 0x4234f
cmpl $0x0, 0x48(%rsp)
jle 0x429e0
movabsq $0x100000000, %rcx      # imm = 0x100000000
leaq 0x120(%rsp), %rax
movl 0x6c(%rax), %r13d
imull 0x70(%rax), %r13d
movl 0xfc(%rax), %edi
movq 0x1c0(%rax), %rdx
movq %rdx, 0x10(%rsp)
movq 0x1c8(%rax), %r8
leaq 0x5c0(%rsp), %r10
movq 0xe0(%r10), %rdx
movslq 0x68(%rax), %rsi
leal 0x1(%r13), %r9d
movl %r13d, %eax
imull %edi, %eax
movl %eax, 0xc(%rsp)
cltq
leaq (%r14,%rax,4), %rax
movq %rax, 0x58(%rsp)
movl 0xc8(%r10), %eax
movl %eax, 0x7c(%rsp)
movl %r9d, 0x84(%rsp)
movslq %r9d, %rax
movq %rdx, 0x40(%rsp)
addq $0x4, %rdx
movq %rdx, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
shlq $0x20, %rsi
movq %rsi, 0xc0(%rsp)
movq %rax, 0x98(%rsp)
shlq $0x20, %rax
movq %rax, 0xd8(%rsp)
movsd 0x16253(%rip), %xmm1    # 0x58680
movq $0x0, 0x30(%rsp)
leaq 0x17b53(%rip), %rbp     # 0x59f90
leaq 0x17b3c(%rip), %r15     # 0x59f80
movq %rcx, 0x50(%rsp)
movq $0x0, 0x28(%rsp)
movl %edi, 0x80(%rsp)
movq %r8, 0x18(%rsp)
testl %edi, %edi
jle 0x42495
xorl %eax, %eax
movq 0x30(%rsp), %rcx
testl %r13d, %r13d
jle 0x4248c
movq %r13, %rdx
movl %ecx, %esi
movslq %esi, %rsi
xorps %xmm0, %xmm0
subss (%r14,%rsi,4), %xmm0
movss %xmm0, (%r12,%rsi,4)
incl %esi
decq %rdx
jne 0x42473
incl %eax
addl %r13d, %ecx
cmpl %edi, %eax
jne 0x42469
testl %ebx, %ebx
jle 0x424d7
xorl %eax, %eax
movl 0xc(%rsp), %ecx
testl %r13d, %r13d
jle 0x424ce
movq %r13, %rdx
movl %ecx, %esi
movslq %esi, %rsi
xorps %xmm0, %xmm0
subss (%r14,%rsi,4), %xmm0
cvtss2sd %xmm0, %xmm0
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r12,%rsi,4)
incl %esi
decq %rdx
jne 0x424a9
incl %eax
addl %r13d, %ecx
cmpl %ebx, %eax
jne 0x4249f
movl $0x168, %edx            # imm = 0x168
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0x31c0
movq 0x28(%rsp), %rcx
imulq 0xc8(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0xa8(%rsp)
leaq (%rax,%rcx,4), %rax
movq %rax, 0xe0(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x60(%rsp)
xorl %edx, %edx
movq %rdx, 0xa0(%rsp)
movq 0x18(%rsp), %rax
movq (%rax,%rdx,8), %rsi
xorps %xmm0, %xmm0
movl %ebx, %edi
movl $0x1, %edx
callq 0x29d83
movq 0xa0(%rsp), %rdx
movq %rdx, %rax
imulq 0x98(%rsp), %rax
movq 0xe0(%rsp), %rcx
cvttss2si (%rcx,%rax,4), %r9d
testl %r9d, %r9d
js 0x42648
testl %r13d, %r13d
xorpd %xmm1, %xmm1
jle 0x42629
imull %r13d, %r9d
addl 0x30(%rsp), %r9d
movq %r13, %rdi
movq 0x60(%rsp), %r8
movq 0x58(%rsp), %rsi
movq %r8, %rax
sarq $0x1e, %rax
movq 0x40(%rsp), %rcx
movss (%rcx,%rax), %xmm0
ucomiss %xmm1, %xmm0
jne 0x425a1
jnp 0x4260c
movslq %r9d, %rax
subss (%r14,%rax,4), %xmm0
movss %xmm0, (%r12,%rax,4)
movq 0x18(%rsp), %rax
movq (%rax,%rdx,8), %rcx
movq %rdi, 0x20(%rsp)
movl %ebx, %edi
movss 0x12a40(%rip), %xmm0    # 0x55008
movl %r9d, 0x70(%rsp)
movl %r13d, %edx
movq %r8, 0x38(%rsp)
movl $0x1, %r8d
movq %rsi, 0x68(%rsp)
callq 0x29d31
movq 0x68(%rsp), %rsi
movq 0x38(%rsp), %r8
movq 0x20(%rsp), %rdi
movl 0x70(%rsp), %r9d
movq 0xa0(%rsp), %rdx
xorps %xmm1, %xmm1
movq 0x10(%rsp), %rax
incl (%rax,%rdx,4)
addq $0x4, %rsi
incl %r9d
movabsq $0x100000000, %rax      # imm = 0x100000000
addq %rax, %r8
decq %rdi
jne 0x42589
incq %rdx
movq 0x60(%rsp), %rax
addq 0xd8(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpq $0x5a, %rdx
jne 0x4251b
movl $0x5a, %edi
movl $0x4, %esi
callq 0x3250
movq %rax, %r11
xorl %eax, %eax
movq 0x18(%rsp), %r10
xorps %xmm3, %xmm3
movq %rax, %rcx
imulq 0x98(%rsp), %rcx
addq 0xa8(%rsp), %rcx
movq 0x40(%rsp), %rdx
cvttss2si (%rdx,%rcx,4), %edx
testl %edx, %edx
js 0x42746
testl %r13d, %r13d
jle 0x4271c
movq 0xd0(%rsp), %rdx
leaq (%rdx,%rcx,4), %rcx
movl 0xc(%rsp), %edx
xorl %esi, %esi
movss (%rcx,%rsi,4), %xmm0
ucomiss %xmm3, %xmm0
jne 0x426b1
jnp 0x42712
testl %ebx, %ebx
jle 0x42703
movq 0x10(%rsp), %rdi
xorps %xmm1, %xmm1
cvtsi2ssl (%rdi,%rax,4), %xmm1
movq (%r10,%rax,8), %rdi
xorps %xmm0, %xmm0
movl %edx, %r8d
xorl %r9d, %r9d
movss (%rdi,%r9,4), %xmm2
divss %xmm1, %xmm2
movslq %r8d, %r8
subss (%r14,%r8,4), %xmm2
cvtss2sd %xmm2, %xmm2
mulsd %xmm2, %xmm2
cvtss2sd %xmm0, %xmm0
addsd %xmm2, %xmm0
cvtsd2ss %xmm0, %xmm0
incq %r9
addl %r13d, %r8d
cmpq %r9, %rbx
jne 0x426cf
jmp 0x42706
xorps %xmm0, %xmm0
addss (%r11,%rax,4), %xmm0
movss %xmm0, (%r11,%rax,4)
incq %rsi
incl %edx
cmpq %r13, %rsi
jne 0x426a5
movq 0x10(%rsp), %rcx
xorps %xmm0, %xmm0
cvtsi2ssl (%rcx,%rax,4), %xmm0
movss (%r11,%rax,4), %xmm1
divss %xmm0, %xmm1
movss %xmm1, (%r11,%rax,4)
incq %rax
cmpq $0x5a, %rax
jne 0x42664
cmpl $0x0, 0x7c(%rsp)
sete %al
cmpq $0x0, 0x28(%rsp)
sete %cl
andb %al, %cl
movb %cl, 0x68(%rsp)
xorl %ecx, %ecx
movq %r11, 0x38(%rsp)
movq 0x10(%rsp), %rax
movl (%rax,%rcx,4), %eax
testl %eax, %eax
je 0x42826
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movss 0x12885(%rip), %xmm0    # 0x55008
divss %xmm1, %xmm0
movq 0x18(%rsp), %rax
movq (%rax,%rcx,8), %rsi
movl %ebx, %edi
movl $0x1, %edx
movq %rcx, 0x20(%rsp)
callq 0x29d61
movq 0x20(%rsp), %rcx
movq 0x38(%rsp), %r11
cmpb $0x0, 0x68(%rsp)
je 0x42826
movq 0x10(%rsp), %rax
movl (%rax,%rcx,4), %esi
xorps %xmm0, %xmm0
cvtss2sd (%r11,%rcx,4), %xmm0
leaq 0x177dc(%rip), %rdi     # 0x59fa6
movb $0x1, %al
callq 0x3160
testl %ebx, %ebx
movq 0x18(%rsp), %rcx
jle 0x42812
xorl %edx, %edx
movq %rdx, 0x70(%rsp)
movq 0x20(%rsp), %rax
movq (%rcx,%rax,8), %rax
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rdx,4), %xmm0
leaq 0x177ba(%rip), %rdi     # 0x59fb3
movb $0x1, %al
callq 0x3160
movq 0x70(%rsp), %rdx
movq 0x18(%rsp), %rcx
incq %rdx
cmpq %rdx, %rbx
jne 0x427dc
movl $0xa, %edi
callq 0x3060
movq 0x38(%rsp), %r11
movq 0x20(%rsp), %rcx
incq %rcx
cmpq $0x5a, %rcx
jne 0x42764
movq %r11, %rdi
callq 0x3040
xorl %eax, %eax
xorpd %xmm2, %xmm2
movq 0x10(%rsp), %rcx
cmpl $0x0, (%rcx,%rax,4)
je 0x4292a
testl %r13d, %r13d
jle 0x4292a
movl 0x84(%rsp), %ecx
imull %eax, %ecx
movl 0xc(%rsp), %edx
xorl %esi, %esi
incq %rsi
leal (%rcx,%rsi), %edi
addl 0xa8(%rsp), %edi
movslq %edi, %rdi
movq 0x40(%rsp), %r8
movss (%r8,%rdi,4), %xmm0
ucomiss %xmm2, %xmm0
jne 0x4288f
jnp 0x4291f
xorl %edi, %edi
testl %ebx, %ebx
setle %r8b
movq 0x10(%rsp), %r9
cmpl $0x0, (%r9,%rdi,4)
sete %r9b
orb %r8b, %r9b
jne 0x42912
movq 0x18(%rsp), %r8
movq (%r8,%rdi,8), %r8
movl %edx, %r9d
xorl %r10d, %r10d
movss (%r8,%r10,4), %xmm0
movslq %r9d, %r9
movss (%r14,%r9,4), %xmm1
xorl %r11d, %r11d
cmpq %rax, %rdi
jne 0x428e0
ucomiss %xmm0, %xmm1
seta %r11b
movsd (%rbp,%r11,8), %xmm0
jmp 0x428ed
ucomiss %xmm0, %xmm1
seta %r11b
movsd (%r15,%r11,8), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r12,%r9,4), %xmm1
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
movss %xmm0, (%r12,%r9,4)
incq %r10
addl %r13d, %r9d
cmpq %r10, %rbx
jne 0x428b9
incq %rdi
cmpq $0x5a, %rdi
jne 0x42891
incl %edx
cmpq %r13, %rsi
jne 0x42869
incq %rax
cmpq $0x5a, %rax
jne 0x42841
testl %ebx, %ebx
movl 0x80(%rsp), %edi
movsd 0x15d38(%rip), %xmm1    # 0x58680
movsd 0x15d20(%rip), %xmm2    # 0x58670
jle 0x4298c
xorl %eax, %eax
movl 0xc(%rsp), %ecx
testl %r13d, %r13d
jle 0x42983
movq %r13, %rdx
movl %ecx, %esi
movslq %esi, %rsi
xorps %xmm0, %xmm0
cvtss2sd (%r12,%rsi,4), %xmm0
mulsd %xmm2, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r12,%rsi,4)
incl %esi
decq %rdx
jne 0x42962
incl %eax
addl %r13d, %ecx
cmpl %ebx, %eax
jne 0x42958
movq 0x28(%rsp), %rdx
incq %rdx
movq 0x88(%rsp), %rax
movq 0x30(%rsp), %rcx
addl %eax, %ecx
movq %rcx, 0x30(%rsp)
addl %eax, 0xc(%rsp)
movq 0x58(%rsp), %rax
addq 0x90(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x50(%rsp), %rax
addq 0xc0(%rsp), %rax
movq %rax, 0x50(%rsp)
movq %rdx, 0x28(%rsp)
cmpq 0x48(%rsp), %rdx
jne 0x4245e
movq %r12, %rdi
movq 0xb0(%rsp), %rsi
callq 0xf92d
mulss %xmm0, %xmm0
leaq 0x120(%rsp), %rax
movq 0x1d8(%rax), %rax
movss %xmm0, (%rax)
xorl %eax, %eax
callq 0xe79c
subsd 0xb8(%rsp), %xmm0
leaq 0x1759c(%rip), %rdi     # 0x59fba
movb $0x1, %al
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3160