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
import torch.nn as nn
import torch.utils.data.distributed
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_mean_mul_sub_0(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, 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)
    r1 = rindex // 64
    r0 = rindex % 64
    r2 = rindex
    tmp3 = tl.load(in_ptr0 + (64 + r0), None, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (64 + r0), None, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + r0, None, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + r0, None, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr1 + r2, None)
    tmp14 = tl.load(in_ptr2 + (64 + r0), None, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr2 + r0, None, eviction_policy='evict_last')
    tmp18 = tl.load(in_ptr2 + r2, None)
    tmp23 = tl.load(in_ptr0 + (192 + r0), None, eviction_policy='evict_last')
    tmp24 = tl.load(in_ptr1 + (192 + r0), None, eviction_policy='evict_last')
    tmp28 = tl.load(in_ptr0 + (128 + r0), None, eviction_policy='evict_last')
    tmp29 = tl.load(in_ptr1 + (128 + r0), None, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr2 + (192 + r0), None, eviction_policy='evict_last')
    tmp35 = tl.load(in_ptr2 + (128 + r0), None, eviction_policy='evict_last')
    tmp0 = r1
    tmp1 = tl.full([1], 1, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tl.full([1], 0, tl.int32)
    tmp7 = tmp0 == tmp6
    tmp10 = tmp8 * tmp9
    tmp12 = tl.where(tmp7, tmp10, tmp11)
    tmp13 = tl.where(tmp2, tmp5, tmp12)
    tmp15 = tmp3 * tmp14
    tmp17 = tmp8 * tmp16
    tmp19 = tl.where(tmp7, tmp17, tmp18)
    tmp20 = tl.where(tmp2, tmp15, tmp19)
    tmp21 = tl.full([1], 3, tl.int32)
    tmp22 = tmp0 == tmp21
    tmp25 = tmp23 * tmp24
    tmp26 = tl.full([1], 2, tl.int32)
    tmp27 = tmp0 == tmp26
    tmp30 = tmp28 * tmp29
    tmp31 = tl.where(tmp27, tmp30, tmp13)
    tmp32 = tl.where(tmp22, tmp25, tmp31)
    tmp34 = tmp23 * tmp33
    tmp36 = tmp28 * tmp35
    tmp37 = tl.where(tmp27, tmp36, tmp20)
    tmp38 = tl.where(tmp22, tmp34, tmp37)
    tmp39 = tmp32 - tmp38
    tmp40 = tl_math.abs(tmp39)
    tmp41 = tl.broadcast_to(tmp40, [RBLOCK])
    tmp43 = triton_helpers.promote_to_tensor(tl.sum(tmp41, 0))
    tmp44 = 256.0
    tmp45 = tmp43 / tmp44
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp45, None)


def call(args):
    arg0_1, arg1_1, arg2_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))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf3 = buf2
        del buf2
        get_raw_stream(0)
        triton_per_fused_abs_mean_mul_sub_0[grid(1)](buf3, arg2_1, arg0_1,
            arg1_1, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
    return buf3,


class BG_lossNew(nn.Module):

    def __init__(self):
        super(BG_lossNew, self).__init__()
        self.loss = nn.L1Loss()

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


Measured latency_ms: 0.015699