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 libdevice, 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_repeat_0(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
    x0 = xindex % 4
    x2 = xindex // 16
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tl.store(out_ptr0 + x3, tmp0, xmask)


@triton.jit
def triton_poi_fused_mul_tanh_1(in_ptr0, in_ptr1, 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
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp2 = tl.load(in_ptr1 + x0, xmask)
    tmp1 = libdevice.tanh(tmp0)
    tmp3 = libdevice.tanh(tmp2)
    tmp4 = tmp1 * tmp3
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 16
    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)
    tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3 * tmp1
    tmp6 = tmp5 * tmp1
    tmp7 = triton_helpers.maximum(tmp4, tmp6)
    tmp9 = tmp8 * tmp1
    tmp10 = triton_helpers.maximum(tmp7, tmp9)
    tmp12 = tmp11 * tmp1
    tmp13 = triton_helpers.maximum(tmp10, tmp12)
    tmp14 = tmp2 - tmp13
    tmp15 = tmp14 * tmp1
    tmp16 = tl_math.exp(tmp15)
    tl.store(out_ptr0 + x2, tmp16, xmask)


@triton.jit
def triton_poi_fused__softmax_add_ones_like_3(in_ptr0, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 16
    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
    tmp9 = 1.0
    tmp10 = tmp8 + tmp9
    tl.store(out_ptr0 + x2, tmp10, xmask)


@triton.jit
def triton_poi_fused__softmax_add_mul_ones_like_repeat_sum_4(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
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp4 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp8 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp12 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tl.store(out_ptr0 + x2, tmp14, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (1, 4), (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_repeat_0[grid(64)](primals_2, buf0, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del primals_2
        buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1)
        del primals_3
        buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf2)
        del primals_4
        buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_mul_tanh_1[grid(64)](buf1, buf2, buf3, 64, XBLOCK=
            64, num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf3, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_5, (4, 1), (1, 4), 0), out=buf4)
        buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_2[grid(16)](buf4, buf5, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_add_ones_like_3[grid(16)](buf5, buf6, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf7 = reinterpret_tensor(buf5, (4, 4), (4, 1), 0)
        del buf5
        triton_poi_fused__softmax_add_mul_ones_like_repeat_sum_4[grid(16)](
            primals_1, buf6, buf7, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del buf6
    return buf7, primals_1, reinterpret_tensor(buf0, (16, 4), (4, 1), 0
        ), buf1, buf2, reinterpret_tensor(buf3, (16, 4), (4, 1), 0
        ), buf4, primals_5


class SeqAttendImgFusion(nn.Module):

    def __init__(self, seq_dim, img_dim, hidden_dim, att_dropout,
        att_scale_factor=1, **kwargs):
        super(SeqAttendImgFusion, self).__init__()
        self.SeqTrans = nn.Linear(seq_dim, hidden_dim, bias=False)
        self.ImgTrans = nn.Linear(img_dim, hidden_dim, bias=False)
        self.WeightTrans = nn.Linear(hidden_dim, 1, bias=False)
        self.AttentionScaleFactor = att_scale_factor
        if att_dropout is None:
            self.Dropout = nn.Identity()
        else:
            self.Dropout = nn.Dropout(att_dropout)

    def _attend(self, seq_features, img_features, res_att=False):
        assert len(img_features.size()
            ) == 3, f'[SeqAttendImgFusion] Input image shape must have 3 dimensions (batch, patch(w*h), feature(channel)), but got {img_features.size()}'
        img_feature_dim = img_features.size(-1)
        patch_size = img_features.size(1)
        seq_repeated_features = seq_features.unsqueeze(1).repeat((1,
            patch_size, 1))
        attend_output = torch.tanh(self.SeqTrans(seq_repeated_features)
            ) * torch.tanh(self.ImgTrans(img_features))
        attend_output = self.Dropout(attend_output)
        attend_alpha = torch.softmax(self.AttentionScaleFactor * self.
            WeightTrans(attend_output), dim=1)
        if res_att:
            res_ones = torch.ones_like(attend_alpha)
            attend_alpha = attend_alpha + res_ones
        attend_alpha = attend_alpha.repeat((1, 1, img_feature_dim))
        attended_img = torch.sum(img_features * attend_alpha, dim=1)
        return attended_img

    def forward(self, seq_features, img_features, **kwargs):
        raise NotImplementedError


class SeqAttendImgResAttOnlyFusionNew(SeqAttendImgFusion):

    def __init__(self, seq_dim, img_dim, hidden_dim, att_dropout, **kwargs):
        super(SeqAttendImgResAttOnlyFusionNew, self).__init__(seq_dim,
            img_dim, hidden_dim, att_dropout, **kwargs)

    def forward(self, input_0, input_1):
        primals_2 = self.SeqTrans.weight
        primals_3 = self.ImgTrans.weight
        primals_5 = self.WeightTrans.weight
        primals_4 = input_0
        primals_1 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0]


Measured latency_ms: 0.014727