import torch
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 numpy as np
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_mean_mul_pow_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    out_ptr1, out_ptr2, out_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp3 = tl.load(in_ptr1 + x0, xmask)
    tmp4 = tl.load(in_ptr1 + (4 + x0), xmask)
    tmp6 = tl.load(in_ptr1 + (8 + x0), xmask)
    tmp8 = tl.load(in_ptr1 + (12 + x0), xmask)
    tmp15 = tl.load(in_ptr2 + x0, xmask)
    tmp1 = 0.99999
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp9 = tmp7 + tmp8
    tmp10 = 4.0
    tmp11 = tmp9 / tmp10
    tmp12 = 9.99999999995449e-06
    tmp13 = tmp11 * tmp12
    tmp14 = tmp2 + tmp13
    tmp16 = tmp15 * tmp1
    tmp17 = tmp3 * tmp3
    tmp18 = tmp4 * tmp4
    tmp19 = tmp17 + tmp18
    tmp20 = tmp6 * tmp6
    tmp21 = tmp19 + tmp20
    tmp22 = tmp8 * tmp8
    tmp23 = tmp21 + tmp22
    tmp24 = tmp23 / tmp10
    tmp25 = tmp24 * tmp12
    tmp26 = tmp16 + tmp25
    tl.store(out_ptr0 + x0, tmp14, xmask)
    tl.store(out_ptr1 + x0, tmp26, xmask)
    tl.store(out_ptr2 + x0, tmp14, xmask)
    tl.store(out_ptr3 + x0, tmp26, xmask)


@triton.jit
def triton_poi_fused_div_sub_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + 0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
    tmp12 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp4 = 0.99999
    tmp5 = tmp3 * tmp4
    tmp6 = 9.99999999995449e-06
    tmp7 = tmp5 + tmp6
    tmp8 = 1e-05
    tmp9 = triton_helpers.maximum(tmp7, tmp8)
    tmp10 = tmp1 / tmp9
    tmp11 = tmp0 - tmp10
    tmp13 = tmp12 / tmp9
    tmp14 = tmp10 * tmp10
    tmp15 = tmp13 - tmp14
    tmp16 = 0.01
    tmp17 = triton_helpers.maximum(tmp15, tmp16)
    tmp18 = libdevice.sqrt(tmp17)
    tmp19 = tmp11 / tmp18
    tl.store(out_ptr0 + x2, tmp19, xmask)


@triton.jit
def triton_poi_fused_add_mul_2(in_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr
    ):
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    tmp0 = tl.load(in_ptr0 + 0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp2 = 0.99999
    tmp3 = tmp1 * tmp2
    tmp4 = 9.99999999995449e-06
    tmp5 = tmp3 + tmp4
    tl.store(out_ptr1 + tl.full([XBLOCK], 0, tl.int32), tmp5, None)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4,), (1,))
    assert_size_stride(arg2_1, (4,), (1,))
    assert_size_stride(arg3_1, (), ())
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4,), (1,), torch.float32)
        buf1 = empty_strided_cuda((4,), (1,), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_mean_mul_pow_0[grid(4)](arg1_1, arg0_1, arg2_1,
            buf0, buf1, arg1_1, arg2_1, 4, XBLOCK=4, num_warps=1, num_stages=1)
        del arg1_1
        del arg2_1
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_div_sub_1[grid(16)](arg0_1, buf0, arg3_1, buf1,
            buf2, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del arg0_1
        del buf0
        del buf1
        triton_poi_fused_add_mul_2[grid(1)](arg3_1, arg3_1, 1, XBLOCK=1,
            num_warps=1, num_stages=1)
        del arg3_1
    return buf2,


class PopArtNew(nn.Module):
    """Normalize a vector of observations - across the first norm_axes dimensions"""

    def __init__(self, input_shape, norm_axes=1, beta=0.99999,
        per_element_update=False, epsilon=1e-05, device=torch.device('cpu')):
        super(PopArtNew, self).__init__()
        self.input_shape = input_shape
        self.norm_axes = norm_axes
        self.epsilon = epsilon
        self.beta = beta
        self.per_element_update = per_element_update
        self.tpdv = dict(dtype=torch.float32, device=device)
        self.running_mean = nn.Parameter(torch.zeros(input_shape),
            requires_grad=False)
        self.running_mean_sq = nn.Parameter(torch.zeros(input_shape),
            requires_grad=False)
        self.debiasing_term = nn.Parameter(torch.tensor(0.0), requires_grad
            =False)

    def reset_parameters(self):
        self.running_mean.zero_()
        self.running_mean_sq.zero_()
        self.debiasing_term.zero_()

    def running_mean_var(self):
        debiased_mean = self.running_mean / self.debiasing_term.clamp(min=
            self.epsilon)
        debiased_mean_sq = self.running_mean_sq / self.debiasing_term.clamp(min
            =self.epsilon)
        debiased_var = (debiased_mean_sq - debiased_mean ** 2).clamp(min=0.01)
        return debiased_mean, debiased_var

    def denormalize(self, input_vector):
        """Transform normalized data back into original distribution"""
        if type(input_vector) == np.ndarray:
            input_vector = torch.from_numpy(input_vector)
        input_vector = input_vector
        mean, var = self.running_mean_var()
        out = input_vector * torch.sqrt(var)[(None,) * self.norm_axes] + mean[
            (None,) * self.norm_axes]
        out = out.cpu().numpy()
        return out

    def forward(self, input_0):
        arg1_1 = self.running_mean
        arg2_1 = self.running_mean_sq
        arg3_1 = self.debiasing_term
        arg0_1 = input_0
        output = call([arg0_1, arg1_1, arg2_1, arg3_1])
        return output[0]


Measured latency_ms: 0.045201