import torch
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.functional as F
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_div_sub_0(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 % 4
    x2 = xindex // 16
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr0 + (4 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (8 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr0 + (12 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp7 = tl.load(in_ptr0 + x3, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp8 = tmp6 - tmp7
    tmp9 = 0.3333333333333333
    tmp10 = tmp8 * tmp9
    tl.store(out_ptr0 + x3, tmp10, xmask)


@triton.jit
def triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_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
    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')
    tmp16 = tl.load(in_ptr1 + x2, xmask)
    tmp17 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp25 = tl.load(in_ptr1 + (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 = 1e-08
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp0 / tmp14
    tmp18 = tmp17 * tmp17
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp23 = tmp22 * tmp22
    tmp24 = tmp21 + tmp23
    tmp26 = tmp25 * tmp25
    tmp27 = tmp24 + tmp26
    tmp28 = libdevice.sqrt(tmp27)
    tmp29 = triton_helpers.maximum(tmp28, tmp13)
    tmp30 = tmp16 / tmp29
    tmp31 = tmp15 * tmp30
    tl.store(out_ptr0 + x2, tmp31, xmask)


@triton.jit
def triton_poi_fused_linalg_vector_norm_mean_repeat_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 + 16 * (x0 % 4), xmask, eviction_policy='evict_last'
        )
    tmp1 = tl.load(in_ptr0 + (4 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (8 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr0 + (12 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp10 = tl.load(in_ptr0 + (1 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp11 = tl.load(in_ptr0 + (5 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp13 = tl.load(in_ptr0 + (9 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp15 = tl.load(in_ptr0 + (13 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp20 = tl.load(in_ptr0 + (2 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp21 = tl.load(in_ptr0 + (6 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp23 = tl.load(in_ptr0 + (10 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp25 = tl.load(in_ptr0 + (14 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp30 = tl.load(in_ptr0 + (3 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp31 = tl.load(in_ptr0 + (7 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp33 = tl.load(in_ptr0 + (11 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp35 = tl.load(in_ptr0 + (15 + 16 * (x0 % 4)), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 4.0
    tmp8 = tmp6 / tmp7
    tmp9 = tmp8 * tmp8
    tmp12 = tmp10 + tmp11
    tmp14 = tmp12 + tmp13
    tmp16 = tmp14 + tmp15
    tmp17 = tmp16 / tmp7
    tmp18 = tmp17 * tmp17
    tmp19 = tmp9 + tmp18
    tmp22 = tmp20 + tmp21
    tmp24 = tmp22 + tmp23
    tmp26 = tmp24 + tmp25
    tmp27 = tmp26 / tmp7
    tmp28 = tmp27 * tmp27
    tmp29 = tmp19 + tmp28
    tmp32 = tmp30 + tmp31
    tmp34 = tmp32 + tmp33
    tmp36 = tmp34 + tmp35
    tmp37 = tmp36 / tmp7
    tmp38 = tmp37 * tmp37
    tmp39 = tmp29 + tmp38
    tl.store(out_ptr0 + x0, tmp39, xmask)


@triton.jit
def triton_poi_fused_clamp_min_div_linalg_vector_norm_mean_mul_repeat_3(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
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * (x1 // 4)), xmask)
    tmp1 = tl.load(in_ptr0 + 4 * (x1 // 4), xmask, eviction_policy='evict_last'
        )
    tmp3 = tl.load(in_ptr0 + (1 + 4 * (x1 // 4)), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (2 + 4 * (x1 // 4)), xmask, eviction_policy=
        'evict_last')
    tmp9 = tl.load(in_ptr0 + (3 + 4 * (x1 // 4)), xmask, eviction_policy=
        'evict_last')
    tmp16 = tl.load(in_ptr0 + (x0 + 16 * (x1 % 4)), xmask)
    tmp17 = tl.load(in_ptr0 + (4 + x0 + 16 * (x1 % 4)), xmask)
    tmp19 = tl.load(in_ptr0 + (8 + x0 + 16 * (x1 % 4)), xmask)
    tmp21 = tl.load(in_ptr0 + (12 + x0 + 16 * (x1 % 4)), xmask)
    tmp25 = tl.load(in_ptr1 + 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 = 1e-08
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp0 / tmp14
    tmp18 = tmp16 + tmp17
    tmp20 = tmp18 + tmp19
    tmp22 = tmp20 + tmp21
    tmp23 = 4.0
    tmp24 = tmp22 / tmp23
    tmp26 = libdevice.sqrt(tmp25)
    tmp27 = triton_helpers.maximum(tmp26, tmp13)
    tmp28 = tmp24 / tmp27
    tmp29 = tmp15 * tmp28
    tl.store(out_ptr0 + x2, tmp29, xmask)


@triton.jit
def triton_poi_fused_sum_4(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')
    tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tl.store(out_ptr0 + x0, tmp6, xmask)


@triton.jit
def triton_poi_fused_index_put_5(in_ptr0, 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
    x2 = xindex
    x0 = xindex % 4
    tmp11 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp14 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp0 = x1
    tmp1 = tl.full([1], 2, tl.int64)
    tmp2 = tmp0 < tmp1
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.full([1], 0, tl.int64)
    tmp6 = tl.where(tmp4, tmp5, tmp3)
    tmp7 = tl.full([1], 3, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tl.where(tmp8, tmp1, tmp7)
    tmp10 = tl.where(tmp2, tmp6, tmp9)
    tmp13 = tmp11 + tmp12
    tmp15 = tmp13 + tmp14
    tmp17 = tmp15 + tmp16
    tl.store(out_ptr0 + (4 * x0 + 17 * tmp10), tmp17, xmask)


@triton.jit
def triton_per_fused_add_exp_index_log_mul_sub_sum_6(in_ptr0, in_ptr1,
    in_ptr2, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 16
    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)
    r1 = rindex // 4
    r0 = rindex % 4
    r2 = rindex
    tmp11 = tl.load(in_ptr0 + 0)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK])
    tmp17 = tl.load(in_ptr2 + 0)
    tmp18 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK])
    tmp20 = tl.load(in_ptr1 + 4 * r2, None, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr1 + (1 + 4 * r2), None, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr1 + (2 + 4 * r2), None, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr1 + (3 + 4 * r2), None, eviction_policy='evict_last')
    tmp0 = r1
    tmp1 = tl.full([1, 1], 2, tl.int64)
    tmp2 = tmp0 < tmp1
    tmp3 = tl.full([1, 1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.full([1, 1], 0, tl.int64)
    tmp6 = tl.where(tmp4, tmp5, tmp3)
    tmp7 = tl.full([1, 1], 3, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tl.where(tmp8, tmp1, tmp7)
    tmp10 = tl.where(tmp2, tmp6, tmp9)
    tmp13 = tl.load(in_ptr1 + (4 * r0 + 17 * tmp10), None, eviction_policy=
        'evict_last')
    tmp14 = 1e-06
    tmp15 = tmp13 + tmp14
    tmp16 = tmp12 * tmp15
    tmp19 = tmp16 + tmp18
    tmp21 = tmp20 + tmp14
    tmp22 = tmp12 * tmp21
    tmp23 = tmp22 + tmp18
    tmp24 = tl_math.exp(tmp23)
    tmp26 = tmp25 + tmp14
    tmp27 = tmp12 * tmp26
    tmp28 = tmp27 + tmp18
    tmp29 = tl_math.exp(tmp28)
    tmp30 = tmp24 + tmp29
    tmp32 = tmp31 + tmp14
    tmp33 = tmp12 * tmp32
    tmp34 = tmp33 + tmp18
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp30 + tmp35
    tmp38 = tmp37 + tmp14
    tmp39 = tmp12 * tmp38
    tmp40 = tmp39 + tmp18
    tmp41 = tl_math.exp(tmp40)
    tmp42 = tmp36 + tmp41
    tmp43 = tmp42 + tmp14
    tmp44 = tl_math.log(tmp43)
    tmp45 = tmp19 - tmp44
    tmp46 = -1.0
    tmp47 = tmp45 * tmp46
    tmp48 = tl.broadcast_to(tmp47, [XBLOCK, RBLOCK])
    tmp50 = tl.sum(tmp48, 1)[:, None]
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp50, None)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (), ())
    assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_3, (), ())
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_div_sub_0[grid(64)](primals_2, buf0, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_1[grid(64)](
            primals_2, buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf2 = reinterpret_tensor(buf0, (64, 1), (1, 64), 0)
        del buf0
        triton_poi_fused_linalg_vector_norm_mean_repeat_2[grid(64)](primals_2,
            buf2, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        triton_poi_fused_clamp_min_div_linalg_vector_norm_mean_mul_repeat_3[
            grid(256)](primals_2, buf2, buf3, 256, XBLOCK=256, num_warps=4,
            num_stages=1)
        del primals_2
        buf4 = reinterpret_tensor(buf2, (64,), (1,), 0)
        del buf2
        triton_poi_fused_sum_4[grid(64)](buf3, buf4, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf3
        triton_poi_fused_index_put_5[grid(16)](buf1, buf4, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf1
        buf7 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_add_exp_index_log_mul_sub_sum_6[grid(1)](primals_1,
            buf4, primals_3, buf7, 1, 16, XBLOCK=1, num_warps=2, num_stages=1)
    return buf7, primals_1, primals_3, reinterpret_tensor(buf4, (4, 4, 4),
        (16, 4, 1), 0)


def calc_loss(sim_matrix):
    same_idx = list(range(sim_matrix.size(0)))
    pos = sim_matrix[same_idx, :, same_idx]
    neg = (torch.exp(sim_matrix).sum(dim=2) + 1e-06).log_()
    per_embedding_loss = -1 * (pos - neg)
    loss = per_embedding_loss.sum()
    return loss, per_embedding_loss


def get_centroids(embeddings):
    centroids = embeddings.mean(dim=1)
    return centroids


def get_utterance_centroids(embeddings):
    """
    Returns the centroids for each utterance of a speaker, where
    the utterance centroid is the speaker centroid without considering
    this utterance
    Shape of embeddings should be:
        (speaker_ct, utterance_per_speaker_ct, embedding_size)
    """
    sum_centroids = embeddings.sum(dim=1)
    sum_centroids = sum_centroids.reshape(sum_centroids.shape[0], 1,
        sum_centroids.shape[-1])
    num_utterances = embeddings.shape[1] - 1
    centroids = (sum_centroids - embeddings) / num_utterances
    return centroids


def get_cossim(embeddings, centroids):
    num_utterances = embeddings.shape[1]
    utterance_centroids = get_utterance_centroids(embeddings)
    utterance_centroids_flat = utterance_centroids.view(utterance_centroids
        .shape[0] * utterance_centroids.shape[1], -1)
    embeddings_flat = embeddings.view(embeddings.shape[0] * num_utterances, -1)
    cos_same = F.cosine_similarity(embeddings_flat, utterance_centroids_flat)
    centroids_expand = centroids.repeat((num_utterances * embeddings.shape[
        0], 1))
    embeddings_expand = embeddings_flat.unsqueeze(1).repeat(1, embeddings.
        shape[0], 1)
    embeddings_expand = embeddings_expand.view(embeddings_expand.shape[0] *
        embeddings_expand.shape[1], embeddings_expand.shape[-1])
    cos_diff = F.cosine_similarity(embeddings_expand, centroids_expand)
    cos_diff = cos_diff.view(embeddings.size(0), num_utterances, centroids.
        size(0))
    same_idx = list(range(embeddings.size(0)))
    cos_diff[same_idx, :, same_idx] = cos_same.view(embeddings.shape[0],
        num_utterances)
    cos_diff = cos_diff + 1e-06
    return cos_diff


class GE2ELossNew(nn.Module):

    def __init__(self, device):
        super(GE2ELossNew, self).__init__()
        self.w = nn.Parameter(torch.tensor(10.0), requires_grad=True)
        self.b = nn.Parameter(torch.tensor(-5.0), requires_grad=True)
        self.device = device

    def forward(self, input_0):
        primals_1 = self.w
        primals_3 = self.b
        primals_2 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.090162