



from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


_lowercase						: Any     						=logging.get_logger(__name__)

# General docstring
_lowercase						: Any     						='ResNetConfig'

# Base docstring
_lowercase						: Optional[Any]     						='microsoft/resnet-50'
_lowercase						: List[Any]     						=[1, 2048, 7, 7]

# Image classification docstring
_lowercase						: int     						='microsoft/resnet-50'
_lowercase						: List[Any]     						='tiger cat'

_lowercase						: Optional[Any]     						=[
    'microsoft/resnet-50',
    # See all resnet models at https://huggingface.co/models?filter=resnet
]

class   snake_case__							(nn.Module						):







						"""simple docstring"""




						def __init__(  self   ,  __lowercase   ,  __lowercase   ,  __lowercase = 3   ,  __lowercase = 1   ,  __lowercase = "relu"    )   -> Tuple:
									"""simple docstring"""




									super().__init__()
									a__  :		List[str]					=    nn.Convad(
									    SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  kernel_size=SCREAMING_SNAKE_CASE_   ,  stride=SCREAMING_SNAKE_CASE_   ,  padding=kernel_size // 2   ,  bias=SCREAMING_SNAKE_CASE_    )
									a__  :		List[Any]					=    nn.BatchNormad(SCREAMING_SNAKE_CASE_    )
									a__  :		Optional[Any]					=    ACTaFN[activation] if activation is not None else nn.Identity()

						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase    )   -> Tensor:
									"""simple docstring"""




									a__  :		str					=    self.convolution(SCREAMING_SNAKE_CASE_    )
									a__  :		Optional[int]					=    self.normalization(SCREAMING_SNAKE_CASE_    )
									a__  :		Optional[Any]					=    self.activation(SCREAMING_SNAKE_CASE_    )
									return hidden_state




class   snake_case__							(nn.Module						):







						"""simple docstring"""




						def __init__(  self   ,  __lowercase    )   -> List[Any]:
									"""simple docstring"""




									super().__init__()
									a__  :		Optional[Any]					=    ResNetConvLayer(
									    config.num_channels   ,  config.embedding_size   ,  kernel_size=7   ,  stride=2   ,  activation=config.hidden_act    )
									a__  :		Tuple					=    nn.MaxPoolad(kernel_size=3   ,  stride=2   ,  padding=1    )
									a__  :		int					=    config.num_channels

						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase    )   -> Tensor:
									"""simple docstring"""




									a__  :		List[Any]					=    pixel_values.shape[1]
									if num_channels != self.num_channels:
												raise ValueError(
												    """Make sure that the channel dimension of the pixel values match with the one set in the configuration."""    )
									a__  :		Union[str, Any]					=    self.embedder(SCREAMING_SNAKE_CASE_    )
									a__  :		List[Any]					=    self.pooler(SCREAMING_SNAKE_CASE_    )
									return embedding




class   snake_case__							(nn.Module						):







						"""simple docstring"""




						def __init__(  self   ,  __lowercase   ,  __lowercase   ,  __lowercase = 2    )   -> Optional[Any]:
									"""simple docstring"""




									super().__init__()
									a__  :		Dict					=    nn.Convad(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  kernel_size=1   ,  stride=SCREAMING_SNAKE_CASE_   ,  bias=SCREAMING_SNAKE_CASE_    )
									a__  :		Optional[int]					=    nn.BatchNormad(SCREAMING_SNAKE_CASE_    )

						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase    )   -> Tensor:
									"""simple docstring"""




									a__  :		Union[str, Any]					=    self.convolution(SCREAMING_SNAKE_CASE_    )
									a__  :		Dict					=    self.normalization(SCREAMING_SNAKE_CASE_    )
									return hidden_state




