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, math as tl_math
import numpy as np
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_mul_sin_0(in_ptr0, out_ptr0, 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 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp1 = 0.06479529375
    tmp2 = tmp0 * tmp1
    tmp3 = tl_math.sin(tmp2)
    tl.store(out_ptr0 + x0, tmp3, xmask)


@triton.jit
def triton_poi_fused_fill_lift_fresh_1(in_ptr0, out_ptr0, out_ptr1, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x2 = xindex
    tmp5 = tl.load(in_ptr0 + x2, xmask)
    tmp0 = x0
    tmp1 = tl.full([1], 3, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp3 = tl.full([1], 1, tl.int32)
    tmp4 = tmp0 == tmp3
    tmp6 = 0.0
    tmp7 = tl.where(tmp4, tmp6, tmp5)
    tmp8 = tl.where(tmp2, tmp6, tmp7)
    tl.store(out_ptr0 + x2, tmp8, xmask)
    tl.store(out_ptr1 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_tanh_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 48
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 12
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.tanh(tmp2)
    tl.store(in_out_ptr0 + x2, tmp3, xmask)


@triton.jit
def triton_poi_fused_tanh_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 64
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.tanh(tmp2)
    tl.store(in_out_ptr0 + x2, tmp3, xmask)


@triton.jit
def triton_poi_fused_cat_4(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 560
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 140
    x1 = xindex // 140
    x2 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 128, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (128 * x1 + x0), tmp4 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp6 = libdevice.tanh(tmp5)
    tmp7 = tl.full(tmp6.shape, 0.0, tmp6.dtype)
    tmp8 = tl.where(tmp4, tmp6, tmp7)
    tmp9 = tmp0 >= tmp3
    tl.full([1], 140, tl.int64)
    tmp12 = tl.load(in_ptr1 + (12 * x1 + (-128 + x0)), tmp9 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp13 = libdevice.tanh(tmp12)
    tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype)
    tmp15 = tl.where(tmp9, tmp13, tmp14)
    tmp16 = tl.where(tmp4, tmp8, tmp15)
    tl.store(out_ptr0 + x2, tmp16, 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, primals_12,
        primals_13, primals_14, primals_15, primals_16, primals_17) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (12, 1), (1, 1))
    assert_size_stride(primals_3, (12,), (1,))
    assert_size_stride(primals_4, (12, 12), (12, 1))
    assert_size_stride(primals_5, (12,), (1,))
    assert_size_stride(primals_6, (64, 4), (4, 1))
    assert_size_stride(primals_7, (64,), (1,))
    assert_size_stride(primals_8, (128, 64), (64, 1))
    assert_size_stride(primals_9, (128,), (1,))
    assert_size_stride(primals_10, (64, 140), (140, 1))
    assert_size_stride(primals_11, (64,), (1,))
    assert_size_stride(primals_12, (64, 64), (64, 1))
    assert_size_stride(primals_13, (64,), (1,))
    assert_size_stride(primals_14, (64, 140), (140, 1))
    assert_size_stride(primals_15, (64,), (1,))
    assert_size_stride(primals_16, (1, 64), (64, 1))
    assert_size_stride(primals_17, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4,), (1,), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_sin_0[grid(4)](primals_1, buf0, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_fill_lift_fresh_1[grid(16)](primals_1, buf1,
            primals_1, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del primals_1
        buf2 = empty_strided_cuda((4, 12), (12, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf0, (4, 1), (1, 0), 0),
            reinterpret_tensor(primals_2, (1, 12), (1, 1), 0), out=buf2)
        del primals_2
        buf3 = buf2
        del buf2
        triton_poi_fused_tanh_2[grid(48)](buf3, primals_3, 48, XBLOCK=64,
            num_warps=1, num_stages=1)
        del primals_3
        buf4 = empty_strided_cuda((4, 12), (12, 1), torch.float32)
        extern_kernels.addmm(primals_5, buf3, reinterpret_tensor(primals_4,
            (12, 12), (1, 12), 0), alpha=1, beta=1, out=buf4)
        del primals_5
        buf5 = empty_strided_cuda((4, 64), (64, 1), torch.float32)
        extern_kernels.mm(buf1, reinterpret_tensor(primals_6, (4, 64), (1, 
            4), 0), out=buf5)
        del primals_6
        buf6 = buf5
        del buf5
        triton_poi_fused_tanh_3[grid(256)](buf6, primals_7, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del primals_7
        buf7 = empty_strided_cuda((4, 128), (128, 1), torch.float32)
        extern_kernels.addmm(primals_9, buf6, reinterpret_tensor(primals_8,
            (64, 128), (1, 64), 0), alpha=1, beta=1, out=buf7)
        del primals_9
        buf8 = empty_strided_cuda((4, 140), (140, 1), torch.float32)
        triton_poi_fused_cat_4[grid(560)](buf7, buf4, buf8, 560, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf9 = empty_strided_cuda((4, 64), (64, 1), torch.float32)
        extern_kernels.mm(buf8, reinterpret_tensor(primals_10, (140, 64), (
            1, 140), 0), out=buf9)
        buf10 = buf9
        del buf9
        triton_poi_fused_tanh_3[grid(256)](buf10, primals_11, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_11
        buf11 = empty_strided_cuda((4, 64), (64, 1), torch.float32)
        extern_kernels.mm(buf10, reinterpret_tensor(primals_12, (64, 64), (
            1, 64), 0), out=buf11)
        buf12 = buf11
        del buf11
        triton_poi_fused_tanh_3[grid(256)](buf12, primals_13, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_13
        buf13 = empty_strided_cuda((4, 64), (64, 1), torch.float32)
        extern_kernels.mm(buf8, reinterpret_tensor(primals_14, (140, 64), (
            1, 140), 0), out=buf13)
        buf14 = buf13
        del buf13
        triton_poi_fused_tanh_3[grid(256)](buf14, primals_15, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_15
        buf16 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_17, buf14, reinterpret_tensor(
            primals_16, (64, 1), (1, 64), 0), alpha=1, beta=1, out=buf16)
        del primals_17
    return (buf16, buf12, reinterpret_tensor(buf0, (4, 1), (1, 1), 0), buf1,
        buf3, buf4, buf6, buf7, buf8, buf10, buf12, buf14, primals_16,
        primals_14, primals_12, primals_10, primals_8, primals_4)


def init(module, weight_init, bias_init, gain=1):
    weight_init(module.weight.data, gain=gain)
    bias_init(module.bias.data)
    return module


class NNBase(nn.Module):

    def __init__(self, recurrent, recurrent_input_size, hidden_size):
        super(NNBase, self).__init__()
        self._hidden_size = hidden_size
        self._recurrent = recurrent
        if recurrent:
            self.gru = nn.GRU(recurrent_input_size, hidden_size)
            for name, param in self.gru.named_parameters():
                if 'bias' in name:
                    nn.init.constant_(param, 0)
                elif 'weight' in name:
                    nn.init.orthogonal_(param)

    @property
    def is_recurrent(self):
        return self._recurrent

    @property
    def recurrent_hidden_state_size(self):
        if self._recurrent:
            return self._hidden_size
        return 1

    @property
    def output_size(self):
        return self._hidden_size

    def _forward_gru(self, x, hxs, masks):
        if x.size(0) == hxs.size(0):
            x, hxs = self.gru(x.unsqueeze(0), (hxs * masks).unsqueeze(0))
            x = x.squeeze(0)
            hxs = hxs.squeeze(0)
        else:
            N = hxs.size(0)
            T = int(x.size(0) / N)
            x = x.view(T, N, x.size(1))
            masks = masks.view(T, N)
            has_zeros = (masks[1:] == 0.0).any(dim=-1).nonzero().squeeze().cpu(
                )
            if has_zeros.dim() == 0:
                has_zeros = [has_zeros.item() + 1]
            else:
                has_zeros = (has_zeros + 1).numpy().tolist()
            has_zeros = [0] + has_zeros + [T]
            hxs = hxs.unsqueeze(0)
            outputs = []
            for i in range(len(has_zeros) - 1):
                start_idx = has_zeros[i]
                end_idx = has_zeros[i + 1]
                rnn_scores, hxs = self.gru(x[start_idx:end_idx], hxs *
                    masks[start_idx].view(1, -1, 1))
                outputs.append(rnn_scores)
            x = torch.cat(outputs, dim=0)
            x = x.view(T * N, -1)
            hxs = hxs.squeeze(0)
        return x, hxs


class OscBaseNew(NNBase):

    def __init__(self, num_inputs, recurrent=False, hidden_size=64):
        super(OscBaseNew, self).__init__(recurrent, num_inputs, hidden_size)

        def init_(m):
            return init(m, nn.init.orthogonal_, lambda x: nn.init.constant_
                (x, 0), np.sqrt(2))
        self.time_idx = num_inputs // 2 - 1
        self.osc_fanout1 = nn.Linear(1, 12)
        self.osc_fanout2 = nn.Linear(12, 12)
        self.layer1 = nn.Linear(num_inputs, hidden_size)
        self.layer2 = nn.Linear(hidden_size, hidden_size * 2)
        self.layerA1 = nn.Linear(hidden_size * 2 + 12, hidden_size)
        self.layerA2 = nn.Linear(hidden_size, hidden_size)
        self.layerC1 = nn.Linear(hidden_size * 2 + 12, hidden_size)
        self.layerC2 = init_(nn.Linear(hidden_size, 1))
        self.train()

    def forward(self, input_0):
        primals_2 = self.osc_fanout1.weight
        primals_3 = self.osc_fanout1.bias
        primals_4 = self.osc_fanout2.weight
        primals_5 = self.osc_fanout2.bias
        primals_6 = self.layer1.weight
        primals_7 = self.layer1.bias
        primals_8 = self.layer2.weight
        primals_9 = self.layer2.bias
        primals_10 = self.layerA1.weight
        primals_11 = self.layerA1.bias
        primals_12 = self.layerA2.weight
        primals_13 = self.layerA2.bias
        primals_14 = self.layerC1.weight
        primals_15 = self.layerC1.bias
        primals_16 = self.layerC2.weight
        primals_17 = self.layerC2.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11, primals_12, primals_13, primals_14,
            primals_15, primals_16, primals_17])
        return output[0], output[1]


Measured latency_ms: 0.16395