

import random
import unittest

import torch

from diffusers import IFSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin
@skip_mps
class    A_				( _a	,       _a	,       unittest.TestCase  ):


    '''simple docstring'''






    a__       							=    IFSuperResolutionPipeline
    a__       							=    TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"width", "height"}
    a__       							=    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
    a__       							=    PipelineTesterMixin.required_optional_params - {"latents"}


    def        lowerCAmelCase_  (self					)       -> Tuple:
        return self._get_superresolution_dummy_components()


    def        lowerCAmelCase_  (self       , lowercase__       , lowercase__=0					)       -> Union[str, Any]:
        if str(lowercase__					).startswith('''mps'''					):
            __UpperCAmelCase			   =     torch.manual_seed(lowercase__					)
        else:
            __UpperCAmelCase			   =     torch.Generator(device=lowercase__					).manual_seed(lowercase__					)

        __UpperCAmelCase			   =     floats_tensor((1, 3, 32, 32)       , rng=random.Random(lowercase__					)					).to(lowercase__					)

        __UpperCAmelCase			   =     {
            '''prompt''': '''A painting of a squirrel eating a burger''',
            '''image''': image,
            '''generator''': generator,
            '''num_inference_steps''': 2,
            '''output_type''': '''numpy''',
        }

        return inputs


    @unittest.skipIf(
        torch_device != '''cuda''' or not is_xformers_available()       , reason='''XFormers attention is only available with CUDA and `xformers` installed'''       , )
    def        lowerCAmelCase_  (self					)       -> Union[str, Any]:
        self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3					)


    def        lowerCAmelCase_  (self					)       -> int:
        self._test_save_load_optional_components()


    @unittest.skipIf(torch_device != '''cuda'''       , reason='''float16 requires CUDA'''					)
    def        lowerCAmelCase_  (self					)       -> Optional[int]:
        # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
        super().test_save_load_floataa(expected_max_diff=1E-1					)


    def        lowerCAmelCase_  (self					)       -> Optional[Any]:
        self._test_attention_slicing_forward_pass(expected_max_diff=1E-2					)


    def        lowerCAmelCase_  (self					)       -> str:
        self._test_save_load_local()






    def        lowerCAmelCase_  (self					)       -> int:
        self._test_inference_batch_single_identical(
            expected_max_diff=1E-2       , )








from __future__ import annotations


def  __a    (				SCREAMING_SNAKE_CASE ) ->       list[int]:






    '''simple docstring'''
    if not list_of_ints:
        return []

    if min(SCREAMING_SNAKE_CASE ) < 0:
        raise ValueError('''All numbers must be positive''' )

    __UpperCAmelCase			   =     max(len(bin(SCREAMING_SNAKE_CASE )[2:] ) for x in list_of_ints )
    return _msd_radix_sort(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE )


def  __a    (				SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE ) ->       list[int]:






    '''simple docstring'''
    if bit_position == 0 or len(SCREAMING_SNAKE_CASE ) in [0, 1]:
        return list_of_ints

    __UpperCAmelCase			   =     []
    __UpperCAmelCase			   =     []
    # Split numbers based on bit at bit_position from the right
    for number in list_of_ints:
        if (number >> (bit_position - 1)) & 1:
            # number has a one at bit bit_position
            ones.append(SCREAMING_SNAKE_CASE )
        else:
            # number has a zero at bit bit_position
            zeros.append(SCREAMING_SNAKE_CASE )

    # recursively split both lists further
    __UpperCAmelCase			   =     _msd_radix_sort(SCREAMING_SNAKE_CASE    ,				bit_position - 1 )
    __UpperCAmelCase			   =     _msd_radix_sort(SCREAMING_SNAKE_CASE    ,				bit_position - 1 )

    # recombine lists
    __UpperCAmelCase			   =     zeros
    res.extend(SCREAMING_SNAKE_CASE )

    return res


def  __a    (				SCREAMING_SNAKE_CASE ) ->       Optional[int]:






    '''simple docstring'''
    __UpperCAmelCase			   =     len(SCREAMING_SNAKE_CASE )
    if not list_of_ints or length == 1:
        return

    if min(SCREAMING_SNAKE_CASE ) < 0:
        raise ValueError('''All numbers must be positive''' )

    __UpperCAmelCase			   =     max(len(bin(SCREAMING_SNAKE_CASE )[2:] ) for x in list_of_ints )
    _msd_radix_sort_inplace(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				0    ,				SCREAMING_SNAKE_CASE )


def  __a    (				SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE ) ->       int:






    '''simple docstring'''
    if bit_position == 0 or end_index - begin_index <= 1:
        return

    bit_position -= 1

    __UpperCAmelCase			   =     begin_index
    __UpperCAmelCase			   =     end_index - 1
    while i <= j:
        __UpperCAmelCase			   =     False
        if not (list_of_ints[i] >> bit_position) & 1:
            # found zero at the beginning
            i += 1
            __UpperCAmelCase			   =     True
        if (list_of_ints[j] >> bit_position) & 1:
            # found one at the end
            j -= 1
            __UpperCAmelCase			   =     True

        if changed:
            continue

        __UpperCAmelCase   ,       __UpperCAmelCase			   =     list_of_ints[j], list_of_ints[i]
        j -= 1
        if j != i:
            i += 1

    _msd_radix_sort_inplace(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE )
    _msd_radix_sort_inplace(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE )


if __name__ == "__main__":
       import doctest

       doctest.testmod()




