



"""simple docstring"""







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 ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_outputs import (
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import logging
from .configuration_regnet import RegNetConfig


UpperCamelCase     =							logging.get_logger(__name__)

# General docstring
UpperCamelCase     =							"""RegNetConfig"""

# Base docstring
UpperCamelCase     =							"""facebook/regnet-y-040"""
UpperCamelCase     =							[1, 1088, 7, 7]

# Image classification docstring
UpperCamelCase     =							"""facebook/regnet-y-040"""
UpperCamelCase     =							"""tabby, tabby cat"""

UpperCamelCase     =							[
    """facebook/regnet-y-040""",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]






class       UpperCamelCase__			(   nn.Module      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = 3						,	SCREAMING_SNAKE_CASE__ = 1						,	SCREAMING_SNAKE_CASE__ = 1						,	SCREAMING_SNAKE_CASE__ = "relu"						,	)      ->	Any:
				super().__init__()
				A__											=					nn.Convad(
				    SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	kernel_size=SCREAMING_SNAKE_CASE__						,	stride=SCREAMING_SNAKE_CASE__						,	padding=kernel_size // 2						,	groups=SCREAMING_SNAKE_CASE__						,	bias=SCREAMING_SNAKE_CASE__						,	)
				A__											=					nn.BatchNormad(SCREAMING_SNAKE_CASE__		)
				A__											=					ACTaFN[activation] if activation is not None else nn.Identity()







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	List[Any]:
				A__											=					self.convolution(SCREAMING_SNAKE_CASE__		)
				A__											=					self.normalization(SCREAMING_SNAKE_CASE__		)
				A__											=					self.activation(SCREAMING_SNAKE_CASE__		)
				return hidden_state






class       UpperCamelCase__			(   nn.Module      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__		)      ->	int:
				super().__init__()
				A__											=					RegNetConvLayer(
				    config.num_channels						,	config.embedding_size						,	kernel_size=3						,	stride=2						,	activation=config.hidden_act		)
				A__											=					config.num_channels







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Tuple:
				A__											=					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__											=					self.embedder(SCREAMING_SNAKE_CASE__		)
				return hidden_state






class       UpperCamelCase__			(   nn.Module      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = 2		)      ->	int:
				super().__init__()
				A__											=					nn.Convad(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	kernel_size=1						,	stride=SCREAMING_SNAKE_CASE__						,	bias=SCREAMING_SNAKE_CASE__		)
				A__											=					nn.BatchNormad(SCREAMING_SNAKE_CASE__		)







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Tensor:
				A__											=					self.convolution(SCREAMING_SNAKE_CASE__		)
				A__											=					self.normalization(SCREAMING_SNAKE_CASE__		)
				return hidden_state






class       UpperCamelCase__			(   nn.Module      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)      ->	List[str]:
				super().__init__()

				A__											=					nn.AdaptiveAvgPoolad((1, 1)		)
				A__											=					nn.Sequential(
				    nn.Convad(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	kernel_size=1		)						,	nn.ReLU()						,	nn.Convad(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	kernel_size=1		)						,	nn.Sigmoid()						,	)







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	int:
				# b c h w -> b c 1 1
				A__											=					self.pooler(SCREAMING_SNAKE_CASE__		)
				A__											=					self.attention(SCREAMING_SNAKE_CASE__		)
				A__											=					hidden_state * attention
				return hidden_state






class       UpperCamelCase__			(   nn.Module      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = 1		)      ->	Tuple:
				super().__init__()
				A__											=					in_channels != out_channels or stride != 1
				A__											=					max(1						,	out_channels // config.groups_width		)
				A__											=					(
				    RegNetShortCut(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	stride=SCREAMING_SNAKE_CASE__		) if should_apply_shortcut else nn.Identity()
				)
				A__											=					nn.Sequential(
				    RegNetConvLayer(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	kernel_size=1						,	activation=config.hidden_act		)						,	RegNetConvLayer(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	stride=SCREAMING_SNAKE_CASE__						,	groups=SCREAMING_SNAKE_CASE__						,	activation=config.hidden_act		)						,	RegNetConvLayer(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	kernel_size=1						,	activation=SCREAMING_SNAKE_CASE__		)						,	)
				A__											=					ACTaFN[config.hidden_act]







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Optional[int]:
				A__											=					hidden_state
				A__											=					self.layer(SCREAMING_SNAKE_CASE__		)
				A__											=					self.shortcut(SCREAMING_SNAKE_CASE__		)
				hidden_state += residual
				A__											=					self.activation(SCREAMING_SNAKE_CASE__		)
				return hidden_state






class       UpperCamelCase__			(   nn.Module      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = 1		)      ->	str:
				super().__init__()
				A__											=					in_channels != out_channels or stride != 1
				A__											=					max(1						,	out_channels // config.groups_width		)
				A__											=					(
				    RegNetShortCut(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	stride=SCREAMING_SNAKE_CASE__		) if should_apply_shortcut else nn.Identity()
				)
				A__											=					nn.Sequential(
				    RegNetConvLayer(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	kernel_size=1						,	activation=config.hidden_act		)						,	RegNetConvLayer(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	stride=SCREAMING_SNAKE_CASE__						,	groups=SCREAMING_SNAKE_CASE__						,	activation=config.hidden_act		)						,	RegNetSELayer(SCREAMING_SNAKE_CASE__						,	reduced_channels=int(round(in_channels / 4		)		)		)						,	RegNetConvLayer(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	kernel_size=1						,	activation=SCREAMING_SNAKE_CASE__		)						,	)
				A__											=					ACTaFN[config.hidden_act]







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Tuple:
				A__											=					hidden_state
				A__											=					self.layer(SCREAMING_SNAKE_CASE__		)
				A__											=					self.shortcut(SCREAMING_SNAKE_CASE__		)
				hidden_state += residual
				A__											=					self.activation(SCREAMING_SNAKE_CASE__		)
				return hidden_state






class       UpperCamelCase__			(   nn.Module      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = 2						,	SCREAMING_SNAKE_CASE__ = 2						,	)      ->	Tuple:
				super().__init__()

				A__											=					RegNetXLayer if config.layer_type == "x" else RegNetYLayer

				A__											=					nn.Sequential(
				    # downsampling is done in the first layer with stride of 2
				    layer(
				        SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	stride=SCREAMING_SNAKE_CASE__						,	)						,	*[layer(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) for _ in range(depth - 1		)]						,	)







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	List[Any]:
				A__											=					self.layers(SCREAMING_SNAKE_CASE__		)
				return hidden_state






class       UpperCamelCase__			(   nn.Module      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Union[str, Any]:
				super().__init__()
				A__											=					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(
				    RegNetStage(
				        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__											=					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(RegNetStage(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	depth=SCREAMING_SNAKE_CASE__		)		)







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = False						,	SCREAMING_SNAKE_CASE__ = True		)      ->	BaseModelOutputWithNoAttention:
				A__											=					() if output_hidden_states else None

				for stage_module in self.stages:
					if output_hidden_states:
						A__											=					hidden_states + (hidden_state,)

					A__											=					stage_module(SCREAMING_SNAKE_CASE__		)

				if output_hidden_states:
					A__											=					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       UpperCamelCase__			(   _lowerCAmelCase      ):

			"""simple docstring"""


			A__	:					Optional[Any]							  =     RegNetConfig
			A__	:					Tuple							  =     "regnet"
			A__	:					Tuple							  =     "pixel_values"
			A__	:					List[Any]							  =     True


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	str:
				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   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__=False		)      ->	Optional[Any]:
				if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		):
					A__											=					value



UpperCamelCase     =							R"""
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
"""

UpperCamelCase     =							R"""
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple.
"""






@add_start_docstrings(
    "The bare RegNet model outputting raw features without any specific head on top."       ,		_lowerCAmelCase       ,		)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class       UpperCamelCase__			(   _lowerCAmelCase      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Optional[Any]:
				super().__init__(SCREAMING_SNAKE_CASE__		)
				A__											=					config
				A__											=					RegNetEmbeddings(SCREAMING_SNAKE_CASE__		)
				A__											=					RegNetEncoder(SCREAMING_SNAKE_CASE__		)
				A__											=					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   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None						,	SCREAMING_SNAKE_CASE__ = None		)      ->	BaseModelOutputWithPoolingAndNoAttention:
				A__											=					(
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				A__											=					return_dict if return_dict is not None else self.config.use_return_dict

				A__											=					self.embedder(SCREAMING_SNAKE_CASE__		)

				A__											=					self.encoder(
				    SCREAMING_SNAKE_CASE__						,	output_hidden_states=SCREAMING_SNAKE_CASE__						,	return_dict=SCREAMING_SNAKE_CASE__		)

				A__											=					encoder_outputs[0]

				A__											=					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    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "       ,		_lowerCAmelCase       ,		)
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class       UpperCamelCase__			(   _lowerCAmelCase      ):

			"""simple docstring"""


			def __init__(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Dict:
				super().__init__(SCREAMING_SNAKE_CASE__		)
				A__											=					config.num_labels
				A__											=					RegNetModel(SCREAMING_SNAKE_CASE__		)
				# classification head
				A__											=					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   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__ = None						,	SCREAMING_SNAKE_CASE__ = None						,	SCREAMING_SNAKE_CASE__ = None						,	SCREAMING_SNAKE_CASE__ = None						,	)      ->	ImageClassifierOutputWithNoAttention:
				A__											=					return_dict if return_dict is not None else self.config.use_return_dict

				A__											=					self.regnet(SCREAMING_SNAKE_CASE__						,	output_hidden_states=SCREAMING_SNAKE_CASE__						,	return_dict=SCREAMING_SNAKE_CASE__		)

				A__											=					outputs.pooler_output if return_dict else outputs[1]

				A__											=					self.classifier(SCREAMING_SNAKE_CASE__		)

				A__											=					None

				if labels is not None:
					if self.config.problem_type is None:
						if self.num_labels == 1:
							A__											=					"regression"
						elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
							A__											=					"single_label_classification"
						else:
							A__											=					"multi_label_classification"
					if self.config.problem_type == "regression":
						A__											=					MSELoss()
						if self.num_labels == 1:
							A__											=					loss_fct(logits.squeeze()						,	labels.squeeze()		)
						else:
							A__											=					loss_fct(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)
					elif self.config.problem_type == "single_label_classification":
						A__											=					CrossEntropyLoss()
						A__											=					loss_fct(logits.view(-1						,	self.num_labels		)						,	labels.view(-1		)		)
					elif self.config.problem_type == "multi_label_classification":
						A__											=					BCEWithLogitsLoss()
						A__											=					loss_fct(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)

				if not return_dict:
					A__											=					(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		)









"""simple docstring"""







import json
import os
from functools import lru_cache
from typing import List, Optional, Tuple

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


UpperCamelCase     =							logging.get_logger(__name__)


UpperCamelCase     =							{"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt"""}

# See all BART models at https://huggingface.co/models?filter=bart
UpperCamelCase     =							{
    """vocab_file""": {
        """facebook/bart-base""": """https://huggingface.co/facebook/bart-base/resolve/main/vocab.json""",
        """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/vocab.json""",
        """facebook/bart-large-mnli""": """https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json""",
        """facebook/bart-large-cnn""": """https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json""",
        """facebook/bart-large-xsum""": """https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json""",
        """yjernite/bart_eli5""": """https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """facebook/bart-base""": """https://huggingface.co/facebook/bart-base/resolve/main/merges.txt""",
        """facebook/bart-large""": """https://huggingface.co/facebook/bart-large/resolve/main/merges.txt""",
        """facebook/bart-large-mnli""": """https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt""",
        """facebook/bart-large-cnn""": """https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt""",
        """facebook/bart-large-xsum""": """https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt""",
        """yjernite/bart_eli5""": """https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt""",
    },
}

UpperCamelCase     =							{
    """facebook/bart-base""": 1024,
    """facebook/bart-large""": 1024,
    """facebook/bart-large-mnli""": 1024,
    """facebook/bart-large-cnn""": 1024,
    """facebook/bart-large-xsum""": 1024,
    """yjernite/bart_eli5""": 1024,
}

@lru_cache()
def       _lowerCamelCase				(							)	-> Tuple:






	"""simple docstring"""





	A__											=					(
	    list(range(ord("!"     ),	ord("~"     ) + 1     )     ) + list(range(ord("¡"     ),	ord("¬"     ) + 1     )     ) + list(range(ord("®"     ),	ord("ÿ"     ) + 1     )     )
	)
	A__											=					bs[:]
	A__											=					0
	for b in range(2**8     ):
		if b not in bs:
			bs.append(UpperCAmelCase_     )
			cs.append(2**8 + n     )
			n += 1
	A__											=					[chr(UpperCAmelCase_     ) for n in cs]
	return dict(zip(UpperCAmelCase_,	UpperCAmelCase_     )     )

def       _lowerCamelCase				(							UpperCAmelCase_ :       str     )	-> List[str]:






	"""simple docstring"""





	A__											=					set()
	A__											=					word[0]
	for char in word[1:]:
		pairs.add((prev_char, char)     )
		A__											=					char
	return pairs






class       UpperCamelCase__			(   _lowerCAmelCase      ):

			"""simple docstring"""


			A__	:					Union[str, Any]							  =     VOCAB_FILES_NAMES
			A__	:					Optional[Any]							  =     PRETRAINED_VOCAB_FILES_MAP
			A__	:					Dict							  =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			A__	:					Optional[int]							  =     ["input_ids", "attention_mask"]


			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__="replace"						,	SCREAMING_SNAKE_CASE__="<s>"						,	SCREAMING_SNAKE_CASE__="</s>"						,	SCREAMING_SNAKE_CASE__="</s>"						,	SCREAMING_SNAKE_CASE__="<s>"						,	SCREAMING_SNAKE_CASE__="<unk>"						,	SCREAMING_SNAKE_CASE__="<pad>"						,	SCREAMING_SNAKE_CASE__="<mask>"						,	SCREAMING_SNAKE_CASE__=False						,	**SCREAMING_SNAKE_CASE__						,	)      ->	Tuple:
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else bos_token
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else eos_token
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else sep_token
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else cls_token
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else unk_token
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else pad_token

				# Mask token behave like a normal word, i.e. include the space before it
				A__											=					AddedToken(SCREAMING_SNAKE_CASE__						,	lstrip=SCREAMING_SNAKE_CASE__						,	rstrip=SCREAMING_SNAKE_CASE__		) if isinstance(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		) else mask_token

				super().__init__(
				    errors=SCREAMING_SNAKE_CASE__						,	bos_token=SCREAMING_SNAKE_CASE__						,	eos_token=SCREAMING_SNAKE_CASE__						,	unk_token=SCREAMING_SNAKE_CASE__						,	sep_token=SCREAMING_SNAKE_CASE__						,	cls_token=SCREAMING_SNAKE_CASE__						,	pad_token=SCREAMING_SNAKE_CASE__						,	mask_token=SCREAMING_SNAKE_CASE__						,	add_prefix_space=SCREAMING_SNAKE_CASE__						,	**SCREAMING_SNAKE_CASE__						,	)

				with open(SCREAMING_SNAKE_CASE__						,	encoding="utf-8"		) as vocab_handle:
					A__											=					json.load(SCREAMING_SNAKE_CASE__		)
				A__											=					{v: k for k, v in self.encoder.items()}
				A__											=					errors  # how to handle errors in decoding
				A__											=					bytes_to_unicode()
				A__											=					{v: k for k, v in self.byte_encoder.items()}
				with open(SCREAMING_SNAKE_CASE__						,	encoding="utf-8"		) as merges_handle:
					A__											=					merges_handle.read().split("\n"		)[1:-1]
				A__											=					[tuple(merge.split()		) for merge in bpe_merges]
				A__											=					dict(zip(SCREAMING_SNAKE_CASE__						,	range(len(SCREAMING_SNAKE_CASE__		)		)		)		)
				A__											=					{}
				A__											=					add_prefix_space

				# Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
				A__											=					re.compile(R"'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"		)


			@property
			def   snake_case__	(      self		)      ->	List[Any]:
				return len(self.encoder		)


			def   snake_case__	(      self		)      ->	List[Any]:
				return dict(self.encoder						,	**self.added_tokens_encoder		)


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Dict:
				if token in self.cache:
					return self.cache[token]
				A__											=					tuple(SCREAMING_SNAKE_CASE__		)
				A__											=					get_pairs(SCREAMING_SNAKE_CASE__		)

				if not pairs:
					return token

				while True:
					A__											=					min(SCREAMING_SNAKE_CASE__						,	key=lambda SCREAMING_SNAKE_CASE__		: self.bpe_ranks.get(SCREAMING_SNAKE_CASE__						,	float("inf"		)		)		)
					if bigram not in self.bpe_ranks:
						break
					A__							,							A__											=					bigram
					A__											=					[]
					A__											=					0
					while i < len(SCREAMING_SNAKE_CASE__		):
						try:
							A__											=					word.index(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)
						except ValueError:
							new_word.extend(word[i:]		)
							break
						else:
							new_word.extend(word[i:j]		)
							A__											=					j

						if word[i] == first and i < len(SCREAMING_SNAKE_CASE__		) - 1 and word[i + 1] == second:
							new_word.append(first + second		)
							i += 2
						else:
							new_word.append(word[i]		)
							i += 1
					A__											=					tuple(SCREAMING_SNAKE_CASE__		)
					A__											=					new_word
					if len(SCREAMING_SNAKE_CASE__		) == 1:
						break
					else:
						A__											=					get_pairs(SCREAMING_SNAKE_CASE__		)
				A__											=					" ".join(SCREAMING_SNAKE_CASE__		)
				A__											=					word
				return word


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	List[Any]:
				A__											=					[]
				for token in re.findall(self.pat						,	SCREAMING_SNAKE_CASE__		):
					A__											=					"".join(
					    self.byte_encoder[b] for b in token.encode("utf-8"		)		)  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
					bpe_tokens.extend(bpe_token for bpe_token in self.bpe(SCREAMING_SNAKE_CASE__		).split(" "		)		)
				return bpe_tokens


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	str:
				return self.encoder.get(SCREAMING_SNAKE_CASE__						,	self.encoder.get(self.unk_token		)		)


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Any:
				return self.decoder.get(SCREAMING_SNAKE_CASE__		)


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__		)      ->	Dict:
				A__											=					"".join(SCREAMING_SNAKE_CASE__		)
				A__											=					bytearray([self.byte_decoder[c] for c in text]		).decode("utf-8"						,	errors=self.errors		)
				return text


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None		)      ->	Tuple[str]:
				if not os.path.isdir(SCREAMING_SNAKE_CASE__		):
					logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""		)
					return
				A__											=					os.path.join(
				    SCREAMING_SNAKE_CASE__						,	(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]		)
				A__											=					os.path.join(
				    SCREAMING_SNAKE_CASE__						,	(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]		)

				with open(SCREAMING_SNAKE_CASE__						,	"w"						,	encoding="utf-8"		) as f:
					f.write(json.dumps(self.encoder						,	indent=2						,	sort_keys=SCREAMING_SNAKE_CASE__						,	ensure_ascii=SCREAMING_SNAKE_CASE__		) + "\n"		)

				A__											=					0
				with open(SCREAMING_SNAKE_CASE__						,	"w"						,	encoding="utf-8"		) as writer:
					writer.write("#version: 0.2\n"		)
					for bpe_tokens, token_index in sorted(self.bpe_ranks.items()						,	key=lambda SCREAMING_SNAKE_CASE__		: kv[1]		):
						if index != token_index:
							logger.warning(
							    f"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."""
							    " Please check that the tokenizer is not corrupted!"		)
							A__											=					token_index
						writer.write(" ".join(SCREAMING_SNAKE_CASE__		) + "\n"		)
						index += 1

				return vocab_file, merge_file


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None		)      ->	List[int]:
				if token_ids_a is None:
					return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
				A__											=					[self.cls_token_id]
				A__											=					[self.sep_token_id]
				return cls + token_ids_a + sep + sep + token_ids_a + sep


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None						,	SCREAMING_SNAKE_CASE__ = False		)      ->	List[int]:
				if already_has_special_tokens:
					return super().get_special_tokens_mask(
					    token_ids_a=SCREAMING_SNAKE_CASE__						,	token_ids_a=SCREAMING_SNAKE_CASE__						,	already_has_special_tokens=SCREAMING_SNAKE_CASE__		)

				if token_ids_a is None:
					return [1] + ([0] * len(SCREAMING_SNAKE_CASE__		)) + [1]
				return [1] + ([0] * len(SCREAMING_SNAKE_CASE__		)) + [1, 1] + ([0] * len(SCREAMING_SNAKE_CASE__		)) + [1]


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__ = None		)      ->	List[int]:
				A__											=					[self.sep_token_id]
				A__											=					[self.cls_token_id]

				if token_ids_a is None:
					return len(cls + token_ids_a + sep		) * [0]
				return len(cls + token_ids_a + sep + sep + token_ids_a + sep		) * [0]







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__=False						,	**SCREAMING_SNAKE_CASE__		)      ->	Optional[int]:
				A__											=					kwargs.pop("add_prefix_space"						,	self.add_prefix_space		)
				if (is_split_into_words or add_prefix_space) and (len(SCREAMING_SNAKE_CASE__		) > 0 and not text[0].isspace()):
					A__											=					" " + text
				return (text, kwargs)




