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 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


@triton.jit
def triton_poi_fused_convolution_mul_sin_0(in_out_ptr0, in_ptr0, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex
    x1 = xindex // 16 % 64
    tmp0 = tl.load(in_out_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 30.0
    tmp4 = tmp2 * tmp3
    tmp5 = tl_math.sin(tmp4)
    tl.store(in_out_ptr0 + x3, tmp2, None)
    tl.store(out_ptr0 + x3, tmp5, None)


@triton.jit
def triton_poi_fused_convolution_mul_sin_1(in_out_ptr0, in_ptr0, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex
    x1 = xindex // 16 % 32
    tmp0 = tl.load(in_out_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 30.0
    tmp4 = tmp2 * tmp3
    tmp5 = tl_math.sin(tmp4)
    tl.store(in_out_ptr0 + x3, tmp2, None)
    tl.store(out_ptr0 + x3, tmp5, None)


@triton.jit
def triton_poi_fused_convolution_mul_sin_2(in_out_ptr0, in_ptr0, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 16 % 4
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 30.0
    tmp4 = tmp2 * tmp3
    tmp5 = tl_math.sin(tmp4)
    tl.store(in_out_ptr0 + x3, tmp2, xmask)
    tl.store(out_ptr0 + x3, tmp5, 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, (64, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_2, (64,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (32, 64, 1, 1), (64, 1, 1, 1))
    assert_size_stride(primals_5, (32,), (1,))
    assert_size_stride(primals_6, (4, 32, 1, 1), (32, 1, 1, 1))
    assert_size_stride(primals_7, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 64, 4, 4), (1024, 16, 4, 1))
        buf1 = buf0
        del buf0
        buf2 = empty_strided_cuda((4, 64, 4, 4), (1024, 16, 4, 1), torch.
            float32)
        get_raw_stream(0)
        triton_poi_fused_convolution_mul_sin_0[grid(4096)](buf1, primals_2,
            buf2, 4096, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_2
        buf3 = extern_kernels.convolution(buf2, primals_4, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 32, 4, 4), (512, 16, 4, 1))
        buf4 = buf3
        del buf3
        buf5 = empty_strided_cuda((4, 32, 4, 4), (512, 16, 4, 1), torch.float32
            )
        triton_poi_fused_convolution_mul_sin_1[grid(2048)](buf4, primals_5,
            buf5, 2048, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_5
        buf6 = extern_kernels.convolution(buf5, primals_6, stride=(1, 1),
            padding=(0, 0), 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))
        buf7 = buf6
        del buf6
        buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_convolution_mul_sin_2[grid(256)](buf7, primals_7,
            buf8, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_7
    return (buf8, primals_1, primals_3, primals_4, primals_6, buf1, buf2,
        buf4, buf5, buf7)


def act(act_fun='LeakyReLU'):
    """
        Either string defining an activation function or module (e.g. nn.ReLU)
    """
    if isinstance(act_fun, str):
        if act_fun == 'LeakyReLU':
            return nn.LeakyReLU(0.2, inplace=True)
        elif act_fun == 'Swish':
            return Swish()
        elif act_fun[:3] == 'ELU':
            if len(act_fun) > 3:
                param = float(act_fun[3:])
                return nn.ELU(param, inplace=True)
            return nn.ELU(inplace=True)
        elif act_fun == 'ReLU':
            return nn.ReLU()
        elif act_fun == 'tanh':
            return Tanh()
        elif act_fun == 'sine':
            return Sin()
        elif act_fun == 'soft':
            return nn.Softplus()
        elif act_fun == 'none':
            return nn.Sequential()
        else:
            assert False
    else:
        return act_fun()


class Swish(nn.Module):
    """
        https://arxiv.org/abs/1710.05941
        The hype was so huge that I could not help but try it
    """

    def __init__(self):
        super(Swish, self).__init__()
        self.s = nn.Sigmoid()

    def forward(self, x):
        return x * self.s(x)


class Tanh(nn.Module):
    """
        https://arxiv.org/abs/1710.05941
        The hype was so huge that I could not help but try it
    """

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

    def forward(self, x):
        return torch.tanh(x)


class Sin(nn.Module):
    """
        https://arxiv.org/abs/1710.05941
        The hype was so huge that I could not help but try it
    """

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

    def forward(self, x):
        return torch.sin(x)


class SIREN_layer(nn.Module):

    def __init__(self, ch_in, ch_out, frist=False, act_fun='sine', omega_0=30):
        super(SIREN_layer, self).__init__()
        self.conv1 = nn.Conv2d(ch_in, ch_out, kernel_size=1, stride=1, bias
            =True)
        self.act_fun = act(act_fun)
        self.omega_0 = omega_0
        self.in_features = ch_in
        self.frist = frist
        self.init()

    def init(self):
        with torch.no_grad():
            if self.frist:
                self.conv1.weight.uniform_(-1 / self.in_features, 1 / self.
                    in_features)
            else:
                self.conv1.weight.uniform_(-np.sqrt(6 / self.in_features) /
                    self.omega_0, np.sqrt(6 / self.in_features) / self.omega_0)

    def forward(self, x):
        x = self.conv1(x)
        return self.act_fun(self.omega_0 * x)


class SIREN_CONVNew(nn.Module):

    def __init__(self, ch_in, ch_out):
        super(SIREN_CONVNew, self).__init__()
        self.conv1 = SIREN_layer(ch_in, 64, frist=True)
        self.conv2 = SIREN_layer(64, 32)
        self.conv3 = SIREN_layer(32, ch_out)
        self.conv = nn.Sequential(self.conv1, self.conv2, self.conv3)

    def forward(self, input_0):
        primals_1 = self.conv1.conv1.weight
        primals_2 = self.conv1.conv1.bias
        primals_4 = self.conv2.conv1.weight
        primals_5 = self.conv2.conv1.bias
        primals_6 = self.conv3.conv1.weight
        primals_7 = self.conv3.conv1.bias
        primals_3 = 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.014152