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 math
from torch import 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__to_copy_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tmp0.to(tl.int64)
    tl.store(out_ptr0 + x0, tmp1, xmask)


@triton.jit
def triton_poi_fused_add_embedding_sum_1(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x1 = xindex // 16
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
    tmp7 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
    tmp14 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
    tmp21 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
    tmp28 = tl.load(in_ptr2 + 0)
    tmp29 = tl.broadcast_to(tmp28, [XBLOCK])
    tmp1 = tl.full([XBLOCK], 5, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 5) | ~xmask,
        'index out of bounds: 0 <= tmp4 < 5')
    tmp6 = tl.load(in_ptr1 + tmp4, xmask, eviction_policy='evict_last')
    tmp8 = tmp7 + tmp1
    tmp9 = tmp7 < 0
    tmp10 = tl.where(tmp9, tmp8, tmp7)
    tl.device_assert((0 <= tmp10) & (tmp10 < 5) | ~xmask,
        'index out of bounds: 0 <= tmp10 < 5')
    tmp12 = tl.load(in_ptr1 + tmp10, xmask, eviction_policy='evict_last')
    tmp13 = tmp6 + tmp12
    tmp15 = tmp14 + tmp1
    tmp16 = tmp14 < 0
    tmp17 = tl.where(tmp16, tmp15, tmp14)
    tl.device_assert((0 <= tmp17) & (tmp17 < 5) | ~xmask,
        'index out of bounds: 0 <= tmp17 < 5')
    tmp19 = tl.load(in_ptr1 + tmp17, xmask, eviction_policy='evict_last')
    tmp20 = tmp13 + tmp19
    tmp22 = tmp21 + tmp1
    tmp23 = tmp21 < 0
    tmp24 = tl.where(tmp23, tmp22, tmp21)
    tl.device_assert((0 <= tmp24) & (tmp24 < 5) | ~xmask,
        'index out of bounds: 0 <= tmp24 < 5')
    tmp26 = tl.load(in_ptr1 + tmp24, xmask, eviction_policy='evict_last')
    tmp27 = tmp20 + tmp26
    tmp30 = tmp27 + tmp29
    tl.store(out_ptr0 + x2, tmp30, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (5, 1), (1, 1))
    assert_size_stride(primals_3, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.int64)
        get_raw_stream(0)
        triton_poi_fused__to_copy_0[grid(256)](primals_1, buf0, 256, XBLOCK
            =128, num_warps=4, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
        triton_poi_fused_add_embedding_sum_1[grid(64)](buf0, primals_2,
            primals_3, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del primals_2
        del primals_3
    return buf1, buf0


class WideNew(nn.Module):
    """wide (linear) component

    Linear model implemented via an Embedding layer connected to the output
    neuron(s).

    Parameters
    -----------
    wide_dim: int
        size of the Embedding layer. `wide_dim` is the summation of all the
        individual values for all the features that go through the wide
        component. For example, if the wide component receives 2 features with
        5 individual values each, `wide_dim = 10`
    pred_dim: int, default = 1
        size of the ouput tensor containing the predictions

    Attributes
    -----------
    wide_linear: ``nn.Module``
        the linear layer that comprises the wide branch of the model

    Examples
    --------
    >>> import torch
    >>> from pytorch_widedeep.models import Wide
    >>> X = torch.empty(4, 4).random_(6)
    >>> wide = Wide(wide_dim=X.unique().size(0), pred_dim=1)
    >>> out = wide(X)
    """

    def __init__(self, wide_dim: 'int', pred_dim: 'int'=1):
        super(WideNew, self).__init__()
        self.wide_linear = nn.Embedding(wide_dim + 1, pred_dim, padding_idx=0)
        self.bias = nn.Parameter(torch.zeros(pred_dim))
        self._reset_parameters()

    def _reset_parameters(self) ->None:
        """initialize Embedding and bias like nn.Linear. See `original
        implementation
        <https://pytorch.org/docs/stable/_modules/torch/nn/modules/linear.html#Linear>`_.
        """
        nn.init.kaiming_uniform_(self.wide_linear.weight, a=math.sqrt(5))
        fan_in, _ = nn.init._calculate_fan_in_and_fan_out(self.wide_linear.
            weight)
        bound = 1 / math.sqrt(fan_in)
        nn.init.uniform_(self.bias, -bound, bound)

    def forward(self, input_0):
        primals_3 = self.bias
        primals_2 = self.wide_linear.weight
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.015682