jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_interpolate_illegal_memory_access(self):
        in_s = 45
        out_s = 14

        input = torch.ones((1, 1, in_s), device='cuda', requires_grad=True)
        # note we allocated grad_output to be larger so out of bound access
        # would be visible in grad_input
        grad = torch.ones((1, 1, out_s * 2), device='cuda', requires_grad=True)
        grad = grad[:, :, :out_s]

        input_ref = input.detach().cpu().requires_grad_()
        grad_ref = grad.cpu()

        out = F.interpolate(input, size=(out_s,), mode='nearest')
        out.backward(grad)

        out_ref = F.interpolate(input_ref, size=(out_s,), mode='nearest')
        out_ref.backward(grad_ref)

        self.assertEqual(out_ref, out)
        self.assertEqual(input_ref.grad, input.grad)
//  [python]
def test_upsamplingNearest1d_correctness(self, device, isize, osize):
        # Here we check if output matches OpenCV's INTER_NEAREST-like result
        in_t = torch.arange(isize, dtype=torch.float, device=device).unsqueeze(0).unsqueeze(0)
        out_t = t.nn.functional.interpolate(
            in_t, size=(osize, ), recompute_scale_factor=False, mode="nearest"
        )
        # compute expected output as OpenCV
        expected_out = torch.zeros(osize, dtype=torch.float).unsqueeze(0).unsqueeze(0)
        scale = 1.0 * isize / osize
        for o in range(osize):
            i_f32 = o * scale
            i = int(i_f32)
            expected_out[0, 0, o] = in_t[0, 0, i]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_upsamplingNearest1d_correctness(self, device, isize, osize):
        # Here we check if output matches OpenCV's INTER_NEAREST-like result
        in_t = torch.arange(isize, dtype=torch.float, device=device).unsqueeze(0).unsqueeze(0)
        out_t = F.interpolate(
            in_t, size=(osize, ), recompute_scale_factor=False, mode="nearest"
        )
        # compute expected output as OpenCV
        expected_out = torch.zeros(osize, dtype=torch.float).unsqueeze(0).unsqueeze(0)
        scale = 1.0 * isize / osize
        for o in range(osize):
            i_f32 = o * scale
            i = int(i_f32)
            expected_out[0, 0, o] = in_t[0, 0, i]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_upsamplingNearestExact1d_correctness(self, device, isize, osize):
        # Here we check if output matches Scikit-Image/Scipy-like result
        # Checks https://github.com/pytorch/pytorch/issues/34808
        in_t = torch.arange(isize, dtype=torch.float, device=device).unsqueeze(0).unsqueeze(0)
        out_t = t.upsample_nearest(in_t, size=(osize,))
        # compute expected output as scikit-image/scipy
        expected_out = torch.zeros(osize, dtype=torch.float).unsqueeze(0).unsqueeze(0)
        scale = 1.0 * isize / osize
        for o in range(osize):
            i_f32 = (o + 0.5) * scale
            i = int(i_f32)
            expected_out[0, 0, o] = in_t[0, 0, i]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_upsamplingNearestExact1d_correctness(self, device, isize, osize):
        # Here we check if output matches Scikit-Image/Scipy-like result
        # Checks https://github.com/pytorch/pytorch/issues/34808
        in_t = torch.arange(isize, dtype=torch.float, device=device).unsqueeze(0).unsqueeze(0)
        out_t = F.interpolate(
            in_t, size=(osize, ), recompute_scale_factor=False, mode="nearest-exact"
        )
        # compute expected output as scikit-image/scipy
        expected_out = torch.zeros(osize, dtype=torch.float).unsqueeze(0).unsqueeze(0)
        scale = 1.0 * isize / osize
        for o in range(osize):
            i_f32 = (o + 0.5) * scale
            i = int(i_f32)
            expected_out[0, 0, o] = in_t[0, 0, i]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_upsamplingNearest2d_correctness(self, device, memory_format, isize, osize):
        # Here we check if output matches OpenCV's INTER_NEAREST-like result
        in_t = torch.arange(isize * isize, dtype=torch.float, device=device).reshape(1, 1, isize, isize)
        in_t = in_t.contiguous(memory_format=memory_format)
        out_t = t.nn.functional.interpolate(
            in_t, size=(osize, osize), recompute_scale_factor=False, mode="nearest"
        )
        # compute expected output as OpenCV
        expected_out = torch.zeros(1, 1, osize, osize, dtype=torch.float)
        scale = 1.0 * isize / osize
        for o1 in range(osize):
            i1_f32 = o1 * scale
            i1 = int(i1_f32)
            for o2 in range(osize):
                i2_f32 = o2 * scale
                i2 = int(i2_f32)
                expected_out[0, 0, o1, o2] = in_t[0, 0, i1, i2]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_upsamplingNearest2d_correctness(self, device, memory_format, isize, osize):
        # Here we check if output matches OpenCV's INTER_NEAREST-like result
        in_t = torch.arange(isize * isize, dtype=torch.float, device=device).reshape(1, 1, isize, isize)
        in_t = in_t.contiguous(memory_format=memory_format)
        out_t = F.interpolate(
            in_t, size=(osize, osize), recompute_scale_factor=False, mode="nearest"
        )
        # compute expected output as OpenCV
        expected_out = torch.zeros(1, 1, osize, osize, dtype=torch.float)
        scale = 1.0 * isize / osize
        for o1 in range(osize):
            i1_f32 = o1 * scale
            i1 = int(i1_f32)
            for o2 in range(osize):
                i2_f32 = o2 * scale
                i2 = int(i2_f32)
                expected_out[0, 0, o1, o2] = in_t[0, 0, i1, i2]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_upsamplingNearestExact2d_correctness(self, device, memory_format, isize, osize):
        # Here we check if output matches Scikit-Image/Scipy-like result
        # Checks https://github.com/pytorch/pytorch/issues/34808
        in_t = torch.arange(isize * isize, dtype=torch.float, device=device).reshape(1, 1, isize, isize)
        in_t = in_t.contiguous(memory_format=memory_format)
        out_t = t.upsample_nearest(
            in_t, size=(osize, osize)
        )
        # compute expected output as Scikit-Image/Scipy
        expected_out = torch.zeros(1, 1, osize, osize, dtype=torch.float)
        scale = 1.0 * isize / osize
        for o1 in range(osize):
            i1_f32 = (o1 + 0.5) * scale
            i1 = int(i1_f32)
            for o2 in range(osize):
                i2_f32 = (o2 + 0.5) * scale
                i2 = int(i2_f32)
                expected_out[0, 0, o1, o2] = in_t[0, 0, i1, i2]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_upsamplingNearestExact2d_correctness(self, device, memory_format, isize, osize):
        # Here we check if output matches Scikit-Image/Scipy-like result
        # Checks https://github.com/pytorch/pytorch/issues/34808
        in_t = torch.arange(isize * isize, dtype=torch.float, device=device).reshape(1, 1, isize, isize)
        in_t = in_t.contiguous(memory_format=memory_format)
        out_t = F.interpolate(
            in_t, size=(osize, osize), recompute_scale_factor=False, mode="nearest-exact"
        )
        # compute expected output as Scikit-Image/Scipy
        expected_out = torch.zeros(1, 1, osize, osize, dtype=torch.float)
        scale = 1.0 * isize / osize
        for o1 in range(osize):
            i1_f32 = (o1 + 0.5) * scale
            i1 = int(i1_f32)
            for o2 in range(osize):
                i2_f32 = (o2 + 0.5) * scale
                i2 = int(i2_f32)
                expected_out[0, 0, o1, o2] = in_t[0, 0, i1, i2]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_interpolate_buffer_overflow(self):
        # Test buffer overflow issue due to inaccurate floating point
        # representation for integer values. See issue below for details.
        # https://github.com/pytorch/pytorch/issues/88939

        def helper(size, dtype, mode, device, is_channels_last):
            input = torch.ones(size, dtype=dtype, device=device)
            if is_channels_last:
                if len(size) == 3:
                    input = input.transpose(1, 2).contiguous().transpose(1, 2)
                elif len(size) == 4:
                    input = input.to(memory_format=torch.channels_last)
                else:
                    input = input.to(memory_format=torch.channels_last_3d)
            output1 = t.interpolate(input, 2, mode=mode, align_corners=True)
            # reset the corner value and expect the output is changed as well
            # the output won't be changed on buffer overflow
            input[(-1,) * len(size)] = 0.5
            output2 = t.interpolate(input, 2, mode=mode, align_corners=True)
            self.assertNotEqual(output1, output2)

        size_dtype_list = []
        # We set the size larger than the floating point exactly representable range
        # float: exact representable range (-2**24,2**24)
        size_dtype_list.append(([1, 10, 2**24 + 4], torch.float))
        size_dtype_list.append(([1, 10, 2, 2**24 + 4], torch.float))
        size_dtype_list.append(([1, 10, 2, 2, 2**24 + 4], torch.float))
        # bfloat16: exact representable range (-2**8, 2**8)
        size_dtype_list.append(([1, 10, 2**8 + 4], torch.bfloat16))
        size_dtype_list.append(([1, 10, 2, 2**8 + 4], torch.bfloat16))
        size_dtype_list.append(([1, 10, 2, 2, 2**8 + 4], torch.bfloat16))
        # half: exact representable range (-2**11, 2**11)
        size_dtype_list.append(([1, 10, 2**11 + 4], torch.half))
        size_dtype_list.append(([1, 10, 2, 2**11 + 4], torch.half))
        size_dtype_list.append(([1, 10, 2, 2, 2**11 + 4], torch.half))

        # TODO: turn on cuda test after buffer overflow issue is fixed in cuda kernel
        # devices = ['cpu'] + (['cuda'] if torch.cuda.is_available() else [])
        devices = ['cpu']

        for mode in ('linear', 'bilinear', 'bicubic', 'trilinear'):
            for size_dtype in size_dtype_list:
                size, dtype = size_dtype
                if (
                    mode == 'linear' and len(size) != 3
                    or (mode == 'bilinear' and len(size) != 4)
                    or (mode == 'bicubic' and len(size) != 4)
                    or (mode == 'trilinear' and len(size) != 5)
                ):
                    continue
                for device in devices:
                    if (
                        device == 'cpu' and dtype == torch.half
                        or (device == 'cuda' and dtype == torch.bfloat16)
                    ):
                        # no half precision support on cpu or bfloat16 on cuda yet
                        continue
                    for is_channels_last in (True, False):
                        helper(size, dtype, mode, device, is_channels_last)
