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.autograd import Function
import math
import numbers
import numpy as np
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_stack_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 192
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4 % 12
    x0 = xindex % 4
    x2 = xindex // 48
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (16 + x0 + 4 * x1 + 64 * x2), tmp4 & xmask,
        other=0.0)
    tmp6 = tl.load(in_ptr0 + (32 + x0 + 4 * x1 + 64 * x2), tmp4 & xmask,
        other=0.0)
    tmp7 = tmp5 - tmp6
    tmp8 = 1.7320508075688772
    tmp9 = tmp7 * tmp8
    tmp10 = tl.load(in_ptr0 + (x0 + 4 * x1 + 64 * x2), tmp4 & xmask, other=0.0)
    tmp11 = 2.0
    tmp12 = tmp10 * tmp11
    tmp13 = tmp12 - tmp5
    tmp14 = tmp13 - tmp6
    tmp15 = libdevice.atan2(tmp9, tmp14)
    tmp16 = 6.283185307179586
    tmp17 = tmp15 % tmp16
    tmp18 = tl.full([1], 0, tl.int32)
    tmp19 = tmp17 != tmp18
    tmp20 = libdevice.signbit(tmp17
        ) if tmp17.dtype is tl.float32 else tmp17 < 0
    tmp21 = libdevice.signbit(tmp16
        ) if tmp16.dtype is tl.float32 else tmp16 < 0
    tmp22 = tmp20 != tmp21
    tmp23 = tmp19 & tmp22
    tmp24 = tmp17 + tmp16
    tmp25 = tl.where(tmp23, tmp24, tmp17)
    tmp26 = 0.15915494309189535
    tmp27 = tmp25 * tmp26
    tmp28 = tl.full(tmp27.shape, 0.0, tmp27.dtype)
    tmp29 = tl.where(tmp4, tmp27, tmp28)
    tmp30 = tmp0 >= tmp3
    tmp31 = tl.full([1], 8, tl.int64)
    tmp32 = tmp0 < tmp31
    tmp33 = tmp30 & tmp32
    tmp34 = tl.load(in_ptr0 + (x0 + 4 * (-4 + x1) + 64 * x2), tmp33 & xmask,
        other=0.0)
    tmp35 = tl.load(in_ptr0 + (16 + x0 + 4 * (-4 + x1) + 64 * x2), tmp33 &
        xmask, other=0.0)
    tmp36 = triton_helpers.minimum(tmp34, tmp35)
    tmp37 = tl.load(in_ptr0 + (32 + x0 + 4 * (-4 + x1) + 64 * x2), tmp33 &
        xmask, other=0.0)
    tmp38 = triton_helpers.minimum(tmp36, tmp37)
    tmp39 = tl.load(in_ptr0 + (48 + x0 + 4 * (-4 + x1) + 64 * x2), tmp33 &
        xmask, other=0.0)
    tmp40 = triton_helpers.minimum(tmp38, tmp39)
    tmp41 = triton_helpers.maximum(tmp34, tmp35)
    tmp42 = triton_helpers.maximum(tmp41, tmp37)
    tmp43 = triton_helpers.maximum(tmp42, tmp39)
    tmp44 = 1e-08
    tmp45 = tmp43 + tmp44
    tmp46 = tmp40 / tmp45
    tmp47 = 1.0
    tmp48 = tmp47 - tmp46
    tmp49 = tl.full(tmp48.shape, 0.0, tmp48.dtype)
    tmp50 = tl.where(tmp33, tmp48, tmp49)
    tmp51 = tmp0 >= tmp31
    tl.full([1], 12, tl.int64)
    tmp54 = tl.load(in_ptr0 + (x0 + 4 * (-8 + x1) + 64 * x2), tmp51 & xmask,
        other=0.0)
    tmp55 = tl.load(in_ptr0 + (16 + x0 + 4 * (-8 + x1) + 64 * x2), tmp51 &
        xmask, other=0.0)
    tmp56 = triton_helpers.maximum(tmp54, tmp55)
    tmp57 = tl.load(in_ptr0 + (32 + x0 + 4 * (-8 + x1) + 64 * x2), tmp51 &
        xmask, other=0.0)
    tmp58 = triton_helpers.maximum(tmp56, tmp57)
    tmp59 = tl.load(in_ptr0 + (48 + x0 + 4 * (-8 + x1) + 64 * x2), tmp51 &
        xmask, other=0.0)
    tmp60 = triton_helpers.maximum(tmp58, tmp59)
    tmp61 = tl.full(tmp60.shape, 0.0, tmp60.dtype)
    tmp62 = tl.where(tmp51, tmp60, tmp61)
    tmp63 = tl.where(tmp33, tmp50, tmp62)
    tmp64 = tl.where(tmp4, tmp29, tmp63)
    tl.store(out_ptr0 + x3, tmp64, xmask)


