# AOT ID: ['0_forward']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_1/inductor_cache/ys/cysmpoovu7ymuvaaoio356tm6mogxnbmw2uz7mmr5af7irqs7m4j.py
# Topologically Sorted Source Nodes: [p_1, p_2], Original ATen: [aten.mul, aten.sum]
# Source node to ATen node mapping:
#   p_1 => mul
#   p_2 => sum_1
# Graph fragment:
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%unsqueeze, %unsqueeze_1), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul, [-1]), kwargs = {})
triton_poi_fused_mul_sum_0 = async_compile.triton('triton_poi_fused_mul_sum_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[128], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=90, major=9, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=132), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_mul_sum_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 0, 'backend_hash': 'B098E03CDA7B8ADC90DAFFDF24A2956451D1B13F297756A5DCC209498AA53705', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


# kernel path: runs/run_shard_1/inductor_cache/b3/cb3pluhky34w5fgwnwjqa7ntoyhfwi3hev346jlv6swkx5ozygp6.py
# Topologically Sorted Source Nodes: [q, outer_product, sum_2], Original ATen: [aten.index, aten.mul, aten.sum]
# Source node to ATen node mapping:
#   outer_product => mul_1
#   q => index_1
#   sum_2 => sum_2
# Graph fragment:
#   %index_1 : [num_users=1] = call_function[target=torch.ops.aten.index.Tensor](args = (%primals_1, [None, %lift_fresh_copy_1]), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%permute, %index_1), kwargs = {})
#   %sum_2 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_1, [-1]), kwargs = {})
triton_poi_fused_index_mul_sum_1 = async_compile.triton('triton_poi_fused_index_mul_sum_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[32], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=90, major=9, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=132), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_index_mul_sum_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 0, 'backend_hash': 'B098E03CDA7B8ADC90DAFFDF24A2956451D1B13F297756A5DCC209498AA53705', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

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)
        # Topologically Sorted Source Nodes: [p_1, p_2], Original ATen: [aten.mul, aten.sum]
        stream0 = get_raw_stream(0)
        triton_poi_fused_mul_sum_0.run(primals_1, primals_2, buf0, 96, grid=grid(96), stream=stream0)
        del primals_2
        buf1 = empty_strided_cuda((4, 6), (6, 1), torch.float32)
        # Topologically Sorted Source Nodes: [q, outer_product, sum_2], Original ATen: [aten.index, aten.mul, aten.sum]
        triton_poi_fused_index_mul_sum_1.run(buf0, primals_1, buf1, 24, grid=grid(24), stream=stream0)
        del buf0
    return (buf1, primals_1, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    primals_1 = rand_strided((4, 4, 4), (16, 4, 1), device='cuda:0', dtype=torch.float32)
    primals_2 = rand_strided((4, 6, 4), (24, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([primals_1, primals_2])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

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]
