





'''simple docstring'''






import gc
import unittest

import numpy as np
import torch
import torch.nn.functional as F
from transformers import (
    ClapTextConfig,
    ClapTextModelWithProjection,
    RobertaTokenizer,
    SpeechTaHifiGan,
    SpeechTaHifiGanConfig,
)

from diffusers import (
    AudioLDMPipeline,
    AutoencoderKL,
    DDIMScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()
class 		lowerCamelCase  (      _A      ,   unittest.TestCase       ):
	snake_case_      =   AudioLDMPipeline
	snake_case_      =   TEXT_TO_AUDIO_PARAMS
	snake_case_      =   TEXT_TO_AUDIO_BATCH_PARAMS
	snake_case_      =   frozenset(
	    [
	        "num_inference_steps",
	        "num_waveforms_per_prompt",
	        "generator",
	        "latents",
	        "output_type",
	        "return_dict",
	        "callback",
	        "callback_steps",
	    ]       )



	def   _lowerCamelCase    (   self	):
			torch.manual_seed(0	)
			lowerCAmelCase     :    Union[str, Any]						=   UNetaDConditionModel(
			    block_out_channels=(32, 64)		,      layers_per_block=2		,      sample_size=32		,      in_channels=4		,      out_channels=4		,      down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")		,      up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")		,      cross_attention_dim=(32, 64)		,      class_embed_type="simple_projection"		,      projection_class_embeddings_input_dim=32		,      class_embeddings_concat=a_		,      )
			lowerCAmelCase     :    Any						=   DDIMScheduler(
			    beta_start=0.00085		,      beta_end=0.012		,      beta_schedule="scaled_linear"		,      clip_sample=a_		,      set_alpha_to_one=a_		,      )
			torch.manual_seed(0	)
			lowerCAmelCase     :    List[str]						=   AutoencoderKL(
			    block_out_channels=[32, 64]		,      in_channels=1		,      out_channels=1		,      down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]		,      up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]		,      latent_channels=4		,      )
			torch.manual_seed(0	)
			lowerCAmelCase     :    List[str]						=   ClapTextConfig(
			    bos_token_id=0		,      eos_token_id=2		,      hidden_size=32		,      intermediate_size=37		,      layer_norm_eps=1e-0_5		,      num_attention_heads=4		,      num_hidden_layers=5		,      pad_token_id=1		,      vocab_size=1_000		,      projection_dim=32		,      )
			lowerCAmelCase     :    Union[str, Any]						=   ClapTextModelWithProjection(a_	)
			lowerCAmelCase     :    int						=   RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta"		,      model_max_length=77	)

			lowerCAmelCase     :    Dict						=   SpeechTaHifiGanConfig(
			    model_in_dim=8		,      sampling_rate=16_000		,      upsample_initial_channel=16		,      upsample_rates=[2, 2]		,      upsample_kernel_sizes=[4, 4]		,      resblock_kernel_sizes=[3, 7]		,      resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]]		,      normalize_before=a_		,      )

			lowerCAmelCase     :    int						=   SpeechTaHifiGan(a_	)

			lowerCAmelCase     :    str						=   {
			    "unet": unet,
			    "scheduler": scheduler,
			    "vae": vae,
			    "text_encoder": text_encoder,
			    "tokenizer": tokenizer,
			    "vocoder": vocoder,
			}
			return components



	def   _lowerCamelCase    (   self		,      a_		,      a_=0	):
			if str(a_	).startswith("mps"	):
					lowerCAmelCase     :    Tuple						=   torch.manual_seed(a_	)
			else:
					lowerCAmelCase     :    Union[str, Any]						=   torch.Generator(device=a_	).manual_seed(a_	)
			lowerCAmelCase     :    Union[str, Any]						=   {
			    "prompt": "A hammer hitting a wooden surface",
			    "generator": generator,
			    "num_inference_steps": 2,
			    "guidance_scale": 6.0,
			}
			return inputs



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Tuple						=   "cpu"  # ensure determinism for the device-dependent torch.Generator

			lowerCAmelCase     :    str						=   self.get_dummy_components()
			lowerCAmelCase     :    Tuple						=   AudioLDMPipeline(**a_	)
			lowerCAmelCase     :    Optional[int]						=   audioldm_pipe.to(a_	)
			audioldm_pipe.set_progress_bar_config(disable=a_	)

			lowerCAmelCase     :    Union[str, Any]						=   self.get_dummy_inputs(a_	)
			lowerCAmelCase     :    Union[str, Any]						=   audioldm_pipe(**a_	)
			lowerCAmelCase     :    Union[str, Any]						=   output.audios[0]

			assert audio.ndim == 1
			assert len(a_	) == 256

			lowerCAmelCase     :    Tuple						=   audio[:10]
			lowerCAmelCase     :    Optional[int]						=   np.array(
			    [-0.0050, 0.0050, -0.0060, 0.0033, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0033]	)

			assert np.abs(audio_slice - expected_slice	).max() < 1e-2



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[str]						=   self.get_dummy_components()
			lowerCAmelCase     :    Optional[Any]						=   AudioLDMPipeline(**a_	)
			lowerCAmelCase     :    Any						=   audioldm_pipe.to(a_	)
			lowerCAmelCase     :    List[str]						=   audioldm_pipe.to(a_	)
			audioldm_pipe.set_progress_bar_config(disable=a_	)

			lowerCAmelCase     :    Optional[int]						=   self.get_dummy_inputs(a_	)
			lowerCAmelCase     :    Dict						=   3 * [inputs["prompt"]]

			# forward
			lowerCAmelCase     :    str						=   audioldm_pipe(**a_	)
			lowerCAmelCase     :    Any						=   output.audios[0]

			lowerCAmelCase     :    Any						=   self.get_dummy_inputs(a_	)
			lowerCAmelCase     :    Any						=   3 * [inputs.pop("prompt"	)]

			lowerCAmelCase     :    List[str]						=   audioldm_pipe.tokenizer(
			    a_		,      padding="max_length"		,      max_length=audioldm_pipe.tokenizer.model_max_length		,      truncation=a_		,      return_tensors="pt"		,      )
			lowerCAmelCase     :    List[str]						=   text_inputs["input_ids"].to(a_	)

			lowerCAmelCase     :    Union[str, Any]						=   audioldm_pipe.text_encoder(
			    a_		,      )
			lowerCAmelCase     :    Optional[int]						=   prompt_embeds.text_embeds
			# additional L_2 normalization over each hidden-state
			lowerCAmelCase     :    str						=   F.normalize(a_		,      dim=-1	)

			lowerCAmelCase     :    str						=   prompt_embeds

			# forward
			lowerCAmelCase     :    Tuple						=   audioldm_pipe(**a_	)
			lowerCAmelCase     :    str						=   output.audios[0]

			assert np.abs(audio_a - audio_a	).max() < 1e-2



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[str]						=   self.get_dummy_components()
			lowerCAmelCase     :    str						=   AudioLDMPipeline(**a_	)
			lowerCAmelCase     :    str						=   audioldm_pipe.to(a_	)
			lowerCAmelCase     :    Tuple						=   audioldm_pipe.to(a_	)
			audioldm_pipe.set_progress_bar_config(disable=a_	)

			lowerCAmelCase     :    Any						=   self.get_dummy_inputs(a_	)
			lowerCAmelCase     :    List[str]						=   3 * ["this is a negative prompt"]
			lowerCAmelCase     :    Optional[int]						=   negative_prompt
			lowerCAmelCase     :    Optional[Any]						=   3 * [inputs["prompt"]]

			# forward
			lowerCAmelCase     :    int						=   audioldm_pipe(**a_	)
			lowerCAmelCase     :    str						=   output.audios[0]

			lowerCAmelCase     :    str						=   self.get_dummy_inputs(a_	)
			lowerCAmelCase     :    str						=   3 * [inputs.pop("prompt"	)]

			lowerCAmelCase     :    Optional[int]						=   []
			for p in [prompt, negative_prompt]:
					lowerCAmelCase     :    Tuple						=   audioldm_pipe.tokenizer(
					    a_		,      padding="max_length"		,      max_length=audioldm_pipe.tokenizer.model_max_length		,      truncation=a_		,      return_tensors="pt"		,      )
					lowerCAmelCase     :    List[Any]						=   text_inputs["input_ids"].to(a_	)

					lowerCAmelCase     :    Optional[int]						=   audioldm_pipe.text_encoder(
					    a_		,      )
					lowerCAmelCase     :    Any						=   text_embeds.text_embeds
					# additional L_2 normalization over each hidden-state
					lowerCAmelCase     :    Union[str, Any]						=   F.normalize(a_		,      dim=-1	)

					embeds.append(a_	)

			lowerCAmelCase     :    List[Any]						=   embeds

			# forward
			lowerCAmelCase     :    Optional[Any]						=   audioldm_pipe(**a_	)
			lowerCAmelCase     :    int						=   output.audios[0]

			assert np.abs(audio_a - audio_a	).max() < 1e-2



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[str]						=   "cpu"  # ensure determinism for the device-dependent torch.Generator
			lowerCAmelCase     :    Optional[Any]						=   self.get_dummy_components()
			lowerCAmelCase     :    Union[str, Any]						=   PNDMScheduler(skip_prk_steps=a_	)
			lowerCAmelCase     :    Optional[int]						=   AudioLDMPipeline(**a_	)
			lowerCAmelCase     :    int						=   audioldm_pipe.to(a_	)
			audioldm_pipe.set_progress_bar_config(disable=a_	)

			lowerCAmelCase     :    Optional[Any]						=   self.get_dummy_inputs(a_	)
			lowerCAmelCase     :    List[Any]						=   "egg cracking"
			lowerCAmelCase     :    List[str]						=   audioldm_pipe(**a_		,      negative_prompt=a_	)
			lowerCAmelCase     :    List[str]						=   output.audios[0]

			assert audio.ndim == 1
			assert len(a_	) == 256

			lowerCAmelCase     :    Union[str, Any]						=   audio[:10]
			lowerCAmelCase     :    int						=   np.array(
			    [-0.0051, 0.0050, -0.0060, 0.0034, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0032]	)

			assert np.abs(audio_slice - expected_slice	).max() < 1e-2



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Any						=   "cpu"  # ensure determinism for the device-dependent torch.Generator
			lowerCAmelCase     :    Tuple						=   self.get_dummy_components()
			lowerCAmelCase     :    str						=   PNDMScheduler(skip_prk_steps=a_	)
			lowerCAmelCase     :    Tuple						=   AudioLDMPipeline(**a_	)
			lowerCAmelCase     :    List[Any]						=   audioldm_pipe.to(a_	)
			audioldm_pipe.set_progress_bar_config(disable=a_	)

			lowerCAmelCase     :    Any						=   "A hammer hitting a wooden surface"

			# test num_waveforms_per_prompt=1 (default)
			lowerCAmelCase     :    List[str]						=   audioldm_pipe(a_		,      num_inference_steps=2	).audios

			assert audios.shape == (1, 256)

			# test num_waveforms_per_prompt=1 (default) for batch of prompts
			lowerCAmelCase     :    Optional[Any]						=   2
			lowerCAmelCase     :    Tuple						=   audioldm_pipe([prompt] * batch_size		,      num_inference_steps=2	).audios

			assert audios.shape == (batch_size, 256)

			# test num_waveforms_per_prompt for single prompt
			lowerCAmelCase     :    List[Any]						=   2
			lowerCAmelCase     :    Dict						=   audioldm_pipe(a_		,      num_inference_steps=2		,      num_waveforms_per_prompt=a_	).audios

			assert audios.shape == (num_waveforms_per_prompt, 256)

			# test num_waveforms_per_prompt for batch of prompts
			lowerCAmelCase     :    Any						=   2
			lowerCAmelCase     :    Dict						=   audioldm_pipe(
			    [prompt] * batch_size		,      num_inference_steps=2		,      num_waveforms_per_prompt=a_	).audios

			assert audios.shape == (batch_size * num_waveforms_per_prompt, 256)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    int						=   "cpu"  # ensure determinism for the device-dependent torch.Generator
			lowerCAmelCase     :    Optional[Any]						=   self.get_dummy_components()
			lowerCAmelCase     :    Tuple						=   AudioLDMPipeline(**a_	)
			lowerCAmelCase     :    Union[str, Any]						=   audioldm_pipe.to(a_	)
			audioldm_pipe.set_progress_bar_config(disable=a_	)
			lowerCAmelCase     :    List[Any]						=   audioldm_pipe.vocoder.config.sampling_rate

			lowerCAmelCase     :    Optional[Any]						=   self.get_dummy_inputs(a_	)
			lowerCAmelCase     :    Optional[int]						=   audioldm_pipe(audio_length_in_s=0.016		,      **a_	)
			lowerCAmelCase     :    int						=   output.audios[0]

			assert audio.ndim == 1
			assert len(a_	) / vocoder_sampling_rate == 0.016

			lowerCAmelCase     :    Any						=   audioldm_pipe(audio_length_in_s=0.032		,      **a_	)
			lowerCAmelCase     :    List[str]						=   output.audios[0]

			assert audio.ndim == 1
			assert len(a_	) / vocoder_sampling_rate == 0.032



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    str						=   self.get_dummy_components()
			lowerCAmelCase     :    Optional[Any]						=   AudioLDMPipeline(**a_	)
			lowerCAmelCase     :    int						=   audioldm_pipe.to(a_	)
			audioldm_pipe.set_progress_bar_config(disable=a_	)

			lowerCAmelCase     :    Optional[Any]						=   ["hey"]

			lowerCAmelCase     :    Union[str, Any]						=   audioldm_pipe(a_		,      num_inference_steps=1	)
			lowerCAmelCase     :    str						=   output.audios.shape
			assert audio_shape == (1, 256)

			lowerCAmelCase     :    List[str]						=   audioldm_pipe.vocoder.config
			config.model_in_dim *= 2
			lowerCAmelCase     :    Dict						=   SpeechTaHifiGan(a_	).to(a_	)
			lowerCAmelCase     :    List[Any]						=   audioldm_pipe(a_		,      num_inference_steps=1	)
			lowerCAmelCase     :    Optional[int]						=   output.audios.shape
			# waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram
			assert audio_shape == (1, 256)



	def   _lowerCamelCase    (   self	):
			self._test_attention_slicing_forward_pass(test_mean_pixel_difference=a_	)



	def   _lowerCamelCase    (   self	):
			self._test_inference_batch_single_identical(test_mean_pixel_difference=a_	)





	@unittest.skipIf(
	    torch_device != "cuda" or not is_xformers_available()		,      reason="XFormers attention is only available with CUDA and `xformers` installed"		,      )
	def   _lowerCamelCase    (   self	):
			self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=a_	)






