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._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
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_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 4 % 4
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x3, tmp2, xmask)


@triton.jit
def triton_poi_fused_convolution_div_sigmoid_1(in_out_ptr0, in_ptr0,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 4 % 16
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.sigmoid(tmp2)
    tmp4 = 0.5
    tmp5 = tmp3 * tmp4
    tl.store(in_out_ptr0 + x3, tmp2, xmask)
    tl.store(out_ptr0 + x3, tmp5, xmask)


@triton.jit
def triton_poi_fused_abs_div_mul_neg_sub_2(out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4 % 4
    x2 = xindex // 16
    x3 = xindex
    tmp0 = -1 * tl_math.abs(x0 + -1 * x1) + -1 * x2 * tl_math.abs(x0 + -1 * x1)
    tmp1 = tmp0.to(tl.float32)
    tmp2 = 0.5
    tmp3 = tmp1 * tmp2
    tl.store(out_ptr0 + x3, tmp3, xmask)


@triton.jit
def triton_poi_fused_arange_eye_3(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
    x0 = xindex % 4
    x2 = xindex
    tmp0 = x1
    tmp1 = x0
    tmp2 = tmp0 == tmp1
    tl.store(out_ptr0 + x2, tmp2, xmask)


@triton.jit
def triton_poi_fused__softmax_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 4
    xnumel = 16
    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
    y0 = yindex
    x1 = xindex
    tmp0 = tl.load(in_ptr0 + y0, ymask, eviction_policy='evict_last').to(tl
        .int1)
    tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + (y0 + 4 * x1), xmask & ymask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr3 + (x1 + 64 * y0), xmask & ymask, eviction_policy
        ='evict_last')
    tmp10 = tl.load(in_ptr0 + (4 + y0), ymask, eviction_policy='evict_last'
        ).to(tl.int1)
    tmp11 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp14 = tl.load(in_ptr3 + (16 + x1 + 64 * y0), xmask & ymask,
        eviction_policy='evict_last')
    tmp18 = tl.load(in_ptr0 + (8 + y0), ymask, eviction_policy='evict_last'
        ).to(tl.int1)
    tmp19 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr3 + (32 + x1 + 64 * y0), xmask & ymask,
        eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr0 + (12 + y0), ymask, eviction_policy='evict_last'
        ).to(tl.int1)
    tmp27 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp30 = tl.load(in_ptr3 + (48 + x1 + 64 * y0), xmask & ymask,
        eviction_policy='evict_last')
    tmp3 = tmp1 * tmp2
    tmp4 = 1.0
    tmp5 = tmp3 * tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = -100.0
    tmp9 = tl.where(tmp0, tmp8, tmp7)
    tmp12 = tmp11 * tmp2
    tmp13 = tmp12 * tmp4
    tmp15 = tmp13 + tmp14
    tmp16 = tl.where(tmp10, tmp8, tmp15)
    tmp17 = triton_helpers.maximum(tmp9, tmp16)
    tmp20 = tmp19 * tmp2
    tmp21 = tmp20 * tmp4
    tmp23 = tmp21 + tmp22
    tmp24 = tl.where(tmp18, tmp8, tmp23)
    tmp25 = triton_helpers.maximum(tmp17, tmp24)
    tmp28 = tmp27 * tmp2
    tmp29 = tmp28 * tmp4
    tmp31 = tmp29 + tmp30
    tmp32 = tl.where(tmp26, tmp8, tmp31)
    tmp33 = triton_helpers.maximum(tmp25, tmp32)
    tl.store(out_ptr0 + (x1 + 16 * y0), tmp33, xmask & ymask)


@triton.jit
def triton_poi_fused__softmax_5(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    xnumel = 16
    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
    x3 = xindex
    x2 = xindex // 4
    y0 = yindex
    x1 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last').to(tl
        .int1)
    tmp1 = tl.load(in_ptr1 + (x2 + 4 * y0), xmask & ymask, eviction_policy=
        'evict_last')
    tmp2 = tl.load(in_ptr2 + (x1 + 4 * y0), xmask & ymask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr3 + (y0 + 16 * x2 + 64 * x1), xmask & ymask)
    tmp10 = tl.load(in_ptr4 + (y0 + 16 * x1), xmask & ymask,
        eviction_policy='evict_last')
    tmp3 = tmp1 * tmp2
    tmp4 = 1.0
    tmp5 = tmp3 * tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = -100.0
    tmp9 = tl.where(tmp0, tmp8, tmp7)
    tmp11 = tmp9 - tmp10
    tmp12 = tl_math.exp(tmp11)
    tl.store(out_ptr0 + (x3 + 16 * y0), tmp12, xmask & ymask)


@triton.jit
def triton_poi_fused__softmax_6(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x0 = xindex % 4
    x2 = xindex // 16
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tl.load(in_ptr0 + (4 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (8 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (12 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x3, tmp8, xmask)


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


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 1), (4, 1, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4, 4, 1), (4, 1, 1))
    assert_size_stride(primals_5, (4,), (1,))
    assert_size_stride(primals_6, (16, 4, 1), (4, 1, 1))
    assert_size_stride(primals_7, (16,), (1,))
    assert_size_stride(primals_8, (4, 4, 1), (4, 1, 1))
    assert_size_stride(primals_9, (4,), (1,))
    assert_size_stride(primals_10, (4, 4, 1), (4, 1, 1))
    assert_size_stride(primals_11, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1,),
            padding=(0,), dilation=(1,), transposed=False, output_padding=(
            0,), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 4), (16, 4, 1))
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_poi_fused_convolution_0[grid(64)](buf1, primals_3, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_3
        buf2 = extern_kernels.convolution(primals_1, primals_4, stride=(1,),
            padding=(0,), dilation=(1,), transposed=False, output_padding=(
            0,), groups=1, bias=None)
        assert_size_stride(buf2, (4, 4, 4), (16, 4, 1))
        buf3 = buf2
        del buf2
        triton_poi_fused_convolution_0[grid(64)](buf3, primals_5, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_5
        buf4 = extern_kernels.convolution(primals_1, primals_6, stride=(1,),
            padding=(0,), dilation=(1,), transposed=False, output_padding=(
            0,), groups=1, bias=None)
        assert_size_stride(buf4, (4, 16, 4), (64, 4, 1))
        buf5 = buf4
        del buf4
        buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_convolution_div_sigmoid_1[grid(256)](buf5,
            primals_7, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_7
        buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_abs_div_mul_neg_sub_2[grid(64)](buf6, 64, XBLOCK=
            64, num_warps=1, num_stages=1)
        buf8 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf6, (4, 4, 4), (1, 4, 16), 
            0), reinterpret_tensor(buf7, (4, 4, 16), (1, 4, 16), 0), out=buf8)
        buf9 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused_arange_eye_3[grid(16)](buf9, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf10 = empty_strided_cuda((4, 4, 1, 4), (4, 1, 64, 16), torch.float32)
        triton_poi_fused__softmax_4[grid(4, 16)](buf9, buf3, buf1, buf8,
            buf10, 4, 16, XBLOCK=16, YBLOCK=4, num_warps=1, num_stages=1)
        buf11 = buf7
        del buf7
        triton_poi_fused__softmax_5[grid(16, 16)](buf9, buf3, buf1, buf8,
            buf10, buf11, 16, 16, XBLOCK=16, YBLOCK=16, num_warps=4,
            num_stages=1)
        buf12 = reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf8
        triton_poi_fused__softmax_6[grid(256)](buf11, buf12, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        del buf11
        buf13 = extern_kernels.convolution(primals_1, primals_8, stride=(1,
            ), padding=(0,), dilation=(1,), transposed=False,
            output_padding=(0,), groups=1, bias=None)
        assert_size_stride(buf13, (4, 4, 4), (16, 4, 1))
        buf14 = buf13
        del buf13
        triton_poi_fused_convolution_0[grid(64)](buf14, primals_9, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_9
        buf15 = reinterpret_tensor(buf10, (16, 4, 1), (4, 1, 1), 0)
        del buf10
        extern_kernels.bmm(reinterpret_tensor(buf12, (16, 4, 4), (16, 1, 4),
            0), reinterpret_tensor(buf14, (16, 4, 1), (4, 1, 0), 0), out=buf15)
        buf16 = extern_kernels.convolution(reinterpret_tensor(buf15, (4, 4,
            4), (16, 4, 1), 0), primals_10, stride=(1,), padding=(0,),
            dilation=(1,), transposed=False, output_padding=(0,), groups=1,
            bias=None)
        assert_size_stride(buf16, (4, 4, 4), (16, 4, 1))
        buf17 = buf16
        del buf16
        triton_poi_fused_add_convolution_7[grid(64)](buf17, primals_1,
            primals_11, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del primals_11
    return (buf17, primals_1, primals_2, primals_4, primals_6, primals_8,
        primals_10, buf1, buf3, buf5, buf9, buf12, reinterpret_tensor(buf15,
        (4, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf14, (16, 1, 4), (4,
        4, 1), 0), reinterpret_tensor(buf6, (4, 4, 4), (1, 16, 4), 0))


class LocalStateNew(nn.Module):
    """Local state allows to have attention based only on data (no positional embedding),
    but while setting a constraint on the time window (e.g. decaying penalty term).

    Also a failed experiments with trying to provide some frequency based attention.
    """

    def __init__(self, channels: 'int', heads: 'int'=4, nfreqs: 'int'=0,
        ndecay: 'int'=4):
        super().__init__()
        assert channels % heads == 0, (channels, heads)
        self.heads = heads
        self.nfreqs = nfreqs
        self.ndecay = ndecay
        self.content = nn.Conv1d(channels, channels, 1)
        self.query = nn.Conv1d(channels, channels, 1)
        self.key = nn.Conv1d(channels, channels, 1)
        if nfreqs:
            self.query_freqs = nn.Conv1d(channels, heads * nfreqs, 1)
        if ndecay:
            self.query_decay = nn.Conv1d(channels, heads * ndecay, 1)
            self.query_decay.weight.data *= 0.01
            assert self.query_decay.bias is not None
            self.query_decay.bias.data[:] = -2
        self.proj = nn.Conv1d(channels + heads * nfreqs, channels, 1)

    def forward(self, input_0):
        primals_2 = self.content.weight
        primals_3 = self.content.bias
        primals_4 = self.query.weight
        primals_5 = self.query.bias
        primals_8 = self.key.weight
        primals_9 = self.key.bias
        primals_6 = self.query_decay.weight
        primals_7 = self.query_decay.bias
        primals_10 = self.proj.weight
        primals_11 = self.proj.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11])
        return output[0]


Measured latency_ms: 0.015