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 import 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_mul_rsub_sum_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    out_ptr2, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, other=0.0)
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, 0)
    tmp6 = tl.sum(tmp5, 1)[:, None]
    tmp7 = 1.0
    tmp8 = tmp7 - tmp0
    tmp9 = tmp8 * tmp1
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK])
    tmp12 = tl.where(xmask, tmp10, 0)
    tmp13 = tl.sum(tmp12, 1)[:, None]
    tmp14 = tmp7 - tmp1
    tmp15 = tmp0 * tmp14
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp18 = tl.where(xmask, tmp16, 0)
    tmp19 = tl.sum(tmp18, 1)[:, None]
    tmp20 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp22 = tl.where(xmask, tmp20, 0)
    tmp23 = tl.sum(tmp22, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp6, xmask)
    tl.store(out_ptr1 + x0, tmp13, xmask)
    tl.store(out_ptr2 + x0, tmp19, xmask)
    tl.store(out_ptr3 + x0, tmp23, xmask)


@triton.jit
def triton_per_fused_add_clamp_div_mul_rsub_sum_1(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 16
    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
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp5 = tl.load(in_ptr1 + r0, None)
    tmp9 = tl.load(in_ptr2 + r0, None)
    tmp13 = tl.load(in_ptr3 + r0, None)
    tmp1 = 2.0
    tmp2 = tmp0 * tmp1
    tmp3 = 0.0001
    tmp4 = tmp2 + tmp3
    tmp6 = 1.0
    tmp7 = tmp5 * tmp6
    tmp8 = tmp2 + tmp7
    tmp10 = tmp8 + tmp9
    tmp11 = tmp10 + tmp3
    tmp12 = tmp4 / tmp11
    tmp14 = 0.0
    tmp15 = triton_helpers.maximum(tmp13, tmp14)
    tmp16 = triton_helpers.minimum(tmp15, tmp6)
    tmp17 = tmp12 * tmp16
    tmp18 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK])
    tmp20 = tl.sum(tmp18, 1)[:, None]
    tmp21 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp23 = tl.sum(tmp21, 1)[:, None]
    tmp24 = tmp23 + tmp3
    tmp25 = tmp20 / tmp24
    tmp26 = triton_helpers.maximum(tmp25, tmp14)
    tmp27 = triton_helpers.minimum(tmp26, tmp6)
    tmp28 = tmp6 - tmp27
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp28, 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((4, 4), (4, 1), torch.float32)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_mul_rsub_sum_0[grid(16)](arg0_1, arg1_1, buf0,
            buf1, buf2, buf3, 16, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf6 = buf4
        del buf4
        triton_per_fused_add_clamp_div_mul_rsub_sum_1[grid(1)](buf6, buf0,
            buf1, buf2, buf3, 1, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del buf0
        del buf1
        del buf2
        del buf3
    return buf6,


def fb_loss(preds, trues, beta):
    smooth = 0.0001
    beta2 = beta * beta
    batch = preds.size(0)
    classes = preds.size(1)
    preds = preds.view(batch, classes, -1)
    trues = trues.view(batch, classes, -1)
    weights = torch.clamp(trues.sum(-1), 0.0, 1.0)
    TP = (preds * trues).sum(2)
    FP = (preds * (1 - trues)).sum(2)
    FN = ((1 - preds) * trues).sum(2)
    Fb = ((1 + beta2) * TP + smooth) / ((1 + beta2) * TP + beta2 * FN + FP +
        smooth)
    Fb = Fb * weights
    score = Fb.sum() / (weights.sum() + smooth)
    return torch.clamp(score, 0.0, 1.0)


class FBLossNew(nn.Module):

    def __init__(self, beta=1):
        super().__init__()
        self.beta = beta

    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.034724