


"""simple docstring"""


import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_       							=    logging.get_logger(__name__)

a_       							=    {
    "BAAI/AltCLIP": "https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json",
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}
class snake_case      (  UpperCAmelCase_):
		__UpperCamelCase           =			'altclip_text_model'







		def __init__(					self      :       Union[str, Any]    ,   a__      :       Dict=25_00_02    ,   a__      :       Tuple=10_24    ,   a__      :       Dict=24    ,   a__      :       Union[str, Any]=16    ,   a__      :       Union[str, Any]=40_96    ,   a__      :       Union[str, Any]="gelu"    ,   a__      :       Any=0.1    ,   a__      :       int=0.1    ,   a__      :       Dict=5_14    ,   a__      :       int=1    ,   a__      :       List[str]=0.0_2    ,   a__      :       Tuple=0.0_2    ,   a__      :       int=1E-0_5    ,   a__      :       int=1    ,   a__      :       Optional[int]=0    ,   a__      :       List[str]=2    ,   a__      :       Any="absolute"    ,   a__      :       Any=True    ,   a__      :       str=7_68    ,   **a__      :       Tuple    ,   )       ->						List[str]:





						'''simple docstring'''







						super().__init__(pad_token_id=_lowercase    ,   bos_token_id=_lowercase    ,   eos_token_id=_lowercase    ,   **_lowercase		)

						_A					  =			vocab_size
						_A					  =			hidden_size
						_A					  =			num_hidden_layers
						_A					  =			num_attention_heads
						_A					  =			hidden_act
						_A					  =			intermediate_size
						_A					  =			hidden_dropout_prob
						_A					  =			attention_probs_dropout_prob
						_A					  =			max_position_embeddings
						_A					  =			type_vocab_size
						_A					  =			initializer_range
						_A					  =			initializer_factor
						_A					  =			layer_norm_eps
						_A					  =			position_embedding_type
						_A					  =			use_cache
						_A					  =			project_dim





class snake_case      (  UpperCAmelCase_):
		__UpperCamelCase           =			'altclip_vision_model'







		def __init__(					self      :       Union[str, Any]    ,   a__      :       Any=7_68    ,   a__      :       List[str]=30_72    ,   a__      :       Tuple=5_12    ,   a__      :       int=12    ,   a__      :       List[Any]=12    ,   a__      :       Tuple=3    ,   a__      :       List[Any]=2_24    ,   a__      :       Optional[Any]=32    ,   a__      :       Dict="quick_gelu"    ,   a__      :       List[Any]=1E-5    ,   a__      :       Tuple=0.0    ,   a__      :       List[Any]=0.0_2    ,   a__      :       Optional[int]=1.0    ,   **a__      :       Dict    ,   )       ->						Tuple:





						'''simple docstring'''







						super().__init__(**_lowercase		)

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







		@classmethod
		def  a_ (					cls      :       Optional[int]    ,   a__      :       Union[str, os.PathLike]    ,   **a__      :       int		)       ->						int:





						'''simple docstring'''







						cls._set_token_in_kwargs(_lowercase		)

						_A  ,		_A					  =			cls.get_config_dict(_lowercase    ,   **_lowercase		)

						# get the vision config dict if we are loading from AltCLIPConfig
						if config_dict.get("model_type"		) == "altclip":
										_A					  =			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(_lowercase    ,   **_lowercase		)





