import torch
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
import math
import torch.utils.data
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused_add_div_log_mul_pow_softplus_sub_sum_0(in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, out_ptr1, xnumel,
    rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp8 = tl.load(in_ptr1 + r0, None)
    tmp10 = tl.load(in_ptr2 + r0, None)
    tmp18 = tl.load(in_ptr3 + r0, None)
    tmp19 = tl.load(in_ptr4 + r0, None)
    tmp22 = tl.load(in_ptr5 + r0, None)
    tmp23 = tl.load(in_ptr6 + r0, None)
    tmp1 = 20.0
    tmp2 = tmp0 > tmp1
    tmp3 = tl_math.exp(tmp0)
    tmp4 = libdevice.log1p(tmp3)
    tmp5 = tl.where(tmp2, tmp0, tmp4)
    tmp6 = 4.0
    tmp7 = tmp5 + tmp6
    tmp9 = tmp8 * tmp8
    tmp11 = tmp10 * tmp10
    tmp12 = tmp9 + tmp11
    tmp13 = tmp7 + tmp12
    tmp14 = tmp7 * tmp13
    tmp15 = tl_math.log(tmp14)
    tmp16 = 1.8378770664093453
    tmp17 = tmp15 + tmp16
    tmp20 = tmp18 - tmp19
    tmp21 = tmp20 * tmp20
    tmp24 = tmp22 - tmp23
    tmp25 = tmp24 * tmp24
    tmp26 = tmp21 + tmp25
    tmp27 = tmp26 / tmp7
    tmp28 = tmp17 + tmp27
    tmp29 = tmp20 * tmp8
    tmp30 = tmp24 * tmp10
    tmp31 = tmp29 + tmp30
    tmp32 = tmp31 * tmp31
    tmp33 = tmp32 / tmp14
    tmp34 = tmp28 - tmp33
    tmp35 = 0.5
    tmp36 = tmp34 * tmp35
    tmp37 = tl.broadcast_to(tmp36, [RBLOCK])
    tmp39 = triton_helpers.promote_to_tensor(tl.sum(tmp37, 0))
    tl.store(out_ptr1 + tl.full([1], 0, tl.int32), tmp39, None)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1, arg6_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg4_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg5_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg6_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_add_div_log_mul_pow_softplus_sub_sum_0[grid(1)](arg0_1
            , arg1_1, arg2_1, arg3_1, arg4_1, arg5_1, arg6_1, buf1, 1, 256,
            num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
        del arg3_1
        del arg4_1
        del arg5_1
        del arg6_1
    return buf1,


class IndepAnisotropicGaussianUVLossNew(nn.Module):
    """
    Loss for the case of independent residuals with anisotropic covariances:
    $Sigma_i = sigma_i^2 I + r_i r_i^T$
    The loss (negative log likelihood) is then:
    $1/2 sum_{i=1}^n (log(2 pi)
      + log sigma_i^2 (sigma_i^2 + ||r_i||^2)
      + ||delta_i||^2 / sigma_i^2
      - <delta_i, r_i>^2 / (sigma_i^2 * (sigma_i^2 + ||r_i||^2)))$,
    where $delta_i=(u - u', v - v')$ is a 2D vector containing UV coordinates
    difference between estimated and ground truth UV values
    For details, see:
    N. Neverova, D. Novotny, A. Vedaldi "Correlated Uncertainty for Learning
    Dense Correspondences from Noisy Labels", p. 918--926, in Proc. NIPS 2019
    """

    def __init__(self, sigma_lower_bound: 'float'):
        super(IndepAnisotropicGaussianUVLossNew, self).__init__()
        self.sigma_lower_bound = sigma_lower_bound
        self.log2pi = math.log(2 * math.pi)

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


Measured latency_ms: 0.019286