import torch
from torch._inductor.select_algorithm import extern_kernels
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
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_exp_0(in_ptr0, 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)
    tmp1 = tl_math.exp(tmp0)
    tl.store(out_ptr0 + x0, tmp1, xmask)


@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_rsub_1(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 = 3 + -2 * x0
    tmp1 = tmp0.to(tl.float32)
    tl.store(out_ptr0 + x0, tmp1, xmask)


@triton.jit
def triton_poi_fused_ones_2(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 = 1.0
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_abs_sub_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex // 4
    x0 = xindex % 4
    x2 = xindex // 16
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tl.store(out_ptr0 + x4, tmp3, xmask)


@triton.jit
def triton_poi_fused__softmax_div_4(in_ptr0, in_ptr1, 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
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + (x0 + 16 * x1), xmask)
    tmp5 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp6 = tl.load(in_ptr1 + (4 + x0 + 16 * x1), xmask)
    tmp10 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp11 = tl.load(in_ptr1 + (8 + x0 + 16 * x1), xmask)
    tmp15 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp16 = tl.load(in_ptr1 + (12 + x0 + 16 * x1), xmask)
    tmp2 = tmp0 - tmp1
    tmp3 = 1.0
    tmp4 = tmp2 * tmp3
    tmp7 = tmp5 - tmp6
    tmp8 = tmp7 * tmp3
    tmp9 = triton_helpers.maximum(tmp4, tmp8)
    tmp12 = tmp10 - tmp11
    tmp13 = tmp12 * tmp3
    tmp14 = triton_helpers.maximum(tmp9, tmp13)
    tmp17 = tmp15 - tmp16
    tmp18 = tmp17 * tmp3
    tmp19 = triton_helpers.maximum(tmp14, tmp18)
    tmp20 = tmp4 - tmp19
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp8 - tmp19
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tmp21 + tmp23
    tmp25 = tmp13 - tmp19
    tmp26 = tl_math.exp(tmp25)
    tmp27 = tmp24 + tmp26
    tmp28 = tmp18 - tmp19
    tmp29 = tl_math.exp(tmp28)
    tmp30 = tmp27 + tmp29
    tl.store(out_ptr0 + x2, tmp19, xmask)
    tl.store(out_ptr1 + x2, tmp30, xmask)


@triton.jit
def triton_poi_fused__softmax_div_5(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.
    constexpr):
    ynumel = 16
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y3 = yindex
    y1 = yindex // 4
    y0 = yindex % 4
    tmp0 = tl.load(in_ptr0 + (x2 + 4 * y3), xmask & ymask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + (x2 + 4 * y3), xmask & ymask, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr2 + (x2 + 4 * y1), xmask & ymask, eviction_policy=
        'evict_last')
    tmp8 = tl.load(in_ptr3 + (x2 + 4 * y1), xmask & ymask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 - tmp1
    tmp3 = 1.0
    tmp4 = tmp2 * tmp3
    tmp6 = tmp4 - tmp5
    tmp7 = tl_math.exp(tmp6)
    tmp9 = tmp7 / tmp8
    tl.store(out_ptr0 + (y0 + 4 * x2 + 16 * y1), tmp9, xmask & ymask)
    tl.store(out_ptr1 + (x2 + 4 * y3), tmp4, xmask & ymask)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_exp_0[grid(16)](arg1_1, buf0, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del arg1_1
        buf1 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_poi_fused__to_copy_add_arange_mul_rsub_1[grid(4)](buf1, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf0, (16, 1), (1, 0), 0),
            reinterpret_tensor(buf1, (1, 4), (0, 1), 0), out=buf2)
        buf3 = reinterpret_tensor(buf1, (4, 1), (1, 1), 0)
        del buf1
        triton_poi_fused_ones_2[grid(4)](buf3, 4, XBLOCK=4, num_warps=1,
            num_stages=1)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf3, reinterpret_tensor(buf3, (1, 4), (1, 1), 0),
            out=buf4)
        del buf3
        buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_abs_sub_3[grid(64)](buf0, buf5, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf5, (16, 4), (4, 1), 0),
            buf4, out=buf6)
        buf7 = reinterpret_tensor(buf4, (4, 4, 1), (4, 1, 16), 0)
        del buf4
        buf8 = reinterpret_tensor(buf0, (4, 4, 1), (4, 1, 16), 0)
        del buf0
        triton_poi_fused__softmax_div_4[grid(16)](buf2, buf6, buf7, buf8, 
            16, XBLOCK=16, num_warps=1, num_stages=1)
        buf9 = buf5
        del buf5
        buf11 = empty_strided_cuda((4, 4, 4), (16, 1, 4), torch.float32)
        triton_poi_fused__softmax_div_5[grid(16, 4)](buf2, buf6, buf7, buf8,
            buf9, buf11, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        del buf2
        del buf7
        del buf8
        buf10 = reinterpret_tensor(buf6, (4, 4, 4), (16, 4, 1), 0)
        del buf6
        extern_kernels.bmm(buf9, arg0_1, out=buf10)
        del arg0_1
    return buf10, buf9, buf11


class NeuralSortNew(torch.nn.Module):

    def __init__(self, tau=1.0, hard=False):
        super(NeuralSortNew, self).__init__()
        self.hard = hard
        self.tau = tau

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


Measured latency_ms: 0.112968