class snake_case      (  UpperCAmelCase_):
		__UpperCamelCase           =			'altclip'
		__UpperCamelCase           =			True







		def __init__(					self      :       List[str]    ,   a__      :       Optional[Any]=None    ,   a__      :       Optional[Any]=None    ,   a__      :       Tuple=7_68    ,   a__      :       List[str]=2.6_5_9_2    ,   **a__      :       Union[str, Any]		)       ->						Tuple:





						'''simple docstring'''







						_A					  =			kwargs.pop("text_config_dict"    ,   _lowercase		)
						_A					  =			kwargs.pop("vision_config_dict"    ,   _lowercase		)

						super().__init__(**_lowercase		)

						# Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
						# `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
						# cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
						if text_config_dict is not None:
										if text_config is None:
														_A					  =			{}

										# This is the complete result when using `text_config_dict`.
										_A					  =			AltCLIPTextConfig(**_lowercase		).to_dict()

										# Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
										for key, value in _text_config_dict.items():
														if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
																		# If specified in `text_config_dict`
																		if key in text_config_dict:
																						_A					  =			(
																						    F"""`{key}` is found in both `text_config_dict` and `text_config` but with different values. """
																						    F"""The value `text_config_dict[\"{key}\"]` will be used instead."""
																						)
																		# If inferred from default argument values (just to be super careful)
																		else:
																						_A					  =			(
																						    F"""`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The """
																						    F"""value `text_config[\"{key}\"]` will be overriden."""
																						)
																		logger.warning(_lowercase		)

            # Update all values in `text_config` with the ones in `_text_config_dict`.
										text_config.update(_text_config_dict		)

						if vision_config_dict is not None:
										if vision_config is None:
														_A					  =			{}

										# This is the complete result when using `vision_config_dict`.
										_A					  =			AltCLIPVisionConfig(**_lowercase		).to_dict()
										# convert keys to string instead of integer
										if "id2label" in _vision_config_dict:
														_A					  =			{
														    str(_lowercase		): value for key, value in _vision_config_dict["id2label"].items()
														}

										# Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
										for key, value in _vision_config_dict.items():
														if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
																		# If specified in `vision_config_dict`
																		if key in vision_config_dict:
																						_A					  =			(
																						    F"""`{key}` is found in both `vision_config_dict` and `vision_config` but with different """
																						    F"""values. The value `vision_config_dict[\"{key}\"]` will be used instead."""
																						)
																		# If inferred from default argument values (just to be super careful)
																		else:
																						_A					  =			(
																						    F"""`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. """
																						    F"""The value `vision_config[\"{key}\"]` will be overriden."""
																						)
																		logger.warning(_lowercase		)

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
										vision_config.update(_vision_config_dict		)

						if text_config is None:
										_A					  =			{}
										logger.info("`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."		)

						if vision_config is None:
										_A					  =			{}
										logger.info("`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."		)

						_A					  =			AltCLIPTextConfig(**_lowercase		)
						_A					  =			AltCLIPVisionConfig(**_lowercase		)

						_A					  =			projection_dim
						_A					  =			logit_scale_init_value
						_A					  =			1.0







		@classmethod
		def  a_ (					cls      :       str    ,   a__      :       AltCLIPTextConfig    ,   a__      :       AltCLIPVisionConfig    ,   **a__      :       Any		)       ->						int:





						'''simple docstring'''







						return cls(text_config=text_config.to_dict()    ,   vision_config=vision_config.to_dict()    ,   **_lowercase		)







		def  a_ (					self      :       Optional[int]		)       ->						List[str]:





						'''simple docstring'''







						_A					  =			copy.deepcopy(self.__dict__		)
						_A					  =			self.text_config.to_dict()
						_A					  =			self.vision_config.to_dict()
						_A					  =			self.__class__.model_type
						return output



"""simple docstring"""


import os
import time
import warnings
from dataclasses import dataclass, field
from enum import Enum
from typing import List, Optional, Union

import torch
from filelock import FileLock
from torch.utils.data import Dataset

from ...tokenization_utils_base import PreTrainedTokenizerBase
from ...utils import logging
from ..processors.glue import glue_convert_examples_to_features, glue_output_modes, glue_processors
from ..processors.utils import InputFeatures


a_       							=    logging.get_logger(__name__)
@dataclass
class snake_case      :
		__UpperCamelCase           =			field(metadata={'help': 'The name of the task to train on: ' + ', '.join(glue_processors.keys())})
		__UpperCamelCase           =			field(
		    metadata={'help': 'The input data dir. Should contain the .tsv files (or other data files) for the task.'})
		__UpperCamelCase           =			field(
		    default=128     ,				metadata={
		        'help': (
		            'The maximum total input sequence length after tokenization. Sequences longer '
		            'than this will be truncated, sequences shorter will be padded.'
		        )
		    }     ,				)
		__UpperCamelCase           =			field(
		    default=_UpperCamelCase     ,				metadata={'help': 'Overwrite the cached training and evaluation sets'})







		def  a_ (					self      :       Optional[Any]		)       ->						Tuple:





						'''simple docstring'''







						_A					  =			self.task_name.lower()





