



"""simple docstring"""





import unittest

import numpy as np
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()



class    _lowerCAmelCase    (				SCREAMING_SNAKE_CASE__  ,				unittest.TestCase		):





		"""simple docstring"""






		_lowerCamelCase			  =       DDIMPipeline
		_lowerCamelCase			  =       UNCONDITIONAL_IMAGE_GENERATION_PARAMS
		_lowerCamelCase			  =       PipelineTesterMixin.required_optional_params - {
		    '''num_images_per_prompt''',
		    '''latents''',
		    '''callback''',
		    '''callback_steps''',
		}
		_lowerCamelCase			  =       UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS
		_lowerCamelCase			  =       False





		def    UpperCAmelCase__							(					self   ) ->						Any:







								'''simple docstring'''

								torch.manual_seed(0   )
								snake_case_   : List[str]						=			UNetaDModel(
								    block_out_channels=(3_2, 6_4)							,    layers_per_block=2							,    sample_size=3_2							,    in_channels=3							,    out_channels=3							,    down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")							,    up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")							,    )
								snake_case_   : Optional[Any]						=			DDIMScheduler()
								snake_case_   : Any						=			{"""unet""": unet, """scheduler""": scheduler}
								return components





		def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase=0   ) ->						Optional[Any]:







								'''simple docstring'''

								if str(_lowercase   ).startswith("""mps"""   ):
														snake_case_   : int						=			torch.manual_seed(_lowercase   )
								else:
														snake_case_   : Dict						=			torch.Generator(device=_lowercase   ).manual_seed(_lowercase   )
								snake_case_   : Optional[int]						=			{
								    """batch_size""": 1,
								    """generator""": generator,
								    """num_inference_steps""": 2,
								    """output_type""": """numpy""",
								}
								return inputs





		def    UpperCAmelCase__							(					self   ) ->						Optional[Any]:







								'''simple docstring'''

								snake_case_   : Tuple						=			"""cpu"""

								snake_case_   : int						=			self.get_dummy_components()
								snake_case_   : Tuple						=			self.pipeline_class(**_lowercase   )
								pipe.to(_lowercase   )
								pipe.set_progress_bar_config(disable=_lowercase   )

								snake_case_   : Any						=			self.get_dummy_inputs(_lowercase   )
								snake_case_   : Dict						=			pipe(**_lowercase   ).images
								snake_case_   : Optional[int]						=			image[0, -3:, -3:, -1]

								self.assertEqual(image.shape							,    (1, 3_2, 3_2, 3)   )
								snake_case_   : Optional[int]						=			np.array(
								    [1.000E00, 5.717E-01, 4.717E-01, 1.000E00, 0.000E00, 1.000E00, 3.000E-04, 0.000E00, 9.000E-04]   )
								snake_case_   : Union[str, Any]						=			np.abs(image_slice.flatten() - expected_slice   ).max()
								self.assertLessEqual(_lowercase							,    1E-3   )





		def    UpperCAmelCase__							(					self   ) ->						Dict:







								'''simple docstring'''

								super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3   )





		def    UpperCAmelCase__							(					self   ) ->						List[str]:







								'''simple docstring'''

								super().test_save_load_local(expected_max_difference=3E-3   )





		def    UpperCAmelCase__							(					self   ) ->						Optional[Any]:







								'''simple docstring'''

								super().test_save_load_optional_components(expected_max_difference=3E-3   )





		def    UpperCAmelCase__							(					self   ) ->						Any:







								'''simple docstring'''

								super().test_inference_batch_single_identical(expected_max_diff=3E-3   )




@slow
@require_torch_gpu
class    _lowerCAmelCase    (				unittest.TestCase		):





		"""simple docstring"""






		def    UpperCAmelCase__							(					self   ) ->						Union[str, Any]:







								'''simple docstring'''

								snake_case_   : str						=			"""google/ddpm-cifar10-32"""

								snake_case_   : Optional[Any]						=			UNetaDModel.from_pretrained(_lowercase   )
								snake_case_   : int						=			DDIMScheduler()

								snake_case_   : Union[str, Any]						=			DDIMPipeline(unet=_lowercase							,    scheduler=_lowercase   )
								ddim.to(_lowercase   )
								ddim.set_progress_bar_config(disable=_lowercase   )

								snake_case_   : List[Any]						=			torch.manual_seed(0   )
								snake_case_   : Union[str, Any]						=			ddim(generator=_lowercase							,    eta=0.0							,    output_type="""numpy"""   ).images

								snake_case_   : List[Any]						=			image[0, -3:, -3:, -1]

								assert image.shape == (1, 3_2, 3_2, 3)
								snake_case_   : Tuple						=			np.array([0.1723, 0.1617, 0.1600, 0.1626, 0.1497, 0.1513, 0.1505, 0.1442, 0.1453]   )

								assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2





		def    UpperCAmelCase__							(					self   ) ->						Dict:







								'''simple docstring'''

								snake_case_   : Dict						=			"""google/ddpm-ema-bedroom-256"""

								snake_case_   : Tuple						=			UNetaDModel.from_pretrained(_lowercase   )
								snake_case_   : List[Any]						=			DDIMScheduler.from_pretrained(_lowercase   )

								snake_case_   : List[str]						=			DDIMPipeline(unet=_lowercase							,    scheduler=_lowercase   )
								ddpm.to(_lowercase   )
								ddpm.set_progress_bar_config(disable=_lowercase   )

								snake_case_   : Union[str, Any]						=			torch.manual_seed(0   )
								snake_case_   : Any						=			ddpm(generator=_lowercase							,    output_type="""numpy"""   ).images

								snake_case_   : Optional[int]						=			image[0, -3:, -3:, -1]

								assert image.shape == (1, 2_5_6, 2_5_6, 3)
								snake_case_   : Optional[Any]						=			np.array([0.0060, 0.0201, 0.0344, 0.0024, 0.0018, 0.0002, 0.0022, 0.0000, 0.0069]   )

								assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2






"""simple docstring"""





from .data_collator import (
    DataCollatorForLanguageModeling,
    DataCollatorForPermutationLanguageModeling,
    DataCollatorForSeqaSeq,
    DataCollatorForSOP,
    DataCollatorForTokenClassification,
    DataCollatorForWholeWordMask,
    DataCollatorWithPadding,
    DefaultDataCollator,
    default_data_collator,
)
from .metrics import glue_compute_metrics, xnli_compute_metrics
from .processors import (
    DataProcessor,
    InputExample,
    InputFeatures,
    SingleSentenceClassificationProcessor,
    SquadExample,
    SquadFeatures,
    SquadVaProcessor,
    SquadVaProcessor,
    glue_convert_examples_to_features,
    glue_output_modes,
    glue_processors,
    glue_tasks_num_labels,
    squad_convert_examples_to_features,
    xnli_output_modes,
    xnli_processors,
    xnli_tasks_num_labels,
)

