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 libdevice, 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_per_fused_add_div_log_sigmoid_forward_neg_sum_0(in_out_ptr0,
    in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex % 16
    r1 = rindex // 16
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
    tmp8 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
    tmp16 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
    tmp25 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
    tmp1 = 0.0
    tmp2 = triton_helpers.minimum(tmp1, tmp0)
    tmp3 = tl_math.abs(tmp0)
    tmp4 = -tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp6 = libdevice.log1p(tmp5)
    tmp7 = tmp2 - tmp6
    tmp9 = -tmp8
    tmp10 = triton_helpers.minimum(tmp1, tmp9)
    tmp11 = tl_math.abs(tmp9)
    tmp12 = -tmp11
    tmp13 = tl_math.exp(tmp12)
    tmp14 = libdevice.log1p(tmp13)
    tmp15 = tmp10 - tmp14
    tmp17 = -tmp16
    tmp18 = triton_helpers.minimum(tmp1, tmp17)
    tmp19 = tl_math.abs(tmp17)
    tmp20 = -tmp19
    tmp21 = tl_math.exp(tmp20)
    tmp22 = libdevice.log1p(tmp21)
    tmp23 = tmp18 - tmp22
    tmp24 = tmp15 + tmp23
    tmp26 = -tmp25
    tmp27 = triton_helpers.minimum(tmp1, tmp26)
    tmp28 = tl_math.abs(tmp26)
    tmp29 = -tmp28
    tmp30 = tl_math.exp(tmp29)
    tmp31 = libdevice.log1p(tmp30)
    tmp32 = tmp27 - tmp31
    tmp33 = tmp24 + tmp32
    tmp34 = 0.3333333333333333
    tmp35 = tmp33 * tmp34
    tmp36 = tmp7 + tmp35
    tmp37 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
    tmp39 = tl.sum(tmp37, 1)[:, None]
    tmp40 = -tmp39
    tmp41 = 0.25
    tmp42 = tmp40 * tmp41
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp42, None)


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)
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = buf1
        del buf1
        get_raw_stream(0)
        triton_per_fused_add_div_log_sigmoid_forward_neg_sum_0[grid(1)](buf2,
            arg0_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
    return buf2,


class NegativeSamplingNew(nn.Module):
    """Negative sampling loss as proposed by T. Mikolov et al. in Distributed
    Representations of Words and Phrases and their Compositionality.
    """

    def __init__(self):
        super(NegativeSamplingNew, self).__init__()
        self._log_sigmoid = nn.LogSigmoid()

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


Measured latency_ms: 0.014258