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
import torch.nn as nn
from torch.nn import LayerNorm
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_ptr0, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    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)


@triton.jit
def triton_poi_fused_convolution_1(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__softmax_2(in_ptr0, 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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x2, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_3(in_ptr0, 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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x2, tmp8, 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, 4), (16, 4, 1))
    assert_size_stride(primals_5, (4, 4, 1), (4, 1, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (4, 4, 4), (16, 4, 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 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_convolution_0[grid(16, 4)](primals_1, buf0, 16, 4,
            XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1,),
            padding=(0,), dilation=(1,), transposed=False, output_padding=(
            0,), groups=1, bias=None)
        assert_size_stride(buf1, (4, 4, 4), (16, 4, 1))
        buf2 = buf0
        del buf0
        triton_poi_fused_convolution_0[grid(16, 4)](primals_4, buf2, 16, 4,
            XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        buf3 = extern_kernels.convolution(buf2, primals_5, stride=(1,),
            padding=(0,), dilation=(1,), transposed=False, output_padding=(
            0,), groups=1, bias=None)
        assert_size_stride(buf3, (4, 4, 4), (16, 4, 1))
        buf4 = buf2
        del buf2
        triton_poi_fused_convolution_0[grid(16, 4)](primals_7, buf4, 16, 4,
            XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        buf5 = extern_kernels.convolution(buf4, primals_8, stride=(1,),
            padding=(0,), dilation=(1,), transposed=False, output_padding=(
            0,), groups=1, bias=None)
        assert_size_stride(buf5, (4, 4, 4), (16, 4, 1))
        buf6 = buf1
        del buf1
        triton_poi_fused_convolution_1[grid(64)](buf6, primals_3, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_3
        buf7 = buf3
        del buf3
        triton_poi_fused_convolution_1[grid(64)](buf7, primals_6, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_6
        buf8 = buf4
        del buf4
        extern_kernels.bmm(reinterpret_tensor(buf6, (4, 4, 4), (16, 1, 4), 
            0), buf7, out=buf8)
        buf9 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_2[grid(64)](buf8, buf9, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf10 = buf8
        del buf8
        triton_poi_fused__softmax_3[grid(64)](buf9, buf10, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf11 = buf5
        del buf5
        triton_poi_fused_convolution_1[grid(64)](buf11, primals_9, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_9
        buf12 = buf9
        del buf9
        extern_kernels.bmm(buf10, reinterpret_tensor(buf11, (4, 4, 4), (16,
            1, 4), 0), out=buf12)
        buf13 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_convolution_0[grid(16, 4)](buf12, buf13, 16, 4,
            XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        buf14 = extern_kernels.convolution(buf13, primals_10, stride=(1,),
            padding=(0,), dilation=(1,), transposed=False, output_padding=(
            0,), groups=1, bias=None)
        assert_size_stride(buf14, (4, 4, 4), (16, 4, 1))
        del buf13
        buf15 = buf14
        del buf14
        triton_poi_fused_convolution_1[grid(64)](buf15, primals_11, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_11
    return reinterpret_tensor(buf15, (4, 4, 4), (16, 1, 4), 0
        ), buf10, primals_2, primals_5, primals_8, primals_10, reinterpret_tensor(
        primals_1, (4, 4, 4), (16, 1, 4), 0), reinterpret_tensor(primals_4,
        (4, 4, 4), (16, 1, 4), 0), reinterpret_tensor(primals_7, (4, 4, 4),
        (16, 1, 4), 0), buf10, reinterpret_tensor(buf12, (4, 4, 4), (16, 1,
        4), 0), buf11, buf6, reinterpret_tensor(buf7, (4, 4, 4), (16, 1, 4), 0)


def scaled_dot_attention(q, k, v, mask=None, noise=0, dropout=lambda x: x):
    """
    :param q: queries, (batch, time1, channels1)
    :param k: keys, (batch, time2, channels1)
    :param v: values, (batch, time2, channels2)
    :param mask: boolean mask, (batch, time1, time2)
    :param dropout: a dropout function - this allows keeping dropout as a module -> better control when training/eval
    :return: (batch, time1, channels2), (batch, time1, time2)
    """
    weights = torch.matmul(q, k.transpose(2, 1))
    if mask is not None:
        weights = weights.masked_fill(~mask, float('-inf'))
    if noise:
        weights += noise * torch.randn(weights.shape)
    weights = torch.softmax(weights, dim=-1)
    weights = dropout(weights)
    result = torch.matmul(weights, v)
    return result, weights


def mask(x, lengths, dim=-1):
    assert dim != 0, 'Masking not available for batch dimension'
    assert len(lengths) == x.shape[0
        ], 'Lengths must contain as many elements as there are items in the batch'
    lengths = torch.as_tensor(lengths)
    to_expand = [1] * (x.ndim - 1) + [-1]
    mask = torch.arange(x.shape[dim]).expand(to_expand).transpose(dim, -1
        ).expand(x.shape)
    mask = mask < lengths.expand(to_expand).transpose(0, -1)
    return mask


class Conv1d(nn.Conv1d):
    """A wrapper around nn.Conv1d, that works on (batch, time, channels)"""

    def __init__(self, in_channels, out_channels, kernel_size=1, stride=1,
        dilation=1, groups=1, bias=True, padding=0):
        super(Conv1d, self).__init__(in_channels=in_channels, out_channels=
            out_channels, kernel_size=kernel_size, stride=stride, dilation=
            dilation, groups=groups, bias=bias, padding=padding)

    def forward(self, x):
        return super().forward(x.transpose(2, 1)).transpose(2, 1)


class ScaledDotAttentionNew(nn.Module):

    def __init__(self, in_channels, hidden_channels, out_channels, noise=0,
        normalize=False, dropout=False):
        super(ScaledDotAttentionNew, self).__init__()
        self.noise = noise
        self.dropout = torch.nn.Dropout(p=dropout)
        self.normalize = normalize
        self.fc_query = Conv1d(in_channels, hidden_channels)
        self.fc_keys = Conv1d(in_channels, hidden_channels)
        if normalize:
            self.qnorm = LayerNorm(in_channels)
            self.knorm = LayerNorm(in_channels)
        self.fc_keys.weight = torch.nn.Parameter(self.fc_query.weight.clone())
        self.fc_keys.bias = torch.nn.Parameter(self.fc_query.bias.clone())
        self.fc_values = Conv1d(in_channels, hidden_channels)
        self.fc_out = Conv1d(hidden_channels, out_channels)

    def forward(self, input_0, input_1, input_2):
        primals_2 = self.fc_query.weight
        primals_3 = self.fc_query.bias
        primals_5 = self.fc_keys.weight
        primals_6 = self.fc_keys.bias
        primals_8 = self.fc_values.weight
        primals_9 = self.fc_values.bias
        primals_10 = self.fc_out.weight
        primals_11 = self.fc_out.bias
        primals_1 = input_0
        primals_4 = input_1
        primals_7 = input_2
        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], output[1]


Measured latency_ms: 0.013858