//  [python]
def test_interpolate_buffer_overflow(self):
        # Test buffer overflow issue due to inaccurate floating point
        # representation for integer values. See issue below for details.
        # https://github.com/pytorch/pytorch/issues/88939

        def helper(size, dtype, mode, device, is_channels_last):
            input = torch.ones(size, dtype=dtype, device=device)
            if is_channels_last:
                if len(size) == 3:
                    input = input.transpose(1, 2).contiguous().transpose(1, 2)
                elif len(size) == 4:
                    input = input.to(memory_format=torch.channels_last)
                else:
                    input = input.to(memory_format=torch.channels_last_3d)
            output1 = F.interpolate(input, 2, mode=mode, align_corners=True)
            # reset the corner value and expect the output is changed as well
            # the output won't be changed on buffer overflow
            input[(-1,) * len(size)] = 0.5
            output2 = F.interpolate(input, 2, mode=mode, align_corners=True)
            self.assertNotEqual(output1, output2)

        size_dtype_list = []
        # We set the size larger than the floating point exactly representable range
        # float: exact representable range (-2**24,2**24)
        size_dtype_list.append(([1, 10, 2**24 + 4], torch.float))
        size_dtype_list.append(([1, 10, 2, 2**24 + 4], torch.float))
        size_dtype_list.append(([1, 10, 2, 2, 2**24 + 4], torch.float))
        # bfloat16: exact representable range (-2**8, 2**8)
        size_dtype_list.append(([1, 10, 2**8 + 4], torch.bfloat16))
        size_dtype_list.append(([1, 10, 2, 2**8 + 4], torch.bfloat16))
        size_dtype_list.append(([1, 10, 2, 2, 2**8 + 4], torch.bfloat16))
        # half: exact representable range (-2**11, 2**11)
        size_dtype_list.append(([1, 10, 2**11 + 4], torch.half))
        size_dtype_list.append(([1, 10, 2, 2**11 + 4], torch.half))
        size_dtype_list.append(([1, 10, 2, 2, 2**11 + 4], torch.half))

        # TODO: turn on cuda test after buffer overflow issue is fixed in cuda kernel
        # devices = ['cpu'] + (['cuda'] if torch.cuda.is_available() else [])
        devices = ['cpu']

        for mode in ('linear', 'bilinear', 'bicubic', 'trilinear'):
            for size_dtype in size_dtype_list:
                size, dtype = size_dtype
                if (
                    mode == 'linear' and len(size) != 3
                    or (mode == 'bilinear' and len(size) != 4)
                    or (mode == 'bicubic' and len(size) != 4)
                    or (mode == 'trilinear' and len(size) != 5)
                ):
                    continue
                for device in devices:
                    if (
                        device == 'cpu' and dtype == torch.half
                        or (device == 'cuda' and dtype == torch.bfloat16)
                    ):
                        # no half precision support on cpu or bfloat16 on cuda yet
                        continue
                    for is_channels_last in (True, False):
                        helper(size, dtype, mode, device, is_channels_last)
