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
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_add_mul_0(in_out_ptr0, in_ptr0, in_ptr1, 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 % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_out_ptr0 + x2, xmask)
    tmp4 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp3 = tmp2 + tmp1
    tmp5 = tmp3 + tmp4
    tl.store(in_out_ptr0 + x2, tmp5, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0)
        del primals_2
        buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf0
        get_raw_stream(0)
        triton_poi_fused_add_mul_0[grid(256)](buf1, primals_1, primals_3, 
            256, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_3
    return buf1, primals_1


class TaylorNetNew(nn.Module):
    """Applies a non-linear multiplicative transformation to the incoming data,
    in order to generate output features that can be quadratic and linear in the 
    input features: 
    :math:`y = (x W_2^T) * (x W_1^T) + x W_1^T + b`
    
    Note that if output size = input size, then W_2 is not used, and the
    transformation becomes:
    :math:`y = x * (x W^T) + x W^T + b`

    Args:
        in_features: size of each input sample
        out_features: size of each output sample
        bias: If set to False, the layer will not learn an additive bias.
            Default: ``True``

    Shape:
        - Input: :math:`(N, *, in\\_features)` where :math:`*` means any number of
          additional dimensions
        - Output: :math:`(N, *, out\\_features)` where :math:`*` means any number of
          additional dimensions

    Attributes:
        weight_1: the learnable weights of the module of shape
            `(out_features x in_features)`
        weight_2: the learnable weights of the module of shape
            `(out_features x in_features)`
            If out_features = in_features, there is no weight_2 matrix
        bias:   the learnable bias of the module of shape `(in_features)`

    Examples::

        >>> m = nn.TaylorNet(5)
        >>> input = torch.randn(128, 5)
        >>> output = m(input)
        >>> print(output.size())
    """

    def __init__(self, in_features, out_features=None, bias=True):
        super(TaylorNetNew, self).__init__()
        if out_features is None:
            out_features = in_features
        self.in_features = in_features
        self.out_features = out_features
        self.weight1 = nn.Parameter(torch.Tensor(out_features, in_features))
        if in_features != out_features:
            self.weight2 = nn.Parameter(torch.Tensor(out_features, in_features)
                )
        else:
            self.weight2 = None
        if bias:
            self.bias = nn.Parameter(torch.Tensor(out_features))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1.0 / math.sqrt(self.weight1.size(1))
        nn.init.xavier_normal_(self.weight1)
        if self.weight2 is not None:
            nn.init.xavier_normal_(self.weight2)
        if self.bias is not None:
            self.bias.data.uniform_(-stdv, stdv)

    def extra_repr(self):
        return 'in_features={}, out_features={}, bias={}'.format(self.
            in_features, self.out_features, self.bias is not None)

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


Measured latency_ms: 0.022404