import torch
import torch.nn as nn


class WingLoss(nn.Module):

    def __init__(self, l1_log_cutoff, epsilon):
        super().__init__()
        self.l1_log_cutoff = l1_log_cutoff
        self.epsilon = epsilon
        log_val = torch.log(torch.FloatTensor([1 + self.l1_log_cutoff /
            self.epsilon])).item()
        self.link_constant = self.l1_log_cutoff - self.l1_log_cutoff * log_val

    def forward(self, x, y):
        assert x.shape == y.shape
        n_dims = len(x.shape)
        n_samples = x.size(0) if n_dims > 0 else 1
        n_vertices = x.size(1) if n_dims > 1 else 1
        diff = x - y
        abs_diff = diff.abs()
        is_item_in_l1_zone = torch.ge(abs_diff, self.l1_log_cutoff).float()
        is_item_in_log_zone = 1 - is_item_in_l1_zone
        log_val = self.l1_log_cutoff * torch.log(1 + abs_diff / self.epsilon)
        res = is_item_in_l1_zone * (abs_diff - self.link_constant
            ) + is_item_in_log_zone * log_val
        res = res.sum() / (n_samples * n_vertices)
        return res


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {'l1_log_cutoff': 4, 'epsilon': 4}]

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 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__to_copy_abs_add_div_ge_log_mul_rsub_sub_sum_0(in_out_ptr0
    , in_ptr0, in_ptr1, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp4 = 4.0
    tmp5 = tmp3 >= tmp4
    tmp6 = tmp5.to(tl.float32)
    tmp7 = 1.2274112701416016
    tmp8 = tmp3 - tmp7
    tmp9 = tmp6 * tmp8
    tmp10 = 1.0
    tmp11 = tmp10 - tmp6
    tmp12 = 0.25
    tmp13 = tmp3 * tmp12
    tmp14 = tmp13 + tmp10
    tmp15 = tl_math.log(tmp14)
    tmp16 = tmp15 * tmp4
    tmp17 = tmp11 * tmp16
    tmp18 = tmp9 + tmp17
    tmp19 = tl.broadcast_to(tmp18, [RBLOCK])
    tmp21 = triton_helpers.promote_to_tensor(tl.sum(tmp19, 0))
    tmp22 = 0.0625
    tmp23 = tmp21 * tmp22
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp23, 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_div_ge_log_mul_rsub_sub_sum_0[grid(1)
            ](buf1, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf1,


class WingLossNew(nn.Module):

    def __init__(self, l1_log_cutoff, epsilon):
        super().__init__()
        self.l1_log_cutoff = l1_log_cutoff
        self.epsilon = epsilon
        log_val = torch.log(torch.FloatTensor([1 + self.l1_log_cutoff /
            self.epsilon])).item()
        self.link_constant = self.l1_log_cutoff - self.l1_log_cutoff * log_val

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