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
import math
from torch import nn
from torch import autograd
from torch.nn import init
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_full_0(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    tmp0 = tl.full([1], 0.0, tl.float64)
    tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp0, None)


@triton.jit
def triton_poi_fused_full_1(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    tmp0 = tl.full([1], 1e-15, tl.float64)
    tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp0, None)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(primals_3, reinterpret_tensor(primals_1, (4, 4),
            (1, 4), 0), out=buf0)
        buf1 = empty_strided_cuda((), (), torch.float64)
        get_raw_stream(0)
        triton_poi_fused_full_0[grid(1)](buf1, 1, XBLOCK=1, num_warps=1,
            num_stages=1)
        buf2 = empty_strided_cuda((), (), torch.float64)
        triton_poi_fused_full_1[grid(1)](buf2, 1, XBLOCK=1, num_warps=1,
            num_stages=1)
        buf3 = torch.ops.aten.linalg_pinv.atol_rtol_tensor(primals_3, atol=
            buf1, rtol=buf2)
        del buf1
        del buf2
        del primals_3
        buf4 = buf3
        del buf3
    return buf0, primals_1, buf0, buf4


class ExtremeLinearFunction(autograd.Function):

    @staticmethod
    def forward(ctx, input, forward_weight, feedback_weight):
        ctx.save_for_backward(input, forward_weight, feedback_weight)
        output = input.mm(forward_weight.t())
        ctx.output = output
        return output

    @staticmethod
    def backward(ctx, grad_output):
        input, forward_weight, _feedback_weight = ctx.saved_tensors
        grad_input = grad_weight = None
        if ctx.needs_input_grad[1]:
            inv_x = torch.pinverse(input)
            error_weight = inv_x.mm(ctx.output - grad_output)
            grad_weight = forward_weight - error_weight.t()
        if ctx.needs_input_grad[0]:
            grad_input = grad_output.mm(forward_weight)
        return grad_input, grad_weight, None


class ExtremeLinearNew(nn.Module):

    def __init__(self, input_features, output_features):
        super(ExtremeLinearNew, self).__init__()
        self.input_features = input_features
        self.output_features = output_features
        self.forward_weight = nn.Parameter(torch.Tensor(output_features,
            input_features))
        self.feedback_weight = nn.Parameter(torch.Tensor(output_features,
            input_features))
        self.reset_parameters()

    def reset_parameters(self) ->None:
        init.kaiming_uniform_(self.forward_weight, a=math.sqrt(5))
        init.kaiming_uniform_(self.feedback_weight, a=math.sqrt(5))

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


Measured latency_ms: 0.190433