




'''simple docstring'''



import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available





class      lowerCAmelCase (			_snake_case):
       __lowercase	:   Optional[Any]										=      ['''image_processor''', '''tokenizer''']
       __lowercase	:   List[Any]										=      '''OwlViTImageProcessor'''
       __lowercase	:   List[Any]										=      ('''CLIPTokenizer''', '''CLIPTokenizerFast''')


       def __init__( self     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None     ,	**__SCREAMING_SNAKE_CASE	)   ->	Tuple:



            '''simple docstring'''




            __snake_case            =	None
            if "feature_extractor" in kwargs:
                 warnings.warn(
                     '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
                     ''' instead.'''     ,	__lowerCamelCase     ,	)
                 __snake_case            =	kwargs.pop('''feature_extractor'''	)

            __snake_case            =	image_processor if image_processor is not None else feature_extractor
            if image_processor is None:
                 raise ValueError('''You need to specify an `image_processor`.'''	)
            if tokenizer is None:
                 raise ValueError('''You need to specify a `tokenizer`.'''	)

            super().__init__(__lowerCamelCase     ,	__lowerCamelCase	)


       def __call__( self     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE=None     ,	__SCREAMING_SNAKE_CASE="max_length"     ,	__SCREAMING_SNAKE_CASE="np"     ,	**__SCREAMING_SNAKE_CASE	)   ->	Dict:



            '''simple docstring'''




            if text is None and query_images is None and images is None:
                 raise ValueError(
                     '''You have to specify at least one text or query image or image. All three cannot be none.'''	)

            if text is not None:
                 if isinstance(__lowerCamelCase     ,	__lowerCamelCase	) or (isinstance(__lowerCamelCase     ,	__lowerCamelCase	) and not isinstance(text[0]     ,	__lowerCamelCase	)):
                      __snake_case            =	[self.tokenizer(__lowerCamelCase     ,	padding=__lowerCamelCase     ,	return_tensors=__lowerCamelCase     ,	**__lowerCamelCase	)]

                 elif isinstance(__lowerCamelCase     ,	__lowerCamelCase	) and isinstance(text[0]     ,	__lowerCamelCase	):
                      __snake_case            =	[]

                      # Maximum number of queries across batch
                      __snake_case            =	max([len(__lowerCamelCase	) for t in text]	)

                      # Pad all batch samples to max number of text queries
                      for t in text:
                           if len(__lowerCamelCase	) != max_num_queries:
                                __snake_case            =	t + [''' '''] * (max_num_queries - len(__lowerCamelCase	))

                           __snake_case            =	self.tokenizer(__lowerCamelCase     ,	padding=__lowerCamelCase     ,	return_tensors=__lowerCamelCase     ,	**__lowerCamelCase	)
                           encodings.append(__lowerCamelCase	)
                 else:
                      raise TypeError('''Input text should be a string, a list of strings or a nested list of strings'''	)

                 if return_tensors == "np":
                      __snake_case            =	np.concatenate([encoding['''input_ids'''] for encoding in encodings]     ,	axis=0	)
                      __snake_case            =	np.concatenate([encoding['''attention_mask'''] for encoding in encodings]     ,	axis=0	)

                 elif return_tensors == "jax" and is_flax_available():
                      import jax.numpy as jnp

                      __snake_case            =	jnp.concatenate([encoding['''input_ids'''] for encoding in encodings]     ,	axis=0	)
                      __snake_case            =	jnp.concatenate([encoding['''attention_mask'''] for encoding in encodings]     ,	axis=0	)

                 elif return_tensors == "pt" and is_torch_available():
                      import torch

                      __snake_case            =	torch.cat([encoding['''input_ids'''] for encoding in encodings]     ,	dim=0	)
                      __snake_case            =	torch.cat([encoding['''attention_mask'''] for encoding in encodings]     ,	dim=0	)

                 elif return_tensors == "tf" and is_tf_available():
                      import tensorflow as tf

                      __snake_case            =	tf.stack([encoding['''input_ids'''] for encoding in encodings]     ,	axis=0	)
                      __snake_case            =	tf.stack([encoding['''attention_mask'''] for encoding in encodings]     ,	axis=0	)

                 else:
                      raise ValueError('''Target return tensor type could not be returned'''	)

                 __snake_case            =	BatchEncoding()
                 __snake_case            =	input_ids
                 __snake_case            =	attention_mask

            if query_images is not None:
                 __snake_case            =	BatchEncoding()
                 __snake_case            =	self.image_processor(
                     __lowerCamelCase     ,	return_tensors=__lowerCamelCase     ,	**__lowerCamelCase	).pixel_values
                 __snake_case            =	query_pixel_values

            if images is not None:
                 __snake_case            =	self.image_processor(__lowerCamelCase     ,	return_tensors=__lowerCamelCase     ,	**__lowerCamelCase	)

            if text is not None and images is not None:
                 __snake_case            =	image_features.pixel_values
                 return encoding
            elif query_images is not None and images is not None:
                 __snake_case            =	image_features.pixel_values
                 return encoding
            elif text is not None or query_images is not None:
                 return encoding
            else:
                 return BatchEncoding(data=dict(**__lowerCamelCase	)     ,	tensor_type=__lowerCamelCase	)


       def    lowerCAmelCase   ( self     ,	*__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)   ->	Dict:



            '''simple docstring'''




            return self.image_processor.post_process(*__lowerCamelCase     ,	**__lowerCamelCase	)


       def    lowerCAmelCase   ( self     ,	*__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)   ->	Any:



            '''simple docstring'''




            return self.image_processor.post_process_object_detection(*__lowerCamelCase     ,	**__lowerCamelCase	)


       def    lowerCAmelCase   ( self     ,	*__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)   ->	List[Any]:



            '''simple docstring'''




            return self.image_processor.post_process_image_guided_detection(*__lowerCamelCase     ,	**__lowerCamelCase	)


       def    lowerCAmelCase   ( self     ,	*__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)   ->	Tuple:



            '''simple docstring'''




            return self.tokenizer.batch_decode(*__lowerCamelCase     ,	**__lowerCamelCase	)


       def    lowerCAmelCase   ( self     ,	*__SCREAMING_SNAKE_CASE     ,	**__SCREAMING_SNAKE_CASE	)   ->	List[str]:



            '''simple docstring'''




            return self.tokenizer.decode(*__lowerCamelCase     ,	**__lowerCamelCase	)


       @property
       def    lowerCAmelCase   ( self	)   ->	Tuple:



            '''simple docstring'''




            warnings.warn(
                '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''     ,	__lowerCamelCase     ,	)
            return self.image_processor_class




       @property
       def    lowerCAmelCase   ( self	)   ->	Any:



            '''simple docstring'''




            warnings.warn(
                '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''     ,	__lowerCamelCase     ,	)
            return self.image_processor










'''simple docstring'''





from typing import Optional

from torch import nn

from .transformer_ad import TransformeraDModel, TransformeraDModelOutput




class 				UpperCAmelCase			(			nn.Module						):


 def __init__(      self							:	int			,       __lowerCamelCase							:	int = 1_6			,       __lowerCamelCase							:	int = 8_8			,       __lowerCamelCase							:	Optional[int] = None			,       __lowerCamelCase							:	int = 1			,       __lowerCamelCase							:	float = 0.0			,       __lowerCamelCase							:	int = 3_2			,       __lowerCamelCase							:	Optional[int] = None			,       __lowerCamelCase							:	bool = False			,       __lowerCamelCase							:	Optional[int] = None			,       __lowerCamelCase							:	Optional[int] = None			,       __lowerCamelCase							:	str = "geglu"			,       __lowerCamelCase							:	Optional[int] = None			,       ):
    super().__init__()
    UpperCAmelCase__	:Optional[int]      =      nn.ModuleList(
        [
            TransformeraDModel(
                num_attention_heads=__lowerCamelCase			,       attention_head_dim=__lowerCamelCase			,       in_channels=__lowerCamelCase			,       num_layers=__lowerCamelCase			,       dropout=__lowerCamelCase			,       norm_num_groups=__lowerCamelCase			,       cross_attention_dim=__lowerCamelCase			,       attention_bias=__lowerCamelCase			,       sample_size=__lowerCamelCase			,       num_vector_embeds=__lowerCamelCase			,       activation_fn=__lowerCamelCase			,       num_embeds_ada_norm=__lowerCamelCase			,       )
            for _ in range(2 )
        ] )

    # Variables that can be set by a pipeline:

    # The ratio of transformer1 to transformer2's output states to be combined during inference
    UpperCAmelCase__	:Tuple      =      0.5

    # The shape of `encoder_hidden_states` is expected to be
    # `(batch_size, condition_lengths[0]+condition_lengths[1], num_features)`
    UpperCAmelCase__	:int      =      [7_7, 2_5_7]

    # Which transformer to use to encode which condition.
    # E.g. `(1, 0)` means that we'll use `transformers[1](conditions[0])` and `transformers[0](conditions[1])`
    UpperCAmelCase__	:Any      =      [1, 0]





 def        __SCREAMING_SNAKE_CASE					(      self							:	str			,       __lowerCamelCase							:	Optional[int]			,       __lowerCamelCase							:	Any			,       __lowerCamelCase							:	int=None			,       __lowerCamelCase							:	Dict=None			,       __lowerCamelCase							:	Dict=None			,       __lowerCamelCase							:	bool = True			,       ):
    UpperCAmelCase__	:Optional[int]      =      hidden_states

    UpperCAmelCase__	:Dict      =      []
    UpperCAmelCase__	:Tuple      =      0
    # attention_mask is not used yet
    for i in range(2 ):
       # for each of the two transformers, pass the corresponding condition tokens
       UpperCAmelCase__	:List[Any]      =      encoder_hidden_states[:, tokens_start : tokens_start + self.condition_lengths[i]]
       UpperCAmelCase__	:Dict      =      self.transformer_index_for_condition[i]
       UpperCAmelCase__	:Tuple      =      self.transformers[transformer_index](
           __lowerCamelCase			,       encoder_hidden_states=__lowerCamelCase			,       timestep=__lowerCamelCase			,       cross_attention_kwargs=__lowerCamelCase			,       return_dict=__lowerCamelCase			,       )[0]
       encoded_states.append(encoded_state - input_states )
       tokens_start += self.condition_lengths[i]

    UpperCAmelCase__	:Optional[int]      =      encoded_states[0] * self.mix_ratio + encoded_states[1] * (1 - self.mix_ratio)
    UpperCAmelCase__	:Optional[Any]      =      output_states + input_states

    if not return_dict:
       return (output_states,)

    return TransformeraDModelOutput(sample=__lowerCamelCase )