//  [python]
def test_upsamplingNearest3d_correctness(self, device, memory_format, isize, osize):
        # Here we check if output matches OpenCV's INTER_NEAREST-like result
        in_t = torch.arange(isize * isize * isize, dtype=torch.float, device=device)
        in_t = in_t.reshape(1, 1, isize, isize, isize)
        in_t = in_t.contiguous(memory_format=memory_format)
        out_t = t.nn.functional.interpolate(
            in_t, size=(osize, osize, osize), recompute_scale_factor=False, mode="nearest"
        )
        # compute expected output as OpenCV
        expected_out = torch.zeros(1, 1, osize, osize, osize, dtype=torch.float)
        scale = 1.0 * isize / osize
        for o1 in range(osize):
            i1_f32 = o1 * scale
            i1 = int(i1_f32)
            for o2 in range(osize):
                i2_f32 = o2 * scale
                i2 = int(i2_f32)
                for o3 in range(osize):
                    i3_f32 = o3 * scale
                    i3 = int(i3_f32)
                    expected_out[0, 0, o1, o2, o3] = in_t[0, 0, i1, i2, i3]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_upsamplingNearest3d_correctness(self, device, memory_format, isize, osize):
        # Here we check if output matches OpenCV's INTER_NEAREST-like result
        in_t = torch.arange(isize * isize * isize, dtype=torch.float, device=device)
        in_t = in_t.reshape(1, 1, isize, isize, isize)
        in_t = in_t.contiguous(memory_format=memory_format)
        out_t = F.interpolate(
            in_t, size=(osize, osize, osize), recompute_scale_factor=False, mode="nearest"
        )
        # compute expected output as OpenCV
        expected_out = torch.zeros(1, 1, osize, osize, osize, dtype=torch.float)
        scale = 1.0 * isize / osize
        for o1 in range(osize):
            i1_f32 = o1 * scale
            i1 = int(i1_f32)
            for o2 in range(osize):
                i2_f32 = o2 * scale
                i2 = int(i2_f32)
                for o3 in range(osize):
                    i3_f32 = o3 * scale
                    i3 = int(i3_f32)
                    expected_out[0, 0, o1, o2, o3] = in_t[0, 0, i1, i2, i3]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_interpolate(self):
        def _test_interpolate_non_integer_size_warning(in_t, out_size, dim, **kwargs):
            test_sizes = [float(out_size),
                          torch.tensor(out_size, dtype=torch.float)]
            for size in test_sizes:
                self.assertRaisesRegex(TypeError,
                                       "(expected size to be one of int or).*",
                                       F.interpolate, in_t, size=(size,) * dim, **kwargs)

        def _test_interpolate_helper(in_t, scale_factor, layer):
            out_size = int(math.floor(in_t.shape[-1] * scale_factor))
            dim = len(in_t.shape) - 2
            out_shape = [1, 1] + [out_size] * dim
            with warnings.catch_warnings(record=True) as w:
                out_t = layer(in_t)
            self.assertEqual(torch.ones(out_shape), out_t)

            self.assertEqual(
                F.interpolate(in_t, (out_size,) * dim, **kwargs),
                F.interpolate(in_t, scale_factor=scale_factor, **kwargs))
            gradcheck(lambda x: F.interpolate(x, out_size, **kwargs), [in_t], nondet_tol=GRADCHECK_NONDET_TOL)
            gradgradcheck(lambda x: F.interpolate(x, out_size, **kwargs), [in_t], nondet_tol=GRADCHECK_NONDET_TOL)
            _test_interpolate_non_integer_size_warning(in_t, out_size, dim, **kwargs)

        def _make_input(dim, device):
            size = [1, 1]
            size += [2] * dim
            return torch.ones(size, requires_grad=True, device=device)

        device_list = ['cpu']
        if TEST_CUDA:
            device_list.append('cuda')

        for device in device_list:
            for scale_factor in [0.5, 1.5, 2]:
                for mode in ['nearest', 'area']:
                    kwargs = dict(mode=mode)
                    m = nn.Upsample(scale_factor=scale_factor, **kwargs).to(device)
                    for input in [_make_input(1, device), _make_input(2, device), _make_input(3, device)]:
                        _test_interpolate_helper(input, scale_factor, m)

                for align_corners in [True, False]:
                    kwargs = dict(mode='linear', align_corners=align_corners)
                    m = nn.Upsample(scale_factor=scale_factor, **kwargs).to(device)
                    _test_interpolate_helper(_make_input(1, device), scale_factor, m)

                    kwargs = dict(mode='bilinear', align_corners=align_corners)
                    m = nn.Upsample(scale_factor=scale_factor, **kwargs).to(device)
                    _test_interpolate_helper(_make_input(2, device), scale_factor, m)

                    kwargs = dict(mode='bicubic', align_corners=align_corners)

                    def m(t):
                        return F.interpolate(t, scale_factor=scale_factor, **kwargs).to(device)
                    _test_interpolate_helper(_make_input(2, device), scale_factor, m)

                    kwargs = dict(mode='trilinear', align_corners=align_corners)
                    m = nn.Upsample(scale_factor=scale_factor, **kwargs).to(device)
                    _test_interpolate_helper(_make_input(3, device), scale_factor, m)
