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.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


@triton.jit
def triton_poi_fused_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 320
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16 % 5
    x0 = xindex % 16
    x2 = xindex // 80
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 64 * x2), tmp4 & xmask, other=0.0)
    tmp6 = 2.0
    tmp7 = tmp5 * tmp6
    tmp8 = tl.load(in_ptr0 + (x0 + 64 * x2), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp9 = tmp8 * tmp8
    tmp10 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp11 = tmp10 * tmp10
    tmp12 = tmp9 + tmp11
    tmp13 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp14 = tmp13 * tmp13
    tmp15 = tmp12 + tmp14
    tmp16 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp17 = tmp16 * tmp16
    tmp18 = tmp15 + tmp17
    tmp19 = libdevice.sqrt(tmp18)
    tmp20 = tmp19 * tmp19
    tmp21 = 1.0
    tmp22 = tmp20 + tmp21
    tmp23 = tmp7 / tmp22
    tmp24 = tl.full(tmp23.shape, 0.0, tmp23.dtype)
    tmp25 = tl.where(tmp4, tmp23, tmp24)
    tmp26 = tmp0 >= tmp3
    tl.full([1], 5, tl.int64)
    tmp29 = tl.load(in_ptr0 + (x0 + 64 * x2), tmp26 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp30 = tmp29 * tmp29
    tmp31 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), tmp26 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp32 = tmp31 * tmp31
    tmp33 = tmp30 + tmp32
    tmp34 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), tmp26 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp35 = tmp34 * tmp34
    tmp36 = tmp33 + tmp35
    tmp37 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), tmp26 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp38 = tmp37 * tmp37
    tmp39 = tmp36 + tmp38
    tmp40 = libdevice.sqrt(tmp39)
    tmp41 = tmp40 * tmp40
    tmp42 = tmp41 - tmp21
    tmp43 = tmp41 + tmp21
    tmp44 = tmp42 / tmp43
    tmp45 = tl.full(tmp44.shape, 0.0, tmp44.dtype)
    tmp46 = tl.where(tmp26, tmp44, tmp45)
    tmp47 = tl.where(tmp4, tmp25, tmp46)
    tl.store(out_ptr0 + x3, tmp47, xmask)


@triton.jit
def triton_per_fused_acos_add_div_linalg_vector_norm_mean_mul_pow_1(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 + (64 + r0 + 80 * r1), None)
    tmp3 = tl.load(in_ptr0 + (r0 + 80 * r1), None)
    tmp5 = tl.load(in_ptr0 + (16 + r0 + 80 * r1), None)
    tmp8 = tl.load(in_ptr0 + (32 + r0 + 80 * r1), None)
    tmp11 = tl.load(in_ptr0 + (48 + r0 + 80 * r1), None)
    tmp1 = 2.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3 * tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp12 = tmp11 * tmp11
    tmp13 = tmp10 + tmp12
    tmp14 = tmp0 * tmp0
    tmp15 = tmp13 + tmp14
    tmp16 = libdevice.sqrt(tmp15)
    tmp17 = tmp16 * tmp16
    tmp18 = 1.0
    tmp19 = tmp17 + tmp18
    tmp20 = tmp2 / tmp19
    tmp21 = libdevice.acos(tmp20)
    tmp22 = tl.broadcast_to(tmp21, [XBLOCK, RBLOCK])
    tmp24 = tl.sum(tmp22, 1)[:, None]
    tmp25 = 64.0
    tmp26 = tmp24 / tmp25
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp26, None)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 5, 4, 4), (80, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(320)](arg0_1, buf0, 320, XBLOCK=256,
            num_warps=4, num_stages=1)
        del arg0_1
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = buf1
        del buf1
        triton_per_fused_acos_add_div_linalg_vector_norm_mean_mul_pow_1[grid(1)
            ](buf2, buf0, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del buf0
    return buf2,


class HyperSphereLossNew(nn.Module):

    def project_to_hypersphere(self, v):
        v_norm = torch.norm(v, dim=1, keepdim=True) ** 2
        a = 2 * v / (v_norm + 1)
        b = (v_norm - 1) / (v_norm + 1)
        return torch.cat([a, b], dim=1)

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


Measured latency_ms: 0.025719