import torch
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.triton_helpers import libdevice
import torch.nn.functional as F
import torch.nn as nn
import torch.functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused_add_mul_native_layer_norm_sigmoid_0(in_ptr0, in_ptr1,
    in_ptr2, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + 0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
    tmp9 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp11 = tl.load(in_ptr2 + 1)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK])
    tmp18 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp19 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp20 = tl.load(in_ptr2 + 2)
    tmp21 = tl.broadcast_to(tmp20, [XBLOCK])
    tmp27 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp28 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp29 = tl.load(in_ptr2 + 3)
    tmp30 = tl.broadcast_to(tmp29, [XBLOCK])
    tmp4 = tl.sigmoid(tmp3)
    tmp5 = tmp1 * tmp4
    tmp6 = 2.0
    tmp7 = tmp5 * tmp6
    tmp8 = tmp0 + tmp7
    tmp13 = tl.sigmoid(tmp12)
    tmp14 = tmp10 * tmp13
    tmp15 = tmp14 * tmp6
    tmp16 = tmp9 + tmp15
    tmp17 = tmp8 + tmp16
    tmp22 = tl.sigmoid(tmp21)
    tmp23 = tmp19 * tmp22
    tmp24 = tmp23 * tmp6
    tmp25 = tmp18 + tmp24
    tmp26 = tmp17 + tmp25
    tmp31 = tl.sigmoid(tmp30)
    tmp32 = tmp28 * tmp31
    tmp33 = tmp32 * tmp6
    tmp34 = tmp27 + tmp33
    tmp35 = tmp26 + tmp34
    tmp36 = 4.0
    tmp37 = tmp35 / tmp36
    tmp38 = tmp8 - tmp37
    tmp39 = tmp38 * tmp38
    tmp40 = tmp16 - tmp37
    tmp41 = tmp40 * tmp40
    tmp42 = tmp39 + tmp41
    tmp43 = tmp25 - tmp37
    tmp44 = tmp43 * tmp43
    tmp45 = tmp42 + tmp44
    tmp46 = tmp34 - tmp37
    tmp47 = tmp46 * tmp46
    tmp48 = tmp45 + tmp47
    tmp49 = tmp48 / tmp36
    tl.store(out_ptr0 + x0, tmp37, xmask)
    tl.store(out_ptr1 + x0, tmp49, xmask)


@triton.jit
def triton_poi_fused_add_mul_native_layer_norm_sigmoid_1(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, out_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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp2 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr6 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.sigmoid(tmp2)
    tmp4 = tmp1 * tmp3
    tmp5 = 2.0
    tmp6 = tmp4 * tmp5
    tmp7 = tmp0 + tmp6
    tmp9 = tmp7 - tmp8
    tmp11 = 1e-05
    tmp12 = tmp10 + tmp11
    tmp13 = libdevice.rsqrt(tmp12)
    tmp14 = tmp9 * tmp13
    tmp16 = tmp14 * tmp15
    tmp18 = tmp16 + tmp17
    tl.store(out_ptr0 + x2, tmp18, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4,), (1,))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_mul_native_layer_norm_sigmoid_0[grid(64)](
            primals_3, primals_2, primals_1, buf0, buf1, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_mul_native_layer_norm_sigmoid_1[grid(256)](
            primals_3, primals_2, primals_1, buf0, buf1, primals_4,
            primals_5, buf2, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del buf0
        del buf1
        del primals_5
    return buf2, primals_1, primals_2, primals_3, primals_4


class TimeDistributedInterpolation(nn.Module):

    def __init__(self, output_size: 'int', batch_first: 'bool'=False,
        trainable: 'bool'=False):
        super().__init__()
        self.output_size = output_size
        self.batch_first = batch_first
        self.trainable = trainable
        if self.trainable:
            self.mask = nn.Parameter(torch.zeros(self.output_size, dtype=
                torch.float32))
            self.gate = nn.Sigmoid()

    def interpolate(self, x):
        upsampled = F.interpolate(x.unsqueeze(1), self.output_size, mode=
            'linear', align_corners=True).squeeze(1)
        if self.trainable:
            upsampled = upsampled * self.gate(self.mask.unsqueeze(0)) * 2.0
        return upsampled

    def forward(self, x):
        if len(x.size()) <= 2:
            return self.interpolate(x)
        x_reshape = x.contiguous().view(-1, x.size(-1))
        y = self.interpolate(x_reshape)
        if self.batch_first:
            y = y.contiguous().view(x.size(0), -1, y.size(-1))
        else:
            y = y.view(-1, x.size(1), y.size(-1))
        return y


class AddNormNew(nn.Module):

    def __init__(self, input_size: 'int', skip_size: 'int'=None,
        trainable_add: 'bool'=True):
        super().__init__()
        self.input_size = input_size
        self.trainable_add = trainable_add
        self.skip_size = skip_size or input_size
        if self.input_size != self.skip_size:
            self.resample = TimeDistributedInterpolation(self.input_size,
                batch_first=True, trainable=False)
        if self.trainable_add:
            self.mask = nn.Parameter(torch.zeros(self.input_size, dtype=
                torch.float))
            self.gate = nn.Sigmoid()
        self.norm = nn.LayerNorm(self.input_size)

    def forward(self, input_0, input_1):
        primals_1 = self.mask
        primals_4 = self.norm.weight
        primals_5 = self.norm.bias
        primals_2 = input_0
        primals_3 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0]


Measured latency_ms: 0.036962