'''simple docstring'''




import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
  from PIL import Image

  from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
  import torch

if is_tf_available():
  import tensorflow as tf




@require_vision
@require_torchvision
class    a__				(       unittest.TestCase			):






     def        SCREAMING_SNAKE_CASE__       (			self		:		str   ):





         """simple docstring"""



         __lowerCamelCase          =  tempfile.mkdtemp()
         __lowerCamelCase          =  SamImageProcessor()
         __lowerCamelCase          =  SamProcessor(a   )
         processor.save_pretrained(self.tmpdirname   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[Any]  ,    **a		:		int   ):





         """simple docstring"""



         return AutoProcessor.from_pretrained(self.tmpdirname  ,    **a   ).image_processor






     def        SCREAMING_SNAKE_CASE__       (			self		:		int   ):





         """simple docstring"""



         shutil.rmtree(self.tmpdirname   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]   ):





         """simple docstring"""



         __lowerCamelCase          =  [np.random.randint(2_55  ,    size=(3, 30, 4_00)  ,    dtype=np.uinta   )]

         __lowerCamelCase          =  [Image.fromarray(np.moveaxis(a  ,    0  ,    -1   )   ) for x in image_inputs]

         return image_inputs






     def        SCREAMING_SNAKE_CASE__       (			self		:		Any   ):





         """simple docstring"""



         __lowerCamelCase          =  SamProcessor(image_processor=self.get_image_processor()   )
         processor.save_pretrained(self.tmpdirname   )

         __lowerCamelCase          =  self.get_image_processor(do_normalize=a  ,    padding_value=1.0   )

         __lowerCamelCase          =  SamProcessor.from_pretrained(self.tmpdirname  ,    do_normalize=a  ,    padding_value=1.0   )

         self.assertEqual(processor.image_processor.to_json_string()  ,    image_processor_add_kwargs.to_json_string()   )
         self.assertIsInstance(processor.image_processor  ,    a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]   ):





         """simple docstring"""



         __lowerCamelCase          =  self.get_image_processor()

         __lowerCamelCase          =  SamProcessor(image_processor=a   )

         __lowerCamelCase          =  self.prepare_image_inputs()

         __lowerCamelCase          =  image_processor(a  ,    return_tensors='''np'''   )
         __lowerCamelCase          =  processor(images=a  ,    return_tensors='''np'''   )

         input_feat_extract.pop('''original_sizes'''   )  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop('''reshaped_input_sizes'''   )  # pop original_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
             self.assertAlmostEqual(input_feat_extract[key].sum()  ,    input_processor[key].sum()  ,    delta=1e-2   )






     @require_torch
     def        SCREAMING_SNAKE_CASE__       (			self		:		List[Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  self.get_image_processor()

         __lowerCamelCase          =  SamProcessor(image_processor=a   )
         __lowerCamelCase          =  [torch.ones((1, 3, 5, 5)   )]

         __lowerCamelCase          =  [[17_64, 26_46]]

         __lowerCamelCase          =  [[6_83, 10_24]]
         __lowerCamelCase          =  processor.post_process_masks(a  ,    a  ,    a   )
         self.assertEqual(masks[0].shape  ,    (1, 3, 17_64, 26_46)   )

         __lowerCamelCase          =  processor.post_process_masks(
             a  ,    torch.tensor(a   )  ,    torch.tensor(a   )   )
         self.assertEqual(masks[0].shape  ,    (1, 3, 17_64, 26_46)   )

         # should also work with np
         __lowerCamelCase          =  [np.ones((1, 3, 5, 5)   )]
         __lowerCamelCase          =  processor.post_process_masks(a  ,    np.array(a   )  ,    np.array(a   )   )

         self.assertEqual(masks[0].shape  ,    (1, 3, 17_64, 26_46)   )

         __lowerCamelCase          =  [[1, 0], [0, 1]]
         with self.assertRaises(a   ):
             __lowerCamelCase          =  processor.post_process_masks(a  ,    np.array(a   )  ,    np.array(a   )   )




@require_vision
@require_tf
class    a__				(       unittest.TestCase			):






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  tempfile.mkdtemp()
         __lowerCamelCase          =  SamImageProcessor()
         __lowerCamelCase          =  SamProcessor(a   )
         processor.save_pretrained(self.tmpdirname   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[int]  ,    **a		:		str   ):





         """simple docstring"""



         return AutoProcessor.from_pretrained(self.tmpdirname  ,    **a   ).image_processor






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]   ):





         """simple docstring"""



         shutil.rmtree(self.tmpdirname   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Union[str, Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  [np.random.randint(2_55  ,    size=(3, 30, 4_00)  ,    dtype=np.uinta   )]

         __lowerCamelCase          =  [Image.fromarray(np.moveaxis(a  ,    0  ,    -1   )   ) for x in image_inputs]

         return image_inputs






     def        SCREAMING_SNAKE_CASE__       (			self		:		Union[str, Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  SamProcessor(image_processor=self.get_image_processor()   )
         processor.save_pretrained(self.tmpdirname   )

         __lowerCamelCase          =  self.get_image_processor(do_normalize=a  ,    padding_value=1.0   )

         __lowerCamelCase          =  SamProcessor.from_pretrained(self.tmpdirname  ,    do_normalize=a  ,    padding_value=1.0   )

         self.assertEqual(processor.image_processor.to_json_string()  ,    image_processor_add_kwargs.to_json_string()   )
         self.assertIsInstance(processor.image_processor  ,    a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		Tuple   ):





         """simple docstring"""



         __lowerCamelCase          =  self.get_image_processor()

         __lowerCamelCase          =  SamProcessor(image_processor=a   )

         __lowerCamelCase          =  self.prepare_image_inputs()

         __lowerCamelCase          =  image_processor(a  ,    return_tensors='''np'''   )
         __lowerCamelCase          =  processor(images=a  ,    return_tensors='''np'''   )

         input_feat_extract.pop('''original_sizes'''   )  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop('''reshaped_input_sizes'''   )  # pop reshaped_input_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
             self.assertAlmostEqual(input_feat_extract[key].sum()  ,    input_processor[key].sum()  ,    delta=1e-2   )






     @require_tf
     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  self.get_image_processor()

         __lowerCamelCase          =  SamProcessor(image_processor=a   )
         __lowerCamelCase          =  [tf.ones((1, 3, 5, 5)   )]

         __lowerCamelCase          =  [[17_64, 26_46]]

         __lowerCamelCase          =  [[6_83, 10_24]]
         __lowerCamelCase          =  processor.post_process_masks(a  ,    a  ,    a  ,    return_tensors='''tf'''   )
         self.assertEqual(masks[0].shape  ,    (1, 3, 17_64, 26_46)   )

         __lowerCamelCase          =  processor.post_process_masks(
             a  ,    tf.convert_to_tensor(a   )  ,    tf.convert_to_tensor(a   )  ,    return_tensors='''tf'''  ,    )
         self.assertEqual(masks[0].shape  ,    (1, 3, 17_64, 26_46)   )

         # should also work with np
         __lowerCamelCase          =  [np.ones((1, 3, 5, 5)   )]
         __lowerCamelCase          =  processor.post_process_masks(
             a  ,    np.array(a   )  ,    np.array(a   )  ,    return_tensors='''tf'''   )

         self.assertEqual(masks[0].shape  ,    (1, 3, 17_64, 26_46)   )

         __lowerCamelCase          =  [[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError   ):
             __lowerCamelCase          =  processor.post_process_masks(
                 a  ,    np.array(a   )  ,    np.array(a   )  ,    return_tensors='''tf'''   )




@require_vision
@require_torchvision
class    a__				(       unittest.TestCase			):






     def        SCREAMING_SNAKE_CASE__       (			self		:		Tuple   ):





         """simple docstring"""



         __lowerCamelCase          =  tempfile.mkdtemp()
         __lowerCamelCase          =  SamImageProcessor()
         __lowerCamelCase          =  SamProcessor(a   )
         processor.save_pretrained(self.tmpdirname   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		str  ,    **a		:		Any   ):





         """simple docstring"""



         return AutoProcessor.from_pretrained(self.tmpdirname  ,    **a   ).image_processor






     def        SCREAMING_SNAKE_CASE__       (			self		:		Optional[Any]   ):





         """simple docstring"""



         shutil.rmtree(self.tmpdirname   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		List[str]   ):





         """simple docstring"""



         __lowerCamelCase          =  [np.random.randint(2_55  ,    size=(3, 30, 4_00)  ,    dtype=np.uinta   )]

         __lowerCamelCase          =  [Image.fromarray(np.moveaxis(a  ,    0  ,    -1   )   ) for x in image_inputs]

         return image_inputs






     @is_pt_tf_cross_test
     def        SCREAMING_SNAKE_CASE__       (			self		:		Union[str, Any]   ):





         """simple docstring"""



         __lowerCamelCase          =  self.get_image_processor()

         __lowerCamelCase          =  SamProcessor(image_processor=a   )
         __lowerCamelCase          =  np.random.randint(0  ,    2  ,    size=(1, 3, 5, 5)   ).astype(np.floataa   )
         __lowerCamelCase          =  [tf.convert_to_tensor(a   )]
         __lowerCamelCase          =  [torch.tensor(a   )]

         __lowerCamelCase          =  [[17_64, 26_46]]

         __lowerCamelCase          =  [[6_83, 10_24]]
         __lowerCamelCase          =  processor.post_process_masks(
             a  ,    a  ,    a  ,    return_tensors='''tf'''   )
         __lowerCamelCase          =  processor.post_process_masks(
             a  ,    a  ,    a  ,    return_tensors='''pt'''   )

         self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()   )   )






     @is_pt_tf_cross_test
     def        SCREAMING_SNAKE_CASE__       (			self		:		int   ):





         """simple docstring"""



         __lowerCamelCase          =  self.get_image_processor()

         __lowerCamelCase          =  SamProcessor(image_processor=a   )

         __lowerCamelCase          =  self.prepare_image_inputs()

         __lowerCamelCase          =  image_processor(a  ,    return_tensors='''pt'''   )['''pixel_values'''].numpy()
         __lowerCamelCase          =  processor(images=a  ,    return_tensors='''pt'''   )['''pixel_values'''].numpy()

         __lowerCamelCase          =  image_processor(a  ,    return_tensors='''tf'''   )['''pixel_values'''].numpy()
         __lowerCamelCase          =  processor(images=a  ,    return_tensors='''tf'''   )['''pixel_values'''].numpy()

         self.assertTrue(np.allclose(a  ,    a   )   )
         self.assertTrue(np.allclose(a  ,    a   )   )
         self.assertTrue(np.allclose(a  ,    a   )   )

'''simple docstring'''




import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin




class    a__				(       UpperCAmelCase__							,       UpperCAmelCase__			):






     @register_to_config
     def __init__(			self		:		Any  ,    *,
         a		:		int = 4  ,    a		:		int = 7_68  ,    a		:		int  ,    a		:		Optional[int]  ,    ):





         """simple docstring"""



         super().__init__()

         __lowerCamelCase          =  nn.Parameter(torch.zeros(a   )   )

         # parameters for additional clip time embeddings
         __lowerCamelCase          =  nn.Linear(a  ,    a   )
         __lowerCamelCase          =  nn.Linear(a  ,    a   )

         # parameters for encoder hidden states
         __lowerCamelCase          =  clip_extra_context_tokens
         __lowerCamelCase          =  nn.Linear(
             a  ,    self.clip_extra_context_tokens * cross_attention_dim   )
         __lowerCamelCase          =  nn.Linear(a  ,    a   )
         __lowerCamelCase          =  nn.LayerNorm(a   )






     def        SCREAMING_SNAKE_CASE__       (			self		:		int  ,    *, a		:		Union[str, Any]  ,    a		:		Any  ,    a		:		str  ,    a		:		Any   ):





         """simple docstring"""



         if do_classifier_free_guidance:
             # Add the classifier free guidance embeddings to the image embeddings
             __lowerCamelCase          =  image_embeddings.shape[0]
             __lowerCamelCase          =  self.learned_classifier_free_guidance_embeddings.unsqueeze(0   )
             __lowerCamelCase          =  classifier_free_guidance_embeddings.expand(
                 a  ,    -1   )
             __lowerCamelCase          =  torch.cat([classifier_free_guidance_embeddings, image_embeddings]  ,    dim=0   )

         # The image embeddings batch size and the text embeddings batch size are equal
         assert image_embeddings.shape[0] == prompt_embeds.shape[0]

         __lowerCamelCase          =  prompt_embeds.shape[0]

         # "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
         # adding CLIP embeddings to the existing timestep embedding, ...
         __lowerCamelCase          =  self.embedding_proj(a   )
         __lowerCamelCase          =  self.clip_image_embeddings_project_to_time_embeddings(a   )
         __lowerCamelCase          =  time_projected_image_embeddings + time_projected_prompt_embeds

         # ... and by projecting CLIP embeddings into four
         # extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
         __lowerCamelCase          =  self.clip_extra_context_tokens_proj(a   )
         __lowerCamelCase          =  clip_extra_context_tokens.reshape(a  ,    -1  ,    self.clip_extra_context_tokens   )
         __lowerCamelCase          =  clip_extra_context_tokens.permute(0  ,    2  ,    1   )

         __lowerCamelCase          =  self.encoder_hidden_states_proj(a   )
         __lowerCamelCase          =  self.text_encoder_hidden_states_norm(a   )
         __lowerCamelCase          =  torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]  ,    dim=1   )

         return text_encoder_hidden_states, additive_clip_time_embeddings
