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
import torch.utils.data
from typing import Dict
from typing import Tuple
from abc import ABC
from abc import abstractmethod
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_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x2, xmask)
    tmp3 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 + tmp3
    tl.store(in_out_ptr0 + x2, tmp4, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (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_2, (64, 4), (4, 1), 0),
            primals_1, out=buf0)
        del primals_1
        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_3, primals_4, 
            256, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_4
    return buf1, primals_3, reinterpret_tensor(primals_2, (4, 64), (1, 4), 0)


class BaseLayer(nn.Module, ABC):
    """
    Base Layer for the torecsys module
    """

    def __init__(self, **kwargs):
        """
        Initializer for BaseLayer

        Args:
            **kwargs: kwargs
        """
        super(BaseLayer, self).__init__()

    @property
    @abstractmethod
    def inputs_size(self) ->Dict[str, Tuple[str, ...]]:
        """
        Get inputs size of the layer

        Returns:
            Dict[str, Tuple[str, ...]]: dictionary of inputs_size
        """
        raise NotImplementedError('not implemented')

    @property
    @abstractmethod
    def outputs_size(self) ->Dict[str, Tuple[str, ...]]:
        """
        Get outputs size of the layer

        Returns:
            Dict[str, Tuple[str, ...]]: dictionary of outputs_size
        """
        raise NotImplementedError('not implemented')


class FieldAllTypeBilinearNew(BaseLayer):
    """
    Applies a bilinear transformation to the incoming data: :math:`y = x_1 \\cdot W \\odot x_2 + b`
    
    Args:
        in1_features: size of each first input sample
        in2_features: size of each second input sample
        bias: If set to False, the layer will not learn an additive bias. Default: ``True``

    Shape:
        - Input1: :math:`(N, *, H_{in1})` where :math:`H_{in1}=\\text{in1\\_features}` and
            :math:`*` means any number of additional dimensions. All but the last dimension
            of the inputs should be the same
        - Input2: :math:`(N, *, H_{in2})` where :math:`H_{in2}=\\text{in2\\_features}`.
        - Output: :math:`(N, *, H_{out})` where :math:`H_{out}=\\text{out\\_features}`
            and all but the last dimension are the same shape as the input
    
    Examples::

        >>> m = FieldAllTypeBilinear(20, 20)
        >>> input1 = torch.randn(128, 10, 20)
        >>> input2 = torch.randn(128, 10, 3)
        >>> output = m(input1, input2)
        >>> print(output.size())
            torch.Size([128, 10, 3])
    """

    @property
    def inputs_size(self) ->Dict[str, Tuple[str, ...]]:
        return {'inputs1': ('B', 'NC2', 'E'), 'inputs2': ('B', 'NC2', 'E')}

    @property
    def outputs_size(self) ->Dict[str, Tuple[str, ...]]:
        return {'outputs': ('B', 'NC2', 'E')}
    __constants__ = ['in1_features', 'in2_features', 'bias']

    def __init__(self, in1_features, in2_features, bias=True):
        super(FieldAllTypeBilinearNew, self).__init__()
        self.in1_features = in1_features
        self.in2_features = in2_features
        self.weight = nn.Parameter(torch.Tensor(in1_features, in2_features))
        if bias:
            self.bias = nn.Parameter(torch.Tensor(in2_features))
        else:
            self.register_parameter('bias', nn.Parameter(torch.tensor([0])))
        self.reset_parameters()

    def reset_parameters(self):
        bound = 1 / math.sqrt(self.weight.shape[0])
        nn.init.uniform_(self.weight, -bound, bound)
        if self.bias is not None:
            nn.init.uniform_(self.bias, -bound, bound)

    def extra_repr(self):
        return (
            f'in1_features={self.in1_features}, in2_features={self.in2_features}, bias={self.bias is not None}'
            )

    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]


Measured latency_ms: 0.038248