



'''simple docstring'''





import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import CvtForImageClassification, CvtModel
 from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor


class 					snake_case			(UpperCamelCase ):



       def 				_a   (					self   )	->				str:
           lowercase__									=    self.config_class(**self.inputs_dict   )
           self.parent.assertTrue(hasattr(UpperCAmelCase_  ,"embed_dim"   )   )
           self.parent.assertTrue(hasattr(UpperCAmelCase_  ,"num_heads"   )   )





class 					snake_case			:



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=13  ,UpperCAmelCase_=64  ,UpperCAmelCase_=3  ,UpperCAmelCase_=[16, 48, 96]  ,UpperCAmelCase_=[1, 3, 6]  ,UpperCAmelCase_=[1, 2, 10]  ,UpperCAmelCase_=[7, 3, 3]  ,UpperCAmelCase_=[4, 2, 2]  ,UpperCAmelCase_=[2, 1, 1]  ,UpperCAmelCase_=[2, 2, 2]  ,UpperCAmelCase_=[False, False, True]  ,UpperCAmelCase_=[0.0, 0.0, 0.0]  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=1E-1_2  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=2  ,)	->				str:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    image_size
           lowercase__									=    patch_sizes
           lowercase__									=    patch_stride
           lowercase__									=    patch_padding
           lowercase__									=    is_training
           lowercase__									=    use_labels
           lowercase__									=    num_labels
           lowercase__									=    num_channels
           lowercase__									=    embed_dim
           lowercase__									=    num_heads
           lowercase__									=    stride_kv
           lowercase__									=    depth
           lowercase__									=    cls_token
           lowercase__									=    attention_drop_rate
           lowercase__									=    initializer_range
           lowercase__									=    layer_norm_eps



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

           lowercase__									=    None
           if self.use_labels:
               lowercase__									=    ids_tensor([self.batch_size]  ,self.num_labels   )

           lowercase__									=    self.get_config()
           return config, pixel_values, labels



       def 				_a   (					self   )	->				Any:
           return CvtConfig(
               image_size=self.image_size  ,num_labels=self.num_labels  ,num_channels=self.num_channels  ,embed_dim=self.embed_dim  ,num_heads=self.num_heads  ,patch_sizes=self.patch_sizes  ,patch_padding=self.patch_padding  ,patch_stride=self.patch_stride  ,stride_kv=self.stride_kv  ,depth=self.depth  ,cls_token=self.cls_token  ,attention_drop_rate=self.attention_drop_rate  ,initializer_range=self.initializer_range  ,)



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				List[str]:
           lowercase__									=    CvtModel(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(UpperCAmelCase_   )
           lowercase__									=    (self.image_size, self.image_size)
           lowercase__							,							lowercase__									=    image_size[0], image_size[1]
           for i in range(len(self.depth   )   ):
               lowercase__									=    floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1   )
               lowercase__									=    floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1   )
           self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, self.embed_dim[-1], height, width)   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				int:
           lowercase__									=    self.num_labels
           lowercase__									=    CvtForImageClassification(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)   )







       def 				_a   (					self   )	->				Dict:
           lowercase__									=    self.prepare_config_and_inputs()
           lowercase__							,							lowercase__							,							lowercase__									=    config_and_inputs
           lowercase__									=    {"pixel_values": pixel_values}
           return config, inputs_dict





