






import functools
import math

import flax.linen as nn
import jax
import jax.numpy as jnp






def  UpperCAmelCase__						(    lowerCamelCase	:			int			,lowerCamelCase	:			Optional[Any]			,lowerCamelCase	:			int			,lowerCamelCase	:			Dict			,lowerCamelCase	:			int = 40_96    ):
   lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=key.shape[-3:]
   lowerCamelCase									=value.shape[-1]
   lowerCamelCase									=min(lowerCamelCase			,lowerCamelCase    )
   lowerCamelCase									=query / jnp.sqrt(lowerCamelCase    )

   @functools.partial(jax.checkpoint			,prevent_cse=lowerCamelCase    )
   def summarize_chunk(lowerCamelCase	:			Tuple			,lowerCamelCase	:			Dict			,lowerCamelCase	:			Optional[int]    ):
      lowerCamelCase									=jnp.einsum("""...qhd,...khd->...qhk"""			,lowerCamelCase			,lowerCamelCase			,precision=lowerCamelCase    )

      lowerCamelCase									=jnp.max(lowerCamelCase			,axis=-1			,keepdims=lowerCamelCase    )
      lowerCamelCase									=jax.lax.stop_gradient(lowerCamelCase    )
      lowerCamelCase									=jnp.exp(attn_weights - max_score    )

      lowerCamelCase									=jnp.einsum("""...vhf,...qhv->...qhf"""			,lowerCamelCase			,lowerCamelCase			,precision=lowerCamelCase    )
      lowerCamelCase									=jnp.einsum("""...qhk->...qh"""			,lowerCamelCase    )

      return (exp_values, exp_weights.sum(axis=-1    ), max_score)

   def chunk_scanner(lowerCamelCase	:			Tuple    ):
      # julienne key array
      lowerCamelCase									=jax.lax.dynamic_slice(
          operand=lowerCamelCase			,start_indices=[0] * (key.ndim - 3) + [chunk_idx, 0, 0]			,slice_sizes=list(key.shape[:-3]    ) + [key_chunk_size, num_heads, k_features]			,)

      # julienne value array
      lowerCamelCase									=jax.lax.dynamic_slice(
          operand=lowerCamelCase			,start_indices=[0] * (value.ndim - 3) + [chunk_idx, 0, 0]			,slice_sizes=list(value.shape[:-3]    ) + [key_chunk_size, num_heads, v_features]			,)

      return summarize_chunk(lowerCamelCase			,lowerCamelCase			,lowerCamelCase    )

   lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=jax.lax.map(f=lowerCamelCase			,xs=jnp.arange(0			,lowerCamelCase			,lowerCamelCase    )    )

   lowerCamelCase									=jnp.max(lowerCamelCase			,axis=0			,keepdims=lowerCamelCase    )
   lowerCamelCase									=jnp.exp(chunk_max - global_max    )

   chunk_values *= jnp.expand_dims(lowerCamelCase			,axis=-1    )
   chunk_weights *= max_diffs

   lowerCamelCase									=chunk_values.sum(axis=0    )
   lowerCamelCase									=jnp.expand_dims(lowerCamelCase			,-1    ).sum(axis=0    )

   return all_values / all_weights






def  UpperCAmelCase__						(    lowerCamelCase	:			Any			,lowerCamelCase	:			Optional[int]			,lowerCamelCase	:			Dict			,lowerCamelCase	:			Dict=jax.lax.Precision.HIGHEST			,lowerCamelCase	:			int = 10_24			,lowerCamelCase	:			int = 40_96    ):
   lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=query.shape[-3:]

   def chunk_scanner(lowerCamelCase	:			Optional[Any]			,lowerCamelCase	:			Union[str, Any]    ):
      # julienne query array
      lowerCamelCase									=jax.lax.dynamic_slice(
          operand=lowerCamelCase			,start_indices=([0] * (query.ndim - 3)) + [chunk_idx, 0, 0]			,slice_sizes=list(query.shape[:-3]    ) + [min(lowerCamelCase			,lowerCamelCase    ), num_heads, q_features]			,)

      return (
          chunk_idx + query_chunk_size,  # unused ignore it
          _query_chunk_attention(
              query=lowerCamelCase			,key=lowerCamelCase			,value=lowerCamelCase			,precision=lowerCamelCase			,key_chunk_size=lowerCamelCase    ),
      )

   lowerCamelCase				,       lowerCamelCase									=jax.lax.scan(
       f=lowerCamelCase			,init=0			,xs=lowerCamelCase			,length=math.ceil(num_q / query_chunk_size    )    )

   return jnp.concatenate(lowerCamelCase			,axis=-3    )  # fuse the chunked result back


