


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        lowerCamelCase__	(							UpperCAmelCase      ):






			"""simple docstring"""






			def __init__(							self      ,						*snake_case      ,						snake_case=None      ,						snake_case=None      ,						**snake_case     ):

						'''simple docstring'''


						super().__init__(*snake_case      ,						**snake_case     )
						UpperCamelCase__					=       eval_examples
						UpperCamelCase__					=       post_process_function




			def 				snake_case__				(							self      ,						snake_case=None      ,						snake_case=None      ,						snake_case=None      ,						snake_case = "eval"     ):

						'''simple docstring'''


						UpperCamelCase__					=       self.eval_dataset if eval_dataset is None else eval_dataset
						UpperCamelCase__					=       self.get_eval_dataloader(snake_case     )
						UpperCamelCase__					=       self.eval_examples if eval_examples is None else eval_examples

						# Temporarily disable metric computation, we will do it in the loop here.
						UpperCamelCase__					=       self.compute_metrics
						UpperCamelCase__					=       None
						UpperCamelCase__					=       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
						UpperCamelCase__					=       time.time()
						try:
									UpperCamelCase__					=       eval_loop(
									    snake_case      ,						description="Evaluation"      ,						prediction_loss_only=True if compute_metrics is None else None      ,						ignore_keys=snake_case      ,						metric_key_prefix=snake_case      ,						)
						finally:
									UpperCamelCase__					=       compute_metrics
						UpperCamelCase__					=       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(
						        snake_case      ,						snake_case      ,						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
									UpperCamelCase__					=       self.post_process_function(snake_case      ,						snake_case      ,						output.predictions     )
									UpperCamelCase__					=       self.compute_metrics(snake_case     )

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

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

						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()     )

						UpperCamelCase__					=       self.callback_handler.on_evaluate(self.args      ,						self.state      ,						self.control      ,						snake_case     )
						return metrics







			def 				snake_case__				(							self      ,						snake_case      ,						snake_case      ,						snake_case=None      ,						snake_case = "test"     ):

						'''simple docstring'''


						UpperCamelCase__					=       self.get_test_dataloader(snake_case     )

						# Temporarily disable metric computation, we will do it in the loop here.
						UpperCamelCase__					=       self.compute_metrics
						UpperCamelCase__					=       None
						UpperCamelCase__					=       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
						UpperCamelCase__					=       time.time()
						try:
									UpperCamelCase__					=       eval_loop(
									    snake_case      ,						description="Prediction"      ,						prediction_loss_only=True if compute_metrics is None else None      ,						ignore_keys=snake_case      ,						metric_key_prefix=snake_case      ,						)
						finally:
									UpperCamelCase__					=       compute_metrics
						UpperCamelCase__					=       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(
						        snake_case      ,						snake_case      ,						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

						UpperCamelCase__					=       self.post_process_function(snake_case      ,						snake_case      ,						output.predictions      ,						"predict"     )
						UpperCamelCase__					=       self.compute_metrics(snake_case     )

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






import unittest

from diffusers import FlaxAutoencoderKL
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import require_flax

from .test_modeling_common_flax import FlaxModelTesterMixin


if is_flax_available():
						import jax







@require_flax
class        lowerCamelCase__	(							UpperCAmelCase					,	unittest.TestCase      ):






			"""simple docstring"""






			_UpperCamelCase		:					List[str]    =		FlaxAutoencoderKL




			@property
			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       4
						UpperCamelCase__					=       3
						UpperCamelCase__					=       (32, 32)

						UpperCamelCase__					=       jax.random.PRNGKey(0     )
						UpperCamelCase__					=       jax.random.uniform(snake_case      ,						((batch_size, num_channels) + sizes)     )

						return {"sample": image, "prng_key": prng_key}







			def 				snake_case__				(							self     ):

						'''simple docstring'''


						UpperCamelCase__					=       {
						    "block_out_channels": [32, 64],
						    "in_channels": 3,
						    "out_channels": 3,
						    "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"],
						    "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"],
						    "latent_channels": 4,
						}
						UpperCamelCase__					=       self.dummy_input
						return init_dict, inputs_dict


