
"""simple docstring"""

import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
      import torch

      from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin

class __lowerCAmelCase		:





     def __init__(     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase=None     ,					__UpperCAmelCase="resnet50"     ,					__UpperCAmelCase=3     ,					__UpperCAmelCase=32     ,					__UpperCAmelCase=3     ,					__UpperCAmelCase=True     ,					__UpperCAmelCase=True     ,					):

      '''simple docstring'''




      __UpperCamelCase      =	parent
      __UpperCamelCase      =	out_indices if out_indices is not None else [4]
      __UpperCamelCase      =	stage_names
      __UpperCamelCase      =	out_features
      __UpperCamelCase      =	backbone
      __UpperCamelCase      =	batch_size
      __UpperCamelCase      =	image_size
      __UpperCamelCase      =	num_channels
      __UpperCamelCase      =	use_pretrained_backbone
      __UpperCamelCase      =	is_training





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)
      __UpperCamelCase      =	self.get_config()

      return config, pixel_values





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      return TimmBackboneConfig(
          image_size=self.image_size     ,					num_channels=self.num_channels     ,					out_features=self.out_features     ,					out_indices=self.out_indices     ,					stage_names=self.stage_names     ,					use_pretrained_backbone=self.use_pretrained_backbone     ,					backbone=self.backbone     ,					)





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	TimmBackbone(config=__UpperCAmelCase				)
      model.to(__UpperCAmelCase				)
      model.eval()
      with torch.no_grad():
       __UpperCamelCase      =	model(__UpperCAmelCase				)
      self.parent.assertEqual(
          result.feature_map[-1].shape     ,					(self.batch_size, model.channels[-1], 14, 14)     ,					)







     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	self.prepare_config_and_inputs()
      __UpperCamelCase  ,     __UpperCamelCase      =	config_and_inputs
      __UpperCamelCase      =	{'pixel_values': pixel_values}
      return config, inputs_dict


