
"""simple docstring"""





import logging
import os
from dataclasses import dataclass, field
from typing import Dict, Optional

import numpy as np
from utils_multiple_choice import MultipleChoiceDataset, Split, processors

import transformers
from transformers import (
    AutoConfig,
    AutoModelForMultipleChoice,
    AutoTokenizer,
    DataCollatorWithPadding,
    EvalPrediction,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import is_main_process


__magic_name__       =			logging.getLogger(__name__)



def 						_lowerCAmelCase				(				UpperCamelCase_  ,				UpperCamelCase_				):
		return (preds == labels).mean()
@dataclass
class 						SCREAMING_SNAKE_CASE_ :




		"""simple docstring"""





		__lowercase							:  str						   =		field(
		    metadata={'''help''': '''Path to pretrained model or model identifier from huggingface.co/models'''} )
		__lowercase							:  Optional[str]						   =		field(
		    default=__a     ,				metadata={'''help''': '''Pretrained config name or path if not the same as model_name'''} )
		__lowercase							:  Optional[str]						   =		field(
		    default=__a     ,				metadata={'''help''': '''Pretrained tokenizer name or path if not the same as model_name'''} )
		__lowercase							:  Optional[str]						   =		field(
		    default=__a     ,				metadata={'''help''': '''Where do you want to store the pretrained models downloaded from huggingface.co'''}     ,				)



@dataclass
class 						SCREAMING_SNAKE_CASE_ :




		"""simple docstring"""





		__lowercase							:  str						   =		field(metadata={'''help''': '''The name of the task to train on: ''' + ''', '''.join(processors.keys() )} )
		__lowercase							:  str						   =		field(metadata={'''help''': '''Should contain the data files for the task.'''} )
		__lowercase							:  int						   =		field(
		    default=128     ,				metadata={
		        '''help''': (
		            '''The maximum total input sequence length after tokenization. Sequences longer '''
		            '''than this will be truncated, sequences shorter will be padded.'''
		        )
		    }     ,				)
		__lowercase							:  bool						   =		field(
		    default=__a     ,				metadata={'''help''': '''Overwrite the cached training and evaluation sets'''} )



def 						_lowerCAmelCase				(				):
		# See all possible arguments in src/transformers/training_args.py
		# or by passing the --help flag to this script.
		# We now keep distinct sets of args, for a cleaner separation of concerns.

		__SCREAMING_SNAKE_CASE            = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)				)
		__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE            = parser.parse_args_into_dataclasses()

		if (
		    os.path.exists(training_args.output_dir				)
		    and os.listdir(training_args.output_dir				)
		    and training_args.do_train
		    and not training_args.overwrite_output_dir
		):
				raise ValueError(
				    f"Output directory ({training_args.output_dir}) already exists and is not empty. Use"
				    """ --overwrite_output_dir to overcome."""				)

		# Setup logging
		logging.basicConfig(
		    format="""%(asctime)s - %(levelname)s - %(name)s - %(message)s"""  ,				datefmt="""%m/%d/%Y %H:%M:%S"""  ,				level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN  ,				)
		logger.warning(
		    """Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s"""  ,				training_args.local_rank  ,				training_args.device  ,				training_args.n_gpu  ,				bool(training_args.local_rank != -1				)  ,				training_args.fpaa  ,				)
		# Set the verbosity to info of the Transformers logger (on main process only):
		if is_main_process(training_args.local_rank				):
				transformers.utils.logging.set_verbosity_info()
				transformers.utils.logging.enable_default_handler()
				transformers.utils.logging.enable_explicit_format()
		logger.info("""Training/evaluation parameters %s"""  ,				UpperCamelCase_				)

		# Set seed
		set_seed(training_args.seed				)

		try:
				__SCREAMING_SNAKE_CASE            = processors[data_args.task_name]()
				__SCREAMING_SNAKE_CASE            = processor.get_labels()
				__SCREAMING_SNAKE_CASE            = len(UpperCamelCase_				)
		except KeyError:
				raise ValueError("""Task not found: %s""" % (data_args.task_name)				)

		# Load pretrained model and tokenizer
		#
		# Distributed training:
		# The .from_pretrained methods guarantee that only one local process can concurrently
		# download model & vocab.

		__SCREAMING_SNAKE_CASE            = AutoConfig.from_pretrained(
		    model_args.config_name if model_args.config_name else model_args.model_name_or_path  ,				num_labels=UpperCamelCase_  ,				finetuning_task=data_args.task_name  ,				cache_dir=model_args.cache_dir  ,				)
		__SCREAMING_SNAKE_CASE            = AutoTokenizer.from_pretrained(
		    model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path  ,				cache_dir=model_args.cache_dir  ,				)
		__SCREAMING_SNAKE_CASE            = AutoModelForMultipleChoice.from_pretrained(
		    model_args.model_name_or_path  ,				from_tf=bool(""".ckpt""" in model_args.model_name_or_path				)  ,				config=UpperCamelCase_  ,				cache_dir=model_args.cache_dir  ,				)

		# Get datasets
		__SCREAMING_SNAKE_CASE            = (
		    MultipleChoiceDataset(
		        data_dir=data_args.data_dir  ,				tokenizer=UpperCamelCase_  ,				task=data_args.task_name  ,				max_seq_length=data_args.max_seq_length  ,				overwrite_cache=data_args.overwrite_cache  ,				mode=Split.train  ,				)
		    if training_args.do_train
		    else None
		)
		__SCREAMING_SNAKE_CASE            = (
		    MultipleChoiceDataset(
		        data_dir=data_args.data_dir  ,				tokenizer=UpperCamelCase_  ,				task=data_args.task_name  ,				max_seq_length=data_args.max_seq_length  ,				overwrite_cache=data_args.overwrite_cache  ,				mode=Split.dev  ,				)
		    if training_args.do_eval
		    else None
		)

		def compute_metrics(UpperCamelCase_				) -> Dict:
				__SCREAMING_SNAKE_CASE            = np.argmax(p.predictions  ,				axis=1				)
				return {"acc": simple_accuracy(UpperCamelCase_  ,				p.label_ids				)}

		# Data collator
		__SCREAMING_SNAKE_CASE            = DataCollatorWithPadding(UpperCamelCase_  ,				pad_to_multiple_of=8				) if training_args.fpaa else None

		# Initialize our Trainer
		__SCREAMING_SNAKE_CASE            = Trainer(
		    model=UpperCamelCase_  ,				args=UpperCamelCase_  ,				train_dataset=UpperCamelCase_  ,				eval_dataset=UpperCamelCase_  ,				compute_metrics=UpperCamelCase_  ,				data_collator=UpperCamelCase_  ,				)

		# Training
		if training_args.do_train:
				trainer.train(
				    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path				) else None				)
				trainer.save_model()
				# For convenience, we also re-save the tokenizer to the same directory,
				# so that you can share your model easily on huggingface.co/models =)
				if trainer.is_world_master():
						tokenizer.save_pretrained(training_args.output_dir				)

    # Evaluation
		__SCREAMING_SNAKE_CASE            = {}
		if training_args.do_eval:
				logger.info("""*** Evaluate ***"""				)

				__SCREAMING_SNAKE_CASE            = trainer.evaluate()

				__SCREAMING_SNAKE_CASE            = os.path.join(training_args.output_dir  ,				"""eval_results.txt"""				)
				if trainer.is_world_master():
						with open(UpperCamelCase_  ,				"""w"""				) as writer:
								logger.info("""***** Eval results *****"""				)
								for key, value in result.items():
										logger.info("""  %s = %s"""  ,				UpperCamelCase_  ,				UpperCamelCase_				)
										writer.write("""%s = %s\n""" % (key, value)				)

								results.update(UpperCamelCase_				)

		return results



def 						_lowerCAmelCase				(				UpperCamelCase_				):
		# For xla_spawn (TPUs)
		main()


if __name__ == "__main__":
							main()








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


