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
import torch.nn as nn
import torch.onnx
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_clamp_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp9 = tl.load(in_ptr0 + x0, xmask)
    tmp0 = tl.full([1], 0, tl.int64)
    tmp1 = tmp0 == tmp0
    tmp2 = tl.full([1], 0, tl.int32)
    tmp3 = tmp2 == tmp2
    tmp4 = tl.load(in_ptr0 + x0, tmp1 & xmask, other=0.0)
    tmp5 = tl.sigmoid(tmp4)
    tmp6 = tl.where(tmp3, tmp5, tmp4)
    tmp7 = tl.full(tmp6.shape, 0.0, tmp6.dtype)
    tmp8 = tl.where(tmp1, tmp6, tmp7)
    tmp10 = tl.where(tmp1, tmp8, tmp9)
    tmp11 = 0.0001
    tmp12 = triton_helpers.maximum(tmp10, tmp11)
    tmp13 = 0.9999
    tmp14 = triton_helpers.minimum(tmp12, tmp13)
    tl.store(out_ptr0 + x0, tmp14, xmask)


@triton.jit
def triton_poi_fused_sigmoid_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 64
    x0 = xindex % 64
    x2 = xindex
    tmp35 = tl.load(in_ptr0 + x2, xmask)
    tmp0 = x1
    tmp1 = tl.full([1], 1, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = (-1 + x1) % 9
    tmp4 = tl.full([1], 0, tl.int64)
    tmp5 = tmp3 == tmp4
    tmp6 = tmp2 & tmp5
    tmp7 = triton_helpers.div_floor_integer(-1 + x1, 9)
    tmp8 = tl.full([1], 0, tl.int32)
    tmp9 = tmp7 == tmp8
    tmp10 = tmp1 == tmp4
    tmp11 = tmp10 & tmp6
    tmp12 = tmp8 == tmp8
    tmp13 = tl.load(in_ptr0 + x0, tmp11 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp14 = tl.sigmoid(tmp13)
    tmp15 = tl.where(tmp12, tmp14, tmp13)
    tmp16 = tl.full(tmp15.shape, 0.0, tmp15.dtype)
    tmp17 = tl.where(tmp11, tmp15, tmp16)
    tmp18 = tl.load(in_ptr0 + (64 + x0), tmp6 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp19 = tl.where(tmp10, tmp17, tmp18)
    tmp20 = tl.sigmoid(tmp19)
    tmp21 = tl.where(tmp9, tmp14, tmp13)
    tmp22 = tl.full(tmp21.shape, 0.0, tmp21.dtype)
    tmp23 = tl.where(tmp11, tmp21, tmp22)
    tmp24 = tl.load(in_ptr0 + (64 + x0 + 576 * triton_helpers.
        div_floor_integer(-1 + x1, 9)), tmp6 & xmask, other=0.0)
    tmp25 = tl.where(tmp10, tmp23, tmp24)
    tmp26 = tl.where(tmp9, tmp20, tmp25)
    tmp27 = tl.full(tmp26.shape, 0.0, tmp26.dtype)
    tmp28 = tl.where(tmp6, tmp26, tmp27)
    tmp29 = tmp0 == tmp4
    tmp30 = tl.load(in_ptr0 + x0, tmp29 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp31 = tl.sigmoid(tmp30)
    tmp32 = tl.where(tmp12, tmp31, tmp30)
    tmp33 = tl.full(tmp32.shape, 0.0, tmp32.dtype)
    tmp34 = tl.where(tmp29, tmp32, tmp33)
    tmp36 = tl.where(tmp29, tmp34, tmp35)
    tmp37 = tl.where(tmp6, tmp28, tmp36)
    tl.store(out_ptr0 + x2, tmp37, xmask)


@triton.jit
def triton_poi_fused_clamp_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (64 + x0), xmask)
    tmp1 = 0.0001
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp3 = 0.9999
    tmp4 = triton_helpers.minimum(tmp2, tmp3)
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused_clamp_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp12 = tl.load(in_ptr0 + (128 + x0), xmask)
    tmp0 = tl.full([1], 2, tl.int64)
    tmp1 = tmp0 >= tmp0
    tmp2 = tl.full([1], 0, tl.int64)
    tmp3 = tmp2 == tmp2
    tmp4 = tmp1 & tmp3
    tmp5 = tl.full([1], 0, tl.int32)
    tmp6 = tmp5 == tmp5
    tmp7 = tl.load(in_ptr0 + (128 + x0), tmp4 & xmask, other=0.0)
    tmp8 = tl.sigmoid(tmp7)
    tmp9 = tl.where(tmp6, tmp8, tmp7)
    tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype)
    tmp11 = tl.where(tmp4, tmp9, tmp10)
    tmp13 = tl.where(tmp4, tmp11, tmp12)
    tmp14 = 0.0001
    tmp15 = triton_helpers.maximum(tmp13, tmp14)
    tmp16 = 0.9999
    tmp17 = triton_helpers.minimum(tmp15, tmp16)
    tl.store(out_ptr0 + x0, tmp17, xmask)


@triton.jit
def triton_poi_fused_sigmoid_4(in_ptr0, out_ptr0, out_ptr1, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 64
    x0 = xindex % 64
    x2 = xindex
    tmp50 = tl.load(in_ptr0 + x2, xmask)
    tmp0 = x1
    tmp1 = tl.full([1], 3, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = (-3 + x1) % 9
    tmp4 = tl.full([1], 0, tl.int64)
    tmp5 = tmp3 == tmp4
    tmp6 = tmp2 & tmp5
    tmp7 = triton_helpers.div_floor_integer(-3 + x1, 9)
    tmp8 = tl.full([1], 0, tl.int32)
    tmp9 = tmp7 == tmp8
    tmp10 = tl.full([1], 2, tl.int64)
    tmp11 = tmp1 >= tmp10
    tmp12 = tl.full([1], 1, tl.int64)
    tmp13 = tmp12 == tmp4
    tmp14 = tmp11 & tmp13
    tmp15 = tmp14 & tmp6
    tmp16 = tmp8 == tmp8
    tmp17 = tl.load(in_ptr0 + (128 + x0), tmp15 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp18 = tl.sigmoid(tmp17)
    tmp19 = tl.where(tmp16, tmp18, tmp17)
    tmp20 = tl.full(tmp19.shape, 0.0, tmp19.dtype)
    tmp21 = tl.where(tmp15, tmp19, tmp20)
    tmp22 = tl.load(in_ptr0 + (192 + x0), tmp6 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp23 = tl.where(tmp14, tmp21, tmp22)
    tmp24 = tl.sigmoid(tmp23)
    tmp25 = 3 + 9 * triton_helpers.div_floor_integer(-3 + x1, 9)
    tmp26 = tmp25 >= tmp10
    tmp27 = tmp26 & tmp13
    tmp28 = tmp27 & tmp6
    tmp29 = tl.load(in_ptr0 + (128 + x0), tmp28 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp30 = tl.sigmoid(tmp29)
    tmp31 = tl.where(tmp9, tmp30, tmp29)
    tmp32 = tl.full(tmp31.shape, 0.0, tmp31.dtype)
    tmp33 = tl.where(tmp28, tmp31, tmp32)
    tmp34 = tl.load(in_ptr0 + (192 + x0 + 576 * triton_helpers.
        div_floor_integer(-3 + x1, 9)), tmp6 & xmask, other=0.0)
    tmp35 = tl.where(tmp27, tmp33, tmp34)
    tmp36 = tl.where(tmp9, tmp24, tmp35)
    tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype)
    tmp38 = tl.where(tmp6, tmp36, tmp37)
    tmp39 = tmp0 >= tmp10
    tmp40 = (-2 + x1) % 9
    tmp41 = tmp40 == tmp4
    tmp42 = tmp39 & tmp41
    tmp43 = triton_helpers.div_floor_integer(-2 + x1, 9)
    tmp44 = tmp43 == tmp8
    tmp45 = tl.load(in_ptr0 + (128 + x0), tmp42 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp46 = tl.sigmoid(tmp45)
    tmp47 = tl.where(tmp44, tmp46, tmp45)
    tmp48 = tl.full(tmp47.shape, 0.0, tmp47.dtype)
    tmp49 = tl.where(tmp42, tmp47, tmp48)
    tmp51 = tl.where(tmp42, tmp49, tmp50)
    tmp52 = tl.where(tmp6, tmp38, tmp51)
    tl.store(out_ptr0 + x2, tmp52, xmask)
    tl.store(out_ptr1 + x2, tmp52, xmask)


@triton.jit
def triton_poi_fused_clamp_5(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (192 + x0), xmask)
    tmp1 = 0.0001
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp3 = 0.9999
    tmp4 = triton_helpers.minimum(tmp2, tmp3)
    tl.store(out_ptr0 + x0, tmp4, xmask)


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, (10, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clamp_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_sigmoid_1[grid(256)](arg0_1, buf1, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_clamp_2[grid(64)](buf1, buf2, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_clamp_3[grid(64)](buf1, buf3, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_sigmoid_4[grid(256)](buf1, buf4, arg0_1, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del arg0_1
        del buf1
        buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_clamp_5[grid(64)](buf4, buf5, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
    return buf0, reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 0
        ), buf2, buf3, buf5, reinterpret_tensor(buf4, (0, 4, 4, 4), (576, 
        16, 4, 1), 256), reinterpret_tensor(buf4, (0, 4, 4, 4), (576, 16, 4,
        1), 256), reinterpret_tensor(buf4, (0, 4, 4, 4), (576, 16, 4, 1), 256
        ), reinterpret_tensor(buf4, (0, 4, 4, 4), (576, 16, 4, 1), 256
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 64
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 128
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 192
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 256
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 320
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 384
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 448
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 512
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 576)


def _neg_loss(preds, gt):
    pos_inds = gt.eq(1)
    neg_inds = gt.lt(1)
    neg_weights = torch.pow(1 - gt[neg_inds], 4)
    loss = 0
    for pred in preds:
        pos_pred = pred[pos_inds]
        neg_pred = pred[neg_inds]
        pos_loss = torch.log(pos_pred) * torch.pow(1 - pos_pred, 2)
        neg_loss = torch.log(1 - neg_pred) * torch.pow(neg_pred, 2
            ) * neg_weights
        num_pos = pos_inds.float().sum()
        pos_loss = pos_loss.sum()
        neg_loss = neg_loss.sum()
        if pos_pred.nelement() == 0:
            loss = loss - neg_loss
        else:
            loss = loss - (pos_loss + neg_loss) / num_pos
    return loss


def _regr_loss(regr, gt_regr, mask):
    num = mask.float().sum()
    mask = mask.unsqueeze(2).expand_as(gt_regr)
    regr = regr[mask]
    gt_regr = gt_regr[mask]
    regr_loss = nn.functional.smooth_l1_loss(regr, gt_regr, size_average=False)
    regr_loss = regr_loss / (num + 0.0001)
    return regr_loss


def _sigmoid(x):
    x = torch.clamp(x.sigmoid_(), min=0.0001, max=1 - 0.0001)
    return x


class CTLossNew(nn.Module):

    def __init__(self, regr_weight=1, focal_loss=_neg_loss):
        super(CTLossNew, self).__init__()
        self.regr_weight = regr_weight
        self.focal_loss = focal_loss
        self.regr_loss = _regr_loss

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