


"""simple docstring"""


import gc
import unittest

from parameterized import parameterized

from diffusers import FlaxUNetaDConditionModel
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow


if is_flax_available():
	import jax
	import jax.numpy as jnp
@slow
@require_flax
class 						_UpperCamelCase						(     unittest.TestCase	):
							'''simple docstring'''







							def     snake_case       (  self					,     __a					,     __a			):
										return f"gaussian_noise_s={seed}_shape={'_'.join([str(__a			) for s in shape]			)}.npy"

							def     snake_case       (  self			):
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()

							def     snake_case       (  self					,     __a=0					,     __a=(4, 4, 64, 64)					,     __a=False			):
										__lowerCAmelCase       					=    jnp.bfloataa if fpaa else jnp.floataa
										__lowerCAmelCase       					=    jnp.array(load_hf_numpy(self.get_file_format(__a					,     __a			)			)					,     dtype=__a			)
										return image

							def     snake_case       (  self					,     __a=False					,     __a="CompVis/stable-diffusion-v1-4"			):
										__lowerCAmelCase       					=    jnp.bfloataa if fpaa else jnp.floataa
										__lowerCAmelCase       					=    "bf16" if fpaa else None

										__lowerCAmelCase					,			__lowerCAmelCase       					=    FlaxUNetaDConditionModel.from_pretrained(
										    __a					,     subfolder="unet"					,     dtype=__a					,     revision=__a			)
										return model, params

							def     snake_case       (  self					,     __a=0					,     __a=(4, 77, 7_68)					,     __a=False			):
										__lowerCAmelCase       					=    jnp.bfloataa if fpaa else jnp.floataa
										__lowerCAmelCase       					=    jnp.array(load_hf_numpy(self.get_file_format(__a					,     __a			)			)					,     dtype=__a			)
										return hidden_states

							@parameterized.expand(
							    [
							        # fmt: off
							        [83, 4, [-0.2_3_2_3, -0.1_3_0_4, 0.0_8_1_3, -0.3_0_9_3, -0.0_9_1_9, -0.1_5_7_1, -0.1_1_2_5, -0.5_8_0_6]],
							        [17, 0.5_5, [-0.0_8_3_1, -0.2_4_4_3, 0.0_9_0_1, -0.0_9_1_9, 0.3_3_9_6, 0.0_1_0_3, -0.3_7_4_3, 0.0_7_0_1]],
							        [8, 0.8_9, [-0.4_8_6_3, 0.0_8_5_9, 0.0_8_7_5, -0.1_6_5_8, 0.9_1_9_9, -0.0_1_1_4, 0.4_8_3_9, 0.4_6_3_9]],
							        [3, 10_00, [-0.5_6_4_9, 0.2_4_0_2, -0.5_5_1_8, 0.1_2_4_8, 1.1_3_2_8, -0.2_4_4_3, -0.0_3_2_5, -1.0_0_7_8]],
							        # fmt: on
							    ]			)
							def     snake_case       (  self					,     __a					,     __a					,     __a			):
										__lowerCAmelCase					,			__lowerCAmelCase       					=    self.get_unet_model(model_id="CompVis/stable-diffusion-v1-4"					,     fpaa=__a			)
										__lowerCAmelCase       					=    self.get_latents(__a					,     fpaa=__a			)
										__lowerCAmelCase       					=    self.get_encoder_hidden_states(__a					,     fpaa=__a			)

										__lowerCAmelCase       					=    model.apply(
										    {"params": params}					,     __a					,     jnp.array(__a					,     dtype=jnp.intaa			)					,     encoder_hidden_states=__a					,     ).sample

										assert sample.shape == latents.shape

										__lowerCAmelCase       					=    jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())			)					,     dtype=jnp.floataa			)
										__lowerCAmelCase       					=    jnp.array(__a					,     dtype=jnp.floataa			)

										# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware
										assert jnp.allclose(__a					,     __a					,     atol=1e-2			)



							@parameterized.expand(
							    [
							        # fmt: off
							        [83, 4, [0.1_5_1_4, 0.0_8_0_7, 0.1_6_2_4, 0.1_0_1_6, -0.1_8_9_6, 0.0_2_6_3, 0.0_6_7_7, 0.2_3_1_0]],
							        [17, 0.5_5, [0.1_1_6_4, -0.0_2_1_6, 0.0_1_7_0, 0.1_5_8_9, -0.3_1_2_0, 0.1_0_0_5, -0.0_5_8_1, -0.1_4_5_8]],
							        [8, 0.8_9, [-0.1_7_5_8, -0.0_1_6_9, 0.1_0_0_4, -0.1_4_1_1, 0.1_3_1_2, 0.1_1_0_3, -0.1_9_9_6, 0.2_1_3_9]],
							        [3, 10_00, [0.1_2_1_4, 0.0_3_5_2, -0.0_7_3_1, -0.1_5_6_2, -0.0_9_9_4, -0.0_9_0_6, -0.2_3_4_0, -0.0_5_3_9]],
							        # fmt: on
							    ]			)
							def     snake_case       (  self					,     __a					,     __a					,     __a			):
										__lowerCAmelCase					,			__lowerCAmelCase       					=    self.get_unet_model(model_id="stabilityai/stable-diffusion-2"					,     fpaa=__a			)
										__lowerCAmelCase       					=    self.get_latents(__a					,     shape=(4, 4, 96, 96)					,     fpaa=__a			)
										__lowerCAmelCase       					=    self.get_encoder_hidden_states(__a					,     shape=(4, 77, 10_24)					,     fpaa=__a			)

										__lowerCAmelCase       					=    model.apply(
										    {"params": params}					,     __a					,     jnp.array(__a					,     dtype=jnp.intaa			)					,     encoder_hidden_states=__a					,     ).sample

										assert sample.shape == latents.shape

										__lowerCAmelCase       					=    jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())			)					,     dtype=jnp.floataa			)
										__lowerCAmelCase       					=    jnp.array(__a					,     dtype=jnp.floataa			)

										# Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware
										assert jnp.allclose(__a					,     __a					,     atol=1e-2			)








