






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       __SCREAMING_SNAKE_CASE   (				unittest.TestCase):






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase				):






												"""simple docstring"""
												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(_UpperCamelCase				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'sshleifer/tiny-gpt2'
												lowerCAmelCase__        =  TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]       ,  training=_UpperCamelCase       ,  inference=_UpperCamelCase       ,  sequence_lengths=[8]       ,  batch_sizes=[1]       ,  eager_mode=_UpperCamelCase       ,  multi_process=_UpperCamelCase       ,  )
												lowerCAmelCase__        =  TensorFlowBenchmark(_UpperCamelCase				)
												lowerCAmelCase__        =  benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result				)
												self.check_results_dict_not_empty(results.memory_inference_result				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'sgugger/tiny-distilbert-classification'
												lowerCAmelCase__        =  TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]       ,  training=_UpperCamelCase       ,  inference=_UpperCamelCase       ,  sequence_lengths=[8]       ,  batch_sizes=[1]       ,  multi_process=_UpperCamelCase       ,  only_pretrain_model=_UpperCamelCase       ,  )
												lowerCAmelCase__        =  TensorFlowBenchmark(_UpperCamelCase				)
												lowerCAmelCase__        =  benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result				)
												self.check_results_dict_not_empty(results.memory_inference_result				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'sshleifer/tiny-gpt2'
												lowerCAmelCase__        =  TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]       ,  training=_UpperCamelCase       ,  inference=_UpperCamelCase       ,  sequence_lengths=[8]       ,  batch_sizes=[1]       ,  multi_process=_UpperCamelCase       ,  )
												lowerCAmelCase__        =  TensorFlowBenchmark(_UpperCamelCase				)
												lowerCAmelCase__        =  benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result				)
												self.check_results_dict_not_empty(results.memory_inference_result				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'sshleifer/tiny-gpt2'
												lowerCAmelCase__        =  AutoConfig.from_pretrained(_UpperCamelCase				)
												lowerCAmelCase__        =  TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]       ,  training=_UpperCamelCase       ,  inference=_UpperCamelCase       ,  sequence_lengths=[8]       ,  batch_sizes=[1]       ,  eager_mode=_UpperCamelCase       ,  multi_process=_UpperCamelCase       ,  )
												lowerCAmelCase__        =  TensorFlowBenchmark(_UpperCamelCase       ,  [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  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'sshleifer/tiny-gpt2'
												lowerCAmelCase__        =  AutoConfig.from_pretrained(_UpperCamelCase				)
												lowerCAmelCase__        =  TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]       ,  training=_UpperCamelCase       ,  inference=_UpperCamelCase       ,  sequence_lengths=[8]       ,  batch_sizes=[1]       ,  multi_process=_UpperCamelCase       ,  )
												lowerCAmelCase__        =  TensorFlowBenchmark(_UpperCamelCase       ,  [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  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'sshleifer/tiny-gpt2'
												lowerCAmelCase__        =  TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]       ,  training=_UpperCamelCase       ,  inference=_UpperCamelCase       ,  sequence_lengths=[8]       ,  batch_sizes=[1]       ,  multi_process=_UpperCamelCase       ,  )
												lowerCAmelCase__        =  TensorFlowBenchmark(_UpperCamelCase				)
												lowerCAmelCase__        =  benchmark.run()
												self.check_results_dict_not_empty(results.time_train_result				)
												self.check_results_dict_not_empty(results.memory_train_result				)






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'sshleifer/tiny-gpt2'
												lowerCAmelCase__        =  AutoConfig.from_pretrained(_UpperCamelCase				)
												lowerCAmelCase__        =  TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]       ,  training=_UpperCamelCase       ,  inference=_UpperCamelCase       ,  sequence_lengths=[8]       ,  batch_sizes=[1]       ,  multi_process=_UpperCamelCase       ,  )
												lowerCAmelCase__        =  TensorFlowBenchmark(_UpperCamelCase       ,  [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  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'patrickvonplaten/t5-tiny-random'
												lowerCAmelCase__        =  AutoConfig.from_pretrained(_UpperCamelCase				)
												lowerCAmelCase__        =  TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]       ,  training=_UpperCamelCase       ,  inference=_UpperCamelCase       ,  sequence_lengths=[8]       ,  batch_sizes=[1]       ,  multi_process=_UpperCamelCase       ,  )
												lowerCAmelCase__        =  TensorFlowBenchmark(_UpperCamelCase       ,  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  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'sshleifer/tiny-gpt2'
												lowerCAmelCase__        =  TensorFlowBenchmarkArguments(
												    models=[MODEL_ID]       ,  training=_UpperCamelCase       ,  inference=_UpperCamelCase       ,  sequence_lengths=[8]       ,  batch_sizes=[1]       ,  use_xla=_UpperCamelCase       ,  multi_process=_UpperCamelCase       ,  )
												lowerCAmelCase__        =  TensorFlowBenchmark(_UpperCamelCase				)
												lowerCAmelCase__        =  benchmark.run()
												self.check_results_dict_not_empty(results.time_inference_result				)
												self.check_results_dict_not_empty(results.memory_inference_result				)






						def  UpperCamelCase__ (		self				):






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







						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'sshleifer/tiny-gpt2'

												def _check_summary_is_not_empty(_UpperCamelCase				):
																		self.assertTrue(hasattr(_UpperCamelCase       ,  'sequential'				)				)
																		self.assertTrue(hasattr(_UpperCamelCase       ,  'cumulative'				)				)
																		self.assertTrue(hasattr(_UpperCamelCase       ,  'current'				)				)
																		self.assertTrue(hasattr(_UpperCamelCase       ,  'total'				)				)

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













import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    TextToVideoSDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, load_numpy, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


@skip_mps
class       __SCREAMING_SNAKE_CASE   (				__lowercase						,   unittest.TestCase):
						_SCREAMING_SNAKE_CASE		:    List[str]						=   TextToVideoSDPipeline
						_SCREAMING_SNAKE_CASE		:    str						=   TEXT_TO_IMAGE_PARAMS
						_SCREAMING_SNAKE_CASE		:    List[str]						=   TEXT_TO_IMAGE_BATCH_PARAMS
						# No `output_type`.
						_SCREAMING_SNAKE_CASE		:    Dict						=   frozenset(
						    [
						        '''num_inference_steps''',
						        '''generator''',
						        '''latents''',
						        '''return_dict''',
						        '''callback''',
						        '''callback_steps''',
						    ])






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												torch.manual_seed(0				)
												lowerCAmelCase__        =  UNetaDConditionModel(
												    block_out_channels=(32, 64, 64, 64)       ,  layers_per_block=2       ,  sample_size=32       ,  in_channels=4       ,  out_channels=4       ,  down_block_types=('CrossAttnDownBlock3D', 'CrossAttnDownBlock3D', 'CrossAttnDownBlock3D', 'DownBlock3D')       ,  up_block_types=('UpBlock3D', 'CrossAttnUpBlock3D', 'CrossAttnUpBlock3D', 'CrossAttnUpBlock3D')       ,  cross_attention_dim=32       ,  attention_head_dim=4       ,  )
												lowerCAmelCase__        =  DDIMScheduler(
												    beta_start=0.0_00_85       ,  beta_end=0.0_12       ,  beta_schedule='scaled_linear'       ,  clip_sample=_UpperCamelCase       ,  set_alpha_to_one=_UpperCamelCase       ,  )
												torch.manual_seed(0				)
												lowerCAmelCase__        =  AutoencoderKL(
												    block_out_channels=[32, 64]       ,  in_channels=3       ,  out_channels=3       ,  down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']       ,  up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']       ,  latent_channels=4       ,  sample_size=1_28       ,  )
												torch.manual_seed(0				)
												lowerCAmelCase__        =  CLIPTextConfig(
												    bos_token_id=0       ,  eos_token_id=2       ,  hidden_size=32       ,  intermediate_size=37       ,  layer_norm_eps=1E-05       ,  num_attention_heads=4       ,  num_hidden_layers=5       ,  pad_token_id=1       ,  vocab_size=10_00       ,  hidden_act='gelu'       ,  projection_dim=5_12       ,  )
												lowerCAmelCase__        =  CLIPTextModel(_UpperCamelCase				)
												lowerCAmelCase__        =  CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'				)

												lowerCAmelCase__        =  {
												    'unet': unet,
												    'scheduler': scheduler,
												    'vae': vae,
												    'text_encoder': text_encoder,
												    'tokenizer': tokenizer,
												}
												return components






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase=0				):






												"""simple docstring"""
												if str(_UpperCamelCase				).startswith('mps'				):
																		lowerCAmelCase__        =  torch.manual_seed(_UpperCamelCase				)
												else:
																		lowerCAmelCase__        =  torch.Generator(device=_UpperCamelCase				).manual_seed(_UpperCamelCase				)
												lowerCAmelCase__        =  {
												    'prompt': 'A painting of a squirrel eating a burger',
												    'generator': generator,
												    'num_inference_steps': 2,
												    'guidance_scale': 6.0,
												    'output_type': 'pt',
												}
												return inputs






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  'cpu'  # ensure determinism for the device-dependent torch.Generator
												lowerCAmelCase__        =  self.get_dummy_components()
												lowerCAmelCase__        =  TextToVideoSDPipeline(**_UpperCamelCase				)
												lowerCAmelCase__        =  sd_pipe.to(_UpperCamelCase				)
												sd_pipe.set_progress_bar_config(disable=_UpperCamelCase				)

												lowerCAmelCase__        =  self.get_dummy_inputs(_UpperCamelCase				)
												lowerCAmelCase__        =  'np'
												lowerCAmelCase__        =  sd_pipe(**_UpperCamelCase				).frames
												lowerCAmelCase__        =  frames[0][-3:, -3:, -1]

												assert frames[0].shape == (64, 64, 3)
												lowerCAmelCase__        =  np.array([1_58.0, 1_60.0, 1_53.0, 1_25.0, 1_00.0, 1_21.0, 1_11.0, 93.0, 1_13.0]				)

												assert np.abs(image_slice.flatten() - expected_slice				).max() < 1E-2






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_UpperCamelCase       ,  expected_max_diff=3E-3				)






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






												"""simple docstring"""
												self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_UpperCamelCase       ,  expected_max_diff=1E-2				)






						@unittest.skip(reason='Batching needs to be properly figured out first for this pipeline.'				)
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												pass






						@unittest.skip(reason='Batching needs to be properly figured out first for this pipeline.'				)
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												pass






						@unittest.skip(reason='`num_images_per_prompt` argument is not supported for this pipeline.'				)
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												pass







						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												return super().test_progress_bar()




