

import argparse
import dataclasses
import json
import logging
import os
import shutil
from typing import List, Optional

import datasets
from accelerate import Accelerator
from datasets import load_dataset
from finetuning import finetune
from tqdm.auto import tqdm

import transformers
from transformers import AutoConfig, set_seed
from transformers.trainer_utils import IntervalStrategy


_UpperCamelCase 			=		logging.getLogger(__name__)

_UpperCamelCase 			=		"pytorch_model.bin"







@dataclasses.dataclass
class   __lowercase      :
 _UpperCamelCase			  =  dataclasses.field(
     metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models."""} )
 _UpperCamelCase			  =  dataclasses.field(
     default=_UpperCAmelCase		,   metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co."""}		,   )







@dataclasses.dataclass
class   __lowercase      :
 _UpperCamelCase			  =  dataclasses.field(metadata={"""help""": """A csv or a json file containing the training data."""} )
 _UpperCamelCase			  =  dataclasses.field(metadata={"""help""": """A csv or a json file containing the data to predict on."""} )
 _UpperCamelCase			  =  dataclasses.field(
     default=_UpperCAmelCase		,   metadata={"""help""": """A csv or a json file containing the validation data."""} )
 _UpperCamelCase			  =  dataclasses.field(
     default=_UpperCAmelCase		,   metadata={"""help""": """The name of the task to train on."""}		,   )
 _UpperCamelCase			  =  dataclasses.field(
     default=_UpperCAmelCase		,   metadata={"""help""": """The list of labels for the task."""} )







@dataclasses.dataclass
class   __lowercase      :
 _UpperCamelCase			  =  dataclasses.field(
     metadata={"""help""": """The output directory where the model predictions and checkpoints will be written."""} )
 _UpperCamelCase			  =  dataclasses.field(
     default="""accuracy"""		,   metadata={"""help""": """The evaluation metric used for the task."""} )
 _UpperCamelCase			  =  dataclasses.field(
     default="""no"""		,   metadata={
         """help""": """The evaluation strategy to adopt during training. Possible values are: [\"no\", \"step\", \"epoch]"""
     }		,   )
 _UpperCamelCase			  =  dataclasses.field(
     default=10		,   metadata={"""help""": """Number of evaluation calls with no improvement after which training will be stopped."""}		,   )
 _UpperCamelCase			  =  dataclasses.field(
     default=0.0		,   metadata={
         """help""": """How much the specified evaluation metric must improve to satisfy early stopping conditions."""
     }		,   )
 _UpperCamelCase			  =  dataclasses.field(
     default=_UpperCAmelCase		,   metadata={"""help""": """Whether to filter the pseudo-labeled data based on the confidence score."""}		,   )
 _UpperCamelCase			  =  dataclasses.field(
     default=_UpperCAmelCase		,   metadata={"""help""": """Whether to filter the pseudo-labeled data based on the validation performance."""}		,   )
 _UpperCamelCase			  =  dataclasses.field(
     default=_UpperCAmelCase		,   metadata={"""help""": """Whether to fine-tune on labeled data after pseudo training."""}		,   )
 _UpperCamelCase			  =  dataclasses.field(
     default=0.0		,   metadata={"""help""": """Confidence threshold for pseudo-labeled data filtering."""}		,   )
 _UpperCamelCase			  =  dataclasses.field(
     default=100		,   metadata={"""help""": """Number of evaluation calls with no improvement after which training will be stopped."""}		,   )
 _UpperCamelCase			  =  dataclasses.field(
     default=_UpperCAmelCase		,   metadata={"""help""": """Random seed for initialization."""}		,   )