class   snake_case__							(nn.Module						):







						"""simple docstring"""




						def __init__(  self   ,  __lowercase   ,  __lowercase   ,  __lowercase = 1   ,  __lowercase = "relu"    )   -> Dict:
									"""simple docstring"""




									super().__init__()
									a__  :		List[Any]					=    in_channels != out_channels or stride != 1
									a__  :		Dict					=    (
									    ResNetShortCut(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  stride=SCREAMING_SNAKE_CASE_    ) if should_apply_shortcut else nn.Identity()
									)
									a__  :		str					=    nn.Sequential(
									    ResNetConvLayer(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  stride=SCREAMING_SNAKE_CASE_    )   ,  ResNetConvLayer(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  activation=SCREAMING_SNAKE_CASE_    )   ,  )
									a__  :		List[str]					=    ACTaFN[activation]

						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase    )   -> Dict:
									"""simple docstring"""




									a__  :		Union[str, Any]					=    hidden_state
									a__  :		Dict					=    self.layer(SCREAMING_SNAKE_CASE_    )
									a__  :		str					=    self.shortcut(SCREAMING_SNAKE_CASE_    )
									hidden_state += residual
									a__  :		Union[str, Any]					=    self.activation(SCREAMING_SNAKE_CASE_    )
									return hidden_state




class   snake_case__							(nn.Module						):







						"""simple docstring"""




						def __init__(  self   ,  __lowercase   ,  __lowercase   ,  __lowercase = 1   ,  __lowercase = "relu"   ,  __lowercase = 4    )   -> int:
									"""simple docstring"""




									super().__init__()
									a__  :		List[Any]					=    in_channels != out_channels or stride != 1
									a__  :		List[str]					=    out_channels // reduction
									a__  :		Tuple					=    (
									    ResNetShortCut(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  stride=SCREAMING_SNAKE_CASE_    ) if should_apply_shortcut else nn.Identity()
									)
									a__  :		List[Any]					=    nn.Sequential(
									    ResNetConvLayer(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  kernel_size=1    )   ,  ResNetConvLayer(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  stride=SCREAMING_SNAKE_CASE_    )   ,  ResNetConvLayer(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  kernel_size=1   ,  activation=SCREAMING_SNAKE_CASE_    )   ,  )
									a__  :		List[Any]					=    ACTaFN[activation]

						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase    )   -> Optional[Any]:
									"""simple docstring"""




									a__  :		Optional[Any]					=    hidden_state
									a__  :		List[str]					=    self.layer(SCREAMING_SNAKE_CASE_    )
									a__  :		Union[str, Any]					=    self.shortcut(SCREAMING_SNAKE_CASE_    )
									hidden_state += residual
									a__  :		int					=    self.activation(SCREAMING_SNAKE_CASE_    )
									return hidden_state




class   snake_case__							(nn.Module						):







						"""simple docstring"""




						def __init__(  self   ,  __lowercase   ,  __lowercase   ,  __lowercase   ,  __lowercase = 2   ,  __lowercase = 2   ,  )   -> str:
									"""simple docstring"""




									super().__init__()

									a__  :		Union[str, Any]					=    ResNetBottleNeckLayer if config.layer_type == """bottleneck""" else ResNetBasicLayer

									a__  :		List[Any]					=    nn.Sequential(
									    # downsampling is done in the first layer with stride of 2
									    layer(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  stride=SCREAMING_SNAKE_CASE_   ,  activation=config.hidden_act    )   ,  *[layer(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  activation=config.hidden_act    ) for _ in range(depth - 1    )]   ,  )

						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase    )   -> Tensor:
									"""simple docstring"""




									a__  :		Union[str, Any]					=    input
									for layer in self.layers:
												a__  :		List[Any]					=    layer(SCREAMING_SNAKE_CASE_    )
									return hidden_state




