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
import torch.nn as nn
from itertools import repeat
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused_index_pow_sub_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp1 = tl.full([XBLOCK], 4, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp4 < 4')
    tmp6 = tl.load(in_ptr1 + 4 * tmp4, xmask, eviction_policy='evict_last')
    tmp8 = tmp7 + tmp1
    tmp9 = tmp7 < 0
    tmp10 = tl.where(tmp9, tmp8, tmp7)
    tl.device_assert((0 <= tmp10) & (tmp10 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp10 < 4')
    tmp12 = tl.load(in_ptr1 + 4 * tmp10, xmask, eviction_policy='evict_last')
    tmp13 = tmp6 - tmp12
    tmp14 = tmp13 * tmp13
    tmp15 = tl.load(in_ptr1 + (1 + 4 * tmp4), xmask, eviction_policy=
        'evict_last')
    tmp16 = tl.load(in_ptr1 + (1 + 4 * tmp10), xmask, eviction_policy=
        'evict_last')
    tmp17 = tmp15 - tmp16
    tmp18 = tmp17 * tmp17
    tmp19 = tmp14 + tmp18
    tmp20 = tl.load(in_ptr1 + (2 + 4 * tmp4), xmask, eviction_policy=
        'evict_last')
    tmp21 = tl.load(in_ptr1 + (2 + 4 * tmp10), xmask, eviction_policy=
        'evict_last')
    tmp22 = tmp20 - tmp21
    tmp23 = tmp22 * tmp22
    tmp24 = tmp19 + tmp23
    tmp25 = tl.load(in_ptr1 + (3 + 4 * tmp4), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_ptr1 + (3 + 4 * tmp10), xmask, eviction_policy=
        'evict_last')
    tmp27 = tmp25 - tmp26
    tmp28 = tmp27 * tmp27
    tmp29 = tmp24 + tmp28
    tl.store(out_ptr0 + x0, tmp29, xmask)


@triton.jit
def triton_poi_fused_mul_new_full_pow_scatter_add_sub_1(out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = 0.0
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_mul_new_full_pow_scatter_add_sub_2(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr1 + x2, xmask)
    tmp3 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + x2, xmask)
    tmp10 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tl.device_assert((0 <= tmp0) & (tmp0 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp0 < 4')
    tmp4 = tmp3.to(tl.float32)
    tmp5 = libdevice.sqrt(tmp4)
    tmp7 = tmp5 - tmp6
    tmp8 = tmp7 * tmp7
    tmp9 = tmp2 * tmp8
    tl.device_assert((0 <= tmp10) & (tmp10 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp10 < 4')
    tl.atomic_add(out_ptr0 + (x0 + 4 * tmp0), tmp9, xmask, sem='relaxed')
    tl.atomic_add(out_ptr1 + (x0 + 4 * tmp10), tmp9, xmask, sem='relaxed')


@triton.jit
def triton_poi_fused_add_mul_3(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp3 = tl.load(in_ptr1 + x0, xmask)
    tmp1 = 0.5
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3 * tmp1
    tmp5 = tmp2 + tmp4
    tl.store(out_ptr0 + x0, tmp5, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    assert_size_stride(arg2_1, (4, 4), (4, 1))
    assert_size_stride(arg3_1, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4,), (1,), torch.int64)
        get_raw_stream(0)
        triton_poi_fused_index_pow_sub_sum_0[grid(4)](arg0_1, arg1_1, buf0,
            4, XBLOCK=4, num_warps=1, num_stages=1)
        del arg1_1
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_mul_new_full_pow_scatter_add_sub_1[grid(16)](buf1,
            16, XBLOCK=16, num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_mul_new_full_pow_scatter_add_sub_1[grid(16)](buf3,
            16, XBLOCK=16, num_warps=1, num_stages=1)
        triton_poi_fused_mul_new_full_pow_scatter_add_sub_2[grid(16)](arg0_1,
            arg3_1, buf0, arg2_1, buf1, buf3, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        del arg0_1
        del arg2_1
        del arg3_1
        del buf0
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_mul_3[grid(16)](buf1, buf3, buf5, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        del buf1
        del buf3
    return buf5,


def gen(src, index, dim=-1, out=None, dim_size=None, fill_value=0):
    dim = range(src.dim())[dim]
    if index.dim() == 1:
        index_size = list(repeat(1, src.dim()))
        index_size[dim] = src.size(dim)
        index = index.view(index_size).expand_as(src)
    if out is None:
        dim_size = index.max().item() + 1 if dim_size is None else dim_size
        out_size = list(src.size())
        out_size[dim] = dim_size
        out = src.new_full(out_size, fill_value)
    return src, out, index, dim


def scatter_add(src, index, dim=-1, out=None, dim_size=None, fill_value=0):
    src, out, index, dim = gen(src, index, dim, out, dim_size, fill_value)
    return out.scatter_add_(dim, index, src)


class BondEnergyModuleNew(nn.Module):

    def __init__(self, batch=True):
        super().__init__()

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


Measured latency_ms: 0.066423