from _paritybench_helpers import _mock_config
from torch.nn import Module
import math
import torch
import torch.nn.functional as f
import torch.nn as nn


class Module(nn.Module):

    def __init__(self):
        super().__init__()
        self.summary = {}

    def add_summary(self, name, val):
        if self.training:
            self.summary[name] = val.clone().detach().cpu().numpy()

    def get_summary(self, base_name=''):
        summary = {}
        if base_name:
            base_name += '/'
        if self.summary:
            summary.update({(base_name + name): val for name, val in self.
                summary.items()})
        for name, child in self.named_children():
            if hasattr(child, 'get_summary'):
                name = base_name + name
                summary.update(child.get_summary(name))
        return summary


class Alignment(Module):

    def __init__(self, args, __):
        super().__init__()
        self.temperature = nn.Parameter(torch.tensor(1 / math.sqrt(args.
            hidden_size)))

    def _attention(self, a, b):
        return torch.matmul(a, b.transpose(1, 2)) * self.temperature

    def forward(self, a, b, mask_a, mask_b):
        attn = self._attention(a, b)
        mask = torch.matmul(mask_a.float(), mask_b.transpose(1, 2).float()
            ).byte()
        mask = mask > 0
        attn.masked_fill_(~mask, -10000000.0)
        attn_a = f.softmax(attn, dim=1)
        attn_b = f.softmax(attn, dim=2)
        feature_b = torch.matmul(attn_a.transpose(1, 2), a)
        feature_a = torch.matmul(attn_b, b)
        self.add_summary('temperature', self.temperature)
        self.add_summary('attention_a', attn_a)
        self.add_summary('attention_b', attn_b)
        return feature_a, feature_b


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


def get_init_inputs():
    return [[], {'args': _mock_config(hidden_size=4), '__': 4}]

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.nn import Module
import math
import torch.nn as 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_clone_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
    x0 = xindex % 4
    x1 = xindex // 4 % 4
    x2 = xindex // 16 % 4
    x3 = xindex // 64
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), xmask)
    tl.store(out_ptr0 + x4, tmp0, xmask)


@triton.jit
def triton_poi_fused__to_copy_bitwise_not_gt_1(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
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tmp0.to(tl.int8).to(tl.uint8)
    tmp2 = tl.full([1], 0, tl.uint8)
    tmp3 = tmp1 > tmp2
    tmp4 = tmp3 == 0
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused__softmax_masked_fill_mul_2(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, out_ptr1, out_ptr2, out_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x1 = xindex // 16
    x4 = xindex
    x2 = xindex % 4
    x3 = xindex // 4
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask).to(tl.int1)
    tmp1 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask)
    tmp2 = tl.load(in_ptr2 + 0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
    tmp7 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask).to(tl.int1)
    tmp8 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask)
    tmp12 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask).to(tl.int1)
    tmp13 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask)
    tmp17 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask).to(tl.int1)
    tmp18 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask)
    tmp33 = tl.load(in_ptr0 + (x2 + 16 * x3), xmask).to(tl.int1)
    tmp34 = tl.load(in_ptr1 + (x2 + 16 * x3), xmask)
    tmp37 = tl.load(in_ptr0 + (4 + x2 + 16 * x3), xmask).to(tl.int1)
    tmp38 = tl.load(in_ptr1 + (4 + x2 + 16 * x3), xmask)
    tmp42 = tl.load(in_ptr0 + (8 + x2 + 16 * x3), xmask).to(tl.int1)
    tmp43 = tl.load(in_ptr1 + (8 + x2 + 16 * x3), xmask)
    tmp47 = tl.load(in_ptr0 + (12 + x2 + 16 * x3), xmask).to(tl.int1)
    tmp48 = tl.load(in_ptr1 + (12 + x2 + 16 * x3), xmask)
    tmp4 = tmp1 * tmp3
    tmp5 = -10000000.0
    tmp6 = tl.where(tmp0, tmp5, tmp4)
    tmp9 = tmp8 * tmp3
    tmp10 = tl.where(tmp7, tmp5, tmp9)
    tmp11 = triton_helpers.maximum(tmp6, tmp10)
    tmp14 = tmp13 * tmp3
    tmp15 = tl.where(tmp12, tmp5, tmp14)
    tmp16 = triton_helpers.maximum(tmp11, tmp15)
    tmp19 = tmp18 * tmp3
    tmp20 = tl.where(tmp17, tmp5, tmp19)
    tmp21 = triton_helpers.maximum(tmp16, tmp20)
    tmp22 = tmp6 - tmp21
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tmp10 - tmp21
    tmp25 = tl_math.exp(tmp24)
    tmp26 = tmp23 + tmp25
    tmp27 = tmp15 - tmp21
    tmp28 = tl_math.exp(tmp27)
    tmp29 = tmp26 + tmp28
    tmp30 = tmp20 - tmp21
    tmp31 = tl_math.exp(tmp30)
    tmp32 = tmp29 + tmp31
    tmp35 = tmp34 * tmp3
    tmp36 = tl.where(tmp33, tmp5, tmp35)
    tmp39 = tmp38 * tmp3
    tmp40 = tl.where(tmp37, tmp5, tmp39)
    tmp41 = triton_helpers.maximum(tmp36, tmp40)
    tmp44 = tmp43 * tmp3
    tmp45 = tl.where(tmp42, tmp5, tmp44)
    tmp46 = triton_helpers.maximum(tmp41, tmp45)
    tmp49 = tmp48 * tmp3
    tmp50 = tl.where(tmp47, tmp5, tmp49)
    tmp51 = triton_helpers.maximum(tmp46, tmp50)
    tmp52 = tmp36 - tmp51
    tmp53 = tl_math.exp(tmp52)
    tmp54 = tmp40 - tmp51
    tmp55 = tl_math.exp(tmp54)
    tmp56 = tmp53 + tmp55
    tmp57 = tmp45 - tmp51
    tmp58 = tl_math.exp(tmp57)
    tmp59 = tmp56 + tmp58
    tmp60 = tmp50 - tmp51
    tmp61 = tl_math.exp(tmp60)
    tmp62 = tmp59 + tmp61
    tl.store(out_ptr0 + x4, tmp21, xmask)
    tl.store(out_ptr1 + x4, tmp32, xmask)
    tl.store(out_ptr2 + x4, tmp51, xmask)
    tl.store(out_ptr3 + x4, tmp62, xmask)