class   snake_case__							(nn.Module						):







						"""simple docstring"""




						def __init__(  self   ,  __lowercase    )   -> Optional[int]:
									"""simple docstring"""




									super().__init__()
									a__  :		Any					=    nn.ModuleList([]    )
									# based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
									self.stages.append(
									    ResNetStage(
									        SCREAMING_SNAKE_CASE_   ,  config.embedding_size   ,  config.hidden_sizes[0]   ,  stride=2 if config.downsample_in_first_stage else 1   ,  depth=config.depths[0]   ,  )    )
									a__  :		Tuple					=    zip(config.hidden_sizes   ,  config.hidden_sizes[1:]    )
									for (in_channels, out_channels), depth in zip(SCREAMING_SNAKE_CASE_   ,  config.depths[1:]    ):
												self.stages.append(ResNetStage(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_   ,  depth=SCREAMING_SNAKE_CASE_    )    )

						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase   ,  __lowercase = False   ,  __lowercase = True    )   -> BaseModelOutputWithNoAttention:
									"""simple docstring"""




									a__  :		Optional[Any]					=    () if output_hidden_states else None

									for stage_module in self.stages:
												if output_hidden_states:
															a__  :		Union[str, Any]					=    hidden_states + (hidden_state,)

												a__  :		Any					=    stage_module(SCREAMING_SNAKE_CASE_    )

									if output_hidden_states:
												a__  :		Tuple					=    hidden_states + (hidden_state,)

									if not return_dict:
												return tuple(v for v in [hidden_state, hidden_states] if v is not None    )

									return BaseModelOutputWithNoAttention(
									    last_hidden_state=SCREAMING_SNAKE_CASE_   ,  hidden_states=SCREAMING_SNAKE_CASE_   ,  )




class   snake_case__							(_lowerCAmelCase						):







						"""simple docstring"""




						__lowerCAmelCase	:Dict         =	ResNetConfig
						__lowerCAmelCase	:Tuple         =	"resnet"
						__lowerCAmelCase	:str         =	"pixel_values"
						__lowerCAmelCase	:Optional[Any]         =	True

						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase    )   -> Optional[Any]:
									"""simple docstring"""




									if isinstance(SCREAMING_SNAKE_CASE_   ,  nn.Convad    ):
												nn.init.kaiming_normal_(module.weight   ,  mode="""fan_out"""   ,  nonlinearity="""relu"""    )
									elif isinstance(SCREAMING_SNAKE_CASE_   ,  (nn.BatchNormad, nn.GroupNorm)    ):
												nn.init.constant_(module.weight   ,  1    )
												nn.init.constant_(module.bias   ,  0    )

						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase   ,  __lowercase=False    )   -> int:
									"""simple docstring"""




									if isinstance(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_    ):
												a__  :		Tuple					=    value




_lowercase						: Any     						=r'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

_lowercase						: Dict     						=r'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConvNextImageProcessor.__call__`] for details.\n\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n'

@add_start_docstrings(
    "The bare ResNet model outputting raw features without any specific head on top."   ,      _lowerCAmelCase   ,      )
class   snake_case__							(_lowerCAmelCase						):







						"""simple docstring"""




						def __init__(  self   ,  __lowercase    )   -> Optional[Any]:
									"""simple docstring"""




									super().__init__(SCREAMING_SNAKE_CASE_    )
									a__  :		List[Any]					=    config
									a__  :		Union[str, Any]					=    ResNetEmbeddings(SCREAMING_SNAKE_CASE_    )
									a__  :		Optional[Any]					=    ResNetEncoder(SCREAMING_SNAKE_CASE_    )
									a__  :		Optional[int]					=    nn.AdaptiveAvgPoolad((1, 1)    )
									# Initialize weights and apply final processing
									self.post_init()

						@add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_    )
						@add_code_sample_docstrings(
						    checkpoint=_CHECKPOINT_FOR_DOC   ,  output_type=SCREAMING_SNAKE_CASE_   ,  config_class=_CONFIG_FOR_DOC   ,  modality="""vision"""   ,  expected_output=_EXPECTED_OUTPUT_SHAPE   ,  )
						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase   ,  __lowercase = None   ,  __lowercase = None    )   -> BaseModelOutputWithPoolingAndNoAttention:
									"""simple docstring"""




									a__  :		List[str]					=    (
									    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
									)
									a__  :		Any					=    return_dict if return_dict is not None else self.config.use_return_dict

									a__  :		Optional[Any]					=    self.embedder(SCREAMING_SNAKE_CASE_    )

									a__  :		Dict					=    self.encoder(
									    SCREAMING_SNAKE_CASE_   ,  output_hidden_states=SCREAMING_SNAKE_CASE_   ,  return_dict=SCREAMING_SNAKE_CASE_    )

									a__  :		str					=    encoder_outputs[0]

									a__  :		Dict					=    self.pooler(SCREAMING_SNAKE_CASE_    )

									if not return_dict:
												return (last_hidden_state, pooled_output) + encoder_outputs[1:]

									return BaseModelOutputWithPoolingAndNoAttention(
									    last_hidden_state=SCREAMING_SNAKE_CASE_   ,  pooler_output=SCREAMING_SNAKE_CASE_   ,  hidden_states=encoder_outputs.hidden_states   ,  )




