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.triton_helpers import libdevice
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_add_linalg_vector_norm_mul_reciprocal_repeat_sub_view_0(
    in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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
    x4 = xindex // 4
    x0 = xindex % 4
    x2 = xindex // 16
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x4, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr2 + (4 * x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp7 = tl.load(in_ptr3 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp13 = tl.load(in_ptr0 + (1 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp15 = tl.load(in_ptr2 + (1 + 4 * x0 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp20 = tl.load(in_ptr0 + (2 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr2 + (2 + 4 * x0 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp27 = tl.load(in_ptr0 + (3 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp29 = tl.load(in_ptr2 + (3 + 4 * x0 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp2 = 0.0
    tmp3 = tmp1 == tmp2
    tmp4 = tmp3.to(tl.float32)
    tmp5 = tmp0 * tmp4
    tmp8 = tmp7 == tmp2
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp6 * tmp9
    tmp11 = tmp5 - tmp10
    tmp12 = tmp11 * tmp11
    tmp14 = tmp13 * tmp4
    tmp16 = tmp15 * tmp9
    tmp17 = tmp14 - tmp16
    tmp18 = tmp17 * tmp17
    tmp19 = tmp12 + tmp18
    tmp21 = tmp20 * tmp4
    tmp23 = tmp22 * tmp9
    tmp24 = tmp21 - tmp23
    tmp25 = tmp24 * tmp24
    tmp26 = tmp19 + tmp25
    tmp28 = tmp27 * tmp4
    tmp30 = tmp29 * tmp9
    tmp31 = tmp28 - tmp30
    tmp32 = tmp31 * tmp31
    tmp33 = tmp26 + tmp32
    tmp34 = libdevice.sqrt(tmp33)
    tmp35 = 1.0
    tmp36 = tmp34 + tmp35
    tmp37 = tl.full([1], 1, tl.int32)
    tmp38 = tmp37 / tmp36
    tmp39 = tmp38 * tmp35
    tl.store(out_ptr0 + x3, tmp33, xmask)
    tl.store(out_ptr1 + x3, tmp39, xmask)


@triton.jit
def triton_poi_fused__unsafe_view_clone_1(in_ptr0, out_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
    x1 = xindex
    y0 = yindex
    tmp0 = tl.load(in_ptr0 + (4 * x1 + 16 * (y0 // 4) + y0 % 4), xmask &
        ymask, eviction_policy='evict_last')
    tmp1 = libdevice.sqrt(tmp0)
    tmp2 = 1.0
    tmp3 = tmp1 + tmp2
    tmp4 = tl.full([1, 1], 1, tl.int32)
    tmp5 = tmp4 / tmp3
    tmp6 = tmp5 * tmp2
    tl.store(out_ptr0 + (x1 + 4 * y0), tmp6, xmask & ymask)


@triton.jit
def triton_poi_fused_cat_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16 % 2
    x0 = xindex % 16
    x2 = xindex // 32
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 16 * x2), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 2, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x0 + 16 * x2), tmp6 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tl.store(out_ptr0 + x3, tmp10, xmask)


@triton.jit
def triton_poi_fused_tanh_3(in_out_ptr0, in_out_ptr1, in_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_out_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr0 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp5 = tl.load(in_out_ptr1 + x0, xmask)
    tmp3 = tmp0 + tmp2
    tmp4 = libdevice.tanh(tmp3)
    tmp6 = tmp5 + tmp2
    tmp7 = libdevice.tanh(tmp6)
    tl.store(in_out_ptr0 + x0, tmp4, xmask)
    tl.store(in_out_ptr1 + x0, tmp7, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7) = 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), (16, 4, 1))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    assert_size_stride(primals_6, (1, 2, 3, 3), (18, 9, 3, 1))
    assert_size_stride(primals_7, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_linalg_vector_norm_mul_reciprocal_repeat_sub_view_0[
            grid(64)](primals_1, primals_2, primals_4, primals_3, buf0,
            buf1, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del primals_2
        del primals_3
        buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf1, primals_5, out=buf2)
        buf3 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        triton_poi_fused__unsafe_view_clone_1[grid(16, 4)](buf0, buf3, 16, 
            4, XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        buf4 = reinterpret_tensor(buf0, (16, 4), (4, 1), 0)
        del buf0
        extern_kernels.mm(buf3, primals_5, out=buf4)
        del primals_5
        buf5 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.float32)
        triton_poi_fused_cat_2[grid(128)](primals_1, buf2, buf5, 128,
            XBLOCK=128, num_warps=4, num_stages=1)
        del buf2
        del primals_1
        buf6 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.float32)
        triton_poi_fused_cat_2[grid(128)](primals_4, buf4, buf6, 128,
            XBLOCK=128, num_warps=4, num_stages=1)
        del buf4
        del primals_4
        buf7 = extern_kernels.convolution(buf5, primals_6, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf7, (4, 1, 4, 4), (16, 16, 4, 1))
        buf8 = extern_kernels.convolution(buf6, primals_6, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf8, (4, 1, 4, 4), (16, 16, 4, 1))
        buf9 = reinterpret_tensor(buf7, (4, 4, 4), (16, 4, 1), 0)
        del buf7
        buf10 = reinterpret_tensor(buf8, (4, 4, 4), (16, 4, 1), 0)
        del buf8
        triton_poi_fused_tanh_3[grid(64)](buf9, buf10, primals_7, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_7
    return buf9, buf10, primals_6, buf5, buf6, buf9, buf10, reinterpret_tensor(
        buf3, (4, 16), (1, 4), 0), reinterpret_tensor(buf1, (4, 16), (1, 4), 0)


def match_score(s1, s2, mask1, mask2):
    """
    s1, s2:  batch_size * seq_len  * dim
    """
    _batch, seq_len, _dim = s1.shape
    s1 = s1 * mask1.eq(0).unsqueeze(2).float()
    s2 = s2 * mask2.eq(0).unsqueeze(2).float()
    s1 = s1.unsqueeze(2).repeat(1, 1, seq_len, 1)
    s2 = s2.unsqueeze(1).repeat(1, seq_len, 1, 1)
    a = s1 - s2
    a = torch.norm(a, dim=-1, p=2)
    return 1.0 / (1.0 + a)


class Wide_ConvNew(nn.Module):

    def __init__(self, seq_len, embeds_size, device='gpu'):
        super(Wide_ConvNew, self).__init__()
        self.seq_len = seq_len
        self.embeds_size = embeds_size
        self.W = nn.Parameter(torch.randn((seq_len, embeds_size)))
        nn.init.xavier_normal_(self.W)
        self.W
        self.conv = nn.Conv2d(in_channels=2, out_channels=1, kernel_size=3,
            padding=[1, 1], stride=1)
        self.tanh = nn.Tanh()

    def forward(self, input_0, input_1, input_2, input_3):
        primals_2 = self.W
        primals_6 = self.conv.weight
        primals_7 = self.conv.bias
        primals_1 = input_0
        primals_4 = input_1
        primals_3 = input_2
        primals_5 = input_3
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0], output[1]


Measured latency_ms: 0.014033