@require_torch
@require_timm
class __lowerCAmelCase		(				__SCREAMING_SNAKE_CASE      ,  __SCREAMING_SNAKE_CASE      ,  __SCREAMING_SNAKE_CASE      ,  unittest.TestCase      ):
     lowercase						       =						(TimmBackbone,) if is_torch_available() else ()
     lowercase						       =						{"feature-extraction": TimmBackbone} if is_torch_available() else {}
     lowercase						       =						False
     lowercase						       =						False
     lowercase						       =						False
     lowercase						       =						False





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	TimmBackboneModelTester(self				)
      __UpperCamelCase      =	ConfigTester(self     ,					config_class=__UpperCAmelCase     ,					has_text_modality=__UpperCAmelCase				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      self.config_tester.create_and_test_config_to_json_string()
      self.config_tester.create_and_test_config_to_json_file()
      self.config_tester.create_and_test_config_from_and_save_pretrained()
      self.config_tester.create_and_test_config_with_num_labels()
      self.config_tester.check_config_can_be_init_without_params()
      self.config_tester.check_config_arguments_init()





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	'resnet18'
      __UpperCamelCase      =	'microsoft/resnet-18'

      __UpperCamelCase      =	AutoBackbone.from_pretrained(__UpperCAmelCase     ,					use_timm_backbone=__UpperCAmelCase				)
      __UpperCamelCase      =	AutoBackbone.from_pretrained(__UpperCAmelCase				)

      self.assertEqual(len(timm_model.out_features				)     ,					len(transformers_model.out_features				)				)
      self.assertEqual(len(timm_model.stage_names				)     ,					len(transformers_model.stage_names				)				)
      self.assertEqual(timm_model.channels     ,					transformers_model.channels				)
      # Out indices are set to the last layer by default. For timm models, we don't know
      # the number of layers in advance, so we set it to (-1,), whereas for transformers
      # models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
      self.assertEqual(timm_model.out_indices     ,					(-1,)				)
      self.assertEqual(transformers_model.out_indices     ,					[len(timm_model.stage_names				) - 1]				)

      __UpperCamelCase      =	AutoBackbone.from_pretrained(__UpperCAmelCase     ,					use_timm_backbone=__UpperCAmelCase     ,					out_indices=[1, 2, 3]				)
      __UpperCamelCase      =	AutoBackbone.from_pretrained(__UpperCAmelCase     ,					out_indices=[1, 2, 3]				)

      self.assertEqual(timm_model.out_indices     ,					transformers_model.out_indices				)
      self.assertEqual(len(timm_model.out_features				)     ,					len(transformers_model.out_features				)				)
      self.assertEqual(timm_model.channels     ,					transformers_model.channels				)





     @unittest.skip('TimmBackbone doesn\'t support feed forward chunking'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('TimmBackbone doesn\'t have num_hidden_layers attribute'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('TimmBackbone initialization is managed on the timm side'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('TimmBackbone models doesn\'t have inputs_embeds'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('TimmBackbone models doesn\'t have inputs_embeds'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('TimmBackbone model cannot be created without specifying a backbone checkpoint'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('model weights aren\'t tied in TimmBackbone.'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('model weights aren\'t tied in TimmBackbone.'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('TimmBackbone doesn\'t have hidden size info in its configuration.'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('TimmBackbone doesn\'t support output_attentions.'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('Safetensors is not supported by timm.'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'				)
     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      pass





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase  ,     __UpperCamelCase      =	self.model_tester.prepare_config_and_inputs_for_common()

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

       __UpperCamelCase      =	['pixel_values']
       self.assertListEqual(arg_names[:1]     ,					__UpperCAmelCase				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase  ,     __UpperCamelCase      =	self.model_tester.prepare_config_and_inputs_for_common()
      __UpperCamelCase      =	True
      __UpperCamelCase      =	self.has_attentions

      # no need to test all models as different heads yield the same functionality
      __UpperCamelCase      =	self.all_model_classes[0]
      __UpperCamelCase      =	model_class(__UpperCAmelCase				)
      model.to(__UpperCAmelCase				)

      __UpperCamelCase      =	self._prepare_for_class(__UpperCAmelCase     ,					__UpperCAmelCase				)
      __UpperCamelCase      =	model(**__UpperCAmelCase				)
      __UpperCamelCase      =	outputs[0][-1]

      # Encoder-/Decoder-only models
      __UpperCamelCase      =	outputs.hidden_states[0]
      hidden_states.retain_grad()

      if self.has_attentions:
       __UpperCamelCase      =	outputs.attentions[0]
       attentions.retain_grad()

      output.flatten()[0].backward(retain_graph=__UpperCAmelCase				)

      self.assertIsNotNone(hidden_states.grad				)

      if self.has_attentions:
       self.assertIsNotNone(attentions.grad				)







     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase  ,     __UpperCamelCase      =	self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       __UpperCamelCase      =	model_class(__UpperCAmelCase				)
       model.to(__UpperCAmelCase				)
       model.eval()
       __UpperCamelCase      =	model(**__UpperCAmelCase				)

       self.assertEqual(len(result.feature_maps				)     ,					len(config.out_indices				)				)
       self.assertEqual(len(model.channels				)     ,					len(config.out_indices				)				)

       # Check output of last stage is taken if out_features=None, out_indices=None
       __UpperCamelCase      =	copy.deepcopy(__UpperCAmelCase				)
       __UpperCamelCase      =	None
       __UpperCamelCase      =	model_class(__UpperCAmelCase				)
       model.to(__UpperCAmelCase				)
       model.eval()
       __UpperCamelCase      =	model(**__UpperCAmelCase				)

       self.assertEqual(len(result.feature_maps				)     ,					1				)
       self.assertEqual(len(model.channels				)     ,					1				)

       # Check backbone can be initialized with fresh weights
       __UpperCamelCase      =	copy.deepcopy(__UpperCAmelCase				)
       __UpperCamelCase      =	False
       __UpperCamelCase      =	model_class(__UpperCAmelCase				)
       model.to(__UpperCAmelCase				)
       model.eval()
       __UpperCamelCase      =	model(**__UpperCAmelCase				)





"""simple docstring"""

import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionAttendAndExcitePipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_numpy, skip_mps, slow
from diffusers.utils.testing_utils import require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


UpperCamelCase     :    List[Any]       =			False

@skip_mps
class __lowerCAmelCase		(				__SCREAMING_SNAKE_CASE      ,  __SCREAMING_SNAKE_CASE      ,  __SCREAMING_SNAKE_CASE      ,  unittest.TestCase      ):
     lowercase						       =						StableDiffusionAttendAndExcitePipeline
     lowercase						       =						False
     lowercase						       =						TEXT_TO_IMAGE_PARAMS
     lowercase						       =						TEXT_TO_IMAGE_BATCH_PARAMS.union({"token_indices"}      )
     lowercase						       =						TEXT_TO_IMAGE_IMAGE_PARAMS
     lowercase						       =						TEXT_TO_IMAGE_IMAGE_PARAMS





     @classmethod
     def 						UpperCAmelCase (     cls				):

      '''simple docstring'''




      super().setUpClass()
      torch.use_deterministic_algorithms(__UpperCAmelCase				)





     @classmethod
     def 						UpperCAmelCase (     cls				):

      '''simple docstring'''




      super().tearDownClass()
      torch.use_deterministic_algorithms(__UpperCAmelCase				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      torch.manual_seed(0				)
      __UpperCamelCase      =	UNetaDConditionModel(
          block_out_channels=(32, 64)     ,					layers_per_block=1     ,					sample_size=32     ,					in_channels=4     ,					out_channels=4     ,					down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')     ,					up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')     ,					cross_attention_dim=32     ,					attention_head_dim=(2, 4)     ,					use_linear_projection=__UpperCAmelCase     ,					)
      __UpperCamelCase      =	DDIMScheduler(
          beta_start=0.0_0_0_8_5     ,					beta_end=0.0_1_2     ,					beta_schedule='scaled_linear'     ,					clip_sample=__UpperCAmelCase     ,					set_alpha_to_one=__UpperCAmelCase     ,					)
      torch.manual_seed(0				)
      __UpperCamelCase      =	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=128     ,					)
      torch.manual_seed(0				)
      __UpperCamelCase      =	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=1000     ,					hidden_act='gelu'     ,					projection_dim=512     ,					)
      __UpperCamelCase      =	CLIPTextModel(__UpperCAmelCase				)
      __UpperCamelCase      =	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'				)

      __UpperCamelCase      =	{
          'unet': unet,
          'scheduler': scheduler,
          'vae': vae,
          'text_encoder': text_encoder,
          'tokenizer': tokenizer,
          'safety_checker': None,
          'feature_extractor': None,
      }

      return components





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=0				):

      '''simple docstring'''




      if str(__UpperCAmelCase				).startswith('mps'				):
       __UpperCamelCase      =	torch.manual_seed(__UpperCAmelCase				)
      else:
       __UpperCamelCase      =	torch.Generator(device=__UpperCAmelCase				).manual_seed(__UpperCAmelCase				)
      __UpperCamelCase      =	__UpperCamelCase      =	{
          'prompt': 'a cat and a frog',
          'token_indices': [2, 5],
          'generator': generator,
          'num_inference_steps': 1,
          'guidance_scale': 6.0,
          'output_type': 'numpy',
          'max_iter_to_alter': 2,
          'thresholds': {0: 0.7},
      }
      return inputs





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	'cpu'

      __UpperCamelCase      =	self.get_dummy_components()
      __UpperCamelCase      =	self.pipeline_class(**__UpperCAmelCase				)
      pipe.to(__UpperCAmelCase				)
      pipe.set_progress_bar_config(disable=__UpperCAmelCase				)

      __UpperCamelCase      =	self.get_dummy_inputs(__UpperCAmelCase				)
      __UpperCamelCase      =	pipe(**__UpperCAmelCase				).images
      __UpperCamelCase      =	image[0, -3:, -3:, -1]

      self.assertEqual(image.shape     ,					(1, 64, 64, 3)				)
      __UpperCamelCase      =	np.array(
          [0.6_3_9_0_5_3_6_4, 0.6_2_8_9_7_3_0_7, 0.4_8_5_9_9_0_1_7, 0.5_1_3_3_6_2_4, 0.5_5_5_0_0_4_8, 0.4_5_7_6_9_5_1_6, 0.5_0_3_2_6_9_7_3, 0.5_0_2_3_1_3_9, 0.4_5_3_8_4_4_9_6]				)
      __UpperCamelCase      =	np.abs(image_slice.flatten() - expected_slice				).max()
      self.assertLessEqual(__UpperCAmelCase     ,					1E-3				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      super().test_cpu_offload_forward_pass(expected_max_diff=5E-4				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      self._test_inference_batch_consistent(batch_sizes=[1, 2]				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      self._test_inference_batch_single_identical(batch_size=2     ,					expected_max_diff=7E-4				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5E-4				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      super().test_save_load_local(expected_max_difference=5E-4				)







     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      super().test_save_load_optional_components(expected_max_difference=4E-4				)


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





     @classmethod
     def 						UpperCAmelCase (     cls				):

      '''simple docstring'''




      super().setUpClass()
      torch.use_deterministic_algorithms(__UpperCAmelCase				)





     @classmethod
     def 						UpperCAmelCase (     cls				):

      '''simple docstring'''




      super().tearDownClass()
      torch.use_deterministic_algorithms(__UpperCAmelCase				)





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




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







     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      __UpperCamelCase      =	torch.manual_seed(51				)

      __UpperCamelCase      =	StableDiffusionAttendAndExcitePipeline.from_pretrained(
          'CompVis/stable-diffusion-v1-4'     ,					safety_checker=__UpperCAmelCase     ,					torch_dtype=torch.floataa				)
      pipe.to('cuda'				)

      __UpperCamelCase      =	'a painting of an elephant with glasses'
      __UpperCamelCase      =	[5, 7]

      __UpperCamelCase      =	pipe(
          prompt=__UpperCAmelCase     ,					token_indices=__UpperCAmelCase     ,					guidance_scale=7.5     ,					generator=__UpperCAmelCase     ,					num_inference_steps=5     ,					max_iter_to_alter=5     ,					output_type='numpy'     ,					).images[0]

      __UpperCamelCase      =	load_numpy(
          'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy'				)
      assert np.abs((expected_image - image).max()				) < 5E-1



