





import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
          CTRLForSequenceClassification,
          CTRLLMHeadModel,
          CTRLModel,
      )

class 		UpperCamelCase__ :



     '''simple docstring'''

     def __init__(							self						:    Union[str, Any]						,lowerCamelCase__						:    Union[str, Any]						,lowerCamelCase__						:    Optional[Any]=14						,lowerCamelCase__						:    str=7						,lowerCamelCase__						:    int=True						,lowerCamelCase__						:    Optional[int]=True						,lowerCamelCase__						:    Optional[Any]=True						,lowerCamelCase__						:    str=True						,lowerCamelCase__						:    Union[str, Any]=True						,lowerCamelCase__						:    Tuple=99						,lowerCamelCase__						:    Union[str, Any]=32						,lowerCamelCase__						:    Optional[int]=5						,lowerCamelCase__						:    Dict=4						,lowerCamelCase__						:    Tuple=37						,lowerCamelCase__						:    Dict="gelu"						,lowerCamelCase__						:    int=0.1						,lowerCamelCase__						:    Any=0.1						,lowerCamelCase__						:    Union[str, Any]=512						,lowerCamelCase__						:    Optional[int]=16						,lowerCamelCase__						:    Optional[int]=2						,lowerCamelCase__						:    Optional[Any]=0.02						,lowerCamelCase__						:    int=3						,lowerCamelCase__						:    Dict=4						,lowerCamelCase__						:    List[str]=None						,)     ->		Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						parent
           SCREAMING_SNAKE_CASE		=						batch_size
           SCREAMING_SNAKE_CASE		=						seq_length
           SCREAMING_SNAKE_CASE		=						is_training
           SCREAMING_SNAKE_CASE		=						use_token_type_ids
           SCREAMING_SNAKE_CASE		=						use_input_mask
           SCREAMING_SNAKE_CASE		=						use_labels
           SCREAMING_SNAKE_CASE		=						use_mc_token_ids
           SCREAMING_SNAKE_CASE		=						vocab_size
           SCREAMING_SNAKE_CASE		=						hidden_size
           SCREAMING_SNAKE_CASE		=						num_hidden_layers
           SCREAMING_SNAKE_CASE		=						num_attention_heads
           SCREAMING_SNAKE_CASE		=						intermediate_size
           SCREAMING_SNAKE_CASE		=						hidden_act
           SCREAMING_SNAKE_CASE		=						hidden_dropout_prob
           SCREAMING_SNAKE_CASE		=						attention_probs_dropout_prob
           SCREAMING_SNAKE_CASE		=						max_position_embeddings
           SCREAMING_SNAKE_CASE		=						type_vocab_size
           SCREAMING_SNAKE_CASE		=						type_sequence_label_size
           SCREAMING_SNAKE_CASE		=						initializer_range
           SCREAMING_SNAKE_CASE		=						num_labels
           SCREAMING_SNAKE_CASE		=						num_choices
           SCREAMING_SNAKE_CASE		=						scope
           SCREAMING_SNAKE_CASE		=						self.vocab_size - 1






     def 				SCREAMING_SNAKE_CASE__  (							self						:    int       )     ->		Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						ids_tensor([self.batch_size, self.seq_length]						,self.vocab_size       )

           SCREAMING_SNAKE_CASE		=						None
           if self.use_input_mask:
                 SCREAMING_SNAKE_CASE		=						random_attention_mask([self.batch_size, self.seq_length]       )

           SCREAMING_SNAKE_CASE		=						None
           if self.use_token_type_ids:
                 SCREAMING_SNAKE_CASE		=						ids_tensor([self.batch_size, self.seq_length]						,self.type_vocab_size       )

           SCREAMING_SNAKE_CASE		=						None
           if self.use_mc_token_ids:
                 SCREAMING_SNAKE_CASE		=						ids_tensor([self.batch_size, self.num_choices]						,self.seq_length       )

           SCREAMING_SNAKE_CASE		=						None
           SCREAMING_SNAKE_CASE		=						None
           SCREAMING_SNAKE_CASE		=						None
           if self.use_labels:
                 SCREAMING_SNAKE_CASE		=						ids_tensor([self.batch_size]						,self.type_sequence_label_size       )
                 SCREAMING_SNAKE_CASE		=						ids_tensor([self.batch_size, self.seq_length]						,self.num_labels       )
                 SCREAMING_SNAKE_CASE		=						ids_tensor([self.batch_size]						,self.num_choices       )

           SCREAMING_SNAKE_CASE		=						self.get_config()

           SCREAMING_SNAKE_CASE		=						ids_tensor([self.num_hidden_layers, self.num_attention_heads]						,2       )

           return (
               config,
               input_ids,
               input_mask,
               head_mask,
               token_type_ids,
               mc_token_ids,
               sequence_labels,
               token_labels,
               choice_labels,
           )






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




           '''simple docstring'''


           return CTRLConfig(
               vocab_size=self.vocab_size						,n_embd=self.hidden_size						,n_layer=self.num_hidden_layers						,n_head=self.num_attention_heads						,n_positions=self.max_position_embeddings						,pad_token_id=self.pad_token_id						,)






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Tuple						,lowerCamelCase__						:    Optional[int]						,lowerCamelCase__						:    List[str]						,lowerCamelCase__						:    Optional[int]						,lowerCamelCase__						:    Optional[int]						,lowerCamelCase__						:    str						,*lowerCamelCase__						:    Any       )     ->		List[str]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						CTRLModel(config=lowerCamelCase__       )
           model.to(lowerCamelCase__       )
           model.eval()

           model(lowerCamelCase__						,token_type_ids=lowerCamelCase__						,head_mask=lowerCamelCase__       )
           model(lowerCamelCase__						,token_type_ids=lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						model(lowerCamelCase__       )
           self.parent.assertEqual(result.last_hidden_state.shape						,(self.batch_size, self.seq_length, self.hidden_size)       )
           self.parent.assertEqual(len(result.past_key_values       )						,config.n_layer       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    str						,lowerCamelCase__						:    List[Any]						,lowerCamelCase__						:    Optional[Any]						,lowerCamelCase__						:    Union[str, Any]						,lowerCamelCase__						:    int						,lowerCamelCase__						:    Any						,*lowerCamelCase__						:    List[str]       )     ->		Dict:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						CTRLLMHeadModel(lowerCamelCase__       )
           model.to(lowerCamelCase__       )
           model.eval()

           SCREAMING_SNAKE_CASE		=						model(lowerCamelCase__						,token_type_ids=lowerCamelCase__						,labels=lowerCamelCase__       )
           self.parent.assertEqual(result.loss.shape						,()       )
           self.parent.assertEqual(result.logits.shape						,(self.batch_size, self.seq_length, self.vocab_size)       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		List[str]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.prepare_config_and_inputs()

           (
               (
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					(
               SCREAMING_SNAKE_CASE
           ),					
           )		=						config_and_inputs

           SCREAMING_SNAKE_CASE		=						{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

           return config, inputs_dict






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Union[str, Any]						,lowerCamelCase__						:    int						,lowerCamelCase__						:    Optional[int]						,lowerCamelCase__						:    List[str]						,lowerCamelCase__						:    List[Any]						,*lowerCamelCase__						:    Optional[int]       )     ->		int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.num_labels
           SCREAMING_SNAKE_CASE		=						CTRLForSequenceClassification(lowerCamelCase__       )
           model.to(lowerCamelCase__       )
           model.eval()
           SCREAMING_SNAKE_CASE		=						ids_tensor([self.batch_size]						,self.type_sequence_label_size       )
           SCREAMING_SNAKE_CASE		=						model(lowerCamelCase__						,token_type_ids=lowerCamelCase__						,labels=lowerCamelCase__       )
           self.parent.assertEqual(result.logits.shape						,(self.batch_size, self.num_labels)       )

@require_torch
class 		UpperCamelCase__ (      lowerCAmelCase_							,      lowerCAmelCase_							,      lowerCAmelCase_							,      unittest.TestCase   ):



     '''simple docstring'''

     __snake_case    :	int			=			(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
     __snake_case    :	Optional[int]			=			(CTRLLMHeadModel,) if is_torch_available() else ()
     __snake_case    :	List[Any]			=			(
         {
             "feature-extraction": CTRLModel,
             "text-classification": CTRLForSequenceClassification,
             "text-generation": CTRLLMHeadModel,
             "zero-shot": CTRLForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     __snake_case    :	List[Any]			=			True
     __snake_case    :	Any			=			False
     __snake_case    :	int			=			False






     def 				SCREAMING_SNAKE_CASE__  (							self						:    List[str]						,lowerCamelCase__						:    Optional[Any]						,lowerCamelCase__						:    Dict						,lowerCamelCase__						:    Any						,lowerCamelCase__						:    int						,lowerCamelCase__						:    Union[str, Any]       )     ->		List[Any]:




           '''simple docstring'''


           if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
                 # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
                 # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
                 # config could not be created.
                 return True

           return False






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Any       )     ->		Any:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						CTRLModelTester(self       )
           SCREAMING_SNAKE_CASE		=						ConfigTester(self						,config_class=lowerCamelCase__						,n_embd=37       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    List[str]       )     ->		int:




           '''simple docstring'''


           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()






     def 				SCREAMING_SNAKE_CASE__  (							self						:    List[Any]       )     ->		Tuple:




           '''simple docstring'''


           self.config_tester.run_common_tests()






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[int]       )     ->		int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_ctrl_model(*lowerCamelCase__       )






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head_model(*lowerCamelCase__       )






     @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""       )
     def 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		Any:




           '''simple docstring'''


           pass






     @slow
     def 				SCREAMING_SNAKE_CASE__  (							self						:    str       )     ->		str:




           '''simple docstring'''


           for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 SCREAMING_SNAKE_CASE		=						CTRLModel.from_pretrained(lowerCamelCase__       )
                 self.assertIsNotNone(lowerCamelCase__       )






     @unittest.skip("""The model doesn't support left padding"""       )  # and it's not used enough to be worth fixing :)
     def 				SCREAMING_SNAKE_CASE__  (							self						:    int       )     ->		Dict:




           '''simple docstring'''


           pass

@require_torch
class 		UpperCamelCase__ (      unittest.TestCase   ):



     '''simple docstring'''

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




           '''simple docstring'''


           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()






     @slow
     def 				SCREAMING_SNAKE_CASE__  (							self						:    List[str]       )     ->		int:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						CTRLLMHeadModel.from_pretrained("""ctrl"""       )
           model.to(lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						torch.tensor(
               [[11859, 0, 1611, 8]]						,dtype=torch.long						,device=lowerCamelCase__       )  # Legal the president is
           SCREAMING_SNAKE_CASE		=						[
               11859,
               0,
               1611,
               8,
               5,
               150,
               26449,
               2,
               19,
               348,
               469,
               3,
               2595,
               48,
               20740,
               246533,
               246533,
               19,
               30,
               5,
           ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

           SCREAMING_SNAKE_CASE		=						model.generate(lowerCamelCase__						,do_sample=lowerCamelCase__       )
           self.assertListEqual(output_ids[0].tolist()						,lowerCamelCase__       )









import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()

class 		UpperCamelCase__ (      unittest.TestCase   ):



     '''simple docstring'''

     @property
     def 				SCREAMING_SNAKE_CASE__  (							self						:    str       )     ->		Optional[int]:




           '''simple docstring'''


           torch.manual_seed(0       )
           SCREAMING_SNAKE_CASE		=						UNetaDModel(
               block_out_channels=(32, 64)						,layers_per_block=2						,sample_size=32						,in_channels=3						,out_channels=3						,down_block_types=("""DownBlock2D""", """AttnDownBlock2D""")						,up_block_types=("""AttnUpBlock2D""", """UpBlock2D""")						,)
           return model






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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						self.dummy_uncond_unet
           SCREAMING_SNAKE_CASE		=						KarrasVeScheduler()

           SCREAMING_SNAKE_CASE		=						KarrasVePipeline(unet=lowerCamelCase__						,scheduler=lowerCamelCase__       )
           pipe.to(lowerCamelCase__       )
           pipe.set_progress_bar_config(disable=lowerCamelCase__       )

           SCREAMING_SNAKE_CASE		=						torch.manual_seed(0       )
           SCREAMING_SNAKE_CASE		=						pipe(num_inference_steps=2						,generator=lowerCamelCase__						,output_type="""numpy"""       ).images

           SCREAMING_SNAKE_CASE		=						torch.manual_seed(0       )
           SCREAMING_SNAKE_CASE		=						pipe(num_inference_steps=2						,generator=lowerCamelCase__						,output_type="""numpy"""						,return_dict=lowerCamelCase__       )[0]

           SCREAMING_SNAKE_CASE		=						image[0, -3:, -3:, -1]
           SCREAMING_SNAKE_CASE		=						image_from_tuple[0, -3:, -3:, -1]

           assert image.shape == (1, 32, 32, 3)
           SCREAMING_SNAKE_CASE		=						np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]       )

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

@slow
@require_torch
class 		UpperCamelCase__ (      unittest.TestCase   ):



     '''simple docstring'''

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




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						"""google/ncsnpp-celebahq-256"""
           SCREAMING_SNAKE_CASE		=						UNetaDModel.from_pretrained(lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						KarrasVeScheduler()

           SCREAMING_SNAKE_CASE		=						KarrasVePipeline(unet=lowerCamelCase__						,scheduler=lowerCamelCase__       )
           pipe.to(lowerCamelCase__       )
           pipe.set_progress_bar_config(disable=lowerCamelCase__       )

           SCREAMING_SNAKE_CASE		=						torch.manual_seed(0       )
           SCREAMING_SNAKE_CASE		=						pipe(num_inference_steps=20						,generator=lowerCamelCase__						,output_type="""numpy"""       ).images

           SCREAMING_SNAKE_CASE		=						image[0, -3:, -3:, -1]
           assert image.shape == (1, 256, 256, 3)
           SCREAMING_SNAKE_CASE		=						np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586]       )

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


