# AOT ID: ['0_forward']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_4/inductor_cache/g7/cg7yygwsbwngseams3ffhkaht5lhhnqbzaonxa56ywtkmfqsj27o.py
# Topologically Sorted Source Nodes: [rz_t, rz_t_1], Original ATen: [aten.add, aten.sigmoid]
# Source node to ATen node mapping:
#   rz_t => add
#   rz_t_1 => sigmoid
# Graph fragment:
#   %add_tensor_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mm_default_3, %primals_2), kwargs = {})
#   %add_tensor_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mm_default_2, %primals_5), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_tensor_3, %add_tensor_2), kwargs = {})
#   %sigmoid : [num_users=3] = call_function[target=torch.ops.aten.sigmoid.default](args = (%add,), kwargs = {})
triton_poi_fused_add_sigmoid_0 = async_compile.triton('triton_poi_fused_add_sigmoid_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[32], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_add_sigmoid_0', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@triton.jit
def triton_poi_fused_add_sigmoid_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK : tl.constexpr):
    xnumel = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 8
    tmp0 = tl.load(in_out_ptr0 + (x2), xmask)
    tmp1 = tl.load(in_ptr0 + (x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + (x2), xmask)
    tmp4 = tl.load(in_ptr2 + (x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = tmp2 + tmp5
    tmp7 = tl.sigmoid(tmp6)
    tl.store(in_out_ptr0 + (x2), tmp7, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/em/cemoh4ty72bxzpnrqfq5y7hodpenyuc2tp54jl5sph77ekgjjtt5.py
# Topologically Sorted Source Nodes: [mul], Original ATen: [aten.mul]
# Source node to ATen node mapping:
#   mul => mul
# Graph fragment:
#   %mul : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%slice_2, %primals_6), kwargs = {})
triton_poi_fused_mul_1 = async_compile.triton('triton_poi_fused_mul_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_mul_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 2, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@triton.jit
def triton_poi_fused_mul_1(in_ptr0, in_ptr1, 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)
    tmp1 = tl.load(in_ptr1 + (x2), xmask)
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + (x2), tmp2, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/pt/cptohbq652zhzjyclhg7kfvcbcwp3u25ro3hcyopkosa6ffv2lv4.py
# Topologically Sorted Source Nodes: [h_h_tm1_1, h_t_above], Original ATen: [aten.add, aten.tanh]
# Source node to ATen node mapping:
#   h_h_tm1_1 => add_1
#   h_t_above => tanh
# Graph fragment:
#   %add_tensor_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mm_default_1, %primals_8), kwargs = {})
#   %add_tensor : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mm_default, %primals_10), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_tensor_1, %add_tensor), kwargs = {})
#   %tanh : [num_users=2] = call_function[target=torch.ops.aten.tanh.default](args = (%add_1,), kwargs = {})
triton_poi_fused_add_tanh_2 = async_compile.triton('triton_poi_fused_add_tanh_2', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_add_tanh_2', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@triton.jit
def triton_poi_fused_add_tanh_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK : tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + (x2), xmask)
    tmp1 = tl.load(in_ptr0 + (x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + (x2), xmask)
    tmp4 = tl.load(in_ptr2 + (x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = tmp2 + tmp5
    tmp7 = libdevice.tanh(tmp6)
    tl.store(in_out_ptr0 + (x2), tmp7, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/ir/cirdpmtn7oh4j2wa3f4ilfab42gx7p3l7oeugkf7pnt4rrukx2f7.py
# Topologically Sorted Source Nodes: [sub, mul_1, mul_2, h_t, mul_3, sub_1, mul_4, h_t_1], Original ATen: [aten.rsub, aten.mul, aten.add]
# Source node to ATen node mapping:
#   h_t => add_2
#   h_t_1 => add_3
#   mul_1 => mul_1
#   mul_2 => mul_2
#   mul_3 => mul_3
#   mul_4 => mul_4
#   sub => sub
#   sub_1 => sub_1
# Graph fragment:
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %slice_4), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub, %primals_6), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%slice_4, %tanh), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_1, %mul_2), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%unsqueeze, %add_2), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %unsqueeze), kwargs = {})
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_1, %primals_6), kwargs = {})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_3, %mul_4), kwargs = {})
triton_poi_fused_add_mul_rsub_3 = async_compile.triton('triton_poi_fused_add_mul_rsub_3', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[64], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_add_mul_rsub_3', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@triton.jit
def triton_poi_fused_add_mul_rsub_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x2 = (xindex // 16)
    x1 = (xindex // 4) % 4
    x3 = xindex % 16
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + (4*x2)), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (4 + x0 + (8*x1)), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + (x3), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + (x3), xmask, eviction_policy='evict_last')
    tmp2 = 1.0
    tmp3 = tmp2 - tmp1
    tmp5 = tmp3 * tmp4
    tmp7 = tmp1 * tmp6
    tmp8 = tmp5 + tmp7
    tmp9 = tmp0 * tmp8
    tmp10 = tmp2 - tmp0
    tmp11 = tmp10 * tmp4
    tmp12 = tmp9 + tmp11
    tl.store(out_ptr0 + (x4), tmp12, xmask)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11 = args
    args.clear()
    assert_size_stride(primals_1, (8, 4), (4, 1))
    assert_size_stride(primals_2, (8, ), (1, ))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (8, 4), (4, 1))
    assert_size_stride(primals_5, (8, ), (1, ))
    assert_size_stride(primals_6, (4, 4), (4, 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, ))
    assert_size_stride(primals_11, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        # Topologically Sorted Source Nodes: [], Original ATen: []
        extern_kernels.mm(primals_3, reinterpret_tensor(primals_1, (4, 8), (1, 4), 0), out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        # Topologically Sorted Source Nodes: [], Original ATen: []
        extern_kernels.mm(primals_6, reinterpret_tensor(primals_4, (4, 8), (1, 4), 0), out=buf1)
        del primals_4
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [], Original ATen: []
        extern_kernels.mm(primals_3, reinterpret_tensor(primals_7, (4, 4), (1, 4), 0), out=buf2)
        del primals_7
        buf3 = buf0; del buf0  # reuse
        # Topologically Sorted Source Nodes: [rz_t, rz_t_1], Original ATen: [aten.add, aten.sigmoid]
        stream0 = get_raw_stream(0)
        triton_poi_fused_add_sigmoid_0.run(buf3, primals_2, buf1, primals_5, 32, grid=grid(32), stream=stream0)
        del buf1
        del primals_2
        del primals_5
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [mul], Original ATen: [aten.mul]
        triton_poi_fused_mul_1.run(buf3, primals_6, buf4, 16, grid=grid(16), stream=stream0)
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [], Original ATen: []
        extern_kernels.mm(buf4, reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), out=buf5)
        buf6 = buf2; del buf2  # reuse
        # Topologically Sorted Source Nodes: [h_h_tm1_1, h_t_above], Original ATen: [aten.add, aten.tanh]
        triton_poi_fused_add_tanh_2.run(buf6, primals_8, buf5, primals_10, 16, grid=grid(16), stream=stream0)
        del buf5
        del primals_10
        del primals_8
        buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [sub, mul_1, mul_2, h_t, mul_3, sub_1, mul_4, h_t_1], Original ATen: [aten.rsub, aten.mul, aten.add]
        triton_poi_fused_add_mul_rsub_3.run(primals_11, buf3, primals_6, buf6, buf7, 64, grid=grid(64), stream=stream0)
    return (buf7, primals_3, primals_6, primals_11, buf3, buf4, buf6, primals_9, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    primals_1 = rand_strided((8, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_2 = rand_strided((8, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_3 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_4 = rand_strided((8, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_5 = rand_strided((8, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_6 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_7 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_8 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_9 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_10 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_11 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([primals_1, primals_2, primals_3, primals_4, primals_5, primals_6, primals_7, primals_8, primals_9, primals_10, primals_11])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import torch
import torch as tc
import torch.nn as nn


class Layer_Norm(nn.Module):

    def __init__(self, d_hid, eps=0.001):
        super(Layer_Norm, self).__init__()
        self.eps = eps
        self.g = nn.Parameter(tc.ones(d_hid), requires_grad=True)
        self.b = nn.Parameter(tc.zeros(d_hid), requires_grad=True)

    def forward(self, z):
        if z.size(-1) == 1:
            return z
        mu = tc.mean(z, dim=-1, keepdim=True)
        variance = tc.mean(tc.pow(z - mu, 2), dim=-1, keepdim=True)
        sigma = tc.sqrt(variance)
        z_norm = tc.div(z - mu, sigma + self.eps)
        if z_norm.is_cuda:
            z_norm = z_norm * self.g.expand_as(z_norm) + self.b.expand_as(
                z_norm)
        else:
            z_norm = z_norm * self.g.expand_as(z_norm) + self.b.expand_as(
                z_norm)
        return z_norm


class GRU(nn.Module):
    """
    Gated Recurrent Unit network with initial state as parameter:

        z_t = sigmoid((x_t dot W_xz + b_xz) + (h_{t-1} dot U_hz + b_hz))
        r_t = sigmoid((x_t dot W_xr + b_xr) + (h_{t-1} dot U_hr + b_xr))

        => zr_t = sigmoid((x_t dot W_xzr + b_xzr) + (h_{t-1} dot U_hzr + b_hzr))
        slice ...

        h_above = tanh(x_t dot W_xh + b_xh + (h_{t-1} dot U_hh + b_hh) * r_t)

        h_t = (1 - z_t) * h_above + z_t * h_{t-1}
        #h_t = (1 - z_t) * h_{t-1} + z_t * h_above

    all parameters are initialized in [-0.01, 0.01]
    """

    def __init__(self, input_size, hidden_size, enc_hid_size=None, with_ln=
        False, prefix='GRU', **kwargs):
        super(GRU, self).__init__()
        self.enc_hid_size = enc_hid_size
        self.hidden_size = hidden_size
        self.with_ln = with_ln
        self.prefix = prefix
        self.xh = nn.Linear(input_size, hidden_size)
        self.hh = nn.Linear(hidden_size, hidden_size)
        self.xrz = nn.Linear(input_size, 2 * hidden_size)
        self.hrz = nn.Linear(hidden_size, 2 * hidden_size)
        if self.enc_hid_size is not None:
            self.crz = nn.Linear(enc_hid_size, 2 * hidden_size)
            self.ch = nn.Linear(enc_hid_size, hidden_size)
        if self.with_ln is True:
            self.ln0 = Layer_Norm(2 * hidden_size)
            self.ln1 = Layer_Norm(2 * hidden_size)
            self.ln2 = Layer_Norm(hidden_size)
            self.ln3 = Layer_Norm(hidden_size)
            if self.enc_hid_size is not None:
                self.ln4 = Layer_Norm(2 * hidden_size)
                self.ln5 = Layer_Norm(hidden_size)
        self.sigmoid = nn.Sigmoid()
        self.tanh = nn.Tanh()
    """
        x_t: input at time t
        x_m: mask of x_t
        h_tm1: previous state
    """

    def forward(self, x_t, x_m, h_tm1, attend=None):
        x_rz_t, h_rz_tm1, x_h_t = self.xrz(x_t), self.hrz(h_tm1), self.xh(x_t)
        if self.with_ln is not True:
            if self.enc_hid_size is None:
                rz_t = x_rz_t + h_rz_tm1
            else:
                a_rz_t, a_h_t = self.crz(attend), self.ch(attend)
                rz_t = x_rz_t + h_rz_tm1 + a_rz_t
        else:
            x_rz_t, h_rz_tm1, x_h_t = self.ln0(x_rz_t), self.ln1(h_rz_tm1
                ), self.ln2(x_h_t)
            if self.enc_hid_size is None:
                rz_t = x_rz_t + h_rz_tm1
            else:
                a_rz_t, a_h_t = self.crz(attend), self.ch(attend)
                a_rz_t, a_h_t = self.ln4(a_rz_t), self.ln5(a_h_t)
                rz_t = x_rz_t + h_rz_tm1 + a_rz_t
        assert rz_t.dim() == 2
        rz_t = self.sigmoid(rz_t)
        r_t, z_t = rz_t[:, :self.hidden_size], rz_t[:, self.hidden_size:]
        h_h_tm1 = self.hh(r_t * h_tm1)
        if self.with_ln:
            h_h_tm1 = self.ln3(h_h_tm1)
        if self.enc_hid_size is None:
            h_h_tm1 = x_h_t + h_h_tm1
        else:
            h_h_tm1 = x_h_t + h_h_tm1 + a_h_t
        h_t_above = self.tanh(h_h_tm1)
        h_t = (1.0 - z_t) * h_tm1 + z_t * h_t_above
        if x_m is not None:
            h_t = x_m[:, None] * h_t + (1.0 - x_m[:, None]) * h_tm1
        return h_t


def get_inputs():
    return [torch.rand([4, 4]), torch.rand([4, 4]), torch.rand([4, 4])]


def get_init_inputs():
    return [[], {'input_size': 4, 'hidden_size': 4}]

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 torch as tc
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_add_sigmoid_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 8
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + x2, xmask)
    tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = tmp2 + tmp5
    tmp7 = tl.sigmoid(tmp6)
    tl.store(in_out_ptr0 + x2, tmp7, xmask)


@triton.jit
def triton_poi_fused_mul_1(in_ptr0, in_ptr1, 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)
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x2, tmp2, xmask)


@triton.jit
def triton_poi_fused_add_tanh_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + x2, xmask)
    tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = tmp2 + tmp5
    tmp7 = libdevice.tanh(tmp6)
    tl.store(in_out_ptr0 + x2, tmp7, xmask)


@triton.jit
def triton_poi_fused_add_mul_rsub_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x2 = xindex // 16
    x1 = xindex // 4 % 4
    x3 = xindex % 16
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp1 = tl.load(in_ptr1 + (4 + x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + x3, xmask, eviction_policy='evict_last')
    tmp2 = 1.0
    tmp3 = tmp2 - tmp1
    tmp5 = tmp3 * tmp4
    tmp7 = tmp1 * tmp6
    tmp8 = tmp5 + tmp7
    tmp9 = tmp0 * tmp8
    tmp10 = tmp2 - tmp0
    tmp11 = tmp10 * tmp4
    tmp12 = tmp9 + tmp11
    tl.store(out_ptr0 + x4, tmp12, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11) = args
    args.clear()
    assert_size_stride(primals_1, (8, 4), (4, 1))
    assert_size_stride(primals_2, (8,), (1,))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (8, 4), (4, 1))
    assert_size_stride(primals_5, (8,), (1,))
    assert_size_stride(primals_6, (4, 4), (4, 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,))
    assert_size_stride(primals_11, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        extern_kernels.mm(primals_3, reinterpret_tensor(primals_1, (4, 8),
            (1, 4), 0), out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        extern_kernels.mm(primals_6, reinterpret_tensor(primals_4, (4, 8),
            (1, 4), 0), out=buf1)
        del primals_4
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(primals_3, reinterpret_tensor(primals_7, (4, 4),
            (1, 4), 0), out=buf2)
        del primals_7
        buf3 = buf0
        del buf0
        get_raw_stream(0)
        triton_poi_fused_add_sigmoid_0[grid(32)](buf3, primals_2, buf1,
            primals_5, 32, XBLOCK=32, num_warps=1, num_stages=1)
        del buf1
        del primals_2
        del primals_5
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_mul_1[grid(16)](buf3, primals_6, buf4, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf4, reinterpret_tensor(primals_9, (4, 4), (1, 4
            ), 0), out=buf5)
        buf6 = buf2
        del buf2
        triton_poi_fused_add_tanh_2[grid(16)](buf6, primals_8, buf5,
            primals_10, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del buf5
        del primals_10
        del primals_8
        buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_mul_rsub_3[grid(64)](primals_11, buf3,
            primals_6, buf6, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1)
    return buf7, primals_3, primals_6, primals_11, buf3, buf4, buf6, primals_9


class Layer_Norm(nn.Module):

    def __init__(self, d_hid, eps=0.001):
        super(Layer_Norm, self).__init__()
        self.eps = eps
        self.g = nn.Parameter(tc.ones(d_hid), requires_grad=True)
        self.b = nn.Parameter(tc.zeros(d_hid), requires_grad=True)

    def forward(self, z):
        if z.size(-1) == 1:
            return z
        mu = tc.mean(z, dim=-1, keepdim=True)
        variance = tc.mean(tc.pow(z - mu, 2), dim=-1, keepdim=True)
        sigma = tc.sqrt(variance)
        z_norm = tc.div(z - mu, sigma + self.eps)
        if z_norm.is_cuda:
            z_norm = z_norm * self.g.expand_as(z_norm) + self.b.expand_as(
                z_norm)
        else:
            z_norm = z_norm * self.g.expand_as(z_norm) + self.b.expand_as(
                z_norm)
        return z_norm


class GRUNew(nn.Module):
    """
    Gated Recurrent Unit network with initial state as parameter:

        z_t = sigmoid((x_t dot W_xz + b_xz) + (h_{t-1} dot U_hz + b_hz))
        r_t = sigmoid((x_t dot W_xr + b_xr) + (h_{t-1} dot U_hr + b_xr))

        => zr_t = sigmoid((x_t dot W_xzr + b_xzr) + (h_{t-1} dot U_hzr + b_hzr))
        slice ...

        h_above = tanh(x_t dot W_xh + b_xh + (h_{t-1} dot U_hh + b_hh) * r_t)

        h_t = (1 - z_t) * h_above + z_t * h_{t-1}
        #h_t = (1 - z_t) * h_{t-1} + z_t * h_above

    all parameters are initialized in [-0.01, 0.01]
    """

    def __init__(self, input_size, hidden_size, enc_hid_size=None, with_ln=
        False, prefix='GRU', **kwargs):
        super(GRUNew, self).__init__()
        self.enc_hid_size = enc_hid_size
        self.hidden_size = hidden_size
        self.with_ln = with_ln
        self.prefix = prefix
        self.xh = nn.Linear(input_size, hidden_size)
        self.hh = nn.Linear(hidden_size, hidden_size)
        self.xrz = nn.Linear(input_size, 2 * hidden_size)
        self.hrz = nn.Linear(hidden_size, 2 * hidden_size)
        if self.enc_hid_size is not None:
            self.crz = nn.Linear(enc_hid_size, 2 * hidden_size)
            self.ch = nn.Linear(enc_hid_size, hidden_size)
        if self.with_ln is True:
            self.ln0 = Layer_Norm(2 * hidden_size)
            self.ln1 = Layer_Norm(2 * hidden_size)
            self.ln2 = Layer_Norm(hidden_size)
            self.ln3 = Layer_Norm(hidden_size)
            if self.enc_hid_size is not None:
                self.ln4 = Layer_Norm(2 * hidden_size)
                self.ln5 = Layer_Norm(hidden_size)
        self.sigmoid = nn.Sigmoid()
        self.tanh = nn.Tanh()
    """
        x_t: input at time t
        x_m: mask of x_t
        h_tm1: previous state
    """

    def forward(self, input_0, input_1, input_2):
        primals_3 = self.xh.weight
        primals_8 = self.xh.bias
        primals_6 = self.hh.weight
        primals_10 = self.hh.bias
        primals_1 = self.xrz.weight
        primals_2 = self.xrz.bias
        primals_4 = self.hrz.weight
        primals_5 = self.hrz.bias
        primals_7 = input_0
        primals_9 = input_1
        primals_11 = input_2
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11])
        return output[0]
