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 math as tl_math
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


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


@triton.jit
def triton_poi_fused__softmax_add_log_neg_1(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, 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)
    tmp1 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask)
    tmp7 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
    tmp8 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask)
    tmp14 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
    tmp15 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask)
    tmp21 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
    tmp22 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask)
    tmp2 = tl_math.log(tmp1)
    tmp3 = -tmp2
    tmp4 = tmp0 + tmp3
    tmp5 = 1.0
    tmp6 = tmp4 * tmp5
    tmp9 = tl_math.log(tmp8)
    tmp10 = -tmp9
    tmp11 = tmp7 + tmp10
    tmp12 = tmp11 * tmp5
    tmp13 = triton_helpers.maximum(tmp6, tmp12)
    tmp16 = tl_math.log(tmp15)
    tmp17 = -tmp16
    tmp18 = tmp14 + tmp17
    tmp19 = tmp18 * tmp5
    tmp20 = triton_helpers.maximum(tmp13, tmp19)
    tmp23 = tl_math.log(tmp22)
    tmp24 = -tmp23
    tmp25 = tmp21 + tmp24
    tmp26 = tmp25 * tmp5
    tmp27 = triton_helpers.maximum(tmp20, tmp26)
    tmp28 = tmp6 - tmp27
    tmp29 = tmp28 * tmp5
    tmp30 = tl_math.exp(tmp29)
    tmp31 = tmp12 - tmp27
    tmp32 = tmp31 * tmp5
    tmp33 = tl_math.exp(tmp32)
    tmp34 = tmp30 + tmp33
    tmp35 = tmp19 - tmp27
    tmp36 = tmp35 * tmp5
    tmp37 = tl_math.exp(tmp36)
    tmp38 = tmp34 + tmp37
    tmp39 = tmp26 - tmp27
    tmp40 = tmp39 * tmp5
    tmp41 = tl_math.exp(tmp40)
    tmp42 = tmp38 + tmp41
    tl.store(out_ptr0 + x2, tmp27, xmask)
    tl.store(out_ptr1 + x2, tmp42, xmask)


