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.triton_helpers import libdevice
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_cat_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    RBLOCK: tl.constexpr = 8
    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)
    r2 = rindex
    x0 = xindex % 16
    x1 = xindex // 16
    x3 = xindex
    tmp15 = tl.load(in_ptr1 + (x0 + 16 * r2 + 128 * x1), xmask, other=0.0)
    tmp0 = r2
    tl.full([1, 1], 0, tl.int64)
    tmp3 = tl.full([1, 1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 16 * r2 + 64 * x1), tmp4 & xmask, other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1, 1], 8, tl.int64)
    tmp9 = tl.load(in_ptr0 + (x0 + 16 * (-4 + r2) + 64 * x1), tmp6 & xmask,
        other=0.0)
    tmp10 = 1.0
    tmp11 = tmp10 - tmp9
    tmp12 = tl.full(tmp11.shape, 0.0, tmp11.dtype)
    tmp13 = tl.where(tmp6, tmp11, tmp12)
    tmp14 = tl.where(tmp4, tmp5, tmp13)
    tmp16 = tmp14 * tmp15
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, 0)
    tmp20 = tl.sum(tmp19, 1)[:, None]
    tl.store(out_ptr0 + x3, tmp20, xmask)


@triton.jit
def triton_per_fused__to_copy_add_ge_gt_le_log2_lt_mean_mul_pow_rsub_1(
    in_out_ptr0, in_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
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = 1.0
    tmp2 = tmp1 - tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = -0.25
    tmp5 = tmp3 * tmp4
    tmp6 = 1e-12
    tmp7 = tmp0 >= tmp6
    tmp8 = tmp7.to(tl.float32)
    tmp9 = tmp8 * tmp0
    tmp10 = tmp0 < tmp6
    tmp11 = tmp10.to(tl.float32)
    tmp12 = tmp11 * tmp6
    tmp13 = tmp9 + tmp12
    tmp14 = tmp13 <= tmp1
    tmp15 = tmp14.to(tl.float32)
    tmp16 = tmp15 * tmp13
    tmp17 = tmp13 > tmp1
    tmp18 = tmp17.to(tl.float32)
    tmp19 = tmp18 * tmp1
    tmp20 = tmp16 + tmp19
    tmp21 = libdevice.log2(tmp20)
    tmp22 = tmp5 * tmp21
    tmp23 = tl.broadcast_to(tmp22, [XBLOCK, RBLOCK])
    tmp25 = tl.sum(tmp23, 1)[:, None]
    tmp26 = 64.0
    tmp27 = tmp25 / tmp26
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp27, 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, 8, 4, 4), (128, 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_per_fused_cat_mul_sum_0[grid(64)](arg0_1, arg1_1, buf0, 64, 
            8, XBLOCK=8, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = buf1
        del buf1
        triton_per_fused__to_copy_add_ge_gt_le_log2_lt_mean_mul_pow_rsub_1[grid
            (1)](buf2, buf0, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del buf0
    return buf2,


def clip_by_tensor(t, t_min, t_max):
    """
    clip_by_tensor
    :param t: tensor
    :param t_min: min
    :param t_max: max
    :return: cliped tensor
    """
    t = t.float()
    result = (t >= t_min).float() * t + (t < t_min).float() * t_min
    result = (result <= t_max).float() * result + (result > t_max).float(
        ) * t_max
    return result


class focal_lossNew(nn.Module):

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

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


Measured latency_ms: 0.063842