
"""simple docstring"""


import math
import os
import unittest

from transformers import MegatronBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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 (
							    MODEL_FOR_PRETRAINING_MAPPING,
							    MegatronBertForCausalLM,
							    MegatronBertForMaskedLM,
							    MegatronBertForMultipleChoice,
							    MegatronBertForNextSentencePrediction,
							    MegatronBertForPreTraining,
							    MegatronBertForQuestionAnswering,
							    MegatronBertForSequenceClassification,
							    MegatronBertForTokenClassification,
							    MegatronBertModel,
							)

class     A_	:



							"""simple docstring"""






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





										"""simple docstring"""

										lowerCamelCase__	:     Optional[Any]              =parent
										lowerCamelCase__	:     Tuple              =batch_size
										lowerCamelCase__	:     str              =seq_length
										lowerCamelCase__	:     str              =is_training
										lowerCamelCase__	:     Dict              =use_input_mask
										lowerCamelCase__	:     Optional[int]              =use_token_type_ids
										lowerCamelCase__	:     Optional[Any]              =use_labels
										lowerCamelCase__	:     Any              =vocab_size
										lowerCamelCase__	:     Tuple              =hidden_size
										lowerCamelCase__	:     List[Any]              =embedding_size
										lowerCamelCase__	:     Dict              =num_hidden_layers
										lowerCamelCase__	:     Tuple              =num_attention_heads
										lowerCamelCase__	:     Optional[int]              =intermediate_size
										lowerCamelCase__	:     Optional[int]              =hidden_act
										lowerCamelCase__	:     Any              =hidden_dropout_prob
										lowerCamelCase__	:     Union[str, Any]              =attention_probs_dropout_prob
										lowerCamelCase__	:     List[Any]              =max_position_embeddings
										lowerCamelCase__	:     Optional[Any]              =type_vocab_size
										lowerCamelCase__	:     List[str]              =type_sequence_label_size
										lowerCamelCase__	:     Any              =initializer_range
										lowerCamelCase__	:     Dict              =num_labels
										lowerCamelCase__	:     List[Any]              =num_choices
										lowerCamelCase__	:     Tuple              =scope





							def       UpperCAmelCase__							(  self  :Any       ):





										"""simple docstring"""

										lowerCamelCase__	:     str              =ids_tensor([self.batch_size, self.seq_length]     ,							self.vocab_size       )

										lowerCamelCase__	:     List[str]              =None
										if self.use_input_mask:
													lowerCamelCase__	:     Optional[Any]              =random_attention_mask([self.batch_size, self.seq_length]       )

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

										lowerCamelCase__	:     Dict              =None
										lowerCamelCase__	:     Optional[int]              =None
										lowerCamelCase__	:     str              =None
										if self.use_labels:
													lowerCamelCase__	:     List[str]              =ids_tensor([self.batch_size]     ,							self.type_sequence_label_size       )
													lowerCamelCase__	:     Optional[Any]              =ids_tensor([self.batch_size, self.seq_length]     ,							self.num_labels       )
													lowerCamelCase__	:     List[str]              =ids_tensor([self.batch_size]     ,							self.num_choices       )

										lowerCamelCase__	:     Any              =self.get_config()

										return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





							def       UpperCAmelCase__							(  self  :Tuple       ):





										"""simple docstring"""

										return MegatronBertConfig(
										    vocab_size=self.vocab_size     ,							hidden_size=self.hidden_size     ,							num_hidden_layers=self.num_hidden_layers     ,							num_attention_heads=self.num_attention_heads     ,							intermediate_size=self.intermediate_size     ,							embedding_size=self.embedding_size     ,							hidden_act=self.hidden_act     ,							hidden_dropout_prob=self.hidden_dropout_prob     ,							attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,							max_position_embeddings=self.max_position_embeddings     ,							type_vocab_size=self.type_vocab_size     ,							is_decoder=lowerCamelCase_     ,							initializer_range=self.initializer_range     ,							)





							def       UpperCAmelCase__							(  self  :Union[str, Any]     ,							lowerCamelCase_  :Optional[Any]     ,							lowerCamelCase_  :Tuple     ,							lowerCamelCase_  :List[str]     ,							lowerCamelCase_  :List[str]     ,							lowerCamelCase_  :Optional[int]     ,							lowerCamelCase_  :List[str]     ,							lowerCamelCase_  :Tuple       ):





										"""simple docstring"""

										lowerCamelCase__	:     Any              =MegatronBertModel(config=lowerCamelCase_       )
										model.to(lowerCamelCase_       )
										model.eval()
										lowerCamelCase__	:     Any              =model(lowerCamelCase_     ,							attention_mask=lowerCamelCase_     ,							token_type_ids=lowerCamelCase_       )
										lowerCamelCase__	:     List[Any]              =model(lowerCamelCase_     ,							token_type_ids=lowerCamelCase_       )
										lowerCamelCase__	:     Dict              =model(lowerCamelCase_       )

										self.parent.assertEqual(result.last_hidden_state.shape     ,							(self.batch_size, self.seq_length, self.hidden_size)       )
										self.parent.assertEqual(result.pooler_output.shape     ,							(self.batch_size, self.hidden_size)       )





							def       UpperCAmelCase__							(  self  :int     ,							lowerCamelCase_  :Tuple     ,							lowerCamelCase_  :Optional[Any]     ,							lowerCamelCase_  :Any     ,							lowerCamelCase_  :Dict     ,							lowerCamelCase_  :int     ,							lowerCamelCase_  :Any     ,							lowerCamelCase_  :int       ):





										"""simple docstring"""

										lowerCamelCase__	:     Optional[int]              =MegatronBertForMaskedLM(config=lowerCamelCase_       )
										model.to(lowerCamelCase_       )
										model.eval()
										lowerCamelCase__	:     str              =model(lowerCamelCase_     ,							attention_mask=lowerCamelCase_     ,							token_type_ids=lowerCamelCase_     ,							labels=lowerCamelCase_       )
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.seq_length, self.vocab_size)       )





							def       UpperCAmelCase__							(  self  :Any     ,							lowerCamelCase_  :Any     ,							lowerCamelCase_  :int     ,							lowerCamelCase_  :Any     ,							lowerCamelCase_  :List[str]     ,							lowerCamelCase_  :List[Any]     ,							lowerCamelCase_  :List[Any]     ,							lowerCamelCase_  :int       ):





										"""simple docstring"""

										lowerCamelCase__	:     Any              =MegatronBertForCausalLM(config=lowerCamelCase_       )
										model.to(lowerCamelCase_       )
										model.eval()
										lowerCamelCase__	:     List[Any]              =model(lowerCamelCase_     ,							attention_mask=lowerCamelCase_     ,							token_type_ids=lowerCamelCase_     ,							labels=lowerCamelCase_       )
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.seq_length, self.vocab_size)       )





							def       UpperCAmelCase__							(  self  :int     ,							lowerCamelCase_  :Optional[Any]     ,							lowerCamelCase_  :Optional[Any]     ,							lowerCamelCase_  :Tuple     ,							lowerCamelCase_  :Optional[Any]     ,							lowerCamelCase_  :List[Any]     ,							lowerCamelCase_  :Optional[int]     ,							lowerCamelCase_  :List[Any]       ):





										"""simple docstring"""

										lowerCamelCase__	:     Dict              =MegatronBertForNextSentencePrediction(config=lowerCamelCase_       )
										model.to(lowerCamelCase_       )
										model.eval()
										lowerCamelCase__	:     Optional[int]              =model(
										    lowerCamelCase_     ,							attention_mask=lowerCamelCase_     ,							token_type_ids=lowerCamelCase_     ,							labels=lowerCamelCase_     ,							)
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, 2)       )





							def       UpperCAmelCase__							(  self  :Any     ,							lowerCamelCase_  :List[str]     ,							lowerCamelCase_  :Union[str, Any]     ,							lowerCamelCase_  :int     ,							lowerCamelCase_  :List[str]     ,							lowerCamelCase_  :List[Any]     ,							lowerCamelCase_  :Optional[int]     ,							lowerCamelCase_  :Union[str, Any]       ):





										"""simple docstring"""

										lowerCamelCase__	:     int              =MegatronBertForPreTraining(config=lowerCamelCase_       )
										model.to(lowerCamelCase_       )
										model.eval()
										lowerCamelCase__	:     List[Any]              =model(
										    lowerCamelCase_     ,							attention_mask=lowerCamelCase_     ,							token_type_ids=lowerCamelCase_     ,							labels=lowerCamelCase_     ,							next_sentence_label=lowerCamelCase_     ,							)
										self.parent.assertEqual(result.prediction_logits.shape     ,							(self.batch_size, self.seq_length, self.vocab_size)       )
										self.parent.assertEqual(result.seq_relationship_logits.shape     ,							(self.batch_size, 2)       )





							def       UpperCAmelCase__							(  self  :Any     ,							lowerCamelCase_  :Union[str, Any]     ,							lowerCamelCase_  :Optional[Any]     ,							lowerCamelCase_  :Union[str, Any]     ,							lowerCamelCase_  :List[Any]     ,							lowerCamelCase_  :List[str]     ,							lowerCamelCase_  :int     ,							lowerCamelCase_  :int       ):





										"""simple docstring"""

										lowerCamelCase__	:     Tuple              =MegatronBertForQuestionAnswering(config=lowerCamelCase_       )
										model.to(lowerCamelCase_       )
										model.eval()
										lowerCamelCase__	:     List[str]              =model(
										    lowerCamelCase_     ,							attention_mask=lowerCamelCase_     ,							token_type_ids=lowerCamelCase_     ,							start_positions=lowerCamelCase_     ,							end_positions=lowerCamelCase_     ,							)
										self.parent.assertEqual(result.start_logits.shape     ,							(self.batch_size, self.seq_length)       )
										self.parent.assertEqual(result.end_logits.shape     ,							(self.batch_size, self.seq_length)       )





							def       UpperCAmelCase__							(  self  :Optional[int]     ,							lowerCamelCase_  :List[Any]     ,							lowerCamelCase_  :str     ,							lowerCamelCase_  :Optional[int]     ,							lowerCamelCase_  :str     ,							lowerCamelCase_  :List[str]     ,							lowerCamelCase_  :List[Any]     ,							lowerCamelCase_  :str       ):





										"""simple docstring"""

										lowerCamelCase__	:     Dict              =self.num_labels
										lowerCamelCase__	:     Tuple              =MegatronBertForSequenceClassification(lowerCamelCase_       )
										model.to(lowerCamelCase_       )
										model.eval()
										lowerCamelCase__	:     Union[str, Any]              =model(lowerCamelCase_     ,							attention_mask=lowerCamelCase_     ,							token_type_ids=lowerCamelCase_     ,							labels=lowerCamelCase_       )
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_labels)       )





							def       UpperCAmelCase__							(  self  :List[Any]     ,							lowerCamelCase_  :Optional[int]     ,							lowerCamelCase_  :Optional[int]     ,							lowerCamelCase_  :Optional[Any]     ,							lowerCamelCase_  :List[Any]     ,							lowerCamelCase_  :List[str]     ,							lowerCamelCase_  :Union[str, Any]     ,							lowerCamelCase_  :Tuple       ):





										"""simple docstring"""

										lowerCamelCase__	:     int              =self.num_labels
										lowerCamelCase__	:     Optional[int]              =MegatronBertForTokenClassification(config=lowerCamelCase_       )
										model.to(lowerCamelCase_       )
										model.eval()
										lowerCamelCase__	:     Union[str, Any]              =model(lowerCamelCase_     ,							attention_mask=lowerCamelCase_     ,							token_type_ids=lowerCamelCase_     ,							labels=lowerCamelCase_       )
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.seq_length, self.num_labels)       )





							def       UpperCAmelCase__							(  self  :Tuple     ,							lowerCamelCase_  :Dict     ,							lowerCamelCase_  :str     ,							lowerCamelCase_  :List[Any]     ,							lowerCamelCase_  :List[Any]     ,							lowerCamelCase_  :str     ,							lowerCamelCase_  :Any     ,							lowerCamelCase_  :str       ):





										"""simple docstring"""

										lowerCamelCase__	:     Union[str, Any]              =self.num_choices
										lowerCamelCase__	:     Optional[int]              =MegatronBertForMultipleChoice(config=lowerCamelCase_       )
										model.to(lowerCamelCase_       )
										model.eval()
										lowerCamelCase__	:     Tuple              =input_ids.unsqueeze(1       ).expand(-1     ,							self.num_choices     ,							-1       ).contiguous()
										lowerCamelCase__	:     int              =token_type_ids.unsqueeze(1       ).expand(-1     ,							self.num_choices     ,							-1       ).contiguous()
										lowerCamelCase__	:     List[Any]              =input_mask.unsqueeze(1       ).expand(-1     ,							self.num_choices     ,							-1       ).contiguous()
										lowerCamelCase__	:     Optional[int]              =model(
										    lowerCamelCase_     ,							attention_mask=lowerCamelCase_     ,							token_type_ids=lowerCamelCase_     ,							labels=lowerCamelCase_     ,							)
										self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_choices)       )





							def       UpperCAmelCase__							(  self  :int       ):





										"""simple docstring"""

										lowerCamelCase__	:     List[Any]              =self.prepare_config_and_inputs()
										(
										    lowerCamelCase__				
										)	:     List[str]              =config_and_inputs
										lowerCamelCase__	:     Optional[int]              ={'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
										return config, inputs_dict






@require_torch
class     A_	(     _a      ,					_a      ,					unittest.TestCase   ):



							"""simple docstring"""






							SCREAMING_SNAKE_CASE_	      =   (
							    (
							        MegatronBertModel,
							        MegatronBertForMaskedLM,
							        MegatronBertForCausalLM,
							        MegatronBertForMultipleChoice,
							        MegatronBertForNextSentencePrediction,
							        MegatronBertForPreTraining,
							        MegatronBertForQuestionAnswering,
							        MegatronBertForSequenceClassification,
							        MegatronBertForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							SCREAMING_SNAKE_CASE_	      =   (
							    {
							        """feature-extraction""": MegatronBertModel,
							        """fill-mask""": MegatronBertForMaskedLM,
							        """question-answering""": MegatronBertForQuestionAnswering,
							        """text-classification""": MegatronBertForSequenceClassification,
							        """text-generation""": MegatronBertForCausalLM,
							        """token-classification""": MegatronBertForTokenClassification,
							        """zero-shot""": MegatronBertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							SCREAMING_SNAKE_CASE_	      =   True
							# test_resize_embeddings = False
							SCREAMING_SNAKE_CASE_	      =   False





							def       UpperCAmelCase__							(  self  :Dict     ,							lowerCamelCase_  :int     ,							lowerCamelCase_  :Optional[int]     ,							lowerCamelCase_  :Any=False       ):





										"""simple docstring"""

										lowerCamelCase__	:     int              =super()._prepare_for_class(lowerCamelCase_     ,							lowerCamelCase_     ,							return_labels=lowerCamelCase_       )

										if return_labels:
													if model_class in get_values(lowerCamelCase_       ):
																lowerCamelCase__	:     Union[str, Any]              =torch.zeros(
																    (self.model_tester.batch_size, self.model_tester.seq_length)     ,							dtype=torch.long     ,							device=lowerCamelCase_       )
																lowerCamelCase__	:     Any              =torch.zeros(
																    self.model_tester.batch_size     ,							dtype=torch.long     ,							device=lowerCamelCase_       )
										return inputs_dict





							def       UpperCAmelCase__							(  self  :List[str]       ):





										"""simple docstring"""

										lowerCamelCase__	:     List[Any]              =MegatronBertModelTester(self       )
										lowerCamelCase__	:     List[Any]              =ConfigTester(self     ,							config_class=lowerCamelCase_     ,							hidden_size=37       )





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





										"""simple docstring"""

										self.config_tester.run_common_tests()





							def       UpperCAmelCase__							(  self  :List[Any]       ):





										"""simple docstring"""

										lowerCamelCase__	:     Dict              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_megatron_bert_model(*lowerCamelCase_       )





							def       UpperCAmelCase__							(  self  :str       ):





										"""simple docstring"""

										lowerCamelCase__	:     List[str]              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_megatron_bert_for_masked_lm(*lowerCamelCase_       )





							def       UpperCAmelCase__							(  self  :str       ):





										"""simple docstring"""

										lowerCamelCase__	:     Dict              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*lowerCamelCase_       )





							def       UpperCAmelCase__							(  self  :Any       ):





										"""simple docstring"""

										lowerCamelCase__	:     Tuple              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*lowerCamelCase_       )





							def       UpperCAmelCase__							(  self  :Any       ):





										"""simple docstring"""

										lowerCamelCase__	:     List[str]              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_megatron_bert_for_pretraining(*lowerCamelCase_       )





							def       UpperCAmelCase__							(  self  :Dict       ):





										"""simple docstring"""

										lowerCamelCase__	:     Optional[Any]              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_megatron_bert_for_question_answering(*lowerCamelCase_       )





							def       UpperCAmelCase__							(  self  :Dict       ):





										"""simple docstring"""

										lowerCamelCase__	:     List[Any]              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*lowerCamelCase_       )





							def       UpperCAmelCase__							(  self  :Dict       ):





										"""simple docstring"""

										lowerCamelCase__	:     Tuple              =self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_megatron_bert_for_token_classification(*lowerCamelCase_       )






def    lowerCAmelCase_   (		snake_case_		:			Union[str, Any]			)   ->Optional[Any]:
			return torch.tensor(
			    lowerCamelCase_							,    dtype=torch.long							,    device=lowerCamelCase_							,    )


lowerCAmelCase								=						1E-4

@require_torch
@require_sentencepiece
@require_tokenizers
class     A_	(     unittest.TestCase   ):



							"""simple docstring"""






							@slow
							@unittest.skip('Model is not available.'       )
							def       UpperCAmelCase__							(  self  :Optional[Any]       ):





										"""simple docstring"""

										lowerCamelCase__	:     int              ='nvidia/megatron-bert-uncased-345m'
										if "MYDIR" in os.environ:
													lowerCamelCase__	:     Optional[int]              =os.path.join(os.environ['MYDIR']     ,							lowerCamelCase_       )
										lowerCamelCase__	:     Optional[Any]              =MegatronBertModel.from_pretrained(lowerCamelCase_       )
										model.to(lowerCamelCase_       )
										model.half()
										lowerCamelCase__	:     List[Any]              =_long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]       )
										with torch.no_grad():
													lowerCamelCase__	:     Tuple              =model(lowerCamelCase_       )[0]
										lowerCamelCase__	:     Optional[Any]              =torch.Size((1, 9, 1_024)       )
										self.assertEqual(output.shape     ,							lowerCamelCase_       )

										lowerCamelCase__	:     List[Any]              =[-0.60_40, -0.25_17, -0.10_25, 0.34_20, -0.67_58, -0.00_17, -0.10_89, -0.19_90, 0.57_28]
										for ii in range(3       ):
													for jj in range(3       ):
																lowerCamelCase__	:     List[Any]              =output[0, ii, jj]
																lowerCamelCase__	:     Dict              =expected[3 * ii + jj]
																lowerCamelCase__	:     List[str]              ='ii={} jj={} a={} b={}'.format(lowerCamelCase_     ,							lowerCamelCase_     ,							lowerCamelCase_     ,							lowerCamelCase_       )
																self.assertTrue(math.isclose(lowerCamelCase_     ,							lowerCamelCase_     ,							rel_tol=lowerCamelCase_     ,							abs_tol=lowerCamelCase_       )     ,							msg=lowerCamelCase_       )



