



from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from transformers import CLIPImageProcessor, CLIPVisionModel

from ...models import PriorTransformer
from ...pipelines import DiffusionPipeline
from ...schedulers import HeunDiscreteScheduler
from ...utils import (
    BaseOutput,
    is_accelerate_available,
    logging,
    randn_tensor,
    replace_example_docstring,
)
from .renderer import ShapERenderer


lowercase_	       =			logging.get_logger(__name__)  # pylint: disable=invalid-name

lowercase_	       =			'''
    Examples:
        ```py
        >>> from PIL import Image
        >>> import torch
        >>> from diffusers import DiffusionPipeline
        >>> from diffusers.utils import export_to_gif, load_image

        >>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

        >>> repo = "openai/shap-e-img2img"
        >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)
        >>> pipe = pipe.to(device)

        >>> guidance_scale = 3.0
        >>> image_url = "https://hf.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png"
        >>> image = load_image(image_url).convert("RGB")

        >>> images = pipe(
        ...     image,
        ...     guidance_scale=guidance_scale,
        ...     num_inference_steps=64,
        ...     frame_size=256,
        ... ).images

        >>> gif_path = export_to_gif(images[0], "corgi_3d.gif")
        ```
'''






@dataclass
class 						__a (     SCREAMING_SNAKE_CASE				):
	SCREAMING_SNAKE_CASE       =       42






