import torch
from torch import nn
from torch.nn.utils import weight_norm


class K1TemporalBlock(nn.Module):

    def __init__(self, n_inputs, n_outputs, dropout=0.2):
        super(K1TemporalBlock, self).__init__()
        self.conv1 = weight_norm(nn.Conv1d(n_inputs, n_outputs, 1))
        self.relu1 = nn.ReLU()
        self.dropout1 = nn.Dropout(dropout)
        self.conv2 = weight_norm(nn.Conv1d(n_outputs, n_outputs, 1))
        self.relu2 = nn.ReLU()
        self.dropout2 = nn.Dropout(dropout)
        self.net = nn.Sequential(self.conv1, self.relu1, self.dropout1,
            self.conv2, self.relu2, self.dropout2)
        self.downsample = nn.Conv1d(n_inputs, n_outputs, 1
            ) if n_inputs != n_outputs else None
        self.relu = nn.ReLU()
        self.init_weights()

    def init_weights(self):
        self.conv1.weight.data.normal_(0, 0.01)
        self.conv2.weight.data.normal_(0, 0.01)
        if self.downsample is not None:
            self.downsample.weight.data.normal_(0, 0.01)

    def forward(self, x):
        out = self.net(x)
        res = x if self.downsample is None else self.downsample(x)
        return self.relu(out + res)


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


def get_init_inputs():
    return [[], {'n_inputs': 4, 'n_outputs': 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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice
from torch import nn
from torch.nn.utils import weight_norm
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__weight_norm_interface_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 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp11 = libdevice.sqrt(tmp10)
    tl.store(out_ptr0 + x0, tmp11, xmask)


@triton.jit
def triton_poi_fused__weight_norm_interface_1(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp3 = tmp1 / tmp2
    tmp4 = tmp0 * tmp3
    tl.store(out_ptr0 + x2, tmp4, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_2(in_out_ptr0, in_ptr0,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp5 = 0.0
    tmp6 = tmp4 <= tmp5
    tl.store(in_out_ptr0 + x2, tmp4, xmask)
    tl.store(out_ptr0 + x2, tmp6, xmask)


@triton.jit
def triton_poi_fused_add_relu_threshold_backward_3(in_ptr0, in_ptr1,
    in_ptr2, out_ptr0, out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr2 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp6 = tmp4 + tmp5
    tmp7 = triton_helpers.maximum(tmp3, tmp6)
    tmp8 = 0.0
    tmp9 = tmp4 <= tmp8
    tmp10 = tmp7 <= tmp8
    tl.store(out_ptr0 + x2, tmp7, xmask)
    tl.store(out_ptr1 + x2, tmp9, xmask)
    tl.store(out_ptr2 + x2, tmp10, 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, 1, 1), (1, 1, 1))
    assert_size_stride(primals_2, (4, 4, 1), (4, 1, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4, 1, 1), (1, 1, 1))
    assert_size_stride(primals_6, (4, 4, 1), (4, 1, 1))
    assert_size_stride(primals_7, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__weight_norm_interface_0[grid(4)](primals_2, buf0,
            4, XBLOCK=4, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
        triton_poi_fused__weight_norm_interface_1[grid(16)](primals_2,
            primals_1, buf0, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf2 = extern_kernels.convolution(reinterpret_tensor(primals_4, (1,
            4, 4), (16, 4, 1), 0), buf1, stride=(1,), padding=(0,),
            dilation=(1,), transposed=False, output_padding=(0,), groups=1,
            bias=None)
        assert_size_stride(buf2, (1, 4, 4), (16, 4, 1))
        buf3 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        triton_poi_fused__weight_norm_interface_0[grid(4)](primals_6, buf3,
            4, XBLOCK=4, num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
        triton_poi_fused__weight_norm_interface_1[grid(16)](primals_6,
            primals_5, buf3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf5 = reinterpret_tensor(buf2, (4, 4), (4, 1), 0)
        del buf2
        buf10 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_2[grid(16)](buf5,
            primals_3, buf10, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del primals_3
        buf6 = extern_kernels.convolution(reinterpret_tensor(buf5, (1, 4, 4
            ), (0, 4, 1), 0), buf4, stride=(1,), padding=(0,), dilation=(1,
            ), transposed=False, output_padding=(0,), groups=1, bias=None)
        assert_size_stride(buf6, (1, 4, 4), (16, 4, 1))
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf9 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        buf8 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused_add_relu_threshold_backward_3[grid(16)](buf6,
            primals_7, primals_4, buf7, buf9, buf8, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf6
        del primals_7
    return (buf7, buf1, buf4, primals_1, primals_2, primals_5, primals_6,
        buf0, buf1, reinterpret_tensor(primals_4, (1, 4, 4), (16, 4, 1), 0),
        buf3, buf4, reinterpret_tensor(buf5, (1, 4, 4), (16, 4, 1), 0),
        buf8, buf9, buf10)


class K1TemporalBlockNew(nn.Module):

    def __init__(self, n_inputs, n_outputs, dropout=0.2):
        super(K1TemporalBlockNew, self).__init__()
        self.conv1 = weight_norm(nn.Conv1d(n_inputs, n_outputs, 1))
        self.relu1 = nn.ReLU()
        self.dropout1 = nn.Dropout(dropout)
        self.conv2 = weight_norm(nn.Conv1d(n_outputs, n_outputs, 1))
        self.relu2 = nn.ReLU()
        self.dropout2 = nn.Dropout(dropout)
        self.net = nn.Sequential(self.conv1, self.relu1, self.dropout1,
            self.conv2, self.relu2, self.dropout2)
        self.downsample = nn.Conv1d(n_inputs, n_outputs, 1
            ) if n_inputs != n_outputs else None
        self.relu = nn.ReLU()
        self.init_weights()

    def init_weights(self):
        self.conv1.weight.data.normal_(0, 0.01)
        self.conv2.weight.data.normal_(0, 0.01)
        if self.downsample is not None:
            self.downsample.weight.data.normal_(0, 0.01)

    def forward(self, input_0):
        primals_3 = self.conv1.bias
        primals_1 = self.conv1.weight_g
        primals_2 = self.conv1.weight_v
        primals_7 = self.conv2.bias
        primals_5 = self.conv2.weight_g
        primals_6 = self.conv2.weight_v
        primals_4 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0]