@triton.jit
def triton_poi_fused__softmax_add_log_neg_2(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x0 = xindex % 16
    x2 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_out_ptr0 + x3, xmask)
    tmp7 = tl.load(in_ptr1 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp11 = tl.load(in_ptr2 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp13 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp16 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tl_math.log(tmp1)
    tmp3 = -tmp2
    tmp4 = tmp0 + tmp3
    tmp5 = 1.0
    tmp6 = tmp4 * tmp5
    tmp8 = tmp6 - tmp7
    tmp9 = tmp8 * tmp5
    tmp10 = tl_math.exp(tmp9)
    tmp12 = tmp10 / tmp11
    tmp15 = triton_helpers.maximum(tmp13, tmp14)
    tmp17 = triton_helpers.maximum(tmp15, tmp16)
    tmp19 = triton_helpers.maximum(tmp17, tmp18)
    tmp20 = tmp0 - tmp19
    tmp21 = tl_math.exp(tmp20)
    tl.store(in_out_ptr0 + x3, tmp12, xmask)
    tl.store(out_ptr0 + x3, tmp21, xmask)


@triton.jit
def triton_poi_fused_add_argmax_max_scatter_sub_3(in_ptr0, out_ptr0,
    out_ptr1, 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)
    tmp1 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
    tmp17 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
    tmp32 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
    tmp2 = tmp0 > tmp1
    tmp3 = tmp0 == tmp1
    tmp4 = tmp0 != tmp0
    tmp5 = tmp1 != tmp1
    tmp6 = tmp4 > tmp5
    tmp7 = tmp2 | tmp6
    tmp8 = tmp4 & tmp5
    tmp9 = tmp3 | tmp8
    tmp10 = tl.full([1], 0, tl.int64)
    tmp11 = tl.full([1], 1, tl.int64)
    tmp12 = tmp10 < tmp11
    tmp13 = tmp9 & tmp12
    tmp14 = tmp7 | tmp13
    tmp15 = tl.where(tmp14, tmp0, tmp1)
    tmp16 = tl.where(tmp14, tmp10, tmp11)
    tmp18 = tmp15 > tmp17
    tmp19 = tmp15 == tmp17
    tmp20 = tmp15 != tmp15
    tmp21 = tmp17 != tmp17
    tmp22 = tmp20 > tmp21
    tmp23 = tmp18 | tmp22
    tmp24 = tmp20 & tmp21
    tmp25 = tmp19 | tmp24
    tmp26 = tl.full([1], 2, tl.int64)
    tmp27 = tmp16 < tmp26
    tmp28 = tmp25 & tmp27
    tmp29 = tmp23 | tmp28
    tmp30 = tl.where(tmp29, tmp15, tmp17)
    tmp31 = tl.where(tmp29, tmp16, tmp26)
    tmp33 = tmp30 > tmp32
    tmp34 = tmp30 == tmp32
    tmp35 = tmp30 != tmp30
    tmp36 = tmp32 != tmp32
    tmp37 = tmp35 > tmp36
    tmp38 = tmp33 | tmp37
    tmp39 = tmp35 & tmp36
    tmp40 = tmp34 | tmp39
    tmp41 = tl.full([1], 3, tl.int64)
    tmp42 = tmp31 < tmp41
    tmp43 = tmp40 & tmp42
    tmp44 = tmp38 | tmp43
    tl.where(tmp44, tmp30, tmp32)
    tmp46 = tl.where(tmp44, tmp31, tmp41)
    tmp47 = tmp46 == tmp10
    tmp48 = 1.0
    tmp49 = 0.0
    tmp50 = tl.where(tmp47, tmp48, tmp49)
    tmp51 = tmp50 - tmp0
    tmp52 = tmp51 + tmp0
    tmp53 = tmp46 == tmp11
    tmp54 = tl.where(tmp53, tmp48, tmp49)
    tmp55 = tmp54 - tmp1
    tmp56 = tmp55 + tmp1
    tmp57 = tmp52 > tmp56
    tmp58 = tmp52 == tmp56
    tmp59 = tmp52 != tmp52
    tmp60 = tmp56 != tmp56
    tmp61 = tmp59 > tmp60
    tmp62 = tmp57 | tmp61
    tmp63 = tmp59 & tmp60
    tmp64 = tmp58 | tmp63
    tmp65 = tmp64 & tmp12
    tmp66 = tmp62 | tmp65
    tmp67 = tl.where(tmp66, tmp52, tmp56)
    tmp68 = tl.where(tmp66, tmp10, tmp11)
    tmp69 = tmp46 == tmp26
    tmp70 = tl.where(tmp69, tmp48, tmp49)
    tmp71 = tmp70 - tmp17
    tmp72 = tmp71 + tmp17
    tmp73 = tmp67 > tmp72
    tmp74 = tmp67 == tmp72
    tmp75 = tmp67 != tmp67
    tmp76 = tmp72 != tmp72
    tmp77 = tmp75 > tmp76
    tmp78 = tmp73 | tmp77
    tmp79 = tmp75 & tmp76
    tmp80 = tmp74 | tmp79
    tmp81 = tmp68 < tmp26
    tmp82 = tmp80 & tmp81
    tmp83 = tmp78 | tmp82
    tmp84 = tl.where(tmp83, tmp67, tmp72)
    tmp85 = tl.where(tmp83, tmp68, tmp26)
    tmp86 = tmp46 == tmp41
    tmp87 = tl.where(tmp86, tmp48, tmp49)
    tmp88 = tmp87 - tmp32
    tmp89 = tmp88 + tmp32
    tmp90 = tmp84 > tmp89
    tmp91 = tmp84 == tmp89
    tmp92 = tmp84 != tmp84
    tmp93 = tmp89 != tmp89
    tmp94 = tmp92 > tmp93
    tmp95 = tmp90 | tmp94
    tmp96 = tmp92 & tmp93
    tmp97 = tmp91 | tmp96
    tmp98 = tmp85 < tmp41
    tmp99 = tmp97 & tmp98
    tmp100 = tmp95 | tmp99
    tl.where(tmp100, tmp84, tmp89)
    tmp102 = tl.where(tmp100, tmp85, tmp41)
    tl.store(out_ptr0 + x2, tmp46, xmask)
    tl.store(out_ptr1 + x2, tmp102, xmask)


@triton.jit
def triton_poi_fused_clone_4(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    xnumel = 16
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y1 = yindex // 4
    y0 = yindex % 4
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (x2 + 16 * y1), xmask & ymask, eviction_policy
        ='evict_last')
    tmp6 = tl.load(in_ptr1 + (x2 + 16 * y3), xmask & ymask)
    tmp1 = y0
    tmp2 = tmp0 == tmp1
    tmp3 = 1.0
    tmp4 = 0.0
    tmp5 = tl.where(tmp2, tmp3, tmp4)
    tmp7 = tmp5 - tmp6
    tmp8 = tmp7 + tmp6
    tl.store(out_ptr0 + (y0 + 4 * x2 + 64 * y1), tmp8, xmask & ymask)


@triton.jit
def triton_poi_fused__softmax_5(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
    x3 = xindex
    x0 = xindex % 16
    x2 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x3, tmp8, xmask)


@triton.jit
def triton_per_fused_add_log_mean_mul_sum_6(in_out_ptr0, in_ptr0, 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 % 16
    r1 = rindex // 16
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
    tmp7 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
    tmp13 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
    tmp19 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
    tmp1 = 4.0
    tmp2 = tmp0 * tmp1
    tmp3 = 1e-10
    tmp4 = tmp2 + tmp3
    tmp5 = tl_math.log(tmp4)
    tmp6 = tmp0 * tmp5
    tmp8 = tmp7 * tmp1
    tmp9 = tmp8 + tmp3
    tmp10 = tl_math.log(tmp9)
    tmp11 = tmp7 * tmp10
    tmp12 = tmp6 + tmp11
    tmp14 = tmp13 * tmp1
    tmp15 = tmp14 + tmp3
    tmp16 = tl_math.log(tmp15)
    tmp17 = tmp13 * tmp16
    tmp18 = tmp12 + tmp17
    tmp20 = tmp19 * tmp1
    tmp21 = tmp20 + tmp3
    tmp22 = tl_math.log(tmp21)
    tmp23 = tmp19 * tmp22
    tmp24 = tmp18 + tmp23
    tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp27 = tl.sum(tmp25, 1)[:, None]
    tmp28 = 64.0
    tmp29 = tmp27 / tmp28
    tmp30 = 1.0
    tmp31 = tmp29 * tmp30
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp31, None)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1))
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_poi_fused_convolution_0[grid(256)](buf1, primals_2, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_2
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf3 = torch.ops.aten.exponential.default(buf2)
        buf4 = buf3
        del buf3
        buf5 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        buf6 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        triton_poi_fused__softmax_add_log_neg_1[grid(64)](buf1, buf4, buf5,
            buf6, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf7 = buf4
        del buf4
        buf12 = buf2
        del buf2
        triton_poi_fused__softmax_add_log_neg_2[grid(256)](buf7, buf1, buf5,
            buf6, buf12, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del buf5
        del buf6
        buf8 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.int64)
        buf9 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int64)
        triton_poi_fused_add_argmax_max_scatter_sub_3[grid(64)](buf7, buf8,
            buf9, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf10 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 1),
            torch.float32)
        triton_poi_fused_clone_4[grid(16, 16)](buf8, buf7, buf10, 16, 16,
            XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1)
        del buf8
        buf11 = empty_strided_cuda((1, 64, 4), (256, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf10, (1, 64, 4), (0, 4, 1),
            0), reinterpret_tensor(primals_4, (1, 4, 4), (16, 4, 1), 0),
            out=buf11)
        buf13 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__softmax_5[grid(256)](buf12, buf13, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        del buf12
        buf14 = empty_strided_cuda((), (), torch.float32)
        buf15 = buf14
        del buf14
        triton_per_fused_add_log_mean_mul_sum_6[grid(1)](buf15, buf13, 1, 
            64, XBLOCK=1, num_warps=2, num_stages=1)
        del buf13
    return reinterpret_tensor(buf11, (4, 4, 4, 4), (64, 1, 16, 4), 0
        ), buf15, buf9, primals_1, primals_3, buf1, buf7, reinterpret_tensor(
        buf10, (1, 4, 64), (256, 1, 4), 0), reinterpret_tensor(primals_4, (
        1, 4, 4), (16, 1, 4), 0)


class GumbelQuantizeNew(nn.Module):
    """
    Reference:
    Categorical Reparameterization with Gumbel-Softmax, Jang et al. 2016
    https://arxiv.org/abs/1611.01144
    """

    def __init__(self, hidden_channel, n_e, e_dim, kl_weight=1.0, temp_init
        =1.0, straight_through=True):
        super().__init__()
        self.e_dim = e_dim
        self.n_e = n_e
        self.straight_through = straight_through
        self.temperature = temp_init
        self.kl_weight = kl_weight
        self.proj = nn.Conv2d(hidden_channel, n_e, kernel_size=1)
        self.embedding = nn.Embedding(n_e, e_dim)

    def get_codebook(self):
        return self.embedding.weight

    def get_codebook_entry(self, indices, shape=None):
        z_q = self.embedding(indices)
        if shape is not None:
            z_q = z_q.view(shape)
            z_q = z_q.permute(0, 3, 1, 2).contiguous()
        return z_q

    def forward(self, input_0):
        primals_1 = self.proj.weight
        primals_2 = self.proj.bias
        primals_4 = self.embedding.weight
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0], output[1], output[2]


Measured latency_ms: 0.081206