

"""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    (A__      ):
			lowerCamelCase__ :		Union[str, Any]       		=				['image_processor', 'tokenizer']
			lowerCamelCase__ :		Optional[Any]       		=				'OwlViTImageProcessor'
			lowerCamelCase__ :		List[str]       		=				('CLIPTokenizer', 'CLIPTokenizerFast')

			def __init__(       self					:    Union[str, Any]     ,					__UpperCAmelCase					:    str=None     ,					__UpperCAmelCase					:    List[Any]=None     ,					**__UpperCAmelCase					:    Any							) ->     Union[str, Any]:
						SCREAMING_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."""     ,					__UpperCAmelCase     ,					)
									SCREAMING_SNAKE_CASE__										=   kwargs.pop("""feature_extractor"""							)

						SCREAMING_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__(__UpperCAmelCase     ,					__UpperCAmelCase							)

			def __call__(       self					:    List[str]     ,					__UpperCAmelCase					:    str=None     ,					__UpperCAmelCase					:    Any=None     ,					__UpperCAmelCase					:    int=None     ,					__UpperCAmelCase					:    Dict="max_length"     ,					__UpperCAmelCase					:    List[str]="np"     ,					**__UpperCAmelCase					:    Union[str, Any]							) ->     Optional[int]:

						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(__UpperCAmelCase     ,					__UpperCAmelCase							) or (isinstance(__UpperCAmelCase     ,					__UpperCAmelCase							) and not isinstance(text[0]     ,					__UpperCAmelCase							)):
												SCREAMING_SNAKE_CASE__										=   [self.tokenizer(__UpperCAmelCase     ,					padding=__UpperCAmelCase     ,					return_tensors=__UpperCAmelCase     ,					**__UpperCAmelCase							)]

									elif isinstance(__UpperCAmelCase     ,					__UpperCAmelCase							) and isinstance(text[0]     ,					__UpperCAmelCase							):
												SCREAMING_SNAKE_CASE__										=   []

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

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

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

									if return_tensors == "np":
												SCREAMING_SNAKE_CASE__										=   np.concatenate([encoding["""input_ids"""] for encoding in encodings]     ,					axis=0							)
												SCREAMING_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

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

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

												SCREAMING_SNAKE_CASE__										=   torch.cat([encoding["""input_ids"""] for encoding in encodings]     ,					dim=0							)
												SCREAMING_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

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

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

									SCREAMING_SNAKE_CASE__										=   BatchEncoding()
									SCREAMING_SNAKE_CASE__										=   input_ids
									SCREAMING_SNAKE_CASE__										=   attention_mask

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

						if images is not None:
									SCREAMING_SNAKE_CASE__										=   self.image_processor(__UpperCAmelCase     ,					return_tensors=__UpperCAmelCase     ,					**__UpperCAmelCase							)

						if text is not None and images is not None:
									SCREAMING_SNAKE_CASE__										=   image_features.pixel_values
									return encoding
						elif query_images is not None and images is not None:
									SCREAMING_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(**__UpperCAmelCase							)     ,					tensor_type=__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    List[str]     ,					*__UpperCAmelCase					:    Tuple     ,					**__UpperCAmelCase					:    str							) ->     Optional[int]:
						return self.image_processor.post_process(*__UpperCAmelCase     ,					**__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    int     ,					*__UpperCAmelCase					:    int     ,					**__UpperCAmelCase					:    Any							) ->     int:
						return self.image_processor.post_process_object_detection(*__UpperCAmelCase     ,					**__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    str     ,					*__UpperCAmelCase					:    List[str]     ,					**__UpperCAmelCase					:    Optional[int]							) ->     Any:
						return self.image_processor.post_process_image_guided_detection(*__UpperCAmelCase     ,					**__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Tuple     ,					*__UpperCAmelCase					:    Optional[Any]     ,					**__UpperCAmelCase					:    int							) ->     str:
						return self.tokenizer.batch_decode(*__UpperCAmelCase     ,					**__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    Tuple     ,					*__UpperCAmelCase					:    Tuple     ,					**__UpperCAmelCase					:    Union[str, Any]							) ->     Any:
						return self.tokenizer.decode(*__UpperCAmelCase     ,					**__UpperCAmelCase							)

			@property
			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[int]							) ->     str:
						warnings.warn(
						    """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""     ,					__UpperCAmelCase     ,					)
						return self.image_processor_class





			@property
			def        SCREAMING_SNAKE_CASE					(       self					:    int							) ->     List[Any]:
						warnings.warn(
						    """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."""     ,					__UpperCAmelCase     ,					)
						return self.image_processor





"""simple docstring"""




A_				:      Any           =			"0.21.0"

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
						from .utils import rich


