import torch
from torch._inductor.select_algorithm import extern_kernels
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 math
import torch.nn as nn
import torch.nn.functional as F
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_mul_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    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')
    tmp5 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp11 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp15 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 * tmp1
    tmp3 = 1.0
    tmp4 = tmp2 * tmp3
    tmp7 = tmp5 * tmp6
    tmp8 = tmp7 * tmp3
    tmp9 = triton_helpers.maximum(tmp4, tmp8)
    tmp12 = tmp10 * tmp11
    tmp13 = tmp12 * tmp3
    tmp14 = triton_helpers.maximum(tmp9, tmp13)
    tmp17 = tmp15 * tmp16
    tmp18 = tmp17 * tmp3
    tmp19 = triton_helpers.maximum(tmp14, tmp18)
    tmp20 = tmp4 - tmp19
    tmp21 = 0.5
    tmp22 = tmp20 * tmp21
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tmp8 - tmp19
    tmp25 = tmp24 * tmp21
    tmp26 = tl_math.exp(tmp25)
    tmp27 = tmp23 + tmp26
    tmp28 = tmp13 - tmp19
    tmp29 = tmp28 * tmp21
    tmp30 = tl_math.exp(tmp29)
    tmp31 = tmp27 + tmp30
    tmp32 = tmp18 - tmp19
    tmp33 = tmp32 * tmp21
    tmp34 = tl_math.exp(tmp33)
    tmp35 = tmp31 + tmp34
    tl.store(out_ptr0 + x0, tmp19, xmask)
    tl.store(out_ptr1 + x0, tmp35, xmask)


@triton.jit
def triton_poi_fused__softmax_mul_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    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)
    tmp5 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp3 = 1.0
    tmp4 = tmp2 * tmp3
    tmp6 = tmp4 - tmp5
    tmp7 = 0.5
    tmp8 = tmp6 * tmp7
    tmp9 = tl_math.exp(tmp8)
    tmp11 = tmp9 / tmp10
    tl.store(in_out_ptr0 + x2, tmp11, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (4, 4), (4, 1))
    assert_size_stride(primals_8, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_4, primals_1, reinterpret_tensor(
            primals_3, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf0)
        del primals_3
        del primals_4
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_6, primals_2, reinterpret_tensor(
            primals_5, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf1)
        del primals_5
        del primals_6
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf0, reinterpret_tensor(buf1, (4, 4), (1, 4), 0),
            out=buf2)
        buf3 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        buf4 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__softmax_mul_0[grid(4)](primals_7, buf2, buf3,
            buf4, 4, XBLOCK=4, num_warps=1, num_stages=1)
        buf5 = buf2
        del buf2
        triton_poi_fused__softmax_mul_1[grid(16)](buf5, primals_7, buf3,
            buf4, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(buf0, buf5, buf1, alpha=1, beta=1, out=buf6)
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf1, reinterpret_tensor(buf0, (4, 4), (1, 4), 0),
            out=buf7)
        buf8 = buf4
        del buf4
        buf9 = buf3
        del buf3
        triton_poi_fused__softmax_mul_0[grid(4)](primals_8, buf7, buf8,
            buf9, 4, XBLOCK=4, num_warps=1, num_stages=1)
        buf10 = buf7
        del buf7
        triton_poi_fused__softmax_mul_1[grid(16)](buf10, primals_8, buf8,
            buf9, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del buf8
        del buf9
        buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(buf1, buf10, buf0, alpha=1, beta=1, out=buf11)
    return (buf6, buf11, primals_1, primals_2, primals_7, primals_8, buf0,
        buf1, buf5, buf10)


class Attention(nn.Module):

    def __init__(self, opt):
        super(Attention, self).__init__()
        self.lin_u = nn.Linear(opt['hidden_dim'], opt['hidden_dim'])
        self.lin_v = nn.Linear(opt['hidden_dim'], opt['hidden_dim'])
        self.opt = opt

    def forward(self, user, item, UV_adj, VU_adj):
        user = self.lin_u(user)
        item = self.lin_v(item)
        query = user
        key = item
        value = torch.mm(query, key.transpose(0, 1))
        value = UV_adj.to_dense() * value
        value /= math.sqrt(self.opt['hidden_dim'])
        value = F.softmax(value, dim=1)
        learn_user = torch.matmul(value, key) + user
        query = item
        key = user
        value = torch.mm(query, key.transpose(0, 1))
        value = VU_adj.to_dense() * value
        value /= math.sqrt(self.opt['hidden_dim'])
        value = F.softmax(value, dim=1)
        learn_item = torch.matmul(value, key) + item
        return learn_user, learn_item


class GATNew(nn.Module):

    def __init__(self, opt):
        super(GATNew, self).__init__()
        self.att = Attention(opt)
        self.dropout = opt['dropout']
        self.leakyrelu = nn.LeakyReLU(opt['leakey'])

    def forward(self, input_0, input_1, input_2, input_3):
        primals_1 = self.att.lin_u.weight
        primals_4 = self.att.lin_u.bias
        primals_2 = self.att.lin_v.weight
        primals_6 = self.att.lin_v.bias
        primals_3 = input_0
        primals_5 = input_1
        primals_7 = input_2
        primals_8 = input_3
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8])
        return output[0], output[1]


Measured latency_ms: 0.049312