import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused_binary_cross_entropy_with_logits_0(in_ptr0, in_ptr1,
    out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex % 16
    r1 = rindex // 16
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
    tmp3 = tl.load(in_ptr1 + (r0 + 64 * r1), None)
    tmp1 = 1.0
    tmp2 = tmp1 - tmp0
    tmp4 = tmp2 * tmp3
    tmp5 = 0.0
    tmp6 = triton_helpers.minimum(tmp5, tmp3)
    tmp7 = tl_math.abs(tmp3)
    tmp8 = -tmp7
    tmp9 = tl_math.exp(tmp8)
    tmp10 = libdevice.log1p(tmp9)
    tmp11 = tmp6 - tmp10
    tmp12 = tmp4 - tmp11
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.sum(tmp13, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp15, None)


@triton.jit
def triton_per_fused_add_binary_cross_entropy_with_logits_mean_mul_pow_rsub_sigmoid_1(
    in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    rnumel = 192
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    rmask = rindex < rnumel
    r0 = rindex % 48
    r1 = rindex // 48
    tmp0 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), rmask, other=0.0)
    tmp3 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), rmask, other=0.0)
    tmp28 = tl.load(in_out_ptr0 + 0)
    tmp29 = tl.broadcast_to(tmp28, [XBLOCK, 1])
    tmp1 = 0.25
    tmp2 = tmp0 * tmp1
    tmp4 = tl.sigmoid(tmp3)
    tmp5 = 1.0
    tmp6 = tmp5 - tmp4
    tmp7 = tmp6 * tmp6
    tmp8 = tmp2 * tmp7
    tmp9 = tmp5 - tmp0
    tmp10 = tmp9 * tmp3
    tmp11 = 0.0
    tmp12 = triton_helpers.minimum(tmp11, tmp3)
    tmp13 = tl_math.abs(tmp3)
    tmp14 = -tmp13
    tmp15 = tl_math.exp(tmp14)
    tmp16 = libdevice.log1p(tmp15)
    tmp17 = tmp12 - tmp16
    tmp18 = tmp10 - tmp17
    tmp19 = tmp8 * tmp18
    tmp20 = tmp4 * tmp4
    tmp21 = tmp9 * tmp20
    tmp22 = tmp21 * tmp18
    tmp23 = tmp19 + tmp22
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp26 = tl.where(rmask, tmp24, 0)
    tmp27 = tl.sum(tmp26, 1)[:, None]
    tmp30 = 64.0
    tmp31 = tmp29 / tmp30
    tmp32 = tmp31 * tmp5
    tmp33 = 192.0
    tmp34 = tmp27 / tmp33
    tmp35 = tmp34 * tmp5
    tmp36 = tmp32 + tmp35
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp36, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_binary_cross_entropy_with_logits_0[grid(1)](arg1_1,
            arg0_1, buf0, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        buf2 = buf0
        del buf0
        triton_per_fused_add_binary_cross_entropy_with_logits_mean_mul_pow_rsub_sigmoid_1[
            grid(1)](buf2, arg1_1, arg0_1, 1, 192, XBLOCK=1, num_warps=2,
            num_stages=1)
        del arg0_1
        del arg1_1
    return buf2,


class BCEFocalLoss(nn.Module):

    def __init__(self, alpha=0.25, gamma=2.0):
        super().__init__()
        self.alpha = alpha
        self.gamma = gamma

    def forward(self, preds, targets):
        bce_loss = nn.BCEWithLogitsLoss(reduction='none')(preds, targets)
        probas = torch.sigmoid(preds)
        loss = targets * self.alpha * (1.0 - probas
            ) ** self.gamma * bce_loss + (1.0 - targets
            ) * probas ** self.gamma * bce_loss
        loss = loss.mean()
        return loss


class SeparatedLossNew(nn.Module):

    def __init__(self, loss_disease_risk='BCEWithLogitsLoss', loss_disease=
        'BCEFocalLoss', weights=[1.0, 1.0]):
        super().__init__()
        if loss_disease_risk == 'BCEWithLogitsLoss':
            self.loss_disease_risk = nn.BCEWithLogitsLoss()
        elif loss_disease_risk == 'BCEFocalLoss':
            self.loss_disease_risk = BCEFocalLoss()
        else:
            raise NotImplementedError
        if loss_disease == 'BCEWithLogitsLoss':
            self.loss_disease = nn.BCEWithLogitsLoss()
        elif loss_disease == 'BCEFocalLoss':
            self.loss_disease = BCEFocalLoss()
        self.weights = weights

    def forward(self, input_0, input_1):
        arg0_1 = input_0
        arg1_1 = input_1
        output = call([arg0_1, arg1_1])
        return output[0]


Measured latency_ms: 0.027123