





import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append(""".""")


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       _UpperCamelCase              =						test_file.split(os.path.sep					)
       if components[0:2] != ["tests", "models"]:
              raise ValueError(
                  '''`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got '''
                  f'''{test_file} instead.'''					)
       _UpperCamelCase              =						components[-1]
       if not test_fn.endswith('''py'''					):
              raise ValueError(f'''`test_file` should be a python file. Got {test_fn} instead.'''					)
       if not test_fn.startswith('''test_modeling_'''					):
              raise ValueError(
                  f'''`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead.'''					)

       _UpperCamelCase              =						components[:-1] + [test_fn.replace('''.py'''    ,       ''''''					)]
       _UpperCamelCase              =						'''.'''.join(lowerCAmelCase					)

       return test_module_path


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       _UpperCamelCase              =						get_module_path(lowerCAmelCase					)
       _UpperCamelCase              =						importlib.import_module(lowerCAmelCase					)

       return test_module


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       _UpperCamelCase              =						[]
       _UpperCamelCase              =						get_test_module(lowerCAmelCase					)
       for attr in dir(lowerCAmelCase					):
              if attr.endswith('''ModelTester'''					):
                     tester_classes.append(getattr(lowerCAmelCase    ,       lowerCAmelCase					)					)

    # sort with class names
       return sorted(lowerCAmelCase    ,       key=lambda lowerCAmelCase					: x.__name__					)


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       _UpperCamelCase              =						[]
       _UpperCamelCase              =						get_test_module(lowerCAmelCase					)
       for attr in dir(lowerCAmelCase					):
              _UpperCamelCase              =						getattr(lowerCAmelCase    ,       lowerCAmelCase					)
              # (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
              # `all_model_classes` is not empty (which also excludes other special classes).
              _UpperCamelCase              =						getattr(lowerCAmelCase    ,       '''all_model_classes'''    ,       []					)
              if len(lowerCAmelCase					) > 0:
                     test_classes.append(lowerCAmelCase					)

    # sort with class names
       return sorted(lowerCAmelCase    ,       key=lambda lowerCAmelCase					: x.__name__					)


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       _UpperCamelCase              =						get_test_classes(lowerCAmelCase					)
       _UpperCamelCase              =						set()
       for test_class in test_classes:
              model_classes.update(test_class.all_model_classes					)

       # sort with class names
       return sorted(lowerCAmelCase    ,       key=lambda lowerCAmelCase					: x.__name__					)


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       _UpperCamelCase              =						test_class()
       if hasattr(lowerCAmelCase    ,       '''setUp'''					):
              test.setUp()

       _UpperCamelCase              =						None
       if hasattr(lowerCAmelCase    ,       '''model_tester'''					):
              # `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
              if test.model_tester is not None:
                     _UpperCamelCase              =						test.model_tester.__class__

       return model_tester


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase    ,       lowerCAmelCase					):
       _UpperCamelCase              =						get_test_classes(lowerCAmelCase					)

       _UpperCamelCase              =						[]
       for test_class in test_classes:
              if model_class in test_class.all_model_classes:
                     target_test_classes.append(lowerCAmelCase					)

    # sort with class names
       return sorted(lowerCAmelCase    ,       key=lambda lowerCAmelCase					: x.__name__					)


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase    ,       lowerCAmelCase					):
       _UpperCamelCase              =						get_test_classes_for_model(lowerCAmelCase    ,       lowerCAmelCase					)

       _UpperCamelCase              =						[]
       for test_class in test_classes:
              _UpperCamelCase              =						get_model_tester_from_test_class(lowerCAmelCase					)
              if tester_class is not None:
                     tester_classes.append(lowerCAmelCase					)

    # sort with class names
       return sorted(lowerCAmelCase    ,       key=lambda lowerCAmelCase					: x.__name__					)


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       _UpperCamelCase              =						get_test_classes(lowerCAmelCase					)
       _UpperCamelCase              =						{test_class: get_model_tester_from_test_class(lowerCAmelCase					) for test_class in test_classes}
       return test_tester_mapping


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       _UpperCamelCase              =						get_model_classes(lowerCAmelCase					)
       _UpperCamelCase              =						{
           model_class: get_test_classes_for_model(lowerCAmelCase    ,       lowerCAmelCase					) for model_class in model_classes
       }
       return model_test_mapping


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       _UpperCamelCase              =						get_model_classes(lowerCAmelCase					)
       _UpperCamelCase              =						{
           model_class: get_tester_classes_for_model(lowerCAmelCase    ,       lowerCAmelCase					) for model_class in model_classes
       }
       return model_to_tester_mapping