@add_start_docstrings(
    "\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "   ,      _lowerCAmelCase   ,      )
class   snake_case__							(_lowerCAmelCase						):







						"""simple docstring"""




						def __init__(  self   ,  __lowercase    )   -> str:
									"""simple docstring"""




									super().__init__(SCREAMING_SNAKE_CASE_    )
									a__  :		List[Any]					=    config.num_labels
									a__  :		Tuple					=    ResNetModel(SCREAMING_SNAKE_CASE_    )
									# classification head
									a__  :		Dict					=    nn.Sequential(
									    nn.Flatten()   ,  nn.Linear(config.hidden_sizes[-1]   ,  config.num_labels    ) if config.num_labels > 0 else nn.Identity()   ,  )
									# initialize weights and apply final processing
									self.post_init()

						@add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_    )
						@add_code_sample_docstrings(
						    checkpoint=_IMAGE_CLASS_CHECKPOINT   ,  output_type=SCREAMING_SNAKE_CASE_   ,  config_class=_CONFIG_FOR_DOC   ,  expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT   ,  )
						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase = None   ,  __lowercase = None   ,  __lowercase = None   ,  __lowercase = None   ,  )   -> ImageClassifierOutputWithNoAttention:
									"""simple docstring"""




									a__  :		List[str]					=    return_dict if return_dict is not None else self.config.use_return_dict

									a__  :		List[Any]					=    self.resnet(SCREAMING_SNAKE_CASE_   ,  output_hidden_states=SCREAMING_SNAKE_CASE_   ,  return_dict=SCREAMING_SNAKE_CASE_    )

									a__  :		Union[str, Any]					=    outputs.pooler_output if return_dict else outputs[1]

									a__  :		Union[str, Any]					=    self.classifier(SCREAMING_SNAKE_CASE_    )

									a__  :		Tuple					=    None

									if labels is not None:
												if self.config.problem_type is None:
															if self.num_labels == 1:
																		a__  :		int					=    """regression"""
															elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																		a__  :		List[str]					=    """single_label_classification"""
															else:
																		a__  :		int					=    """multi_label_classification"""
												if self.config.problem_type == "regression":
															a__  :		List[str]					=    MSELoss()
															if self.num_labels == 1:
																		a__  :		Any					=    loss_fct(logits.squeeze()   ,  labels.squeeze()    )
															else:
																		a__  :		Dict					=    loss_fct(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_    )
												elif self.config.problem_type == "single_label_classification":
															a__  :		List[str]					=    CrossEntropyLoss()
															a__  :		Tuple					=    loss_fct(logits.view(-1   ,  self.num_labels    )   ,  labels.view(-1    )    )
												elif self.config.problem_type == "multi_label_classification":
															a__  :		str					=    BCEWithLogitsLoss()
															a__  :		Dict					=    loss_fct(SCREAMING_SNAKE_CASE_   ,  SCREAMING_SNAKE_CASE_    )

									if not return_dict:
												a__  :		str					=    (logits,) + outputs[2:]
												return (loss,) + output if loss is not None else output

									return ImageClassifierOutputWithNoAttention(loss=SCREAMING_SNAKE_CASE_   ,  logits=SCREAMING_SNAKE_CASE_   ,  hidden_states=outputs.hidden_states    )




