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 import nn
from torch.nn import 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_mul_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 % 64
    x1 = xindex // 64
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (16 * x1 + 64 * (x0 // 16) + x0 % 16), xmask)
    tmp1 = 2.0
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x2, tmp2, xmask)


@triton.jit
def triton_poi_fused_add_pow_sub_sum_1(in_out_ptr0, in_ptr0, in_ptr1,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + (64 * (x1 // 16) + x1 % 16), xmask,
        eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (16 + 64 * (x1 // 16) + x1 % 16), xmask,
        eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (32 + 64 * (x1 // 16) + x1 % 16), xmask,
        eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (48 + 64 * (x1 // 16) + x1 % 16), xmask,
        eviction_policy='evict_last')
    tmp11 = tl.load(in_out_ptr0 + x2, xmask)
    tmp13 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp18 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp21 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp12 = tmp10 - tmp11
    tmp14 = tmp13 * tmp13
    tmp16 = tmp15 * tmp15
    tmp17 = tmp14 + tmp16
    tmp19 = tmp18 * tmp18
    tmp20 = tmp17 + tmp19
    tmp22 = tmp21 * tmp21
    tmp23 = tmp20 + tmp22
    tmp24 = tmp12 + tmp23
    tl.store(in_out_ptr0 + x2, tmp24, xmask)


@triton.jit
def triton_poi_fused_max_neg_2(in_ptr0, 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
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp35 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp1 = -tmp0
    tmp3 = -tmp2
    tmp4 = tmp1 > tmp3
    tmp5 = tmp1 == tmp3
    tmp6 = tmp1 != tmp1
    tmp7 = tmp3 != tmp3
    tmp8 = tmp6 > tmp7
    tmp9 = tmp4 | tmp8
    tmp10 = tmp6 & tmp7
    tmp11 = tmp5 | tmp10
    tmp12 = tl.full([1], 0, tl.int64)
    tmp13 = tl.full([1], 1, tl.int64)
    tmp14 = tmp12 < tmp13
    tmp15 = tmp11 & tmp14
    tmp16 = tmp9 | tmp15
    tmp17 = tl.where(tmp16, tmp1, tmp3)
    tmp18 = tl.where(tmp16, tmp12, tmp13)
    tmp20 = -tmp19
    tmp21 = tmp17 > tmp20
    tmp22 = tmp17 == tmp20
    tmp23 = tmp17 != tmp17
    tmp24 = tmp20 != tmp20
    tmp25 = tmp23 > tmp24
    tmp26 = tmp21 | tmp25
    tmp27 = tmp23 & tmp24
    tmp28 = tmp22 | tmp27
    tmp29 = tl.full([1], 2, tl.int64)
    tmp30 = tmp18 < tmp29
    tmp31 = tmp28 & tmp30
    tmp32 = tmp26 | tmp31
    tmp33 = tl.where(tmp32, tmp17, tmp20)
    tmp34 = tl.where(tmp32, tmp18, tmp29)
    tmp36 = -tmp35
    tmp37 = tmp33 > tmp36
    tmp38 = tmp33 == tmp36
    tmp39 = tmp33 != tmp33
    tmp40 = tmp36 != tmp36
    tmp41 = tmp39 > tmp40
    tmp42 = tmp37 | tmp41
    tmp43 = tmp39 & tmp40
    tmp44 = tmp38 | tmp43
    tmp45 = tl.full([1], 3, tl.int64)
    tmp46 = tmp34 < tmp45
    tmp47 = tmp44 & tmp46
    tmp48 = tmp42 | tmp47
    tl.where(tmp48, tmp33, tmp36)
    tmp50 = tl.where(tmp48, tmp34, tmp45)
    tl.store(out_ptr0 + x0, tmp50, xmask)


@triton.jit
def triton_red_fused_add_clone_mean_mul_pow_sub_3(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK: tl.
    constexpr, RBLOCK: tl.constexpr):
    rnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rbase = tl.arange(0, RBLOCK)[None, :]
    _tmp11 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r0 = rindex % 16
        r2 = rindex // 64
        r1 = rindex // 16 % 4
        r3 = rindex
        tmp0 = tl.load(in_ptr0 + (r0 + 16 * r2), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp7 = tl.load(in_ptr2 + r3, rmask, eviction_policy='evict_first',
            other=0.0)
        tmp1 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
        tmp2 = tmp0 + tmp1
        tmp3 = tmp0 < 0
        tmp4 = tl.where(tmp3, tmp2, tmp0)
        tl.device_assert((0 <= tmp4) & (tmp4 < 4) | ~rmask,
            'index out of bounds: 0 <= tmp4 < 4')
        tmp6 = tl.load(in_ptr1 + (tmp4 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp8 = tmp6 - tmp7
        tmp9 = tmp8 * tmp8
        tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK])
        tmp12 = _tmp11 + tmp10
        _tmp11 = tl.where(rmask, tmp12, _tmp11)
        tmp13 = tmp7 + tmp8
        tmp14 = 2.0
        tmp15 = tmp8 * tmp14
        tl.store(out_ptr0 + tl.broadcast_to(r3, [XBLOCK, RBLOCK]), tmp13, rmask
            )
        tl.store(out_ptr1 + tl.broadcast_to(r3, [XBLOCK, RBLOCK]), tmp15, rmask
            )
    tmp11 = tl.sum(_tmp11, 1)[:, None]
    tmp16 = 256.0
    tmp17 = tmp11 / tmp16
    tmp18 = 0.25
    tmp19 = tmp17 * tmp18
    tmp20 = tmp17 + tmp19
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp20, None)


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((64, 4), (1, 64), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_0[grid(256)](primals_1, buf0, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf0, primals_2, out=buf1)
        buf2 = buf1
        del buf1
        triton_poi_fused_add_pow_sub_sum_1[grid(256)](buf2, primals_1,
            primals_2, 256, XBLOCK=256, num_warps=4, num_stages=1)
        buf3 = empty_strided_cuda((64,), (1,), torch.int64)
        triton_poi_fused_max_neg_2[grid(64)](buf2, buf3, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf5 = reinterpret_tensor(buf2, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf2
        buf6 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf0
        buf7 = buf4
        del buf4
        triton_red_fused_add_clone_mean_mul_pow_sub_3[grid(1)](buf7, buf3,
            primals_2, primals_1, buf5, buf6, 1, 256, XBLOCK=1, RBLOCK=256,
            num_warps=8, num_stages=1)
        del primals_1
        del primals_2
    return buf5, buf7, reinterpret_tensor(buf3, (4, 4, 4), (16, 4, 1), 0
        ), reinterpret_tensor(buf3, (4, 4, 4), (16, 4, 1), 0), buf6


class QuantizeNew(nn.Module):
    """
    Discretization bottleneck part of the VQ-VAE.
    Inputs:
    - n_e : number of embeddings
    - e_dim : dimension of embedding
    - beta : commitment cost used in loss term, beta * ||z_e(x)-sg[e]||^2
    """

    def __init__(self, dim, n_embed, beta=0.25):
        super().__init__()
        self.n_e = n_embed
        self.e_dim = dim
        self.beta = beta
        rand_range = 1.0 / self.n_e
        self.embeddings = nn.Parameter(torch.rand(dim, n_embed).mul_(2 *
            rand_range).sub_(rand_range))

    def embed_code(self, embed_id):
        codes = F.embedding(embed_id, self.embeddings.transpose(0, 1))
        return codes.permute(0, 3, 1, 2).contiguous()

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


Measured latency_ms: 0.065582