//  [python]
def test_upsamplingNearestExact3d_correctness(self, device, memory_format, isize, osize):
        # Here we check if output matches Scikit-Image/Scipy-like result
        # Checks https://github.com/pytorch/pytorch/issues/34808
        in_t = torch.arange(isize * isize * isize, dtype=torch.float, device=device)
        in_t = in_t.reshape(1, 1, isize, isize, isize)
        in_t = in_t.contiguous(memory_format=memory_format)
        out_t = t.interpolate(
            in_t, size=(osize, osize, osize), recompute_scale_factor=False, mode="nearest-exact"
        )
        # compute expected output as Scikit-Image/Scipy
        expected_out = torch.zeros(1, 1, osize, osize, osize, dtype=torch.float)
        scale = 1.0 * isize / osize
        for o1 in range(osize):
            i1_f32 = (o1 + 0.5) * scale
            i1 = int(i1_f32)
            for o2 in range(osize):
                i2_f32 = (o2 + 0.5) * scale
                i2 = int(i2_f32)
                for o3 in range(osize):
                    i3_f32 = (o3 + 0.5) * scale
                    i3 = int(i3_f32)
                    expected_out[0, 0, o1, o2, o3] = in_t[0, 0, i1, i2, i3]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_upsamplingNearestExact3d_correctness(self, device, memory_format, isize, osize):
        # Here we check if output matches Scikit-Image/Scipy-like result
        # Checks https://github.com/pytorch/pytorch/issues/34808
        in_t = torch.arange(isize * isize * isize, dtype=torch.float, device=device)
        in_t = in_t.reshape(1, 1, isize, isize, isize)
        in_t = in_t.contiguous(memory_format=memory_format)
        out_t = F.interpolate(
            in_t, size=(osize, osize, osize), recompute_scale_factor=False, mode="nearest-exact"
        )
        # compute expected output as Scikit-Image/Scipy
        expected_out = torch.zeros(1, 1, osize, osize, osize, dtype=torch.float)
        scale = 1.0 * isize / osize
        for o1 in range(osize):
            i1_f32 = (o1 + 0.5) * scale
            i1 = int(i1_f32)
            for o2 in range(osize):
                i2_f32 = (o2 + 0.5) * scale
                i2 = int(i2_f32)
                for o3 in range(osize):
                    i3_f32 = (o3 + 0.5) * scale
                    i3 = int(i3_f32)
                    expected_out[0, 0, o1, o2, o3] = in_t[0, 0, i1, i2, i3]
        expected_out = expected_out.to(device=device)
        self.assertEqual(out_t, expected_out)
//  [python]
def test_upsamplingBiMode2d_nonsupported_dtypes(self, device, antialias, num_channels, mode, dtype):
        x = torch.ones(1, num_channels, 32, 32, dtype=dtype, device=device)

        should_raise_runtime_error = True

        if "nearest" in mode:
            if antialias:
                raise SkipTest("Nearest mode does not have antialiasing")
            if dtype in (torch.uint8, ) + floating_types():
                should_raise_runtime_error = False

        elif mode in ("bilinear", "bicubic"):
            if dtype in floating_types() or (device == "cpu" and dtype == torch.uint8):
                should_raise_runtime_error = False

        if should_raise_runtime_error:
            with self.assertRaisesRegex(RuntimeError, "not implemented for"):
                t.interpolate(x, (12, 12), mode=mode, antialias=antialias)
        else:
            _ = t.interpolate(x, (12, 12), mode=mode, antialias=antialias)
//  [python]
def test_upsamplingBiMode2d_nonsupported_dtypes(self, device, antialias, num_channels, mode, dtype):
        x = torch.ones(1, num_channels, 32, 32, dtype=dtype, device=device)

        should_raise_runtime_error = True

        if "nearest" in mode:
            if antialias:
                raise SkipTest("Nearest mode does not have antialiasing")
            if dtype in (torch.uint8, ) + floating_types():
                should_raise_runtime_error = False

        elif mode in ("bilinear", "bicubic"):
            if dtype in floating_types() or (device == "cpu" and dtype == torch.uint8):
                should_raise_runtime_error = False

        if should_raise_runtime_error:
            with self.assertRaisesRegex(RuntimeError, "not implemented for"):
                F.interpolate(x, (12, 12), mode=mode, antialias=antialias)
        else:
            _ = F.interpolate(x, (12, 12), mode=mode, antialias=antialias)
