



"""simple docstring"""





import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()
class  lowercase__  (       SCREAMING_SNAKE_CASE							,   SCREAMING_SNAKE_CASE							,   unittest.TestCase ):







       '''simple docstring'''


       UpperCamelCase		=	VQModel
       UpperCamelCase		=	'''sample'''



       @property
       def 	lowercase__							(				self     :    str						,							_UpperCAmelCase     :    str=(32, 32)       )					->		Union[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_     					=				4
             UpperCAmelCase_     					=				3

             UpperCAmelCase_     					=				floats_tensor((batch_size, num_channels) + sizes       ).to(_UpperCAmelCase       )

             return {"sample": image}



       @property
       def 	lowercase__							(				self     :    Dict       )					->		List[Any]:


             '''simple docstring'''




             return (3, 32, 32)



       @property
       def 	lowercase__							(				self     :    int       )					->		Dict:


             '''simple docstring'''




             return (3, 32, 32)



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


             '''simple docstring'''




             UpperCAmelCase_     					=				{
                 "block_out_channels": [32, 64],
                 "in_channels": 3,
                 "out_channels": 3,
                 "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"],
                 "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"],
                 "latent_channels": 3,
             }
             UpperCAmelCase_     					=				self.dummy_input
             return init_dict, inputs_dict



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


             '''simple docstring'''




             pass



       def 	lowercase__							(				self     :    Any       )					->		Optional[int]:


             '''simple docstring'''




             pass



       def 	lowercase__							(				self     :    Tuple       )					->		Any:


             '''simple docstring'''




             UpperCAmelCase_					,       UpperCAmelCase_     					=				VQModel.from_pretrained("fusing/vqgan-dummy"						,							output_loading_info=_UpperCAmelCase       )
             self.assertIsNotNone(_UpperCAmelCase       )
             self.assertEqual(len(loading_info["missing_keys"]       )						,							0       )

             model.to(_UpperCAmelCase       )
             UpperCAmelCase_     					=				model(**self.dummy_input       )

             assert image is not None, "Make sure output is not None"







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


             '''simple docstring'''




             UpperCAmelCase_     					=				VQModel.from_pretrained("fusing/vqgan-dummy"       )
             model.to(_UpperCAmelCase       ).eval()

             torch.manual_seed(0       )
             if torch.cuda.is_available():
                   torch.cuda.manual_seed_all(0       )

             UpperCAmelCase_     					=				torch.randn(1						,							model.config.in_channels						,							model.config.sample_size						,							model.config.sample_size       )
             UpperCAmelCase_     					=				image.to(_UpperCAmelCase       )
             with torch.no_grad():
                   UpperCAmelCase_     					=				model(_UpperCAmelCase       ).sample

             UpperCAmelCase_     					=				output[0, -1, -3:, -3:].flatten().cpu()
             # fmt: off
             UpperCAmelCase_     					=				torch.tensor([-0.0153, -0.4044, -0.1880, -0.5161, -0.2418, -0.4072, -0.1612, -0.0633, -0.0143]       )
             # fmt: on
             self.assertTrue(torch.allclose(_UpperCAmelCase						,							_UpperCAmelCase						,							atol=1e-3       )       )





'''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


A_     :						Optional[Any]    							=DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

A_     :						Tuple    							='''main'''
# Default branch name
A_     :						Dict    							='''f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'''
# One particular commit (not the top of `main`)
A_     :						int    							='''aaaaaaa'''
# This commit does not exist, so we should 404.

A_     :						List[str]    							='''d9e9f15bc825e4b2c9249e9578f884bbcb5e3684'''
# Sha-1 of config.json on the top of `main`, for checking purposes
A_     :						List[str]    							='''4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'''



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



@contextlib.contextmanager
def snake_case_      (					)			->					str:
   print('''Bonjour!''')
   yield
   print('''Au revoir!''')


class     __UpperCAmelCase       ( unittest.TestCase		):





  def 						UpperCAmelCase_	(	self ):
     # If the spec is missing, importlib would not be able to import the module dynamically.
     assert transformers.__spec__ is not None
     assert importlib.util.find_spec('''transformers''' ) is not None






class     __UpperCAmelCase       ( unittest.TestCase		):





  @unittest.mock.patch('''sys.stdout'''    ,     new_callable=io.StringIO )
  def 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     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 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     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 						UpperCAmelCase_	(	self    ,     _lowerCamelCase ):
     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 						UpperCAmelCase_	(	self ):
     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     __UpperCAmelCase       ( __a		):
       pass






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





  @require_tf
  def 						UpperCAmelCase_	(	self ):
     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     __UpperCAmelCase       ( __a		):
       pass






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







  @require_flax
  def 						UpperCAmelCase_	(	self ):
     # Flax models don't have labels
     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )
     self.assertEqual(find_labels(_lowerCamelCase )    ,     [] )


     class     __UpperCAmelCase       ( __a		):
       pass






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





