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 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_stack_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp15 = tl.load(in_ptr1 + 0)
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK])
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = 3 + x0
    tmp7 = tl.full([1], 3, tl.int64)
    tmp8 = tmp5 < tmp7
    tmp9 = tmp8 & tmp4
    tmp10 = tl.load(in_ptr0 + (3 + x0), tmp9 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp11 = tmp5 >= tmp7
    tmp12 = tmp5 < tmp3
    tmp13 = tmp11 & tmp12
    tmp14 = tmp13 & tmp4
    tmp17 = 1.0
    tmp18 = tmp16 + tmp17
    tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype)
    tmp20 = tl.where(tmp14, tmp18, tmp19)
    tmp21 = tmp5 >= tmp3
    tl.full([1], 7, tl.int64)
    tmp24 = tmp21 & tmp4
    tmp25 = tl.load(in_ptr2 + (-1 + x0), tmp24 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp26 = tl.where(tmp13, tmp20, tmp25)
    tmp27 = tl.where(tmp8, tmp10, tmp26)
    tmp28 = tl.full(tmp27.shape, 0.0, tmp27.dtype)
    tmp29 = tl.where(tmp4, tmp27, tmp28)
    tmp30 = tmp0 >= tmp3
    tmp31 = tl.full([1], 8, tl.int64)
    tmp32 = tmp0 < tmp31
    tmp33 = tmp30 & tmp32
    tmp34 = 2 + (-4 + x0)
    tmp36 = tmp34 < tmp7
    tmp37 = tmp36 & tmp33
    tmp38 = tl.load(in_ptr0 + (2 + (-4 + x0)), tmp37 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp39 = tmp34 >= tmp7
    tmp40 = tmp34 < tmp3
    tmp41 = tmp39 & tmp40
    tmp42 = tmp41 & tmp33
    tmp43 = tl.where(tmp42, tmp18, tmp19)
    tmp44 = tmp34 >= tmp3
    tmp46 = tmp44 & tmp33
    tmp47 = tl.load(in_ptr2 + (-2 + (-4 + x0)), tmp46 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp48 = tl.where(tmp41, tmp43, tmp47)
    tmp49 = tl.where(tmp36, tmp38, tmp48)
    tmp50 = tl.full(tmp49.shape, 0.0, tmp49.dtype)
    tmp51 = tl.where(tmp33, tmp49, tmp50)
    tmp52 = tmp0 >= tmp31
    tmp53 = tl.full([1], 12, tl.int64)
    tmp54 = tmp0 < tmp53
    tmp55 = tmp52 & tmp54
    tmp56 = 1 + (-8 + x0)
    tmp58 = tmp56 < tmp7
    tmp59 = tmp58 & tmp55
    tmp60 = tl.load(in_ptr0 + (1 + (-8 + x0)), tmp59 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp61 = tmp56 >= tmp7
    tmp62 = tmp56 < tmp3
    tmp63 = tmp61 & tmp62
    tmp64 = tmp63 & tmp55
    tmp65 = tl.where(tmp64, tmp18, tmp19)
    tmp66 = tmp56 >= tmp3
    tmp68 = tmp66 & tmp55
    tmp69 = tl.load(in_ptr2 + (-3 + (-8 + x0)), tmp68 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp70 = tl.where(tmp63, tmp65, tmp69)
    tmp71 = tl.where(tmp58, tmp60, tmp70)
    tmp72 = tl.full(tmp71.shape, 0.0, tmp71.dtype)
    tmp73 = tl.where(tmp55, tmp71, tmp72)
    tmp74 = tmp0 >= tmp53
    tl.full([1], 16, tl.int64)
    tmp77 = -12 + x0
    tmp79 = tmp77 < tmp7
    tmp80 = tmp79 & tmp74
    tmp81 = tl.load(in_ptr0 + (-12 + x0), tmp80 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp82 = tmp77 >= tmp7
    tmp83 = tmp77 < tmp3
    tmp84 = tmp82 & tmp83
    tmp85 = tmp84 & tmp74
    tmp86 = tl.where(tmp85, tmp18, tmp19)
    tmp87 = tmp77 >= tmp3
    tmp89 = tmp87 & tmp74
    tmp90 = tl.load(in_ptr2 + (-4 + (-12 + x0)), tmp89 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp91 = tl.where(tmp84, tmp86, tmp90)
    tmp92 = tl.where(tmp79, tmp81, tmp91)
    tmp93 = tl.full(tmp92.shape, 0.0, tmp92.dtype)
    tmp94 = tl.where(tmp74, tmp92, tmp93)
    tmp95 = tl.where(tmp55, tmp73, tmp94)
    tmp96 = tl.where(tmp33, tmp51, tmp95)
    tmp97 = tl.where(tmp4, tmp29, tmp96)
    tl.store(out_ptr0 + x0, tmp97, xmask)


@triton.jit
def triton_poi_fused_clone_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 64
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y0 = yindex % 4
    y1 = yindex // 4
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (1,), (1,))
    assert_size_stride(primals_2, (3,), (1,))
    assert_size_stride(primals_3, (3,), (1,))
    assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16,), (1,), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_stack_0[grid(16)](primals_3, primals_1, primals_2,
            buf0, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del primals_1
        del primals_2
        del primals_3
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_clone_1[grid(64, 4)](primals_4, buf1, 64, 4,
            XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1)
        del primals_4
        buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf1, (64, 4), (4, 1), 0),
            reinterpret_tensor(buf0, (4, 4), (1, 4), 0), out=buf2)
        del buf0
        buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_clone_1[grid(64, 4)](buf2, buf3, 64, 4, XBLOCK=4,
            YBLOCK=32, num_warps=4, num_stages=1)
        del buf2
    return buf3, reinterpret_tensor(buf1, (64, 4), (4, 1), 0)


def expand_toeplitz(diag, lower_diags, upper_diags):
    pattern = torch.cat([upper_diags, diag, lower_diags], 0)
    d = lower_diags.size(0)
    columns = []
    for i in range(d + 1):
        columns.append(pattern[d - i:d - i + d + 1])
    return torch.stack(columns, 0)


class ToeplitzBlockNew(nn.Module):

    def __init__(self, dim):
        super().__init__()
        self.diag = nn.Parameter(torch.Tensor([0]))
        self.lower_diags = nn.Parameter(torch.Tensor(dim - 1).zero_())
        self.upper_diags = nn.Parameter(torch.Tensor(dim - 1).zero_())

    def diagonals(self):
        return [self.diag + 1, self.lower_diags, self.upper_diags]

    def forward(self, input_0):
        primals_1 = self.diag
        primals_2 = self.lower_diags
        primals_3 = self.upper_diags
        primals_4 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.051285