//  [python]
def test_upsamplingBiLinear2d_consistency_interp_size_bug(self, device, memory_format, align_corners, input_size, output_size):
        # Non-regression test for https://github.com/pytorch/pytorch/pull/101403

        if torch.device(device).type == "cuda":
            raise SkipTest("CUDA implementation is not yet supporting uint8")

        mode = "bilinear"
        input_ui8 = torch.randint(0, 256, size=(1, 3, input_size, input_size), dtype=torch.uint8, device=device)
        input_ui8 = input_ui8.contiguous(memory_format=memory_format)
        input_f32 = input_ui8.float()

        output_f32 = t.interpolate(
            input_f32, size=(output_size, output_size), mode=mode, align_corners=align_corners, antialias=False
        ).round().to(torch.uint8)
        output_ui8 = t.interpolate(
            input_ui8, size=(output_size, output_size), mode=mode, align_corners=align_corners, antialias=False
        )
        torch.testing.assert_close(output_f32, output_ui8, atol=1, rtol=0)
//  [python]
def test_upsamplingBiLinear2d_consistency_interp_size_bug(self, device, memory_format, align_corners, input_size, output_size):
        # Non-regression test for https://github.com/pytorch/pytorch/pull/101403

        if torch.device(device).type == "cuda":
            raise SkipTest("CUDA implementation is not yet supporting uint8")

        mode = "bilinear"
        input_ui8 = torch.randint(0, 256, size=(1, 3, input_size, input_size), dtype=torch.uint8, device=device)
        input_ui8 = input_ui8.contiguous(memory_format=memory_format)
        input_f32 = input_ui8.float()

        output_f32 = F.interpolate(
            input_f32, size=(output_size, output_size), mode=mode, align_corners=align_corners, antialias=False
        ).round().to(torch.uint8)
        output_ui8 = F.interpolate(
            input_ui8, size=(output_size, output_size), mode=mode, align_corners=align_corners, antialias=False
        )
        torch.testing.assert_close(output_f32, output_ui8, atol=1, rtol=0)
//  [python]
def test_upsampling_64bit_indexing_channels_last(self, device, dtype):
        x = torch.rand((32, 64, 512, 512), dtype=dtype, device=device)
        out = torch.nn.functional.interpolate(x.to(memory_format=torch.channels_last), scale_factor=2, mode='nearest')
        out_ref = torch.nn.functional.interpolate(x, scale_factor=2, mode='nearest')
        del x
        self.assertTrue(torch.allclose(out, out_ref))
//  [python]
def test_upsamplingnearest2d_backward_64bit_indexing(self, device, dtype):
        x = torch.randn(size=(36, 128, 512, 512), device=device, dtype=dtype).requires_grad_()
        y = torch.nn.functional.upsample_nearest(x, scale_factor=2)
        y.backward(torch.randn_like(y))
//  [python]
def test_upsamplingnearest2d_backward_64bit_indexing(self, device, dtype):
        x = torch.randn(size=(36, 128, 512, 512), device=device, dtype=dtype).requires_grad_()
        y = F.interpolate(x, scale_factor=2, mode="nearest")
        y.backward(torch.randn_like(y))
//  [python]
def test_basic_upsample_nearest_size(self):
        class SimpleOp(nn.Module):
            def forward(self, x):
                out = torch.nn.functional.upsample(x, size=16, mode='nearest')
                return out
        x = torch.randn(1, 2, 3, 4, requires_grad=True)
        self.checkExportImport(SimpleOp(), (x, ))
//  [python]
def test_basic_upsample_nearest_size(self):
        class SimpleOp(nn.Module):
            def forward(self, x):
                out = torch.nn.functional.interpolate(x, size=16, mode='nearest')
                return out
        x = torch.randn(1, 2, 3, 4, requires_grad=True)
        self.checkExportImport(SimpleOp(), (x, ))
//  [python]
def test_upsample_nearest_scale(self):
        class SimpleOp(nn.Module):
            def forward(self, x):
                out = torch.nn.functional.upsample_nearest(x, scale_factor=2.)
                return out

        x = torch.randn(1, 2, 3, 4, requires_grad=True)
        self.checkExportImport(SimpleOp(), (x, ))
//  [python]
def test_upsample_nearest_scale(self):
        class SimpleOp(nn.Module):
            def forward(self, x):
                out = torch.nn.functional.interpolate(x, scale_factor=2.,
                        mode='nearest', recompute_scale_factor=False)
                return out

        x = torch.randn(1, 2, 3, 4, requires_grad=True)
        self.checkExportImport(SimpleOp(), (x, ))
//  [python]
def _get_random_crop(self, clip):
        _, _, clip_height, clip_width = clip.shape

        short_side = min(clip_height, clip_width)
        long_side = max(clip_height, clip_width)
        new_short_side = int(random.uniform(*self.short_side_range))
        new_long_side = int(long_side / short_side * new_short_side)
        if clip_height < clip_width:
            new_clip_height = new_short_side
            new_clip_width = new_long_side
        else:
            new_clip_height = new_long_side
            new_clip_width = new_short_side

        new_clip = t.nn.functional.interpolate(
            clip, size=(new_clip_height, new_clip_width), mode='bilinear')

        x_max = int(new_clip_width - self.crop_size)
        y_max = int(new_clip_height - self.crop_size)
        x = int(random.uniform(0, x_max))
        y = int(random.uniform(0, y_max))
        return new_clip[:, :, y:y + self.crop_size, x:x + self.crop_size]
