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.triton_helpers import libdevice
import 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_per_fused_add_native_layer_norm_sigmoid_0(in_ptr0, in_ptr1,
    out_ptr4, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 8
    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 + 8 * x0), xmask, other=0.0)
    tmp17 = tl.load(in_ptr1 + (r1 + 8 * x0), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp1, 0)
    tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp6 = tl.where(xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = tl.full([XBLOCK, 1], 8, tl.int32)
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp7 / tmp9
    tmp11 = tmp1 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.where(xmask, tmp13, 0)
    tmp16 = tl.sum(tmp15, 1)[:, None]
    tmp18 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp18, 0)
    tmp21 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp23 = tl.where(xmask, tmp21, 0)
    tmp24 = tl.sum(tmp23, 1)[:, None]
    tmp25 = tmp24 / tmp9
    tmp26 = tmp18 - tmp25
    tmp27 = tmp26 * tmp26
    tmp28 = tl.broadcast_to(tmp27, [XBLOCK, RBLOCK])
    tmp30 = tl.where(xmask, tmp28, 0)
    tmp31 = tl.sum(tmp30, 1)[:, None]
    tmp32 = tmp0 - tmp10
    tmp33 = 8.0
    tmp34 = tmp16 / tmp33
    tmp35 = 1e-05
    tmp36 = tmp34 + tmp35
    tmp37 = libdevice.rsqrt(tmp36)
    tmp38 = tmp32 * tmp37
    tmp39 = tmp17 - tmp25
    tmp40 = tmp31 / tmp33
    tmp41 = tmp40 + tmp35
    tmp42 = libdevice.rsqrt(tmp41)
    tmp43 = tmp39 * tmp42
    tmp44 = tmp38 + tmp43
    tmp45 = tl.sigmoid(tmp44)
    tl.store(out_ptr4 + (r1 + 8 * x0), tmp45, xmask)