import gc
import unittest

import numpy as np
import torch
from torch.backends.cuda import sdp_kernel

from diffusers import (
    CMStochasticIterativeScheduler,
    ConsistencyModelPipeline,
    UNetaDModel,
)
from diffusers.utils import randn_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_a, require_torch_gpu

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


enable_full_determinism()






class _lowerCamelCase(						_a, unittest.TestCase   ):
       lowercase_ :	Union[str, Any]     =			ConsistencyModelPipeline
       lowercase_ :	Tuple     =			UNCONDITIONAL_IMAGE_GENERATION_PARAMS
       lowercase_ :	List[str]     =			UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS

       # Override required_optional_params to remove num_images_per_prompt
       lowercase_ :	List[str]     =			frozenset(
           [
               """num_inference_steps""",
               """generator""",
               """latents""",
               """output_type""",
               """return_dict""",
               """callback""",
               """callback_steps""",
           ]   )
       @property
       def 			UpperCamelCase     (					self)       ->   Tuple:




           """simple docstring"""

           _lowercase       :							Tuple      		=		UNetaDModel.from_pretrained(
               'diffusers/consistency-models-test',      subfolder='test_unet',      )
           return unet
       @property
       def 			UpperCamelCase     (					self)       ->   List[Any]:




           """simple docstring"""

           _lowercase       :							Tuple      		=		UNetaDModel.from_pretrained(
               'diffusers/consistency-models-test',      subfolder='test_unet_class_cond',      )
           return unet
       def 			UpperCamelCase     (					self,      lowerCamelCase=False)       ->   Dict:




           """simple docstring"""

           if class_cond:
               _lowercase       :							Union[str, Any]      		=		self.dummy_cond_unet
           else:
               _lowercase       :							Union[str, Any]      		=		self.dummy_uncond_unet

           # Default to CM multistep sampler
           _lowercase       :							List[str]      		=		CMStochasticIterativeScheduler(
               num_train_timesteps=40,      sigma_min=0.0_0_2,      sigma_max=8_0.0,      )

           _lowercase       :							Optional[Any]      		=		{
               'unet': unet,
               'scheduler': scheduler,
           }

           return components
       def 			UpperCamelCase     (					self,      lowerCamelCase,      lowerCamelCase=0)       ->   Tuple:




           """simple docstring"""

           if str(lowerCamelCase).startswith('mps'):
               _lowercase       :							str      		=		torch.manual_seed(lowerCamelCase)
           else:
               _lowercase       :							int      		=		torch.Generator(device=lowerCamelCase).manual_seed(lowerCamelCase)

           _lowercase       :							Tuple      		=		{
               'batch_size': 1,
               'num_inference_steps': None,
               'timesteps': [22, 0],
               'generator': generator,
               'output_type': 'np',
           }

           return inputs
       def 			UpperCamelCase     (					self)       ->   Any:




           """simple docstring"""

           _lowercase       :							Optional[int]      		=		'cpu'  # ensure determinism for the device-dependent torch.Generator
           _lowercase       :							Optional[int]      		=		self.get_dummy_components()
           _lowercase       :							str      		=		ConsistencyModelPipeline(**lowerCamelCase)
           _lowercase       :							Dict      		=		pipe.to(lowerCamelCase)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							Tuple      		=		self.get_dummy_inputs(lowerCamelCase)
           _lowercase       :							Optional[int]      		=		pipe(**lowerCamelCase).images
           assert image.shape == (1, 32, 32, 3)

           _lowercase       :							int      		=		image[0, -3:, -3:, -1]
           _lowercase       :							Dict      		=		np.array([0.3_5_7_2, 0.6_2_7_3, 0.4_0_3_1, 0.3_9_6_1, 0.4_3_2_1, 0.5_7_3_0, 0.5_2_6_6, 0.4_7_8_0, 0.5_0_0_4])

           assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3
       def 			UpperCamelCase     (					self)       ->   Any:




           """simple docstring"""

           _lowercase       :							Union[str, Any]      		=		'cpu'  # ensure determinism for the device-dependent torch.Generator
           _lowercase       :							Dict      		=		self.get_dummy_components(class_cond=lowerCamelCase)
           _lowercase       :							Any      		=		ConsistencyModelPipeline(**lowerCamelCase)
           _lowercase       :							str      		=		pipe.to(lowerCamelCase)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							Any      		=		self.get_dummy_inputs(lowerCamelCase)
           _lowercase       :							Any      		=		0
           _lowercase       :							List[str]      		=		pipe(**lowerCamelCase).images
           assert image.shape == (1, 32, 32, 3)

           _lowercase       :							Any      		=		image[0, -3:, -3:, -1]
           _lowercase       :							Union[str, Any]      		=		np.array([0.3_5_7_2, 0.6_2_7_3, 0.4_0_3_1, 0.3_9_6_1, 0.4_3_2_1, 0.5_7_3_0, 0.5_2_6_6, 0.4_7_8_0, 0.5_0_0_4])

           assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3
       def 			UpperCamelCase     (					self)       ->   Tuple:




           """simple docstring"""

           _lowercase       :							Optional[int]      		=		'cpu'  # ensure determinism for the device-dependent torch.Generator
           _lowercase       :							Any      		=		self.get_dummy_components()
           _lowercase       :							Optional[Any]      		=		ConsistencyModelPipeline(**lowerCamelCase)
           _lowercase       :							List[str]      		=		pipe.to(lowerCamelCase)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							Optional[Any]      		=		self.get_dummy_inputs(lowerCamelCase)
           _lowercase       :							Union[str, Any]      		=		1
           _lowercase       :							Tuple      		=		None
           _lowercase       :							Tuple      		=		pipe(**lowerCamelCase).images
           assert image.shape == (1, 32, 32, 3)

           _lowercase       :							str      		=		image[0, -3:, -3:, -1]
           _lowercase       :							List[str]      		=		np.array([0.5_0_0_4, 0.5_0_0_4, 0.4_9_9_4, 0.5_0_0_8, 0.4_9_7_6, 0.5_0_1_8, 0.4_9_9_0, 0.4_9_8_2, 0.4_9_8_7])

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




       def 			UpperCamelCase     (					self)       ->   str:




           """simple docstring"""

           _lowercase       :							Union[str, Any]      		=		'cpu'  # ensure determinism for the device-dependent torch.Generator
           _lowercase       :							Dict      		=		self.get_dummy_components(class_cond=lowerCamelCase)
           _lowercase       :							Dict      		=		ConsistencyModelPipeline(**lowerCamelCase)
           _lowercase       :							Optional[Any]      		=		pipe.to(lowerCamelCase)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							Tuple      		=		self.get_dummy_inputs(lowerCamelCase)
           _lowercase       :							Tuple      		=		1
           _lowercase       :							int      		=		None
           _lowercase       :							Tuple      		=		0
           _lowercase       :							Dict      		=		pipe(**lowerCamelCase).images
           assert image.shape == (1, 32, 32, 3)

           _lowercase       :							List[str]      		=		image[0, -3:, -3:, -1]
           _lowercase       :							Any      		=		np.array([0.5_0_0_4, 0.5_0_0_4, 0.4_9_9_4, 0.5_0_0_8, 0.4_9_7_6, 0.5_0_1_8, 0.4_9_9_0, 0.4_9_8_2, 0.4_9_8_7])

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






