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.utils.data
import torch
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__to_copy__unsafe_index_add_arange_clamp_mul_sub_0(
    in_out_ptr0, in_out_ptr1, in_out_ptr3, in_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex // 16 % 4
    x1 = xindex // 4 % 4
    x0 = xindex % 4
    x3 = xindex // 64
    x6 = xindex
    tmp0 = x2
    tmp1 = tmp0.to(tl.float32)
    tmp2 = 0.5
    tmp3 = tmp1 + tmp2
    tmp4 = 1.0
    tmp5 = tmp3 * tmp4
    tmp6 = tmp5 - tmp2
    tmp7 = 0.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp9 = tmp8.to(tl.int32)
    tmp10 = x1
    tmp11 = tmp10.to(tl.float32)
    tmp12 = tmp11 + tmp2
    tmp13 = tmp12 * tmp4
    tmp14 = tmp13 - tmp2
    tmp15 = triton_helpers.maximum(tmp14, tmp7)
    tmp16 = tmp15.to(tl.int32)
    tmp17 = tl.full([1], 1, tl.int64)
    tmp18 = tmp16 + tmp17
    tmp19 = tl.full([1], 3, tl.int64)
    tmp20 = triton_helpers.minimum(tmp18, tmp19)
    tmp21 = x0
    tmp22 = tmp21.to(tl.float32)
    tmp23 = tmp22 + tmp2
    tmp24 = tmp23 * tmp4
    tmp25 = tmp24 - tmp2
    tmp26 = triton_helpers.maximum(tmp25, tmp7)
    tmp27 = tmp26.to(tl.int32)
    tmp28 = tmp27 + tmp17
    tmp29 = triton_helpers.minimum(tmp28, tmp19)
    tmp30 = tl.load(in_ptr0 + (tmp29 + 4 * tmp20 + 16 * tmp9 + 64 * x3),
        xmask, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr0 + (tmp27 + 4 * tmp20 + 16 * tmp9 + 64 * x3),
        xmask, eviction_policy='evict_last')
    tmp32 = tmp30 - tmp31
    tmp33 = tmp27.to(tl.float32)
    tmp34 = tmp26 - tmp33
    tmp35 = triton_helpers.maximum(tmp34, tmp7)
    tmp36 = triton_helpers.minimum(tmp35, tmp4)
    tmp37 = tmp32 * tmp36
    tmp38 = tmp31 + tmp37
    tmp39 = tl.load(in_ptr0 + (tmp29 + 4 * tmp16 + 16 * tmp9 + 64 * x3),
        xmask, eviction_policy='evict_last')
    tmp40 = tl.load(in_ptr0 + (tmp27 + 4 * tmp16 + 16 * tmp9 + 64 * x3),
        xmask, eviction_policy='evict_last')
    tmp41 = tmp39 - tmp40
    tmp42 = tmp41 * tmp36
    tmp43 = tmp40 + tmp42
    tmp44 = tmp9 + tmp17
    tmp45 = triton_helpers.minimum(tmp44, tmp19)
    tmp46 = tl.load(in_ptr0 + (tmp29 + 4 * tmp20 + 16 * tmp45 + 64 * x3),
        xmask, eviction_policy='evict_last')
    tmp47 = tl.load(in_ptr0 + (tmp27 + 4 * tmp20 + 16 * tmp45 + 64 * x3),
        xmask, eviction_policy='evict_last')
    tmp48 = tl.load(in_ptr0 + (tmp29 + 4 * tmp16 + 16 * tmp45 + 64 * x3),
        xmask, eviction_policy='evict_last')
    tmp49 = tl.load(in_ptr0 + (tmp27 + 4 * tmp16 + 16 * tmp45 + 64 * x3),
        xmask, eviction_policy='evict_last')
    tmp50 = tmp48 - tmp49
    tmp51 = tmp50 * tmp36
    tmp52 = tmp49 + tmp51
    tmp53 = tmp46 - tmp47
    tmp54 = tmp53 * tmp36
    tmp55 = tmp47 + tmp54
    tmp56 = tmp55 - tmp52
    tmp57 = tmp16.to(tl.float32)
    tmp58 = tmp15 - tmp57
    tmp59 = triton_helpers.maximum(tmp58, tmp7)
    tmp60 = triton_helpers.minimum(tmp59, tmp4)
    tmp61 = tmp56 * tmp60
    tmp62 = tmp52 + tmp61
    tmp63 = tmp38 - tmp43
    tmp64 = tmp63 * tmp60
    tmp65 = tmp43 + tmp64
    tmp66 = tmp62 - tmp65
    tmp67 = tmp9.to(tl.float32)
    tmp68 = tmp8 - tmp67
    tmp69 = triton_helpers.maximum(tmp68, tmp7)
    tmp70 = triton_helpers.minimum(tmp69, tmp4)
    tmp71 = tmp66 * tmp70
    tl.store(in_out_ptr0 + x6, tmp38, xmask)
    tl.store(in_out_ptr1 + x6, tmp43, xmask)
    tl.store(in_out_ptr3 + x6, tmp71, xmask)


@triton.jit
def triton_poi_fused_sum_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex // 64
    x4 = xindex % 64
    x1 = xindex // 4 % 4
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + (x4 + 256 * x3), xmask)
    tmp1 = tl.load(in_ptr1 + (x4 + 256 * x3), xmask)
    tmp19 = tl.load(in_ptr2 + (x4 + 256 * x3), xmask)
    tmp21 = tl.load(in_ptr0 + (64 + x4 + 256 * x3), xmask)
    tmp22 = tl.load(in_ptr1 + (64 + x4 + 256 * x3), xmask)
    tmp26 = tl.load(in_ptr2 + (64 + x4 + 256 * x3), xmask)
    tmp29 = tl.load(in_ptr0 + (128 + x4 + 256 * x3), xmask)
    tmp30 = tl.load(in_ptr1 + (128 + x4 + 256 * x3), xmask)
    tmp34 = tl.load(in_ptr2 + (128 + x4 + 256 * x3), xmask)
    tmp37 = tl.load(in_ptr0 + (192 + x4 + 256 * x3), xmask)
    tmp38 = tl.load(in_ptr1 + (192 + x4 + 256 * x3), xmask)
    tmp42 = tl.load(in_ptr2 + (192 + x4 + 256 * x3), xmask)
    tmp2 = tmp1 - tmp0
    tmp3 = x1
    tmp4 = tmp3.to(tl.float32)
    tmp5 = 0.5
    tmp6 = tmp4 + tmp5
    tmp7 = 1.0
    tmp8 = tmp6 * tmp7
    tmp9 = tmp8 - tmp5
    tmp10 = 0.0
    tmp11 = triton_helpers.maximum(tmp9, tmp10)
    tmp12 = tmp11.to(tl.int32)
    tmp13 = tmp12.to(tl.float32)
    tmp14 = tmp11 - tmp13
    tmp15 = triton_helpers.maximum(tmp14, tmp10)
    tmp16 = triton_helpers.minimum(tmp15, tmp7)
    tmp17 = tmp2 * tmp16
    tmp18 = tmp0 + tmp17
    tmp20 = tmp18 + tmp19
    tmp23 = tmp22 - tmp21
    tmp24 = tmp23 * tmp16
    tmp25 = tmp21 + tmp24
    tmp27 = tmp25 + tmp26
    tmp28 = tmp20 + tmp27
    tmp31 = tmp30 - tmp29
    tmp32 = tmp31 * tmp16
    tmp33 = tmp29 + tmp32
    tmp35 = tmp33 + tmp34
    tmp36 = tmp28 + tmp35
    tmp39 = tmp38 - tmp37
    tmp40 = tmp39 * tmp16
    tmp41 = tmp37 + tmp40
    tmp43 = tmp41 + tmp42
    tmp44 = tmp36 + tmp43
    tl.store(out_ptr0 + x5, tmp44, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4, 4), (256, 64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf6 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        buf7 = buf6
        del buf6
        buf8 = buf7
        del buf7
        buf9 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        buf10 = buf9
        del buf9
        buf0 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        buf5 = buf0
        del buf0
        buf11 = buf5
        del buf5
        get_raw_stream(0)
        triton_poi_fused__to_copy__unsafe_index_add_arange_clamp_mul_sub_0[grid
            (1024)](buf8, buf10, buf11, arg0_1, 1024, XBLOCK=256, num_warps
            =4, num_stages=1)
        del arg0_1
        buf12 = empty_strided_cuda((4, 1, 4, 4, 4), (64, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused_sum_1[grid(256)](buf10, buf8, buf11, buf12, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del buf10
        del buf11
        del buf8
    return buf12,


class LinearAdditiveUpsampleNew(nn.Module):
    """Bi/Trilinear Additive Upsample

    Upsampling strategy described in Wojna et al (https://doi.org/10.1007/s11263-019-01170-8) to avoid checkerboard
    patterns while keeping a better performance for the convolution.

    Parameters:
        scale_factor (int)  -- the factor for the upsampling operation
        n_splits (float)    -- the channel reduction factor
        threed (bool)       -- 3D (true) or 2D (false) network

    """

    def __init__(self, scale_factor, n_splits, threed):
        super(LinearAdditiveUpsampleNew, self).__init__()
        self.scale_factor = scale_factor
        self.n_splits = n_splits
        if threed:
            self.mode = 'trilinear'
        else:
            self.mode = 'bilinear'

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


Measured latency_ms: 0.030796