import math
import torch


def cheb_conv(laplacian, inputs, weight):
    """Chebyshev convolution.

    Args:
        laplacian (:obj:`torch.sparse.Tensor`): The laplacian corresponding to the current sampling of the sphere.
        inputs (:obj:`torch.Tensor`): The current input data being forwarded.
        weight (:obj:`torch.Tensor`): The weights of the current layer.

    Returns:
        :obj:`torch.Tensor`: Inputs after applying Chebyshev convolution.
    """
    B, V, Fin = inputs.shape
    K, Fin, Fout = weight.shape
    x0 = inputs.permute(1, 2, 0).contiguous()
    x0 = x0.view([V, Fin * B])
    inputs = x0.unsqueeze(0)
    if K > 0:
        x1 = torch.sparse.mm(laplacian, x0)
        inputs = torch.cat((inputs, x1.unsqueeze(0)), 0)
        for _ in range(1, K - 1):
            x2 = 2 * torch.sparse.mm(laplacian, x1) - x0
            inputs = torch.cat((inputs, x2.unsqueeze(0)), 0)
            x0, x1 = x1, x2
    inputs = inputs.view([K, V, Fin, B])
    inputs = inputs.permute(3, 1, 2, 0).contiguous()
    inputs = inputs.view([B * V, Fin * K])
    weight = weight.view(Fin * K, Fout)
    inputs = inputs.matmul(weight)
    inputs = inputs.view([B, V, Fout])
    return inputs


class ChebConv(torch.nn.Module):
    """Graph convolutional layer.
    """

    def __init__(self, in_channels, out_channels, kernel_size, bias=True,
        conv=cheb_conv):
        """Initialize the Chebyshev layer.

        Args:
            in_channels (int): Number of channels/features in the input graph.
            out_channels (int): Number of channels/features in the output graph.
            kernel_size (int): Number of trainable parameters per filter, which is also the size of the convolutional kernel.
                                The order of the Chebyshev polynomials is kernel_size - 1.
            bias (bool): Whether to add a bias term.
            conv (callable): Function which will perform the actual convolution.
        """
        super().__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = kernel_size
        self._conv = conv
        shape = kernel_size, in_channels, out_channels
        self.weight = torch.nn.Parameter(torch.Tensor(*shape))
        if bias:
            self.bias = torch.nn.Parameter(torch.Tensor(out_channels))
        else:
            self.register_parameter('bias', None)
        self.kaiming_initialization()

    def kaiming_initialization(self):
        """Initialize weights and bias.
        """
        std = math.sqrt(2 / (self.in_channels * self.kernel_size))
        self.weight.data.normal_(0, std)
        if self.bias is not None:
            self.bias.data.fill_(0.01)

    def forward(self, laplacian, inputs):
        """Forward graph convolution.

        Args:
            laplacian (:obj:`torch.sparse.Tensor`): The laplacian corresponding to the current sampling of the sphere.
            inputs (:obj:`torch.Tensor`): The current input data being forwarded.

        Returns:
            :obj:`torch.Tensor`: The convoluted inputs.
        """
        outputs = self._conv(laplacian, inputs, self.weight)
        if self.bias is not None:
            outputs += self.bias
        return outputs


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


