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 math as tl_math
import torch.nn as nn
import torch.nn.functional as F
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__to_copy_abs_add_lt_mean_mul_pow_rsub_sub_sum_0(
    in_out_ptr0, in_ptr0, in_ptr1, 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)
    tmp1 = tl.load(in_ptr1 + (r0 + 64 * r1), None)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp4 = 0.1
    tmp5 = tmp3 < tmp4
    tmp6 = tmp5.to(tl.float32)
    tmp7 = tmp3 * tmp3
    tmp8 = tmp6 * tmp7
    tmp9 = 5.0
    tmp10 = tmp8 * tmp9
    tmp11 = 1.0
    tmp12 = tmp11 - tmp6
    tmp13 = tmp3 - tmp4
    tmp14 = 0.05000000000000001
    tmp15 = tmp13 + tmp14
    tmp16 = tmp12 * tmp15
    tmp17 = tmp10 + tmp16
    tmp18 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK])
    tmp20 = tl.sum(tmp18, 1)[:, None]
    tmp21 = 64.0
    tmp22 = tmp20 / tmp21
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp22, 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)
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused__to_copy_abs_add_lt_mean_mul_pow_rsub_sub_sum_0[grid
            (1)](buf1, arg1_1, arg0_1, 1, 64, XBLOCK=1, num_warps=2,
            num_stages=1)
        del arg0_1
        del arg1_1
    return buf1,


def adjust_smooth_l1_loss(y_pred, theta=0.1):
    less_grad_factor = 1.0 / (2 * theta)
    less_loss_bias = less_grad_factor * theta ** 2
    less_than_theta = (y_pred < theta).float()
    loss = less_than_theta * y_pred ** 2 * less_grad_factor + (1 -
        less_than_theta) * (y_pred - theta + less_loss_bias)
    return loss


class CrossEntropyLoss(nn.Module):

    def __init__(self, weight=None, ignore_index=255, reduction='mean'):
        """ NLLLoss: negative log likelihood loss.
            # nll_loss: weights: None | a tensor of size C
                        pred in [N, C, d1, d2, ..., dk]
                        target in [N, d1, d2, ..., dk]
                        output in [N, d1, d2, ..., dk]
        """
        super(CrossEntropyLoss, self).__init__()
        self.nll_loss = nn.NLLLoss(weight, ignore_index=ignore_index,
            reduction=reduction)

    def forward(self, inputs, targets):
        return self.nll_loss(F.log_softmax(inputs, dim=1), targets)


class BinaryLossNew(nn.Module):
    """This class computes the Binary loss to force BG pixels close to 0 and FG pixels far away.
    """

    def __init__(self, margin=2.0, FG_stCH=1, loss_type='l1', weights=None):
        """
        margin: minimum distance between FG/BG if prediction with 1 channel
        FG_stCH: start channel of FG objects on prediction with multiple channels
        loss_type: 'l1' | 'CE', works for prediction with multiple channel.
                   if 'l1', prediction is expected to be softmax2d output.
                   if 'CE', prediction is expected to be net logits
                   if prediction has channel=1,
        weights: if not None, a tensor of size C
        """
        super(BinaryLossNew, self).__init__()
        self.margin = margin
        self.FG_stCH = FG_stCH
        self.loss_type = loss_type if FG_stCH > 1 else 'l1'
        if self.loss_type == 'CE':
            self.CE_loss = CrossEntropyLoss(weight=weights, reduction='none')

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