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
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_argmax_clamp_div_mean_mul_rsub_sum_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 % 16
    r1 = rindex // 16
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
    tmp1 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
    tmp17 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
    tmp32 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
    tmp47 = tl.load(in_ptr1 + r2, None)
    tmp53 = tl.load(in_ptr1 + (64 + r2), None)
    tmp57 = tl.load(in_ptr1 + (128 + r2), None)
    tmp61 = tl.load(in_ptr1 + (192 + r2), None)
    tmp2 = tmp0 > tmp1
    tmp3 = tmp0 == tmp1
    tmp4 = tmp0 != tmp0
    tmp5 = tmp1 != tmp1
    tmp6 = tmp4 > tmp5
    tmp7 = tmp2 | tmp6
    tmp8 = tmp4 & tmp5
    tmp9 = tmp3 | tmp8
    tmp10 = tl.full([1, 1], 0, tl.int64)
    tmp11 = tl.full([1, 1], 1, tl.int64)
    tmp12 = tmp10 < tmp11
    tmp13 = tmp9 & tmp12
    tmp14 = tmp7 | tmp13
    tmp15 = tl.where(tmp14, tmp0, tmp1)
    tmp16 = tl.where(tmp14, tmp10, tmp11)
    tmp18 = tmp15 > tmp17
    tmp19 = tmp15 == tmp17
    tmp20 = tmp15 != tmp15
    tmp21 = tmp17 != tmp17
    tmp22 = tmp20 > tmp21
    tmp23 = tmp18 | tmp22
    tmp24 = tmp20 & tmp21
    tmp25 = tmp19 | tmp24
    tmp26 = tl.full([1, 1], 2, tl.int64)
    tmp27 = tmp16 < tmp26
    tmp28 = tmp25 & tmp27
    tmp29 = tmp23 | tmp28
    tmp30 = tl.where(tmp29, tmp15, tmp17)
    tmp31 = tl.where(tmp29, tmp16, tmp26)
    tmp33 = tmp30 > tmp32
    tmp34 = tmp30 == tmp32
    tmp35 = tmp30 != tmp30
    tmp36 = tmp32 != tmp32
    tmp37 = tmp35 > tmp36
    tmp38 = tmp33 | tmp37
    tmp39 = tmp35 & tmp36
    tmp40 = tmp34 | tmp39
    tmp41 = tl.full([1, 1], 3, tl.int64)
    tmp42 = tmp31 < tmp41
    tmp43 = tmp40 & tmp42
    tmp44 = tmp38 | tmp43
    tl.where(tmp44, tmp30, tmp32)
    tmp46 = tl.where(tmp44, tmp31, tmp41)
    tmp48 = 1.0
    tmp49 = tmp48 - tmp47
    tmp50 = tmp11 - tmp46
    tmp51 = tmp50.to(tl.float32)
    tmp52 = tmp49 * tmp51
    tmp54 = tmp48 - tmp53
    tmp55 = tmp54 * tmp51
    tmp56 = tmp52 + tmp55
    tmp58 = tmp48 - tmp57
    tmp59 = tmp58 * tmp51
    tmp60 = tmp56 + tmp59
    tmp62 = tmp48 - tmp61
    tmp63 = tmp62 * tmp51
    tmp64 = tmp60 + tmp63
    tmp65 = tmp48 - tmp49
    tmp66 = tmp65 * tmp51
    tmp67 = tmp48 - tmp54
    tmp68 = tmp67 * tmp51
    tmp69 = tmp66 + tmp68
    tmp70 = tmp48 - tmp58
    tmp71 = tmp70 * tmp51
    tmp72 = tmp69 + tmp71
    tmp73 = tmp48 - tmp62
    tmp74 = tmp73 * tmp51
    tmp75 = tmp72 + tmp74
    tmp76 = tmp64 + tmp75
    tmp77 = 1e-07
    tmp78 = tmp76 + tmp77
    tmp79 = tmp64 / tmp78
    tmp80 = tmp11 - tmp50
    tmp81 = tmp80.to(tl.float32)
    tmp82 = tmp49 * tmp81
    tmp83 = tmp54 * tmp81
    tmp84 = tmp82 + tmp83
    tmp85 = tmp58 * tmp81
    tmp86 = tmp84 + tmp85
    tmp87 = tmp62 * tmp81
    tmp88 = tmp86 + tmp87
    tmp89 = tmp64 + tmp88
    tmp90 = tmp89 + tmp77
    tmp91 = tmp64 / tmp90
    tmp92 = tmp79 * tmp91
    tmp93 = 2.0
    tmp94 = tmp92 * tmp93
    tmp95 = tmp79 + tmp91
    tmp96 = tmp95 + tmp77
    tmp97 = tmp94 / tmp96
    tmp98 = triton_helpers.maximum(tmp97, tmp77)
    tmp99 = 0.9999999
    tmp100 = triton_helpers.minimum(tmp98, tmp99)
    tmp101 = tl.broadcast_to(tmp100, [XBLOCK, RBLOCK])
    tmp103 = tl.sum(tmp101, 1)[:, None]
    tmp104 = 64.0
    tmp105 = tmp103 / tmp104
    tmp106 = tmp48 - tmp105
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp106, 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_add_argmax_clamp_div_mean_mul_rsub_sum_0[grid(1)](buf5
            , arg0_1, arg1_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf5,


class F1_LossNew(nn.Module):
    """Calculate F1 score. Can work with gpu tensors
    
    The original implmentation is written by Michal Haltuf on Kaggle.
    
    Returns
    -------
    torch.Tensor
        `ndim` == 1. epsilon <= val <= 1
    
    Reference
    ---------
    - https://www.kaggle.com/rejpalcz/best-loss-function-for-f1-score-metric
    - https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html#sklearn.metrics.f1_score
    - https://discuss.pytorch.org/t/calculating-precision-recall-and-f1-score-in-case-of-multi-label-classification/28265/6
    - http://www.ryanzhang.info/python/writing-your-own-loss-function-module-for-pytorch/
    """

    def __init__(self, epsilon=1e-07):
        super().__init__()
        self.epsilon = epsilon

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