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_index_linalg_vector_norm_log_sigmoid_forward_mul_neg_pow_sub_sum_0(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp7 = tl.load(in_ptr2 + r0, None)
    tmp26 = tl.load(in_ptr4 + r0, None)
    tmp1 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 4),
        'index out of bounds: 0 <= tmp4 < 4')
    tmp6 = tl.load(in_ptr1 + 4 * tmp4, None, eviction_policy='evict_last')
    tmp8 = tmp7 + tmp1
    tmp9 = tmp7 < 0
    tmp10 = tl.where(tmp9, tmp8, tmp7)
    tl.device_assert((0 <= tmp10) & (tmp10 < 4),
        'index out of bounds: 0 <= tmp10 < 4')
    tmp12 = tl.load(in_ptr3 + 4 * tmp10, None, eviction_policy='evict_last')
    tmp13 = tmp6 * tmp12
    tmp14 = tl.load(in_ptr1 + (1 + 4 * tmp4), None, eviction_policy=
        'evict_last')
    tmp15 = tl.load(in_ptr3 + (1 + 4 * tmp10), None, eviction_policy=
        'evict_last')
    tmp16 = tmp14 * tmp15
    tmp17 = tmp13 + tmp16
    tmp18 = tl.load(in_ptr1 + (2 + 4 * tmp4), None, eviction_policy=
        'evict_last')
    tmp19 = tl.load(in_ptr3 + (2 + 4 * tmp10), None, eviction_policy=
        'evict_last')
    tmp20 = tmp18 * tmp19
    tmp21 = tmp17 + tmp20
    tmp22 = tl.load(in_ptr1 + (3 + 4 * tmp4), None, eviction_policy=
        'evict_last')
    tmp23 = tl.load(in_ptr3 + (3 + 4 * tmp10), None, eviction_policy=
        'evict_last')
    tmp24 = tmp22 * tmp23
    tmp25 = tmp21 + tmp24
    tmp27 = tmp26 + tmp1
    tmp28 = tmp26 < 0
    tmp29 = tl.where(tmp28, tmp27, tmp26)
    tl.device_assert((0 <= tmp29) & (tmp29 < 4),
        'index out of bounds: 0 <= tmp29 < 4')
    tmp31 = tl.load(in_ptr3 + 4 * tmp29, None, eviction_policy='evict_last')
    tmp32 = tmp6 * tmp31
    tmp33 = tl.load(in_ptr3 + (1 + 4 * tmp29), None, eviction_policy=
        'evict_last')
    tmp34 = tmp14 * tmp33
    tmp35 = tmp32 + tmp34
    tmp36 = tl.load(in_ptr3 + (2 + 4 * tmp29), None, eviction_policy=
        'evict_last')
    tmp37 = tmp18 * tmp36
    tmp38 = tmp35 + tmp37
    tmp39 = tl.load(in_ptr3 + (3 + 4 * tmp29), None, eviction_policy=
        'evict_last')
    tmp40 = tmp22 * tmp39
    tmp41 = tmp38 + tmp40
    tmp42 = tmp6 * tmp6
    tmp43 = tmp14 * tmp14
    tmp44 = tmp42 + tmp43
    tmp45 = tmp18 * tmp18
    tmp46 = tmp44 + tmp45
    tmp47 = tmp22 * tmp22
    tmp48 = tmp46 + tmp47
    tmp49 = libdevice.sqrt(tmp48)
    tmp50 = tmp49 * tmp49
    tmp51 = tl.broadcast_to(tmp50, [XBLOCK, RBLOCK])
    tmp53 = tl.sum(tmp51, 1)[:, None]
    tmp54 = tmp12 * tmp12
    tmp55 = tmp15 * tmp15
    tmp56 = tmp54 + tmp55
    tmp57 = tmp19 * tmp19
    tmp58 = tmp56 + tmp57
    tmp59 = tmp23 * tmp23
    tmp60 = tmp58 + tmp59
    tmp61 = libdevice.sqrt(tmp60)
    tmp62 = tmp61 * tmp61
    tmp63 = tl.broadcast_to(tmp62, [XBLOCK, RBLOCK])
    tmp65 = tl.sum(tmp63, 1)[:, None]
    tmp66 = tmp31 * tmp31
    tmp67 = tmp33 * tmp33
    tmp68 = tmp66 + tmp67
    tmp69 = tmp36 * tmp36
    tmp70 = tmp68 + tmp69
    tmp71 = tmp39 * tmp39
    tmp72 = tmp70 + tmp71
    tmp73 = libdevice.sqrt(tmp72)
    tmp74 = tmp73 * tmp73
    tmp75 = tl.broadcast_to(tmp74, [XBLOCK, RBLOCK])
    tmp77 = tl.sum(tmp75, 1)[:, None]
    tmp78 = tmp25 - tmp41
    tmp79 = 0.0
    tmp80 = triton_helpers.minimum(tmp79, tmp78)
    tmp81 = tl_math.abs(tmp78)
    tmp82 = -tmp81
    tmp83 = tl_math.exp(tmp82)
    tmp84 = libdevice.log1p(tmp83)
    tmp85 = tmp80 - tmp84
    tmp86 = tl.broadcast_to(tmp85, [XBLOCK, RBLOCK])
    tmp88 = tl.sum(tmp86, 1)[:, None]
    tmp89 = -tmp88
    tmp90 = tmp53 + tmp65
    tmp91 = tmp90 + tmp77
    tmp92 = 4.0
    tmp93 = tmp91 * tmp92
    tmp94 = tmp89 + tmp93
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp94, None)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf6 = buf2
        del buf2
        get_raw_stream(0)
        triton_per_fused_add_index_linalg_vector_norm_log_sigmoid_forward_mul_neg_pow_sub_sum_0[
            grid(1)](buf6, primals_2, primals_1, primals_4, primals_3,
            primals_5, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
    return buf6, primals_1, primals_2, primals_3, primals_4, primals_5


class BPRNew(nn.Module):

    def __init__(self, user_size, item_size, dim, weight_decay):
        super().__init__()
        self.W = nn.Parameter(torch.empty(user_size, dim))
        self.H = nn.Parameter(torch.empty(item_size, dim))
        nn.init.xavier_normal_(self.W.data)
        nn.init.xavier_normal_(self.H.data)
        self.weight_decay = weight_decay

    def recommend(self, u):
        """Return recommended item list given users.

        Args:
            u(torch.LongTensor): tensor stored user indexes. [batch_size,]

        Returns:
            pred(torch.LongTensor): recommended item list sorted by preference. [batch_size, item_size]
        """
        u = self.W[u, :]
        x_ui = torch.mm(u, self.H.t())
        pred = torch.argsort(x_ui, dim=1)
        return pred

    def forward(self, input_0, input_1, input_2):
        primals_1 = self.W
        primals_3 = self.H
        primals_2 = input_0
        primals_4 = input_1
        primals_5 = input_2
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0]


Measured latency_ms: 0.01763