def 		_lowercase (     lowercase__   ,	lowercase__   ,	lowercase__   ,	lowercase__   ,	lowercase__   ,	lowercase__						):
      __lowerCAmelCase					:						Dict					=					datasets.concatenate_datasets([infer_input, infer_output]   ,	axis=1						)

      if args.do_filter_by_confidence:
            __lowerCAmelCase					:						Tuple					=					dataset.filter(lambda lowercase__						: example["probability"] > args.confidence_threshold						)

      if args.do_filter_by_val_performance:
            assert eval_result >= 0.0 and eval_result <= 1.0
            __lowerCAmelCase					:						Optional[Any]					=					int(eval_result * len(lowercase__						)						)
            print(lowercase__						)
            __lowerCAmelCase					:						int					=					dataset.sort('''probability'''   ,	reverse=lowercase__						)
            __lowerCAmelCase					:						Union[str, Any]					=					dataset.select(range(lowercase__						)						)

      __lowerCAmelCase					:						int					=					dataset.remove_columns(['''label''', '''probability''']						)
      __lowerCAmelCase					:						Any					=					dataset.rename_column('''prediction'''   ,	'''label'''						)
      __lowerCAmelCase					:						Optional[Any]					=					dataset.map(lambda lowercase__						: {"label": idalabel[example["label"]]}						)
      __lowerCAmelCase					:						Any					=					dataset.shuffle(seed=args.seed						)

      __lowerCAmelCase					:						int					=					os.path.join(lowercase__   ,	f"""train_pseudo.{args.data_file_extension}"""						)
      if args.data_file_extension == "csv":
            dataset.to_csv(lowercase__   ,	index=lowercase__						)
      else:
            dataset.to_json(lowercase__						)







