




'''simple docstring'''



import math
import time

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
 import torch_xla.core.xla_model as xm
 import torch_xla.debug.metrics as met







class 		lowercase_   (			lowercase__       ):


   """simple docstring"""

   def __init__(   self :     Union[str, Any]  ,				*__lowerCamelCase :     Optional[Any]  ,				__lowerCamelCase :     List[str]=None  ,				__lowerCamelCase :     Tuple=None  ,				**__lowerCamelCase :     int   ):

      """simple docstring"""
      super().__init__(*_a  ,				**_a   )
      _SCREAMING_SNAKE_CASE	=				eval_examples
      _SCREAMING_SNAKE_CASE	=				post_process_function



   def 							lowerCAmelCase_      (   self :     Tuple  ,				__lowerCamelCase :     Optional[int]=None  ,				__lowerCamelCase :     Optional[Any]=None  ,				__lowerCamelCase :     int=None  ,				__lowerCamelCase :     str = "eval"   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.eval_dataset if eval_dataset is None else eval_dataset
      _SCREAMING_SNAKE_CASE	=				self.get_eval_dataloader(_a   )
      _SCREAMING_SNAKE_CASE	=				self.eval_examples if eval_examples is None else eval_examples

      # Temporarily disable metric computation, we will do it in the loop here.
      _SCREAMING_SNAKE_CASE	=				self.compute_metrics
      _SCREAMING_SNAKE_CASE	=				None
      _SCREAMING_SNAKE_CASE	=				self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
      _SCREAMING_SNAKE_CASE	=				time.time()
      try:
         _SCREAMING_SNAKE_CASE	=				eval_loop(
             _a  ,				description="Evaluation"  ,				prediction_loss_only=True if compute_metrics is None else None  ,				ignore_keys=_a  ,				metric_key_prefix=_a  ,				)
      finally:
         _SCREAMING_SNAKE_CASE	=				compute_metrics
      _SCREAMING_SNAKE_CASE	=				self.args.eval_batch_size * self.args.world_size
      if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics:
         start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""]
      output.metrics.update(
          speed_metrics(
              _a  ,				_a  ,				num_samples=output.num_samples  ,				num_steps=math.ceil(output.num_samples / total_batch_size   )  ,				)   )
      if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
         # Only the main node write the results by default
         _SCREAMING_SNAKE_CASE	=				self.post_process_function(_a  ,				_a  ,				output.predictions   )
         _SCREAMING_SNAKE_CASE	=				self.compute_metrics(_a   )

         # Prefix all keys with metric_key_prefix + '_'
         for key in list(metrics.keys()   ):
            if not key.startswith(F"""{metric_key_prefix}_"""   ):
               _SCREAMING_SNAKE_CASE	=				metrics.pop(_a   )
         metrics.update(output.metrics   )
      else:
         _SCREAMING_SNAKE_CASE	=				output.metrics

      if self.args.should_log:
         # Only the main node log the results by default
         self.log(_a   )

      if self.args.tpu_metrics_debug or self.args.debug:
         # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
         xm.master_print(met.metrics_report()   )

      _SCREAMING_SNAKE_CASE	=				self.callback_handler.on_evaluate(self.args  ,				self.state  ,				self.control  ,				_a   )
      return metrics



   def 							lowerCAmelCase_      (   self :     Union[str, Any]  ,				__lowerCamelCase :     Optional[Any]  ,				__lowerCamelCase :     List[Any]  ,				__lowerCamelCase :     int=None  ,				__lowerCamelCase :     str = "test"   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				self.get_test_dataloader(_a   )

      # Temporarily disable metric computation, we will do it in the loop here.
      _SCREAMING_SNAKE_CASE	=				self.compute_metrics
      _SCREAMING_SNAKE_CASE	=				None
      _SCREAMING_SNAKE_CASE	=				self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
      _SCREAMING_SNAKE_CASE	=				time.time()
      try:
         _SCREAMING_SNAKE_CASE	=				eval_loop(
             _a  ,				description="Prediction"  ,				prediction_loss_only=True if compute_metrics is None else None  ,				ignore_keys=_a  ,				metric_key_prefix=_a  ,				)
      finally:
         _SCREAMING_SNAKE_CASE	=				compute_metrics
      _SCREAMING_SNAKE_CASE	=				self.args.eval_batch_size * self.args.world_size
      if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics:
         start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""]
      output.metrics.update(
          speed_metrics(
              _a  ,				_a  ,				num_samples=output.num_samples  ,				num_steps=math.ceil(output.num_samples / total_batch_size   )  ,				)   )

      if self.post_process_function is None or self.compute_metrics is None:
         return output

      _SCREAMING_SNAKE_CASE	=				self.post_process_function(_a  ,				_a  ,				output.predictions  ,				"predict"   )
      _SCREAMING_SNAKE_CASE	=				self.compute_metrics(_a   )

      # Prefix all keys with metric_key_prefix + '_'
      for key in list(metrics.keys()   ):
         if not key.startswith(F"""{metric_key_prefix}_"""   ):
            _SCREAMING_SNAKE_CASE	=				metrics.pop(_a   )
      metrics.update(output.metrics   )
      return PredictionOutput(predictions=predictions.predictions  ,				label_ids=predictions.label_ids  ,				metrics=_a   )