@slow
@skip_mps
class       __SCREAMING_SNAKE_CASE   (				unittest.TestCase):






						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  load_numpy(
												    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy'				)

												lowerCAmelCase__        =  TextToVideoSDPipeline.from_pretrained('damo-vilab/text-to-video-ms-1.7b'				)
												lowerCAmelCase__        =  DPMSolverMultistepScheduler.from_config(pipe.scheduler.config				)
												lowerCAmelCase__        =  pipe.to('cuda'				)

												lowerCAmelCase__        =  'Spiderman is surfing'
												lowerCAmelCase__        =  torch.Generator(device='cpu'				).manual_seed(0				)

												lowerCAmelCase__        =  pipe(_UpperCamelCase       ,  generator=_UpperCamelCase       ,  num_inference_steps=25       ,  output_type='pt'				).frames
												lowerCAmelCase__        =  video_frames.cpu().numpy()

												assert np.abs(expected_video - video				).mean() < 5E-2







						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												lowerCAmelCase__        =  load_numpy(
												    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy'				)

												lowerCAmelCase__        =  TextToVideoSDPipeline.from_pretrained('damo-vilab/text-to-video-ms-1.7b'				)
												lowerCAmelCase__        =  pipe.to('cuda'				)

												lowerCAmelCase__        =  'Spiderman is surfing'
												lowerCAmelCase__        =  torch.Generator(device='cpu'				).manual_seed(0				)

												lowerCAmelCase__        =  pipe(_UpperCamelCase       ,  generator=_UpperCamelCase       ,  num_inference_steps=2       ,  output_type='pt'				).frames
												lowerCAmelCase__        =  video_frames.cpu().numpy()

												assert np.abs(expected_video - video				).mean() < 5E-2





