


import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image

from diffusers import (
    DDIMScheduler,
    KandinskyVaaImgaImgPipeline,
    KandinskyVaaPriorPipeline,
    UNetaDConditionModel,
    VQModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()


class        _a							(			UpperCamelCase__     ,	unittest.TestCase			):
							_lowercase	:			Union[str, Any]        = KandinskyVaaImgaImgPipeline
							_lowercase	:			Tuple        = ['''image_embeds''', '''negative_image_embeds''', '''image''']
							_lowercase	:			Any        = [
							    '''image_embeds''',
							    '''negative_image_embeds''',
							    '''image''',
							]
							_lowercase	:			Union[str, Any]        = [
							    '''generator''',
							    '''height''',
							    '''width''',
							    '''strength''',
							    '''guidance_scale''',
							    '''num_inference_steps''',
							    '''return_dict''',
							    '''guidance_scale''',
							    '''num_images_per_prompt''',
							    '''output_type''',
							    '''return_dict''',
							]
							_lowercase	:			Optional[Any]        = False

							@property
							def       lowerCamelCase_ (							self:	Union[str, Any]							)			->   Dict:





									"""simple docstring"""







									return 32

							@property
							def       lowerCamelCase_ (							self:	Optional[int]							)			->   Optional[Any]:





									"""simple docstring"""







									return 32

							@property
							def       lowerCamelCase_ (							self:	Any							)			->   Any:





									"""simple docstring"""







									return self.time_input_dim

							@property
							def       lowerCamelCase_ (							self:	Tuple							)			->   Any:





									"""simple docstring"""







									return self.time_input_dim * 4

							@property
							def       lowerCamelCase_ (							self:	List[Any]							)			->   Optional[Any]:





									"""simple docstring"""







									return 100

							@property
							def       lowerCamelCase_ (							self:	int							)			->   int:





									"""simple docstring"""







									torch.manual_seed(0							)

									lowercase__            =      {
									    '''in_channels''': 4,
									    # Out channels is double in channels because predicts mean and variance
									    '''out_channels''': 8,
									    '''addition_embed_type''': '''image''',
									    '''down_block_types''': ('''ResnetDownsampleBlock2D''', '''SimpleCrossAttnDownBlock2D'''),
									    '''up_block_types''': ('''SimpleCrossAttnUpBlock2D''', '''ResnetUpsampleBlock2D'''),
									    '''mid_block_type''': '''UNetMidBlock2DSimpleCrossAttn''',
									    '''block_out_channels''': (self.block_out_channels_a, self.block_out_channels_a * 2),
									    '''layers_per_block''': 1,
									    '''encoder_hid_dim''': self.text_embedder_hidden_size,
									    '''encoder_hid_dim_type''': '''image_proj''',
									    '''cross_attention_dim''': self.cross_attention_dim,
									    '''attention_head_dim''': 4,
									    '''resnet_time_scale_shift''': '''scale_shift''',
									    '''class_embed_type''': None,
									}

									lowercase__            =      UNetaDConditionModel(**UpperCamelCase_							)
									return model

							@property
							def       lowerCamelCase_ (							self:	Optional[int]							)			->   Union[str, Any]:





									"""simple docstring"""







									return {
									    "block_out_channels": [32, 64],
									    "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
									    "in_channels": 3,
									    "latent_channels": 4,
									    "layers_per_block": 1,
									    "norm_num_groups": 8,
									    "norm_type": "spatial",
									    "num_vq_embeddings": 12,
									    "out_channels": 3,
									    "up_block_types": [
									        "AttnUpDecoderBlock2D",
									        "UpDecoderBlock2D",
									    ],
									    "vq_embed_dim": 4,
									}

							@property
							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   int:





									"""simple docstring"""







									torch.manual_seed(0							)
									lowercase__            =      VQModel(**self.dummy_movq_kwargs							)
									return model

							def       lowerCamelCase_ (							self:	Optional[int]							)			->   Optional[int]:





									"""simple docstring"""







									lowercase__            =      self.dummy_unet
									lowercase__            =      self.dummy_movq

									lowercase__            =      {
									    '''num_train_timesteps''': 1_000,
									    '''beta_schedule''': '''linear''',
									    '''beta_start''': 0.00085,
									    '''beta_end''': 0.012,
									    '''clip_sample''': False,
									    '''set_alpha_to_one''': False,
									    '''steps_offset''': 0,
									    '''prediction_type''': '''epsilon''',
									    '''thresholding''': False,
									}

									lowercase__            =      DDIMScheduler(**UpperCamelCase_							)

									lowercase__            =      {
									    '''unet''': unet,
									    '''scheduler''': scheduler,
									    '''movq''': movq,
									}

									return components

							def       lowerCamelCase_ (							self:	Optional[int]    ,						UpperCamelCase_:	Optional[Any]    ,						UpperCamelCase_:	Optional[int]=0							)			->   Optional[int]:





									"""simple docstring"""







									lowercase__            =      floats_tensor((1, self.text_embedder_hidden_size)    ,						rng=random.Random(UpperCamelCase_							)							).to(UpperCamelCase_							)
									lowercase__            =      floats_tensor((1, self.text_embedder_hidden_size)    ,						rng=random.Random(seed + 1							)							).to(
									    UpperCamelCase_							)
									# create init_image
									lowercase__            =      floats_tensor((1, 3, 64, 64)    ,						rng=random.Random(UpperCamelCase_							)							).to(UpperCamelCase_							)
									lowercase__            =      image.cpu().permute(0    ,						2    ,						3    ,						1							)[0]
									lowercase__            =      Image.fromarray(np.uinta(UpperCamelCase_							)							).convert('''RGB'''							).resize((256, 256)							)

									if str(UpperCamelCase_							).startswith('''mps'''							):
											lowercase__            =      torch.manual_seed(UpperCamelCase_							)
									else:
											lowercase__            =      torch.Generator(device=UpperCamelCase_							).manual_seed(UpperCamelCase_							)
									lowercase__            =      {
									    '''image''': init_image,
									    '''image_embeds''': image_embeds,
									    '''negative_image_embeds''': negative_image_embeds,
									    '''generator''': generator,
									    '''height''': 64,
									    '''width''': 64,
									    '''num_inference_steps''': 10,
									    '''guidance_scale''': 7.0,
									    '''strength''': 0.2,
									    '''output_type''': '''np''',
									}
									return inputs


							def       lowerCamelCase_ (							self:	Optional[int]							)			->   Dict:





									"""simple docstring"""







									lowercase__            =      '''cpu'''

									lowercase__            =      self.get_dummy_components()

									lowercase__            =      self.pipeline_class(**UpperCamelCase_							)
									lowercase__            =      pipe.to(UpperCamelCase_							)

									pipe.set_progress_bar_config(disable=UpperCamelCase_							)

									lowercase__            =      pipe(**self.get_dummy_inputs(UpperCamelCase_							)							)
									lowercase__            =      output.images

									lowercase__            =      pipe(
									    **self.get_dummy_inputs(UpperCamelCase_							)    ,						return_dict=UpperCamelCase_    ,						)[0]

									lowercase__            =      image[0, -3:, -3:, -1]
									lowercase__            =      image_from_tuple[0, -3:, -3:, -1]

									assert image.shape == (1, 64, 64, 3)

									lowercase__            =      np.array(
									    [0.6199778, 0.63984406, 0.46145785, 0.62944984, 0.5622215, 0.47306132, 0.47441456, 0.4607606, 0.48719263]							)
									assert (
									    np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2
									), f' expected_slice {expected_slice}, but got {image_slice.flatten()}'
									assert (
									    np.abs(image_from_tuple_slice.flatten() - expected_slice							).max() < 1E-2
									), f' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}'


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

							def       lowerCamelCase_ (							self:	str							)			->   List[Any]:





									"""simple docstring"""







									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()


							def       lowerCamelCase_ (							self:	List[str]							)			->   Union[str, Any]:





									"""simple docstring"""







									lowercase__            =      load_numpy(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
									    '''/kandinskyv22/kandinskyv22_img2img_frog.npy'''							)

									lowercase__            =      load_image(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main''' '''/kandinsky/cat.png'''							)
									lowercase__            =      '''A red cartoon frog, 4k'''

									lowercase__            =      KandinskyVaaPriorPipeline.from_pretrained(
									    '''kandinsky-community/kandinsky-2-2-prior'''    ,						torch_dtype=torch.floataa							)
									pipe_prior.to(UpperCamelCase_							)

									lowercase__            =      KandinskyVaaImgaImgPipeline.from_pretrained(
									    '''kandinsky-community/kandinsky-2-2-decoder'''    ,						torch_dtype=torch.floataa							)
									lowercase__            =      pipeline.to(UpperCamelCase_							)

									pipeline.set_progress_bar_config(disable=UpperCamelCase_							)

									lowercase__            =      torch.Generator(device='''cpu'''							).manual_seed(0							)
									lowercase__     ,		lowercase__            =      pipe_prior(
									    UpperCamelCase_    ,						generator=UpperCamelCase_    ,						num_inference_steps=5    ,						negative_prompt=''''''    ,						).to_tuple()

									lowercase__            =      pipeline(
									    image=UpperCamelCase_    ,						image_embeds=UpperCamelCase_    ,						negative_image_embeds=UpperCamelCase_    ,						generator=UpperCamelCase_    ,						num_inference_steps=100    ,						height=768    ,						width=768    ,						strength=0.2    ,						output_type='''np'''    ,						)

									lowercase__            =      output.images[0]

									assert image.shape == (768, 768, 3)

									assert_mean_pixel_difference(UpperCamelCase_    ,						UpperCamelCase_							)






import inspect
import unittest

from transformers import DecisionTransformerConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import DecisionTransformerModel
			from transformers.models.decision_transformer.modeling_decision_transformer import (
			    DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
			)


class        _a							:

							def __init__(							self:	Tuple    ,						UpperCamelCase_:	int    ,						UpperCamelCase_:	Any=13    ,						UpperCamelCase_:	Optional[Any]=7    ,						UpperCamelCase_:	Optional[Any]=6    ,						UpperCamelCase_:	Any=17    ,						UpperCamelCase_:	str=23    ,						UpperCamelCase_:	List[Any]=11    ,						UpperCamelCase_:	Optional[int]=True    ,						)			->   List[Any]:





									"""simple docstring"""







									lowercase__            =      parent
									lowercase__            =      batch_size
									lowercase__            =      seq_length
									lowercase__            =      act_dim
									lowercase__            =      state_dim
									lowercase__            =      hidden_size
									lowercase__            =      max_length
									lowercase__            =      is_training

							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   Tuple:





									"""simple docstring"""







									lowercase__            =      floats_tensor((self.batch_size, self.seq_length, self.state_dim)							)
									lowercase__            =      floats_tensor((self.batch_size, self.seq_length, self.act_dim)							)
									lowercase__            =      floats_tensor((self.batch_size, self.seq_length, 1)							)
									lowercase__            =      floats_tensor((self.batch_size, self.seq_length, 1)							)
									lowercase__            =      ids_tensor((self.batch_size, self.seq_length)    ,						vocab_size=1_000							)
									lowercase__            =      random_attention_mask((self.batch_size, self.seq_length)							)

									lowercase__            =      self.get_config()

									return (
									    config,
									    states,
									    actions,
									    rewards,
									    returns_to_go,
									    timesteps,
									    attention_mask,
									)

							def       lowerCamelCase_ (							self:	Dict							)			->   List[Any]:





									"""simple docstring"""







									return DecisionTransformerConfig(
									    batch_size=self.batch_size    ,						seq_length=self.seq_length    ,						act_dim=self.act_dim    ,						state_dim=self.state_dim    ,						hidden_size=self.hidden_size    ,						max_length=self.max_length    ,						)

							def       lowerCamelCase_ (							self:	List[str]    ,						UpperCamelCase_:	int    ,						UpperCamelCase_:	List[str]    ,						UpperCamelCase_:	Any    ,						UpperCamelCase_:	List[Any]    ,						UpperCamelCase_:	Any    ,						UpperCamelCase_:	List[Any]    ,						UpperCamelCase_:	int    ,						)			->   Dict:





									"""simple docstring"""







									lowercase__            =      DecisionTransformerModel(config=UpperCamelCase_							)
									model.to(UpperCamelCase_							)
									model.eval()
									lowercase__            =      model(UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_							)

									self.parent.assertEqual(result.state_preds.shape    ,						states.shape							)
									self.parent.assertEqual(result.action_preds.shape    ,						actions.shape							)
									self.parent.assertEqual(result.return_preds.shape    ,						returns_to_go.shape							)
									self.parent.assertEqual(
									    result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length * 3, self.hidden_size)							)  # seq length *3 as there are 3 modelities: states, returns and actions


							def       lowerCamelCase_ (							self:	List[Any]							)			->   str:





									"""simple docstring"""







									lowercase__            =      self.prepare_config_and_inputs()
									(
									    (
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		(
									    lowercase__
									)     ,		
									)            =      config_and_inputs
									lowercase__            =      {
									    '''states''': states,
									    '''actions''': actions,
									    '''rewards''': rewards,
									    '''returns_to_go''': returns_to_go,
									    '''timesteps''': timesteps,
									    '''attention_mask''': attention_mask,
									}
									return config, inputs_dict


@require_torch
class        _a							(			UpperCamelCase__     ,	UpperCamelCase__     ,	UpperCamelCase__     ,	unittest.TestCase			):
							_lowercase	:			str        = (DecisionTransformerModel,) if is_torch_available() else ()
							_lowercase	:			List[str]        = ()
							_lowercase	:			List[Any]        = {'''feature-extraction''': DecisionTransformerModel} if is_torch_available() else {}

							# Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids
							_lowercase	:			Any        = False

							# Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features
							_lowercase	:			Tuple        = False
							_lowercase	:			str        = False
							_lowercase	:			Tuple        = False
							_lowercase	:			Optional[Any]        = False
							_lowercase	:			Tuple        = False
							_lowercase	:			Dict        = False
							_lowercase	:			Tuple        = False
							_lowercase	:			Optional[Any]        = False
							_lowercase	:			Optional[int]        = False

							def       lowerCamelCase_ (							self:	Union[str, Any]							)			->   Optional[Any]:





									"""simple docstring"""







									lowercase__            =      DecisionTransformerModelTester(self							)
									lowercase__            =      ConfigTester(self    ,						config_class=UpperCamelCase_    ,						hidden_size=37							)

							def       lowerCamelCase_ (							self:	Any							)			->   Optional[Any]:





									"""simple docstring"""







									self.config_tester.run_common_tests()

							def       lowerCamelCase_ (							self:	int							)			->   Optional[int]:





									"""simple docstring"""







									lowercase__            =      self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*UpperCamelCase_							)

							@slow
							def       lowerCamelCase_ (							self:	Dict							)			->   Optional[Any]:





									"""simple docstring"""







									for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowercase__            =      DecisionTransformerModel.from_pretrained(UpperCamelCase_							)
											self.assertIsNotNone(UpperCamelCase_							)


							def       lowerCamelCase_ (							self:	List[str]							)			->   Dict:





									"""simple docstring"""







									lowercase__     ,		lowercase__            =      self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											lowercase__            =      model_class(UpperCamelCase_							)
											lowercase__            =      inspect.signature(model.forward							)
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											lowercase__            =      [*signature.parameters.keys()]

											lowercase__            =      [
											    '''states''',
											    '''actions''',
											    '''rewards''',
											    '''returns_to_go''',
											    '''timesteps''',
											    '''attention_mask''',
											]

											self.assertListEqual(arg_names[: len(UpperCamelCase_							)]    ,						UpperCamelCase_							)


@require_torch
class        _a							(			unittest.TestCase			):

							@slow
							def       lowerCamelCase_ (							self:	Tuple							)			->   Any:





									"""simple docstring"""







									lowercase__            =      2  # number of steps of autoregressive prediction we will perform
									lowercase__            =      10  # defined by the RL environment, may be normalized
									lowercase__            =      DecisionTransformerModel.from_pretrained('''edbeeching/decision-transformer-gym-hopper-expert'''							)
									lowercase__            =      model.to(UpperCamelCase_							)
									lowercase__            =      model.config
									torch.manual_seed(0							)
									lowercase__            =      torch.randn(1    ,						1    ,						config.state_dim							).to(device=UpperCamelCase_    ,						dtype=torch.floataa							)  # env.reset()

									lowercase__            =      torch.tensor(
									    [[0.242793, -0.28693074, 0.8742613], [0.67815274, -0.08101085, -0.12952147]]    ,						device=UpperCamelCase_							)

									lowercase__            =      torch.tensor(UpperCamelCase_    ,						device=UpperCamelCase_    ,						dtype=torch.floataa							).reshape(1    ,						1    ,						1							)
									lowercase__            =      state
									lowercase__            =      torch.zeros(1    ,						0    ,						config.act_dim    ,						device=UpperCamelCase_    ,						dtype=torch.floataa							)
									lowercase__            =      torch.zeros(1    ,						0    ,						device=UpperCamelCase_    ,						dtype=torch.floataa							)
									lowercase__            =      torch.tensor(0    ,						device=UpperCamelCase_    ,						dtype=torch.long							).reshape(1    ,						1							)

									for step in range(UpperCamelCase_							):
											lowercase__            =      torch.cat([actions, torch.zeros(1    ,						1    ,						config.act_dim    ,						device=UpperCamelCase_							)]    ,						dim=1							)
											lowercase__            =      torch.cat([rewards, torch.zeros(1    ,						1    ,						device=UpperCamelCase_							)]    ,						dim=1							)

											lowercase__            =      torch.ones(1    ,						states.shape[1]							).to(dtype=torch.long    ,						device=states.device							)

											with torch.no_grad():
													lowercase__     ,		lowercase__     ,		lowercase__            =      model(
													    states=UpperCamelCase_    ,						actions=UpperCamelCase_    ,						rewards=UpperCamelCase_    ,						returns_to_go=UpperCamelCase_    ,						timesteps=UpperCamelCase_    ,						attention_mask=UpperCamelCase_    ,						return_dict=UpperCamelCase_    ,						)

											self.assertEqual(action_pred.shape    ,						actions.shape							)
											self.assertTrue(torch.allclose(action_pred[0, -1]    ,						expected_outputs[step]    ,						atol=1E-4							)							)
											lowercase__     ,		lowercase__     ,		lowercase__     ,		lowercase__            =      (  # env.step(action)
											    torch.randn(1    ,						1    ,						config.state_dim							).to(device=UpperCamelCase_    ,						dtype=torch.floataa							),
											    1.0,
											    False,
											    {},
											)

											lowercase__            =      action_pred[0, -1]
											lowercase__            =      torch.cat([states, state]    ,						dim=1							)
											lowercase__            =      returns_to_go[0, -1] - reward
											lowercase__            =      torch.cat([returns_to_go, pred_return.reshape(1    ,						1    ,						1							)]    ,						dim=1							)
											lowercase__            =      torch.cat(
											    [timesteps, torch.ones((1, 1)    ,						device=UpperCamelCase_    ,						dtype=torch.long							) * (step + 1)]    ,						dim=1							)


