import torch


class CPUReverseForgetMult(torch.nn.Module):

    def __init__(self):
        super(CPUReverseForgetMult, self).__init__()

    def forward(self, f, x, hidden_init=None):
        result = []
        forgets = f.split(1, dim=0)[::-1]
        inputs = (f * x).split(1, dim=0)[::-1]
        prev_h = hidden_init
        for i, h in enumerate(inputs):
            h = h.squeeze()
            if prev_h is not None:
                h = h + (1 - forgets[i]) * prev_h
            result.append(h)
            prev_h = h
        result = result[::-1]
        return torch.cat(result, dim=0)


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


def get_init_inputs():
    return [[], {}]

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
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_cat_mul_rsub_0(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, out_ptr2, out_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 16
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + (64 + x2), xmask)
    tmp3 = tl.load(in_ptr0 + (16 + x0), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr1 + (128 + x2), xmask)
    tmp8 = tl.load(in_ptr0 + (32 + x0), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (192 + x2), xmask)
    tmp16 = tl.load(in_ptr1 + x2, xmask)
    tmp18 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp4 = 1.0
    tmp5 = tmp4 - tmp3
    tmp7 = tmp0 * tmp6
    tmp9 = tmp4 - tmp8
    tmp11 = tmp0 * tmp10
    tmp12 = tmp9 * tmp11
    tmp13 = tmp7 + tmp12
    tmp14 = tmp5 * tmp13
    tmp15 = tmp2 + tmp14
    tmp17 = tmp0 * tmp16
    tmp19 = tmp4 - tmp18
    tmp20 = tmp19 * tmp15
    tmp21 = tmp17 + tmp20
    tl.store(out_ptr0 + x2, tmp15, xmask)
    tl.store(out_ptr1 + x2, tmp13, xmask)
    tl.store(out_ptr2 + x2, tmp21, xmask)
    tl.store(out_ptr3 + x2, tmp11, xmask)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4), (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)
        buf4 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
        buf0 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 64)
        buf2 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 128)
        buf1 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 0)
        buf3 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 192)
        get_raw_stream(0)
        triton_poi_fused_add_cat_mul_rsub_0[grid(64)](arg0_1, arg1_1, buf0,
            buf2, buf1, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del arg0_1
        del arg1_1
    return buf4,


class CPUReverseForgetMultNew(torch.nn.Module):

    def __init__(self):
        super(CPUReverseForgetMultNew, self).__init__()

    def forward(self, input_0, input_1):
        arg0_1 = input_0
        arg1_1 = input_1
        output = call([arg0_1, arg1_1])
        return output[0]
