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
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 = 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)
    tl.store(out_ptr0 + x0, tmp0, xmask)


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


@triton.jit
def triton_poi_fused__softmax_mul_sum_2(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, out_ptr2, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (16 * (x0 // 4) + x0 % 4), xmask)
    tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (4 + 16 * (x0 // 4) + x0 % 4), xmask)
    tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (8 + 16 * (x0 // 4) + x0 % 4), xmask)
    tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (12 + 16 * (x0 // 4) + x0 % 4), xmask)
    tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = triton_helpers.maximum(tmp2, tmp5)
    tmp9 = tmp7 * tmp8
    tmp10 = triton_helpers.maximum(tmp6, tmp9)
    tmp13 = tmp11 * tmp12
    tmp14 = triton_helpers.maximum(tmp10, tmp13)
    tmp15 = tmp2 - tmp14
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp5 - tmp14
    tmp18 = tl_math.exp(tmp17)
    tmp19 = tmp16 + tmp18
    tmp20 = tmp9 - tmp14
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp19 + tmp21
    tmp23 = tmp13 - tmp14
    tmp24 = tl_math.exp(tmp23)
    tmp25 = tmp22 + tmp24
    tmp26 = tmp16 / tmp25
    tmp27 = tmp26 * tmp1
    tmp28 = tmp18 / tmp25
    tmp29 = tmp28 * tmp4
    tmp30 = tmp27 + tmp29
    tmp31 = tmp21 / tmp25
    tmp32 = tmp31 * tmp8
    tmp33 = tmp30 + tmp32
    tmp34 = tmp24 / tmp25
    tmp35 = tmp34 * tmp12
    tmp36 = tmp33 + tmp35
    tl.store(out_ptr0 + x0, tmp14, xmask)
    tl.store(out_ptr1 + x0, tmp25, xmask)
    tl.store(out_ptr2 + x0, tmp36, xmask)


@triton.jit
def triton_poi_fused_add_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9, out_ptr0, out_ptr1, 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
    y3 = yindex
    y0 = yindex % 4
    y1 = yindex // 4
    tmp0 = tl.load(in_ptr0 + (x2 + 4 * y3), xmask & ymask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + (x2 + 4 * y3), xmask & ymask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr2 + y3, ymask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + y3, ymask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr4 + y3, ymask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr5 + (x2 + 4 * y1 + 16 * y0), xmask & ymask,
        eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr6 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr7 + (x2 + 4 * y1), xmask & ymask, eviction_policy
        ='evict_last')
    tmp20 = tl.load(in_ptr8 + (x2 + 4 * y1), xmask & ymask, eviction_policy
        ='evict_last')
    tmp23 = tl.load(in_ptr9 + (x2 + 4 * y1), xmask & ymask, eviction_policy
        ='evict_last')
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 - tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp7 = tmp5 / tmp6
    tmp8 = tmp7 * tmp1
    tmp10 = 1e-13
    tmp11 = tmp9 + tmp10
    tmp12 = tmp8 / tmp11
    tmp14 = tmp12 + tmp13
    tmp16 = tmp0 * tmp15
    tmp18 = tmp16 - tmp17
    tmp19 = tl_math.exp(tmp18)
    tmp21 = tmp19 / tmp20
    tmp22 = tmp21 * tmp15
    tmp24 = tmp23 + tmp10
    tmp25 = tmp22 / tmp24
    tmp26 = tmp25 + tmp13
    tl.store(out_ptr0 + (x2 + 4 * y3), tmp14, xmask & ymask)
    tl.store(out_ptr1 + (x2 + 4 * y3), tmp26, xmask & ymask)


@triton.jit
def triton_poi_fused_clone_mul_4(in_out_ptr0, in_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
    y3 = yindex
    y0 = yindex % 4
    y1 = yindex // 4
    tmp0 = tl.load(in_out_ptr0 + (x2 + 4 * y3), xmask & ymask,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(arg2_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(arg3_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(arg4_1, (4, 4, 4), (16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 1, 4), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clone_0[grid(64)](arg1_1, buf0, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(arg0_1, buf0, out=buf1)
        buf2 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        buf3 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        buf4 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        triton_poi_fused__softmax_mul_sum_1[grid(16)](buf1, arg3_1, buf2,
            buf3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf8 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        buf9 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        buf10 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        triton_poi_fused__softmax_mul_sum_2[grid(16)](buf1, arg4_1, buf8,
            buf9, buf10, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf5 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0)
        del buf0
        buf11 = empty_strided_cuda((4, 4, 4), (16, 1, 4), torch.float32)
        triton_poi_fused_add_3[grid(16, 4)](buf1, arg3_1, buf2, buf3, buf4,
            arg2_1, arg4_1, buf8, buf9, buf10, buf5, buf11, 16, 4, XBLOCK=4,
            YBLOCK=16, num_warps=1, num_stages=1)
        del arg2_1
        del buf10
        del buf2
        del buf3
        del buf4
        del buf8
        del buf9
        buf6 = buf1
        del buf1
        extern_kernels.bmm(buf5, arg1_1, out=buf6)
        del arg1_1
        buf7 = buf6
        del buf6
        triton_poi_fused_clone_mul_4[grid(16, 4)](buf7, arg4_1, 16, 4,
            XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        del arg4_1
        buf12 = buf5
        del buf5
        extern_kernels.bmm(buf11, arg0_1, out=buf12)
        del arg0_1
        del buf11
        buf13 = buf12
        del buf12
        triton_poi_fused_clone_mul_4[grid(16, 4)](buf13, arg3_1, 16, 4,
            XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        del arg3_1
    return buf7, buf13


def masked_softmax(tensor, mask, kb_mask):
    """
    Apply a masked softmax on the last dimension of a tensor.
    The input tensor and mask should be of size (batch, *, sequence_length).

    Args:
        tensor: The tensor on which the softmax function must be applied along
            the last dimension.
        mask: A mask of the same size as the tensor with 0s in the positions of
            the values that must be masked and 1s everywhere else.

    Returns:
        A tensor of the same size as the inputs containing the result of the
        softmax.
    """
    tensor_shape = tensor.size()
    reshaped_tensor = tensor.view(-1, tensor_shape[-1])
    while mask.dim() < tensor.dim():
        mask = mask.unsqueeze(1)
    mask = mask.expand_as(tensor).contiguous().float()
    reshaped_mask = mask.view(-1, mask.size()[-1])
    result = nn.functional.softmax(reshaped_tensor * reshaped_mask, dim=-1)
    result = result * reshaped_mask
    result = result / (result.sum(dim=-1, keepdim=True) + 1e-13)
    return torch.add(result.view(*tensor_shape), kb_mask)


def weighted_sum(tensor, weights, mask):
    """
    Apply a weighted sum on the vectors along the last dimension of 'tensor',
    and mask the vectors in the result with 'mask'.

    Args:
        tensor: A tensor of vectors on which a weighted sum must be applied.
        weights: The weights to use in the weighted sum.
        mask: A mask to apply on the result of the weighted sum.

    Returns:
        A new tensor containing the result of the weighted sum after the mask
        has been applied on it.
    """
    weighted_sum = weights.bmm(tensor)
    while mask.dim() < weighted_sum.dim():
        mask = mask.unsqueeze(1)
    mask = mask.transpose(-1, -2)
    mask = mask.expand_as(weighted_sum).contiguous().float()
    return weighted_sum * mask


class SoftmaxAttentionNew(nn.Module):
    """
    Attention layer taking premises and hypotheses encoded by an RNN as input
    and computing the soft attention between their elements.

    The dot product of the encoded vectors in the premises and hypotheses is
    first computed. The softmax of the result is then used in a weighted sum
    of the vectors of the premises for each element of the hypotheses, and
    conversely for the elements of the premises.
    """

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


Measured latency_ms: 0.115147