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 import 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_div_mean_mul_sub_sum_0(in_ptr0, in_ptr1, in_ptr2,
    out_ptr1, out_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)
    tmp14 = tl.load(in_ptr2 + r0, None)
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
    tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0))
    tmp6 = 256.0
    tmp7 = tmp5 / tmp6
    tmp8 = tmp1 - tmp7
    tmp9 = 2.0
    tmp10 = tmp8 * tmp9
    tmp11 = 0.25
    tmp12 = tmp10 * tmp11
    tmp13 = tmp12 * tmp0
    tmp15 = tmp13 * tmp14
    tmp16 = tl.broadcast_to(tmp15, [RBLOCK])
    tmp18 = triton_helpers.promote_to_tensor(tl.sum(tmp16, 0))
    tl.store(out_ptr1 + tl.broadcast_to(r0, [RBLOCK]), tmp12, None)
    tl.store(out_ptr2 + 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)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf2 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_div_mean_mul_sub_sum_0[grid(1)](arg1_1, arg0_1,
            arg2_1, buf1, buf2, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
    return buf2, buf1


class WaveFunctionLoss(nn.Module):
    """Base class for all wave function loss functions.

    Any such loss must be derived from the local energy and wave function
    values, :math:`L(\\{E_\\text{loc}[\\psi],\\ln|\\psi|,w\\})`, using also
    importance-sampling weights *w*.

    Shape:
        - Input1, :math:`E_\\text{loc}[\\psi](\\mathbf r)`: :math:`(*)`
        - Input2, :math:`\\ln|\\psi(\\mathbf r)|`: :math:`(*)`
        - Input3, :math:`w(\\mathbf r)`: :math:`(*)`
        - Output, *L*: :math:`()`
    """
    pass


class LossEnergyNew(WaveFunctionLoss):
    """Total energy loss function.

    .. math::
        L:=2\\mathbb E\\big[(E_\\text{loc}-\\mathbb E[E_\\text{loc}])\\ln|\\psi|\\big]

    Taking a derivative of only the logarithm, the resulting gradient is equivalent,
    thanks to the Hermitian property of the Hamiltonian, to the gradient of the
    plain total energy loss function, :math:`\\mathbb E[E_\\text{loc}]`.
    """

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