def 		_lowercase (     lowercase__   ,	lowercase__   ,	lowercase__   ,	lowercase__   ,	**lowercase__						):
      __lowerCAmelCase					:						Dict					=					Accelerator()
      # Make one log on every process with the configuration for debugging.
      logging.basicConfig(
          format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s'''   ,	datefmt='''%m/%d/%Y %H:%M:%S'''   ,	level=logging.INFO   ,	)
      logger.info(accelerator.state						)

      # Setup logging, we only want one process per machine to log things on the
      # screen. accelerator.is_local_main_process is only True for one process per
      # machine.
      logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR						)

      if accelerator.is_local_main_process:
            datasets.utils.logging.set_verbosity_warning()
            transformers.utils.logging.set_verbosity_info()
      else:
            datasets.utils.logging.set_verbosity_error()
            transformers.utils.logging.set_verbosity_error()

      __lowerCAmelCase					:						List[str]					=					STModelArguments(model_name_or_path=lowercase__						)
      __lowerCAmelCase					:						List[str]					=					STDataArguments(train_file=lowercase__   ,	infer_file=lowercase__						)
      __lowerCAmelCase					:						Any					=					STTrainingArguments(output_dir=lowercase__						)
      __lowerCAmelCase					:						int					=					argparse.Namespace()

      for arg_class in (model_args, data_args, training_args):
            for key, value in vars(lowercase__						).items():
                  setattr(lowercase__   ,	lowercase__   ,	lowercase__						)

      for key, value in kwargs.items():
            if hasattr(lowercase__   ,	lowercase__						):
                  setattr(lowercase__   ,	lowercase__   ,	lowercase__						)

    # Sanity checks
      __lowerCAmelCase					:						List[Any]					=					{}
      __lowerCAmelCase					:						Any					=					None

      # You need to provide the training data and the data to predict on
      assert args.train_file is not None
      assert args.infer_file is not None
      __lowerCAmelCase					:						int					=					args.train_file
      __lowerCAmelCase					:						str					=					args.infer_file

      if args.evaluation_strategy != IntervalStrategy.NO.value:
            assert args.eval_file is not None
            __lowerCAmelCase					:						str					=					args.eval_file

      for key in data_files:
            __lowerCAmelCase					:						Optional[int]					=					data_files[key].split('''.'''						)[-1]
            assert extension in ["csv", "json"], f"""`{key}_file` should be a csv or a json file."""
            if args.data_file_extension is None:
                  __lowerCAmelCase					:						List[str]					=					extension
            else:
                  assert extension == args.data_file_extension, f"""`{key}_file` should be a {args.data_file_extension} file`."""

      assert (
          args.eval_metric in datasets.list_metrics()
      ), f"""{args.eval_metric} not in the list of supported metrics {datasets.list_metrics()}."""

      # If passed along, set the training seed now.
      if args.seed is not None:
            set_seed(args.seed						)

      logger.info('''Creating the initial data directory for self-training...'''						)
      __lowerCAmelCase					:						Optional[Any]					=					f"""{args.output_dir}/self-train_iter-{{}}""".format
      __lowerCAmelCase					:						str					=					data_dir_format(0						)

      if accelerator.is_main_process:
            if args.output_dir is not None:
                  os.makedirs(args.output_dir   ,	exist_ok=lowercase__						)
                  os.makedirs(lowercase__   ,	exist_ok=lowercase__						)
      accelerator.wait_for_everyone()

      __lowerCAmelCase					:						int					=					None
      __lowerCAmelCase					:						Optional[Any]					=					None
      __lowerCAmelCase					:						Optional[Any]					=					0
      __lowerCAmelCase					:						Tuple					=					False
      # Show the progress bar
      __lowerCAmelCase					:						Dict					=					tqdm(range(args.max_selftrain_iterations						)   ,	disable=not accelerator.is_local_main_process						)

      # Self-train
      for iteration in range(0   ,	int(args.max_selftrain_iterations						)						):
            __lowerCAmelCase					:						List[str]					=					data_dir_format(lowercase__						)
            assert os.path.exists(lowercase__						)

            # Stage 1: initial fine-tuning for iteration = 0 or pseudo-training for
            # iteration > 0
            __lowerCAmelCase					:						int					=					os.path.join(lowercase__   ,	'''stage-1'''						)
            __lowerCAmelCase					:						Optional[Any]					=					{
                '''accelerator''': accelerator,
                '''model_name_or_path''': args.model_name_or_path,
                '''cache_dir''': args.cache_dir,
                '''do_train''': True,
                '''train_file''': data_files['''train'''] if iteration == 0 else data_files['''train_pseudo'''],
                '''do_eval''': True if args.eval_file is not None else False,
                '''eval_file''': data_files['''eval'''],
                '''do_predict''': True,
                '''infer_file''': data_files['''infer'''],
                '''task_name''': args.task_name,
                '''label_list''': args.label_list,
                '''output_dir''': current_output_dir,
                '''eval_metric''': args.eval_metric,
                '''evaluation_strategy''': args.evaluation_strategy,
                '''early_stopping_patience''': args.early_stopping_patience,
                '''early_stopping_threshold''': args.early_stopping_threshold,
                '''seed''': args.seed,
            }
            # Add additional training arguments
            for key, value in kwargs.items():
                  if key not in arguments_dict and not hasattr(lowercase__   ,	lowercase__						):
                        arguments_dict.update({key: value}						)

            __lowerCAmelCase					:						List[Any]					=					os.path.join(lowercase__   ,	'''best-checkpoint'''   ,	lowercase__						)
            if os.path.exists(lowercase__						):
                  logger.info(
                      '''Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 1.'''   ,	lowercase__   ,	lowercase__   ,	)
            else:
                  logger.info('''***** Running self-training: iteration: %d, stage: 1 *****'''   ,	lowercase__						)
                  finetune(**lowercase__						)
                  accelerator.wait_for_everyone()
                  assert os.path.exists(lowercase__						)
                  logger.info('''Self-training job completed: iteration: %d, stage: 1.'''   ,	lowercase__						)

            if iteration > 0 and args.finetune_on_labeled_data:
                  # Stage 2 (optional): fine-tuning on the original labeled data
                  __lowerCAmelCase					:						str					=					os.path.join(lowercase__   ,	'''best-checkpoint'''						)
                  __lowerCAmelCase					:						Union[str, Any]					=					os.path.join(lowercase__   ,	'''stage-2'''						)
                  # Update arguments_dict
                  __lowerCAmelCase					:						int					=					model_path
                  __lowerCAmelCase					:						str					=					data_files['''train''']
                  __lowerCAmelCase					:						List[str]					=					current_output_dir

                  __lowerCAmelCase					:						Optional[int]					=					os.path.join(lowercase__   ,	'''best-checkpoint'''   ,	lowercase__						)
                  if os.path.exists(lowercase__						):
                        logger.info(
                            '''Found existing model checkpoint at %s. Skipping self-training: iteration: %d, stage: 2.'''   ,	lowercase__   ,	lowercase__   ,	)
                  else:
                        logger.info('''***** Running self-training: iteration: %d, stage: 2 *****'''   ,	lowercase__						)
                        finetune(**lowercase__						)
                        accelerator.wait_for_everyone()
                        assert os.path.exists(lowercase__						)
                        logger.info('''Self-training job completed: iteration: %d, stage: 2.'''   ,	lowercase__						)

            __lowerCAmelCase					:						Union[str, Any]					=					iteration
            __lowerCAmelCase					:						Tuple					=					data_dir_format(iteration + 1						)

            __lowerCAmelCase					:						str					=					AutoConfig.from_pretrained(os.path.join(lowercase__   ,	'''best-checkpoint'''						)						)
            __lowerCAmelCase					:						List[Any]					=					config.idalabel
            __lowerCAmelCase					:						Union[str, Any]					=					os.path.join(lowercase__   ,	'''eval_results_best-checkpoint.json'''						)
            __lowerCAmelCase					:						Tuple					=					os.path.join(lowercase__   ,	'''test_results_best-checkpoint.json'''						)
            assert os.path.exists(lowercase__						)

            with open(lowercase__   ,	'''r'''						) as f:
                  __lowerCAmelCase					:						List[str]					=					float(json.load(lowercase__						)[args.eval_metric]						)
            __lowerCAmelCase					:						Dict					=					os.path.join(lowercase__   ,	'''infer_output_best-checkpoint.csv'''						)
            assert os.path.exists(lowercase__						)
            # Loading the dataset from local csv or json files.
            __lowerCAmelCase					:						Any					=					load_dataset(args.data_file_extension   ,	data_files={'''data''': data_files['''infer''']}						)['''data''']
            __lowerCAmelCase					:						Union[str, Any]					=					load_dataset('''csv'''   ,	data_files={'''data''': infer_output_file}						)['''data''']

            if accelerator.is_main_process:
                  os.makedirs(lowercase__   ,	exist_ok=lowercase__						)
                  shutil.copy(lowercase__   ,	os.path.join(lowercase__   ,	f"""eval_results_iter-{iteration}.json"""						)						)
                  if os.path.exists(lowercase__						):
                        shutil.copy(lowercase__   ,	os.path.join(lowercase__   ,	f"""test_results_iter-{iteration}.json"""						)						)
                  create_pseudo_labeled_data(lowercase__   ,	lowercase__   ,	lowercase__   ,	lowercase__   ,	lowercase__   ,	lowercase__						)
            accelerator.wait_for_everyone()

            __lowerCAmelCase					:						str					=					os.path.join(lowercase__   ,	f"""train_pseudo.{args.data_file_extension}"""						)

            if args.evaluation_strategy != IntervalStrategy.NO.value:
                  __lowerCAmelCase					:						int					=					eval_result

                  if best_iteration is None:
                        __lowerCAmelCase					:						Optional[Any]					=					new_iteration
                        __lowerCAmelCase					:						Optional[int]					=					new_eval_result
                  else:
                        if new_eval_result - best_eval_result > args.early_stopping_threshold:
                              __lowerCAmelCase					:						str					=					new_iteration
                              __lowerCAmelCase					:						str					=					new_eval_result
                              __lowerCAmelCase					:						int					=					0
                        else:
                              if new_eval_result == best_eval_result:
                                    __lowerCAmelCase					:						List[str]					=					new_iteration
                                    __lowerCAmelCase					:						Tuple					=					new_eval_result
                              early_stopping_patience_counter += 1

                        if early_stopping_patience_counter >= args.early_stopping_patience:
                              __lowerCAmelCase					:						List[str]					=					True

            progress_bar.update(1						)

            if should_training_stop:
                  break

      if best_iteration is not None:
            # Save the best iteration
            logger.info('''Best iteration: %d'''   ,	lowercase__						)
            logger.info('''Best evaluation result: %s = %f'''   ,	args.eval_metric   ,	lowercase__						)
            accelerator.wait_for_everyone()
            if accelerator.is_main_process:
                  shutil.copy(
                      os.path.join(lowercase__   ,	f"""eval_results_iter-{iteration}.json"""						)   ,	os.path.join(lowercase__   ,	'''eval_results_best-iteration.json'''						)   ,	)
      else:
            # Assume that the last iteration is the best
            logger.info('''Best iteration: %d'''   ,	args.max_selftrain_iterations - 1						)
            logger.info('''Best evaluation result: %s = %f'''   ,	args.eval_metric   ,	lowercase__						)
            accelerator.wait_for_everyone()
            if accelerator.is_main_process:
                  shutil.copy(
                      os.path.join(lowercase__   ,	f"""eval_results_iter-{args.max_selftrain_iterations - 1}.json"""						)   ,	os.path.join(lowercase__   ,	'''eval_results_best-iteration.json'''						)   ,	)





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   __lowercase      (_UpperCAmelCase		,   unittest.TestCase ):
 _UpperCamelCase			  =  ConsistencyModelPipeline
 _UpperCamelCase			  =  UNCONDITIONAL_IMAGE_GENERATION_PARAMS
 _UpperCamelCase			  =  UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS

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



       '''simple docstring'''





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



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					UNetaDModel.from_pretrained(
           '''diffusers/consistency-models-test'''      ,							subfolder='''test_unet_class_cond'''      ,							)
       return unet
 def 	UpperCamelCase__  (							self      ,							A_=False					)					->Dict:



       '''simple docstring'''





       if class_cond:
             __lowerCAmelCase					:						List[str]					=					self.dummy_cond_unet
       else:
             __lowerCAmelCase					:						Optional[Any]					=					self.dummy_uncond_unet

       # Default to CM multistep sampler
       __lowerCAmelCase					:						List[str]					=					CMStochasticIterativeScheduler(
           num_train_timesteps=40      ,							sigma_min=0.002      ,							sigma_max=80.0      ,							)

       __lowerCAmelCase					:						Dict					=					{
           '''unet''': unet,
           '''scheduler''': scheduler,
       }

       return components
 def 	UpperCamelCase__  (							self      ,							A_      ,							A_=0					)					->Tuple:



       '''simple docstring'''





       if str(A_					).startswith('''mps'''					):
             __lowerCAmelCase					:						str					=					torch.manual_seed(A_					)
       else:
             __lowerCAmelCase					:						Dict					=					torch.Generator(device=A_					).manual_seed(A_					)

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

       return inputs
 def 	UpperCamelCase__  (							self					)					->Tuple:



       '''simple docstring'''





       __lowerCAmelCase					:						Union[str, Any]					=					'''cpu'''  # ensure determinism for the device-dependent torch.Generator
       __lowerCAmelCase					:						Tuple					=					self.get_dummy_components()
       __lowerCAmelCase					:						Union[str, Any]					=					ConsistencyModelPipeline(**A_					)
       __lowerCAmelCase					:						List[str]					=					pipe.to(A_					)
       pipe.set_progress_bar_config(disable=A_					)

       __lowerCAmelCase					:						Any					=					self.get_dummy_inputs(A_					)
       __lowerCAmelCase					:						int					=					pipe(**A_					).images
       assert image.shape == (1, 32, 32, 3)

       __lowerCAmelCase					:						Tuple					=					image[0, -3:, -3:, -1]
       __lowerCAmelCase					:						str					=					np.array([0.3_572, 0.6_273, 0.4_031, 0.3_961, 0.4_321, 0.5_730, 0.5_266, 0.4_780, 0.5_004]					)

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



       '''simple docstring'''





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

       __lowerCAmelCase					:						Optional[Any]					=					self.get_dummy_inputs(A_					)
       __lowerCAmelCase					:						Optional[int]					=					0
       __lowerCAmelCase					:						int					=					pipe(**A_					).images
       assert image.shape == (1, 32, 32, 3)

       __lowerCAmelCase					:						Optional[int]					=					image[0, -3:, -3:, -1]
       __lowerCAmelCase					:						List[str]					=					np.array([0.3_572, 0.6_273, 0.4_031, 0.3_961, 0.4_321, 0.5_730, 0.5_266, 0.4_780, 0.5_004]					)

       assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-3
 def 	UpperCamelCase__  (							self					)					->Optional[Any]:



       '''simple docstring'''





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

       __lowerCAmelCase					:						Tuple					=					self.get_dummy_inputs(A_					)
       __lowerCAmelCase					:						Any					=					1
       __lowerCAmelCase					:						List[Any]					=					None
       __lowerCAmelCase					:						Dict					=					pipe(**A_					).images
       assert image.shape == (1, 32, 32, 3)

       __lowerCAmelCase					:						Any					=					image[0, -3:, -3:, -1]
       __lowerCAmelCase					:						List[Any]					=					np.array([0.5_004, 0.5_004, 0.4_994, 0.5_008, 0.4_976, 0.5_018, 0.4_990, 0.4_982, 0.4_987]					)

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





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



       '''simple docstring'''





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

       __lowerCAmelCase					:						Any					=					self.get_dummy_inputs(A_					)
       __lowerCAmelCase					:						List[str]					=					1
       __lowerCAmelCase					:						Dict					=					None
       __lowerCAmelCase					:						Tuple					=					0
       __lowerCAmelCase					:						Dict					=					pipe(**A_					).images
       assert image.shape == (1, 32, 32, 3)

       __lowerCAmelCase					:						List[Any]					=					image[0, -3:, -3:, -1]
       __lowerCAmelCase					:						Any					=					np.array([0.5_004, 0.5_004, 0.4_994, 0.5_008, 0.4_976, 0.5_018, 0.4_990, 0.4_982, 0.4_987]					)

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







@slow
@require_torch_gpu
class   __lowercase      (unittest.TestCase ):
 def 	UpperCamelCase__  (							self					)					->int:



       '''simple docstring'''





       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()
 def 	UpperCamelCase__  (							self      ,							A_=0      ,							A_=False      ,							A_="cpu"      ,							A_=torch.floataa      ,							A_=(1, 3, 64, 64)					)					->str:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					torch.manual_seed(A_					)

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

       if get_fixed_latents:
             __lowerCAmelCase					:						List[str]					=					self.get_fixed_latents(seed=A_      ,							device=A_      ,							dtype=A_      ,							shape=A_					)
             __lowerCAmelCase					:						Union[str, Any]					=					latents

       return inputs
 def 	UpperCamelCase__  (							self      ,							A_=0      ,							A_="cpu"      ,							A_=torch.floataa      ,							A_=(1, 3, 64, 64)					)					->Optional[int]:



       '''simple docstring'''





       if type(A_					) == str:
             __lowerCAmelCase					:						int					=					torch.device(A_					)
       __lowerCAmelCase					:						Optional[Any]					=					torch.Generator(device=A_					).manual_seed(A_					)
       __lowerCAmelCase					:						Union[str, Any]					=					randn_tensor(A_      ,							generator=A_      ,							device=A_      ,							dtype=A_					)
       return latents
 def 	UpperCamelCase__  (							self					)					->Optional[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						int					=					UNetaDModel.from_pretrained('''diffusers/consistency_models'''      ,							subfolder='''diffusers_cd_imagenet64_l2'''					)
       __lowerCAmelCase					:						List[Any]					=					CMStochasticIterativeScheduler(
           num_train_timesteps=40      ,							sigma_min=0.002      ,							sigma_max=80.0      ,							)
       __lowerCAmelCase					:						List[str]					=					ConsistencyModelPipeline(unet=A_      ,							scheduler=A_					)
       pipe.to(torch_device=A_					)
       pipe.set_progress_bar_config(disable=A_					)

       __lowerCAmelCase					:						str					=					self.get_inputs()
       __lowerCAmelCase					:						Any					=					pipe(**A_					).images
       assert image.shape == (1, 64, 64, 3)

       __lowerCAmelCase					:						Dict					=					image[0, -3:, -3:, -1]

       __lowerCAmelCase					:						Optional[int]					=					np.array([0.0_888, 0.0_881, 0.0_666, 0.0_479, 0.0_292, 0.0_195, 0.0_201, 0.0_163, 0.0_254]					)

       assert np.abs(image_slice.flatten() - expected_slice					).max() < 2e-2
 def 	UpperCamelCase__  (							self					)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						int					=					UNetaDModel.from_pretrained('''diffusers/consistency_models'''      ,							subfolder='''diffusers_cd_imagenet64_l2'''					)
       __lowerCAmelCase					:						Optional[Any]					=					CMStochasticIterativeScheduler(
           num_train_timesteps=40      ,							sigma_min=0.002      ,							sigma_max=80.0      ,							)
       __lowerCAmelCase					:						List[str]					=					ConsistencyModelPipeline(unet=A_      ,							scheduler=A_					)
       pipe.to(torch_device=A_					)
       pipe.set_progress_bar_config(disable=A_					)

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

       __lowerCAmelCase					:						Optional[int]					=					image[0, -3:, -3:, -1]

       __lowerCAmelCase					:						List[Any]					=					np.array([0.0_340, 0.0_152, 0.0_063, 0.0_267, 0.0_221, 0.0_107, 0.0_416, 0.0_186, 0.0_217]					)

       assert np.abs(image_slice.flatten() - expected_slice					).max() < 2e-2
 @require_torch_a
 def 	UpperCamelCase__  (							self					)					->Optional[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					UNetaDModel.from_pretrained('''diffusers/consistency_models'''      ,							subfolder='''diffusers_cd_imagenet64_l2'''					)
       __lowerCAmelCase					:						List[str]					=					CMStochasticIterativeScheduler(
           num_train_timesteps=40      ,							sigma_min=0.002      ,							sigma_max=80.0      ,							)
       __lowerCAmelCase					:						Union[str, Any]					=					ConsistencyModelPipeline(unet=A_      ,							scheduler=A_					)
       pipe.to(torch_device=A_      ,							torch_dtype=torch.floataa					)
       pipe.set_progress_bar_config(disable=A_					)

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

       __lowerCAmelCase					:						Dict					=					image[0, -3:, -3:, -1]

       __lowerCAmelCase					:						Optional[int]					=					np.array([0.1_875, 0.1_428, 0.1_289, 0.2_151, 0.2_092, 0.1_477, 0.1_877, 0.1_641, 0.1_353]					)

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





 @require_torch_a
 def 	UpperCamelCase__  (							self					)					->List[str]:



       '''simple docstring'''





       __lowerCAmelCase					:						Optional[Any]					=					UNetaDModel.from_pretrained('''diffusers/consistency_models'''      ,							subfolder='''diffusers_cd_imagenet64_l2'''					)
       __lowerCAmelCase					:						List[Any]					=					CMStochasticIterativeScheduler(
           num_train_timesteps=40      ,							sigma_min=0.002      ,							sigma_max=80.0      ,							)
       __lowerCAmelCase					:						Union[str, Any]					=					ConsistencyModelPipeline(unet=A_      ,							scheduler=A_					)
       pipe.to(torch_device=A_      ,							torch_dtype=torch.floataa					)
       pipe.set_progress_bar_config(disable=A_					)

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

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

       __lowerCAmelCase					:						Any					=					np.array([0.1_663, 0.1_948, 0.2_275, 0.1_680, 0.1_204, 0.1_245, 0.1_858, 0.1_338, 0.2_095]					)

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