@triton.jit
def triton_poi_fused_native_layer_norm_1(in_ptr0, out_ptr0, out_ptr1,
    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')
    tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 4.0
    tmp8 = tmp6 / tmp7
    tmp9 = tmp0 - tmp8
    tmp10 = tmp9 * tmp9
    tmp11 = tmp1 - tmp8
    tmp12 = tmp11 * tmp11
    tmp13 = tmp10 + tmp12
    tmp14 = tmp3 - tmp8
    tmp15 = tmp14 * tmp14
    tmp16 = tmp13 + tmp15
    tmp17 = tmp5 - tmp8
    tmp18 = tmp17 * tmp17
    tmp19 = tmp16 + tmp18
    tmp20 = tmp19 / tmp7
    tmp21 = 1e-05
    tmp22 = tmp20 + tmp21
    tmp23 = libdevice.rsqrt(tmp22)
    tl.store(out_ptr0 + x0, tmp8, xmask)
    tl.store(out_ptr1 + x0, tmp23, xmask)


@triton.jit
def triton_poi_fused_add_mul_native_layer_norm_rsub_tanh_2(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, 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 = tl.load(in_ptr0 + (x0 + 8 * x1), xmask)
    tmp3 = tl.load(in_ptr1 + x2, xmask)
    tmp5 = tl.load(in_ptr2 + x2, xmask)
    tmp6 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr0 + (4 + x0 + 8 * x1), xmask)
    tmp11 = tl.load(in_ptr5 + x2, xmask)
    tmp12 = tl.load(in_ptr6 + x1, xmask, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr7 + x1, xmask, eviction_policy='evict_last')
    tmp1 = 1.0
    tmp2 = tmp1 - tmp0
    tmp4 = tmp2 * tmp3
    tmp7 = tmp5 - tmp6
    tmp9 = tmp7 * tmp8
    tmp13 = tmp11 - tmp12
    tmp15 = tmp13 * tmp14
    tmp16 = tmp10 * tmp15
    tmp17 = tmp9 + tmp16
    tmp18 = libdevice.tanh(tmp17)
    tmp19 = tmp0 * tmp18
    tmp20 = tmp4 + tmp19
    tl.store(out_ptr0 + x2, tmp20, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (8, 4), (4, 1))
    assert_size_stride(primals_4, (8,), (1,))
    assert_size_stride(primals_5, (8, 4), (4, 1))
    assert_size_stride(primals_6, (8,), (1,))
    assert_size_stride(primals_7, (4, 4), (4, 1))
    assert_size_stride(primals_8, (4,), (1,))
    assert_size_stride(primals_9, (4, 4), (4, 1))
    assert_size_stride(primals_10, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        extern_kernels.addmm(primals_4, primals_2, reinterpret_tensor(
            primals_3, (4, 8), (1, 4), 0), alpha=1, beta=1, out=buf0)
        del primals_3
        del primals_4
        buf1 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        extern_kernels.addmm(primals_6, primals_1, reinterpret_tensor(
            primals_5, (4, 8), (1, 4), 0), alpha=1, beta=1, out=buf1)
        del primals_5
        del primals_6
        buf8 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_add_native_layer_norm_sigmoid_0[grid(4)](buf0,
            buf1, buf8, 4, 8, XBLOCK=1, num_warps=2, num_stages=1)
        buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_8, primals_2, reinterpret_tensor(
            primals_7, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf9)
        del primals_7
        del primals_8
        buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_10, primals_1, reinterpret_tensor(
            primals_9, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf10)
        del primals_10
        del primals_9
        buf11 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        buf12 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        triton_poi_fused_native_layer_norm_1[grid(4)](buf9, buf11, buf12, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        buf13 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        buf14 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        triton_poi_fused_native_layer_norm_1[grid(4)](buf10, buf13, buf14, 
            4, XBLOCK=4, num_warps=1, num_stages=1)
        buf15 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_mul_native_layer_norm_rsub_tanh_2[grid(16)](buf8,
            primals_1, buf9, buf11, buf12, buf10, buf13, buf14, buf15, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del buf11
        del buf12
        del buf13
        del buf14
        del buf8
    return buf15, primals_1, primals_2, buf0, buf1, buf9, buf10


class LayerNormGRUCellNew(torch.nn.Module):

    def __init__(self, input_size, hidden_size, bias=True):
        super(LayerNormGRUCellNew, self).__init__()
        self.ln_i2h = torch.nn.LayerNorm(2 * hidden_size,
            elementwise_affine=False)
        self.ln_h2h = torch.nn.LayerNorm(2 * hidden_size,
            elementwise_affine=False)
        self.ln_cell_1 = torch.nn.LayerNorm(hidden_size, elementwise_affine
            =False)
        self.ln_cell_2 = torch.nn.LayerNorm(hidden_size, elementwise_affine
            =False)
        self.i2h = torch.nn.Linear(input_size, 2 * hidden_size, bias=bias)
        self.h2h = torch.nn.Linear(hidden_size, 2 * hidden_size, bias=bias)
        self.h_hat_W = torch.nn.Linear(input_size, hidden_size, bias=bias)
        self.h_hat_U = torch.nn.Linear(hidden_size, hidden_size, bias=bias)
        self.hidden_size = hidden_size
        self.reset_parameters()

    def reset_parameters(self):
        std = 1.0 / math.sqrt(self.hidden_size)
        for w in self.parameters():
            w.data.uniform_(-std, std)

    def forward(self, input_0, input_1):
        primals_3 = self.i2h.weight
        primals_4 = self.i2h.bias
        primals_5 = self.h2h.weight
        primals_6 = self.h2h.bias
        primals_1 = self.h_hat_W.weight
        primals_8 = self.h_hat_W.bias
        primals_2 = self.h_hat_U.weight
        primals_10 = self.h_hat_U.bias
        primals_7 = input_0
        primals_9 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9, primals_10])
        return output[0]


Measured latency_ms: 0.096492