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
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_max_sub_0(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
    tl.store(out_ptr0 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_add_mul_pow_relu_sum_1(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')
    tmp10 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = -3.0
    tmp3 = tmp1 * tmp2
    tmp4 = 1.0
    tmp5 = tmp3 + tmp4
    tmp6 = tl.full([1], 0, tl.int32)
    tmp7 = triton_helpers.maximum(tmp6, tmp5)
    tmp8 = -0.3333333333333333
    tmp9 = libdevice.pow(tmp7, tmp8)
    tmp11 = tmp10 * tmp2
    tmp12 = tmp11 + tmp4
    tmp13 = triton_helpers.maximum(tmp6, tmp12)
    tmp14 = libdevice.pow(tmp13, tmp8)
    tmp15 = tmp9 + tmp14
    tmp17 = tmp16 * tmp2
    tmp18 = tmp17 + tmp4
    tmp19 = triton_helpers.maximum(tmp6, tmp18)
    tmp20 = libdevice.pow(tmp19, tmp8)
    tmp21 = tmp15 + tmp20
    tmp23 = tmp22 * tmp2
    tmp24 = tmp23 + tmp4
    tmp25 = triton_helpers.maximum(tmp6, tmp24)
    tmp26 = libdevice.pow(tmp25, tmp8)
    tmp27 = tmp21 + tmp26
    tmp28 = tl.full([1], 1, tl.int32)
    tmp29 = tmp28 / tmp27
    tmp30 = tmp29 * tmp29
    tmp31 = tmp30 * tmp29
    tmp32 = tmp0 * tmp31
    tl.store(out_ptr0 + x2, tmp32, xmask)


@triton.jit
def triton_poi_fused_add_mul_pow_relu_sum_2(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_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = -3.0
    tmp3 = tmp1 * tmp2
    tmp4 = 1.0
    tmp5 = tmp3 + tmp4
    tmp6 = tl.full([1], 0, tl.int32)
    tmp7 = triton_helpers.maximum(tmp6, tmp5)
    tmp8 = -0.3333333333333333
    tmp9 = libdevice.pow(tmp7, tmp8)
    tmp11 = tmp10 * tmp2
    tmp12 = tmp11 + tmp4
    tmp13 = triton_helpers.maximum(tmp6, tmp12)
    tmp14 = libdevice.pow(tmp13, tmp8)
    tmp15 = tmp9 + tmp14
    tmp17 = tmp16 * tmp2
    tmp18 = tmp17 + tmp4
    tmp19 = triton_helpers.maximum(tmp6, tmp18)
    tmp20 = libdevice.pow(tmp19, tmp8)
    tmp21 = tmp15 + tmp20
    tmp23 = tmp22 * tmp2
    tmp24 = tmp23 + tmp4
    tmp25 = triton_helpers.maximum(tmp6, tmp24)
    tmp26 = libdevice.pow(tmp25, tmp8)
    tmp27 = tmp21 + tmp26
    tmp28 = tl.full([1], 1, tl.int32)
    tmp29 = tmp28 / tmp27
    tmp30 = tmp29 * tmp29
    tmp31 = tmp30 * tmp29
    tmp32 = tmp0 * tmp31
    tl.store(out_ptr0 + x2, tmp32, xmask)


@triton.jit
def triton_poi_fused_add_mul_pow_relu_sum_3(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = -3.0
    tmp3 = tmp1 * tmp2
    tmp4 = 1.0
    tmp5 = tmp3 + tmp4
    tmp6 = tl.full([1], 0, tl.int32)
    tmp7 = triton_helpers.maximum(tmp6, tmp5)
    tmp8 = -0.3333333333333333
    tmp9 = libdevice.pow(tmp7, tmp8)
    tmp11 = tmp10 * tmp2
    tmp12 = tmp11 + tmp4
    tmp13 = triton_helpers.maximum(tmp6, tmp12)
    tmp14 = libdevice.pow(tmp13, tmp8)
    tmp15 = tmp9 + tmp14
    tmp17 = tmp16 * tmp2
    tmp18 = tmp17 + tmp4
    tmp19 = triton_helpers.maximum(tmp6, tmp18)
    tmp20 = libdevice.pow(tmp19, tmp8)
    tmp21 = tmp15 + tmp20
    tmp23 = tmp22 * tmp2
    tmp24 = tmp23 + tmp4
    tmp25 = triton_helpers.maximum(tmp6, tmp24)
    tmp26 = libdevice.pow(tmp25, tmp8)
    tmp27 = tmp21 + tmp26
    tmp28 = tl.full([1], 1, tl.int32)
    tmp29 = tmp28 / tmp27
    tmp30 = tmp29 * tmp29
    tmp31 = tmp30 * tmp29
    tmp32 = tmp0 * tmp31
    tl.store(in_out_ptr0 + x2, tmp32, xmask)


@triton.jit
def triton_poi_fused_add_div_max_mul_neg_pow_reciprocal_relu_sub_sum_4(
    in_out_ptr0, in_ptr0, in_ptr1, 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')
    tmp9 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp21 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp36 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp39 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp41 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp1 = -3.0
    tmp2 = tmp0 * tmp1
    tmp3 = 1.0
    tmp4 = tmp2 + tmp3
    tmp5 = tl.full([1], 0, tl.int32)
    tmp6 = triton_helpers.maximum(tmp5, tmp4)
    tmp7 = -0.3333333333333333
    tmp8 = libdevice.pow(tmp6, tmp7)
    tmp10 = tmp9 * tmp1
    tmp11 = tmp10 + tmp3
    tmp12 = triton_helpers.maximum(tmp5, tmp11)
    tmp13 = libdevice.pow(tmp12, tmp7)
    tmp14 = tmp8 + tmp13
    tmp16 = tmp15 * tmp1
    tmp17 = tmp16 + tmp3
    tmp18 = triton_helpers.maximum(tmp5, tmp17)
    tmp19 = libdevice.pow(tmp18, tmp7)
    tmp20 = tmp14 + tmp19
    tmp22 = tmp21 * tmp1
    tmp23 = tmp22 + tmp3
    tmp24 = triton_helpers.maximum(tmp5, tmp23)
    tmp25 = libdevice.pow(tmp24, tmp7)
    tmp26 = tmp20 + tmp25
    tmp27 = tl.full([1], 1, tl.int32)
    tmp28 = tmp27 / tmp26
    tmp29 = tmp28 * tmp3
    tmp30 = tmp27 / tmp29
    tmp31 = tmp30 * tmp30
    tmp32 = tmp31 * tmp30
    tmp33 = tmp32 - tmp3
    tmp34 = tmp33 * tmp7
    tmp35 = -tmp34
    tmp38 = triton_helpers.maximum(tmp36, tmp37)
    tmp40 = triton_helpers.maximum(tmp38, tmp39)
    tmp42 = triton_helpers.maximum(tmp40, tmp41)
    tmp43 = tmp35 + tmp42
    tl.store(in_out_ptr0 + x0, tmp43, xmask)


@triton.jit
def triton_poi_fused_add_div_max_mul_neg_pow_relu_sub_5(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp12 = tl.load(in_ptr1 + x2, xmask)
    tmp13 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp1 = 1e-10
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 1, tl.int32)
    tmp4 = tmp3 / tmp2
    tmp5 = tmp4 * tmp4
    tmp6 = tmp5 * tmp4
    tmp7 = 1.0
    tmp8 = tmp6 - tmp7
    tmp9 = -0.3333333333333333
    tmp10 = tmp8 * tmp9
    tmp11 = tmp0 * tmp10
    tmp14 = tmp12 - tmp13
    tmp15 = -3.0
    tmp16 = tmp14 * tmp15
    tmp17 = tmp16 + tmp7
    tmp18 = tl.full([1], 0, tl.int32)
    tmp19 = triton_helpers.maximum(tmp18, tmp17)
    tmp20 = libdevice.pow(tmp19, tmp9)
    tmp21 = tmp3 / tmp20
    tmp22 = tmp21 * tmp21
    tmp23 = tmp22 * tmp21
    tmp24 = tmp23 - tmp7
    tmp25 = tmp24 * tmp9
    tmp26 = tmp0 * tmp25
    tmp27 = tmp11 - tmp26
    tmp28 = tmp3 / tmp0
    tmp29 = tmp28 * tmp28
    tmp30 = -0.5
    tmp31 = tmp29 * tmp30
    tmp32 = tmp27 - tmp31
    tl.store(out_ptr0 + x2, tmp32, xmask)


@triton.jit
def triton_per_fused_add_div_max_mean_mul_neg_pow_relu_sub_sum_6(in_out_ptr0,
    in_out_ptr1, 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 + 4 * r0, None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last')
    tmp2 = tl.load(in_out_ptr0 + r0, None)
    tmp18 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr1 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp30 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr1 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp42 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp43 = tl.load(in_ptr1 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp3 = tmp1 - tmp2
    tmp4 = -3.0
    tmp5 = tmp3 * tmp4
    tmp6 = 1.0
    tmp7 = tmp5 + tmp6
    tmp8 = tl.full([1, 1], 0, tl.int32)
    tmp9 = triton_helpers.maximum(tmp8, tmp7)
    tmp10 = -0.3333333333333333
    tmp11 = libdevice.pow(tmp9, tmp10)
    tmp12 = tl.full([1, 1], 1, tl.int32)
    tmp13 = tmp12 / tmp11
    tmp14 = tmp13 * tmp13
    tmp15 = -0.5
    tmp16 = tmp14 * tmp15
    tmp17 = tmp0 + tmp16
    tmp20 = tmp19 - tmp2
    tmp21 = tmp20 * tmp4
    tmp22 = tmp21 + tmp6
    tmp23 = triton_helpers.maximum(tmp8, tmp22)
    tmp24 = libdevice.pow(tmp23, tmp10)
    tmp25 = tmp12 / tmp24
    tmp26 = tmp25 * tmp25
    tmp27 = tmp26 * tmp15
    tmp28 = tmp18 + tmp27
    tmp29 = tmp17 + tmp28
    tmp32 = tmp31 - tmp2
    tmp33 = tmp32 * tmp4
    tmp34 = tmp33 + tmp6
    tmp35 = triton_helpers.maximum(tmp8, tmp34)
    tmp36 = libdevice.pow(tmp35, tmp10)
    tmp37 = tmp12 / tmp36
    tmp38 = tmp37 * tmp37
    tmp39 = tmp38 * tmp15
    tmp40 = tmp30 + tmp39
    tmp41 = tmp29 + tmp40
    tmp44 = tmp43 - tmp2
    tmp45 = tmp44 * tmp4
    tmp46 = tmp45 + tmp6
    tmp47 = triton_helpers.maximum(tmp8, tmp46)
    tmp48 = libdevice.pow(tmp47, tmp10)
    tmp49 = tmp12 / tmp48
    tmp50 = tmp49 * tmp49
    tmp51 = tmp50 * tmp15
    tmp52 = tmp42 + tmp51
    tmp53 = tmp41 + tmp52
    tmp54 = tl.broadcast_to(tmp53, [XBLOCK, RBLOCK])
    tmp56 = tl.sum(tmp54, 1)[:, None]
    tmp57 = 64.0
    tmp58 = tmp56 / tmp57
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp58, None)


def call(args):
    arg0_1, arg1_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, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_max_sub_0[grid(256)](arg0_1, buf0, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_mul_pow_relu_sum_1[grid(256)](buf0, buf1, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_mul_pow_relu_sum_2[grid(256)](buf0, buf1, buf2,
            256, XBLOCK=128, num_warps=4, num_stages=1)
        buf3 = buf1
        del buf1
        triton_poi_fused_add_mul_pow_relu_sum_2[grid(256)](buf0, buf2, buf3,
            256, XBLOCK=128, num_warps=4, num_stages=1)
        buf4 = buf2
        del buf2
        triton_poi_fused_add_mul_pow_relu_sum_2[grid(256)](buf0, buf3, buf4,
            256, XBLOCK=128, num_warps=4, num_stages=1)
        del buf3
        buf5 = buf0
        del buf0
        triton_poi_fused_add_mul_pow_relu_sum_3[grid(256)](buf5, buf4, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del buf4
        buf6 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        buf7 = buf6
        del buf6
        triton_poi_fused_add_div_max_mul_neg_pow_reciprocal_relu_sub_sum_4[grid
            (64)](buf7, buf5, arg0_1, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf8 = buf5
        del buf5
        triton_poi_fused_add_div_max_mul_neg_pow_relu_sub_5[grid(256)](arg1_1,
            arg0_1, buf7, buf8, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del arg1_1
        buf9 = reinterpret_tensor(buf7, (4, 4, 4), (16, 4, 1), 0)
        del buf7
        buf10 = empty_strided_cuda((), (), torch.float32)
        buf11 = buf10
        del buf10
        triton_per_fused_add_div_max_mean_mul_neg_pow_relu_sub_sum_6[grid(1)](
            buf9, buf11, buf8, arg0_1, 1, 64, XBLOCK=1, num_warps=2,
            num_stages=1)
        del arg0_1
        del buf8
        del buf9
    return buf11,


def log_t(u, t):
    """Compute log_t for `u'."""
    if t == 1.0:
        return u.log()
    else:
        return (u.pow(1.0 - t) - 1.0) / (1.0 - t)


def exp_t(u, t):
    """Compute exp_t for `u'."""
    if t == 1:
        return u.exp()
    else:
        return (1.0 + (1.0 - t) * u).relu().pow(1.0 / (1.0 - t))


def compute_normalization_binary_search(activations, t, num_iters):
    """Returns the normalization value for each example (t < 1.0).
    Args:
      activations: A multi-dimensional tensor with last dimension `num_classes`.
      t: Temperature 2 (< 1.0 for finite support).
      num_iters: Number of iterations to run the method.
    Return: A tensor of same rank as activation with the last dimension being 1.
    """
    mu, _ = torch.max(activations, -1, keepdim=True)
    normalized_activations = activations - mu
    effective_dim = torch.sum((normalized_activations > -1.0 / (1.0 - t)).
        to(torch.int32), dim=-1, keepdim=True)
    shape_partition = activations.shape[:-1] + (1,)
    lower = torch.zeros(shape_partition, dtype=activations.dtype, device=
        activations.device)
    upper = -log_t(1.0 / effective_dim, t) * torch.ones_like(lower)
    for _ in range(num_iters):
        logt_partition = (upper + lower) / 2.0
        sum_probs = torch.sum(exp_t(normalized_activations - logt_partition,
            t), dim=-1, keepdim=True)
        update = sum_probs < 1.0
        lower = torch.reshape(lower * update + (1.0 - update) *
            logt_partition, shape_partition)
        upper = torch.reshape(upper * (1.0 - update) + update *
            logt_partition, shape_partition)
    logt_partition = (upper + lower) / 2.0
    return logt_partition + mu


def compute_normalization_fixed_point(activations, t, num_iters):
    """Returns the normalization value for each example (t > 1.0).
    Args:
      activations: A multi-dimensional tensor with last dimension `num_classes`.
      t: Temperature 2 (> 1.0 for tail heaviness).
      num_iters: Number of iterations to run the method.
    Return: A tensor of same shape as activation with the last dimension being 1.
    """
    mu, _ = torch.max(activations, -1, keepdim=True)
    normalized_activations_step_0 = activations - mu
    normalized_activations = normalized_activations_step_0
    for _ in range(num_iters):
        logt_partition = torch.sum(exp_t(normalized_activations, t), -1,
            keepdim=True)
        normalized_activations = (normalized_activations_step_0 *
            logt_partition.pow(1.0 - t))
    logt_partition = torch.sum(exp_t(normalized_activations, t), -1,
        keepdim=True)
    normalization_constants = -log_t(1.0 / logt_partition, t) + mu
    return normalization_constants


def compute_normalization(activations, t, num_iters=5):
    """Returns the normalization value for each example. 
    Backward pass is implemented.
    Args:
      activations: A multi-dimensional tensor with last dimension `num_classes`.
      t: Temperature 2 (> 1.0 for tail heaviness, < 1.0 for finite support).
      num_iters: Number of iterations to run the method.
    Return: A tensor of same rank as activation with the last dimension being 1.
    """
    return ComputeNormalization.apply(activations, t, num_iters)


def tempered_softmax(activations, t, num_iters=5):
    """Tempered softmax function.
    Args:
      activations: A multi-dimensional tensor with last dimension `num_classes`.
      t: Temperature > 1.0.
      num_iters: Number of iterations to run the method.
    Returns:
      A probabilities tensor.
    """
    if t == 1.0:
        return activations.softmax(dim=-1)
    normalization_constants = compute_normalization(activations, t, num_iters)
    return exp_t(activations - normalization_constants, t)


def bi_tempered_logistic_loss(activations, labels, t1, t2, label_smoothing=
    0.0, num_iters=5, reduction='mean'):
    """Bi-Tempered Logistic Loss.
    Args:
      activations: A multi-dimensional tensor with last dimension `num_classes`.
      labels: A tensor with shape and dtype as activations (onehot), 
        or a long tensor of one dimension less than activations (pytorch standard)
      t1: Temperature 1 (< 1.0 for boundedness).
      t2: Temperature 2 (> 1.0 for tail heaviness, < 1.0 for finite support).
      label_smoothing: Label smoothing parameter between [0, 1). Default 0.0.
      num_iters: Number of iterations to run the method. Default 5.
      reduction: ``'none'`` | ``'mean'`` | ``'sum'``. Default ``'mean'``.
        ``'none'``: No reduction is applied, return shape is shape of
        activations without the last dimension.
        ``'mean'``: Loss is averaged over minibatch. Return shape (1,)
        ``'sum'``: Loss is summed over minibatch. Return shape (1,)
    Returns:
      A loss tensor.
    """
    if len(labels.shape) < len(activations.shape):
        labels_onehot = torch.zeros_like(activations)
        labels_onehot.scatter_(1, labels[..., None], 1)
    else:
        labels_onehot = labels
    if label_smoothing > 0:
        num_classes = labels_onehot.shape[-1]
        labels_onehot = (1 - label_smoothing * num_classes / (num_classes - 1)
            ) * labels_onehot + label_smoothing / (num_classes - 1)
    probabilities = tempered_softmax(activations, t2, num_iters)
    loss_values = labels_onehot * log_t(labels_onehot + 1e-10, t1
        ) - labels_onehot * log_t(probabilities, t1) - labels_onehot.pow(
        2.0 - t1) / (2.0 - t1) + probabilities.pow(2.0 - t1) / (2.0 - t1)
    loss_values = loss_values.sum(dim=-1)
    if reduction == 'none':
        return loss_values
    if reduction == 'sum':
        return loss_values.sum()
    if reduction == 'mean':
        return loss_values.mean()


class ComputeNormalization(torch.autograd.Function):
    """
    Class implementing custom backward pass for compute_normalization. See compute_normalization.
    """

    @staticmethod
    def forward(ctx, activations, t, num_iters):
        if t < 1.0:
            normalization_constants = compute_normalization_binary_search(
                activations, t, num_iters)
        else:
            normalization_constants = compute_normalization_fixed_point(
                activations, t, num_iters)
        ctx.save_for_backward(activations, normalization_constants)
        ctx.t = t
        return normalization_constants

    @staticmethod
    def backward(ctx, grad_output):
        activations, normalization_constants = ctx.saved_tensors
        t = ctx.t
        normalized_activations = activations - normalization_constants
        probabilities = exp_t(normalized_activations, t)
        escorts = probabilities.pow(t)
        escorts = escorts / escorts.sum(dim=-1, keepdim=True)
        grad_input = escorts * grad_output
        return grad_input, None, None


class BiTemperedLogisticLossNew(nn.Module):

    def __init__(self, t1, t2, smoothing=0.0):
        super(BiTemperedLogisticLossNew, self).__init__()
        self.t1 = t1
        self.t2 = t2
        self.smoothing = smoothing

    def forward(self, input_0, input_1):
        arg0_1 = input_0
        arg1_1 = input_1
        output = call([arg0_1, arg1_1])
        return output[0]


Measured latency_ms: 0.115765