'''simple docstring'''



import unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible







class 		lowercase_   (			unittest.TestCase       ):


   """simple docstring"""

   def 							lowerCAmelCase_      (   self :     List[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.bin",
          "safety_checker/model.safetensors",
          "vae/diffusion_pytorch_model.bin",
          "vae/diffusion_pytorch_model.safetensors",
          "text_encoder/pytorch_model.bin",
          "text_encoder/model.safetensors",
          "unet/diffusion_pytorch_model.bin",
          "unet/diffusion_pytorch_model.safetensors",
      ]
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Dict   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "unet/diffusion_pytorch_model.bin",
          "unet/diffusion_pytorch_model.safetensors",
      ]
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Tuple   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.bin",
          "safety_checker/model.safetensors",
          "vae/diffusion_pytorch_model.bin",
          "vae/diffusion_pytorch_model.safetensors",
          "text_encoder/pytorch_model.bin",
          "text_encoder/model.safetensors",
          "unet/diffusion_pytorch_model.bin",
          # Removed: 'unet/diffusion_pytorch_model.safetensors',
      ]
      self.assertFalse(is_safetensors_compatible(__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     List[str]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "text_encoder/pytorch_model.bin",
          "text_encoder/model.safetensors",
      ]
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     int   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.bin",
          "safety_checker/model.safetensors",
          "vae/diffusion_pytorch_model.bin",
          "vae/diffusion_pytorch_model.safetensors",
          "text_encoder/pytorch_model.bin",
          # Removed: 'text_encoder/model.safetensors',
          "unet/diffusion_pytorch_model.bin",
          "unet/diffusion_pytorch_model.safetensors",
      ]
      self.assertFalse(is_safetensors_compatible(__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Optional[int]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.fp16.bin",
          "safety_checker/model.fp16.safetensors",
          "vae/diffusion_pytorch_model.fp16.bin",
          "vae/diffusion_pytorch_model.fp16.safetensors",
          "text_encoder/pytorch_model.fp16.bin",
          "text_encoder/model.fp16.safetensors",
          "unet/diffusion_pytorch_model.fp16.bin",
          "unet/diffusion_pytorch_model.fp16.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Any   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "unet/diffusion_pytorch_model.fp16.bin",
          "unet/diffusion_pytorch_model.fp16.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Tuple   ):

      """simple docstring"""
      # pass variant but use the non-variant filenames
      _SCREAMING_SNAKE_CASE	=				[
          "unet/diffusion_pytorch_model.bin",
          "unet/diffusion_pytorch_model.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     int   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.fp16.bin",
          "safety_checker/model.fp16.safetensors",
          "vae/diffusion_pytorch_model.fp16.bin",
          "vae/diffusion_pytorch_model.fp16.safetensors",
          "text_encoder/pytorch_model.fp16.bin",
          "text_encoder/model.fp16.safetensors",
          "unet/diffusion_pytorch_model.fp16.bin",
          # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertFalse(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Dict   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "text_encoder/pytorch_model.fp16.bin",
          "text_encoder/model.fp16.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     str   ):

      """simple docstring"""
      # pass variant but use the non-variant filenames
      _SCREAMING_SNAKE_CASE	=				[
          "text_encoder/pytorch_model.bin",
          "text_encoder/model.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertTrue(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )



   def 							lowerCAmelCase_      (   self :     Optional[Any]   ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE	=				[
          "safety_checker/pytorch_model.fp16.bin",
          "safety_checker/model.fp16.safetensors",
          "vae/diffusion_pytorch_model.fp16.bin",
          "vae/diffusion_pytorch_model.fp16.safetensors",
          "text_encoder/pytorch_model.fp16.bin",
          # 'text_encoder/model.fp16.safetensors',
          "unet/diffusion_pytorch_model.fp16.bin",
          "unet/diffusion_pytorch_model.fp16.safetensors",
      ]
      _SCREAMING_SNAKE_CASE	=				"fp16"
      self.assertFalse(is_safetensors_compatible(__lowerCamelCase  ,				variant=__lowerCamelCase   )   )