class 		lowerCAmelCase_				(  nn.Module ):
     __lowerCamelCase       :    int
     __lowerCamelCase       :    int 	=     8
     __lowerCamelCase       :    int 	=     64
     __lowerCamelCase       :    float 	=     0.0
     __lowerCamelCase       :    bool 	=     False
     __lowerCamelCase       :    jnp.dtype 	=     jnp.floataa





     def 						lowerCamelCase							(	self  )		->				List[Any]:
        lowerCamelCase									=self.dim_head * self.heads
        lowerCamelCase									=self.dim_head**-0.5

        # Weights were exported with old names {to_q, to_k, to_v, to_out}
        lowerCamelCase									=nn.Dense(_UpperCAmelCase							, use_bias=_UpperCAmelCase							, dtype=self.dtype							, name="""to_q"""  )
        lowerCamelCase									=nn.Dense(_UpperCAmelCase							, use_bias=_UpperCAmelCase							, dtype=self.dtype							, name="""to_k"""  )
        lowerCamelCase									=nn.Dense(_UpperCAmelCase							, use_bias=_UpperCAmelCase							, dtype=self.dtype							, name="""to_v"""  )

        lowerCamelCase									=nn.Dense(self.query_dim							, dtype=self.dtype							, name="""to_out_0"""  )
        lowerCamelCase									=nn.Dropout(rate=self.dropout  )





     def 						lowerCamelCase							(	self							, _UpperCAmelCase  )		->				int:
        lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=tensor.shape
        lowerCamelCase									=self.heads
        lowerCamelCase									=tensor.reshape(_UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, dim // head_size  )
        lowerCamelCase									=jnp.transpose(_UpperCAmelCase							, (0, 2, 1, 3)  )
        lowerCamelCase									=tensor.reshape(batch_size * head_size							, _UpperCAmelCase							, dim // head_size  )
        return tensor





     def 						lowerCamelCase							(	self							, _UpperCAmelCase  )		->				int:
        lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=tensor.shape
        lowerCamelCase									=self.heads
        lowerCamelCase									=tensor.reshape(batch_size // head_size							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )
        lowerCamelCase									=jnp.transpose(_UpperCAmelCase							, (0, 2, 1, 3)  )
        lowerCamelCase									=tensor.reshape(batch_size // head_size							, _UpperCAmelCase							, dim * head_size  )
        return tensor





     def __call__(	self							, _UpperCAmelCase							, _UpperCAmelCase=None							, _UpperCAmelCase=True  )		->				int:
        lowerCamelCase									=hidden_states if context is None else context

        lowerCamelCase									=self.query(_UpperCAmelCase  )
        lowerCamelCase									=self.key(_UpperCAmelCase  )
        lowerCamelCase									=self.value(_UpperCAmelCase  )

        lowerCamelCase									=self.reshape_heads_to_batch_dim(_UpperCAmelCase  )
        lowerCamelCase									=self.reshape_heads_to_batch_dim(_UpperCAmelCase  )
        lowerCamelCase									=self.reshape_heads_to_batch_dim(_UpperCAmelCase  )

        if self.use_memory_efficient_attention:
           lowerCamelCase									=query_states.transpose(1							, 0							, 2  )
           lowerCamelCase									=key_states.transpose(1							, 0							, 2  )
           lowerCamelCase									=value_states.transpose(1							, 0							, 2  )

           # this if statement create a chunk size for each layer of the unet
           # the chunk size is equal to the query_length dimension of the deepest layer of the unet

           lowerCamelCase									=query_states.shape[-3]
           if flatten_latent_dim % 64 == 0:
              lowerCamelCase									=int(flatten_latent_dim / 64  )
           elif flatten_latent_dim % 16 == 0:
              lowerCamelCase									=int(flatten_latent_dim / 16  )
           elif flatten_latent_dim % 4 == 0:
              lowerCamelCase									=int(flatten_latent_dim / 4  )
           else:
              lowerCamelCase									=int(_UpperCAmelCase  )

           lowerCamelCase									=jax_memory_efficient_attention(
               _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, query_chunk_size=_UpperCAmelCase							, key_chunk_size=4096 * 4  )

           lowerCamelCase									=hidden_states.transpose(1							, 0							, 2  )
        else:
           # compute attentions
           lowerCamelCase									=jnp.einsum("""b i d, b j d->b i j"""							, _UpperCAmelCase							, _UpperCAmelCase  )
           lowerCamelCase									=attention_scores * self.scale
           lowerCamelCase									=nn.softmax(_UpperCAmelCase							, axis=2  )

           # attend to values
           lowerCamelCase									=jnp.einsum("""b i j, b j d -> b i d"""							, _UpperCAmelCase							, _UpperCAmelCase  )

        lowerCamelCase									=self.reshape_batch_dim_to_heads(_UpperCAmelCase  )
        lowerCamelCase									=self.proj_attn(_UpperCAmelCase  )
        return self.dropout_layer(_UpperCAmelCase							, deterministic=_UpperCAmelCase  )




class 		lowerCAmelCase_				(  nn.Module ):
     __lowerCamelCase       :    int
     __lowerCamelCase       :    int
     __lowerCamelCase       :    int
     __lowerCamelCase       :    float 	=     0.0
     __lowerCamelCase       :    bool 	=     False
     __lowerCamelCase       :    jnp.dtype 	=     jnp.floataa
     __lowerCamelCase       :    bool 	=     False





     def 						lowerCamelCase							(	self  )		->				int:
        # self attention (or cross_attention if only_cross_attention is True)
        lowerCamelCase									=FlaxAttention(
            self.dim							, self.n_heads							, self.d_head							, self.dropout							, self.use_memory_efficient_attention							, dtype=self.dtype  )
        # cross attention
        lowerCamelCase									=FlaxAttention(
            self.dim							, self.n_heads							, self.d_head							, self.dropout							, self.use_memory_efficient_attention							, dtype=self.dtype  )
        lowerCamelCase									=FlaxFeedForward(dim=self.dim							, dropout=self.dropout							, dtype=self.dtype  )
        lowerCamelCase									=nn.LayerNorm(epsilon=1E-5							, dtype=self.dtype  )
        lowerCamelCase									=nn.LayerNorm(epsilon=1E-5							, dtype=self.dtype  )
        lowerCamelCase									=nn.LayerNorm(epsilon=1E-5							, dtype=self.dtype  )
        lowerCamelCase									=nn.Dropout(rate=self.dropout  )





     def __call__(	self							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase=True  )		->				List[Any]:
        # self attention
        lowerCamelCase									=hidden_states
        if self.only_cross_attention:
           lowerCamelCase									=self.attna(self.norma(_UpperCAmelCase  )							, _UpperCAmelCase							, deterministic=_UpperCAmelCase  )
        else:
           lowerCamelCase									=self.attna(self.norma(_UpperCAmelCase  )							, deterministic=_UpperCAmelCase  )
        lowerCamelCase									=hidden_states + residual

        # cross attention
        lowerCamelCase									=hidden_states
        lowerCamelCase									=self.attna(self.norma(_UpperCAmelCase  )							, _UpperCAmelCase							, deterministic=_UpperCAmelCase  )
        lowerCamelCase									=hidden_states + residual

        # feed forward
        lowerCamelCase									=hidden_states
        lowerCamelCase									=self.ff(self.norma(_UpperCAmelCase  )							, deterministic=_UpperCAmelCase  )
        lowerCamelCase									=hidden_states + residual

        return self.dropout_layer(_UpperCAmelCase							, deterministic=_UpperCAmelCase  )




class 		lowerCAmelCase_				(  nn.Module ):
     __lowerCamelCase       :    int
     __lowerCamelCase       :    int
     __lowerCamelCase       :    int
     __lowerCamelCase       :    int 	=     1
     __lowerCamelCase       :    float 	=     0.0
     __lowerCamelCase       :    bool 	=     False
     __lowerCamelCase       :    bool 	=     False
     __lowerCamelCase       :    jnp.dtype 	=     jnp.floataa
     __lowerCamelCase       :    bool 	=     False





     def 						lowerCamelCase							(	self  )		->				Optional[Any]:
        lowerCamelCase									=nn.GroupNorm(num_groups=32							, epsilon=1E-5  )

        lowerCamelCase									=self.n_heads * self.d_head
        if self.use_linear_projection:
           lowerCamelCase									=nn.Dense(_UpperCAmelCase							, dtype=self.dtype  )
        else:
           lowerCamelCase									=nn.Conv(
               _UpperCAmelCase							, kernel_size=(1, 1)							, strides=(1, 1)							, padding="""VALID"""							, dtype=self.dtype							, )

        lowerCamelCase									=[
            FlaxBasicTransformerBlock(
                _UpperCAmelCase							, self.n_heads							, self.d_head							, dropout=self.dropout							, only_cross_attention=self.only_cross_attention							, dtype=self.dtype							, use_memory_efficient_attention=self.use_memory_efficient_attention							, )
            for _ in range(self.depth  )
        ]

        if self.use_linear_projection:
           lowerCamelCase									=nn.Dense(_UpperCAmelCase							, dtype=self.dtype  )
        else:
           lowerCamelCase									=nn.Conv(
               _UpperCAmelCase							, kernel_size=(1, 1)							, strides=(1, 1)							, padding="""VALID"""							, dtype=self.dtype							, )

        lowerCamelCase									=nn.Dropout(rate=self.dropout  )





     def __call__(	self							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase=True  )		->				Dict:
        lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=hidden_states.shape
        lowerCamelCase									=hidden_states
        lowerCamelCase									=self.norm(_UpperCAmelCase  )
        if self.use_linear_projection:
           lowerCamelCase									=hidden_states.reshape(_UpperCAmelCase							, height * width							, _UpperCAmelCase  )
           lowerCamelCase									=self.proj_in(_UpperCAmelCase  )
        else:
           lowerCamelCase									=self.proj_in(_UpperCAmelCase  )
           lowerCamelCase									=hidden_states.reshape(_UpperCAmelCase							, height * width							, _UpperCAmelCase  )

        for transformer_block in self.transformer_blocks:
           lowerCamelCase									=transformer_block(_UpperCAmelCase							, _UpperCAmelCase							, deterministic=_UpperCAmelCase  )

        if self.use_linear_projection:
           lowerCamelCase									=self.proj_out(_UpperCAmelCase  )
           lowerCamelCase									=hidden_states.reshape(_UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )
        else:
           lowerCamelCase									=hidden_states.reshape(_UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )
           lowerCamelCase									=self.proj_out(_UpperCAmelCase  )

        lowerCamelCase									=hidden_states + residual
        return self.dropout_layer(_UpperCAmelCase							, deterministic=_UpperCAmelCase  )




class 		lowerCAmelCase_				(  nn.Module ):
     __lowerCamelCase       :    int
     __lowerCamelCase       :    float 	=     0.0
     __lowerCamelCase       :    jnp.dtype 	=     jnp.floataa





     def 						lowerCamelCase							(	self  )		->				str:
        # The second linear layer needs to be called
        # net_2 for now to match the index of the Sequential layer
        lowerCamelCase									=FlaxGEGLU(self.dim							, self.dropout							, self.dtype  )
        lowerCamelCase									=nn.Dense(self.dim							, dtype=self.dtype  )





     def __call__(	self							, _UpperCAmelCase							, _UpperCAmelCase=True  )		->				Optional[Any]:
        lowerCamelCase									=self.net_a(_UpperCAmelCase							, deterministic=_UpperCAmelCase  )
        lowerCamelCase									=self.net_a(_UpperCAmelCase  )
        return hidden_states




class 		lowerCAmelCase_				(  nn.Module ):
     __lowerCamelCase       :    int
     __lowerCamelCase       :    float 	=     0.0
     __lowerCamelCase       :    jnp.dtype 	=     jnp.floataa





     def 						lowerCamelCase							(	self  )		->				str:
        lowerCamelCase									=self.dim * 4
        lowerCamelCase									=nn.Dense(inner_dim * 2							, dtype=self.dtype  )
        lowerCamelCase									=nn.Dropout(rate=self.dropout  )





     def __call__(	self							, _UpperCAmelCase							, _UpperCAmelCase=True  )		->				Dict:
        lowerCamelCase									=self.proj(_UpperCAmelCase  )
        lowerCamelCase				,       lowerCamelCase									=jnp.split(_UpperCAmelCase							, 2							, axis=2  )
        return self.dropout_layer(hidden_linear * nn.gelu(_UpperCAmelCase  )							, deterministic=_UpperCAmelCase  )







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import BlipImageProcessor


class 		lowerCAmelCase_				(  unittest.TestCase ):





     def __init__(	self							, _UpperCAmelCase							, _UpperCAmelCase=7							, _UpperCAmelCase=3							, _UpperCAmelCase=18							, _UpperCAmelCase=30							, _UpperCAmelCase=400							, _UpperCAmelCase=True							, _UpperCAmelCase=None							, _UpperCAmelCase=True							, _UpperCAmelCase=False							, _UpperCAmelCase=[0.48145466, 0.4578275, 0.40821073]							, _UpperCAmelCase=[0.26862954, 0.26130258, 0.27577711]							, _UpperCAmelCase=True							, )		->				List[str]:
        lowerCamelCase									=size if size is not None else {"""height""": 20, """width""": 20}
        lowerCamelCase									=parent
        lowerCamelCase									=batch_size
        lowerCamelCase									=num_channels
        lowerCamelCase									=image_size
        lowerCamelCase									=min_resolution
        lowerCamelCase									=max_resolution
        lowerCamelCase									=do_resize
        lowerCamelCase									=size
        lowerCamelCase									=do_normalize
        lowerCamelCase									=image_mean
        lowerCamelCase									=image_std
        lowerCamelCase									=do_pad
        lowerCamelCase									=do_convert_rgb





     def 						lowerCamelCase							(	self  )		->				str:
        return {
            "do_resize": self.do_resize,
            "size": self.size,
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_convert_rgb": self.do_convert_rgb,
            "do_pad": self.do_pad,
        }





     def 						lowerCamelCase							(	self							, _UpperCAmelCase=False							, _UpperCAmelCase=False							, _UpperCAmelCase=False  )		->				Optional[int]:
        assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

        if equal_resolution:
           lowerCamelCase									=[]
           for i in range(self.batch_size  ):
              image_inputs.append(
                  np.random.randint(
                      255							, size=(self.num_channels, self.max_resolution, self.max_resolution)							, dtype=np.uinta  )  )
        else:
           lowerCamelCase									=[]
           for i in range(self.batch_size  ):
              lowerCamelCase				,       lowerCamelCase									=np.random.choice(np.arange(self.min_resolution							, self.max_resolution  )							, 2  )
              image_inputs.append(np.random.randint(255							, size=(self.num_channels, width, height)							, dtype=np.uinta  )  )

        if not numpify and not torchify:
           # PIL expects the channel dimension as last dimension
           lowerCamelCase									=[Image.fromarray(np.moveaxis(_UpperCAmelCase							, 0							, -1  )  ) for x in image_inputs]

        if torchify:
           lowerCamelCase									=[torch.from_numpy(_UpperCAmelCase  ) for x in image_inputs]

        return image_inputs




@require_torch
@require_vision
class 		lowerCAmelCase_				(  UpperCamelCase				,     unittest.TestCase ):
     __lowerCamelCase       :    str 	=     BlipImageProcessor if is_vision_available() else None





     def 						lowerCamelCase							(	self  )		->				Dict:
        lowerCamelCase									=BlipImageProcessingTester(self  )





     @property
     def 						lowerCamelCase							(	self  )		->				Optional[int]:
        return self.image_processor_tester.prepare_image_processor_dict()





     def 						lowerCamelCase							(	self  )		->				Union[str, Any]:
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_resize"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """size"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_normalize"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """image_mean"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """image_std"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_convert_rgb"""  )  )





     def 						lowerCamelCase							(	self  )		->				Union[str, Any]:
        pass





     def 						lowerCamelCase							(	self  )		->				int:
        # Initialize image_processor
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        # create random PIL images
        lowerCamelCase									=self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase  )
        for image in image_inputs:
           self.assertIsInstance(_UpperCAmelCase							, Image.Image  )

        # Test not batched input
        lowerCamelCase									=image_processor(image_inputs[0]							, return_tensors="""pt"""  ).pixel_values
        self.assertEqual(
            encoded_images.shape							, (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )							, )

        # Test batched
        lowerCamelCase									=image_processor(_UpperCAmelCase							, return_tensors="""pt"""  ).pixel_values
        self.assertEqual(
            encoded_images.shape							, (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )							, )





     def 						lowerCamelCase							(	self  )		->				Union[str, Any]:
        # Initialize image_processor
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        # create random numpy tensors
        lowerCamelCase									=self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase							, numpify=_UpperCAmelCase  )
        for image in image_inputs:
           self.assertIsInstance(_UpperCAmelCase							, np.ndarray  )

        # Test not batched input
        lowerCamelCase									=image_processor(image_inputs[0]							, return_tensors="""pt"""  ).pixel_values
        self.assertEqual(
            encoded_images.shape							, (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )							, )

        # Test batched
        lowerCamelCase									=image_processor(_UpperCAmelCase							, return_tensors="""pt"""  ).pixel_values
        self.assertEqual(
            encoded_images.shape							, (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )							, )





     def 						lowerCamelCase							(	self  )		->				Optional[Any]:
        # Initialize image_processor
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        # create random PyTorch tensors
        lowerCamelCase									=self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase							, torchify=_UpperCAmelCase  )
        for image in image_inputs:
           self.assertIsInstance(_UpperCAmelCase							, torch.Tensor  )

        # Test not batched input
        lowerCamelCase									=image_processor(image_inputs[0]							, return_tensors="""pt"""  ).pixel_values
        self.assertEqual(
            encoded_images.shape							, (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )							, )

        # Test batched
        lowerCamelCase									=image_processor(_UpperCAmelCase							, return_tensors="""pt"""  ).pixel_values
        self.assertEqual(
            encoded_images.shape							, (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )							, )




