import torch
import torch.nn as nn
import torch.nn.functional as F


class StructuredAttention_bi(nn.Module):

    def __init__(self, dropout=0.1, scale=100):
        super(StructuredAttention_bi, self).__init__()
        self.dropout = dropout
        self.scale = scale

    def forward(self, C, Q, c_mask, q_mask):
        _bsz, _, _num_img, _num_region, _hsz = Q.shape
        S, S_mask = self.similarity(C, Q, c_mask, q_mask)
        S_c = F.softmax(S * self.scale, dim=-1)
        S_q = F.softmax(S * self.scale, dim=-2)
        S_c = S_c * S_mask
        S_q = S_q * S_mask
        A_c = torch.matmul(S_c, Q)
        A_q = torch.matmul(S_q.transpose(-2, -1), C)
        return A_c, A_q, S_mask, S_mask.transpose(-2, -1)

    def similarity(self, C, Q, c_mask, q_mask):
        C = F.dropout(F.normalize(C, p=2, dim=-1), p=self.dropout, training
            =self.training)
        Q = F.dropout(F.normalize(Q, p=2, dim=-1), p=self.dropout, training
            =self.training)
        S_mask = torch.matmul(c_mask.unsqueeze(-1), q_mask.unsqueeze(-2))
        S = torch.matmul(C, Q.transpose(-2, -1))
        masked_S = S - 10000000000.0 * (1 - S_mask)
        return masked_S, S_mask


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4, 4]), torch.
        rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {}]

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
import torch.nn as nn
import torch.nn.functional as F
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_div_0(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp2 = tmp1 * tmp1
    tmp4 = tmp3 * tmp3
    tmp5 = tmp2 + tmp4
    tmp7 = tmp6 * tmp6
    tmp8 = tmp5 + tmp7
    tmp10 = tmp9 * tmp9
    tmp11 = tmp8 + tmp10
    tmp12 = libdevice.sqrt(tmp11)
    tmp13 = 1e-12
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp0 / tmp14
    tl.store(out_ptr0 + x2, tmp15, xmask)


@triton.jit
def triton_poi_fused_clone_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 256
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tl.store(out_ptr0 + x2, tmp0, xmask)


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


@triton.jit
def triton_poi_fused__softmax_mul_rsub_sub_3(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    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')
    tmp8 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp23 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = 1.0
    tmp3 = tmp2 - tmp1
    tmp4 = 10000000000.0
    tmp5 = tmp3 * tmp4
    tmp6 = tmp0 - tmp5
    tmp7 = tmp6 * tmp2
    tmp10 = tmp2 - tmp9
    tmp11 = tmp10 * tmp4
    tmp12 = tmp8 - tmp11
    tmp13 = tmp12 * tmp2
    tmp14 = triton_helpers.maximum(tmp7, tmp13)
    tmp17 = tmp2 - tmp16
    tmp18 = tmp17 * tmp4
    tmp19 = tmp15 - tmp18
    tmp20 = tmp19 * tmp2
    tmp21 = triton_helpers.maximum(tmp14, tmp20)
    tmp24 = tmp2 - tmp23
    tmp25 = tmp24 * tmp4
    tmp26 = tmp22 - tmp25
    tmp27 = tmp26 * tmp2
    tmp28 = triton_helpers.maximum(tmp21, tmp27)
    tmp29 = tmp7 - tmp28
    tmp30 = 100.0
    tmp31 = tmp29 * tmp30
    tmp32 = tl_math.exp(tmp31)
    tmp33 = tmp13 - tmp28
    tmp34 = tmp33 * tmp30
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp32 + tmp35
    tmp37 = tmp20 - tmp28
    tmp38 = tmp37 * tmp30
    tmp39 = tl_math.exp(tmp38)
    tmp40 = tmp36 + tmp39
    tmp41 = tmp27 - tmp28
    tmp42 = tmp41 * tmp30
    tmp43 = tl_math.exp(tmp42)
    tmp44 = tmp40 + tmp43
    tl.store(out_ptr0 + x0, tmp28, xmask)
    tl.store(out_ptr1 + x0, tmp44, xmask)


@triton.jit
def triton_poi_fused__softmax_mul_rsub_sub_4(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    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 + (x0 + 16 * x1), xmask)
    tmp8 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp9 = tl.load(in_ptr1 + (4 + x0 + 16 * x1), xmask)
    tmp15 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp16 = tl.load(in_ptr1 + (8 + x0 + 16 * x1), xmask)
    tmp22 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp23 = tl.load(in_ptr1 + (12 + x0 + 16 * x1), xmask)
    tmp2 = 1.0
    tmp3 = tmp2 - tmp1
    tmp4 = 10000000000.0
    tmp5 = tmp3 * tmp4
    tmp6 = tmp0 - tmp5
    tmp7 = tmp6 * tmp2
    tmp10 = tmp2 - tmp9
    tmp11 = tmp10 * tmp4
    tmp12 = tmp8 - tmp11
    tmp13 = tmp12 * tmp2
    tmp14 = triton_helpers.maximum(tmp7, tmp13)
    tmp17 = tmp2 - tmp16
    tmp18 = tmp17 * tmp4
    tmp19 = tmp15 - tmp18
    tmp20 = tmp19 * tmp2
    tmp21 = triton_helpers.maximum(tmp14, tmp20)
    tmp24 = tmp2 - tmp23
    tmp25 = tmp24 * tmp4
    tmp26 = tmp22 - tmp25
    tmp27 = tmp26 * tmp2
    tmp28 = triton_helpers.maximum(tmp21, tmp27)
    tmp29 = tmp7 - tmp28
    tmp30 = 100.0
    tmp31 = tmp29 * tmp30
    tmp32 = tl_math.exp(tmp31)
    tmp33 = tmp13 - tmp28
    tmp34 = tmp33 * tmp30
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp32 + tmp35
    tmp37 = tmp20 - tmp28
    tmp38 = tmp37 * tmp30
    tmp39 = tl_math.exp(tmp38)
    tmp40 = tmp36 + tmp39
    tmp41 = tmp27 - tmp28
    tmp42 = tmp41 * tmp30
    tmp43 = tl_math.exp(tmp42)
    tmp44 = tmp40 + tmp43
    tl.store(out_ptr0 + x2, tmp28, xmask)
    tl.store(out_ptr1 + x2, tmp44, xmask)


@triton.jit
def triton_poi_fused__softmax_mul_rsub_sub_5(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, in_ptr4, in_ptr5, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x4 = xindex
    x1 = xindex // 4
    x0 = xindex % 4
    x3 = xindex // 16
    tmp0 = tl.load(in_ptr0 + x4, xmask)
    tmp1 = tl.load(in_ptr1 + x4, xmask)
    tmp8 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr4 + (x0 + 4 * x3), xmask, eviction_policy=
        'evict_last')
    tmp20 = tl.load(in_ptr5 + (x0 + 4 * x3), xmask, eviction_policy=
        'evict_last')
    tmp2 = 1.0
    tmp3 = tmp2 - tmp1
    tmp4 = 10000000000.0
    tmp5 = tmp3 * tmp4
    tmp6 = tmp0 - tmp5
    tmp7 = tmp6 * tmp2
    tmp9 = tmp7 - tmp8
    tmp10 = 100.0
    tmp11 = tmp9 * tmp10
    tmp12 = tl_math.exp(tmp11)
    tmp14 = tmp12 / tmp13
    tmp15 = tmp14 * tmp1
    tmp17 = tmp7 - tmp16
    tmp18 = tmp17 * tmp10
    tmp19 = tl_math.exp(tmp18)
    tmp21 = tmp19 / tmp20
    tmp22 = tmp21 * tmp1
    tl.store(out_ptr0 + x4, tmp15, xmask)
    tl.store(out_ptr1 + x4, tmp22, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4, 4), (256, 64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_div_0[grid(256)](arg1_1, buf0, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused_clone_1[grid(1024)](buf0, buf1, 1024, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused_div_2[grid(1024)](arg0_1, buf2, 1024, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf3 = empty_strided_cuda((64, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf1, (64, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(buf2, (64, 4, 4), (16, 1, 4), 0), out=buf3)
        buf4 = reinterpret_tensor(buf2, (64, 4, 4), (16, 4, 1), 0)
        del buf2
        extern_kernels.bmm(reinterpret_tensor(arg2_1, (64, 4, 1), (4, 1, 1),
            0), reinterpret_tensor(arg3_1, (64, 1, 4), (4, 4, 1), 0), out=buf4)
        del arg2_1
        del arg3_1
        buf5 = reinterpret_tensor(buf0, (4, 4, 4, 4, 1), (64, 16, 4, 1, 256), 0
            )
        del buf0
        buf6 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 256),
            torch.float32)
        triton_poi_fused__softmax_mul_rsub_sub_3[grid(256)](buf3, buf4,
            buf5, buf6, 256, XBLOCK=128, num_warps=4, num_stages=1)
        buf9 = empty_strided_cuda((4, 4, 4, 1, 4), (64, 16, 4, 256, 1),
            torch.float32)
        buf10 = empty_strided_cuda((4, 4, 4, 1, 4), (64, 16, 4, 256, 1),
            torch.float32)
        triton_poi_fused__softmax_mul_rsub_sub_4[grid(256)](buf3, buf4,
            buf9, buf10, 256, XBLOCK=128, num_warps=4, num_stages=1)
        buf7 = buf1
        del buf1
        buf11 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused__softmax_mul_rsub_sub_5[grid(1024)](buf3, buf4,
            buf5, buf6, buf9, buf10, buf7, buf11, 1024, XBLOCK=256,
            num_warps=4, num_stages=1)
        del buf10
        del buf5
        del buf6
        del buf9
        buf8 = buf3
        del buf3
        extern_kernels.bmm(reinterpret_tensor(buf7, (64, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(arg0_1, (64, 4, 4), (16, 4, 1), 0), out=buf8
            )
        del arg0_1
        buf12 = buf7
        del buf7
        triton_poi_fused_clone_1[grid(1024)](arg1_1, buf12, 1024, XBLOCK=
            128, num_warps=4, num_stages=1)
        del arg1_1
        buf13 = empty_strided_cuda((64, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf11, (64, 4, 4), (16, 1, 4),
            0), reinterpret_tensor(buf12, (64, 4, 4), (16, 4, 1), 0), out=buf13
            )
        del buf11
        del buf12
    return reinterpret_tensor(buf8, (4, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0
        ), reinterpret_tensor(buf13, (4, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0
        ), reinterpret_tensor(buf4, (4, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0
        ), reinterpret_tensor(buf4, (4, 4, 4, 4, 4), (256, 64, 16, 1, 4), 0)


class StructuredAttention_biNew(nn.Module):

    def __init__(self, dropout=0.1, scale=100):
        super(StructuredAttention_biNew, self).__init__()
        self.dropout = dropout
        self.scale = scale

    def similarity(self, C, Q, c_mask, q_mask):
        C = F.dropout(F.normalize(C, p=2, dim=-1), p=self.dropout, training
            =self.training)
        Q = F.dropout(F.normalize(Q, p=2, dim=-1), p=self.dropout, training
            =self.training)
        S_mask = torch.matmul(c_mask.unsqueeze(-1), q_mask.unsqueeze(-2))
        S = torch.matmul(C, Q.transpose(-2, -1))
        masked_S = S - 10000000000.0 * (1 - S_mask)
        return masked_S, S_mask

    def forward(self, input_0, input_1, input_2, input_3):
        arg1_1 = input_0
        arg0_1 = input_1
        arg2_1 = input_2
        arg3_1 = input_3
        output = call([arg0_1, arg1_1, arg2_1, arg3_1])
        return output[0], output[1], output[2], output[3]
