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.triton_helpers import libdevice
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_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_index_put_lift_fresh_1(in_out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = 1e-08
    tmp2 = tmp0 < tmp1
    tmp3 = 9.99999993922529e-09
    tmp4 = tl.where(tmp2, tmp3, tmp0)
    tl.store(in_out_ptr0 + x0, tmp4, xmask)


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


@triton.jit
def triton_poi_fused_index_3(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
    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, 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, 1), torch.float32)
        extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4),
            (1, 4), 0), out=buf0)
        buf1 = empty_strided_cuda((4,), (1,), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_linalg_vector_norm_0[grid(4)](primals_1, buf1, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        del primals_1
        buf2 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_poi_fused_linalg_vector_norm_0[grid(4)](primals_2, buf2, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf1, (4, 1), (1, 0), 0),
            reinterpret_tensor(buf2, (1, 4), (0, 1), 0), out=buf3)
        del buf1
        del buf2
        buf4 = buf3
        del buf3
        triton_poi_fused_index_put_lift_fresh_1[grid(16)](buf4, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        buf5 = empty_strided_cuda((4,), (1,), torch.int64)
        triton_poi_fused_add_argmin_div_neg_2[grid(4)](buf0, buf4, buf5, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        del buf0
        buf6 = buf4
        del buf4
        triton_poi_fused_index_3[grid(16)](buf5, primals_2, buf6, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del primals_2
    return buf6, buf5, buf5


class Quantizing_cossimNew(nn.Module):
    """
    This is quantizing layer.
    """
    __initialized: 'bool' = True

    def __init__(self, num_quantizing: 'int', quantizing_dim: 'int',
        _weight: 'torch.Tensor'=None, initialize_by_dataset: 'bool'=True,
        mean: 'float'=0.0, std: 'float'=1.0, eps: 'float'=1e-08, dtype:
        'torch.dtype'=None, device: 'torch.device'=None):
        super().__init__()
        assert num_quantizing > 0
        assert quantizing_dim > 0
        self.num_quantizing = num_quantizing
        self.quantizing_dim = quantizing_dim
        self.initialize_by_dataset = initialize_by_dataset
        self.mean, self.std = mean, std
        self.eps = eps
        if _weight is None:
            self.weight = nn.Parameter(torch.empty(num_quantizing,
                quantizing_dim, dtype=dtype, device=device))
            nn.init.normal_(self.weight, mean=mean, std=std)
            if initialize_by_dataset:
                self.__initialized = False
                self.__initialized_length = 0
        else:
            assert _weight.dim() == 2
            assert _weight.size(0) == num_quantizing
            assert _weight.size(1) == quantizing_dim
            self.weight = nn.Parameter(_weight.to(device))

    def from_idx(self, idx: 'torch.Tensor') ->torch.Tensor:
        """
        idx: shape is (*, ). int tensor.
        return -> (*, E) float tensor
        """
        input_size = idx.shape
        i = idx.view(-1)
        q_data = self.weight[i].view(*input_size, self.quantizing_dim)
        return q_data

    def load_state_dict(self, state_dict, strict: 'bool'):
        self.__initialized = True
        return super().load_state_dict(state_dict, strict=strict)

    def __repr__(self):
        s = f'Quantizing({self.num_quantizing}, {self.quantizing_dim})'
        return s

    def calculate_distance(self, x: 'torch.Tensor') ->torch.Tensor:
        """
        x: shape is (B, *), float tensor
        """
        assert x.dim() == 2
        dot = torch.matmul(x, self.weight.T)
        x_l2n = torch.linalg.norm(x, dim=-1)[:, None]
        w_l2n = torch.linalg.norm(self.weight, dim=-1)[None, :]
        norm = torch.matmul(x_l2n, w_l2n)
        norm[norm < self.eps] = self.eps
        cos_sim = dot / norm
        return -cos_sim + 1

    def isInitialized(self) ->bool:
        return self.__initialized

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


Measured latency_ms: 0.027683