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._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
import torch.nn.functional as F
import torch.cuda
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_linalg_vector_norm_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 4
    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')
    tmp5 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (3 + 4 * 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
    tmp11 = libdevice.sqrt(tmp10)
    tl.store(out_ptr0 + x0, tmp11, xmask)


@triton.jit
def triton_poi_fused_div_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 / tmp1
    tl.store(out_ptr0 + x2, tmp2, xmask)


@triton.jit
def triton_poi_fused_div_2(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp2 = tmp1 * tmp1
    tmp4 = tmp3 * tmp3
    tmp5 = tmp2 + tmp4
    tmp7 = tmp6 * tmp6
    tmp8 = tmp5 + tmp7
    tmp10 = tmp9 * tmp9
    tmp11 = tmp8 + tmp10
    tmp12 = libdevice.sqrt(tmp11)
    tmp13 = tmp0 / tmp12
    tl.store(out_ptr0 + x2, tmp13, xmask)


@triton.jit
def triton_poi_fused__softmax_3(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x2, tmp9, xmask)


@triton.jit
def triton_red_fused__softmax_abs_mean_sub_4(in_ptr0, out_ptr0, out_ptr1,
    out_ptr2, 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, :]
    _tmp17 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r2 = rindex
        r1 = rindex // 4
        tmp0 = tl.load(in_ptr0 + r2, rmask, eviction_policy='evict_first',
            other=0.0)
        tmp1 = tl.load(in_ptr0 + 4 * r1, rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp2 = tl.load(in_ptr0 + (1 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp4 = tl.load(in_ptr0 + (2 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp6 = tl.load(in_ptr0 + (3 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp3 = tmp1 + tmp2
        tmp5 = tmp3 + tmp4
        tmp7 = tmp5 + tmp6
        tmp8 = tmp0 / tmp7
        tmp9 = 0.0
        tmp10 = tmp7 >= tmp9
        tmp11 = 1.0
        tmp12 = -1.0
        tmp13 = tl.where(tmp10, tmp11, tmp12)
        tmp14 = tmp0 * tmp13
        tmp15 = tl_math.abs(tmp8)
        tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
        tmp18 = _tmp17 + tmp16
        _tmp17 = tl.where(rmask, tmp18, _tmp17)
        tl.store(out_ptr0 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp8, rmask)
        tl.store(out_ptr1 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp14, rmask
            )
    tmp17 = tl.sum(_tmp17, 1)[:, None]
    tl.store(out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp17, None)


@triton.jit
def triton_poi_fused__softmax_5(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp14 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp11 = tmp9 + tmp10
    tmp13 = tmp11 + tmp12
    tmp15 = tmp13 + tmp14
    tmp16 = 0.0
    tmp17 = tmp15 >= tmp16
    tmp18 = 1.0
    tmp19 = -1.0
    tmp20 = tl.where(tmp17, tmp18, tmp19)
    tmp21 = tmp20 * tmp15
    tmp22 = tmp8 / tmp21
    tl.store(out_ptr0 + x2, tmp22, xmask)


@triton.jit
def triton_poi_fused__log_softmax_6(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp2 = tl_math.exp(tmp1)
    tmp4 = tl_math.exp(tmp3)
    tmp5 = tmp2 + tmp4
    tmp7 = tl_math.exp(tmp6)
    tmp8 = tmp5 + tmp7
    tmp10 = tl_math.exp(tmp9)
    tmp11 = tmp8 + tmp10
    tmp12 = tl_math.log(tmp11)
    tmp13 = tmp0 - tmp12
    tl.store(out_ptr0 + x2, tmp13, xmask)


@triton.jit
def triton_per_fused_mul_sum_7(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 64
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0)
    tmp1 = -1.0
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, 0)
    tmp6 = tl.sum(tmp5, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp6, xmask)


@triton.jit
def triton_per_fused_abs_add_mean_mul_8(in_out_ptr0, in_ptr0, in_ptr1,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp4 = tl.load(in_ptr1 + 0)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, 1])
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp6 = 256.0
    tmp7 = tmp5 / tmp6
    tmp8 = 1000.0
    tmp9 = tmp7 * tmp8
    tmp10 = 64.0
    tmp11 = tmp3 / tmp10
    tmp12 = 5e-05
    tmp13 = tmp11 * tmp12
    tmp14 = tmp9 + tmp13
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp14, 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((4,), (1,), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_linalg_vector_norm_0[grid(4)](primals_2, buf0, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_div_1[grid(16)](primals_2, buf0, buf1, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        del primals_2
        buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        triton_poi_fused_div_2[grid(256)](primals_1, buf2, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del primals_1
        buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf2, reinterpret_tensor(buf1, (4, 4), (1, 4), 0),
            out=buf3)
        buf4 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        triton_poi_fused__softmax_3[grid(256)](buf3, buf4, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf5 = buf3
        del buf3
        buf8 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        buf7 = empty_strided_cuda((), (), torch.float32)
        triton_red_fused__softmax_abs_mean_sub_4[grid(1)](buf4, buf5, buf8,
            buf7, 1, 256, XBLOCK=1, RBLOCK=256, num_warps=2, num_stages=1)
        buf6 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf5, buf1, out=buf6)
        buf9 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        triton_poi_fused__softmax_5[grid(256)](buf8, buf4, buf9, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del buf4
        buf10 = buf8
        del buf8
        triton_poi_fused__log_softmax_6[grid(256)](buf9, buf10, 256, XBLOCK
            =128, num_warps=4, num_stages=1)
        del buf9
        buf11 = empty_strided_cuda((64, 64), (64, 1), torch.float32)
        extern_kernels.mm(buf10, reinterpret_tensor(buf5, (4, 64), (1, 4), 
            0), out=buf11)
        buf12 = empty_strided_cuda((64,), (1,), torch.float32)
        triton_per_fused_mul_sum_7[grid(64)](buf11, buf12, 64, 64, XBLOCK=8,
            num_warps=4, num_stages=1)
        del buf11
        buf13 = empty_strided_cuda((), (), torch.float32)
        buf14 = buf13
        del buf13
        triton_per_fused_abs_add_mean_mul_8[grid(1)](buf14, buf12, buf7, 1,
            64, XBLOCK=1, num_warps=2, num_stages=1)
        del buf12
        del buf7
    return reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 16, 4, 1), 0
        ), buf14, reinterpret_tensor(buf0, (4, 1), (1, 1), 0
        ), buf1, buf2, buf5, buf10


class SequenceQuantizerSoftEMANew(nn.Module):

    def __init__(self, codebook_size, d_model, l1_cost=1000, entropy_cost=
        5e-05, num_samples=10, temp=1.0, epsilon=1e-05, padding_idx=None):
        super(SequenceQuantizerSoftEMANew, self).__init__()
        self.d_model = d_model
        self.codebook_size = codebook_size
        self.padding_idx = padding_idx
        self.codebook = nn.Parameter(torch.FloatTensor(self.codebook_size,
            self.d_model), requires_grad=True)
        torch.nn.init.xavier_uniform_(self.codebook)
        self.l1_cost = l1_cost
        self.entropy_cost = entropy_cost
        self.num_samples = num_samples
        self.temp = temp
        self._epsilon = epsilon

    def entropy(self, tensor):
        return torch.mean(torch.sum(-1 * torch.matmul(F.log_softmax(tensor,
            dim=1), tensor.t()), dim=1))

    def cluster(self, inputs):
        input_shape = inputs.size()
        inputs.dim()
        flat_input = inputs.reshape(-1, self.d_model)
        flat_input = flat_input / flat_input.norm(2, dim=1)[:, None]
        codebook = self.codebook / self.codebook.norm(2, dim=1)[:, None]
        distances = F.softmax(torch.matmul(flat_input, codebook.t()).
            reshape(-1, self.output_nheads, codebook.shape[0]), dim=2)
        reconstruction = torch.matmul(distances, codebook).view(input_shape)
        encoding_indices = torch.argmax(distances, dim=1).reshape(-1, self.
            output_nheads)
        return reconstruction, encoding_indices, distances

    def set_codebook(self, new_codebook):
        self.codebook.copy_(new_codebook)

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


Measured latency_ms: 0.146253