
import gc
import unittest

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

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference







class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_,			unittest.TestCase   ):







	'''simple docstring'''
	_lowerCAmelCase							       =				ShapEPipeline
	_lowerCAmelCase							       =				["prompt"]
	_lowerCAmelCase							       =				["prompt"]
	_lowerCAmelCase							       =				[
	    "num_images_per_prompt",
	    "num_inference_steps",
	    "generator",
	    "latents",
	    "guidance_scale",
	    "frame_size",
	    "output_type",
	    "return_dict",
	]
	_lowerCAmelCase							       =				False





	@property
	def   __snake_case				(			self		):
					return 32





	@property
	def   __snake_case				(			self		):
					return 32





	@property
	def   __snake_case				(			self		):
					return self.time_input_dim * 4





	@property
	def   __snake_case				(			self		):
					return 8





	@property
	def   __snake_case				(			self		):
					A__  :	List[Any]         =							CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''		)
					return tokenizer





	@property
	def   __snake_case				(			self		):
					torch.manual_seed(0		)
					A__  :	List[str]         =							CLIPTextConfig(
					    bos_token_id=0				,       eos_token_id=2				,       hidden_size=self.text_embedder_hidden_size				,       projection_dim=self.text_embedder_hidden_size				,       intermediate_size=37				,       layer_norm_eps=1e-05				,       num_attention_heads=4				,       num_hidden_layers=5				,       pad_token_id=1				,       vocab_size=1000				,       )
					return CLIPTextModelWithProjection(UpperCamelCase__		)





	@property
	def   __snake_case				(			self		):
					torch.manual_seed(0		)

					A__  :	Optional[int]         =							{
					    '''num_attention_heads''': 2,
					    '''attention_head_dim''': 16,
					    '''embedding_dim''': self.time_input_dim,
					    '''num_embeddings''': 32,
					    '''embedding_proj_dim''': self.text_embedder_hidden_size,
					    '''time_embed_dim''': self.time_embed_dim,
					    '''num_layers''': 1,
					    '''clip_embed_dim''': self.time_input_dim * 2,
					    '''additional_embeddings''': 0,
					    '''time_embed_act_fn''': '''gelu''',
					    '''norm_in_type''': '''layer''',
					    '''encoder_hid_proj_type''': None,
					    '''added_emb_type''': None,
					}

					A__  :	int         =							PriorTransformer(**UpperCamelCase__		)
					return model





	@property
	def   __snake_case				(			self		):
					torch.manual_seed(0		)

					A__  :	int         =							{
					    '''param_shapes''': (
					        (self.renderer_dim, 93),
					        (self.renderer_dim, 8),
					        (self.renderer_dim, 8),
					        (self.renderer_dim, 8),
					    ),
					    '''d_latent''': self.time_input_dim,
					    '''d_hidden''': self.renderer_dim,
					    '''n_output''': 12,
					    '''background''': (
					        0.1,
					        0.1,
					        0.1,
					    ),
					}
					A__  :	Any         =							ShapERenderer(**UpperCamelCase__		)
					return model





	def   __snake_case				(			self		):
					A__  :	Optional[int]         =							self.dummy_prior
					A__  :	Dict         =							self.dummy_text_encoder
					A__  :	Optional[int]         =							self.dummy_tokenizer
					A__  :	Optional[int]         =							self.dummy_renderer

					A__  :	List[str]         =							HeunDiscreteScheduler(
					    beta_schedule='''exp'''				,       num_train_timesteps=1024				,       prediction_type='''sample'''				,       use_karras_sigmas=UpperCamelCase__				,       clip_sample=UpperCamelCase__				,       clip_sample_range=1.0				,       )
					A__  :	Optional[Any]         =							{
					    '''prior''': prior,
					    '''text_encoder''': text_encoder,
					    '''tokenizer''': tokenizer,
					    '''renderer''': renderer,
					    '''scheduler''': scheduler,
					}

					return components





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__=0		):
					if str(UpperCamelCase__		).startswith('''mps'''		):
									A__  :	Optional[int]         =							torch.manual_seed(UpperCamelCase__		)
					else:
									A__  :	Union[str, Any]         =							torch.Generator(device=UpperCamelCase__		).manual_seed(UpperCamelCase__		)
					A__  :	List[Any]         =							{
					    '''prompt''': '''horse''',
					    '''generator''': generator,
					    '''num_inference_steps''': 1,
					    '''frame_size''': 32,
					    '''output_type''': '''np''',
					}
					return inputs





	def   __snake_case				(			self		):
					A__  :	Tuple         =							'''cpu'''

					A__  :	str         =							self.get_dummy_components()

					A__  :	Any         =							self.pipeline_class(**UpperCamelCase__		)
					A__  :	Any         =							pipe.to(UpperCamelCase__		)

					pipe.set_progress_bar_config(disable=UpperCamelCase__		)

					A__  :	Optional[Any]         =							pipe(**self.get_dummy_inputs(UpperCamelCase__		)		)
					A__  :	Optional[Any]         =							output.images[0]
					A__  :	List[Any]         =							image[0, -3:, -3:, -1]

					assert image.shape == (20, 32, 32, 3)

					A__  :	Optional[Any]         =							np.array(
					    [
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					        0.0_0_0_3_9_2_1_6,
					    ]		)

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





	def   __snake_case				(			self		):
					# NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
					self._test_inference_batch_consistent(batch_sizes=[1, 2]		)





	def   __snake_case				(			self		):
					A__  :	Dict         =							torch_device == '''cpu'''
					A__  :	Any         =							True

					self._test_inference_batch_single_identical(
					    batch_size=2				,       test_max_difference=UpperCamelCase__				,       relax_max_difference=UpperCamelCase__				,       )





	def   __snake_case				(			self		):
					A__  :	Dict         =							self.get_dummy_components()
					A__  :	Dict         =							self.pipeline_class(**UpperCamelCase__		)
					A__  :	Optional[int]         =							pipe.to(UpperCamelCase__		)
					pipe.set_progress_bar_config(disable=UpperCamelCase__		)

					A__  :	Optional[int]         =							1
					A__  :	List[Any]         =							2

					A__  :	Any         =							self.get_dummy_inputs(UpperCamelCase__		)

					for key in inputs.keys():
									if key in self.batch_params:
													A__  :	List[str]         =							batch_size * [inputs[key]]

					A__  :	Optional[int]         =							pipe(**UpperCamelCase__				,       num_images_per_prompt=UpperCamelCase__		)[0]

					assert images.shape[0] == batch_size * num_images_per_prompt