class 						__a (     SCREAMING_SNAKE_CASE				):



	def __init__(						self							:    Tuple				,	snake_case_							:    PriorTransformer				,	snake_case_							:    CLIPVisionModel				,	snake_case_							:    CLIPImageProcessor				,	snake_case_							:    HeunDiscreteScheduler				,	snake_case_							:    ShapERenderer				,	)->							Dict:
						super().__init__()

						self.register_modules(
						    prior=snake_case_				,	image_encoder=snake_case_				,	image_processor=snake_case_				,	scheduler=snake_case_				,	renderer=snake_case_				,	)



	def 		UpperCamelCase							(						self							:    int				,	snake_case_							:    Optional[int]				,	snake_case_							:    Optional[int]				,	snake_case_							:    Tuple				,	snake_case_							:    int				,	snake_case_							:    List[str]				,	snake_case_							:    int)->							List[str]:
						if latents is None:
											__lowerCAmelCase	   =randn_tensor(snake_case_				,	generator=snake_case_				,	device=snake_case_				,	dtype=snake_case_)
						else:
											if latents.shape != shape:
																raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}""")
											__lowerCAmelCase	   =latents.to(snake_case_)

						__lowerCAmelCase	   =latents * scheduler.init_noise_sigma
						return latents



	def 		UpperCamelCase							(						self							:    List[str]				,	snake_case_							:    str=0)->							Optional[int]:
						if is_accelerate_available():
											from accelerate import cpu_offload
						else:
											raise ImportError("""Please install accelerate via `pip install accelerate`""")

						__lowerCAmelCase	   =torch.device(F"""cuda:{gpu_id}""")

						__lowerCAmelCase	   =[self.image_encoder, self.prior]
						for cpu_offloaded_model in models:
											if cpu_offloaded_model is not None:
																cpu_offload(snake_case_				,	snake_case_)



	@property
	def 		UpperCamelCase							(						self							:    Dict)->							List[str]:
						if self.device != torch.device("""meta""") or not hasattr(self.image_encoder				,	"""_hf_hook"""):
											return self.device
						for module in self.image_encoder.modules():
											if (
											    hasattr(snake_case_				,	"""_hf_hook""")
											    and hasattr(module._hf_hook				,	"""execution_device""")
											    and module._hf_hook.execution_device is not None
											):
																return torch.device(module._hf_hook.execution_device)
						return self.device



	def 		UpperCamelCase							(						self							:    int				,	snake_case_							:    Dict				,	snake_case_							:    Optional[Any]				,	snake_case_							:    str				,	snake_case_							:    List[str]				,	)->							str:
						if isinstance(snake_case_				,	snake_case_) and isinstance(image[0]				,	torch.Tensor):
											__lowerCAmelCase	   =torch.cat(snake_case_				,	axis=0) if image[0].ndim == 4 else torch.stack(snake_case_				,	axis=0)

						if not isinstance(snake_case_				,	torch.Tensor):
											__lowerCAmelCase	   =self.image_processor(snake_case_				,	return_tensors="""pt""").pixel_values[0].unsqueeze(0)

						__lowerCAmelCase	   =image.to(dtype=self.image_encoder.dtype				,	device=snake_case_)

						__lowerCAmelCase	   =self.image_encoder(snake_case_)["""last_hidden_state"""]
						__lowerCAmelCase	   =image_embeds[:, 1:, :].contiguous()  # batch_size, dim, 256

						__lowerCAmelCase	   =image_embeds.repeat_interleave(snake_case_				,	dim=0)

						if do_classifier_free_guidance:
											__lowerCAmelCase	   =torch.zeros_like(snake_case_)

											# For classifier free guidance, we need to do two forward passes.
											# Here we concatenate the unconditional and text embeddings into a single batch
											# to avoid doing two forward passes
											__lowerCAmelCase	   =torch.cat([negative_image_embeds, image_embeds])

						return image_embeds



	@torch.no_grad()
	@replace_example_docstring(snake_case_)
	def __call__(						self							:    Optional[int]				,	snake_case_							:    Union[PIL.Image.Image, List[PIL.Image.Image]]				,	snake_case_							:    int = 1				,	snake_case_							:    int = 25				,	snake_case_							:    Optional[Union[torch.Generator, List[torch.Generator]]] = None				,	snake_case_							:    Optional[torch.FloatTensor] = None				,	snake_case_							:    float = 4.0				,	snake_case_							:    int = 64				,	snake_case_							:    Optional[str] = "pil"				,	snake_case_							:    bool = True				,	)->							int:

						if isinstance(snake_case_				,	PIL.Image.Image):
											__lowerCAmelCase	   =1
						elif isinstance(snake_case_				,	torch.Tensor):
											__lowerCAmelCase	   =image.shape[0]
						elif isinstance(snake_case_				,	snake_case_) and isinstance(image[0]				,	(torch.Tensor, PIL.Image.Image)):
											__lowerCAmelCase	   =len(snake_case_)
						else:
											raise ValueError(
											    F"""`image` has to be of type `PIL.Image.Image`, `torch.Tensor`, `List[PIL.Image.Image]` or `List[torch.Tensor]` but is {type(snake_case_)}""")

						__lowerCAmelCase	   =self._execution_device

						__lowerCAmelCase	   =batch_size * num_images_per_prompt

						__lowerCAmelCase	   =guidance_scale > 1.0
						__lowerCAmelCase	   =self._encode_image(snake_case_				,	snake_case_				,	snake_case_				,	snake_case_)

						# prior

						self.scheduler.set_timesteps(snake_case_				,	device=snake_case_)
						__lowerCAmelCase	   =self.scheduler.timesteps

						__lowerCAmelCase	   =self.prior.config.num_embeddings
						__lowerCAmelCase	   =self.prior.config.embedding_dim

						__lowerCAmelCase	   =self.prepare_latents(
						    (batch_size, num_embeddings * embedding_dim)				,	image_embeds.dtype				,	snake_case_				,	snake_case_				,	snake_case_				,	self.scheduler				,	)

						# YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim
						__lowerCAmelCase	   =latents.reshape(latents.shape[0]				,	snake_case_				,	snake_case_)

						for i, t in enumerate(self.progress_bar(snake_case_)):
											# expand the latents if we are doing classifier free guidance
											__lowerCAmelCase	   =torch.cat([latents] * 2) if do_classifier_free_guidance else latents
											__lowerCAmelCase	   =self.scheduler.scale_model_input(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =self.prior(
											    snake_case_				,	timestep=snake_case_				,	proj_embedding=snake_case_				,	).predicted_image_embedding

											# remove the variance
											__lowerCAmelCase   ,	__lowerCAmelCase	   =noise_pred.split(
											    scaled_model_input.shape[2]				,	dim=2)  # batch_size, num_embeddings, embedding_dim

											if do_classifier_free_guidance is not None:
																__lowerCAmelCase   ,	__lowerCAmelCase	   =noise_pred.chunk(2)
																__lowerCAmelCase	   =noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond)

											__lowerCAmelCase	   =self.scheduler.step(
											    snake_case_				,	timestep=snake_case_				,	sample=snake_case_				,	).prev_sample

						if output_type == "latent":
											return ShapEPipelineOutput(images=snake_case_)

						__lowerCAmelCase	   =[]
						for i, latent in enumerate(snake_case_):
											print()
											__lowerCAmelCase	   =self.renderer.decode(
											    latent[None, :]				,	snake_case_				,	size=snake_case_				,	ray_batch_size=40_96				,	n_coarse_samples=64				,	n_fine_samples=1_28				,	)

											images.append(snake_case_)

						__lowerCAmelCase	   =torch.stack(snake_case_)

						if output_type not in ["np", "pil"]:
											raise ValueError(F"""Only the output types `pil` and `np` are supported not output_type={output_type}""")

						__lowerCAmelCase	   =images.cpu().numpy()

						if output_type == "pil":
											__lowerCAmelCase	   =[self.numpy_to_pil(snake_case_) for image in images]

						# Offload last model to CPU
						if hasattr(self				,	"""final_offload_hook""") and self.final_offload_hook is not None:
											self.final_offload_hook.offload()

						if not return_dict:
											return (images,)

						return ShapEPipelineOutput(images=snake_case_)









import time
from dataclasses import dataclass
from multiprocessing import Pool
from unittest import TestCase
from unittest.mock import patch

import multiprocess
import numpy as np
import pytest

from datasets.utils.py_utils import (
    NestedDataStructure,
    asdict,
    iflatmap_unordered,
    map_nested,
    temp_seed,
    temporary_assignment,
    zip_dict,
)

from .utils import require_tf, require_torch






def 					__lowerCAmelCase		(  __lowerCamelCase	:    List[Any]	)  ->						int:  # picklable for multiprocessing
					return x.sum()






def 					__lowerCAmelCase		(  __lowerCamelCase	:    Any	)  ->						Any:  # picklable for multiprocessing
					return i + 1






@dataclass
class 						__a :
	SCREAMING_SNAKE_CASE       =       42
	SCREAMING_SNAKE_CASE       =       42






class 						__a (     SCREAMING_SNAKE_CASE				):



	def 		UpperCamelCase							(						self							:    List[Any])->							Tuple:
						__lowerCAmelCase	   ={}
						__lowerCAmelCase	   =[]
						__lowerCAmelCase	   =1
						__lowerCAmelCase	   =[1, 2]
						__lowerCAmelCase	   ={"""a""": 1, """b""": 2}
						__lowerCAmelCase	   ={"""a""": [1, 2], """b""": [3, 4]}
						__lowerCAmelCase	   ={"""a""": {"""1""": 1}, """b""": 2}
						__lowerCAmelCase	   ={"""a""": 1, """b""": 2, """c""": 3, """d""": 4}
						__lowerCAmelCase	   ={}
						__lowerCAmelCase	   =[]
						__lowerCAmelCase	   =2
						__lowerCAmelCase	   =[2, 3]
						__lowerCAmelCase	   ={"""a""": 2, """b""": 3}
						__lowerCAmelCase	   ={"""a""": [2, 3], """b""": [4, 5]}
						__lowerCAmelCase	   ={"""a""": {"""1""": 2}, """b""": 3}
						__lowerCAmelCase	   ={"""a""": 2, """b""": 3, """c""": 4, """d""": 5}
						self.assertEqual(map_nested(snake_case_				,	snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_)				,	snake_case_)

						__lowerCAmelCase	   =2
						self.assertEqual(map_nested(snake_case_				,	snake_case_				,	num_proc=snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_				,	num_proc=snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_				,	num_proc=snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_				,	num_proc=snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_				,	num_proc=snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_				,	num_proc=snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_				,	num_proc=snake_case_)				,	snake_case_)
						self.assertEqual(map_nested(snake_case_				,	snake_case_				,	num_proc=snake_case_)				,	snake_case_)

						__lowerCAmelCase	   ={"""a""": np.eye(2), """b""": np.zeros(3), """c""": np.ones(2)}
						__lowerCAmelCase	   ={"""a""": 2, """b""": 0, """c""": 2}
						__lowerCAmelCase	   ={
						    """a""": np.eye(2).astype(snake_case_),
						    """b""": np.zeros(3).astype(snake_case_),
						    """c""": np.ones(2).astype(snake_case_),
						}
						self.assertEqual(map_nested(snake_case_				,	snake_case_				,	map_numpy=snake_case_)				,	snake_case_)
						self.assertEqual(
						    {k: v.tolist() for k, v in map_nested(snake_case_				,	snake_case_				,	map_numpy=snake_case_).items()}				,	{k: v.tolist() for k, v in expected_map_nested_sna_int.items()}				,	)
						self.assertEqual(map_nested(snake_case_				,	snake_case_				,	map_numpy=snake_case_				,	num_proc=snake_case_)				,	snake_case_)
						self.assertEqual(
						    {k: v.tolist() for k, v in map_nested(snake_case_				,	snake_case_				,	map_numpy=snake_case_				,	num_proc=snake_case_).items()}				,	{k: v.tolist() for k, v in expected_map_nested_sna_int.items()}				,	)
						with self.assertRaises(snake_case_):  # can't pickle a local lambda
											map_nested(lambda snake_case_: x + 1				,	snake_case_				,	num_proc=snake_case_)



	def 		UpperCamelCase							(						self							:    Any)->							int:
						__lowerCAmelCase	   ={"""a""": 1, """b""": 2}
						__lowerCAmelCase	   ={"""a""": 3, """b""": 4}
						__lowerCAmelCase	   ={"""a""": 5, """b""": 6}
						__lowerCAmelCase	   =sorted([("""a""", (1, 3, 5)), ("""b""", (2, 4, 6))])
						self.assertEqual(sorted(zip_dict(snake_case_				,	snake_case_				,	snake_case_))				,	snake_case_)



	def 		UpperCamelCase							(						self							:    Optional[Any])->							Optional[int]:






						class 						__a :
							SCREAMING_SNAKE_CASE       =       "bar"


						__lowerCAmelCase	   =Foo()
						self.assertEqual(foo.my_attr				,	"""bar""")
						with temporary_assignment(snake_case_				,	"""my_attr"""				,	"""BAR"""):
											self.assertEqual(foo.my_attr				,	"""BAR""")
						self.assertEqual(foo.my_attr				,	"""bar""")






@pytest.mark.parametrize(
    """iterable_length, num_proc, expected_num_proc"""				,   [
        (1, None, 1),
        (1, 1, 1),
        (2, None, 1),
        (2, 1, 1),
        (2, 2, 1),
        (2, 3, 1),
        (3, 2, 1),
        (16, 16, 16),
        (16, 17, 16),
        (17, 16, 16),
    ]				,   )
def 					__lowerCAmelCase		(  __lowerCamelCase	:    List[str]				,   __lowerCamelCase	:    Any				,   __lowerCamelCase	:    Dict	)  ->						Union[str, Any]:
					with patch("""datasets.utils.py_utils._single_map_nested"""	) as mock_single_map_nested, patch(
					    """datasets.parallel.parallel.Pool"""	) as mock_multiprocessing_pool:
										__lowerCAmelCase	   ={f"""{i}""": i for i in range(__lowerCamelCase	)}
										__lowerCAmelCase	   =map_nested(lambda __lowerCamelCase	: x + 10				,   __lowerCamelCase				,   num_proc=__lowerCamelCase				,   parallel_min_length=16	)
										if expected_num_proc == 1:
															assert mock_single_map_nested.called
															assert not mock_multiprocessing_pool.called
										else:
															assert not mock_single_map_nested.called
															assert mock_multiprocessing_pool.called
															assert mock_multiprocessing_pool.call_args[0][0] == expected_num_proc






class 						__a (     SCREAMING_SNAKE_CASE				):



	@require_tf
	def 		UpperCamelCase							(						self							:    Tuple)->							Optional[int]:
						import tensorflow as tf
						from tensorflow.keras import layers

						__lowerCAmelCase	   =layers.Dense(2)

						def gen_random_output():
											__lowerCAmelCase	   =tf.random.uniform((1, 3))
											return model(snake_case_).numpy()

						with temp_seed(42				,	set_tensorflow=snake_case_):
											__lowerCAmelCase	   =gen_random_output()
						with temp_seed(42				,	set_tensorflow=snake_case_):
											__lowerCAmelCase	   =gen_random_output()
						__lowerCAmelCase	   =gen_random_output()

						np.testing.assert_equal(snake_case_				,	snake_case_)
						self.assertGreater(np.abs(outa - outa).sum()				,	0)



	@require_torch
	def 		UpperCamelCase							(						self							:    Dict)->							Dict:
						import torch

						def gen_random_output():
											__lowerCAmelCase	   =torch.nn.Linear(3				,	2)
											__lowerCAmelCase	   =torch.rand(1				,	3)
											return model(snake_case_).detach().numpy()

						with temp_seed(42				,	set_pytorch=snake_case_):
											__lowerCAmelCase	   =gen_random_output()
						with temp_seed(42				,	set_pytorch=snake_case_):
											__lowerCAmelCase	   =gen_random_output()
						__lowerCAmelCase	   =gen_random_output()

						np.testing.assert_equal(snake_case_				,	snake_case_)
						self.assertGreater(np.abs(outa - outa).sum()				,	0)



	def 		UpperCamelCase							(						self							:    Union[str, Any])->							List[Any]:
						def gen_random_output():
											return np.random.rand(1				,	3)

						with temp_seed(42):
											__lowerCAmelCase	   =gen_random_output()
						with temp_seed(42):
											__lowerCAmelCase	   =gen_random_output()
						__lowerCAmelCase	   =gen_random_output()

						np.testing.assert_equal(snake_case_				,	snake_case_)
						self.assertGreater(np.abs(outa - outa).sum()				,	0)






@pytest.mark.parametrize("""input_data"""				,   [{}]	)
def 					__lowerCAmelCase		(  __lowerCamelCase	:    List[str]	)  ->						List[str]:
					__lowerCAmelCase	   =NestedDataStructure(__lowerCamelCase	).data
					assert output_data == input_data






@pytest.mark.parametrize(
    """data, expected_output"""				,   [
        ({}, []),
        ([], []),
        ("""foo""", ["""foo"""]),
        (["""foo""", """bar"""], ["""foo""", """bar"""]),
        ([["""foo""", """bar"""]], ["""foo""", """bar"""]),
        ([[["""foo"""], ["""bar"""]]], ["""foo""", """bar"""]),
        ([[["""foo"""], """bar"""]], ["""foo""", """bar"""]),
        ({"""a""": 1, """b""": 2}, [1, 2]),
        ({"""a""": [1, 2], """b""": [3, 4]}, [1, 2, 3, 4]),
        ({"""a""": [[1, 2]], """b""": [[3, 4]]}, [1, 2, 3, 4]),
        ({"""a""": [[1, 2]], """b""": [3, 4]}, [1, 2, 3, 4]),
        ({"""a""": [[[1], [2]]], """b""": [[[3], [4]]]}, [1, 2, 3, 4]),
        ({"""a""": [[[1], [2]]], """b""": [[3, 4]]}, [1, 2, 3, 4]),
        ({"""a""": [[[1], [2]]], """b""": [3, 4]}, [1, 2, 3, 4]),
        ({"""a""": [[[1], [2]]], """b""": [3, [4]]}, [1, 2, 3, 4]),
        ({"""a""": {"""1""": 1}, """b""": 2}, [1, 2]),
        ({"""a""": {"""1""": [1]}, """b""": 2}, [1, 2]),
        ({"""a""": {"""1""": [1]}, """b""": [2]}, [1, 2]),
    ]				,   )
def 					__lowerCAmelCase		(  __lowerCamelCase	:    List[Any]				,   __lowerCamelCase	:    int	)  ->						List[str]:
					__lowerCAmelCase	   =NestedDataStructure(__lowerCamelCase	).flatten()
					assert output == expected_output






def 					__lowerCAmelCase		(  )  ->						int:
					__lowerCAmelCase	   =A(x=1				,   y="""foobar"""	)
					__lowerCAmelCase	   ={"""x""": 1, """y""": """foobar"""}
					assert asdict(__lowerCamelCase	) == expected_output

					__lowerCAmelCase	   ={"""a""": {"""b""": A(x=10				,   y="""foo"""	)}, """c""": [A(x=20				,   y="""bar"""	)]}
					__lowerCAmelCase	   ={"""a""": {"""b""": {"""x""": 10, """y""": """foo"""}}, """c""": [{"""x""": 20, """y""": """bar"""}]}
					assert asdict(__lowerCamelCase	) == expected_output

					with pytest.raises(__lowerCamelCase	):
										asdict([1, A(x=10				,   y="""foo"""	)]	)






def 					__lowerCAmelCase		(  __lowerCamelCase	:    str	)  ->						Dict:
					return text.split()






def 					__lowerCAmelCase		(  __lowerCamelCase	:    List[Any]	)  ->						Any:
					yield (time.time(), content)
					time.sleep(2	)
					yield (time.time(), content)






def 					__lowerCAmelCase		(  )  ->						Any:
					with Pool(2	) as pool:
										__lowerCAmelCase	   =list(iflatmap_unordered(__lowerCamelCase				,   _split_text				,   kwargs_iterable=[{"""text""": """hello there"""}] * 10	)	)
										assert out.count("""hello"""	) == 10
										assert out.count("""there"""	) == 10
										assert len(__lowerCamelCase	) == 20

					# check multiprocess from pathos (uses dill for pickling)
					with multiprocess.Pool(2	) as pool:
										__lowerCAmelCase	   =list(iflatmap_unordered(__lowerCamelCase				,   _split_text				,   kwargs_iterable=[{"""text""": """hello there"""}] * 10	)	)
										assert out.count("""hello"""	) == 10
										assert out.count("""there"""	) == 10
										assert len(__lowerCamelCase	) == 20

					# check that we get items as fast as possible
					with Pool(2	) as pool:
										__lowerCAmelCase	   =[]
										for yield_time, content in iflatmap_unordered(
										    __lowerCamelCase				,   _aseconds_generator_of_aitems_with_timing				,   kwargs_iterable=[{"""content""": """a"""}, {"""content""": """b"""}]	):
															assert yield_time < time.time() + 0.1, "we should each item directly after it was yielded"
															out.append(__lowerCamelCase	)
										assert out.count("""a"""	) == 2
										assert out.count("""b"""	) == 2
										assert len(__lowerCamelCase	) == 4




