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
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


@triton.jit
def triton_poi_fused_add_mul_relu_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 4 % 4
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp11 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp1 = 1.0
    tmp2 = tmp0 / tmp1
    tmp3 = tmp0 - tmp2
    tmp4 = tmp3 * tmp3
    tmp5 = 0.0
    tmp6 = tmp4 / tmp5
    tmp7 = 1e-05
    tmp8 = tmp6 + tmp7
    tmp9 = libdevice.sqrt(tmp8)
    tmp10 = tmp3 / tmp9
    tmp12 = tmp10 * tmp11
    tmp14 = tmp12 + tmp13
    tmp15 = tl.full([1], 0, tl.int32)
    tmp16 = triton_helpers.maximum(tmp15, tmp14)
    tl.store(out_ptr0 + x3, tmp16, xmask)


@triton.jit
def triton_poi_fused_add_1(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
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tl.load(in_out_ptr0 + x0, xmask)
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x0, 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, (1, 4, 1), (4, 1, 1))
    assert_size_stride(primals_3, (1, 4, 1), (4, 1, 1))
    assert_size_stride(primals_4, (4, 4, 1), (4, 1, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_mul_relu_0[grid(64)](primals_1, primals_2,
            primals_3, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del primals_2
        del primals_3
        buf1 = extern_kernels.convolution(buf0, primals_4, stride=(1,),
            padding=(0,), dilation=(1,), transposed=False, output_padding=(
            0,), groups=1, bias=None)
        assert_size_stride(buf1, (4, 4, 4), (16, 4, 1))
        buf2 = buf1
        del buf1
        triton_poi_fused_add_1[grid(64)](buf2, primals_1, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
    return buf2, primals_1, primals_4, buf0


class GroupNorm1d(nn.Module):
    """ Group normalization that does per-point group normalization.

    Args:
        groups (int): number of groups
        f_dim (int): feature dimension, mush be divisible by groups
    """

    def __init__(self, groups, f_dim, eps=1e-05, affine=True):
        super().__init__()
        self.groups = groups
        self.f_dim = f_dim
        self.affine = affine
        self.eps = eps
        assert f_dim % groups == 0
        if affine:
            self.gamma = nn.Parameter(torch.ones(1, f_dim, 1))
            self.beta = nn.Parameter(torch.zeros(1, f_dim, 1))

    def forward(self, x):
        batch_size, D, T = x.size()
        net = x.view(batch_size, self.groups, D // self.groups, T)
        means = net.mean(2, keepdim=True)
        variances = net.var(2, keepdim=True)
        net = (net - means) / (variances + self.eps).sqrt()
        net = net.view(batch_size, D, T)
        if self.affine:
            return net * self.gamma + self.beta
        else:
            return net


class ResnetBlockGroupNormShallowConv1dNew(nn.Module):
    """ Fully connected ResNet Block imeplemented with group convolutions and group normalizations.

    Args:
        size_in (int): input dimension
        groups (int): number of groups for group convolutions
        gn_groups (int): number of groups for group normalizations
        size_out (int): output dimension
        size_h (int): hidden dimension
    """

    def __init__(self, size_in, groups, gn_groups=4, size_out=None, size_h=
        None, dropout_prob=0.0, leaky=False):
        super().__init__()
        if size_out is None:
            size_out = size_in
        if size_h is None:
            size_h = min(size_in, size_out)
        if dropout_prob > 0.0:
            self.dropout = nn.Dropout(dropout_prob, inplace=True)
        else:
            self.dropout = None
        self.size_in = size_in
        self.size_h = size_h
        self.size_out = size_out
        self.gn_0 = GroupNorm1d(groups * gn_groups, size_in)
        self.fc_0 = nn.Conv1d(size_in, size_h, 1, groups=groups, bias=False)
        if not leaky:
            self.actvn = nn.ReLU()
        else:
            self.actvn = nn.LeakyReLU(0.1)
        if size_in == size_out:
            self.shortcut = None
        else:
            self.shortcut = nn.Conv1d(size_in, size_out, 1, bias=False,
                groups=groups)

    def forward(self, input_0):
        primals_2 = self.gn_0.gamma
        primals_3 = self.gn_0.beta
        primals_4 = self.fc_0.weight
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.037852