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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
import torch.optim
import torch.utils.data
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_ones_like_pow_0(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
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tmp0 * tmp0
    tl.store(out_ptr0 + x0, tmp1, xmask)


@triton.jit
def triton_poi_fused_convolution_ones_like_pow_1(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
    tmp0 = 1.0
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_per_fused_pow_sum_2(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0)
    tmp1 = tmp0 * tmp0
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp4 = tl.where(xmask, tmp2, 0)
    tmp5 = tl.sum(tmp4, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp5, xmask)


@triton.jit
def triton_per_fused_abs_add_any_isnan_mul_sgn_sqrt_sub_3(in_ptr0, in_ptr1,
    in_ptr2, out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr
    ):
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    r0 = rindex % 4
    tmp0 = tl.load(in_ptr0 + r2, None)
    tmp1 = tl.load(in_ptr1 + r0, None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + r2, None)
    tmp2 = tmp0 + tmp1
    tmp4 = 2.0
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 - tmp5
    tmp7 = tl_math.abs(tmp6)
    tmp8 = 1e-14
    tmp9 = tmp7 + tmp8
    tmp10 = libdevice.sqrt(tmp9)
    tmp11 = tl.full([1, 1], 0, tl.int32)
    tmp12 = tmp11 < tmp6
    tmp13 = tmp12.to(tl.int8)
    tmp14 = tmp6 < tmp11
    tmp15 = tmp14.to(tl.int8)
    tmp16 = tmp13 - tmp15
    tmp17 = tmp16.to(tmp6.dtype)
    tmp18 = libdevice.isnan(tmp10).to(tl.int1)
    tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp21 = triton_helpers.any(tmp19, 1)[:, None]
    tl.store(out_ptr0 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp10, None)
    tl.store(out_ptr1 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp17, None)
    tl.store(out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp21, None)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_convolution_ones_like_pow_0[grid(256)](primals_2,
            buf0, 256, XBLOCK=128, num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_convolution_ones_like_pow_1[grid(256)](buf1, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        buf2 = extern_kernels.convolution(buf0, buf1, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 4, 1, 1), (4, 1, 1, 1))
        del buf0
        del buf1
        buf3 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_per_fused_pow_sum_2[grid(4)](primals_1, buf3, 4, 64, XBLOCK=
            1, num_warps=2, num_stages=1)
        buf4 = extern_kernels.convolution(primals_2, primals_1, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf4, (4, 4, 1, 1), (4, 1, 1, 1))
        buf5 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.float32)
        buf6 = empty_strided_cuda((), (), torch.bool)
        triton_per_fused_abs_add_any_isnan_mul_sgn_sqrt_sub_3[grid(1)](buf2,
            buf3, buf4, buf5, buf7, buf6, 1, 16, XBLOCK=1, num_warps=2,
            num_stages=1)
        del buf2
        del buf3
        del buf4
    return buf5, buf6, primals_1, primals_2, buf5, buf7


class L2Conv2DNew(nn.Module):
    """
    Convolutional layer that computes the squared L2 distance instead of the conventional inner product. 
    """

    def __init__(self, num_prototypes, num_features, w_1, h_1):
        """
        Create a new L2Conv2D layer
        :param num_prototypes: The number of prototypes in the layer
        :param num_features: The number of channels in the input features
        :param w_1: Width of the prototypes
        :param h_1: Height of the prototypes
        """
        super().__init__()
        prototype_shape = num_prototypes, num_features, w_1, h_1
        self.prototype_vectors = nn.Parameter(torch.randn(prototype_shape),
            requires_grad=True)

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


Measured latency_ms: 0.074681