"""simple docstring"""







from typing import Tuple, Union

from ...modeling_outputs import BackboneOutput
from ...modeling_utils import PreTrainedModel
from ...utils import is_timm_available, is_torch_available, requires_backends
from ...utils.backbone_utils import BackboneMixin
from .configuration_timm_backbone import TimmBackboneConfig


if is_timm_available():
	import timm


if is_torch_available():
	from torch import Tensor

class 		UpperCamelCase      (   lowercase				,     lowercase						):
				UpperCAmelCase    :				Dict   =		"""pixel_values"""
				UpperCAmelCase    :				int   =		False
				UpperCAmelCase    :				Optional[Any]   =		TimmBackboneConfig



				def __init__(self    :				Union[str, Any]     ,  _A    :				Tuple     ,  **_A    :				Optional[int])   ->     Tuple:
					requires_backends(self     ,  'timm')
					super().__init__(_A)
					__snake_case      :							str						=			config

					if config.backbone is None:
						raise ValueError('backbone is not set in the config. Please set it to a timm model name.')

					if config.backbone not in timm.list_models():
						raise ValueError(f"backbone {config.backbone} is not supported by timm.")

					if hasattr(_A     ,  'out_features') and config.out_features is not None:
						raise ValueError('out_features is not supported by TimmBackbone. Please use out_indices instead.')

					__snake_case      :							str						=			getattr(_A     ,  'use_pretrained_backbone'     ,  _A)
					if pretrained is None:
						raise ValueError('use_pretrained_backbone is not set in the config. Please set it to True or False.')

					# We just take the final layer by default. This matches the default for the transformers models.
					__snake_case      :							int						=			config.out_indices if getattr(_A     ,  'out_indices'     ,  _A) is not None else (-1,)

					__snake_case      :							List[str]						=			timm.create_model(
					    config.backbone     ,  pretrained=_A     ,  features_only=config.features_only     ,  in_chans=config.num_channels     ,  out_indices=_A     ,  **_A     ,  )
					# These are used to control the output of the model when called. If output_hidden_states is True, then
					# return_layers is modified to include all layers.
					__snake_case      :							Tuple						=			self._backbone.return_layers
					__snake_case      :							str						=			{layer['module']: str(_A) for i, layer in enumerate(self._backbone.feature_info.info)}
					super()._init_backbone(_A)



				@classmethod
				def  _lowercase					(cls    :				Any     ,  _A    :				Optional[int]     ,  *_A    :				int     ,  **_A    :				Optional[int])   ->     str:
					requires_backends(cls     ,  ['vision', 'timm'])
					from ...models.timm_backbone import TimmBackboneConfig

					__snake_case      :							Union[str, Any]						=			kwargs.pop('config'     ,  TimmBackboneConfig())

					__snake_case      :							Any						=			kwargs.pop('use_timm_backbone'     ,  _A)
					if not use_timm:
						raise ValueError('use_timm_backbone must be True for timm backbones')

					__snake_case      :							str						=			kwargs.pop('num_channels'     ,  config.num_channels)
					__snake_case      :							int						=			kwargs.pop('features_only'     ,  config.features_only)
					__snake_case      :							List[Any]						=			kwargs.pop('use_pretrained_backbone'     ,  config.use_pretrained_backbone)
					__snake_case      :							Optional[Any]						=			kwargs.pop('out_indices'     ,  config.out_indices)
					__snake_case      :							Optional[Any]						=			TimmBackboneConfig(
					    backbone=_A     ,  num_channels=_A     ,  features_only=_A     ,  use_pretrained_backbone=_A     ,  out_indices=_A     ,  )
					return super()._from_config(_A     ,  **_A)



				def  _lowercase					(self    :				Optional[Any]     ,  _A    :				Tuple)   ->     str:
					pass






				def  _lowercase					(self    :				Tuple     ,  _A    :				List[Any]     ,  _A    :				Union[str, Any]=None     ,  _A    :				str=None     ,  _A    :				List[Any]=None     ,  **_A    :				str)   ->     Union[BackboneOutput, Tuple[Tensor, ...]]:
					__snake_case      :							List[str]						=			return_dict if return_dict is not None else self.config.use_return_dict
					__snake_case      :							List[Any]						=			(
					    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
					)
					__snake_case      :							Tuple						=			output_attentions if output_attentions is not None else self.config.output_attentions

					if output_attentions:
						raise ValueError('Cannot output attentions for timm backbones at the moment')

					if output_hidden_states:
						# We modify the return layers to include all the stages of the backbone
						__snake_case      :							int						=			self._all_layers
						__snake_case      :							Optional[Any]						=			self._backbone(_A     ,  **_A)
						__snake_case      :							Dict						=			self._return_layers
						__snake_case      :							str						=			tuple(hidden_states[i] for i in self.out_indices)
					else:
						__snake_case      :							str						=			self._backbone(_A     ,  **_A)
						__snake_case      :							int						=			None

					__snake_case      :							Dict						=			tuple(_A)
					__snake_case      :							List[str]						=			tuple(_A) if hidden_states is not None else None

					if not return_dict:
						__snake_case      :							Any						=			(feature_maps,)
						if output_hidden_states:
							__snake_case      :							int						=			output + (hidden_states,)
						return output

					return BackboneOutput(feature_maps=_A     ,  hidden_states=_A     ,  attentions=_A)






