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
from torch import nn as nn
import torch.nn.functional as F
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_new_zeros_0(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = 0.0
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_cat_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16 % 8
    x0 = xindex % 16
    x2 = xindex // 128
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 64 * x2), tmp4 & xmask, other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = 0.0
    tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype)
    tmp11 = tl.where(tmp6, tmp9, tmp10)
    tmp12 = tl.where(tmp4, tmp5, tmp11)
    tl.store(out_ptr0 + x3, tmp12, xmask)


@triton.jit
def triton_poi_fused_convolution_sigmoid_2(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 16 % 8
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.sigmoid(tmp2)
    tl.store(in_out_ptr0 + x3, tmp3, xmask)


@triton.jit
def triton_poi_fused_add_convolution_mul_rsub_tanh_3(in_out_ptr0,
    in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x4 = xindex
    x1 = xindex // 16 % 4
    x2 = xindex // 64
    x3 = xindex % 64
    tmp0 = tl.load(in_out_ptr0 + x4, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_out_ptr1 + x4, xmask)
    tmp4 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr2 + (x3 + 128 * x2), xmask)
    tmp9 = tl.load(in_ptr2 + (64 + x3 + 128 * x2), xmask)
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp7 = 1.0
    tmp8 = tmp7 - tmp6
    tmp10 = tmp9 * tmp5
    tmp11 = tmp2 + tmp10
    tmp12 = libdevice.tanh(tmp11)
    tmp13 = tmp8 * tmp12
    tmp14 = 0.0
    tmp15 = tmp6 * tmp14
    tmp16 = tmp13 + tmp15
    tl.store(in_out_ptr0 + x4, tmp2, xmask)
    tl.store(in_out_ptr1 + x4, tmp5, xmask)
    tl.store(out_ptr0 + x4, tmp8, xmask)
    tl.store(out_ptr1 + x4, tmp16, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (8, 8, 3, 3), (72, 9, 3, 1))
    assert_size_stride(primals_3, (8,), (1,))
    assert_size_stride(primals_4, (4, 4, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_5, (4,), (1,))
    assert_size_stride(primals_6, (4, 4, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_7, (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_new_zeros_0[grid(256)](buf0, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32)
        triton_poi_fused_cat_1[grid(512)](primals_1, buf1, 512, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf2 = extern_kernels.convolution(buf1, primals_2, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 8, 4, 4), (128, 16, 4, 1))
        buf3 = buf2
        del buf2
        triton_poi_fused_convolution_sigmoid_2[grid(512)](buf3, primals_3, 
            512, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_3
        buf4 = extern_kernels.convolution(primals_1, primals_4, stride=(1, 
            1), padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf4, (4, 4, 4, 4), (64, 16, 4, 1))
        buf6 = extern_kernels.convolution(buf0, primals_6, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf6, (4, 4, 4, 4), (64, 16, 4, 1))
        buf5 = buf4
        del buf4
        buf7 = buf6
        del buf6
        buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf9 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_convolution_mul_rsub_tanh_3[grid(256)](buf5,
            buf7, primals_5, primals_7, buf3, buf8, buf9, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del primals_5
        del primals_7
    return (buf9, primals_1, primals_2, primals_4, primals_6, buf0, buf1,
        buf3, reinterpret_tensor(buf3, (4, 4, 4, 4), (128, 16, 4, 1), 64),
        buf5, buf7, buf8)


def one_param(m):
    """First parameter in `m`"""
    return next(m.parameters())


class ConvGRUCellNew(nn.Module):

    def __init__(self, input_dim, hidden_dim, kernel_size=(3, 3), bias=True,
        activation=F.tanh, batchnorm=False):
        """
        Initialize ConvGRU cell.
        Parameters
        ----------
        input_dim: int
            Number of channels of input tensor.
        hidden_dim: int
            Number of channels of hidden state.
        kernel_size: (int, int)
            Size of the convolutional kernel.
        bias: bool
            Whether or not to add the bias.
        """
        super().__init__()
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.kernel_size = kernel_size if isinstance(kernel_size, (tuple, list)
            ) else [kernel_size] * 2
        self.padding = self.kernel_size[0] // 2, self.kernel_size[1] // 2
        self.bias = bias
        self.activation = activation
        self.batchnorm = batchnorm
        self.conv_zr = nn.Conv2d(in_channels=self.input_dim + self.
            hidden_dim, out_channels=2 * self.hidden_dim, kernel_size=self.
            kernel_size, padding=self.padding, bias=self.bias)
        self.conv_h1 = nn.Conv2d(in_channels=self.input_dim, out_channels=
            self.hidden_dim, kernel_size=self.kernel_size, padding=self.
            padding, bias=self.bias)
        self.conv_h2 = nn.Conv2d(in_channels=self.hidden_dim, out_channels=
            self.hidden_dim, kernel_size=self.kernel_size, padding=self.
            padding, bias=self.bias)
        self.reset_parameters()

    def init_hidden(self, input):
        bs, _ch, h, w = input.shape
        return one_param(self).new_zeros(bs, self.hidden_dim, h, w)

    def reset_parameters(self):
        nn.init.xavier_uniform_(self.conv_zr.weight, gain=nn.init.
            calculate_gain('tanh'))
        self.conv_zr.bias.data.zero_()
        nn.init.xavier_uniform_(self.conv_h1.weight, gain=nn.init.
            calculate_gain('tanh'))
        self.conv_h1.bias.data.zero_()
        nn.init.xavier_uniform_(self.conv_h2.weight, gain=nn.init.
            calculate_gain('tanh'))
        self.conv_h2.bias.data.zero_()
        if self.batchnorm:
            self.bn1.reset_parameters()
            self.bn2.reset_parameters()

    def forward(self, input_0):
        primals_2 = self.conv_zr.weight
        primals_3 = self.conv_zr.bias
        primals_4 = self.conv_h1.weight
        primals_5 = self.conv_h1.bias
        primals_6 = self.conv_h2.weight
        primals_7 = self.conv_h2.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0]


Measured latency_ms: 0.083514