//  [python]
def _get_random_crop(self, clip):
        _, _, clip_height, clip_width = clip.shape

        short_side = min(clip_height, clip_width)
        long_side = max(clip_height, clip_width)
        new_short_side = int(random.uniform(*self.short_side_range))
        new_long_side = int(long_side / short_side * new_short_side)
        if clip_height < clip_width:
            new_clip_height = new_short_side
            new_clip_width = new_long_side
        else:
            new_clip_height = new_long_side
            new_clip_width = new_short_side

        new_clip = torch.nn.functional.interpolate(
            clip, size=(new_clip_height, new_clip_width), mode='bilinear')

        x_max = int(new_clip_width - self.crop_size)
        y_max = int(new_clip_height - self.crop_size)
        x = int(random.uniform(0, x_max))
        y = int(random.uniform(0, y_max))
        return new_clip[:, :, y:y + self.crop_size, x:x + self.crop_size]
//  [python]
def test_upsamplingBiMode2d(self, device, antialias, align_corners, mode, memory_format):
        # Forward AD does not support XLA because XLA tensors don't have storage
        check_forward_ad = torch.device(device).type != 'xla'

        kwargs = dict(mode=mode, align_corners=align_corners, antialias=antialias)
        # test float scale factor up & downsampling
        for scale_factor in [0.5, 1.5, 2]:
            in_t = torch.ones(
                2, 3, 8, 8, device=device,
                dtype=torch.double).contiguous(memory_format=memory_format).requires_grad_()
            out_size = int(math.floor(in_t.shape[-1] * scale_factor))
            with warnings.catch_warnings(record=True) as w:
                out_t = t.interpolate(in_t, scale_factor=scale_factor, **kwargs)
            expected_out = torch.ones(2, 3, out_size, out_size, device=device, dtype=torch.double)
            self.assertEqual(expected_out, out_t)
            # Assert that memory format is carried through to the output
            self.assertTrue(out_t.is_contiguous(memory_format=memory_format))
            out_t.backward(torch.randn_like(out_t))
            self.assertTrue(in_t.grad.is_contiguous(memory_format=memory_format))

            if torch.device(device).type == 'cuda':
                # Bilinear backward is nondeterministic because of atomicAdd usage
                nondet_tol = 1e-5
            else:
                nondet_tol = 0.0

            input = torch.randn(
                2, 3, 8, 8, device=device,
                dtype=torch.double).contiguous(memory_format=memory_format).requires_grad_()
            gradcheck(
                lambda x: t.interpolate(x, out_size, **kwargs),
                [input],
                check_forward_ad=check_forward_ad, nondet_tol=nondet_tol
            )
            gradgradcheck(
                lambda x: t.interpolate(x, out_size, **kwargs),
                [input],
                check_fwd_over_rev=check_forward_ad, nondet_tol=nondet_tol
            )

            # Assert that cpu and cuda give same results
            if torch.device(device).type == 'cuda':
                for shapes in [
                    (2, 2, 3, 4), (2, 3, 4, 5), (3, 1, 2, 2), (1, 5, 3, 2)
                ]:
                    a_cuda = torch.randn(
                        *shapes, device=device, dtype=torch.double
                    ).contiguous(memory_format=memory_format).requires_grad_()
                    a_cpu = a_cuda.detach().cpu().requires_grad_()

                    with warnings.catch_warnings(record=True):
                        out_cuda = t.interpolate(a_cuda, scale_factor=scale_factor, **kwargs)
                        out_cpu = t.interpolate(a_cpu, scale_factor=scale_factor, **kwargs)

                    self.assertEqual(out_cpu, out_cuda.cpu())

                    g_cuda = torch.randn_like(out_cuda)
                    g_cpu = g_cuda.cpu()

                    out_cuda.backward(g_cuda)
                    out_cpu.backward(g_cpu)

                    self.assertEqual(a_cuda.grad, a_cpu.grad)
//  [python]
def test_upsamplingBiMode2d(self, device, antialias, align_corners, mode, memory_format):
        # Forward AD does not support XLA because XLA tensors don't have storage
        check_forward_ad = torch.device(device).type != 'xla'

        kwargs = dict(mode=mode, align_corners=align_corners, antialias=antialias)
        # test float scale factor up & downsampling
        for scale_factor in [0.5, 1.5, 2]:
            in_t = torch.ones(
                2, 3, 8, 8, device=device,
                dtype=torch.double).contiguous(memory_format=memory_format).requires_grad_()
            out_size = int(math.floor(in_t.shape[-1] * scale_factor))
            with warnings.catch_warnings(record=True) as w:
                out_t = F.interpolate(in_t, scale_factor=scale_factor, **kwargs)
            expected_out = torch.ones(2, 3, out_size, out_size, device=device, dtype=torch.double)
            self.assertEqual(expected_out, out_t)
            # Assert that memory format is carried through to the output
            self.assertTrue(out_t.is_contiguous(memory_format=memory_format))
            out_t.backward(torch.randn_like(out_t))
            self.assertTrue(in_t.grad.is_contiguous(memory_format=memory_format))

            if torch.device(device).type == 'cuda':
                # Bilinear backward is nondeterministic because of atomicAdd usage
                nondet_tol = 1e-5
            else:
                nondet_tol = 0.0

            input = torch.randn(
                2, 3, 8, 8, device=device,
                dtype=torch.double).contiguous(memory_format=memory_format).requires_grad_()
            gradcheck(
                lambda x: F.interpolate(x, out_size, **kwargs),
                [input],
                check_forward_ad=check_forward_ad, nondet_tol=nondet_tol
            )
            gradgradcheck(
                lambda x: F.interpolate(x, out_size, **kwargs),
                [input],
                check_fwd_over_rev=check_forward_ad, nondet_tol=nondet_tol
            )

            # Assert that cpu and cuda give same results
            if torch.device(device).type == 'cuda':
                for shapes in [
                    (2, 2, 3, 4), (2, 3, 4, 5), (3, 1, 2, 2), (1, 5, 3, 2)
                ]:
                    a_cuda = torch.randn(
                        *shapes, device=device, dtype=torch.double
                    ).contiguous(memory_format=memory_format).requires_grad_()
                    a_cpu = a_cuda.detach().cpu().requires_grad_()

                    with warnings.catch_warnings(record=True):
                        out_cuda = F.interpolate(a_cuda, scale_factor=scale_factor, **kwargs)
                        out_cpu = F.interpolate(a_cpu, scale_factor=scale_factor, **kwargs)

                    self.assertEqual(out_cpu, out_cuda.cpu())

                    g_cuda = torch.randn_like(out_cuda)
                    g_cpu = g_cuda.cpu()

                    out_cuda.backward(g_cuda)
                    out_cpu.backward(g_cpu)

                    self.assertEqual(a_cuda.grad, a_cpu.grad)