@slow
@require_torch_gpu
class  UpperCamelCase__		(    unittest.TestCase   ):







	'''simple docstring'''





	def   __snake_case				(			self		):
					# clean up the VRAM after each test
					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()





	def   __snake_case				(			self		):
					A__  :	str         =							load_numpy(
					    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
					    '''/shap_e/test_shap_e_np_out.npy'''		)
					A__  :	Dict         =							ShapEPipeline.from_pretrained('''openai/shap-e'''		)
					A__  :	Any         =							pipe.to(UpperCamelCase__		)
					pipe.set_progress_bar_config(disable=UpperCamelCase__		)

					A__  :	Union[str, Any]         =							torch.Generator(device=UpperCamelCase__		).manual_seed(0		)

					A__  :	str         =							pipe(
					    '''a shark'''				,       generator=UpperCamelCase__				,       guidance_scale=1_5.0				,       num_inference_steps=64				,       frame_size=64				,       output_type='''np'''				,       ).images[0]

					assert images.shape == (20, 64, 64, 3)

					assert_mean_pixel_difference(UpperCamelCase__				,       UpperCamelCase__		)

# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch

from ..models.speechta import SpeechTaForTextToSpeech, SpeechTaHifiGan, SpeechTaProcessor
from ..utils import is_datasets_available
from .base import PipelineTool


if is_datasets_available():
					from datasets import load_dataset







class  UpperCamelCase__		(    SCREAMING_SNAKE_CASE_   ):







	'''simple docstring'''
	_lowerCAmelCase							       =				"microsoft/speecht5_tts"
	_lowerCAmelCase							       =				(
	    "This is a tool that reads an English text out loud. It takes an input named `text` which should contain the "
	    "text to read (in English) and returns a waveform object containing the sound."
	)
	_lowerCAmelCase							       =				"text_reader"
	_lowerCAmelCase							       =				SpeechTaProcessor
	_lowerCAmelCase							       =				SpeechTaForTextToSpeech
	_lowerCAmelCase							       =				SpeechTaHifiGan

	_lowerCAmelCase							       =				["text"]
	_lowerCAmelCase							       =				["audio"]





	def   __snake_case				(			self		):
					if self.post_processor is None:
									A__  :	int         =							'''microsoft/speecht5_hifigan'''
					super().setup()





	def   __snake_case				(			self				,       UpperCamelCase__				,       UpperCamelCase__=None		):
					A__  :	List[Any]         =							self.pre_processor(text=UpperCamelCase__				,       return_tensors='''pt'''				,       truncation=UpperCamelCase__		)

					if speaker_embeddings is None:
									if not is_datasets_available():
													raise ImportError('''Datasets needs to be installed if not passing speaker embeddings.'''		)

									A__  :	List[Any]         =							load_dataset('''Matthijs/cmu-arctic-xvectors'''				,       split='''validation'''		)
									A__  :	Dict         =							torch.tensor(embeddings_dataset[7305]['''xvector''']		).unsqueeze(0		)

					return {"input_ids": inputs["input_ids"], "speaker_embeddings": speaker_embeddings}





	def   __snake_case				(			self				,       UpperCamelCase__		):
					with torch.no_grad():
									return self.model.generate_speech(**UpperCamelCase__		)





	def   __snake_case				(			self				,       UpperCamelCase__		):
					with torch.no_grad():
									return self.post_processor(UpperCamelCase__		).cpu().detach()