import torch
import torch.nn as nn


def keep_variance_fn(x):
    return x + 0.001


class Softmax(nn.Module):

    def __init__(self, dim=1, keep_variance_fn=None):
        super(Softmax, self).__init__()
        self.dim = dim
        self._keep_variance_fn = keep_variance_fn

    def forward(self, features_mean, features_variance, eps=1e-05):
        """Softmax function applied to a multivariate Gaussian distribution.
        It works under the assumption that features_mean and features_variance
        are the parameters of a the indepent gaussians that contribute to the
        multivariate gaussian.
        Mean and variance of the log-normal distribution are computed following
        https://en.wikipedia.org/wiki/Log-normal_distribution."""
        log_gaussian_mean = features_mean + 0.5 * features_variance
        log_gaussian_variance = 2 * log_gaussian_mean
        log_gaussian_mean = torch.exp(log_gaussian_mean)
        log_gaussian_variance = torch.exp(log_gaussian_variance)
        log_gaussian_variance = log_gaussian_variance * (torch.exp(
            features_variance) - 1)
        constant = torch.sum(log_gaussian_mean, dim=self.dim) + eps
        constant = constant.unsqueeze(self.dim)
        outputs_mean = log_gaussian_mean / constant
        outputs_variance = log_gaussian_variance / constant ** 2
        if self._keep_variance_fn is not None:
            outputs_variance = self._keep_variance_fn(outputs_variance)
        return outputs_mean, outputs_variance


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {}]

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 math as tl_math
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_exp_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x1 = xindex // 16
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask)
    tmp6 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
    tmp7 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask)
    tmp12 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
    tmp13 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask)
    tmp18 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
    tmp19 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask)
    tmp2 = 0.5
    tmp3 = tmp1 * tmp2
    tmp4 = tmp0 + tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp8 = tmp7 * tmp2
    tmp9 = tmp6 + tmp8
    tmp10 = tl_math.exp(tmp9)
    tmp11 = tmp5 + tmp10
    tmp14 = tmp13 * tmp2
    tmp15 = tmp12 + tmp14
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp11 + tmp16
    tmp20 = tmp19 * tmp2
    tmp21 = tmp18 + tmp20
    tmp22 = tl_math.exp(tmp21)
    tmp23 = tmp17 + tmp22
    tmp24 = 1e-05
    tmp25 = tmp23 + tmp24
    tl.store(out_ptr0 + x2, tmp25, xmask)


@triton.jit
def triton_poi_fused_add_div_exp_mul_pow_sub_1(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x0 = xindex % 16
    x2 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr1 + x3, xmask)
    tmp6 = tl.load(in_ptr2 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = 0.5
    tmp3 = tmp1 * tmp2
    tmp4 = tmp0 + tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp7 = tmp5 / tmp6
    tmp8 = 2.0
    tmp9 = tmp4 * tmp8
    tmp10 = tl_math.exp(tmp9)
    tmp11 = tl_math.exp(tmp1)
    tmp12 = 1.0
    tmp13 = tmp11 - tmp12
    tmp14 = tmp10 * tmp13
    tmp15 = tmp6 * tmp6
    tmp16 = tmp14 / tmp15
    tl.store(out_ptr0 + x3, tmp7, xmask)
    tl.store(out_ptr1 + x3, tmp16, 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)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_exp_mul_sum_0[grid(64)](arg1_1, arg0_1, buf0, 
            64, XBLOCK=64, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_div_exp_mul_pow_sub_1[grid(256)](arg1_1,
            arg0_1, buf0, buf1, buf2, 256, XBLOCK=128, num_warps=4,
            num_stages=1)
        del arg0_1
        del arg1_1
        del buf0
    return buf1, buf2


def keep_variance_fn(x):
    return x + 0.001


class SoftmaxNew(nn.Module):

    def __init__(self, dim=1, keep_variance_fn=None):
        super(SoftmaxNew, self).__init__()
        self.dim = dim
        self._keep_variance_fn = keep_variance_fn

    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]
