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 math as tl_math
from torch import Tensor
from typing import Optional
from torch import nn
from typing import Final
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_abs_add_clamp_mean_mul_pow_rsub_0(in_out_ptr0, in_ptr0,
    in_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)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp2 = 0.4
    tmp3 = tmp1 * tmp2
    tmp4 = 4.0
    tmp5 = tmp3 + tmp4
    tmp6 = 0.0
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = 1.0
    tmp9 = triton_helpers.minimum(tmp7, tmp8)
    tmp10 = tmp8 - tmp9
    tmp11 = tmp0 * tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
    tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
    tmp16 = tmp8 - tmp0
    tmp17 = -0.2
    tmp18 = tmp1 * tmp17
    tmp19 = tmp18 + tmp6
    tmp20 = triton_helpers.maximum(tmp19, tmp6)
    tmp21 = triton_helpers.minimum(tmp20, tmp8)
    tmp22 = tmp8 - tmp21
    tmp23 = tmp16 * tmp22
    tmp24 = tl_math.abs(tmp23)
    tmp25 = tl.broadcast_to(tmp24, [RBLOCK])
    tmp27 = triton_helpers.promote_to_tensor(tl.sum(tmp25, 0))
    tmp28 = 256.0
    tmp29 = tmp15 / tmp28
    tmp30 = tmp27 / tmp28
    tmp31 = 0.1
    tmp32 = tmp30 * tmp31
    tmp33 = tmp29 + tmp32
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp33, 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_abs_add_clamp_mean_mul_pow_rsub_0[grid(1)](buf2,
            arg1_1, arg0_1, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf2,


class DfAlphaLossNew(nn.Module):
    """Add a penalty to use DF for very noisy segments.

    Starting from lsnr_thresh, the penalty is increased and has its maximum at lsnr_min.
    """
    factor: 'Final[float]'
    lsnr_thresh: 'Final[float]'
    lsnr_min: 'Final[float]'

    def __init__(self, factor: 'float'=1, lsnr_thresh: 'float'=-7.5,
        lsnr_min: 'float'=-10.0):
        super().__init__()
        self.factor = factor
        self.lsnr_thresh = lsnr_thresh
        self.lsnr_min = lsnr_min

    def lsnr_mapping(self, lsnr: 'Tensor', lsnr_thresh: 'float', lsnr_min:
        'Optional[float]'=None) ->Tensor:
        """Map lsnr_min to 1 and lsnr_thresh to 0"""
        lsnr_min = float(self.lsnr_min) if lsnr_min is None else lsnr_min
        a_ = 1 / (lsnr_thresh - lsnr_min)
        b_ = -a_ * lsnr_min
        return 1 - torch.clamp(a_ * lsnr + b_, 0.0, 1.0)

    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.014863