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
from torch import nn
from sklearn.cluster import KMeans
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_add_mul_pow_sub_sum_0(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
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp19 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp23 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp12 = tmp11 * tmp11
    tmp14 = tmp13 * tmp13
    tmp15 = tmp12 + tmp14
    tmp17 = tmp16 * tmp16
    tmp18 = tmp15 + tmp17
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp22 = tmp10 + tmp21
    tmp24 = 2.0
    tmp25 = tmp23 * tmp24
    tmp26 = tmp22 - tmp25
    tl.store(in_out_ptr0 + x2, tmp26, xmask)


@triton.jit
def triton_poi_fused_add_clamp_div_min_mul_neg_sigmoid_softplus_1(in_ptr0,
    in_ptr1, 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')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + 0)
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK])
    tmp1 = 0.0
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp4 = triton_helpers.maximum(tmp3, tmp1)
    tmp5 = triton_helpers.minimum(tmp2, tmp4)
    tmp7 = triton_helpers.maximum(tmp6, tmp1)
    tmp8 = triton_helpers.minimum(tmp5, tmp7)
    tmp10 = triton_helpers.maximum(tmp9, tmp1)
    tmp11 = triton_helpers.minimum(tmp8, tmp10)
    tmp14 = 20.0
    tmp15 = tmp13 > tmp14
    tmp16 = tl_math.exp(tmp13)
    tmp17 = libdevice.log1p(tmp16)
    tmp18 = tl.where(tmp15, tmp13, tmp17)
    tmp19 = -tmp18
    tmp20 = 6.9077542789816375
    tmp21 = tmp19 * tmp20
    tmp22 = tmp11 + tmp21
    tmp23 = tmp22 / tmp18
    tmp24 = tl.sigmoid(tmp23)
    tl.store(out_ptr0 + x0, tmp24, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    assert_size_stride(arg2_1, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(arg0_1, (64, 4), (4, 1), 0),
            reinterpret_tensor(arg1_1, (4, 4), (1, 4), 0), out=buf0)
        buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf0
        get_raw_stream(0)
        triton_poi_fused_add_mul_pow_sub_sum_0[grid(256)](buf1, arg0_1,
            arg1_1, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del arg0_1
        del arg1_1
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_clamp_div_min_mul_neg_sigmoid_softplus_1[grid(64)
            ](buf1, arg2_1, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del arg2_1
        del buf1
    return buf2,


class translatedSigmoid(nn.Module):

    def __init__(self):
        super(translatedSigmoid, self).__init__()
        self.beta = nn.Parameter(torch.tensor([-3.5]))

    def forward(self, x):
        beta = torch.nn.functional.softplus(self.beta)
        alpha = -beta * 6.9077542789816375
        return torch.sigmoid((x + alpha) / beta)


class DistNetNew(nn.Module):

    def __init__(self, dim, num_points):
        super().__init__()
        self.num_points = num_points
        self.points = nn.Parameter(torch.randn(num_points, dim),
            requires_grad=False)
        self.trans = translatedSigmoid()
        self.initialized = False

    def __dist2__(self, x):
        t1 = (x ** 2).sum(-1, keepdim=True)
        t2 = torch.transpose((self.points ** 2).sum(-1, keepdim=True), -1, -2)
        t3 = 2.0 * torch.matmul(x, torch.transpose(self.points, -1, -2))
        return (t1 + t2 - t3).clamp(min=0.0)

    def kmeans_initializer(self, embeddings):
        km = KMeans(n_clusters=self.num_points).fit(embeddings)
        self.points.data = torch.tensor(km.cluster_centers_, device=self.
            points.device)
        self.initialized = True

    def forward(self, input_0):
        arg1_1 = self.points
        arg2_1 = self.trans.beta
        arg0_1 = input_0
        output = call([arg0_1, arg1_1, arg2_1])
        return output[0]


Measured latency_ms: 0.027451