@require_torch
class 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:Any     =       (CvtModel, CvtForImageClassification) if is_torch_available() else ()
       lowerCAmelCase__	:Dict     =       (
           {"feature-extraction": CvtModel, "image-classification": CvtForImageClassification}
           if is_torch_available()
           else {}
       )

       lowerCAmelCase__	:str     =       False
       lowerCAmelCase__	:Tuple     =       False
       lowerCAmelCase__	:int     =       False
       lowerCAmelCase__	:Dict     =       False
       lowerCAmelCase__	:List[str]     =       False



       def 				_a   (					self   )	->				int:
           lowercase__									=    CvtModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,has_text_modality=UpperCAmelCase_  ,hidden_size=37   )



       def 				_a   (					self   )	->				Dict:
           self.create_and_test_config_common_properties()
           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 				_a   (					self   )	->				Tuple:
           return



       @unittest.skip(reason="Cvt does not output attentions"   )
       def 				_a   (					self   )	->				int:
           pass



       @unittest.skip(reason="Cvt does not use inputs_embeds"   )
       def 				_a   (					self   )	->				int:
           pass



       @unittest.skip(reason="Cvt does not support input and output embeddings"   )
       def 				_a   (					self   )	->				Optional[int]:
           pass



       def 				_a   (					self   )	->				str:
           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__									=    ["pixel_values"]
               self.assertListEqual(arg_names[:1]  ,UpperCAmelCase_   )



       def 				_a   (					self   )	->				Tuple:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[str]:
           def check_hidden_states_output(UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   ):
               lowercase__									=    model_class(UpperCAmelCase_   )
               model.to(UpperCAmelCase_   )
               model.eval()

               with torch.no_grad():
                   lowercase__									=    model(**self._prepare_for_class(UpperCAmelCase_  ,UpperCAmelCase_   )   )

               lowercase__									=    outputs.hidden_states

               lowercase__									=    len(self.model_tester.depth   )
               self.assertEqual(len(UpperCAmelCase_   )  ,UpperCAmelCase_   )

               # verify the first hidden states (first block)
               self.assertListEqual(
                   list(hidden_states[0].shape[-3:]   )  ,[
                       self.model_tester.embed_dim[0],
                       self.model_tester.image_size // 4,
                       self.model_tester.image_size // 4,
                   ]  ,)

           lowercase__							,							lowercase__									=    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               lowercase__									=    True
               check_hidden_states_output(UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               lowercase__									=    True

               check_hidden_states_output(UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )



       def 				_a   (					self   )	->				str:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_   )



       @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."   )
       def 				_a   (					self   )	->				Dict:
           pass







       @slow
       def 				_a   (					self   )	->				Union[str, Any]:
           for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__									=    CvtModel.from_pretrained(UpperCAmelCase_   )
               self.assertIsNotNone(UpperCAmelCase_   )





def 							lowerCamelCase	(					):





    '''simple docstring'''


    lowercase__									=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
    return image



@require_torch
@require_vision
class 					snake_case			(unittest.TestCase ):



       @cached_property
       def 				_a   (					self   )	->				Union[str, Any]:
           return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )







       @slow
       def 				_a   (					self   )	->				int:
           lowercase__									=    CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   ).to(UpperCAmelCase_   )

           lowercase__									=    self.default_image_processor
           lowercase__									=    prepare_img()
           lowercase__									=    image_processor(images=UpperCAmelCase_  ,return_tensors="pt"   ).to(UpperCAmelCase_   )

           # forward pass
           with torch.no_grad():
               lowercase__									=    model(**UpperCAmelCase_   )

           # verify the logits
           lowercase__									=    torch.Size((1, 1_000)   )
           self.assertEqual(outputs.logits.shape  ,UpperCAmelCase_   )

           lowercase__									=    torch.tensor([0.92_85, 0.90_15, -0.31_50]   ).to(UpperCAmelCase_   )

           self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,UpperCAmelCase_  ,atol=1E-4   )   )









'''simple docstring'''





import gc
import unittest

from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline
from diffusers.utils import is_flax_available, load_image, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
 import jax
 import jax.numpy as jnp
 from flax.jax_utils import replicate
 from flax.training.common_utils import shard