@slow
@require_torch_gpu
class _lowerCamelCase(						unittest.TestCase   ):
       def 			UpperCamelCase     (					self)       ->   Union[str, Any]:




           """simple docstring"""

           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()
       def 			UpperCamelCase     (					self,      lowerCamelCase=0,      lowerCamelCase=False,      lowerCamelCase="cpu",      lowerCamelCase=torch.floataa,      lowerCamelCase=(1, 3, 64, 64))       ->   Optional[Any]:




           """simple docstring"""

           _lowercase       :							List[Any]      		=		torch.manual_seed(lowerCamelCase)

           _lowercase       :							str      		=		{
               'num_inference_steps': None,
               'timesteps': [22, 0],
               'class_labels': 0,
               'generator': generator,
               'output_type': 'np',
           }

           if get_fixed_latents:
               _lowercase       :							Optional[Any]      		=		self.get_fixed_latents(seed=lowerCamelCase,      device=lowerCamelCase,      dtype=lowerCamelCase,      shape=lowerCamelCase)
               _lowercase       :							Tuple      		=		latents

           return inputs
       def 			UpperCamelCase     (					self,      lowerCamelCase=0,      lowerCamelCase="cpu",      lowerCamelCase=torch.floataa,      lowerCamelCase=(1, 3, 64, 64))       ->   Any:




           """simple docstring"""

           if type(lowerCamelCase) == str:
               _lowercase       :							Union[str, Any]      		=		torch.device(lowerCamelCase)
           _lowercase       :							int      		=		torch.Generator(device=lowerCamelCase).manual_seed(lowerCamelCase)
           _lowercase       :							List[str]      		=		randn_tensor(lowerCamelCase,      generator=lowerCamelCase,      device=lowerCamelCase,      dtype=lowerCamelCase)
           return latents
       def 			UpperCamelCase     (					self)       ->   str:




           """simple docstring"""

           _lowercase       :							Tuple      		=		UNetaDModel.from_pretrained('diffusers/consistency_models',      subfolder='diffusers_cd_imagenet64_l2')
           _lowercase       :							Optional[int]      		=		CMStochasticIterativeScheduler(
               num_train_timesteps=40,      sigma_min=0.0_0_2,      sigma_max=8_0.0,      )
           _lowercase       :							Any      		=		ConsistencyModelPipeline(unet=lowerCamelCase,      scheduler=lowerCamelCase)
           pipe.to(torch_device=lowerCamelCase)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							str      		=		self.get_inputs()
           _lowercase       :							Optional[int]      		=		pipe(**lowerCamelCase).images
           assert image.shape == (1, 64, 64, 3)

           _lowercase       :							str      		=		image[0, -3:, -3:, -1]

           _lowercase       :							Optional[Any]      		=		np.array([0.0_8_8_8, 0.0_8_8_1, 0.0_6_6_6, 0.0_4_7_9, 0.0_2_9_2, 0.0_1_9_5, 0.0_2_0_1, 0.0_1_6_3, 0.0_2_5_4])

           assert np.abs(image_slice.flatten() - expected_slice).max() < 2E-2
       def 			UpperCamelCase     (					self)       ->   str:




           """simple docstring"""

           _lowercase       :							List[str]      		=		UNetaDModel.from_pretrained('diffusers/consistency_models',      subfolder='diffusers_cd_imagenet64_l2')
           _lowercase       :							List[str]      		=		CMStochasticIterativeScheduler(
               num_train_timesteps=40,      sigma_min=0.0_0_2,      sigma_max=8_0.0,      )
           _lowercase       :							Union[str, Any]      		=		ConsistencyModelPipeline(unet=lowerCamelCase,      scheduler=lowerCamelCase)
           pipe.to(torch_device=lowerCamelCase)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							List[Any]      		=		self.get_inputs()
           _lowercase       :							int      		=		1
           _lowercase       :							Optional[Any]      		=		None
           _lowercase       :							str      		=		pipe(**lowerCamelCase).images
           assert image.shape == (1, 64, 64, 3)

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

           _lowercase       :							List[str]      		=		np.array([0.0_3_4_0, 0.0_1_5_2, 0.0_0_6_3, 0.0_2_6_7, 0.0_2_2_1, 0.0_1_0_7, 0.0_4_1_6, 0.0_1_8_6, 0.0_2_1_7])

           assert np.abs(image_slice.flatten() - expected_slice).max() < 2E-2
       @require_torch_a
       def 			UpperCamelCase     (					self)       ->   Union[str, Any]:




           """simple docstring"""

           _lowercase       :							str      		=		UNetaDModel.from_pretrained('diffusers/consistency_models',      subfolder='diffusers_cd_imagenet64_l2')
           _lowercase       :							Optional[int]      		=		CMStochasticIterativeScheduler(
               num_train_timesteps=40,      sigma_min=0.0_0_2,      sigma_max=8_0.0,      )
           _lowercase       :							Optional[int]      		=		ConsistencyModelPipeline(unet=lowerCamelCase,      scheduler=lowerCamelCase)
           pipe.to(torch_device=lowerCamelCase,      torch_dtype=torch.floataa)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							Any      		=		self.get_inputs(get_fixed_latents=lowerCamelCase,      device=lowerCamelCase)
           # Ensure usage of flash attention in torch 2.0
           with sdp_kernel(enable_flash=lowerCamelCase,      enable_math=lowerCamelCase,      enable_mem_efficient=lowerCamelCase):
               _lowercase       :							Dict      		=		pipe(**lowerCamelCase).images
           assert image.shape == (1, 64, 64, 3)

           _lowercase       :							Any      		=		image[0, -3:, -3:, -1]

           _lowercase       :							Union[str, Any]      		=		np.array([0.1_8_7_5, 0.1_4_2_8, 0.1_2_8_9, 0.2_1_5_1, 0.2_0_9_2, 0.1_4_7_7, 0.1_8_7_7, 0.1_6_4_1, 0.1_3_5_3])

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




       @require_torch_a
       def 			UpperCamelCase     (					self)       ->   Union[str, Any]:




           """simple docstring"""

           _lowercase       :							Dict      		=		UNetaDModel.from_pretrained('diffusers/consistency_models',      subfolder='diffusers_cd_imagenet64_l2')
           _lowercase       :							Optional[Any]      		=		CMStochasticIterativeScheduler(
               num_train_timesteps=40,      sigma_min=0.0_0_2,      sigma_max=8_0.0,      )
           _lowercase       :							int      		=		ConsistencyModelPipeline(unet=lowerCamelCase,      scheduler=lowerCamelCase)
           pipe.to(torch_device=lowerCamelCase,      torch_dtype=torch.floataa)
           pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							List[Any]      		=		self.get_inputs(get_fixed_latents=lowerCamelCase,      device=lowerCamelCase)
           _lowercase       :							int      		=		1
           _lowercase       :							str      		=		None
           # Ensure usage of flash attention in torch 2.0
           with sdp_kernel(enable_flash=lowerCamelCase,      enable_math=lowerCamelCase,      enable_mem_efficient=lowerCamelCase):
               _lowercase       :							Union[str, Any]      		=		pipe(**lowerCamelCase).images
           assert image.shape == (1, 64, 64, 3)

           _lowercase       :							Any      		=		image[0, -3:, -3:, -1]

           _lowercase       :							int      		=		np.array([0.1_6_6_3, 0.1_9_4_8, 0.2_2_7_5, 0.1_6_8_0, 0.1_2_0_4, 0.1_2_4_5, 0.1_8_5_8, 0.1_3_3_8, 0.2_0_9_5])

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


