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.triton_helpers import 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_poi_fused_abs_add_sub_sum_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 12
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (4 + x0), xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask)
    tmp6 = tl.load(in_ptr0 + (68 + x0), xmask)
    tmp7 = tl.load(in_ptr0 + (64 + x0), xmask)
    tmp12 = tl.load(in_ptr0 + (132 + x0), xmask)
    tmp13 = tl.load(in_ptr0 + (128 + x0), xmask)
    tmp18 = tl.load(in_ptr0 + (196 + x0), xmask)
    tmp19 = tl.load(in_ptr0 + (192 + x0), xmask)
    tmp24 = tl.load(in_ptr0 + (20 + x0), xmask)
    tmp25 = tl.load(in_ptr0 + (16 + x0), xmask)
    tmp29 = tl.load(in_ptr0 + (84 + x0), xmask)
    tmp30 = tl.load(in_ptr0 + (80 + x0), xmask)
    tmp35 = tl.load(in_ptr0 + (148 + x0), xmask)
    tmp36 = tl.load(in_ptr0 + (144 + x0), xmask)
    tmp41 = tl.load(in_ptr0 + (212 + x0), xmask)
    tmp42 = tl.load(in_ptr0 + (208 + x0), xmask)
    tmp48 = tl.load(in_ptr0 + (36 + x0), xmask)
    tmp49 = tl.load(in_ptr0 + (32 + x0), xmask)
    tmp53 = tl.load(in_ptr0 + (100 + x0), xmask)
    tmp54 = tl.load(in_ptr0 + (96 + x0), xmask)
    tmp59 = tl.load(in_ptr0 + (164 + x0), xmask)
    tmp60 = tl.load(in_ptr0 + (160 + x0), xmask)
    tmp65 = tl.load(in_ptr0 + (228 + x0), xmask)
    tmp66 = tl.load(in_ptr0 + (224 + x0), xmask)
    tmp72 = tl.load(in_ptr0 + (52 + x0), xmask)
    tmp73 = tl.load(in_ptr0 + (48 + x0), xmask)
    tmp77 = tl.load(in_ptr0 + (116 + x0), xmask)
    tmp78 = tl.load(in_ptr0 + (112 + x0), xmask)
    tmp83 = tl.load(in_ptr0 + (180 + x0), xmask)
    tmp84 = tl.load(in_ptr0 + (176 + x0), xmask)
    tmp89 = tl.load(in_ptr0 + (244 + x0), xmask)
    tmp90 = tl.load(in_ptr0 + (240 + x0), xmask)
    tmp2 = tmp0 - tmp1
    tmp3 = 1e-06
    tmp4 = tmp2 + tmp3
    tmp5 = tl_math.abs(tmp4)
    tmp8 = tmp6 - tmp7
    tmp9 = tmp8 + tmp3
    tmp10 = tl_math.abs(tmp9)
    tmp11 = tmp5 + tmp10
    tmp14 = tmp12 - tmp13
    tmp15 = tmp14 + tmp3
    tmp16 = tl_math.abs(tmp15)
    tmp17 = tmp11 + tmp16
    tmp20 = tmp18 - tmp19
    tmp21 = tmp20 + tmp3
    tmp22 = tl_math.abs(tmp21)
    tmp23 = tmp17 + tmp22
    tmp26 = tmp24 - tmp25
    tmp27 = tmp26 + tmp3
    tmp28 = tl_math.abs(tmp27)
    tmp31 = tmp29 - tmp30
    tmp32 = tmp31 + tmp3
    tmp33 = tl_math.abs(tmp32)
    tmp34 = tmp28 + tmp33
    tmp37 = tmp35 - tmp36
    tmp38 = tmp37 + tmp3
    tmp39 = tl_math.abs(tmp38)
    tmp40 = tmp34 + tmp39
    tmp43 = tmp41 - tmp42
    tmp44 = tmp43 + tmp3
    tmp45 = tl_math.abs(tmp44)
    tmp46 = tmp40 + tmp45
    tmp47 = tmp23 + tmp46
    tmp50 = tmp48 - tmp49
    tmp51 = tmp50 + tmp3
    tmp52 = tl_math.abs(tmp51)
    tmp55 = tmp53 - tmp54
    tmp56 = tmp55 + tmp3
    tmp57 = tl_math.abs(tmp56)
    tmp58 = tmp52 + tmp57
    tmp61 = tmp59 - tmp60
    tmp62 = tmp61 + tmp3
    tmp63 = tl_math.abs(tmp62)
    tmp64 = tmp58 + tmp63
    tmp67 = tmp65 - tmp66
    tmp68 = tmp67 + tmp3
    tmp69 = tl_math.abs(tmp68)
    tmp70 = tmp64 + tmp69
    tmp71 = tmp47 + tmp70
    tmp74 = tmp72 - tmp73
    tmp75 = tmp74 + tmp3
    tmp76 = tl_math.abs(tmp75)
    tmp79 = tmp77 - tmp78
    tmp80 = tmp79 + tmp3
    tmp81 = tl_math.abs(tmp80)
    tmp82 = tmp76 + tmp81
    tmp85 = tmp83 - tmp84
    tmp86 = tmp85 + tmp3
    tmp87 = tl_math.abs(tmp86)
    tmp88 = tmp82 + tmp87
    tmp91 = tmp89 - tmp90
    tmp92 = tmp91 + tmp3
    tmp93 = tl_math.abs(tmp92)
    tmp94 = tmp88 + tmp93
    tmp95 = tmp71 + tmp94
    tl.store(out_ptr0 + x0, tmp95, xmask)


