import torch
from torch import nn


class dce_loss(nn.Module):

    def __init__(self, n_classes, feat_dim, init_weight=True):
        super(dce_loss, self).__init__()
        self.n_classes = n_classes
        self.feat_dim = feat_dim
        self.centers = nn.Parameter(torch.randn(self.feat_dim, self.
            n_classes), requires_grad=True)
        nn.init.kaiming_normal_(self.centers)

    def forward(self, x):
        features_square = torch.sum(torch.pow(x, 2), 1, keepdim=True)
        centers_square = torch.sum(torch.pow(self.centers, 2), 0, keepdim=True)
        features_into_centers = 2 * torch.matmul(x, self.centers)
        dist = features_square + centers_square - features_into_centers
        return self.centers, -dist


def get_inputs():
    return [torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {'n_classes': 4, 'feat_dim': 4}]

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 import 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_add_pow_sum_0(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 // 16
    x3 = xindex % 16
    x0 = xindex % 4
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + (x3 + 64 * x2), xmask)
    tmp2 = tl.load(in_ptr0 + (16 + x3 + 64 * x2), xmask)
    tmp5 = tl.load(in_ptr0 + (32 + x3 + 64 * x2), xmask)
    tmp8 = tl.load(in_ptr0 + (48 + x3 + 64 * x2), xmask)
    tmp11 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr1 + (12 + 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
    tmp12 = tmp11 * tmp11
    tmp14 = tmp13 * tmp13
    tmp15 = tmp12 + tmp14
    tmp17 = tmp16 * tmp16
    tmp18 = tmp15 + tmp17
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp22 = tmp10 + tmp21
    tl.store(out_ptr0 + x5, tmp22, xmask)


@triton.jit
def triton_poi_fused_add_mul_neg_pow_sub_sum_1(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
    x0 = xindex % 16
    x2 = xindex // 64
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_out_ptr0 + x3, xmask)
    tmp2 = 2.0
    tmp3 = tmp1 * tmp2
    tmp4 = tmp0 - tmp3
    tmp5 = -tmp4
    tl.store(in_out_ptr0 + x3, tmp5, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 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((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0),
            primals_2, out=buf0)
        buf1 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_pow_sum_0[grid(64)](primals_1, primals_2, buf1,
            64, XBLOCK=64, num_warps=1, num_stages=1)
        buf2 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf0
        triton_poi_fused_add_mul_neg_pow_sub_sum_1[grid(256)](buf2, buf1, 
            256, XBLOCK=256, num_warps=4, num_stages=1)
        del buf1
    return buf2, primals_2, reinterpret_tensor(primals_1, (4, 64), (1, 4), 0)


class dce_lossNew(nn.Module):

    def __init__(self, n_classes, feat_dim, init_weight=True):
        super(dce_lossNew, self).__init__()
        self.n_classes = n_classes
        self.feat_dim = feat_dim
        self.centers = nn.Parameter(torch.randn(self.feat_dim, self.
            n_classes), requires_grad=True)
        nn.init.kaiming_normal_(self.centers)

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