@require_torch
@require_vision
class 		lowerCAmelCase_				(  UpperCamelCase				,     unittest.TestCase ):
     __lowerCamelCase       :    Any 	=     BlipImageProcessor if is_vision_available() else None





     def 						lowerCamelCase							(	self  )		->				Optional[int]:
        lowerCamelCase									=BlipImageProcessingTester(self							, num_channels=4  )
        lowerCamelCase									=3





     @property
     def 						lowerCamelCase							(	self  )		->				Any:
        return self.image_processor_tester.prepare_image_processor_dict()





     def 						lowerCamelCase							(	self  )		->				List[str]:
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_resize"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """size"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_normalize"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """image_mean"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """image_std"""  )  )
        self.assertTrue(hasattr(_UpperCAmelCase							, """do_convert_rgb"""  )  )





     def 						lowerCamelCase							(	self  )		->				Optional[int]:
        pass





     def 						lowerCamelCase							(	self  )		->				List[Any]:
        # Initialize image_processor
        lowerCamelCase									=self.image_processing_class(**self.image_processor_dict  )
        # create random PIL images
        lowerCamelCase									=self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCAmelCase  )
        for image in image_inputs:
           self.assertIsInstance(_UpperCAmelCase							, Image.Image  )

        # Test not batched input
        lowerCamelCase									=image_processor(image_inputs[0]							, return_tensors="""pt"""  ).pixel_values
        self.assertEqual(
            encoded_images.shape							, (
                1,
                self.expected_encoded_image_num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )							, )

        # Test batched
        lowerCamelCase									=image_processor(_UpperCAmelCase							, return_tensors="""pt"""  ).pixel_values
        self.assertEqual(
            encoded_images.shape							, (
                self.image_processor_tester.batch_size,
                self.expected_encoded_image_num_channels,
                self.image_processor_tester.size["""height"""],
                self.image_processor_tester.size["""width"""],
            )							, )