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
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    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)
    tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3 * tmp1
    tmp6 = tmp5 * tmp1
    tmp7 = triton_helpers.maximum(tmp4, tmp6)
    tmp9 = tmp8 * tmp1
    tmp10 = triton_helpers.maximum(tmp7, tmp9)
    tmp12 = tmp11 * tmp1
    tmp13 = triton_helpers.maximum(tmp10, tmp12)
    tmp14 = tmp2 - tmp13
    tmp15 = tmp14 * tmp1
    tmp16 = tl_math.exp(tmp15)
    tl.store(out_ptr0 + x2, tmp16, xmask)


@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    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_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_stack_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + x0, tmp4 & xmask, eviction_policy='evict_last',
        other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr1 + (-4 + x0), tmp9 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp11 = tmp0 >= tmp7
    tmp12 = tl.full([1], 12, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tl.load(in_ptr2 + (-8 + x0), tmp14 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp16 = tmp0 >= tmp12
    tl.full([1], 16, tl.int64)
    tmp19 = tl.load(in_ptr3 + (-12 + x0), tmp16 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp20 = tl.where(tmp14, tmp15, tmp19)
    tmp21 = tl.where(tmp9, tmp10, tmp20)
    tmp22 = tl.where(tmp4, tmp5, tmp21)
    tl.store(out_ptr0 + x0, tmp22, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4), (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__softmax_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del arg0_1
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_1[grid(64)](buf0, buf1, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf0
        buf2 = torch.ops.aten.multinomial.default(reinterpret_tensor(buf1,
            (4, 4), (4, 1), 0), 1)
        buf3 = buf2
        del buf2
        buf4 = torch.ops.aten.multinomial.default(reinterpret_tensor(buf1,
            (4, 4), (4, 1), 16), 1)
        buf5 = buf4
        del buf4
        buf6 = torch.ops.aten.multinomial.default(reinterpret_tensor(buf1,
            (4, 4), (4, 1), 32), 1)
        buf7 = buf6
        del buf6
        buf8 = torch.ops.aten.multinomial.default(reinterpret_tensor(buf1,
            (4, 4), (4, 1), 48), 1)
        del buf1
        buf9 = buf8
        del buf8
        buf10 = empty_strided_cuda((16, 1), (1, 1), torch.int64)
        triton_poi_fused_stack_2[grid(16)](buf3, buf5, buf7, buf9, buf10, 
            16, XBLOCK=16, num_warps=1, num_stages=1)
        del buf3
        del buf5
        del buf7
        del buf9
    return reinterpret_tensor(buf10, (4, 4), (4, 1), 0),


def cuda():
    return torch.cuda.is_available()


def get_device():
    return torch.device('cuda' if cuda() else 'cpu')


class Search(nn.Module):
    """Base search class."""

    def __init__(self, *args, **kwargs):
        super().__init__()
        self.device = get_device()

    def forward(self, logits: 'torch.Tensor') ->object:
        """
        Error handling.

        Args:
            logits: torch.Tensor (Tensor): the model's
                logits. (batch_size, length, vocabulary_size)
        Returns:
            object: the search output.
        """
        if not len(logits.shape) == 3:
            raise ValueError(
                f'Logits need to be 3D Tensor, was: {logits.shape}')
        if not type(logits) == torch.Tensor:
            raise TypeError(
                f'Logits need to be torch.Tensor, was: {type(logits)}')

    def step(self, logits: 'torch.Tensor') ->object:
        """
        Error handling.

        Args:
            logits: torch.Tensor (Tensor): the model's
                logits. (batch_size, vocabulary_size)
        Returns:
            object: the search output.
        """
        if len(logits.shape) > 3:
            raise ValueError(
                f'Logits need to be 2D or 3D Tensor, was: {logits.shape}')
        if not type(logits) == torch.Tensor:
            raise TypeError(
                f'Logits need to be torch.Tensor, was: {type(logits)}')


class SamplingSearchNew(Search):
    """"Sampling search."""

    def __init__(self, temperature: 'float'=1.0, *args, **kwargs):
        """
        Initialize the sampling search.

        Args:
            temperature (float, optional): temperature parameter. Defaults to
                1.0, a.k.a., no temperature. Temperature < 1 results in a more
                descriminative softmax, > 1 in a flatter distribution.
        """
        super().__init__(*args, **kwargs)
        self.temperature = temperature

    def step(self, logits: 'torch.Tensor') ->torch.Tensor:
        """
        Perform a sampling search step.

        Args:
            logits (torch.Tensor): the model's
                logits. (batch_size, vocabulary_size)
        Returns:
            torch.Tensor: the token indexes for all the batch. (batch_size, 1).
        """
        super().step(logits)
        probabilities = torch.softmax(logits.div(self.temperature), 1)
        return torch.stack([torch.multinomial(probability, 1) for
            probability in probabilities])

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


Measured latency_ms: 0.014028