"""simple docstring"""


import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
	import tensorflow as tf

	from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments
@require_tf
class 						_UpperCamelCase						(     unittest.TestCase	):
							'''simple docstring'''







							def     snake_case       (  self					,     __a			):
										for model_result in results.values():
													for batch_size, sequence_length in zip(model_result["bs"]					,     model_result["ss"]			):
																__lowerCAmelCase       					=    model_result["result"][batch_size][sequence_length]
																self.assertIsNotNone(__a			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     eager_mode=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sgugger/tiny-distilbert-classification"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     only_pretrain_model=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     eager_mode=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_train_result			)
										self.check_results_dict_not_empty(results.memory_train_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_train_result			)
										self.check_results_dict_not_empty(results.memory_train_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "patrickvonplaten/t5-tiny-random"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     configs=[config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("GPU"			)			) == 0					,     "Cannot do xla on CPU."			)
							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     use_xla=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										with tempfile.TemporaryDirectory() as tmp_dir:
													__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
													    models=[MODEL_ID]					,     inference=__a					,     save_to_csv=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     inference_time_csv_file=os.path.join(__a					,     "inf_time.csv"			)					,     inference_memory_csv_file=os.path.join(__a					,     "inf_mem.csv"			)					,     env_info_csv_file=os.path.join(__a					,     "env.csv"			)					,     multi_process=__a					,     )
													__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
													benchmark.run()
													self.assertTrue(Path(os.path.join(__a					,     "inf_time.csv"			)			).exists()			)
													self.assertTrue(Path(os.path.join(__a					,     "inf_mem.csv"			)			).exists()			)
													self.assertTrue(Path(os.path.join(__a					,     "env.csv"			)			).exists()			)



							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"

										def _check_summary_is_not_empty(__a			):
													self.assertTrue(hasattr(__a					,     "sequential"			)			)
													self.assertTrue(hasattr(__a					,     "cumulative"			)			)
													self.assertTrue(hasattr(__a					,     "current"			)			)
													self.assertTrue(hasattr(__a					,     "total"			)			)

										with tempfile.TemporaryDirectory() as tmp_dir:
													__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
													    models=[MODEL_ID]					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     log_filename=os.path.join(__a					,     "log.txt"			)					,     log_print=__a					,     trace_memory_line_by_line=__a					,     eager_mode=__a					,     multi_process=__a					,     )
													__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
													__lowerCAmelCase       					=    benchmark.run()
													_check_summary_is_not_empty(result.inference_summary			)
													self.assertTrue(Path(os.path.join(__a					,     "log.txt"			)			).exists()			)




