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 math as tl_math
from torch import nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
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__log_softmax_add_log_sigmoid_0(in_ptr0, out_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)
    tmp4 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp8 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp13 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.sigmoid(tmp0)
    tmp2 = tl_math.log(tmp1)
    tmp3 = tmp0 + tmp2
    tmp5 = tl.sigmoid(tmp4)
    tmp6 = tl_math.log(tmp5)
    tmp7 = tmp4 + tmp6
    tmp9 = tl.sigmoid(tmp8)
    tmp10 = tl_math.log(tmp9)
    tmp11 = tmp8 + tmp10
    tmp12 = triton_helpers.maximum(tmp7, tmp11)
    tmp14 = tl.sigmoid(tmp13)
    tmp15 = tl_math.log(tmp14)
    tmp16 = tmp13 + tmp15
    tmp17 = triton_helpers.maximum(tmp12, tmp16)
    tmp19 = tl.sigmoid(tmp18)
    tmp20 = tl_math.log(tmp19)
    tmp21 = tmp18 + tmp20
    tmp22 = triton_helpers.maximum(tmp17, tmp21)
    tmp23 = tmp3 - tmp22
    tl.store(out_ptr0 + x3, tmp23, xmask)


@triton.jit
def triton_poi_fused__log_softmax_1(in_ptr0, out_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)
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp9 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tl_math.exp(tmp1)
    tmp4 = tl_math.exp(tmp3)
    tmp5 = tmp2 + tmp4
    tmp7 = tl_math.exp(tmp6)
    tmp8 = tmp5 + tmp7
    tmp10 = tl_math.exp(tmp9)
    tmp11 = tmp8 + tmp10
    tmp12 = tl_math.log(tmp11)
    tmp13 = tmp0 - tmp12
    tl.store(out_ptr0 + x3, tmp13, 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)
        get_raw_stream(0)
        triton_poi_fused__log_softmax_add_log_sigmoid_0[grid(256)](arg0_1,
            buf0, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del arg0_1
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__log_softmax_1[grid(256)](buf0, buf1, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        del buf0
    return buf1,


def logsigsoftmax_v2(logits, dim=1):
    """
    v 1与 v2 差别在于 pytorch 计算softmax时有一个中心化的过程,v1 与 v2 实质上应该等同
    """
    sigmoid_logits = logits.sigmoid().log()
    sigsoftmax_logits = logits + sigmoid_logits
    return sigsoftmax_logits.log_softmax(dim=dim)


class SigSoftmaxV2New(nn.Module):

    def __init__(self, dim=1):
        super().__init__()
        self.dim = dim

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


Measured latency_ms: 0.014136