@slow
class 		lowerCamelCase  (      unittest.TestCase       ):



	def   _lowerCamelCase    (   self	):
			super().tearDown()
			gc.collect()
			torch.cuda.empty_cache()



	def   _lowerCamelCase    (   self		,      a_		,      a_="cpu"		,      a_=torch.floataa		,      a_=0	):
			lowerCAmelCase     :    Dict						=   torch.Generator(device=a_	).manual_seed(a_	)
			lowerCAmelCase     :    Optional[int]						=   np.random.RandomState(a_	).standard_normal((1, 8, 128, 16)	)
			lowerCAmelCase     :    Any						=   torch.from_numpy(a_	).to(device=a_		,      dtype=a_	)
			lowerCAmelCase     :    Tuple						=   {
			    "prompt": "A hammer hitting a wooden surface",
			    "latents": latents,
			    "generator": generator,
			    "num_inference_steps": 3,
			    "guidance_scale": 2.5,
			}
			return inputs



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    int						=   AudioLDMPipeline.from_pretrained("cvssp/audioldm"	)
			lowerCAmelCase     :    List[str]						=   audioldm_pipe.to(a_	)
			audioldm_pipe.set_progress_bar_config(disable=a_	)

			lowerCAmelCase     :    Any						=   self.get_inputs(a_	)
			lowerCAmelCase     :    Any						=   25
			lowerCAmelCase     :    Optional[int]						=   audioldm_pipe(**a_	).audios[0]

			assert audio.ndim == 1
			assert len(a_	) == 81_920

			lowerCAmelCase     :    Union[str, Any]						=   audio[77_230:77_240]
			lowerCAmelCase     :    Dict						=   np.array(
			    [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315]	)
			lowerCAmelCase     :    Optional[Any]						=   np.abs(expected_slice - audio_slice	).max()
			assert max_diff < 1e-2





	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Dict						=   AudioLDMPipeline.from_pretrained("cvssp/audioldm"	)
			lowerCAmelCase     :    int						=   LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config	)
			lowerCAmelCase     :    str						=   audioldm_pipe.to(a_	)
			audioldm_pipe.set_progress_bar_config(disable=a_	)

			lowerCAmelCase     :    Dict						=   self.get_inputs(a_	)
			lowerCAmelCase     :    Union[str, Any]						=   audioldm_pipe(**a_	).audios[0]

			assert audio.ndim == 1
			assert len(a_	) == 81_920

			lowerCAmelCase     :    Optional[int]						=   audio[27_780:27_790]
			lowerCAmelCase     :    Optional[int]						=   np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212]	)
			lowerCAmelCase     :    int						=   np.abs(expected_slice - audio_slice	).max()
			assert max_diff < 3e-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 		lowerCamelCase  (      unittest.TestCase       ):



	def   _lowerCamelCase    (   self		,      a_	):
			for model_result in results.values():
					for batch_size, sequence_length in zip(model_result["bs"]		,      model_result["ss"]	):
							lowerCAmelCase     :    Tuple						=   model_result["result"][batch_size][sequence_length]
							self.assertIsNotNone(a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Optional[int]						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    Tuple						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      eager_mode=a_		,      multi_process=a_		,      )
			lowerCAmelCase     :    Optional[Any]						=   TensorFlowBenchmark(a_	)
			lowerCAmelCase     :    Optional[Any]						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Union[str, Any]						=   "sgugger/tiny-distilbert-classification"
			lowerCAmelCase     :    Tuple						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      only_pretrain_model=a_		,      )
			lowerCAmelCase     :    Any						=   TensorFlowBenchmark(a_	)
			lowerCAmelCase     :    str						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Union[str, Any]						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    str						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      )
			lowerCAmelCase     :    List[str]						=   TensorFlowBenchmark(a_	)
			lowerCAmelCase     :    str						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[str]						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    Union[str, Any]						=   AutoConfig.from_pretrained(a_	)
			lowerCAmelCase     :    Optional[Any]						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      eager_mode=a_		,      multi_process=a_		,      )
			lowerCAmelCase     :    Any						=   TensorFlowBenchmark(a_		,      [config]	)
			lowerCAmelCase     :    Union[str, Any]						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    int						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    Union[str, Any]						=   AutoConfig.from_pretrained(a_	)
			lowerCAmelCase     :    Union[str, Any]						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      )
			lowerCAmelCase     :    Tuple						=   TensorFlowBenchmark(a_		,      [config]	)
			lowerCAmelCase     :    Any						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Tuple						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    List[str]						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      )
			lowerCAmelCase     :    List[str]						=   TensorFlowBenchmark(a_	)
			lowerCAmelCase     :    str						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_train_result	)
			self.check_results_dict_not_empty(results.memory_train_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[Any]						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    Union[str, Any]						=   AutoConfig.from_pretrained(a_	)
			lowerCAmelCase     :    Dict						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      )
			lowerCAmelCase     :    Dict						=   TensorFlowBenchmark(a_		,      [config]	)
			lowerCAmelCase     :    int						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_train_result	)
			self.check_results_dict_not_empty(results.memory_train_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    str						=   "patrickvonplaten/t5-tiny-random"
			lowerCAmelCase     :    Tuple						=   AutoConfig.from_pretrained(a_	)
			lowerCAmelCase     :    Dict						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      )
			lowerCAmelCase     :    List[str]						=   TensorFlowBenchmark(a_		,      configs=[config]	)
			lowerCAmelCase     :    List[Any]						=   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   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    str						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    List[Any]						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      use_xla=a_		,      multi_process=a_		,      )
			lowerCAmelCase     :    Any						=   TensorFlowBenchmark(a_	)
			lowerCAmelCase     :    str						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Tuple						=   "sshleifer/tiny-gpt2"
			with tempfile.TemporaryDirectory() as tmp_dir:
					lowerCAmelCase     :    Optional[int]						=   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     :    Optional[Any]						=   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   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Any						=   "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     :    int						=   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     :    Optional[Any]						=   TensorFlowBenchmark(a_	)
					lowerCAmelCase     :    Tuple						=   benchmark.run()
					_check_summary_is_not_empty(result.inference_summary	)
					self.assertTrue(Path(os.path.join(a_		,      "log.txt"	)	).exists()	)