@add_start_docstrings(
    "\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    "   ,      _lowerCAmelCase   ,      )
class   snake_case__							(_lowerCAmelCase   ,      _lowerCAmelCase						):







						"""simple docstring"""




						def __init__(  self   ,  __lowercase    )   -> Optional[Any]:
									"""simple docstring"""




									super().__init__(SCREAMING_SNAKE_CASE_    )
									super()._init_backbone(SCREAMING_SNAKE_CASE_    )

									a__  :		Optional[Any]					=    [config.embedding_size] + config.hidden_sizes
									a__  :		List[str]					=    ResNetEmbeddings(SCREAMING_SNAKE_CASE_    )
									a__  :		int					=    ResNetEncoder(SCREAMING_SNAKE_CASE_    )

									# initialize weights and apply final processing
									self.post_init()

						@add_start_docstrings_to_model_forward(SCREAMING_SNAKE_CASE_    )
						@replace_return_docstrings(output_type=SCREAMING_SNAKE_CASE_   ,  config_class=_CONFIG_FOR_DOC    )
						def 				SCREAMING_SNAKE_CASE__(  self   ,  __lowercase   ,  __lowercase = None   ,  __lowercase = None    )   -> BackboneOutput:
									"""simple docstring"""




									a__  :		Any					=    return_dict if return_dict is not None else self.config.use_return_dict
									a__  :		Tuple					=    (
									    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
									)

									a__  :		List[Any]					=    self.embedder(SCREAMING_SNAKE_CASE_    )

									a__  :		Optional[int]					=    self.encoder(SCREAMING_SNAKE_CASE_   ,  output_hidden_states=SCREAMING_SNAKE_CASE_   ,  return_dict=SCREAMING_SNAKE_CASE_    )

									a__  :		Any					=    outputs.hidden_states

									a__  :		Optional[Any]					=    ()
									for idx, stage in enumerate(self.stage_names    ):
												if stage in self.out_features:
															feature_maps += (hidden_states[idx],)

									if not return_dict:
												a__  :		str					=    (feature_maps,)
												if output_hidden_states:
															output += (outputs.hidden_states,)
												return output

									return BackboneOutput(
									    feature_maps=SCREAMING_SNAKE_CASE_   ,  hidden_states=outputs.hidden_states if output_hidden_states else None   ,  attentions=SCREAMING_SNAKE_CASE_   ,  )









import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


a      :  Optional[Any]			     =			logging.getLogger(__name__)







@dataclass(frozen=_lowerCAmelCase					)
class 				_a				:
			A      		=	42
			A      		=	42
			A      		=	None
			A      		=	None
			A      		=	None







@dataclass(frozen=_lowerCAmelCase					)
class 				_a				:
			A      		=	42
			A      		=	None
			A      		=	None
			A      		=	None
			A      		=	None


