import torch
from torch import device
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
import torch.multiprocessing
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_helper_fn_add0(arg0_0, arg1_0):
    tmp0 = arg0_0 + arg1_0
    return tmp0


@triton.jit
def triton_per_fused_cumsum_sort_sub_0(in_ptr0, out_ptr0, out_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    RBLOCK: tl.constexpr = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 4 * x0), xmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = r1
    tmp10 = tmp9.to(tl.int16)
    tmp11 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp12 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK])
    tmp13, _tmp14 = triton_helpers.sort_with_index(tmp11, tmp12, None, 1,
        stable=False, descending=True)
    tmp15 = tmp13.to(tl.float32)
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp17, = tl.associative_scan((tmp16,), 1, _triton_helper_fn_add0)
    tl.store(out_ptr0 + (r1 + 4 * x0), tmp8, xmask)
    tl.store(out_ptr1 + (r1 + 4 * x0), tmp13, xmask)
    tl.store(out_ptr2 + (r1 + 4 * x0), tmp17, xmask)


@triton.jit
def triton_poi_fused__to_copy_add_gt_max_mul_sum_1(in_ptr0, in_ptr1,
    in_ptr2, 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 + 0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp2 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr2 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr0 + 1)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK])
    tmp12 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp15 = tl.load(in_ptr2 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp20 = tl.load(in_ptr0 + 2)
    tmp21 = tl.broadcast_to(tmp20, [XBLOCK])
    tmp22 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp25 = tl.load(in_ptr2 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp30 = tl.load(in_ptr0 + 3)
    tmp31 = tl.broadcast_to(tmp30, [XBLOCK])
    tmp32 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp35 = tl.load(in_ptr2 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp3 = tmp1 * tmp2
    tmp4 = 1.0
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 > tmp6
    tmp8 = tmp7.to(tl.float32)
    tmp9 = tmp8 * tmp1
    tmp13 = tmp11 * tmp12
    tmp14 = tmp13 + tmp4
    tmp16 = tmp14 > tmp15
    tmp17 = tmp16.to(tl.float32)
    tmp18 = tmp17 * tmp11
    tmp19 = triton_helpers.maximum(tmp9, tmp18)
    tmp23 = tmp21 * tmp22
    tmp24 = tmp23 + tmp4
    tmp26 = tmp24 > tmp25
    tmp27 = tmp26.to(tl.float32)
    tmp28 = tmp27 * tmp21
    tmp29 = triton_helpers.maximum(tmp19, tmp28)
    tmp33 = tmp31 * tmp32
    tmp34 = tmp33 + tmp4
    tmp36 = tmp34 > tmp35
    tmp37 = tmp36.to(tl.float32)
    tmp38 = tmp37 * tmp31
    tmp39 = triton_helpers.maximum(tmp29, tmp38)
    tmp40 = tmp8 * tmp2
    tmp41 = tmp17 * tmp12
    tmp42 = tmp40 + tmp41
    tmp43 = tmp27 * tmp22
    tmp44 = tmp42 + tmp43
    tmp45 = tmp37 * tmp32
    tmp46 = tmp44 + tmp45
    tl.store(out_ptr0 + x0, tmp39, xmask)
    tl.store(out_ptr1 + x0, tmp46, xmask)


@triton.jit
def triton_poi_fused_maximum_sub_zeros_like_2(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, 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_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp2 = 1.0
    tmp3 = tmp1 - tmp2
    tmp5 = tmp3 / tmp4
    tmp6 = tmp0 - tmp5
    tmp7 = 0.0
    tmp8 = triton_helpers.maximum(tmp7, tmp6)
    tl.store(out_ptr0 + x2, tmp8, 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((64, 4), (4, 1), torch.float32)
        buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_cumsum_sort_sub_0[grid(64)](arg0_1, buf0, buf1,
            buf5, 64, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        buf3 = torch.ops.aten.range.step(1, 4, dtype=torch.float32, layout=
            torch.strided, device=device(type='cuda', index=0))
        buf4 = buf3
        del buf3
        buf6 = empty_strided_cuda((64, 1), (1, 64), torch.float32)
        buf7 = empty_strided_cuda((64, 1), (1, 64), torch.float32)
        triton_poi_fused__to_copy_add_gt_max_mul_sum_1[grid(64)](buf4, buf1,
            buf5, buf6, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf1
        del buf4
        buf8 = buf5
        del buf5
        triton_poi_fused_maximum_sub_zeros_like_2[grid(256)](buf0, buf7,
            buf6, buf8, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del buf0
        del buf6
        del buf7
    return reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0), buf8


class SparsemaxNew(nn.Module):
    """Sparsemax function."""

    def __init__(self, dim=None):
        """Initialize sparsemax activation
        
        Args:
            dim (int, optional): The dimension over which to apply the sparsemax function.
        """
        super(SparsemaxNew, self).__init__()
        self.dim = -1 if dim is None else dim

    def backward(self, grad_output):
        """Backward function."""
        dim = 1
        nonzeros = torch.ne(self.output, 0)
        sum = torch.sum(grad_output * nonzeros, dim=dim) / torch.sum(nonzeros,
            dim=dim)
        self.grad_input = nonzeros * (grad_output - sum.expand_as(grad_output))
        return self.grad_input

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


Measured latency_ms: 0.057277