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
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__to_copy_binary_cross_entropy_with_logits_div_eq_index_put_mul_reciprocal_sum_zeros_0(
    in_out_ptr1, 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)
    tmp27 = tl.load(in_ptr1 + r0, None)
    tmp1 = 0.0
    tmp2 = tmp0 == tmp1
    tmp3 = tmp2.to(tl.int64)
    tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
    tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
    tmp7 = 1.0
    tmp8 = tmp0 == tmp7
    tmp9 = tmp8.to(tl.int64)
    tmp10 = tl.broadcast_to(tmp9, [RBLOCK])
    tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0))
    tmp13 = tmp6.to(tl.float32)
    tmp14 = tl.full([1], 1, tl.int32)
    tmp15 = tmp14 / tmp13
    tmp16 = tmp15 * tmp7
    tmp17 = tl.where(tmp2, tmp16, tmp1)
    tmp18 = tmp12.to(tl.float32)
    tmp19 = tmp14 / tmp18
    tmp20 = tmp19 * tmp7
    tmp21 = tmp20 * tmp7
    tmp22 = tl.where(tmp8, tmp21, tmp17)
    tmp23 = tl.broadcast_to(tmp22, [RBLOCK])
    tmp25 = triton_helpers.promote_to_tensor(tl.sum(tmp23, 0))
    tmp26 = tmp7 - tmp0
    tmp28 = tmp26 * tmp27
    tmp29 = triton_helpers.minimum(tmp1, tmp27)
    tmp30 = tl_math.abs(tmp27)
    tmp31 = -tmp30
    tmp32 = tl_math.exp(tmp31)
    tmp33 = libdevice.log1p(tmp32)
    tmp34 = tmp29 - tmp33
    tmp35 = tmp28 - tmp34
    tmp36 = tmp22 / tmp25
    tmp37 = tmp35 * tmp36
    tmp38 = tl.broadcast_to(tmp37, [RBLOCK])
    tmp40 = triton_helpers.promote_to_tensor(tl.sum(tmp38, 0))
    tl.store(in_out_ptr1 + tl.full([1], 0, tl.int32), tmp40, 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)
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf5 = buf4
        del buf4
        get_raw_stream(0)
        triton_per_fused__to_copy_binary_cross_entropy_with_logits_div_eq_index_put_mul_reciprocal_sum_zeros_0[
            grid(1)](buf5, arg0_1, arg1_1, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf5,


class BalancedLossNew(nn.Module):

    def __init__(self, neg_weight=1.0):
        super(BalancedLossNew, self).__init__()
        self.neg_weight = neg_weight

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