import torch
import torch.nn as nn


class OuterProductLayer(nn.Module):
    """OutterProduct Layer used in PNN. This implemention is
    adapted from code that the author of the paper published on https://github.com/Atomu2014/product-nets.
    """

    def __init__(self, num_feature_field, embedding_size, device):
        """
        Args:
            num_feature_field(int) :number of feature fields.
            embedding_size(int) :number of embedding size.
            device(torch.device) : device object of the model.
        """
        super(OuterProductLayer, self).__init__()
        self.num_feature_field = num_feature_field
        num_pairs = int(num_feature_field * (num_feature_field - 1) / 2)
        embed_size = embedding_size
        self.kernel = nn.Parameter(torch.rand(embed_size, num_pairs,
            embed_size), requires_grad=True)
        nn.init.xavier_uniform_(self.kernel)
        self

    def forward(self, feat_emb):
        """
        Args:
            feat_emb(torch.FloatTensor) :3D tensor with shape: [batch_size,num_pairs,embedding_size].

        Returns:
            outer_product(torch.FloatTensor): The outer product of input tensor. shape of [batch_size, num_pairs]
        """
        row = []
        col = []
        for i in range(self.num_feature_field - 1):
            for j in range(i + 1, self.num_feature_field):
                row.append(i)
                col.append(j)
        p = feat_emb[:, row]
        q = feat_emb[:, col]
        p.unsqueeze_(dim=1)
        p = torch.mul(p, self.kernel.unsqueeze(0))
        p = torch.sum(p, dim=-1)
        p = torch.transpose(p, 2, 1)
        outer_product = p * q
        return outer_product.sum(dim=-1)


def get_inputs():
    return [torch.rand([4, 4, 4])]


def get_init_inputs():
    return [[], {'num_feature_field': 4, 'embedding_size': 4, 'device': 0}]

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
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_poi_fused_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 96
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 6
    x2 = xindex // 24
    x3 = xindex % 24
    x4 = xindex
    tmp18 = tl.load(in_ptr1 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp21 = tl.load(in_ptr1 + (1 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp25 = tl.load(in_ptr1 + (2 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp29 = tl.load(in_ptr1 + (3 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp0 = x0
    tmp1 = tl.full([1], 3, tl.int64)
    tmp2 = tmp0 < tmp1
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.full([1], 2, tl.int64)
    tmp6 = tmp0 < tmp5
    tmp7 = tl.full([1], 0, tl.int64)
    tmp8 = tl.where(tmp6, tmp7, tmp7)
    tmp9 = tl.where(tmp4, tmp7, tmp8)
    tmp10 = tl.full([1], 4, tl.int64)
    tmp11 = tmp0 < tmp10
    tmp12 = tl.full([1], 5, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tl.where(tmp13, tmp3, tmp5)
    tmp15 = tl.where(tmp11, tmp3, tmp14)
    tmp16 = tl.where(tmp2, tmp9, tmp15)
    tmp17 = tl.load(in_ptr0 + (4 * tmp16 + 16 * x2), xmask, eviction_policy
        ='evict_last')
    tmp19 = tmp17 * tmp18
    tmp20 = tl.load(in_ptr0 + (1 + 4 * tmp16 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp22 = tmp20 * tmp21
    tmp23 = tmp19 + tmp22
    tmp24 = tl.load(in_ptr0 + (2 + 4 * tmp16 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp26 = tmp24 * tmp25
    tmp27 = tmp23 + tmp26
    tmp28 = tl.load(in_ptr0 + (3 + 4 * tmp16 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp30 = tmp28 * tmp29
    tmp31 = tmp27 + tmp30
    tl.store(out_ptr0 + x4, tmp31, xmask)


@triton.jit
def triton_poi_fused_index_mul_sum_1(in_ptr0, in_ptr1, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 24
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 6
    x1 = xindex // 6
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 24 * x1), xmask)
    tmp19 = tl.load(in_ptr0 + (6 + x0 + 24 * x1), xmask)
    tmp23 = tl.load(in_ptr0 + (12 + x0 + 24 * x1), xmask)
    tmp27 = tl.load(in_ptr0 + (18 + x0 + 24 * x1), xmask)
    tmp1 = x0
    tmp2 = tl.full([1], 3, tl.int64)
    tmp3 = tmp1 < tmp2
    tmp4 = tl.full([1], 1, tl.int64)
    tmp5 = tmp1 < tmp4
    tmp6 = tl.full([1], 2, tl.int64)
    tmp7 = tmp1 < tmp6
    tmp8 = tl.where(tmp7, tmp6, tmp2)
    tmp9 = tl.where(tmp5, tmp4, tmp8)
    tmp10 = tl.full([1], 4, tl.int64)
    tmp11 = tmp1 < tmp10
    tmp12 = tl.full([1], 5, tl.int64)
    tmp13 = tmp1 < tmp12
    tmp14 = tl.where(tmp13, tmp2, tmp2)
    tmp15 = tl.where(tmp11, tmp6, tmp14)
    tmp16 = tl.where(tmp3, tmp9, tmp15)
    tmp17 = tl.load(in_ptr1 + (4 * tmp16 + 16 * x1), xmask, eviction_policy
        ='evict_last')
    tmp18 = tmp0 * tmp17
    tmp20 = tl.load(in_ptr1 + (1 + 4 * tmp16 + 16 * x1), xmask,
        eviction_policy='evict_last')
    tmp21 = tmp19 * tmp20
    tmp22 = tmp18 + tmp21
    tmp24 = tl.load(in_ptr1 + (2 + 4 * tmp16 + 16 * x1), xmask,
        eviction_policy='evict_last')
    tmp25 = tmp23 * tmp24
    tmp26 = tmp22 + tmp25
    tmp28 = tl.load(in_ptr1 + (3 + 4 * tmp16 + 16 * x1), xmask,
        eviction_policy='evict_last')
    tmp29 = tmp27 * tmp28
    tmp30 = tmp26 + tmp29
    tl.store(out_ptr0 + x2, tmp30, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (4, 6, 4), (24, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 6), (24, 6, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_sum_0[grid(96)](primals_1, primals_2, buf0, 96,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_2
        buf1 = empty_strided_cuda((4, 6), (6, 1), torch.float32)
        triton_poi_fused_index_mul_sum_1[grid(24)](buf0, primals_1, buf1, 
            24, XBLOCK=32, num_warps=1, num_stages=1)
        del buf0
    return buf1, primals_1


class OuterProductLayerNew(nn.Module):
    """OutterProduct Layer used in PNN. This implemention is
    adapted from code that the author of the paper published on https://github.com/Atomu2014/product-nets.
    """

    def __init__(self, num_feature_field, embedding_size, device):
        """
        Args:
            num_feature_field(int) :number of feature fields.
            embedding_size(int) :number of embedding size.
            device(torch.device) : device object of the model.
        """
        super(OuterProductLayerNew, self).__init__()
        self.num_feature_field = num_feature_field
        num_pairs = int(num_feature_field * (num_feature_field - 1) / 2)
        embed_size = embedding_size
        self.kernel = nn.Parameter(torch.rand(embed_size, num_pairs,
            embed_size), requires_grad=True)
        nn.init.xavier_uniform_(self.kernel)
        self

    def forward(self, input_0):
        primals_2 = self.kernel
        primals_1 = input_0
        output = call([primals_1, primals_2])
        return output[0]
