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, math as tl_math
from typing import *
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_add_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x0 = xindex % 4
    x1 = xindex // 4 % 4
    x4 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = 2 * (x0 // 2)
    tmp2 = tmp1.to(tl.float32)
    tmp3 = 0.25
    tmp4 = tmp2 * tmp3
    tmp5 = 10000.0
    tmp6 = libdevice.pow(tmp5, tmp4)
    tmp7 = tl.full([1], 1, tl.int32)
    tmp8 = tmp7 / tmp6
    tmp9 = 1.0
    tmp10 = tmp8 * tmp9
    tmp11 = x1
    tmp12 = tmp11.to(tl.float32)
    tmp13 = tmp12 * tmp10
    tmp14 = tl_math.cos(tmp13)
    tmp15 = tmp0 * tmp14
    tmp16 = x3 % 2
    tl.full([1], 0, tl.int64)
    tmp19 = tl.full([1], 1, tl.int64)
    tmp20 = tmp16 < tmp19
    tmp21 = tl.load(in_ptr0 + (1 + 2 * (x0 // 2) + 4 * x4), tmp20 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp22 = -tmp21
    tmp23 = tl.full(tmp22.shape, 0.0, tmp22.dtype)
    tmp24 = tl.where(tmp20, tmp22, tmp23)
    tmp25 = tmp16 >= tmp19
    tl.full([1], 2, tl.int64)
    tmp28 = tl.load(in_ptr0 + (2 * (x0 // 2) + 4 * x4), tmp25 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp29 = tl.where(tmp20, tmp24, tmp28)
    tmp30 = tl_math.sin(tmp13)
    tmp31 = tmp29 * tmp30
    tmp32 = tmp15 + tmp31
    tl.store(out_ptr0 + x3, tmp32, xmask)


def call(args):
    arg0_1, arg1_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))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_mul_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        del arg0_1
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_mul_0[grid(256)](arg1_1, buf1, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        del arg1_1
    return buf0, buf1


class RotaryEmbeddingNew(torch.nn.Module):
    """`Rotary Position Embedding <https://arxiv.org/abs/2104.09864v2>

    Args:
        rotary_dim (int): rotary dimension
    """

    def __init__(self, rotary_dim: 'int'):
        super().__init__()
        self.rotary_dim = rotary_dim

    def fixed_pos_embedding(self, x, seq_len=None, dtype=torch.float):
        dim = x.shape[-1]
        inv_freq = 1.0 / 10000 ** (torch.arange(0, dim, 2) / dim)
        sinusoid_inp = torch.einsum('i , j -> i j', torch.arange(seq_len),
            inv_freq).to(x.device)
        return torch.sin(sinusoid_inp), torch.cos(sinusoid_inp)

    def rotate_every_two(self, x):
        if x.dim() == 4:
            x1 = x[:, :, :, ::2]
            x2 = x[:, :, :, 1::2]
        else:
            x1 = x[:, :, ::2]
            x2 = x[:, :, 1::2]
        x = torch.stack((-x2, x1), axis=-1)
        return x.flatten(-2)

    def apply_rotary_pos_emb(self, x, sincos, offset=0):
        sin, cos = map(lambda t: t[None, offset:x.shape[-2] + offset, :].
            repeat_interleave(2, 2), sincos)
        return x * cos + self.rotate_every_two(x) * sin

    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]


Measured latency_ms: 0.026479