def get_init_inputs():
    return [[], {'in_channels': 4, 'out_channels': 4, 'kernel_size': 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
import math
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_zeros_0(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    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_clone_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x1 = xindex
    y0 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 16 * x1), xmask & ymask, eviction_policy
        ='evict_last')
    tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_mul_sub_2(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 4
    xnumel = 16
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x1 = xindex
    y0 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr1 + (x1 + 16 * y0), xmask & ymask, eviction_policy
        ='evict_last')
    tmp1 = 2.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 - tmp3
    tl.store(out_ptr0 + (x1 + 16 * y0), tmp4, xmask & ymask)


@triton.jit
def triton_poi_fused_cat_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
    ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 64
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    y1 = yindex // 16
    x2 = xindex
    y0 = yindex % 16
    tmp0 = y1
    tl.full([1, 1], 0, tl.int64)
    tmp3 = tl.full([1, 1], 3, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.broadcast_to(y1, [XBLOCK, YBLOCK])
    tmp7 = tl.full([1, 1], 2, tl.int64)
    tmp8 = tmp5 < tmp7
    tmp9 = tmp8 & tmp4
    tmp10 = tl.full([1, 1], 1, tl.int64)
    tmp11 = tmp5 < tmp10
    tmp12 = tmp11 & tmp9
    tmp13 = tl.load(in_ptr0 + (y0 + 16 * x2), tmp12 & xmask & ymask,
        eviction_policy='evict_last', other=0.0)
    tmp14 = tmp5 >= tmp10
    tmp15 = tmp14 & tmp9
    tmp16 = tl.load(in_ptr1 + (x2 + 4 * y0), tmp15 & xmask & ymask,
        eviction_policy='evict_last', other=0.0)
    tmp17 = tl.where(tmp11, tmp13, tmp16)
    tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype)
    tmp19 = tl.where(tmp9, tmp17, tmp18)
    tmp20 = tmp5 >= tmp7
    tmp22 = tmp20 & tmp4
    tmp23 = tl.load(in_ptr2 + (x2 + 4 * y0), tmp22 & xmask & ymask,
        eviction_policy='evict_last', other=0.0)
    tmp24 = 2.0
    tmp25 = tmp23 * tmp24
    tmp26 = tl.load(in_ptr0 + (y0 + 16 * x2), tmp22 & xmask & ymask,
        eviction_policy='evict_last', other=0.0)
    tmp27 = tmp25 - tmp26
    tmp28 = tl.full(tmp27.shape, 0.0, tmp27.dtype)
    tmp29 = tl.where(tmp22, tmp27, tmp28)
    tmp30 = tl.where(tmp8, tmp19, tmp29)
    tmp31 = tl.full(tmp30.shape, 0.0, tmp30.dtype)
    tmp32 = tl.where(tmp4, tmp30, tmp31)
    tmp33 = tmp0 >= tmp3
    tl.full([1, 1], 4, tl.int64)
    tmp36 = tl.load(in_ptr3 + (x2 + 4 * y0), tmp33 & xmask & ymask,
        eviction_policy='evict_last', other=0.0)
    tmp37 = tmp36 * tmp24
    tmp38 = tl.load(in_ptr1 + (x2 + 4 * y0), tmp33 & xmask & ymask,
        eviction_policy='evict_last', other=0.0)
    tmp39 = tmp37 - tmp38
    tmp40 = tl.full(tmp39.shape, 0.0, tmp39.dtype)
    tmp41 = tl.where(tmp33, tmp39, tmp40)
    tmp42 = tl.where(tmp4, tmp32, tmp41)
    tl.store(out_ptr0 + (y0 + 16 * x2 + 64 * y1), tmp42, xmask & ymask)


@triton.jit
def triton_poi_fused_clone_view_4(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
    x0 = xindex % 16
    x1 = xindex // 16
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (4 * (x0 // 4) + 16 * (x1 % 4) + 64 * (x0 % 4) +
        x1 // 4), xmask, eviction_policy='evict_last')
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused_add_5(in_out_ptr0, in_ptr0, 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 % 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x2, tmp2, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_4, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_zeros_0[grid(64)](buf0, 64, XBLOCK=64, num_warps=1,
            num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_clone_1[grid(16, 4)](primals_3, buf1, 16, 4,
            XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        del primals_3
        buf2 = torch.ops.aten._sparse_addmm.default(reinterpret_tensor(buf0,
            (16, 4), (1, 16), 0), reinterpret_tensor(buf1, (16, 4), (1, 16),
            0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), beta=0)
        buf3 = buf2
        del buf2
        buf4 = torch.ops.aten._sparse_addmm.default(reinterpret_tensor(buf0,
            (16, 4), (1, 16), 0), buf3, reinterpret_tensor(primals_2, (4, 4
            ), (1, 4), 0), beta=0)
        buf5 = buf4
        del buf4
        buf6 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        triton_poi_fused_mul_sub_2[grid(4, 16)](buf5, buf1, buf6, 4, 16,
            XBLOCK=8, YBLOCK=4, num_warps=1, num_stages=1)
        buf7 = torch.ops.aten._sparse_addmm.default(reinterpret_tensor(buf0,
            (16, 4), (1, 16), 0), reinterpret_tensor(buf6, (16, 4), (1, 16),
            0), reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), beta=0)
        del buf0
        del buf6
        del primals_2
        buf8 = buf7
        del buf7
        buf9 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32)
        triton_poi_fused_cat_3[grid(64, 4)](buf1, buf3, buf5, buf8, buf9, 
            64, 4, XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1)
        del buf1
        del buf3
        del buf5
        buf10 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        triton_poi_fused_clone_view_4[grid(256)](buf9, buf10, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        del buf9
        buf11 = buf8
        del buf8
        extern_kernels.mm(buf10, reinterpret_tensor(primals_1, (16, 4), (4,
            1), 0), out=buf11)
        del primals_1
        buf12 = reinterpret_tensor(buf11, (4, 4, 4), (16, 4, 1), 0)
        del buf11
        triton_poi_fused_add_5[grid(64)](buf12, primals_4, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del primals_4
    return buf12, reinterpret_tensor(buf10, (16, 16), (1, 16), 0)


def cheb_conv(laplacian, inputs, weight):
    """Chebyshev convolution.

    Args:
        laplacian (:obj:`torch.sparse.Tensor`): The laplacian corresponding to the current sampling of the sphere.
        inputs (:obj:`torch.Tensor`): The current input data being forwarded.
        weight (:obj:`torch.Tensor`): The weights of the current layer.

    Returns:
        :obj:`torch.Tensor`: Inputs after applying Chebyshev convolution.
    """
    B, V, Fin = inputs.shape
    K, Fin, Fout = weight.shape
    x0 = inputs.permute(1, 2, 0).contiguous()
    x0 = x0.view([V, Fin * B])
    inputs = x0.unsqueeze(0)
    if K > 0:
        x1 = torch.sparse.mm(laplacian, x0)
        inputs = torch.cat((inputs, x1.unsqueeze(0)), 0)
        for _ in range(1, K - 1):
            x2 = 2 * torch.sparse.mm(laplacian, x1) - x0
            inputs = torch.cat((inputs, x2.unsqueeze(0)), 0)
            x0, x1 = x1, x2
    inputs = inputs.view([K, V, Fin, B])
    inputs = inputs.permute(3, 1, 2, 0).contiguous()
    inputs = inputs.view([B * V, Fin * K])
    weight = weight.view(Fin * K, Fout)
    inputs = inputs.matmul(weight)
    inputs = inputs.view([B, V, Fout])
    return inputs


class ChebConvNew(torch.nn.Module):
    """Graph convolutional layer.
    """

    def __init__(self, in_channels, out_channels, kernel_size, bias=True,
        conv=cheb_conv):
        """Initialize the Chebyshev layer.

        Args:
            in_channels (int): Number of channels/features in the input graph.
            out_channels (int): Number of channels/features in the output graph.
            kernel_size (int): Number of trainable parameters per filter, which is also the size of the convolutional kernel.
                                The order of the Chebyshev polynomials is kernel_size - 1.
            bias (bool): Whether to add a bias term.
            conv (callable): Function which will perform the actual convolution.
        """
        super().__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = kernel_size
        self._conv = conv
        shape = kernel_size, in_channels, out_channels
        self.weight = torch.nn.Parameter(torch.Tensor(*shape))
        if bias:
            self.bias = torch.nn.Parameter(torch.Tensor(out_channels))
        else:
            self.register_parameter('bias', None)
        self.kaiming_initialization()

    def kaiming_initialization(self):
        """Initialize weights and bias.
        """
        std = math.sqrt(2 / (self.in_channels * self.kernel_size))
        self.weight.data.normal_(0, std)
        if self.bias is not None:
            self.bias.data.fill_(0.01)

    def forward(self, input_0, input_1):
        primals_1 = self.weight
        primals_4 = self.bias
        primals_2 = input_0
        primals_3 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]
