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
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__to_copy_clamp_div_mul_sub_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 = 4.0
    tmp2 = tmp0 - tmp1
    tmp3 = tmp2 * tmp1
    tmp4 = float('inf')
    tmp5 = tmp3 * tmp4
    tmp6 = 0.0
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = 3.0
    tmp9 = triton_helpers.minimum(tmp7, tmp8)
    tmp10 = tmp9.to(tl.int64)
    tl.store(out_ptr0 + x0, tmp10, xmask)


@triton.jit
def triton_poi_fused_embedding_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp1 = tl.full([XBLOCK], 4, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp4 < 4')
    tmp6 = tl.load(in_ptr1 + (x0 + 4 * tmp4), xmask)
    tl.store(out_ptr0 + x2, tmp6, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 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_clamp_div_mul_sub_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, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused_embedding_1[grid(1024)](buf0, primals_2, buf1, 
            1024, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_2
    return buf1, buf0


class BucketingEmbeddingNew(nn.Module):

    def __init__(self, min_val, max_val, count, dim, use_log_scale=False):
        super().__init__()
        self.min_val = min_val
        self.max_val = max_val
        self.count = count
        self.dim = dim
        self.use_log_scale = use_log_scale
        if self.use_log_scale:
            self.min_val = torch.log2(torch.Tensor([self.min_val])).item()
            self.max_val = torch.log2(torch.Tensor([self.max_val])).item()
        self.main = nn.Embedding(count, dim)

    def get_class(self, x):
        """
        x - (bs, ) values
        """
        if self.use_log_scale:
            x = torch.log2(x)
        x = self.count * (x - self.min_val) / (self.max_val - self.min_val)
        x = torch.clamp(x, 0, self.count - 1).long()
        return x

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


Measured latency_ms: 0.027712