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.optim as optim
from torch import nn
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_per_fused_native_layer_norm_0(in_ptr0, out_ptr2, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [RBLOCK])
    tmp3 = tl.broadcast_to(tmp1, [RBLOCK])
    tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0))
    tmp6 = tl.full([1], 256, tl.int32)
    tmp7 = tmp6.to(tl.float32)
    tmp8 = tmp5 / tmp7
    tmp9 = tmp1 - tmp8
    tmp10 = tmp9 * tmp9
    tmp11 = tl.broadcast_to(tmp10, [RBLOCK])
    tmp13 = triton_helpers.promote_to_tensor(tl.sum(tmp11, 0))
    tmp14 = tmp0 - tmp8
    tmp15 = 256.0
    tmp16 = tmp13 / tmp15
    tmp17 = 1e-05
    tmp18 = tmp16 + tmp17
    tmp19 = libdevice.rsqrt(tmp18)
    tmp20 = tmp14 * tmp19
    tl.store(out_ptr2 + tl.broadcast_to(r0, [RBLOCK]), tmp20, None)


@triton.jit
def triton_red_fused_leaky_relu_native_layer_norm_native_layer_norm_backward_1(
    in_ptr0, in_ptr1, out_ptr0, out_ptr3, out_ptr4, xnumel, rnumel, XBLOCK:
    tl.constexpr, RBLOCK: tl.constexpr):
    rnumel = 4096
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rbase = tl.arange(0, RBLOCK)[None, :]
    tmp9_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    tmp9_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    tmp9_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r2 = rindex
        r0 = rindex % 64
        tmp0 = tl.load(in_ptr0 + r2, rmask, eviction_policy='evict_last',
            other=0.0)
        tmp1 = tl.load(in_ptr1 + r0, rmask, eviction_policy='evict_last',
            other=0.0)
        tmp2 = tmp0 + tmp1
        tmp3 = 0.0
        tmp4 = tmp2 > tmp3
        tmp5 = 0.01
        tmp6 = tmp2 * tmp5
        tmp7 = tl.where(tmp4, tmp2, tmp6)
        tmp8 = tl.broadcast_to(tmp7, [XBLOCK, RBLOCK])
        tmp9_mean_next, tmp9_m2_next, tmp9_weight_next = (triton_helpers.
            welford_reduce(tmp8, tmp9_mean, tmp9_m2, tmp9_weight, roffset == 0)
            )
        tmp9_mean = tl.where(rmask, tmp9_mean_next, tmp9_mean)
        tmp9_m2 = tl.where(rmask, tmp9_m2_next, tmp9_m2)
        tmp9_weight = tl.where(rmask, tmp9_weight_next, tmp9_weight)
        tl.store(out_ptr0 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp4, rmask)
    tmp9_tmp, tmp10_tmp, tmp11_tmp = triton_helpers.welford(tmp9_mean,
        tmp9_m2, tmp9_weight, 1)
    tmp9 = tmp9_tmp[:, None]
    tmp10 = tmp10_tmp[:, None]
    tmp11_tmp[:, None]
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r2 = rindex
        r0 = rindex % 64
        tmp12 = tl.load(out_ptr0 + r2, rmask, eviction_policy='evict_first',
            other=0.0).to(tl.int1)
        tmp13 = tl.load(in_ptr0 + r2, rmask, eviction_policy='evict_first',
            other=0.0)
        tmp14 = tl.load(in_ptr1 + r0, rmask, eviction_policy='evict_last',
            other=0.0)
        tmp15 = tmp13 + tmp14
        tmp16 = 0.01
        tmp17 = tmp15 * tmp16
        tmp18 = tl.where(tmp12, tmp15, tmp17)
        tmp19 = tmp18 - tmp9
        tmp20 = 4096.0
        tmp21 = tmp10 / tmp20
        tmp22 = 1e-05
        tmp23 = tmp21 + tmp22
        tmp24 = libdevice.rsqrt(tmp23)
        tmp25 = tmp19 * tmp24
        tl.store(out_ptr3 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp25, rmask
            )
    tmp26 = 4096.0
    tmp27 = tmp10 / tmp26
    tmp28 = 1e-05
    tmp29 = tmp27 + tmp28
    tmp30 = libdevice.rsqrt(tmp29)
    tmp31 = 0.000244140625
    tmp32 = tmp30 * tmp31
    tl.store(out_ptr4 + tl.full([XBLOCK, 1], 0, tl.int32), tmp32, None)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (64, 4), (4, 1))
    assert_size_stride(primals_3, (64,), (1,))
    assert_size_stride(primals_4, (1, 64), (64, 1))
    assert_size_stride(primals_5, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_native_layer_norm_0[grid(1)](primals_1, buf3, 1, 
            256, num_warps=2, num_stages=1)
        del primals_1
        buf4 = empty_strided_cuda((64, 64), (64, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf3, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 64), (1, 4), 0), out=buf4)
        del primals_2
        buf5 = empty_strided_cuda((4, 4, 4, 64), (1024, 256, 64, 1), torch.bool
            )
        buf9 = empty_strided_cuda((4, 4, 4, 64), (1024, 256, 64, 1), torch.
            float32)
        buf12 = empty_strided_cuda((1, 1, 1, 1), (1, 1, 1, 1), torch.float32)
        triton_red_fused_leaky_relu_native_layer_norm_native_layer_norm_backward_1[
            grid(1)](buf4, primals_3, buf5, buf9, buf12, 1, 4096, XBLOCK=1,
            RBLOCK=2048, num_warps=16, num_stages=1)
        del buf4
        del primals_3
        buf11 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_5, reinterpret_tensor(buf9, (64, 64),
            (64, 1), 0), reinterpret_tensor(primals_4, (64, 1), (1, 64), 0),
            alpha=1, beta=1, out=buf11)
        del primals_5
    return reinterpret_tensor(buf11, (4, 4, 4, 1), (16, 4, 1, 1), 0
        ), reinterpret_tensor(buf3, (64, 4), (4, 1), 0
        ), buf5, buf9, primals_4, buf12


class CriticNNNew(nn.Module):

    def __init__(self, in_channels=3):
        super(CriticNNNew, self).__init__()
        self.fc1 = nn.Linear(4, 64)
        self.fc2 = nn.Linear(64, 1)
        self.optimizer = optim.Adam(self.parameters(), lr=0.0001)
        None

    def init_weights(self, m):
        if type(m) == nn.Linear:
            None
            m.weight.data.fill_(0)
            m.bias.data.fill_(0)

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


Measured latency_ms: 0.053837