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.triton_helpers import libdevice, math as tl_math
import numpy as np
import torch.nn as nn
from numbers import Number
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_div_erf_exp_log_mul_neg_pow_rsub_sqrt_sub_0(
    in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 2 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 2 * x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + 2 * x0, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + 2 * x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = libdevice.sqrt(tmp5)
    tmp7 = tmp2 * tmp6
    tmp8 = tmp0 - tmp1
    tmp9 = tmp8 / tmp6
    tmp10 = 0.0
    tmp11 = tmp9 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = -tmp12
    tmp14 = 0.5
    tmp15 = tmp13 * tmp14
    tmp16 = 0.9189385332046727
    tmp17 = tmp15 - tmp16
    tmp18 = tl_math.exp(tmp17)
    tmp19 = tmp7 * tmp18
    tmp20 = tmp0 * tmp0
    tmp21 = tmp20 + tmp3
    tmp22 = 1.0
    tmp23 = tmp11 * tmp22
    tmp24 = 1.414213562373095
    tmp25 = tmp23 / tmp24
    tmp26 = libdevice.erf(tmp25)
    tmp27 = tmp26 + tmp22
    tmp28 = tmp27 * tmp14
    tmp29 = tmp21 * tmp28
    tmp30 = tmp19 + tmp29
    tmp31 = tmp6 * tmp18
    tmp32 = tmp8 * tmp28
    tmp33 = tmp31 + tmp32
    tmp34 = tmp33 + tmp1
    tmp35 = tmp34 * tmp34
    tmp36 = tmp1 * tmp1
    tmp37 = tmp36 + tmp4
    tmp38 = tmp22 - tmp28
    tmp39 = tmp37 * tmp38
    tmp40 = tmp30 + tmp39
    tmp41 = tmp40 - tmp35
    tl.store(in_out_ptr0 + x0, tmp41, xmask)


@triton.jit
def triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_rsub_sqrt_sub_1(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, out_ptr2, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 2
    x1 = xindex // 2
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (4 + 2 * x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr0 + (5 + 2 * x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr1 + (4 + 2 * x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr1 + (5 + 2 * x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp29 = tl.load(in_ptr0 + (2 * x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp30 = tl.load(in_ptr0 + (1 + 2 * x0 + 8 * x1), xmask, eviction_policy
        ='evict_last')
    tmp33 = tl.load(in_ptr1 + (2 * x0 + 8 * x1), xmask, eviction_policy=
        'evict_last')
    tmp34 = tl.load(in_ptr1 + (1 + 2 * x0 + 8 * x1), xmask, eviction_policy
        ='evict_last')
    tmp54 = tl.load(in_ptr2 + (x0 + 4 * x1), xmask)
    tmp55 = tl.load(in_ptr2 + (2 + x0 + 4 * x1), xmask)
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.sqrt(tmp2)
    tmp6 = tmp4 - tmp5
    tmp7 = tmp6 / tmp3
    tmp8 = 0.0
    tmp9 = tmp7 - tmp8
    tmp10 = tmp9 * tmp9
    tmp11 = -tmp10
    tmp12 = 0.5
    tmp13 = tmp11 * tmp12
    tmp14 = 0.9189385332046727
    tmp15 = tmp13 - tmp14
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp3 * tmp16
    tmp18 = 1.0
    tmp19 = tmp9 * tmp18
    tmp20 = 1.414213562373095
    tmp21 = tmp19 / tmp20
    tmp22 = libdevice.erf(tmp21)
    tmp23 = tmp22 + tmp18
    tmp24 = tmp23 * tmp12
    tmp25 = tmp6 * tmp24
    tmp26 = tmp17 + tmp25
    tmp27 = tmp26 + tmp5
    tmp28 = tmp27 * tmp27
    tmp31 = tmp29 + tmp30
    tmp32 = libdevice.sqrt(tmp31)
    tmp35 = tmp33 - tmp34
    tmp36 = tmp35 / tmp32
    tmp37 = tmp36 - tmp8
    tmp38 = tmp37 * tmp37
    tmp39 = -tmp38
    tmp40 = tmp39 * tmp12
    tmp41 = tmp40 - tmp14
    tmp42 = tl_math.exp(tmp41)
    tmp43 = tmp32 * tmp42
    tmp44 = tmp37 * tmp18
    tmp45 = tmp44 / tmp20
    tmp46 = libdevice.erf(tmp45)
    tmp47 = tmp46 + tmp18
    tmp48 = tmp47 * tmp12
    tmp49 = tmp35 * tmp48
    tmp50 = tmp43 + tmp49
    tmp51 = tmp50 + tmp34
    tmp52 = tmp51 + tmp27
    tmp53 = tmp51 - tmp27
    tmp56 = tmp54 + tmp55
    tmp57 = libdevice.sqrt(tmp56)
    tmp58 = tmp53 / tmp57
    tmp59 = tmp58 - tmp8
    tmp60 = tmp59 * tmp59
    tmp61 = -tmp60
    tmp62 = tmp61 * tmp12
    tmp63 = tmp62 - tmp14
    tmp64 = tl_math.exp(tmp63)
    tmp65 = tmp57 * tmp64
    tmp66 = tmp59 * tmp18
    tmp67 = tmp66 / tmp20
    tmp68 = libdevice.erf(tmp67)
    tmp69 = tmp68 + tmp18
    tmp70 = tmp69 * tmp12
    tmp71 = tmp53 * tmp70
    tmp72 = tmp65 + tmp71
    tmp73 = tmp72 + tmp27
    tmp74 = tmp51 * tmp51
    tmp75 = tmp52 * tmp57
    tmp76 = tmp75 * tmp64
    tmp77 = tmp74 + tmp54
    tmp78 = tmp77 * tmp70
    tmp79 = tmp76 + tmp78
    tmp80 = tmp28 + tmp55
    tmp81 = tmp18 - tmp70
    tmp82 = tmp80 * tmp81
    tmp83 = tmp79 + tmp82
    tmp84 = tmp73 * tmp73
    tmp85 = tmp83 - tmp84
    tl.store(out_ptr2 + x2, tmp73, xmask)
    tl.store(in_out_ptr0 + x2, tmp85, xmask)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((4, 4, 4, 2), (32, 8, 2, 1), torch.float32)
        buf3 = buf1
        del buf1
        get_raw_stream(0)
        triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_rsub_sqrt_sub_0[grid
            (128)](buf3, arg0_1, arg1_1, 128, XBLOCK=128, num_warps=4,
            num_stages=1)
        buf0 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
        buf8 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
        buf6 = buf0
        del buf0
        buf9 = buf6
        del buf6
        triton_poi_fused_add_div_erf_exp_log_mul_neg_pow_rsub_sqrt_sub_1[grid
            (64)](buf9, arg1_1, arg0_1, buf3, buf8, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del arg0_1
        del arg1_1
        del buf3
    return buf8, buf9


def keep_variance_fn(x):
    return x + 0.001


def normcdf(value, mu=0.0, stddev=1.0):
    sinv = 1.0 / stddev if isinstance(stddev, Number) else stddev.reciprocal()
    return 0.5 * (1.0 + torch.erf((value - mu) * sinv / np.sqrt(2.0)))


def _normal_log_pdf(value, mu, stddev):
    var = stddev ** 2
    log_scale = np.log(stddev) if isinstance(stddev, Number) else torch.log(
        stddev)
    return -(value - mu) ** 2 / (2.0 * var) - log_scale - np.log(np.sqrt(
        2.0 * np.pi))


def normpdf(value, mu=0.0, stddev=1.0):
    return torch.exp(_normal_log_pdf(value, mu, stddev))


class MaxPool2dNew(nn.Module):

    def __init__(self, keep_variance_fn=None):
        super(MaxPool2dNew, self).__init__()
        self._keep_variance_fn = keep_variance_fn

    def _max_pool_internal(self, mu_a, mu_b, var_a, var_b):
        stddev = torch.sqrt(var_a + var_b)
        ab = mu_a - mu_b
        alpha = ab / stddev
        pdf = normpdf(alpha)
        cdf = normcdf(alpha)
        z_mu = stddev * pdf + ab * cdf + mu_b
        z_var = (mu_a + mu_b) * stddev * pdf + (mu_a ** 2 + var_a) * cdf + (
            mu_b ** 2 + var_b) * (1.0 - cdf) - z_mu ** 2
        if self._keep_variance_fn is not None:
            z_var = self._keep_variance_fn(z_var)
        return z_mu, z_var

    def _max_pool_1x2(self, inputs_mean, inputs_variance):
        mu_a = inputs_mean[:, :, :, 0::2]
        mu_b = inputs_mean[:, :, :, 1::2]
        var_a = inputs_variance[:, :, :, 0::2]
        var_b = inputs_variance[:, :, :, 1::2]
        outputs_mean, outputs_variance = self._max_pool_internal(mu_a, mu_b,
            var_a, var_b)
        return outputs_mean, outputs_variance

    def _max_pool_2x1(self, inputs_mean, inputs_variance):
        mu_a = inputs_mean[:, :, 0::2, :]
        mu_b = inputs_mean[:, :, 1::2, :]
        var_a = inputs_variance[:, :, 0::2, :]
        var_b = inputs_variance[:, :, 1::2, :]
        outputs_mean, outputs_variance = self._max_pool_internal(mu_a, mu_b,
            var_a, var_b)
        return outputs_mean, outputs_variance

    def forward(self, input_0, input_1):
        arg0_1 = input_0
        arg1_1 = input_1
        output = call([arg0_1, arg1_1])
        return output[0], output[1]


Measured latency_ms: 0.026929