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 typing import Dict
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_clamp_min_linalg_vector_norm_mul_0(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
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp4 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 * tmp1
    tmp3 = tmp2 * tmp2
    tmp5 = tmp4 * tmp1
    tmp6 = tmp5 * tmp5
    tmp7 = tmp3 + tmp6
    tmp9 = tmp8 * tmp1
    tmp10 = tmp9 * tmp9
    tmp11 = tmp7 + tmp10
    tmp13 = tmp12 * tmp1
    tmp14 = tmp13 * tmp13
    tmp15 = tmp11 + tmp14
    tmp16 = libdevice.sqrt(tmp15)
    tmp17 = 1e-10
    tmp18 = triton_helpers.maximum(tmp16, tmp17)
    tl.store(out_ptr0 + x2, tmp18, xmask)


@triton.jit
def triton_poi_fused_div_mul_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex % 16
    x4 = xindex // 4
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 / tmp3
    tl.store(out_ptr0 + x5, tmp4, xmask)


@triton.jit
def triton_poi_fused_div_exp_neg_pow_sub_sum_2(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 336
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 21
    x0 = xindex % 21
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp14 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp23 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp24 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp33 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp34 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 - tmp3
    tmp5 = tmp4 * tmp4
    tmp6 = -tmp5
    tmp8 = tmp7 * tmp7
    tmp9 = tmp6 / tmp8
    tmp10 = 0.5
    tmp11 = tmp9 * tmp10
    tmp12 = tl_math.exp(tmp11)
    tmp15 = tmp13 * tmp14
    tmp16 = tmp15 - tmp3
    tmp17 = tmp16 * tmp16
    tmp18 = -tmp17
    tmp19 = tmp18 / tmp8
    tmp20 = tmp19 * tmp10
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp12 + tmp21
    tmp25 = tmp23 * tmp24
    tmp26 = tmp25 - tmp3
    tmp27 = tmp26 * tmp26
    tmp28 = -tmp27
    tmp29 = tmp28 / tmp8
    tmp30 = tmp29 * tmp10
    tmp31 = tl_math.exp(tmp30)
    tmp32 = tmp22 + tmp31
    tmp35 = tmp33 * tmp34
    tmp36 = tmp35 - tmp3
    tmp37 = tmp36 * tmp36
    tmp38 = -tmp37
    tmp39 = tmp38 / tmp8
    tmp40 = tmp39 * tmp10
    tmp41 = tl_math.exp(tmp40)
    tmp42 = tmp32 + tmp41
    tl.store(out_ptr0 + x2, tmp42, xmask)


@triton.jit
def triton_poi_fused_clamp_log_mul_sum_3(in_ptr0, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 84
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 21
    x1 = xindex // 21
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 84 * x1), xmask)
    tmp6 = tl.load(in_ptr0 + (21 + x0 + 84 * x1), xmask)
    tmp11 = tl.load(in_ptr0 + (42 + x0 + 84 * x1), xmask)
    tmp16 = tl.load(in_ptr0 + (63 + x0 + 84 * x1), xmask)
    tmp1 = 1e-10
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp3 = tl_math.log(tmp2)
    tmp4 = 0.01
    tmp5 = tmp3 * tmp4
    tmp7 = triton_helpers.maximum(tmp6, tmp1)
    tmp8 = tl_math.log(tmp7)
    tmp9 = tmp8 * tmp4
    tmp10 = tmp5 + tmp9
    tmp12 = triton_helpers.maximum(tmp11, tmp1)
    tmp13 = tl_math.log(tmp12)
    tmp14 = tmp13 * tmp4
    tmp15 = tmp10 + tmp14
    tmp17 = triton_helpers.maximum(tmp16, tmp1)
    tmp18 = tl_math.log(tmp17)
    tmp19 = tmp18 * tmp4
    tmp20 = tmp15 + tmp19
    tl.store(out_ptr0 + x2, tmp20, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    assert_size_stride(arg2_1, (4, 4), (4, 1))
    assert_size_stride(arg3_1, (4, 4), (4, 1))
    assert_size_stride(arg4_1, (1, 1, 1, 21), (21, 21, 21, 1))
    assert_size_stride(arg5_1, (1, 1, 1, 21), (21, 21, 21, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clamp_min_linalg_vector_norm_mul_0[grid(16)](arg1_1,
            arg0_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused_clamp_min_linalg_vector_norm_mul_0[grid(16)](arg3_1,
            arg2_1, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_div_mul_1[grid(64)](arg1_1, arg0_1, buf0, buf2, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del arg1_1
        del buf0
        buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_div_mul_1[grid(64)](arg3_1, arg2_1, buf1, buf3, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del arg3_1
        del buf1
        buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(buf2, reinterpret_tensor(buf3, (4, 4, 4), (16, 1,
            4), 0), out=buf4)
        del buf2
        buf5 = buf3
        del buf3
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 4, 1), (4, 1, 1),
            0), reinterpret_tensor(arg2_1, (4, 1, 4), (4, 1, 1), 0), out=buf5)
        del arg0_1
        del arg2_1
        buf6 = empty_strided_cuda((4, 4, 21), (84, 21, 1), torch.float32)
        triton_poi_fused_div_exp_neg_pow_sub_sum_2[grid(336)](buf4, buf5,
            arg4_1, arg5_1, buf6, 336, XBLOCK=128, num_warps=4, num_stages=1)
        del arg4_1
        del arg5_1
        del buf4
        del buf5
        buf7 = empty_strided_cuda((4, 21), (21, 1), torch.float32)
        triton_poi_fused_clamp_log_mul_sum_3[grid(84)](buf6, buf7, 84,
            XBLOCK=128, num_warps=4, num_stages=1)
        del buf6
    return buf7,


class KernelMatcherNew(nn.Module):

    def __init__(self, embed_dim: 'int', kernel_num: 'int'=21) ->None:
        super(KernelMatcherNew, self).__init__()
        self._embed_dim = embed_dim
        self._kernel_num = kernel_num
        mus, sigmas = self.kernel_init(self._kernel_num)
        self._mus = nn.Parameter(mus, requires_grad=False)
        self._sigmas = nn.Parameter(sigmas, requires_grad=False)

    def kernel_init(self, kernel_num: 'int') ->Dict[str, torch.Tensor]:
        mus = [1]
        bin_size = 2.0 / (kernel_num - 1)
        mus.append(1 - bin_size / 2)
        for i in range(1, kernel_num - 1):
            mus.append(mus[i] - bin_size)
        mus = torch.tensor(mus).view(1, 1, 1, kernel_num)
        sigmas = [0.001]
        sigmas += [0.1] * (kernel_num - 1)
        sigmas = torch.tensor(sigmas).view(1, 1, 1, kernel_num)
        return mus, sigmas

    def forward(self, input_0, input_1, input_2, input_3):
        arg4_1 = self._mus
        arg5_1 = self._sigmas
        arg0_1 = input_0
        arg1_1 = input_1
        arg2_1 = input_2
        arg3_1 = input_3
        output = call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1])
        return output[0]


Measured latency_ms: 0.099062