def     SCREAMING_SNAKE_CASE			(      lowerCAmelCase					):
       if isinstance(lowerCAmelCase    ,       lowerCAmelCase					):
              return o
       elif isinstance(lowerCAmelCase    ,       lowerCAmelCase					):
              return o.__name__
       elif isinstance(lowerCAmelCase    ,       (list, tuple)					):
              return [to_json(lowerCAmelCase					) for x in o]
       elif isinstance(lowerCAmelCase    ,       lowerCAmelCase					):
              return {to_json(lowerCAmelCase					): to_json(lowerCAmelCase					) for k, v in o.items()}
       else:
              return o













import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
       import jax
       import jax.numpy as jnp
       from flax.jax_utils import replicate
       from flax.training.common_utils import shard

@slow
@require_flax
class  __A(						unittest.TestCase ):

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            super().tearDown()
            gc.collect()

     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase       ,		_UpperCamelCase              =						FlaxStableDiffusionPipeline.from_pretrained(
                '''stabilityai/stable-diffusion-2''',       revision='''bf16''',       dtype=jnp.bfloataa,       )

            _UpperCamelCase              =						'''A painting of a squirrel eating a burger'''
            _UpperCamelCase              =						jax.device_count()
            _UpperCamelCase              =						num_samples * [prompt]
            _UpperCamelCase              =						sd_pipe.prepare_inputs(A      )

            _UpperCamelCase              =						replicate(A      )
            _UpperCamelCase              =						shard(A      )

            _UpperCamelCase              =						jax.random.PRNGKey(0      )
            _UpperCamelCase              =						jax.random.split(A,       jax.device_count()      )

            _UpperCamelCase              =						sd_pipe(A,       A,       A,       num_inference_steps=25,       jit=A      )[0]
            assert images.shape == (jax.device_count(), 1, 768, 768, 3)

            _UpperCamelCase              =						images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]      )
            _UpperCamelCase              =						images[0, 253:256, 253:256, -1]

            _UpperCamelCase              =						jnp.asarray(jax.device_get(image_slice.flatten()      )      )
            _UpperCamelCase              =						jnp.array([0.4_238, 0.4_414, 0.4_395, 0.4_453, 0.4_629, 0.4_590, 0.4_531, 0.45_508, 0.4_512]      )
            print(F'''output_slice: {output_slice}'''      )
            assert jnp.abs(output_slice - expected_slice      ).max() < 1E-2



     def 	_UpperCamelCase     (      self      ):





            """simple docstring"""

            _UpperCamelCase              =						'''stabilityai/stable-diffusion-2'''
            _UpperCamelCase       ,		_UpperCamelCase              =						FlaxDPMSolverMultistepScheduler.from_pretrained(A,       subfolder='''scheduler'''      )
            _UpperCamelCase       ,		_UpperCamelCase              =						FlaxStableDiffusionPipeline.from_pretrained(
                A,       scheduler=A,       revision='''bf16''',       dtype=jnp.bfloataa,       )
            _UpperCamelCase              =						scheduler_params

            _UpperCamelCase              =						'''A painting of a squirrel eating a burger'''
            _UpperCamelCase              =						jax.device_count()
            _UpperCamelCase              =						num_samples * [prompt]
            _UpperCamelCase              =						sd_pipe.prepare_inputs(A      )

            _UpperCamelCase              =						replicate(A      )
            _UpperCamelCase              =						shard(A      )

            _UpperCamelCase              =						jax.random.PRNGKey(0      )
            _UpperCamelCase              =						jax.random.split(A,       jax.device_count()      )

            _UpperCamelCase              =						sd_pipe(A,       A,       A,       num_inference_steps=25,       jit=A      )[0]
            assert images.shape == (jax.device_count(), 1, 768, 768, 3)

            _UpperCamelCase              =						images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]      )
            _UpperCamelCase              =						images[0, 253:256, 253:256, -1]

            _UpperCamelCase              =						jnp.asarray(jax.device_get(image_slice.flatten()      )      )
            _UpperCamelCase              =						jnp.array([0.4_336, 0.42_969, 0.4_453, 0.4_199, 0.4_297, 0.4_531, 0.4_434, 0.4_434, 0.4_297]      )
            print(F'''output_slice: {output_slice}'''      )
            assert jnp.abs(output_slice - expected_slice      ).max() < 1E-2






