




'''simple docstring'''


import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
				from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
				from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
				from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


_snake_case : List[str]  				=						DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

_snake_case : Union[str, Any]  				=						'main'
# Default branch name
_snake_case : Dict  				=						'f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'
# One particular commit (not the top of `main`)
_snake_case : Dict  				=						'aaaaaaa'
# This commit does not exist, so we should 404.

_snake_case : str  				=						'd9e9f15bc825e4b2c9249e9578f884bbcb5e3684'
# Sha-1 of config.json on the top of `main`, for checking purposes
_snake_case : int  				=						'4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'
@contextlib.contextmanager
def 						snake_case_       ():





							'''simple docstring'''


							print('''Welcome!''' )
							yield
							print('''Bye!''' )
@contextlib.contextmanager
def 						snake_case_       ():





							'''simple docstring'''


							print('''Bonjour!''' )
							yield
							print('''Au revoir!''' )

class      A       (						unittest.TestCase       ):







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


														"""simple docstring"""


														assert transformers.__spec__ is not None
														assert importlib.util.find_spec('''transformers'''   ) is not None



class      A       (						unittest.TestCase       ):







							@unittest.mock.patch('''sys.stdout'''				,     new_callable=io.StringIO   )
							def   __lowerCAmelCase     (    self			:    str				,     lowerCAmelCase_			:    str   )     ->				int:


														"""simple docstring"""


														with ContextManagers([]   ):
																					print('''Transformers are awesome!'''   )
														# The print statement adds a new line at the end of the output
														self.assertEqual(mock_stdout.getvalue()				,     '''Transformers are awesome!\n'''   )







							@unittest.mock.patch('''sys.stdout'''				,     new_callable=io.StringIO   )
							def   __lowerCAmelCase     (    self			:    Any				,     lowerCAmelCase_			:    Optional[int]   )     ->				Any:


														"""simple docstring"""


														with ContextManagers([context_en()]   ):
																					print('''Transformers are awesome!'''   )
														# The output should be wrapped with an English welcome and goodbye
														self.assertEqual(mock_stdout.getvalue()				,     '''Welcome!\nTransformers are awesome!\nBye!\n'''   )







							@unittest.mock.patch('''sys.stdout'''				,     new_callable=io.StringIO   )
							def   __lowerCAmelCase     (    self			:    Optional[Any]				,     lowerCAmelCase_			:    List[Any]   )     ->				str:


														"""simple docstring"""


														with ContextManagers([context_fr(), context_en()]   ):
																					print('''Transformers are awesome!'''   )
														# The output should be wrapped with an English and French welcome and goodbye
														self.assertEqual(mock_stdout.getvalue()				,     '''Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n'''   )







							@require_torch
							def   __lowerCAmelCase     (    self			:    int   )     ->				Optional[int]:


														"""simple docstring"""


														self.assertEqual(find_labels(lowerCAmelCase_   )				,     ['''labels''']   )
														self.assertEqual(find_labels(lowerCAmelCase_   )				,     ['''labels''', '''next_sentence_label''']   )
														self.assertEqual(find_labels(lowerCAmelCase_   )				,     ['''start_positions''', '''end_positions''']   )

														class      A       (						_a       ):
																					pass



														self.assertEqual(find_labels(lowerCAmelCase_   )				,     ['''labels''']   )







							@require_tf
							def   __lowerCAmelCase     (    self			:    Tuple   )     ->				Optional[Any]:


														"""simple docstring"""


														self.assertEqual(find_labels(lowerCAmelCase_   )				,     ['''labels''']   )
														self.assertEqual(find_labels(lowerCAmelCase_   )				,     ['''labels''', '''next_sentence_label''']   )
														self.assertEqual(find_labels(lowerCAmelCase_   )				,     ['''start_positions''', '''end_positions''']   )

														class      A       (						_a       ):
																					pass



														self.assertEqual(find_labels(lowerCAmelCase_   )				,     ['''labels''']   )







							@require_flax
							def   __lowerCAmelCase     (    self			:    int   )     ->				Dict:


														"""simple docstring"""


														self.assertEqual(find_labels(lowerCAmelCase_   )				,     []   )
														self.assertEqual(find_labels(lowerCAmelCase_   )				,     []   )
														self.assertEqual(find_labels(lowerCAmelCase_   )				,     []   )

														class      A       (						_a       ):
																					pass



														self.assertEqual(find_labels(lowerCAmelCase_   )				,     []   )