if is_torch_available():
	import torch
	from torch.utils.data import Dataset







	class 				_a				(   _lowerCAmelCase					):
				A      		=	42

				def __init__(self,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_ = None,   SCREAMING_SNAKE_CASE_=False,   SCREAMING_SNAKE_CASE_ = False,   )       ->   Union[str, Any]:
							UpperCAmelCase_:		Optional[Any]							      =     hans_processors[task]()

							UpperCAmelCase_:		Dict							      =     os.path.join(
							    SCREAMING_SNAKE_CASE_,   """cached_{}_{}_{}_{}""".format(
							        """dev""" if evaluate else """train""",   tokenizer.__class__.__name__,   str(SCREAMING_SNAKE_CASE_					),   SCREAMING_SNAKE_CASE_,   ),   )
							UpperCAmelCase_:		Dict							      =     processor.get_labels()
							if tokenizer.__class__ in (
							    RobertaTokenizer,
							    RobertaTokenizerFast,
							    XLMRobertaTokenizer,
							    BartTokenizer,
							    BartTokenizerFast,
							):
										# HACK(label indices are swapped in RoBERTa pretrained model)
										UpperCAmelCase_      ,							UpperCAmelCase_:		Tuple							      =     label_list[2], label_list[1]
							UpperCAmelCase_:		str							      =     label_list

							# Make sure only the first process in distributed training processes the dataset,
							# and the others will use the cache.
							UpperCAmelCase_:		int							      =     cached_features_file + """.lock"""
							with FileLock(SCREAMING_SNAKE_CASE_					):
										if os.path.exists(SCREAMING_SNAKE_CASE_					) and not overwrite_cache:
													logger.info(f'Loading features from cached file {cached_features_file}'					)
													UpperCAmelCase_:		Dict							      =     torch.load(SCREAMING_SNAKE_CASE_					)
										else:
													logger.info(f'Creating features from dataset file at {data_dir}'					)

													UpperCAmelCase_:		Union[str, Any]							      =     (
													    processor.get_dev_examples(SCREAMING_SNAKE_CASE_					) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_					)
													)

													logger.info("""Training examples: %s""",   len(SCREAMING_SNAKE_CASE_					)					)
													UpperCAmelCase_:		List[Any]							      =     hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_					)
													logger.info("""Saving features into cached file %s""",   SCREAMING_SNAKE_CASE_					)
													torch.save(self.features,   SCREAMING_SNAKE_CASE_					)

				def __len__(self					)       ->   Optional[int]:
							return len(self.features					)

				def __getitem__(self,   SCREAMING_SNAKE_CASE_					)       ->   InputFeatures:
							return self.features[i]

				def      __snake_case       (self					)       ->   List[str]:
							return self.label_list


if is_tf_available():
	import tensorflow as tf







	class 				_a				:
				A      		=	42

				def __init__(self,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_ = 128,   SCREAMING_SNAKE_CASE_=False,   SCREAMING_SNAKE_CASE_ = False,   )       ->   Optional[int]:
							UpperCAmelCase_:		Union[str, Any]							      =     hans_processors[task]()
							UpperCAmelCase_:		List[Any]							      =     processor.get_labels()
							if tokenizer.__class__ in (
							    RobertaTokenizer,
							    RobertaTokenizerFast,
							    XLMRobertaTokenizer,
							    BartTokenizer,
							    BartTokenizerFast,
							):
										# HACK(label indices are swapped in RoBERTa pretrained model)
										UpperCAmelCase_      ,							UpperCAmelCase_:		List[str]							      =     label_list[2], label_list[1]
							UpperCAmelCase_:		Dict							      =     label_list

							UpperCAmelCase_:		List[str]							      =     processor.get_dev_examples(SCREAMING_SNAKE_CASE_					) if evaluate else processor.get_train_examples(SCREAMING_SNAKE_CASE_					)
							UpperCAmelCase_:		Any							      =     hans_convert_examples_to_features(SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_					)

							def gen():
										for ex_index, ex in tqdm.tqdm(enumerate(self.features					),   desc="""convert examples to features"""					):
													if ex_index % 10000 == 0:
																logger.info("""Writing example %d of %d""" % (ex_index, len(SCREAMING_SNAKE_CASE_					))					)

													yield (
													    {
													        "example_id": 0,
													        "input_ids": ex.input_ids,
													        "attention_mask": ex.attention_mask,
													        "token_type_ids": ex.token_type_ids,
													    },
													    ex.label,
													)

							UpperCAmelCase_:		List[str]							      =     tf.data.Dataset.from_generator(
							    SCREAMING_SNAKE_CASE_,   (
							        {
							            """example_id""": tf.intaa,
							            """input_ids""": tf.intaa,
							            """attention_mask""": tf.intaa,
							            """token_type_ids""": tf.intaa,
							        },
							        tf.intaa,
							    ),   (
							        {
							            """example_id""": tf.TensorShape([]					),
							            """input_ids""": tf.TensorShape([None, None]					),
							            """attention_mask""": tf.TensorShape([None, None]					),
							            """token_type_ids""": tf.TensorShape([None, None]					),
							        },
							        tf.TensorShape([]					),
							    ),   )

				def      __snake_case       (self					)       ->   Optional[Any]:
							return self.dataset

				def __len__(self					)       ->   Optional[int]:
							return len(self.features					)

				def __getitem__(self,   SCREAMING_SNAKE_CASE_					)       ->   InputFeatures:
							return self.features[i]

				def      __snake_case       (self					)       ->   Union[str, Any]:
							return self.label_list