@triton.jit
def triton_poi_fused__softmax_clone_masked_fill_mul_3(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, 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
    x5 = xindex
    x3 = xindex // 64
    x6 = xindex % 16
    x0 = xindex % 4
    x1 = xindex // 4 % 4
    x2 = xindex // 16 % 4
    x4 = xindex // 16
    tmp0 = tl.load(in_ptr0 + x5, xmask).to(tl.int1)
    tmp1 = tl.load(in_ptr1 + x5, xmask)
    tmp2 = tl.load(in_ptr2 + 0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
    tmp7 = tl.load(in_ptr3 + (x6 + 16 * x3), xmask, eviction_policy=
        'evict_last')
    tmp10 = tl.load(in_ptr4 + (x6 + 16 * x3), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr5 + (x0 + 4 * x4), xmask, eviction_policy=
        'evict_last')
    tmp15 = tl.load(in_ptr6 + (x0 + 4 * x4), xmask, eviction_policy=
        'evict_last')
    tmp4 = tmp1 * tmp3
    tmp5 = -10000000.0
    tmp6 = tl.where(tmp0, tmp5, tmp4)
    tmp8 = tmp6 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tmp11 = tmp9 / tmp10
    tmp13 = tmp6 - tmp12
    tmp14 = tl_math.exp(tmp13)
    tmp16 = tmp14 / tmp15
    tl.store(out_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), tmp11, xmask)
    tl.store(out_ptr1 + x5, tmp16, 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, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (), ())
    assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_5, (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_clone_0[grid(256)](primals_1, buf0, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(primals_2, (16, 4, 4), (16, 4,
            1), 0), reinterpret_tensor(buf0, (16, 4, 4), (16, 4, 1), 0),
            out=buf1)
        buf2 = buf0
        del buf0
        triton_poi_fused_clone_0[grid(256)](primals_5, buf2, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_5
        buf3 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(primals_4, (16, 4, 4), (16, 4,
            1), 0), reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0),
            out=buf3)
        del primals_4
        buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
        triton_poi_fused__to_copy_bitwise_not_gt_1[grid(256)](buf3, buf4, 
            256, XBLOCK=256, num_warps=4, num_stages=1)
        buf5 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        buf6 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 4, 1, 4), (16, 4, 64, 1), torch.float32)
        buf8 = empty_strided_cuda((4, 4, 1, 4), (16, 4, 64, 1), torch.float32)
        triton_poi_fused__softmax_masked_fill_mul_2[grid(64)](buf4, buf1,
            primals_3, buf5, buf6, buf7, buf8, 64, XBLOCK=64, num_warps=1,
            num_stages=1)
        buf9 = reinterpret_tensor(buf3, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf3
        buf11 = buf2
        del buf2
        triton_poi_fused__softmax_clone_masked_fill_mul_3[grid(256)](buf4,
            buf1, primals_3, buf5, buf6, buf7, buf8, buf9, buf11, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        del buf5
        del buf6
        del buf7
        del buf8
        buf10 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(primals_2, (16, 4, 4), (16, 4, 1), 0),
            out=buf10)
        buf12 = reinterpret_tensor(buf9, (16, 4, 4), (16, 4, 1), 0)
        del buf9
        extern_kernels.bmm(reinterpret_tensor(buf11, (16, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(primals_1, (16, 4, 4), (16, 4, 1), 0),
            out=buf12)
        del buf11
    return reinterpret_tensor(buf12, (4, 4, 4, 4), (64, 16, 4, 1), 0
        ), reinterpret_tensor(buf10, (4, 4, 4, 4), (64, 16, 4, 1), 0
        ), primals_3, buf1, buf4, reinterpret_tensor(primals_1, (16, 4, 4),
        (16, 1, 4), 0), reinterpret_tensor(primals_2, (16, 4, 4), (16, 1, 4), 0
        )


class Module(nn.Module):

    def __init__(self):
        super().__init__()
        self.summary = {}

    def add_summary(self, name, val):
        if self.training:
            self.summary[name] = val.clone().detach().cpu().numpy()

    def get_summary(self, base_name=''):
        summary = {}
        if base_name:
            base_name += '/'
        if self.summary:
            summary.update({(base_name + name): val for name, val in self.
                summary.items()})
        for name, child in self.named_children():
            if hasattr(child, 'get_summary'):
                name = base_name + name
                summary.update(child.get_summary(name))
        return summary


class AlignmentNew(Module):

    def __init__(self, args, __):
        super().__init__()
        self.temperature = nn.Parameter(torch.tensor(1 / math.sqrt(args.
            hidden_size)))

    def _attention(self, a, b):
        return torch.matmul(a, b.transpose(1, 2)) * self.temperature

    def forward(self, input_0, input_1, input_2, input_3):
        primals_3 = self.temperature
        primals_1 = input_0
        primals_2 = input_1
        primals_4 = input_2
        primals_5 = input_3
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0], output[1]