'''simple docstring'''


import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()

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







							def   __lowerCAmelCase     (    self			:    int   )     ->				Any:


														"""simple docstring"""


														super().tearDown()
														gc.collect()
														torch.cuda.empty_cache()







							def   __lowerCAmelCase     (    self			:    List[Any]   )     ->				int:


														"""simple docstring"""


														_a      	=							StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''   )
														_a      	=							sd_pipe.to(lowerCAmelCase_   )
														sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_   )

														sd_pipe.set_scheduler('''sample_euler'''   )

														_a      	=							'''A painting of a squirrel eating a burger'''
														_a      	=							torch.manual_seed(0   )
														_a      	=							sd_pipe([prompt]				,     generator=lowerCAmelCase_				,     guidance_scale=9.0				,     num_inference_steps=20				,     output_type='''np'''   )

														_a      	=							output.images

														_a      	=							image[0, -3:, -3:, -1]

														assert image.shape == (1, 5_12, 5_12, 3)
														_a      	=							np.array([0.0_4_4_7, 0.0_4_9_2, 0.0_4_6_8, 0.0_4_0_8, 0.0_3_8_3, 0.0_4_0_8, 0.0_3_5_4, 0.0_3_8_0, 0.0_3_3_9]   )

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







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


														"""simple docstring"""


														_a      	=							StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''   )
														_a      	=							sd_pipe.to(lowerCAmelCase_   )
														sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_   )

														sd_pipe.set_scheduler('''sample_euler'''   )

														_a      	=							'''A painting of a squirrel eating a burger'''
														_a      	=							torch.manual_seed(0   )
														_a      	=							sd_pipe([prompt]				,     generator=lowerCAmelCase_				,     guidance_scale=9.0				,     num_inference_steps=20				,     output_type='''np'''   )

														_a      	=							output.images

														_a      	=							image[0, -3:, -3:, -1]

														assert image.shape == (1, 5_12, 5_12, 3)
														_a      	=							np.array([0.1_2_3_7, 0.1_3_2_0, 0.1_4_3_8, 0.1_3_5_9, 0.1_3_9_0, 0.1_1_3_2, 0.1_2_7_7, 0.1_1_7_5, 0.1_1_1_2]   )

														assert np.abs(image_slice.flatten() - expected_slice   ).max() < 5e-1







							def   __lowerCAmelCase     (    self			:    Dict   )     ->				Optional[Any]:


														"""simple docstring"""


														_a      	=							StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''   )
														_a      	=							sd_pipe.to(lowerCAmelCase_   )
														sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_   )

														sd_pipe.set_scheduler('''sample_dpmpp_2m'''   )

														_a      	=							'''A painting of a squirrel eating a burger'''
														_a      	=							torch.manual_seed(0   )
														_a      	=							sd_pipe(
														    [prompt]				,     generator=lowerCAmelCase_				,     guidance_scale=7.5				,     num_inference_steps=15				,     output_type='''np'''				,     use_karras_sigmas=lowerCAmelCase_				,     )

														_a      	=							output.images

														_a      	=							image[0, -3:, -3:, -1]

														assert image.shape == (1, 5_12, 5_12, 3)
														_a      	=							np.array(
														    [0.1_1_3_8_1_6_8_9, 0.1_2_1_1_2_9_2_1, 0.1_3_8_9_4_5_7, 0.1_2_5_4_9_6_0_6, 0.1_2_4_4_9_6_4, 0.1_0_8_3_1_5_1_7, 0.1_1_5_6_2_8_6_6, 0.1_0_8_6_7_8_1_6, 0.1_0_4_9_9_0_4_8]   )

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