"""simple docstring"""







import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_funnel import FunnelTokenizer


_a					:					Any=			logging.get_logger(__name__)

_a					:					str=			{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

_a					:					Optional[Any]=			[
    "small",
    "small-base",
    "medium",
    "medium-base",
    "intermediate",
    "intermediate-base",
    "large",
    "large-base",
    "xlarge",
    "xlarge-base",
]

_a					:					List[Any]=			{
    "vocab_file": {
        "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt",
        "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt",
        "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt",
        "funnel-transformer/medium-base": (
            "https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt"
        ),
        "funnel-transformer/intermediate": (
            "https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt"
        ),
        "funnel-transformer/intermediate-base": (
            "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt"
        ),
        "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt",
        "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt",
        "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt",
        "funnel-transformer/xlarge-base": (
            "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json",
        "funnel-transformer/small-base": (
            "https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json",
        "funnel-transformer/medium-base": (
            "https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/intermediate": (
            "https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/intermediate-base": (
            "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json",
        "funnel-transformer/large-base": (
            "https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json",
        "funnel-transformer/xlarge-base": (
            "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json"
        ),
    },
}
_a					:					str=			{f'''funnel-transformer/{name}''': 512 for name in _model_names}
_a					:					List[Any]=			{f'''funnel-transformer/{name}''': {"do_lower_case": True} for name in _model_names}

class 		UpperCamelCase      (   lowercase						):
				UpperCAmelCase    :				List[str]   =		VOCAB_FILES_NAMES
				UpperCAmelCase    :				Tuple   =		PRETRAINED_VOCAB_FILES_MAP
				UpperCAmelCase    :				int   =		PRETRAINED_INIT_CONFIGURATION
				UpperCAmelCase    :				Tuple   =		FunnelTokenizer
				UpperCAmelCase    :				Tuple   =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				UpperCAmelCase    :				int   =		2



				def __init__(self    :				int     ,  _A    :				Any=None     ,  _A    :				Union[str, Any]=None     ,  _A    :				Union[str, Any]=True     ,  _A    :				List[str]="<unk>"     ,  _A    :				Any="<sep>"     ,  _A    :				Dict="<pad>"     ,  _A    :				Tuple="<cls>"     ,  _A    :				Dict="<mask>"     ,  _A    :				Optional[Any]="<s>"     ,  _A    :				List[Any]="</s>"     ,  _A    :				Optional[int]=True     ,  _A    :				Dict=True     ,  _A    :				Tuple=None     ,  _A    :				int="##"     ,  **_A    :				Any     ,  )   ->     str:
					super().__init__(
					    _A     ,  tokenizer_file=_A     ,  do_lower_case=_A     ,  unk_token=_A     ,  sep_token=_A     ,  pad_token=_A     ,  cls_token=_A     ,  mask_token=_A     ,  bos_token=_A     ,  eos_token=_A     ,  clean_text=_A     ,  tokenize_chinese_chars=_A     ,  strip_accents=_A     ,  wordpieces_prefix=_A     ,  **_A     ,  )

					__snake_case      :							Optional[int]						=			json.loads(self.backend_tokenizer.normalizer.__getstate__())
					if (
					    normalizer_state.get('lowercase'     ,  _A) != do_lower_case
					    or normalizer_state.get('strip_accents'     ,  _A) != strip_accents
					    or normalizer_state.get('handle_chinese_chars'     ,  _A) != tokenize_chinese_chars
					):
						__snake_case      :							List[str]						=			getattr(_A     ,  normalizer_state.pop('type'))
						__snake_case      :							int						=			do_lower_case
						__snake_case      :							Optional[int]						=			strip_accents
						__snake_case      :							str						=			tokenize_chinese_chars
						__snake_case      :							Optional[int]						=			normalizer_class(**_A)

					__snake_case      :							str						=			do_lower_case



				def  _lowercase					(self    :				Optional[Any]     ,  _A    :				Dict     ,  _A    :				Tuple=None)   ->     Any:
					__snake_case      :							List[Any]						=			[self.cls_token_id] + token_ids_a + [self.sep_token_id]

					if token_ids_a:
						output += token_ids_a + [self.sep_token_id]

					return output



				def  _lowercase					(self    :				str     ,  _A    :				List[int]     ,  _A    :				Optional[List[int]] = None)   ->     List[int]:
					__snake_case      :							Union[str, Any]						=			[self.sep_token_id]
					__snake_case      :							Union[str, Any]						=			[self.cls_token_id]
					if token_ids_a is None:
						return len(cls) * [self.cls_token_type_id] + len(token_ids_a + sep) * [0]
					return len(cls) * [self.cls_token_type_id] + len(token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1]






				def  _lowercase					(self    :				Tuple     ,  _A    :				str     ,  _A    :				Optional[str] = None)   ->     Tuple[str]:
					__snake_case      :							int						=			self._tokenizer.model.save(_A     ,  name=_A)
					return tuple(_A)