@slow
@require_flax
class 					snake_case			(unittest.TestCase ):



       def 				_a   (					self   )	->				Tuple:
           # clean up the VRAM after each test
           super().tearDown()
           gc.collect()



       def 				_a   (					self   )	->				List[Any]:
           lowercase__							,							lowercase__									=    FlaxControlNetModel.from_pretrained(
               "lllyasviel/sd-controlnet-canny"  ,from_pt=UpperCAmelCase_  ,dtype=jnp.bfloataa   )
           lowercase__							,							lowercase__									=    FlaxStableDiffusionControlNetPipeline.from_pretrained(
               "runwayml/stable-diffusion-v1-5"  ,controlnet=UpperCAmelCase_  ,from_pt=UpperCAmelCase_  ,dtype=jnp.bfloataa   )
           lowercase__									=    controlnet_params

           lowercase__									=    "bird"
           lowercase__									=    jax.device_count()
           lowercase__									=    pipe.prepare_text_inputs([prompts] * num_samples   )

           lowercase__									=    load_image(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png"   )
           lowercase__									=    pipe.prepare_image_inputs([canny_image] * num_samples   )

           lowercase__									=    jax.random.PRNGKey(0   )
           lowercase__									=    jax.random.split(UpperCAmelCase_  ,jax.device_count()   )

           lowercase__									=    replicate(UpperCAmelCase_   )
           lowercase__									=    shard(UpperCAmelCase_   )
           lowercase__									=    shard(UpperCAmelCase_   )

           lowercase__									=    pipe(
               prompt_ids=UpperCAmelCase_  ,image=UpperCAmelCase_  ,params=UpperCAmelCase_  ,prng_seed=UpperCAmelCase_  ,num_inference_steps=50  ,jit=UpperCAmelCase_  ,).images
           assert images.shape == (jax.device_count(), 1, 768, 512, 3)

           lowercase__									=    images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]   )
           lowercase__									=    images[0, 253:256, 253:256, -1]

           lowercase__									=    jnp.asarray(jax.device_get(image_slice.flatten()   )   )
           lowercase__									=    jnp.array(
               [0.16_79_69, 0.11_66_99, 0.08_15_43, 0.15_42_97, 0.13_28_12, 0.10_88_87, 0.16_99_22, 0.16_99_22, 0.20_50_78]   )
           print(F'''output_slice: {output_slice}'''   )
           assert jnp.abs(output_slice - expected_slice   ).max() < 1E-2







       def 				_a   (					self   )	->				List[Any]:
           lowercase__							,							lowercase__									=    FlaxControlNetModel.from_pretrained(
               "lllyasviel/sd-controlnet-openpose"  ,from_pt=UpperCAmelCase_  ,dtype=jnp.bfloataa   )
           lowercase__							,							lowercase__									=    FlaxStableDiffusionControlNetPipeline.from_pretrained(
               "runwayml/stable-diffusion-v1-5"  ,controlnet=UpperCAmelCase_  ,from_pt=UpperCAmelCase_  ,dtype=jnp.bfloataa   )
           lowercase__									=    controlnet_params

           lowercase__									=    "Chef in the kitchen"
           lowercase__									=    jax.device_count()
           lowercase__									=    pipe.prepare_text_inputs([prompts] * num_samples   )

           lowercase__									=    load_image(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png"   )
           lowercase__									=    pipe.prepare_image_inputs([pose_image] * num_samples   )

           lowercase__									=    jax.random.PRNGKey(0   )
           lowercase__									=    jax.random.split(UpperCAmelCase_  ,jax.device_count()   )

           lowercase__									=    replicate(UpperCAmelCase_   )
           lowercase__									=    shard(UpperCAmelCase_   )
           lowercase__									=    shard(UpperCAmelCase_   )

           lowercase__									=    pipe(
               prompt_ids=UpperCAmelCase_  ,image=UpperCAmelCase_  ,params=UpperCAmelCase_  ,prng_seed=UpperCAmelCase_  ,num_inference_steps=50  ,jit=UpperCAmelCase_  ,).images
           assert images.shape == (jax.device_count(), 1, 768, 512, 3)

           lowercase__									=    images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]   )
           lowercase__									=    images[0, 253:256, 253:256, -1]

           lowercase__									=    jnp.asarray(jax.device_get(image_slice.flatten()   )   )
           lowercase__									=    jnp.array(
               [[0.27_14_84, 0.26_17_19, 0.27_53_91, 0.27_73_44, 0.27_92_97, 0.29_10_16, 0.29_49_22, 0.30_27_34, 0.30_27_34]]   )
           print(F'''output_slice: {output_slice}'''   )
           assert jnp.abs(output_slice - expected_slice   ).max() < 1E-2




