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 libdevice, math as tl_math
from torch import nn
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_helper_fn_add0(arg0_0, arg1_0):
    tmp0 = arg0_0 + arg1_0
    return tmp0


@triton.jit
def triton_per_fused_cumsum_0(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl
    .constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 4 * x0), xmask, other=0.0)
    tmp1 = tmp0.to(tl.float32)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp3, = tl.associative_scan((tmp2,), 1, _triton_helper_fn_add0)
    tl.store(out_ptr0 + (r1 + 4 * x0), tmp3, xmask)


@triton.jit
def triton_poi_fused_add_1(in_ptr0, in_ptr1, 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 % 16
    x0 = xindex % 4
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + x5, xmask)
    tmp1 = 1 + x0
    tmp2 = tmp1.to(tl.float32)
    tmp3 = tmp0 / tmp2
    tmp5 = tmp3 + tmp4
    tl.store(out_ptr0 + x5, tmp5, xmask)


@triton.jit
def triton_poi_fused_add_constant_pad_nd_sum_2(in_ptr0, in_ptr1, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 80
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4 % 5
    x3 = xindex % 20
    x0 = xindex % 4
    x2 = xindex // 20
    x5 = xindex
    tmp0 = -1 + x1
    tmp1 = tl.full([1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.load(in_ptr0 + (-4 + x3), tmp2 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp4 = 1 + x0
    tmp5 = tmp4.to(tl.float32)
    tmp6 = tmp3 / tmp5
    tmp7 = tl.load(in_ptr1 + (-4 + x3 + 16 * x2), tmp2 & xmask, other=0.0)
    tmp8 = tmp6 + tmp7
    tmp9 = tl.full(tmp8.shape, 0.0, tmp8.dtype)
    tmp10 = tl.where(tmp2, tmp8, tmp9)
    tl.store(out_ptr0 + x5, tmp10, xmask)


@triton.jit
def triton_poi_fused_triu_indices_3(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 20
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = x0
    tmp1 = tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 10, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tmp0.to(tl.float64)
    tmp6 = tl.full([1], 2.0, tl.float64)
    tmp7 = tmp5 * tmp6
    tmp8 = tl.full([1], 20.25, tl.float64)
    tmp9 = tmp8 - tmp7
    tmp10 = libdevice.sqrt(tmp9)
    tmp11 = tl.full([1], 4.5, tl.float64)
    tmp12 = tmp11 - tmp10
    tmp13 = libdevice.floor(tmp12)
    tmp14 = tmp13.to(tl.int64)
    tmp15 = tmp14 + tmp1
    tmp16 = tl.full(tmp15.shape, 0.0, tmp15.dtype)
    tmp17 = tl.where(tmp4, tmp15, tmp16)
    tmp18 = tmp0 >= tmp3
    tl.full([1], 20, tl.int64)
    tmp21 = -10 + x0
    tmp22 = tmp21.to(tl.float64)
    tmp23 = tmp22 * tmp6
    tmp24 = tmp8 - tmp23
    tmp25 = libdevice.sqrt(tmp24)
    tmp26 = tmp11 - tmp25
    tmp27 = libdevice.floor(tmp26)
    tmp28 = tl.full([1], 7.0, tl.float64)
    tmp29 = tmp28 - tmp27
    tmp30 = tmp29 * tmp27
    tmp31 = tl.full([1], 0.5, tl.float64)
    tmp32 = tmp30 * tmp31
    tmp33 = tmp22 - tmp32
    tmp34 = libdevice.floor(tmp33)
    tmp35 = tmp34.to(tl.int64)
    tmp36 = tmp35 + tmp1
    tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype)
    tmp38 = tl.where(tmp18, tmp36, tmp37)
    tmp39 = tl.where(tmp4, tmp17, tmp38)
    tl.store(out_ptr0 + x0, tmp39, xmask)


@triton.jit
def triton_poi_fused__to_copy_4(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 80
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.full([1], False, tl.int1)
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused__to_copy_index_put_lift_fresh_5(in_ptr0, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 40
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 10
    x1 = xindex // 10
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (10 + 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')
    tmp7 = tl.full([1], 1, tl.int64)
    tmp8 = tmp6 + tmp7
    tmp9 = tl.full([XBLOCK], 5, tl.int32)
    tmp10 = tmp8 + tmp9
    tmp11 = tmp8 < 0
    tmp12 = tl.where(tmp11, tmp10, tmp8)
    tl.device_assert((0 <= tmp12) & (tmp12 < 5) | ~xmask,
        'index out of bounds: 0 <= tmp12 < 5')
    tmp14 = tl.full([1], True, tl.int1)
    tl.store(out_ptr0 + (tmp12 + 5 * tmp4 + 20 * x1), tmp14, xmask)


@triton.jit
def triton_poi_fused__softmax_6(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
    tmp0 = tl.load(in_ptr0 + 5 * x0, xmask, eviction_policy='evict_last').to(tl
        .int1)
    tmp1 = tl.load(in_ptr1 + 5 * x0, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (1 + 5 * x0), xmask, eviction_policy='evict_last'
        ).to(tl.int1)
    tmp5 = tl.load(in_ptr1 + (1 + 5 * x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + 5 * x0), xmask, eviction_policy='evict_last'
        ).to(tl.int1)
    tmp9 = tl.load(in_ptr1 + (2 + 5 * x0), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (3 + 5 * x0), xmask, eviction_policy='evict_last'
        ).to(tl.int1)
    tmp13 = tl.load(in_ptr1 + (3 + 5 * x0), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr0 + (4 + 5 * x0), xmask, eviction_policy='evict_last'
        ).to(tl.int1)
    tmp17 = tl.load(in_ptr1 + (4 + 5 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = -3.4028234663852886e+38
    tmp3 = tl.where(tmp0, tmp2, tmp1)
    tmp6 = tl.where(tmp4, tmp2, tmp5)
    tmp7 = triton_helpers.maximum(tmp3, tmp6)
    tmp10 = tl.where(tmp8, tmp2, tmp9)
    tmp11 = triton_helpers.maximum(tmp7, tmp10)
    tmp14 = tl.where(tmp12, tmp2, tmp13)
    tmp15 = triton_helpers.maximum(tmp11, tmp14)
    tmp18 = tl.where(tmp16, tmp2, tmp17)
    tmp19 = triton_helpers.maximum(tmp15, tmp18)
    tmp20 = tmp3 - tmp19
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp6 - tmp19
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tmp21 + tmp23
    tmp25 = tmp10 - tmp19
    tmp26 = tl_math.exp(tmp25)
    tmp27 = tmp24 + tmp26
    tmp28 = tmp14 - tmp19
    tmp29 = tl_math.exp(tmp28)
    tmp30 = tmp27 + tmp29
    tmp31 = tmp18 - tmp19
    tmp32 = tl_math.exp(tmp31)
    tmp33 = tmp30 + tmp32
    tl.store(out_ptr0 + x0, tmp19, xmask)
    tl.store(out_ptr1 + x0, tmp33, xmask)


@triton.jit
def triton_poi_fused_tril_7(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 20
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 5
    x1 = xindex // 5
    x2 = xindex
    tmp0 = x0 + -1 * x1
    tmp1 = tl.full([1], -1, tl.int64)
    tmp2 = tmp0 <= tmp1
    tl.store(out_ptr0 + x2, tmp2, xmask)


@triton.jit
def triton_poi_fused__softmax_tril_8(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 80
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x4 = xindex
    x1 = xindex // 5
    x2 = xindex % 20
    tmp0 = tl.load(in_ptr0 + x4, xmask).to(tl.int1)
    tmp1 = tl.load(in_out_ptr0 + x4, xmask)
    tmp4 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last').to(tl
        .int1)
    tmp2 = -3.4028234663852886e+38
    tmp3 = tl.where(tmp0, tmp2, tmp1)
    tmp5 = tmp3 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp8 = tmp6 / tmp7
    tmp10 = 0.0
    tmp11 = tl.where(tmp9, tmp8, tmp10)
    tl.store(in_out_ptr0 + x4, tmp8, xmask)
    tl.store(out_ptr0 + x4, tmp11, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (1, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (1, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_cumsum_0[grid(4)](primals_1, buf0, 4, 4, XBLOCK=1,
            num_warps=2, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_per_fused_cumsum_0[grid(4)](primals_4, buf1, 4, 4, XBLOCK=1,
            num_warps=2, num_stages=1)
        del primals_4
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_1[grid(64)](buf0, primals_2, buf2, 64, XBLOCK=
            64, num_warps=1, num_stages=1)
        del primals_2
        buf3 = empty_strided_cuda((4, 5, 4), (20, 4, 1), torch.float32)
        triton_poi_fused_add_constant_pad_nd_sum_2[grid(80)](buf1,
            primals_3, buf3, 80, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_3
        buf4 = empty_strided_cuda((4, 4, 5), (20, 5, 1), torch.float32)
        extern_kernels.bmm(buf2, reinterpret_tensor(buf3, (4, 4, 5), (20, 1,
            4), 0), out=buf4)
        buf5 = empty_strided_cuda((20,), (1,), torch.int64)
        triton_poi_fused_triu_indices_3[grid(20)](buf5, 20, XBLOCK=32,
            num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((4, 4, 5), (20, 5, 1), torch.bool)
        triton_poi_fused__to_copy_4[grid(80)](buf6, 80, XBLOCK=128,
            num_warps=4, num_stages=1)
        triton_poi_fused__to_copy_index_put_lift_fresh_5[grid(40)](buf5,
            buf6, 40, XBLOCK=64, num_warps=1, num_stages=1)
        del buf5
        buf8 = reinterpret_tensor(buf1, (4, 4, 1), (4, 1, 16), 0)
        del buf1
        buf9 = reinterpret_tensor(buf0, (4, 4, 1), (4, 1, 16), 0)
        del buf0
        triton_poi_fused__softmax_6[grid(16)](buf6, buf4, buf8, buf9, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf11 = empty_strided_cuda((4, 5), (5, 1), torch.bool)
        triton_poi_fused_tril_7[grid(20)](buf11, 20, XBLOCK=32, num_warps=1,
            num_stages=1)
        buf10 = buf4
        del buf4
        buf12 = empty_strided_cuda((4, 4, 5), (20, 5, 1), torch.float32)
        triton_poi_fused__softmax_tril_8[grid(80)](buf10, buf6, buf8, buf9,
            buf11, buf12, 80, XBLOCK=128, num_warps=4, num_stages=1)
        del buf8
        del buf9
    return buf12, buf6, buf10, buf11, reinterpret_tensor(buf2, (4, 4, 4), (
        16, 1, 4), 0), buf3


def bucket(buckets, t, dim=1):
    shape = list(t.shape)
    shape[dim:dim + 1] = [buckets, -1]
    return t.reshape(*shape)


def max_neg_value(tensor):
    return -torch.finfo(tensor.dtype).max


def expand_dim(t, dim, k):
    expand_shape = [-1] * len(t.shape)
    expand_shape[dim] = k
    return t.expand(*expand_shape)


def expand_batch_and_merge_head(b, t):
    shape = list(t.squeeze(0).shape)
    t = expand_dim(t, 0, b)
    shape[0] = shape[0] * b
    return t.reshape(*shape)


def cumavg(t, dim):
    r = torch.arange(1, t.shape[dim] + 1, device=t.device)
    expand_slice = [None] * len(t.shape)
    expand_slice[dim] = slice(None, None)
    return t.cumsum(dim=dim) / r[tuple(expand_slice)]


def mask_reordering_matrix(R):
    buckets = R.shape[1]
    mask_value = max_neg_value(R)
    mask = torch.zeros(R.shape, device=R.device).bool()
    i, j = torch.triu_indices(buckets, buckets)
    mask[:, i, j + 1] = True
    R.masked_fill_(mask, mask_value)
    del mask
    R = R.softmax(dim=-1)
    R = R.tril(diagonal=-1)
    return R


class CausalAttentionSortNetNew(nn.Module):

    def __init__(self, heads, buckets, dim):
        super().__init__()
        self.heads = heads
        self.buckets = buckets
        self.dim = dim
        self.q_pos_emb = nn.Parameter(torch.randn(1, heads, buckets, dim))
        self.k_pos_emb = nn.Parameter(torch.randn(1, heads, buckets, dim))

    def forward(self, input_0, input_1):
        primals_2 = self.q_pos_emb
        primals_3 = self.k_pos_emb
        primals_1 = input_0
        primals_4 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.059654