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
import torch.backends.cudnn
import torch.utils
import torch.distributed
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_per_fused__softmax_0(in_ptr0, out_ptr0, out_ptr1, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    xnumel = 64
    rnumel = 19
    RBLOCK: tl.constexpr = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    rmask = rindex < rnumel
    r2 = rindex
    x0 = xindex % 16
    x1 = xindex // 16
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * r2 + 304 * x1), rmask & xmask,
        other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(rmask & xmask, tmp1, float('-inf'))
    tmp4 = triton_helpers.max2(tmp3, 1)[:, None]
    tmp5 = tmp0 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.where(rmask & xmask, tmp7, 0)
    tmp10 = tl.sum(tmp9, 1)[:, None]
    tl.store(out_ptr0 + x3, tmp4, xmask)
    tl.store(out_ptr1 + x3, tmp10, xmask)


@triton.jit
def triton_poi_fused__softmax_replication_pad2d_1(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 2736
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 19
    x1 = xindex // 19 % 6
    x2 = xindex // 114 % 6
    x3 = xindex // 684
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (4 * (3 * (3 <= 0 * (0 >= -1 + x2) + (-1 + x2) *
        (-1 + x2 > 0)) + (0 * (0 >= -1 + x2) + (-1 + x2) * (-1 + x2 > 0)) *
        (0 * (0 >= -1 + x2) + (-1 + x2) * (-1 + x2 > 0) < 3)) + 16 * x0 + 
        304 * x3 + (3 * (3 <= 0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0
        )) + (0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) * (0 * (0 >= 
        -1 + x1) + (-1 + x1) * (-1 + x1 > 0) < 3))), xmask, eviction_policy
        ='evict_last')
    tmp1 = tl.load(in_ptr1 + (4 * (3 * (3 <= 0 * (0 >= -1 + x2) + (-1 + x2) *
        (-1 + x2 > 0)) + (0 * (0 >= -1 + x2) + (-1 + x2) * (-1 + x2 > 0)) *
        (0 * (0 >= -1 + x2) + (-1 + x2) * (-1 + x2 > 0) < 3)) + 16 * x3 + (
        3 * (3 <= 0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) + (0 * (0 >=
        -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) * (0 * (0 >= -1 + x1) + (-1 +
        x1) * (-1 + x1 > 0) < 3))), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + (4 * (3 * (3 <= 0 * (0 >= -1 + x2) + (-1 + x2) *
        (-1 + x2 > 0)) + (0 * (0 >= -1 + x2) + (-1 + x2) * (-1 + x2 > 0)) *
        (0 * (0 >= -1 + x2) + (-1 + x2) * (-1 + x2 > 0) < 3)) + 16 * x3 + (
        3 * (3 <= 0 * (0 >= -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) + (0 * (0 >=
        -1 + x1) + (-1 + x1) * (-1 + x1 > 0)) * (0 * (0 >= -1 + x1) + (-1 +
        x1) * (-1 + x1 > 0) < 3))), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.exp(tmp2)
    tmp5 = tmp3 / tmp4
    tl.store(out_ptr0 + x4, tmp5, xmask)


@triton.jit
def triton_per_fused__softmax_abs_sub_sum_2(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    rnumel = 19
    RBLOCK: tl.constexpr = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    rmask = rindex < rnumel
    r2 = rindex
    x0 = xindex % 16
    x1 = xindex // 16
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * r2 + 304 * x1), rmask & xmask,
        other=0.0)
    tmp1 = tl.load(in_out_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + x3, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr2 + (r2 + 19 * x3), rmask & xmask, other=0.0)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.exp(tmp2)
    tmp5 = tmp3 / tmp4
    tmp7 = tmp5 - tmp6
    tmp8 = tl_math.abs(tmp7)
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.where(rmask & xmask, tmp9, 0)
    tmp12 = tl.sum(tmp11, 1)[:, None]
    tl.store(in_out_ptr0 + x3, tmp12, xmask)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 19, 4, 4), (304, 16, 4, 1))
    assert_size_stride(arg1_1, (19, 1, 3, 3), (9, 9, 3, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        buf1 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused__softmax_0[grid(64)](arg0_1, buf0, buf1, 64, 19,
            XBLOCK=1, num_warps=2, num_stages=1)
        buf2 = empty_strided_cuda((4, 19, 6, 6), (684, 1, 114, 19), torch.
            float32)
        triton_poi_fused__softmax_replication_pad2d_1[grid(2736)](arg0_1,
            buf0, buf1, buf2, 2736, XBLOCK=128, num_warps=4, num_stages=1)
        buf3 = extern_kernels.convolution(buf2, arg1_1, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=19, bias=None)
        assert_size_stride(buf3, (4, 19, 4, 4), (304, 1, 76, 19))
        del arg1_1
        del buf2
        buf4 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0)
        del buf0
        triton_per_fused__softmax_abs_sub_sum_2[grid(64)](buf4, arg0_1,
            buf1, buf3, 64, 19, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del buf1
        del buf3
    return buf4,


class LocalDiscrepancyNew(nn.Module):

    def __init__(self, in_channels=19, padding_mode='replicate', neighbor=8,
        l_type='l1'):
        """
        depth-wise conv to calculate the mean of neighbor
        """
        super(LocalDiscrepancyNew, self).__init__()
        self.type = l_type
        self.mean_conv = nn.Conv2d(in_channels=in_channels, out_channels=
            in_channels, kernel_size=3, stride=1, padding=int(3 / 2), bias=
            False, padding_mode=padding_mode, groups=in_channels)
        if neighbor == 8:
            a = torch.tensor([[[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0,
                1.0]]]]) / 9
        elif neighbor == 4:
            a = torch.tensor([[[[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0,
                0.0]]]]) / 5
        else:
            raise NotImplementedError
        a = a.repeat([in_channels, 1, 1, 1])
        a = nn.Parameter(a)
        self.mean_conv.weight = a
        self.mean_conv.requires_grad_(False)

    def forward(self, input_0):
        arg1_1 = self.mean_conv.weight
        arg0_1 = input_0
        output = call([arg0_1, arg1_1])
        return output[0]


Measured latency_ms: 0.054439