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 torch.nn as 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_binary_cross_entropy_with_logits_mse_loss_mul_sigmoid_sqrt_0(
    in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    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)
    tmp2 = tl.load(in_ptr1 + r0, None)
    tmp8 = tl.load(in_ptr1 + (64 + r0), None)
    tmp11 = tl.load(in_ptr0 + (64 + r0), None)
    tmp1 = tl.sigmoid(tmp0)
    tmp3 = tmp1 - tmp2
    tmp4 = tmp3 * tmp3
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp7 = tl.sum(tmp5, 1)[:, None]
    tmp9 = 1.0
    tmp10 = tmp9 - tmp8
    tmp12 = tmp10 * tmp11
    tmp13 = 0.0
    tmp14 = triton_helpers.minimum(tmp13, tmp11)
    tmp15 = tl_math.abs(tmp11)
    tmp16 = -tmp15
    tmp17 = tl_math.exp(tmp16)
    tmp18 = libdevice.log1p(tmp17)
    tmp19 = tmp14 - tmp18
    tmp20 = tmp12 - tmp19
    tmp21 = tl.broadcast_to(tmp20, [XBLOCK, RBLOCK])
    tmp23 = tl.sum(tmp21, 1)[:, None]
    tmp24 = 64.0
    tmp25 = tmp7 / tmp24
    tmp26 = 1e-06
    tmp27 = tmp25 + tmp26
    tmp28 = libdevice.sqrt(tmp27)
    tmp29 = 2.0
    tmp30 = tmp28 * tmp29
    tmp31 = tmp23 / tmp24
    tmp32 = tmp30 + tmp31
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp32, None)


def call(args):
    arg0_1, arg1_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))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf2 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused_add_binary_cross_entropy_with_logits_mse_loss_mul_sigmoid_sqrt_0[
            grid(1)](buf2, arg0_1, arg1_1, 1, 64, XBLOCK=1, num_warps=2,
            num_stages=1)
        del arg0_1
        del arg1_1
    return buf2,


def rmse_loss(smooth=1e-06):
    """Create Root Mean Squared Error Loss.

    Returns:
        Root mean squared error loss function
    """
    return RMSELoss(smooth=1e-06)


def bce_loss():
    """Create Binary Cross Entropy Loss.
    The loss automatically applies the sigmoid activation
    function on the prediction input.

    Returns:
        Binary cross entropy loss function
    """
    return nn.BCEWithLogitsLoss()


class RMSELoss(nn.Module):

    def __init__(self, smooth=1e-06):
        """RMSE Loss.

        Args:
            smooth (float, optional): Smoothing value.
        """
        super().__init__()
        self.mse = nn.MSELoss()
        self.smooth = smooth

    def forward(self, input, target):
        """Calculate RMSE Loss.

        Args:
            input (torch.Tensor): Model predictions.
            target (torch.Tensor): Target values.
        
        Returns:
            RMSE loss
        """
        loss = torch.sqrt(self.mse(input, target) + self.smooth)
        return loss


class RmseBceLossNew(nn.Module):

    def __init__(self):
        super(RmseBceLossNew, self).__init__()
        self.rmse = rmse_loss()
        self.bce = bce_loss()

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


Measured latency_ms: 0.015181