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
import math
import torch.nn as nn
import torch.nn.functional as F
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_red_fused_add_mean_relu_threshold_backward_0(in_out_ptr0,
    in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr, RBLOCK: tl.constexpr):
    xnumel = 64
    rnumel = 4096
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    tmp1 = tl.load(in_ptr1 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    _tmp7 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp3 = tmp0 + tmp2
        tmp4 = tl.full([1, 1], 0, tl.int32)
        tmp5 = triton_helpers.maximum(tmp4, tmp3)
        tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
        tmp8 = _tmp7 + tmp6
        _tmp7 = tl.where(rmask & xmask, tmp8, _tmp7)
        tmp9 = 0.0
        tmp10 = tmp5 <= tmp9
        tl.store(out_ptr0 + (r1 + 4096 * x0), tmp10, rmask & xmask)
    tmp7 = tl.sum(_tmp7, 1)[:, None]
    tmp13 = tl.load(in_ptr2 + 0)
    tmp14 = tl.broadcast_to(tmp13, [XBLOCK, 1])
    tmp11 = 4096.0
    tmp12 = tmp7 / tmp11
    tmp15 = tmp12 + tmp14
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp15, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
    args.clear()
    assert_size_stride(primals_1, (16, 3, 3, 3), (27, 9, 3, 1))
    assert_size_stride(primals_2, (4, 3, 64, 64), (12288, 4096, 64, 1))
    assert_size_stride(primals_3, (1,), (1,))
    assert_size_stride(primals_4, (1,), (1,))
    assert_size_stride(primals_5, (4, 64), (64, 1))
    assert_size_stride(primals_6, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_2, primals_1, stride=(1, 
            1), padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 16, 64, 64), (65536, 4096, 64, 1))
        buf1 = empty_strided_cuda((4, 16, 1, 1), (16, 1, 64, 64), torch.float32
            )
        buf4 = empty_strided_cuda((4, 16, 64, 64), (65536, 4096, 64, 1),
            torch.bool)
        buf2 = reinterpret_tensor(buf1, (1, 64), (64, 1), 0)
        del buf1
        get_raw_stream(0)
        triton_red_fused_add_mean_relu_threshold_backward_0[grid(64)](buf2,
            buf0, primals_3, primals_4, buf4, 64, 4096, XBLOCK=1, RBLOCK=
            2048, num_warps=16, num_stages=1)
        del buf0
        del primals_3
        del primals_4
        buf3 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_6, buf2, reinterpret_tensor(primals_5,
            (64, 4), (1, 64), 0), alpha=1, beta=1, out=buf3)
        del primals_6
    return buf3, primals_1, primals_2, buf2, primals_5, buf4


class BasicBlock(nn.Module):
    m = 2

    def __init__(self, in_planes, out_planes, stride, dropout, fixup_l,
        fixup_coeff):
        super(BasicBlock, self).__init__()
        self._dropout = dropout
        self.relu = nn.ReLU(inplace=True)
        self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size=3, stride
            =stride, padding=1, bias=False)
        self.conv2 = nn.Conv2d(out_planes, out_planes, kernel_size=3,
            stride=1, padding=1, bias=False)
        self.equalInOut = in_planes == out_planes
        self.conv_res = nn.Conv2d(in_planes, out_planes, kernel_size=1,
            stride=stride, padding=0, bias=False)
        self.conv_res = not self.equalInOut and self.conv_res or None
        self.scale = nn.Parameter(torch.ones(1))
        self.biases = nn.ParameterList([nn.Parameter(torch.zeros(1)) for _ in
            range(4)])
        k = self.conv1.kernel_size[0] * self.conv1.kernel_size[1
            ] * self.conv1.out_channels
        self.conv1.weight.data.normal_(0, fixup_coeff * fixup_l ** (-1 / (2 *
            self.m - 2)) * math.sqrt(2.0 / k))
        self.conv2.weight.data.zero_()
        if self.conv_res is not None:
            k = self.conv_res.kernel_size[0] * self.conv_res.kernel_size[1
                ] * self.conv_res.out_channels
            self.conv_res.weight.data.normal_(0, math.sqrt(2.0 / k))

    def forward(self, x):
        x_out = self.relu(x + self.biases[0])
        out = self.conv1(x_out) + self.biases[1]
        out = self.relu(out) + self.biases[2]
        if self._dropout > 0:
            out = F.dropout(out, p=self._dropout, training=self.training)
        out = self.scale * self.conv2(out) + self.biases[3]
        if self.equalInOut:
            return torch.add(x, out)
        return torch.add(self.conv_res(x_out), out)


class NetworkBlock(nn.Module):

    def __init__(self, nb_layers, in_planes, out_planes, block, stride,
        dropout, fixup_l, fixup_coeff):
        super(NetworkBlock, self).__init__()
        self.layer = self._make_layer(block, in_planes, out_planes,
            nb_layers, stride, dropout, fixup_l, fixup_coeff)

    def _make_layer(self, block, in_planes, out_planes, nb_layers, stride,
        dropout, fixup_l, fixup_coeff):
        layers = []
        for i in range(int(nb_layers)):
            _in_planes = i == 0 and in_planes or out_planes
            _stride = i == 0 and stride or 1
            layers.append(block(_in_planes, out_planes, _stride, dropout=
                dropout, fixup_l=fixup_l, fixup_coeff=fixup_coeff))
        return nn.Sequential(*layers)

    def forward(self, x):
        return self.layer(x)


class WideResNetNew(nn.Module):

    def __init__(self, depth, num_classes, widen_factor=1, dropout=0.0,
        fixup_coeff=1):
        super(WideResNetNew, self).__init__()
        nChannels = [16, 16 * widen_factor, 32 * widen_factor, 64 *
            widen_factor]
        assert (depth - 4) % 6 == 0, 'You need to change the number of layers'
        n = (depth - 4) / 6
        block = BasicBlock
        fixup_l = n * 3
        self.conv1 = nn.Conv2d(3, nChannels[0], kernel_size=3, stride=1,
            padding=1, bias=False)
        self.block1 = NetworkBlock(n, nChannels[0], nChannels[1], block, 1,
            dropout=dropout, fixup_l=fixup_l, fixup_coeff=fixup_coeff)
        self.block2 = NetworkBlock(n, nChannels[1], nChannels[2], block, 2,
            dropout=dropout, fixup_l=fixup_l, fixup_coeff=fixup_coeff)
        self.block3 = NetworkBlock(n, nChannels[2], nChannels[3], block, 2,
            dropout=dropout, fixup_l=fixup_l, fixup_coeff=fixup_coeff)
        self.relu = nn.ReLU(inplace=True)
        self.fc = nn.Linear(nChannels[3], num_classes)
        self.nChannels = nChannels[3]
        self.fc.bias.data.zero_()
        self.fc.weight.data.zero_()
        k = self.conv1.kernel_size[0] * self.conv1.kernel_size[1
            ] * self.conv1.out_channels
        self.conv1.weight.data.normal_(0, math.sqrt(2.0 / k))
        self.bias1 = nn.Parameter(torch.zeros(1))
        self.bias2 = nn.Parameter(torch.zeros(1))

    def forward(self, input_0):
        primals_3 = self.bias1
        primals_4 = self.bias2
        primals_1 = self.conv1.weight
        primals_5 = self.fc.weight
        primals_6 = self.fc.bias
        primals_2 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6])
        return output[0]


Measured latency_ms: 0.027907