




"""simple docstring"""




import copy
import os
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union


if TYPE_CHECKING:
					from ...processing_utils import ProcessorMixin
					from ...utils import TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase							:			int     							=							logging.get_logger(__name__)

lowerCAmelCase							:			Tuple     							=							{
    """google/owlvit-base-patch32""": """https://huggingface.co/google/owlvit-base-patch32/resolve/main/config.json""",
    """google/owlvit-base-patch16""": """https://huggingface.co/google/owlvit-base-patch16/resolve/main/config.json""",
    """google/owlvit-large-patch14""": """https://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json""",
}






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''
				__UpperCamelCase			  =						"owlvit_text_model"





				def __init__(       self		, _a=49_408		, _a=512		, _a=2_048		, _a=12		, _a=8		, _a=16		, _a="quick_gelu"		, _a=1e-5		, _a=0.0		, _a=0.02		, _a=1.0		, _a=0		, _a=49_406		, _a=49_407		, **_a		, ):





							"""simple docstring"""
							super().__init__(pad_token_id=_a		, bos_token_id=_a		, eos_token_id=_a		, **_a )

							lowerCamelCase			      =					vocab_size
							lowerCamelCase			      =					hidden_size
							lowerCamelCase			      =					intermediate_size
							lowerCamelCase			      =					num_hidden_layers
							lowerCamelCase			      =					num_attention_heads
							lowerCamelCase			      =					max_position_embeddings
							lowerCamelCase			      =					hidden_act
							lowerCamelCase			      =					layer_norm_eps
							lowerCamelCase			      =					attention_dropout
							lowerCamelCase			      =					initializer_range
							lowerCamelCase			      =					initializer_factor





				@classmethod
				def    _lowerCAmelCase				(       cls		, _a		, **_a ):





							"""simple docstring"""
							cls._set_token_in_kwargs(_a )

							lowerCamelCase      ,   lowerCamelCase			      =					cls.get_config_dict(_a		, **_a )

							# get the text config dict if we are loading from OwlViTConfig
							if config_dict.get("""model_type""" ) == "owlvit":
										lowerCamelCase			      =					config_dict["""text_config"""]

							if "model_type" in config_dict and hasattr(cls		, """model_type""" ) and config_dict["model_type"] != cls.model_type:
										logger.warning(
										    f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
										    f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.' )

							return cls.from_dict(_a		, **_a )






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''
				__UpperCamelCase			  =						"owlvit_vision_model"





				def __init__(       self		, _a=768		, _a=3_072		, _a=12		, _a=12		, _a=3		, _a=768		, _a=32		, _a="quick_gelu"		, _a=1e-5		, _a=0.0		, _a=0.02		, _a=1.0		, **_a		, ):





							"""simple docstring"""
							super().__init__(**_a )

							lowerCamelCase			      =					hidden_size
							lowerCamelCase			      =					intermediate_size
							lowerCamelCase			      =					num_hidden_layers
							lowerCamelCase			      =					num_attention_heads
							lowerCamelCase			      =					num_channels
							lowerCamelCase			      =					image_size
							lowerCamelCase			      =					patch_size
							lowerCamelCase			      =					hidden_act
							lowerCamelCase			      =					layer_norm_eps
							lowerCamelCase			      =					attention_dropout
							lowerCamelCase			      =					initializer_range
							lowerCamelCase			      =					initializer_factor





				@classmethod
				def    _lowerCAmelCase				(       cls		, _a		, **_a ):





							"""simple docstring"""
							cls._set_token_in_kwargs(_a )

							lowerCamelCase      ,   lowerCamelCase			      =					cls.get_config_dict(_a		, **_a )

							# get the vision config dict if we are loading from OwlViTConfig
							if config_dict.get("""model_type""" ) == "owlvit":
										lowerCamelCase			      =					config_dict["""vision_config"""]

							if "model_type" in config_dict and hasattr(cls		, """model_type""" ) and config_dict["model_type"] != cls.model_type:
										logger.warning(
										    f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
										    f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.' )

							return cls.from_dict(_a		, **_a )






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''
				__UpperCamelCase			  =						"owlvit"
				__UpperCamelCase			  =						True





				def __init__(       self		, _a=None		, _a=None		, _a=512		, _a=2.6_592		, _a=True		, **_a		, ):





							"""simple docstring"""
							super().__init__(**_a )

							if text_config is None:
										lowerCamelCase			      =					{}
										logger.info("""text_config is None. Initializing the OwlViTTextConfig with default values.""" )

							if vision_config is None:
										lowerCamelCase			      =					{}
										logger.info("""vision_config is None. initializing the OwlViTVisionConfig with default values.""" )

							lowerCamelCase			      =					OwlViTTextConfig(**_a )
							lowerCamelCase			      =					OwlViTVisionConfig(**_a )

							lowerCamelCase			      =					projection_dim
							lowerCamelCase			      =					logit_scale_init_value
							lowerCamelCase			      =					return_dict
							lowerCamelCase			      =					1.0





				@classmethod
				def    _lowerCAmelCase				(       cls		, _a		, **_a ):





							"""simple docstring"""
							cls._set_token_in_kwargs(_a )

							lowerCamelCase      ,   lowerCamelCase			      =					cls.get_config_dict(_a		, **_a )

							if "model_type" in config_dict and hasattr(cls		, """model_type""" ) and config_dict["model_type"] != cls.model_type:
										logger.warning(
										    f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
										    f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.' )

							return cls.from_dict(_a		, **_a )





				@classmethod
				def    _lowerCAmelCase				(       cls		, _a		, _a		, **_a ):





							"""simple docstring"""
							lowerCamelCase			      =					{}
							lowerCamelCase			      =					text_config
							lowerCamelCase			      =					vision_config

							return cls.from_dict(_a		, **_a )





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					copy.deepcopy(self.__dict__ )
							lowerCamelCase			      =					self.text_config.to_dict()
							lowerCamelCase			      =					self.vision_config.to_dict()
							lowerCamelCase			      =					self.__class__.model_type
							return output






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return OrderedDict(
							    [
							        ("""input_ids""", {0: """batch""", 1: """sequence"""}),
							        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
							        ("""attention_mask""", {0: """batch""", 1: """sequence"""}),
							    ] )





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return OrderedDict(
							    [
							        ("""logits_per_image""", {0: """batch"""}),
							        ("""logits_per_text""", {0: """batch"""}),
							        ("""text_embeds""", {0: """batch"""}),
							        ("""image_embeds""", {0: """batch"""}),
							    ] )





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return 1e-4





				def    _lowerCAmelCase				(       self		, _a		, _a = -1		, _a = -1		, _a = None		, ):





							"""simple docstring"""
							lowerCamelCase			      =					super().generate_dummy_inputs(
							    processor.tokenizer		, batch_size=_a		, seq_length=_a		, framework=_a )
							lowerCamelCase			      =					super().generate_dummy_inputs(
							    processor.image_processor		, batch_size=_a		, framework=_a )
							return {**text_input_dict, **image_input_dict}





				@property
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return 14









"""simple docstring"""




import importlib
import sys
from argparse import REMAINDER, ArgumentParser
from pathlib import Path

import torch_xla.distributed.xla_multiprocessing as xmp



def     a__ (				)						->					Union[str, Any]:
			lowerCamelCase			      =					ArgumentParser(
			    description=(
			        """PyTorch TPU distributed training launch """
			        """helper utility that will spawn up """
			        """multiple distributed processes"""
			    )				)

			# Optional arguments for the launch helper
			parser.add_argument("""--num_cores"""       , type=snake_case__       , default=1       , help="""Number of TPU cores to use (1 or 8)."""				)

			# positional
			parser.add_argument(
			    """training_script"""       , type=snake_case__       , help=(
			        """The full path to the single TPU training """
			        """program/script to be launched in parallel, """
			        """followed by all the arguments for the """
			        """training script"""
			    )       , )

			# rest from the training program
			parser.add_argument("""training_script_args"""       , nargs=snake_case__				)

			return parser.parse_args()



def     a__ (				)						->					List[str]:
			lowerCamelCase			      =					parse_args()

			# Import training_script as a module.
			lowerCamelCase			      =					Path(args.training_script				)
			sys.path.append(str(script_fpath.parent.resolve()				)				)
			lowerCamelCase			      =					script_fpath.stem
			lowerCamelCase			      =					importlib.import_module(snake_case__				)

			# Patch sys.argv
			lowerCamelCase			      =					[args.training_script] + args.training_script_args + ["""--tpu_num_cores""", str(args.num_cores				)]
			xmp.spawn(mod._mp_fn       , args=()       , nprocs=args.num_cores				)


if __name__ == "__main__":
					main()



