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_abs_add_mean_sub_0(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, 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)
    tmp7 = tl.load(in_ptr2 + r0, None)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
    tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
    tmp8 = tmp7 - tmp0
    tmp9 = tmp7 - tmp1
    tmp10 = tmp8 - tmp9
    tmp11 = tl_math.abs(tmp10)
    tmp12 = tl.broadcast_to(tmp11, [RBLOCK])
    tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp12, 0))
    tmp15 = 256.0
    tmp16 = tmp6 / tmp15
    tmp17 = tmp14 / tmp15
    tmp18 = tmp16 + tmp17
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp18, None)


def call(args):
    arg0_1, arg1_1, arg2_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))
    assert_size_stride(arg2_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)
        buf2 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused_abs_add_mean_sub_0[grid(1)](buf2, arg2_1, arg1_1,
            arg0_1, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
    return buf2,


class EnergyConservingLossNew(nn.L1Loss):
    """Energy conserving loss.

    A two term loss that enforces energy conservation after
    :cite:`Rethage2018`.

    The loss can be described as:

    .. math::
        \\ell(x, y, m) = L = \\{l_1,\\dots,l_N\\}^\\top, \\quad
        l_n = |x_n - y_n| + |b_n - \\hat{b_n}|,

    where :math:`N` is the batch size. If reduction is not ``'none'``, then:

    .. math::
        \\ell(x, y, m) =
        \\begin{cases}
            \\operatorname{mean}(L), & \\text{if reduction} = \\text{'mean';}\\\\
            \\operatorname{sum}(L),  & \\text{if reduction} = \\text{'sum'.}
        \\end{cases}

    :math:`x` is the input signal (estimated target), :math:`y` the target
    signal, :math:`m` the mixture signal, :math:`b` the background signal given
    by :math:`b = m - y`, and :math:`\\hat{b}` the estimated background signal
    given by :math:`\\hat{b} = m - x`.

    Args:
        reduction (string, optional): specifies the reduction to apply to the
            output: 'none' | 'mean' | 'sum'.
            'none': no reduction will be applied, 'mean': the sum of the output
            will be divided by the number of elements in the output, 'sum': the
            output will be summed.

    Shape:
        - Input: :math:`(N, *)` where `*` means, any number of additional
          dimensions
        - Target: :math:`(N, *)`, same shape as the input
        - Mixture: :math:`(N, *)`, same shape as the input
        - Output: scalar. If reduction is ``'none'``, then :math:`(N, *)`, same
          shape as the input

    Examples:
        >>> import torch
        >>> _ = torch.manual_seed(0)
        >>> loss = EnergyConservingLoss()
        >>> input = torch.randn(3, 5, requires_grad=True)
        >>> target = torch.randn(3, 5)
        >>> mixture = torch.randn(3, 5)
        >>> loss(input, target, mixture)
        tensor(2.1352, grad_fn=<AddBackward0>)

    """

    def __init__(self, *, reduction='mean'):
        super().__init__(None, None, reduction)

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


Measured latency_ms: 0.015796