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
from typing import *
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__softmax_add_log_neg_0(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp15 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp21 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = tl_math.log(tmp1)
    tmp3 = -tmp2
    tmp4 = tmp0 + tmp3
    tmp5 = 1.0
    tmp6 = tmp4 * tmp5
    tmp9 = tl_math.log(tmp8)
    tmp10 = -tmp9
    tmp11 = tmp7 + tmp10
    tmp12 = tmp11 * tmp5
    tmp13 = triton_helpers.maximum(tmp6, tmp12)
    tmp16 = tl_math.log(tmp15)
    tmp17 = -tmp16
    tmp18 = tmp14 + tmp17
    tmp19 = tmp18 * tmp5
    tmp20 = triton_helpers.maximum(tmp13, tmp19)
    tmp23 = tl_math.log(tmp22)
    tmp24 = -tmp23
    tmp25 = tmp21 + tmp24
    tmp26 = tmp25 * tmp5
    tmp27 = triton_helpers.maximum(tmp20, tmp26)
    tmp28 = tmp6 - tmp27
    tmp29 = tmp28 * tmp5
    tmp30 = tl_math.exp(tmp29)
    tmp31 = tmp12 - tmp27
    tmp32 = tmp31 * tmp5
    tmp33 = tl_math.exp(tmp32)
    tmp34 = tmp30 + tmp33
    tmp35 = tmp19 - tmp27
    tmp36 = tmp35 * tmp5
    tmp37 = tl_math.exp(tmp36)
    tmp38 = tmp34 + tmp37
    tmp39 = tmp26 - tmp27
    tmp40 = tmp39 * tmp5
    tmp41 = tl_math.exp(tmp40)
    tmp42 = tmp38 + tmp41
    tl.store(out_ptr0 + x0, tmp27, xmask)
    tl.store(out_ptr1 + x0, tmp42, xmask)


@triton.jit
def triton_poi_fused__softmax_add_log_neg_1(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_out_ptr0 + x2, xmask)
    tmp7 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tl_math.log(tmp1)
    tmp3 = -tmp2
    tmp4 = tmp0 + tmp3
    tmp5 = 1.0
    tmp6 = tmp4 * tmp5
    tmp8 = tmp6 - tmp7
    tmp9 = tmp8 * tmp5
    tmp10 = tl_math.exp(tmp9)
    tmp12 = tmp10 / tmp11
    tl.store(in_out_ptr0 + x2, tmp12, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = torch.ops.aten.exponential.default(buf0)
        del buf0
        buf2 = buf1
        del buf1
        buf3 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__softmax_add_log_neg_0[grid(64)](arg0_1, buf2,
            buf3, buf4, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf5 = buf2
        del buf2
        triton_poi_fused__softmax_add_log_neg_1[grid(256)](buf5, arg0_1,
            buf3, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del arg0_1
        del buf3
        del buf4
    return buf5,


class GumbelSoftMaxSamplerNew(nn.Module):

    def __init__(self, hard=False):
        super().__init__()
        self.hard = hard

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


Measured latency_ms: 0.039021