//  [python]
def forward(self, x, timesteps=None, context=None, y=None, **kwargs):
        """
        Apply the model to an input batch.
        :param x: an [N x C x ...] Tensor of inputs.
        :param timesteps: a 1-D batch of timesteps.
        :param context: conditioning plugged in via crossattn
        :param y: an [N] Tensor of labels, if class-conditional.
        :return: an [N x C x ...] Tensor of outputs.
        """
        assert (y is not None) == (
            self.num_classes is not None
        ), "must specify y if and only if the model is class-conditional"
        hs = []
        t_emb = timestep_embedding(timesteps, self.model_channels, repeat_only=False)
        emb = self.time_embed(t_emb)

        if self.num_classes is not None:
            assert y.shape[0] == x.shape[0]
            emb = emb + self.label_emb(y)

        h = x.type(self.dtype)
        for module in self.input_blocks:
            h = module(h, emb, context)
            hs.append(h)
        h = self.middle_block(h, emb, context)
        for module in self.output_blocks:
            # allows us to work with multiples of 8 instead of 64 for image sizes
            # https://github.com/CompVis/stable-diffusion/issues/60#issuecomment-1240294667
            if h.shape[-2:] != hs[-1].shape[-2:]:
                h = F.interpolate(h, hs[-1].shape[-2:], mode="nearest")
            h = th.cat([h, hs.pop()], dim=1)
            h = module(h, emb, context)
        h = h.type(x.dtype)
        if self.predict_codebook_ids:
            return self.id_predictor(h)
        return self.out(h)
//  [python]
def test_upsample_nearest_scale_default_scale_factor(self):
        class SimpleOp(nn.Module):
            def forward(self, x):
                out = torch.nn.functional.upsample_nearest(x, scale_factor=2.)
                return out

        x = torch.randn(1, 2, 3, 4, requires_grad=True)
        self.checkExportImport(SimpleOp(), (x, ))
//  [python]
def test_upsample_nearest_scale_default_scale_factor(self):
        class SimpleOp(nn.Module):
            def forward(self, x):
                out = torch.nn.functional.interpolate(x, scale_factor=2.,
                        mode='nearest')
                return out

        x = torch.randn(1, 2, 3, 4, requires_grad=True)
        self.checkExportImport(SimpleOp(), (x, ))
//  [python]
def _get_controlled_crop(self, clip):
        """Perform controlled crop for video tensor.
        Args:
            clip (Tensor): the video data, the shape is [T, C, H, W]
        """
        _, _, clip_height, clip_width = clip.shape

        length = self.short_side_range[0]

        if clip_height < clip_width:
            new_clip_height = int(length)
            new_clip_width = int(clip_width / clip_height * new_clip_height)
            new_clip = t.nn.functional.interpolate(
                clip, size=(new_clip_height, new_clip_width), mode='bilinear')
        else:
            new_clip_width = int(length)
            new_clip_height = int(clip_height / clip_width * new_clip_width)
            new_clip = t.nn.functional.interpolate(
                clip, size=(new_clip_height, new_clip_width), mode='bilinear')
        x_max = int(new_clip_width - self.crop_size)
        y_max = int(new_clip_height - self.crop_size)
        if self.num_spatial_crops == 1:
            x = x_max // 2
            y = y_max // 2
        elif self.num_spatial_crops == 3:
            if self.idx == 0:
                if new_clip_width == length:
                    x = x_max // 2
                    y = 0
                elif new_clip_height == length:
                    x = 0
                    y = y_max // 2
            elif self.idx == 1:
                x = x_max // 2
                y = y_max // 2
            elif self.idx == 2:
                if new_clip_width == length:
                    x = x_max // 2
                    y = y_max
                elif new_clip_height == length:
                    x = x_max
                    y = y_max // 2
        return new_clip[:, :, y:y + self.crop_size, x:x + self.crop_size]