@triton.jit
def triton_poi_fused_index_put_lift_fresh_1(in_ptr0, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 192
    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 = tmp0 == tmp0
    tmp2 = tl_math.abs(tmp0)
    tmp3 = float('inf')
    tmp4 = tmp2 != tmp3
    tmp5 = tmp1 & tmp4
    tmp6 = tmp5 == 0
    tmp7 = 0.0
    tmp8 = tl.where(tmp6, tmp7, tmp0)
    tl.store(out_ptr0 + x0, tmp8, xmask)


@triton.jit
def triton_poi_fused_new_zeros_2(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 = 0.0
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_new_ones_3(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 = 1.0
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_add_copy_div_mul_remainder_4(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 192
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16 % 3
    x0 = xindex % 16
    x2 = xindex // 48
    x3 = xindex
    tmp6 = tl.load(in_ptr0 + (x0 + 48 * x2), xmask, eviction_policy=
        'evict_last')
    tmp7 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (16 + x0 + 48 * x2), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
    tmp29 = tl.load(in_ptr0 + x3, xmask)
    tmp0 = x1
    tmp1 = tl.full([1], 1, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = tmp1 == tmp3
    tmp5 = tmp3 == tmp3
    tmp8 = 255.0
    tmp9 = tmp7 * tmp8
    tmp10 = 0.002777777777777778
    tmp11 = tmp9 * tmp10
    tmp12 = tmp6 + tmp11
    tmp13 = tl.where(tmp5, tmp12, tmp6)
    tmp14 = 1.0
    tmp15 = tmp13 % tmp14
    tmp16 = tmp15 != tmp3
    tmp17 = libdevice.signbit(tmp15
        ) if tmp15.dtype is tl.float32 else tmp15 < 0
    tmp18 = libdevice.signbit(tmp14
        ) if tmp14.dtype is tl.float32 else tmp14 < 0
    tmp19 = tmp17 != tmp18
    tmp20 = tmp16 & tmp19
    tmp21 = tmp15 + tmp14
    tmp22 = tl.where(tmp20, tmp21, tmp15)
    tmp24 = tl.where(tmp4, tmp12, tmp23)
    tmp25 = tl.where(tmp4, tmp22, tmp24)
    tmp27 = tmp25 * tmp26
    tmp28 = tmp0 == tmp3
    tmp30 = tl.where(tmp28, tmp12, tmp29)
    tmp31 = tl.where(tmp28, tmp22, tmp30)
    tmp32 = tl.where(tmp2, tmp27, tmp31)
    tl.store(out_ptr0 + x3, tmp32, xmask)


@triton.jit
def triton_per_fused_add_clamp_copy_index_mean_minimum_mul_remainder_rsub_sub_5(
    in_ptr0, in_ptr1, in_ptr2, out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 12
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    x0 = xindex % 3
    r2 = rindex
    x1 = xindex // 3
    x3 = xindex
    tmp13 = tl.load(in_ptr0 + (32 + r2 + 48 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp14 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr0 + (r2 + 48 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp42 = tl.load(in_ptr0 + (16 + r2 + 48 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp57 = tl.load(in_ptr2 + x1, xmask, eviction_policy='evict_last')
    tmp0 = x0
    tmp1 = tl.full([1, 1], 1, tl.int64)
    tmp2 = tmp0 < tmp1
    tmp3 = tl.full([1, 1], 2, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = 3.0
    tmp6 = 1.0
    tmp7 = tl.where(tmp4, tmp5, tmp6)
    tmp8 = 5.0
    tmp9 = tl.where(tmp2, tmp8, tmp7)
    tmp10 = tl.full([1, 1], 0, tl.int32)
    tmp11 = tl.full([1, 1], 2, tl.int32)
    tmp12 = tmp10 == tmp11
    tmp15 = tmp13 * tmp14
    tmp17 = tl.where(tmp12, tmp15, tmp16)
    tmp18 = 0.0
    tmp19 = triton_helpers.maximum(tmp17, tmp18)
    tmp20 = triton_helpers.minimum(tmp19, tmp6)
    tmp21 = 6.0
    tmp22 = tmp20 * tmp21
    tmp23 = tmp9 + tmp22
    tmp24 = tmp23 % tmp21
    tmp25 = tmp24 != tmp10
    tmp26 = libdevice.signbit(tmp24
        ) if tmp24.dtype is tl.float32 else tmp24 < 0
    tmp27 = libdevice.signbit(tmp21
        ) if tmp21.dtype is tl.float32 else tmp21 < 0
    tmp28 = tmp26 != tmp27
    tmp29 = tmp25 & tmp28
    tmp30 = tmp24 + tmp21
    tmp31 = tl.where(tmp29, tmp30, tmp24)
    tmp32 = 4.0
    tmp33 = tmp32 - tmp31
    tmp34 = triton_helpers.minimum(tmp31, tmp33)
    tmp35 = triton_helpers.maximum(tmp34, tmp18)
    tmp36 = tmp11 == tmp11
    tmp37 = tl.where(tmp36, tmp15, tmp13)
    tmp38 = triton_helpers.maximum(tmp37, tmp18)
    tmp39 = triton_helpers.minimum(tmp38, tmp6)
    tmp40 = tl.full([1, 1], 1, tl.int32)
    tmp41 = tmp40 == tmp11
    tmp43 = tl.where(tmp41, tmp15, tmp42)
    tmp44 = triton_helpers.maximum(tmp43, tmp18)
    tmp45 = triton_helpers.minimum(tmp44, tmp6)
    tmp46 = tmp39 * tmp45
    tmp47 = triton_helpers.minimum(tmp35, tmp6)
    tmp48 = tmp46 * tmp47
    tmp49 = tmp39 - tmp48
    tmp50 = tl.broadcast_to(tmp49, [XBLOCK, RBLOCK])
    tmp52 = tl.where(xmask, tmp50, 0)
    tmp53 = tl.sum(tmp52, 1)[:, None]
    tmp54 = 16.0
    tmp55 = tmp53 / tmp54
    tmp56 = tmp49 - tmp55
    tmp58 = tmp56 * tmp57
    tmp59 = tmp58 + tmp55
    tmp60 = triton_helpers.maximum(tmp59, tmp18)
    tmp61 = triton_helpers.minimum(tmp60, tmp6)
    tl.store(out_ptr1 + (r2 + 16 * x3), tmp61, 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((4, 12, 4), (48, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_stack_0[grid(192)](arg0_1, buf0, 192, XBLOCK=128,
            num_warps=4, num_stages=1)
        del arg0_1
        triton_poi_fused_index_put_lift_fresh_1[grid(192)](buf0, buf0, 192,
            XBLOCK=256, num_warps=4, num_stages=1)
        buf7 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        triton_poi_fused_new_zeros_2[grid(4)](buf7, 4, XBLOCK=4, num_warps=
            1, num_stages=1)
        buf8 = torch.ops.aten.uniform.default(buf7, -4.0, 4.0)
        buf9 = buf8
        del buf8
        buf10 = buf7
        del buf7
        triton_poi_fused_new_ones_3[grid(4)](buf10, 4, XBLOCK=4, num_warps=
            1, num_stages=1)
        buf11 = torch.ops.aten.uniform.default(buf10, 0.0, 5.0)
        del buf10
        buf12 = buf11
        del buf11
        buf13 = empty_strided_cuda((4, 3, 4, 4), (48, 16, 4, 1), torch.float32)
        triton_poi_fused_add_copy_div_mul_remainder_4[grid(192)](buf0, buf9,
            buf12, buf13, 192, XBLOCK=256, num_warps=4, num_stages=1)
        del buf12
        buf14 = buf9
        del buf9
        triton_poi_fused_new_ones_3[grid(4)](buf14, 4, XBLOCK=4, num_warps=
            1, num_stages=1)
        buf15 = torch.ops.aten.uniform.default(buf14, 0.0, 5.0)
        buf16 = buf15
        del buf15
        buf20 = reinterpret_tensor(buf14, (4, 1, 1, 1), (1, 1, 1, 1), 0)
        del buf14
        buf21 = torch.ops.aten.uniform.default(buf20, 0.0, 5.0)
        del buf20
        buf22 = buf21
        del buf21
        buf23 = reinterpret_tensor(buf0, (4, 3, 4, 4), (48, 16, 4, 1), 0)
        del buf0
        triton_per_fused_add_clamp_copy_index_mean_minimum_mul_remainder_rsub_sub_5[
            grid(12)](buf13, buf16, buf22, buf23, 12, 16, XBLOCK=8,
            num_warps=2, num_stages=1)
        del buf13
        del buf16
        del buf22
    return buf23,


def hsv2rgb(hsv):
    """Convert a 4-d HSV tensor to the RGB counterpart.
    >>> %timeit hsv2rgb_lookup(hsv)
    2.37 ms ± 13.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    >>> %timeit hsv2rgb(rgb)
    298 µs ± 542 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    >>> torch.allclose(hsv2rgb(hsv), hsv2rgb_lookup(hsv), atol=1e-6)
    True
    References
    [1] https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative
    """
    h, s, v = hsv[:, [0]], hsv[:, [1]], hsv[:, [2]]
    c = v * s
    n = hsv.new_tensor([5, 3, 1]).view(3, 1, 1)
    k = (n + h * 6) % 6
    t = torch.min(k, 4.0 - k)
    t = torch.clamp(t, 0, 1)
    return v - c * t


def rgb2hsv(rgb):
    """Convert a 4-d RGB tensor to the HSV counterpart.
    Here, we compute hue using atan2() based on the definition in [1],
    instead of using the common lookup table approach as in [2, 3].
    Those values agree when the angle is a multiple of 30°,
    otherwise they may differ at most ~1.2°.
    >>> %timeit rgb2hsv_lookup(rgb)
    1.07 ms ± 2.96 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    >>> %timeit rgb2hsv(rgb)
    380 µs ± 555 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    >>> (rgb2hsv_lookup(rgb) - rgb2hsv(rgb)).abs().max()
    tensor(0.0031, device='cuda:0')
    References
    [1] https://en.wikipedia.org/wiki/Hue
    [2] https://www.rapidtables.com/convert/color/rgb-to-hsv.html
    [3] https://github.com/scikit-image/scikit-image/blob/master/skimage/color/colorconv.py#L212
    """
    r, g, b = rgb[:, 0, :, :], rgb[:, 1, :, :], rgb[:, 2, :, :]
    Cmax = rgb.max(1)[0]
    Cmin = rgb.min(1)[0]
    hue = torch.atan2(math.sqrt(3) * (g - b), 2 * r - g - b)
    hue = hue % (2 * math.pi) / (2 * math.pi)
    saturate = 1 - Cmin / (Cmax + 1e-08)
    value = Cmax
    hsv = torch.stack([hue, saturate, value], dim=1)
    hsv[~torch.isfinite(hsv)] = 0.0
    return hsv


class RandomHSVFunction(Function):

    @staticmethod
    def forward(ctx, x, f_h, f_s, f_v):
        x = rgb2hsv(x)
        h = x[:, 0, :, :]
        h += f_h * 255.0 / 360.0
        h = h % 1
        x[:, 0, :, :] = h
        x[:, 1, :, :] = x[:, 1, :, :] * f_s
        x[:, 2, :, :] = x[:, 2, :, :] * f_v
        x = torch.clamp(x, 0, 1)
        x = hsv2rgb(x)
        return x

    @staticmethod
    def backward(ctx, grad_output):
        grad_input = None
        if ctx.needs_input_grad[0]:
            grad_input = grad_output.clone()
        return grad_input, None, None, None


class ColorJitterLayerNew(nn.Module):

    def __init__(self, brightness, contrast, saturation, hue):
        super(ColorJitterLayerNew, self).__init__()
        self.brightness = self._check_input(brightness, 'brightness')
        self.contrast = self._check_input(contrast, 'contrast')
        self.saturation = self._check_input(saturation, 'saturation')
        self.hue = self._check_input(hue, 'hue', center=0, bound=(-0.5, 0.5
            ), clip_first_on_zero=False)

    def _check_input(self, value, name, center=1, bound=(0, float('inf')),
        clip_first_on_zero=True):
        if isinstance(value, numbers.Number):
            if value < 0:
                raise ValueError(
                    'If {} is a single number, it must be non negative.'.
                    format(name))
            value = [center - value, center + value]
            if clip_first_on_zero:
                value[0] = max(value[0], 0)
        elif isinstance(value, (tuple, list)) and len(value) == 2:
            if not bound[0] <= value[0] <= value[1] <= bound[1]:
                raise ValueError('{} values should be between {}'.format(
                    name, bound))
        else:
            raise TypeError(
                '{} should be a single number or a list/tuple with lenght 2.'
                .format(name))
        if value[0] == value[1] == center:
            value = None
        return value

    def adjust_contrast(self, x):
        if self.contrast:
            factor = x.new_empty(x.size(0), 1, 1, 1).uniform_(*self.contrast)
            means = torch.mean(x, dim=[2, 3], keepdim=True)
            x = (x - means) * factor + means
        return torch.clamp(x, 0, 1)

    def adjust_hsv(self, x):
        f_h = x.new_zeros(x.size(0), 1, 1)
        f_s = x.new_ones(x.size(0), 1, 1)
        f_v = x.new_ones(x.size(0), 1, 1)
        if self.hue:
            f_h.uniform_(*self.hue)
        if self.saturation:
            f_s = f_s.uniform_(*self.saturation)
        if self.brightness:
            f_v = f_v.uniform_(*self.brightness)
        return RandomHSVFunction.apply(x, f_h, f_s, f_v)

    def transform(self, inputs):
        if np.random.rand() > 0.5:
            transforms = [self.adjust_contrast, self.adjust_hsv]
        else:
            transforms = [self.adjust_hsv, self.adjust_contrast]
        for t in transforms:
            inputs = t(inputs)
        return inputs

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


Measured latency_ms: 0.127578