@triton.jit
def triton_poi_fused_abs_add_sub_sum_1(in_ptr0, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (16 + x0), xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask)
    tmp6 = tl.load(in_ptr0 + (80 + x0), xmask)
    tmp7 = tl.load(in_ptr0 + (64 + x0), xmask)
    tmp12 = tl.load(in_ptr0 + (144 + x0), xmask)
    tmp13 = tl.load(in_ptr0 + (128 + x0), xmask)
    tmp18 = tl.load(in_ptr0 + (208 + x0), xmask)
    tmp19 = tl.load(in_ptr0 + (192 + x0), xmask)
    tmp24 = tl.load(in_ptr0 + (32 + x0), xmask)
    tmp28 = tl.load(in_ptr0 + (96 + x0), xmask)
    tmp33 = tl.load(in_ptr0 + (160 + x0), xmask)
    tmp38 = tl.load(in_ptr0 + (224 + x0), xmask)
    tmp44 = tl.load(in_ptr0 + (48 + x0), xmask)
    tmp48 = tl.load(in_ptr0 + (112 + x0), xmask)
    tmp53 = tl.load(in_ptr0 + (176 + x0), xmask)
    tmp58 = tl.load(in_ptr0 + (240 + x0), xmask)
    tmp2 = tmp0 - tmp1
    tmp3 = 1e-06
    tmp4 = tmp2 + tmp3
    tmp5 = tl_math.abs(tmp4)
    tmp8 = tmp6 - tmp7
    tmp9 = tmp8 + tmp3
    tmp10 = tl_math.abs(tmp9)
    tmp11 = tmp5 + tmp10
    tmp14 = tmp12 - tmp13
    tmp15 = tmp14 + tmp3
    tmp16 = tl_math.abs(tmp15)
    tmp17 = tmp11 + tmp16
    tmp20 = tmp18 - tmp19
    tmp21 = tmp20 + tmp3
    tmp22 = tl_math.abs(tmp21)
    tmp23 = tmp17 + tmp22
    tmp25 = tmp24 - tmp0
    tmp26 = tmp25 + tmp3
    tmp27 = tl_math.abs(tmp26)
    tmp29 = tmp28 - tmp6
    tmp30 = tmp29 + tmp3
    tmp31 = tl_math.abs(tmp30)
    tmp32 = tmp27 + tmp31
    tmp34 = tmp33 - tmp12
    tmp35 = tmp34 + tmp3
    tmp36 = tl_math.abs(tmp35)
    tmp37 = tmp32 + tmp36
    tmp39 = tmp38 - tmp18
    tmp40 = tmp39 + tmp3
    tmp41 = tl_math.abs(tmp40)
    tmp42 = tmp37 + tmp41
    tmp43 = tmp23 + tmp42
    tmp45 = tmp44 - tmp24
    tmp46 = tmp45 + tmp3
    tmp47 = tl_math.abs(tmp46)
    tmp49 = tmp48 - tmp28
    tmp50 = tmp49 + tmp3
    tmp51 = tl_math.abs(tmp50)
    tmp52 = tmp47 + tmp51
    tmp54 = tmp53 - tmp33
    tmp55 = tmp54 + tmp3
    tmp56 = tl_math.abs(tmp55)
    tmp57 = tmp52 + tmp56
    tmp59 = tmp58 - tmp38
    tmp60 = tmp59 + tmp3
    tmp61 = tl_math.abs(tmp60)
    tmp62 = tmp57 + tmp61
    tmp63 = tmp43 + tmp62
    tl.store(out_ptr0 + x0, tmp63, xmask)


@triton.jit
def triton_poi_fused_add_div_sum_2(in_ptr0, in_ptr1, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr0 + (4 + x0), xmask)
    tmp3 = tl.load(in_ptr0 + (8 + x0), xmask)
    tmp5 = tl.load(in_ptr1 + x0, xmask)
    tmp6 = tl.load(in_ptr1 + (4 + x0), xmask)
    tmp8 = tl.load(in_ptr1 + (8 + x0), xmask)
    tmp10 = tl.load(in_ptr1 + (12 + x0), xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp7 = tmp5 + tmp6
    tmp9 = tmp7 + tmp8
    tmp11 = tmp9 + tmp10
    tmp12 = tmp4 + tmp11
    tmp13 = 0.00390625
    tmp14 = tmp12 * tmp13
    tl.store(out_ptr0 + x0, tmp14, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((3, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_abs_add_sub_sum_0[grid(12)](arg0_1, buf0, 12,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_abs_add_sub_sum_1[grid(16)](arg0_1, buf1, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del arg0_1
        buf2 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_poi_fused_add_div_sum_2[grid(4)](buf0, buf1, buf2, 4, XBLOCK
            =4, num_warps=1, num_stages=1)
        del buf0
        del buf1
    return buf2,


class TotalVariationNew(nn.Module):
    """TotalVariation: calculates the total variation of a patch.

    Module providing the functionality necessary to calculate the total vatiation (TV) of an adversarial patch.

    """

    def __init__(self):
        super(TotalVariationNew, self).__init__()

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


Measured latency_ms: 0.039406