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 torch.utils.data
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_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 // 64
    x3 = xindex % 64
    x0 = xindex % 4
    x4 = xindex
    tmp3 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + x4, xmask)
    tmp0 = x2
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp5 = tmp3 + tmp4
    tmp7 = tl.where(tmp2, tmp5, tmp6)
    tl.store(in_out_ptr0 + x4, tmp7, xmask)


def call(args):
    primals_1, primals_2, primals_3 = 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,), (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),
            reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf2 = buf1
        del buf1
        get_raw_stream(0)
        triton_poi_fused_add_0[grid(256)](buf2, buf0, primals_3, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        del buf0
        del primals_3
    return buf2, reinterpret_tensor(primals_2, (64, 4), (4, 1), 0)


class ZonoDenseLayerNew(torch.nn.Module):
    """
    Class implementing a dense layer on a zonotope.
    Bias is only added to the zeroth term.
    """

    def __init__(self, in_features: 'int', out_features: 'int'):
        super().__init__()
        self.weight = torch.nn.Parameter(torch.normal(mean=torch.zeros(
            out_features, in_features), std=torch.ones(out_features,
            in_features)))
        self.bias = torch.nn.Parameter(torch.normal(mean=torch.zeros(
            out_features), std=torch.ones(out_features)))

    def __call__(self, x: "'torch.Tensor'") ->'torch.Tensor':
        return self.forward(x)

    def zonotope_matmul(self, x: "'torch.Tensor'") ->'torch.Tensor':
        """
        Matrix multiplication for dense layer.

        :param x: input to the dense layer.
        :return: zonotope after weight multiplication.
        """
        return torch.matmul(x, torch.transpose(self.weight, 0, 1))

    def zonotope_add(self, x: "'torch.Tensor'") ->'torch.Tensor':
        """
        Modification required compared to the normal torch dense layer.
        The bias is added only to the central zonotope term and not the error terms.

        :param x: zonotope input to have the bias added.
        :return: zonotope with the bias added to the central (first) term.
        """
        x[0] = x[0] + self.bias
        return x

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


Measured latency_ms: 0.022963