class 				_a				(   _lowerCAmelCase					):

			def      __snake_case       (self,   SCREAMING_SNAKE_CASE_					)       ->   Dict:
						return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_,   """heuristics_train_set.txt"""					)					),   """train"""					)

			def      __snake_case       (self,   SCREAMING_SNAKE_CASE_					)       ->   Dict:
						return self._create_examples(self._read_tsv(os.path.join(SCREAMING_SNAKE_CASE_,   """heuristics_evaluation_set.txt"""					)					),   """dev"""					)

			def      __snake_case       (self					)       ->   Optional[int]:
						return ["contradiction", "entailment", "neutral"]

			def      __snake_case       (self,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_					)       ->   List[Any]:
						UpperCAmelCase_:		Optional[Any]							      =     []
						for i, line in enumerate(SCREAMING_SNAKE_CASE_					):
									if i == 0:
												continue
									UpperCAmelCase_:		Tuple							      =     """%s-%s""" % (set_type, line[0])
									UpperCAmelCase_:		int							      =     line[5]
									UpperCAmelCase_:		int							      =     line[6]
									UpperCAmelCase_:		List[str]							      =     line[7][2:] if line[7].startswith("""ex"""					) else line[7]
									UpperCAmelCase_:		Any							      =     line[0]
									examples.append(InputExample(guid=SCREAMING_SNAKE_CASE_,   text_a=SCREAMING_SNAKE_CASE_,   text_b=SCREAMING_SNAKE_CASE_,   label=SCREAMING_SNAKE_CASE_,   pairID=SCREAMING_SNAKE_CASE_					)					)
						return examples







def  lowerCAmelCase_	(lowerCAmelCase__:		List[InputExample]		,						lowerCAmelCase__:		List[str]		,						lowerCAmelCase__:		int		,						lowerCAmelCase__:		PreTrainedTokenizer		,						):





			"""simple docstring"""


			UpperCAmelCase_:		List[str]							      =     {label: i for i, label in enumerate(lowerCAmelCase__  )}

			UpperCAmelCase_:		Optional[int]							      =     []
			for ex_index, example in tqdm.tqdm(enumerate(lowerCAmelCase__  )		,						desc="""convert examples to features"""  ):
						if ex_index % 1_0_0_0_0 == 0:
									logger.info("""Writing example %d""" % (ex_index)  )

						UpperCAmelCase_:		Tuple							      =     tokenizer(
						    example.text_a		,						example.text_b		,						add_special_tokens=lowerCAmelCase__		,						max_length=lowerCAmelCase__		,						padding="""max_length"""		,						truncation=lowerCAmelCase__		,						return_overflowing_tokens=lowerCAmelCase__		,						)

						UpperCAmelCase_:		Optional[Any]							      =     label_map[example.label] if example.label in label_map else 0

						UpperCAmelCase_:		Tuple							      =     int(example.pairID  )

						features.append(InputFeatures(**lowerCAmelCase__		,						label=lowerCAmelCase__		,						pairID=lowerCAmelCase__  )  )

			for i, example in enumerate(examples[:5]  ):
						logger.info("""*** Example ***"""  )
						logger.info(F'guid: {example}'  )
						logger.info(F'features: {features[i]}'  )

			return features


a      :  Optional[int]			     =			{
    'hans': 3,
}

a      :  Dict			     =			{
    'hans': HansProcessor,
}






