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
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_poi_fused__to_copy_add_lift_fresh_sub_0(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 % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = x0
    tmp1 = tl.full([1], 2, tl.int64)
    tmp2 = tmp0 < tmp1
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = 0.0
    tmp6 = 1.0
    tmp7 = tl.where(tmp4, tmp5, tmp6)
    tmp8 = tl.full([1], 3, tl.int64)
    tmp9 = tmp0 < tmp8
    tmp10 = 2.0
    tmp11 = 3.0
    tmp12 = tl.where(tmp9, tmp10, tmp11)
    tmp13 = tl.where(tmp2, tmp7, tmp12)
    tmp14 = tmp13.to(tl.int32)
    tmp15 = x1
    tmp16 = tmp15 < tmp1
    tmp17 = tmp15 < tmp3
    tmp18 = tl.where(tmp17, tmp5, tmp6)
    tmp19 = tmp15 < tmp8
    tmp20 = tl.where(tmp19, tmp10, tmp11)
    tmp21 = tl.where(tmp16, tmp18, tmp20)
    tmp22 = tmp21.to(tl.int32)
    tmp23 = tmp14 - tmp22
    tmp24 = tl.full([1], 512, tl.int64)
    tmp25 = tmp23 + tmp24
    tl.store(out_ptr0 + x2, tmp25, xmask)


@triton.jit
def triton_poi_fused_embedding_1(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
    x1 = xindex // 4 % 4
    x2 = xindex // 16
    x0 = xindex % 4
    x4 = xindex
    tmp0 = x1
    tmp1 = tl.full([1], 2, tl.int64)
    tmp2 = tmp0 < tmp1
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = 0.0
    tmp6 = 1.0
    tmp7 = tl.where(tmp4, tmp5, tmp6)
    tmp8 = tl.full([1], 3, tl.int64)
    tmp9 = tmp0 < tmp8
    tmp10 = 2.0
    tmp11 = 3.0
    tmp12 = tl.where(tmp9, tmp10, tmp11)
    tmp13 = tl.where(tmp2, tmp7, tmp12)
    tmp14 = tmp13.to(tl.int32)
    tmp15 = x2
    tmp16 = tmp15 < tmp1
    tmp17 = tmp15 < tmp3
    tmp18 = tl.where(tmp17, tmp5, tmp6)
    tmp19 = tmp15 < tmp8
    tmp20 = tl.where(tmp19, tmp10, tmp11)
    tmp21 = tl.where(tmp16, tmp18, tmp20)
    tmp22 = tmp21.to(tl.int32)
    tmp23 = tmp14 - tmp22
    tmp24 = tl.full([1], 512, tl.int64)
    tmp25 = tmp23 + tmp24
    tmp26 = tl.load(in_ptr0 + (x0 + 4 * tmp25), xmask)
    tl.store(out_ptr0 + x4, tmp26, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (1024, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.int64)
        get_raw_stream(0)
        triton_poi_fused__to_copy_add_lift_fresh_sub_0[grid(16)](buf0, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_embedding_1[grid(64)](primals_2, buf1, 64, XBLOCK=
            64, num_warps=1, num_stages=1)
        del primals_2
    return buf1, buf0


class RelativePositionalEmbeddingNew(nn.Module):

    def __init__(self, n_model, max_len=1024):
        super().__init__()
        self.embed = nn.Embedding(max_len, n_model)
        self.reset_parameters()

    @torch.no_grad()
    def reset_parameters(self):
        w = self.embed.weight
        max_len, n_model = w.shape
        pos = torch.cat((w.new_tensor(range(-max_len // 2, 0)), w.
            new_tensor(range(max_len // 2))))
        w = pos.unsqueeze(-1) / 10000 ** (w.new_tensor(range(n_model)) // 2 *
            2 / n_model)
        w[:, 0::2], w[:, 1::2] = w[:, 0::2].sin(), w[:, 1::2].cos()
        self.embed.weight.copy_(w)

    def forward(self, input_0):
        primals_2 = self.embed.weight
        primals_1 = input_0
        output = call([primals_1, primals_2])
        return output[0]


Measured latency_ms: 0.026081