import torch
import torch.nn as nn


def taylor_softmax_v1(x, dim=1, n=4, use_log=False):
    assert n % 2 == 0 and n > 0
    fn = torch.ones_like(x)
    denor = 1.0
    for i in range(1, n + 1):
        denor *= i
        fn = fn + x.pow(i) / denor
    out = fn / fn.sum(dim=dim, keepdims=True)
    if use_log:
        out = out.log()
    return out


class LogTaylorSoftmaxV1(nn.Module):

    def __init__(self, dim=1, n=2):
        super(LogTaylorSoftmaxV1, self).__init__()
        assert n % 2 == 0
        self.dim = dim
        self.n = n

    def forward(self, x):
        return taylor_softmax_v1(x, self.dim, self.n, use_log=True)


def get_inputs():
    return [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_div_log_ones_like_pow_sum_0(in_out_ptr0, in_ptr0,
    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)
    tmp8 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp21 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp28 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp3 = tmp1 + tmp2
    tmp4 = tmp0 * tmp0
    tmp5 = 0.5
    tmp6 = tmp4 * tmp5
    tmp7 = tmp3 + tmp6
    tmp9 = tmp8 * tmp1
    tmp10 = tmp1 + tmp9
    tmp11 = tmp8 * tmp8
    tmp12 = tmp11 * tmp5
    tmp13 = tmp10 + tmp12
    tmp15 = tmp14 * tmp1
    tmp16 = tmp1 + tmp15
    tmp17 = tmp14 * tmp14
    tmp18 = tmp17 * tmp5
    tmp19 = tmp16 + tmp18
    tmp20 = tmp13 + tmp19
    tmp22 = tmp21 * tmp1
    tmp23 = tmp1 + tmp22
    tmp24 = tmp21 * tmp21
    tmp25 = tmp24 * tmp5
    tmp26 = tmp23 + tmp25
    tmp27 = tmp20 + tmp26
    tmp29 = tmp28 * tmp1
    tmp30 = tmp1 + tmp29
    tmp31 = tmp28 * tmp28
    tmp32 = tmp31 * tmp5
    tmp33 = tmp30 + tmp32
    tmp34 = tmp27 + tmp33
    tmp35 = tmp7 / tmp34
    tmp36 = tl_math.log(tmp35)
    tl.store(in_out_ptr0 + x3, tmp36, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_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, 4), (64, 16, 4, 1), torch.float32)
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_poi_fused_add_div_log_ones_like_pow_sum_0[grid(256)](buf1,
            arg0_1, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del arg0_1
    return buf1,


def taylor_softmax_v1(x, dim=1, n=4, use_log=False):
    assert n % 2 == 0 and n > 0
    fn = torch.ones_like(x)
    denor = 1.0
    for i in range(1, n + 1):
        denor *= i
        fn = fn + x.pow(i) / denor
    out = fn / fn.sum(dim=dim, keepdims=True)
    if use_log:
        out = out.log()
    return out


class LogTaylorSoftmaxV1New(nn.Module):

    def __init__(self, dim=1, n=2):
        super(LogTaylorSoftmaxV1New, self).__init__()
        assert n % 2 == 0
        self.dim = dim
        self.n = n

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