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, math as tl_math
import torch.nn as nn
import torch.nn.functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_red_fused_abs_add_exp_log_mean_mul_neg_rsub_tanh_0(in_ptr0,
    in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.
    constexpr):
    xnumel = 2
    rnumel = 8192
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    _tmp15 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + (r1 + 8192 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp5 = tl.load(in_ptr1 + (r1 + 8192 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp1 = libdevice.tanh(tmp0)
        tmp2 = tl_math.abs(tmp1)
        tmp3 = 1.0
        tmp4 = tmp3 - tmp2
        tmp6 = libdevice.tanh(tmp5)
        tmp7 = tl_math.abs(tmp6)
        tmp8 = tmp4 * tmp7
        tmp9 = -tmp7
        tmp10 = tl_math.exp(tmp9)
        tmp11 = tmp10 + tmp3
        tmp12 = tl_math.log(tmp11)
        tmp13 = tmp8 + tmp12
        tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
        tmp16 = _tmp15 + tmp14
        _tmp15 = tl.where(rmask & xmask, tmp16, _tmp15)
    tmp15 = tl.sum(_tmp15, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp15, xmask)


@triton.jit
def triton_per_fused_abs_add_exp_log_mean_mul_neg_rsub_tanh_1(in_out_ptr0,
    in_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 2
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp4 = 16384.0
    tmp5 = tmp3 / tmp4
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp5, None)


def call(args):
    arg0_1, arg1_1, arg2_1 = args
    args.clear()
    assert_size_stride(arg0_1, (1, 1, 3, 3), (9, 9, 3, 1))
    assert_size_stride(arg1_1, (4, 1, 64, 64), (4096, 4096, 64, 1))
    assert_size_stride(arg2_1, (4, 1, 64, 64), (4096, 4096, 64, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(arg1_1, arg0_1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg1_1
        buf1 = extern_kernels.convolution(arg2_1, arg0_1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg0_1
        del arg2_1
        buf2 = empty_strided_cuda((2,), (1,), torch.float32)
        get_raw_stream(0)
        triton_red_fused_abs_add_exp_log_mean_mul_neg_rsub_tanh_0[grid(2)](buf0
            , buf1, buf2, 2, 8192, XBLOCK=1, RBLOCK=2048, num_warps=16,
            num_stages=1)
        del buf0
        del buf1
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf4 = buf3
        del buf3
        triton_per_fused_abs_add_exp_log_mean_mul_neg_rsub_tanh_1[grid(1)](buf4
            , buf2, 1, 2, XBLOCK=1, num_warps=2, num_stages=1)
        del buf2
    return buf4,


def cross_entropy(logits, labels):
    return torch.mean((1 - labels) * logits + torch.log(1 + torch.exp(-logits))
        )


class EdgeLossNew(nn.Module):

    def __init__(self):
        super().__init__()
        laplace = torch.FloatTensor([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]
            ).view([1, 1, 3, 3])
        self.laplace = nn.Parameter(data=laplace, requires_grad=False)

    def torchLaplace(self, x):
        edge = F.conv2d(x, self.laplace, padding=1)
        edge = torch.abs(torch.tanh(edge))
        return edge

    def forward(self, input_0, input_1):
        arg0_1 = self.laplace
        arg1_1 = input_0
        arg2_1 = input_1
        output = call([arg0_1, arg1_1, arg2_1])
        return output[0]


Measured latency_ms: 0.044393