class snake_case      (  _UpperCamelCase):
		__UpperCamelCase           =			'train'
		__UpperCamelCase           =			'dev'
		__UpperCamelCase           =			'test'





class snake_case      (  _UpperCamelCase):
		__UpperCamelCase           =			42
		__UpperCamelCase           =			42
		__UpperCamelCase           =			42







		def __init__(					self      :       Optional[int]    ,   a__      :       GlueDataTrainingArguments    ,   a__      :       PreTrainedTokenizerBase    ,   a__      :       Optional[int] = None    ,   a__      :       Union[str, Split] = Split.train    ,   a__      :       Optional[str] = None    ,   )       ->						Tuple:





						'''simple docstring'''







						warnings.warn(
						    "This dataset will be removed from the library soon, preprocessing should be handled with the 🤗 Datasets "
						    "library. You can have a look at this example script for pointers: "
						    "https://github.com/huggingface/transformers/blob/main/examples/pytorch/text-classification/run_glue.py"    ,   a__    ,   )
						_A					  =			args
						_A					  =			glue_processors[args.task_name]()
						_A					  =			glue_output_modes[args.task_name]
						if isinstance(a__    ,   a__		):
										try:
														_A					  =			Split[mode]
										except KeyError:
														raise KeyError("mode is not a valid split name"		)
        # Load data features from cache or dataset file
						_A					  =			os.path.join(
						    cache_dir if cache_dir is not None else args.data_dir    ,   F"""cached_{mode.value}_{tokenizer.__class__.__name__}_{args.max_seq_length}_{args.task_name}"""    ,   )
						_A					  =			self.processor.get_labels()
						if args.task_name in ["mnli", "mnli-mm"] and tokenizer.__class__.__name__ in (
						    "RobertaTokenizer",
						    "RobertaTokenizerFast",
						    "XLMRobertaTokenizer",
						    "BartTokenizer",
						    "BartTokenizerFast",
						):
										# HACK(label indices are swapped in RoBERTa pretrained model)
										_A  ,		_A					  =			label_list[2], label_list[1]
						_A					  =			label_list

						# Make sure only the first process in distributed training processes the dataset,
						# and the others will use the cache.
						_A					  =			cached_features_file + ".lock"
						with FileLock(a__		):
										if os.path.exists(a__		) and not args.overwrite_cache:
														_A					  =			time.time()
														_A					  =			torch.load(a__		)
														logger.info(
														    F"""Loading features from cached file {cached_features_file} [took %.3f s]"""    ,   time.time() - start		)
										else:
														logger.info(F"""Creating features from dataset file at {args.data_dir}"""		)

														if mode == Split.dev:
																		_A					  =			self.processor.get_dev_examples(args.data_dir		)
														elif mode == Split.test:
																		_A					  =			self.processor.get_test_examples(args.data_dir		)
														else:
																		_A					  =			self.processor.get_train_examples(args.data_dir		)
														if limit_length is not None:
																		_A					  =			examples[:limit_length]
														_A					  =			glue_convert_examples_to_features(
														    a__    ,   a__    ,   max_length=args.max_seq_length    ,   label_list=a__    ,   output_mode=self.output_mode    ,   )
														_A					  =			time.time()
														torch.save(self.features    ,   a__		)
														# ^ This seems to take a lot of time so I want to investigate why and how we can improve.
														logger.info(
														    F"""Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]"""		)







		def __len__(					self      :       List[Any]		)       ->						Any:





						'''simple docstring'''







						return len(self.features		)







		def __getitem__(					self      :       Tuple    ,   a__      :       Union[str, Any]		)       ->						InputFeatures:





						'''simple docstring'''







						return self.features[i]







		def  a_ (					self      :       Optional[int]		)       ->						List[Any]:





						'''simple docstring'''







						return self.label_list