//  [python]
def _get_controlled_crop(self, clip):
        """Perform controlled crop for video tensor.
        Args:
            clip (Tensor): the video data, the shape is [T, C, H, W]
        """
        _, _, clip_height, clip_width = clip.shape

        length = self.short_side_range[0]

        if clip_height < clip_width:
            new_clip_height = int(length)
            new_clip_width = int(clip_width / clip_height * new_clip_height)
            new_clip = torch.nn.functional.interpolate(
                clip, size=(new_clip_height, new_clip_width), mode='bilinear')
        else:
            new_clip_width = int(length)
            new_clip_height = int(clip_height / clip_width * new_clip_width)
            new_clip = torch.nn.functional.interpolate(
                clip, size=(new_clip_height, new_clip_width), mode='bilinear')
        x_max = int(new_clip_width - self.crop_size)
        y_max = int(new_clip_height - self.crop_size)
        if self.num_spatial_crops == 1:
            x = x_max // 2
            y = y_max // 2
        elif self.num_spatial_crops == 3:
            if self.idx == 0:
                if new_clip_width == length:
                    x = x_max // 2
                    y = 0
                elif new_clip_height == length:
                    x = 0
                    y = y_max // 2
            elif self.idx == 1:
                x = x_max // 2
                y = y_max // 2
            elif self.idx == 2:
                if new_clip_width == length:
                    x = x_max // 2
                    y = y_max
                elif new_clip_height == length:
                    x = x_max
                    y = y_max // 2
        return new_clip[:, :, y:y + self.crop_size, x:x + self.crop_size]
//  [python]
def forward(self, x):
        if self.scale_factor <= 1:
            return x
        return F.upsample(x, scale_factor=self.scale_factor, mode='nearest')
//  [python]
def forward(self, x):
        if self.scale_factor <= 1:
            return x
        return F.interpolate(x, scale_factor=self.scale_factor, mode='nearest')
//  [python]
def forward(self, x: torch.Tensor, step: tp.Union[int, torch.Tensor], condition: tp.Optional[torch.Tensor] = None):
        skips = []
        bs = x.size(0)
        z = x
        view_args = [1]
        if type(step) is torch.Tensor:
            step_tensor = step
        else:
            step_tensor = torch.tensor([step], device=x.device, dtype=torch.long).expand(bs)

        for idx, encoder in enumerate(self.encoders):
            z = encoder(z)
            if idx == 0:
                z = z + self.embedding(step_tensor).view(bs, -1, *view_args).expand_as(z)
            elif self.embeddings is not None:
                z = z + self.embeddings[idx - 1](step_tensor).view(bs, -1, *view_args).expand_as(z)

            skips.append(z)

        if self.use_codec:  # insert condition in the bottleneck
            assert condition is not None, "Model defined for conditionnal generation"
            condition_emb = self.conv_codec(condition)  # reshape to the bottleneck dim
            assert condition_emb.size(-1) <= 2 * z.size(-1), \
                f"You are downsampling the conditionning with factor >=2 : {condition_emb.size(-1)=} and {z.size(-1)=}"
            if not self.cross_attention:

                condition_emb = torch.nn.functional.upsample(condition_emb, size=z.size(-1))
                assert z.size() == condition_emb.size()
                z += condition_emb
                cross_attention_src = None
            else:
                cross_attention_src = condition_emb.permute(0, 2, 1)  # B, T, C
                B, T, C = cross_attention_src.shape
                positions = torch.arange(T, device=x.device).view(1, -1, 1)
                pos_emb = create_sin_embedding(positions, C, max_period=10_000, dtype=cross_attention_src.dtype)
                cross_attention_src = cross_attention_src + pos_emb
        if self.use_transformer:
            z = self.transformer(z.permute(0, 2, 1), cross_attention_src=cross_attention_src).permute(0, 2, 1)
        else:
            if self.bilstm is None:
                z = torch.zeros_like(z)
            else:
                z = self.bilstm(z)

        for decoder in self.decoders:
            s = skips.pop(-1)
            z = z[:, :, :s.shape[2]]
            z = z + s
            z = decoder(z)

        z = z[:, :, :x.shape[2]]
        return Output(z)
//  [python]
def forward(self, x: torch.Tensor, step: tp.Union[int, torch.Tensor], condition: tp.Optional[torch.Tensor] = None):
        skips = []
        bs = x.size(0)
        z = x
        view_args = [1]
        if type(step) is torch.Tensor:
            step_tensor = step
        else:
            step_tensor = torch.tensor([step], device=x.device, dtype=torch.long).expand(bs)

        for idx, encoder in enumerate(self.encoders):
            z = encoder(z)
            if idx == 0:
                z = z + self.embedding(step_tensor).view(bs, -1, *view_args).expand_as(z)
            elif self.embeddings is not None:
                z = z + self.embeddings[idx - 1](step_tensor).view(bs, -1, *view_args).expand_as(z)

            skips.append(z)

        if self.use_codec:  # insert condition in the bottleneck
            assert condition is not None, "Model defined for conditionnal generation"
            condition_emb = self.conv_codec(condition)  # reshape to the bottleneck dim
            assert condition_emb.size(-1) <= 2 * z.size(-1), \
                f"You are downsampling the conditionning with factor >=2 : {condition_emb.size(-1)=} and {z.size(-1)=}"
            if not self.cross_attention:

                condition_emb = torch.nn.functional.interpolate(condition_emb, z.size(-1))
                assert z.size() == condition_emb.size()
                z += condition_emb
                cross_attention_src = None
            else:
                cross_attention_src = condition_emb.permute(0, 2, 1)  # B, T, C
                B, T, C = cross_attention_src.shape
                positions = torch.arange(T, device=x.device).view(1, -1, 1)
                pos_emb = create_sin_embedding(positions, C, max_period=10_000, dtype=cross_attention_src.dtype)
                cross_attention_src = cross_attention_src + pos_emb
        if self.use_transformer:
            z = self.transformer(z.permute(0, 2, 1), cross_attention_src=cross_attention_src).permute(0, 2, 1)
        else:
            if self.bilstm is None:
                z = torch.zeros_like(z)
            else:
                z = self.bilstm(z)

        for decoder in self.decoders:
            s = skips.pop(-1)
            z = z[:, :, :s.shape[2]]
            z = z + s
            z = decoder(z)

        z = z[:, :, :x.shape[2]]
        return Output(z)