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
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_diagonal_copy_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 5 * x0, xmask, eviction_policy='evict_last')
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_div_mul_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 3
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (1 + x0), xmask)
    tmp3 = tl.load(in_ptr1 + (1 + x0), xmask)
    tmp1 = 2.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 / tmp3
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused_div_mul_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 2
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (6 + x0), xmask)
    tmp3 = tl.load(in_ptr1 + (2 + x0), xmask)
    tmp1 = 2.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 / tmp3
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused_mul_sub_3(in_ptr0, in_ptr1, 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
    tmp10 = tl.load(in_ptr0 + x2, xmask)
    tmp0 = x1
    tmp1 = tl.full([1], 1, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.load(in_ptr0 + x2, tmp2 & xmask, other=0.0)
    tmp4 = tl.load(in_ptr1 + (-1 + x1), tmp2 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp5 = tl.load(in_ptr0 + x0, tmp2 & xmask, eviction_policy='evict_last',
        other=0.0)
    tmp6 = tmp4 * tmp5
    tmp7 = tmp3 - tmp6
    tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype)
    tmp9 = tl.where(tmp2, tmp7, tmp8)
    tmp11 = tl.where(tmp2, tmp9, tmp10)
    tl.store(out_ptr0 + x2, tmp11, xmask)


@triton.jit
def triton_poi_fused_div_mul_4(in_ptr0, in_ptr1, 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.load(in_ptr0 + 11)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp4 = tl.load(in_ptr1 + 3)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
    tmp2 = 2.0
    tmp3 = tmp1 * tmp2
    tmp6 = tmp3 / tmp5
    tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp6, None)


@triton.jit
def triton_poi_fused_mul_sub_5(in_ptr0, in_ptr1, 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
    tmp10 = tl.load(in_ptr0 + x2, xmask)
    tmp0 = x1
    tmp1 = tl.full([1], 2, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.load(in_ptr0 + x2, tmp2 & xmask, other=0.0)
    tmp4 = tl.load(in_ptr1 + (-2 + x1), tmp2 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp5 = tl.load(in_ptr0 + (4 + x0), tmp2 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp4 * tmp5
    tmp7 = tmp3 - tmp6
    tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype)
    tmp9 = tl.where(tmp2, tmp7, tmp8)
    tmp11 = tl.where(tmp2, tmp9, tmp10)
    tl.store(out_ptr0 + x2, tmp11, xmask)


@triton.jit
def triton_poi_fused_mul_sub_6(in_ptr0, in_ptr1, 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
    tmp4 = tl.load(in_ptr1 + 0)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
    tmp11 = tl.load(in_ptr0 + x2, xmask)
    tmp0 = x1
    tmp1 = tl.full([1], 3, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.load(in_ptr0 + x2, tmp2 & xmask, other=0.0)
    tmp6 = tl.load(in_ptr0 + (8 + x0), tmp2 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp7 = tmp5 * tmp6
    tmp8 = tmp3 - tmp7
    tmp9 = tl.full(tmp8.shape, 0.0, tmp8.dtype)
    tmp10 = tl.where(tmp2, tmp8, tmp9)
    tmp12 = tl.where(tmp2, tmp10, tmp11)
    tl.store(out_ptr0 + x2, tmp12, xmask)


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


@triton.jit
def triton_poi_fused_add_sub_8(in_out_ptr0, in_ptr0, in_ptr1, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_out_ptr0 + x2, xmask)
    tmp3 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 - tmp1
    tmp4 = tmp2 + tmp3
    tl.store(in_out_ptr0 + x2, tmp4, xmask)


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, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (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_1, reinterpret_tensor(primals_1, (4, 4),
            (1, 4), 0), out=buf0)
        buf1 = empty_strided_cuda((4,), (1,), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_diagonal_copy_0[grid(4)](buf0, buf1, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((3,), (1,), torch.float32)
        triton_poi_fused_div_mul_1[grid(3)](buf0, buf1, buf2, 3, XBLOCK=4,
            num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((2,), (1,), torch.float32)
        triton_poi_fused_div_mul_2[grid(2)](buf0, buf1, buf3, 2, XBLOCK=2,
            num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_mul_sub_3[grid(16)](primals_1, buf2, buf4, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf5 = empty_strided_cuda((1,), (1,), torch.float32)
        triton_poi_fused_div_mul_4[grid(1)](buf0, buf1, buf5, 1, XBLOCK=1,
            num_warps=1, num_stages=1)
        buf6 = buf0
        del buf0
        triton_poi_fused_mul_sub_5[grid(16)](buf4, buf3, buf6, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_mul_sub_6[grid(16)](buf6, buf5, buf7, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        buf8 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_2, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf8)
        buf9 = reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf8
        triton_poi_fused_div_mul_7[grid(256)](buf9, buf1, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf10 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf9, (64, 4), (4, 1), 0),
            buf7, out=buf10)
        buf11 = reinterpret_tensor(buf10, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf10
        triton_poi_fused_add_sub_8[grid(256)](buf11, primals_2, primals_3, 
            256, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_3
    return buf11, buf7, buf1, primals_1, buf1, buf2, buf3, reinterpret_tensor(
        buf4, (4,), (1,), 4), buf5, reinterpret_tensor(buf6, (4,), (1,), 8
        ), reinterpret_tensor(primals_2, (64, 4), (4, 1), 0
        ), buf9, reinterpret_tensor(buf7, (4, 4), (1, 4), 0)


class OrthogonalHouseholderAlternativeNew(nn.Module):

    def __init__(self, sz, bias=True):
        super(OrthogonalHouseholderAlternativeNew, self).__init__()
        self.sz = sz
        self.bias = bias
        self.A = nn.Parameter(torch.empty((sz, sz)))
        self.b = nn.Parameter(torch.empty(sz)) if bias else 0.0
        self.reset_parameters()

    def reset_parameters(self):
        with torch.no_grad():
            self.A.normal_(0, math.sqrt(2 / self.sz))
            if self.bias:
                self.b.fill_(0.0)

    def _forward_precalc(self):
        B = self.A @ self.A.T
        self.diag = torch.diag(B)
        self.p = self.A.clone()
        for i in range(self.sz - 1):
            self.p[i + 1:] = self.p[i + 1:].clone() - (2 * B[i, i + 1:] /
                self.diag[i + 1:]).unsqueeze(1) * self.p[i].clone()

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


Measured latency_ms: 0.143374