






import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
	import torch

	from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin



class 							SCREAMING_SNAKE_CASE__						:





					"""simple docstring"""






					def __init__( self					:  str							,		__A					:  str							,		__A					:  int=None							,		__A					:  List[Any]=None							,		__A					:  Any=None							,		__A					:  Union[str, Any]="resnet50"							,		__A					:  Union[str, Any]=3							,		__A					:  List[str]=3_2							,		__A					:  str=3							,		__A					:  Tuple=True							,		__A					:  List[str]=True							,		):
						snake_case__			:				Optional[int]       =  parent
						snake_case__			:				Optional[Any]       =  out_indices if out_indices is not None else [4]
						snake_case__			:				Dict       =  stage_names
						snake_case__			:				str       =  out_features
						snake_case__			:				List[Any]       =  backbone
						snake_case__			:				Dict       =  batch_size
						snake_case__			:				Tuple       =  image_size
						snake_case__			:				int       =  num_channels
						snake_case__			:				Union[str, Any]       =  use_pretrained_backbone
						snake_case__			:				int       =  is_training



					def 	_lowercase					( self					:  int	):
						snake_case__			:				int       =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)
						snake_case__			:				Optional[int]       =  self.get_config()

						return config, pixel_values



					def 	_lowercase					( self					:  Union[str, Any]	):
						return TimmBackboneConfig(
						    image_size=self.image_size							,		num_channels=self.num_channels							,		out_features=self.out_features							,		out_indices=self.out_indices							,		stage_names=self.stage_names							,		use_pretrained_backbone=self.use_pretrained_backbone							,		backbone=self.backbone							,		)



					def 	_lowercase					( self					:  int							,		__A					:  Optional[Any]							,		__A					:  int	):
						snake_case__			:				Any       =  TimmBackbone(config=__A	)
						model.to(__A	)
						model.eval()
						with torch.no_grad():
							snake_case__			:				Optional[int]       =  model(__A	)
						self.parent.assertEqual(
						    result.feature_map[-1].shape							,		(self.batch_size, model.channels[-1], 1_4, 1_4)							,		)






					def 	_lowercase					( self					:  str	):
						snake_case__			:				Tuple       =  self.prepare_config_and_inputs()
						snake_case__,						snake_case__			:				Optional[int]       =  config_and_inputs
						snake_case__			:				List[Any]       =  {"pixel_values": pixel_values}
						return config, inputs_dict



@require_torch
@require_timm
class 							SCREAMING_SNAKE_CASE__						(    UpperCamelCase_			,       UpperCamelCase_			,       UpperCamelCase_			,       unittest.TestCase						):





					"""simple docstring"""






					a_     =		(TimmBackbone,) if is_torch_available() else ()
					a_     =		{"feature-extraction": TimmBackbone} if is_torch_available() else {}
					a_     =		False
					a_     =		False
					a_     =		False
					a_     =		False



					def 	_lowercase					( self					:  List[Any]	):
						snake_case__			:				List[Any]       =  TimmBackboneModelTester(self	)
						snake_case__			:				Tuple       =  ConfigTester(self							,		config_class=__A							,		has_text_modality=__A	)



					def 	_lowercase					( self					:  List[Any]	):
						self.config_tester.create_and_test_config_to_json_string()
						self.config_tester.create_and_test_config_to_json_file()
						self.config_tester.create_and_test_config_from_and_save_pretrained()
						self.config_tester.create_and_test_config_with_num_labels()
						self.config_tester.check_config_can_be_init_without_params()
						self.config_tester.check_config_arguments_init()



					def 	_lowercase					( self					:  str	):
						snake_case__			:				str       =  "resnet18"
						snake_case__			:				Any       =  "microsoft/resnet-18"

						snake_case__			:				Union[str, Any]       =  AutoBackbone.from_pretrained(__A							,		use_timm_backbone=__A	)
						snake_case__			:				Optional[Any]       =  AutoBackbone.from_pretrained(__A	)

						self.assertEqual(len(timm_model.out_features	)							,		len(transformers_model.out_features	)	)
						self.assertEqual(len(timm_model.stage_names	)							,		len(transformers_model.stage_names	)	)
						self.assertEqual(timm_model.channels							,		transformers_model.channels	)
						# Out indices are set to the last layer by default. For timm models, we don't know
						# the number of layers in advance, so we set it to (-1,), whereas for transformers
						# models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
						self.assertEqual(timm_model.out_indices							,		(-1,)	)
						self.assertEqual(transformers_model.out_indices							,		[len(timm_model.stage_names	) - 1]	)

						snake_case__			:				List[Any]       =  AutoBackbone.from_pretrained(__A							,		use_timm_backbone=__A							,		out_indices=[1, 2, 3]	)
						snake_case__			:				str       =  AutoBackbone.from_pretrained(__A							,		out_indices=[1, 2, 3]	)

						self.assertEqual(timm_model.out_indices							,		transformers_model.out_indices	)
						self.assertEqual(len(timm_model.out_features	)							,		len(transformers_model.out_features	)	)
						self.assertEqual(timm_model.channels							,		transformers_model.channels	)



					@unittest.skip("TimmBackbone doesn't support feed forward chunking"	)
					def 	_lowercase					( self					:  int	):
						pass



					@unittest.skip("TimmBackbone doesn't have num_hidden_layers attribute"	)
					def 	_lowercase					( self					:  int	):
						pass



					@unittest.skip("TimmBackbone initialization is managed on the timm side"	)
					def 	_lowercase					( self					:  int	):
						pass



					@unittest.skip("TimmBackbone models doesn't have inputs_embeds"	)
					def 	_lowercase					( self					:  int	):
						pass



					@unittest.skip("TimmBackbone models doesn't have inputs_embeds"	)
					def 	_lowercase					( self					:  Union[str, Any]	):
						pass



					@unittest.skip("TimmBackbone model cannot be created without specifying a backbone checkpoint"	)
					def 	_lowercase					( self					:  List[Any]	):
						pass



					@unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"	)
					def 	_lowercase					( self					:  List[str]	):
						pass



					@unittest.skip("model weights aren't tied in TimmBackbone."	)
					def 	_lowercase					( self					:  str	):
						pass



					@unittest.skip("model weights aren't tied in TimmBackbone."	)
					def 	_lowercase					( self					:  Tuple	):
						pass



					@unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"	)
					def 	_lowercase					( self					:  List[Any]	):
						pass



					@unittest.skip("Only checkpoints on timm can be loaded into TimmBackbone"	)
					def 	_lowercase					( self					:  List[Any]	):
						pass



					@unittest.skip("TimmBackbone doesn't have hidden size info in its configuration."	)
					def 	_lowercase					( self					:  int	):
						pass



					@unittest.skip("TimmBackbone doesn't support output_attentions."	)
					def 	_lowercase					( self					:  List[Any]	):
						pass



					@unittest.skip("Safetensors is not supported by timm."	)
					def 	_lowercase					( self					:  int	):
						pass



					@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."	)
					def 	_lowercase					( self					:  Dict	):
						pass



					def 	_lowercase					( self					:  str	):
						snake_case__,						snake_case__			:				Tuple       =  self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							snake_case__			:				int       =  model_class(__A	)
							snake_case__			:				Optional[int]       =  inspect.signature(model.forward	)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							snake_case__			:				Any       =  [*signature.parameters.keys()]

							snake_case__			:				str       =  ["pixel_values"]
							self.assertListEqual(arg_names[:1]							,		__A	)



					def 	_lowercase					( self					:  Union[str, Any]	):
						snake_case__,						snake_case__			:				Tuple       =  self.model_tester.prepare_config_and_inputs_for_common()
						snake_case__			:				List[str]       =  True
						snake_case__			:				Optional[Any]       =  self.has_attentions

						# no need to test all models as different heads yield the same functionality
						snake_case__			:				Tuple       =  self.all_model_classes[0]
						snake_case__			:				Any       =  model_class(__A	)
						model.to(__A	)

						snake_case__			:				List[str]       =  self._prepare_for_class(__A							,		__A	)
						snake_case__			:				Union[str, Any]       =  model(**__A	)
						snake_case__			:				int       =  outputs[0][-1]

						# Encoder-/Decoder-only models
						snake_case__			:				List[Any]       =  outputs.hidden_states[0]
						hidden_states.retain_grad()

						if self.has_attentions:
							snake_case__			:				Tuple       =  outputs.attentions[0]
							attentions.retain_grad()

						output.flatten()[0].backward(retain_graph=__A	)

						self.assertIsNotNone(hidden_states.grad	)

						if self.has_attentions:
							self.assertIsNotNone(attentions.grad	)






					def 	_lowercase					( self					:  Tuple	):
						snake_case__,						snake_case__			:				str       =  self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
							snake_case__			:				Dict       =  model_class(__A	)
							model.to(__A	)
							model.eval()
							snake_case__			:				Any       =  model(**__A	)

							self.assertEqual(len(result.feature_maps	)							,		len(config.out_indices	)	)
							self.assertEqual(len(model.channels	)							,		len(config.out_indices	)	)

							# Check output of last stage is taken if out_features=None, out_indices=None
							snake_case__			:				Optional[int]       =  copy.deepcopy(__A	)
							snake_case__			:				Optional[Any]       =  None
							snake_case__			:				int       =  model_class(__A	)
							model.to(__A	)
							model.eval()
							snake_case__			:				Union[str, Any]       =  model(**__A	)

							self.assertEqual(len(result.feature_maps	)							,		1	)
							self.assertEqual(len(model.channels	)							,		1	)

							# Check backbone can be initialized with fresh weights
							snake_case__			:				Tuple       =  copy.deepcopy(__A	)
							snake_case__			:				List[Any]       =  False
							snake_case__			:				str       =  model_class(__A	)
							model.to(__A	)
							model.eval()
							snake_case__			:				Dict       =  model(**__A	)











import json
from typing import Dict, List, Optional, Tuple, Union

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding, EncodedInput
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_led import LEDTokenizer


__lowerCamelCase :      int										=				logging.get_logger(__name__)


__lowerCamelCase :      List[str]										=				{"""vocab_file""": """vocab.json""", """merges_file""": """merges.txt""", """tokenizer_file""": """tokenizer.json"""}

__lowerCamelCase :      Any										=				{
    """vocab_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/vocab.json""",
    },
    """merges_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/merges.txt""",
    },
    """tokenizer_file""": {
        """allenai/led-base-16384""": """https://huggingface.co/allenai/led-base-16384/resolve/main/tokenizer.json""",
    },
}

__lowerCamelCase :      Optional[int]										=				{
    """allenai/led-base-16384""": 1_6384,
}



class 							SCREAMING_SNAKE_CASE__						(    UpperCamelCase_						):





					"""simple docstring"""






					a_     =		VOCAB_FILES_NAMES
					a_     =		PRETRAINED_VOCAB_FILES_MAP
					a_     =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					a_     =		LEDTokenizer
					a_     =		["input_ids", "attention_mask"]



					def __init__( self					:  int							,		__A					:  Optional[int]=None							,		__A					:  Any=None							,		__A					:  Any=None							,		__A					:  int="replace"							,		__A					:  Optional[Any]="<s>"							,		__A					:  List[Any]="</s>"							,		__A					:  Dict="</s>"							,		__A					:  Union[str, Any]="<s>"							,		__A					:  List[Any]="<unk>"							,		__A					:  Optional[Any]="<pad>"							,		__A					:  Optional[int]="<mask>"							,		__A					:  str=False							,		__A					:  Optional[Any]=True							,		**__A					:  List[Any]							,		):
						super().__init__(
						    __A							,		__A							,		tokenizer_file=__A							,		errors=__A							,		bos_token=__A							,		eos_token=__A							,		sep_token=__A							,		cls_token=__A							,		unk_token=__A							,		pad_token=__A							,		mask_token=__A							,		add_prefix_space=__A							,		trim_offsets=__A							,		**__A							,		)

						snake_case__			:				List[Any]       =  json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()	)
						if pre_tok_state.get("add_prefix_space"							,		__A	) != add_prefix_space:
							snake_case__			:				List[str]       =  getattr(__A							,		pre_tok_state.pop("type"	)	)
							snake_case__			:				Tuple       =  add_prefix_space
							snake_case__			:				Any       =  pre_tok_class(**__A	)

						snake_case__			:				List[Any]       =  add_prefix_space

						# the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
						snake_case__			:				str       =  "post_processor"
						snake_case__			:				Union[str, Any]       =  getattr(self.backend_tokenizer							,		__A							,		__A	)
						if tokenizer_component_instance:
							snake_case__			:				Any       =  json.loads(tokenizer_component_instance.__getstate__()	)

							# The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
							if "sep" in state:
								snake_case__			:				int       =  tuple(state["sep"]	)
							if "cls" in state:
								snake_case__			:				Optional[int]       =  tuple(state["cls"]	)

							snake_case__			:				Union[str, Any]       =  False

							if state.get("add_prefix_space"							,		__A	) != add_prefix_space:
								snake_case__			:				Optional[Any]       =  add_prefix_space
								snake_case__			:				List[str]       =  True

							if state.get("trim_offsets"							,		__A	) != trim_offsets:
								snake_case__			:				str       =  trim_offsets
								snake_case__			:				Tuple       =  True

							if changes_to_apply:
								snake_case__			:				Any       =  getattr(__A							,		state.pop("type"	)	)
								snake_case__			:				Optional[Any]       =  component_class(**__A	)
								setattr(self.backend_tokenizer							,		__A							,		__A	)



					@property
					# Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.mask_token with BART->LED
					def 	_lowercase					( self					:  Union[str, Any]	):
						if self._mask_token is None:
							if self.verbose:
								logger.error("Using mask_token, but it is not set yet."	)
							return None
						return str(self._mask_token	)



					@mask_token.setter
					def 	_lowercase					( self					:  str							,		__A					:  int	):
						snake_case__			:				str       =  AddedToken(__A							,		lstrip=__A							,		rstrip=__A	) if isinstance(__A							,		__A	) else value
						snake_case__			:				Optional[int]       =  value



					def 	_lowercase					( self					:  List[str]							,		*__A					:  Union[str, Any]							,		**__A					:  int	):
						snake_case__			:				Dict       =  kwargs.get("is_split_into_words"							,		__A	)

						if is_split_into_words and not self.add_prefix_space:
							raise ValueError(
							    f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
							    "to use it with pretokenized inputs."	)

						return super()._batch_encode_plus(*__A							,		**__A	)



					def 	_lowercase					( self					:  Union[str, Any]							,		*__A					:  Tuple							,		**__A					:  Union[str, Any]	):
						snake_case__			:				List[str]       =  kwargs.get("is_split_into_words"							,		__A	)

						if is_split_into_words and not self.add_prefix_space:
							raise ValueError(
							    f'''You need to instantiate {self.__class__.__name__} with add_prefix_space=True '''
							    "to use it with pretokenized inputs."	)

						return super()._encode_plus(*__A							,		**__A	)



					def 	_lowercase					( self					:  Union[str, Any]							,		__A					:  str							,		__A					:  Optional[str] = None	):
						snake_case__			:				str       =  self._tokenizer.model.save(__A							,		name=__A	)
						return tuple(__A	)



					def 	_lowercase					( self					:  Union[str, Any]							,		__A					:  int							,		__A					:  str=None	):
						snake_case__			:				int       =  [self.bos_token_id] + token_ids_a + [self.eos_token_id]
						if token_ids_a is None:
							return output

						return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]



					def 	_lowercase					( self					:  Any							,		__A					:  List[int]							,		__A					:  Optional[List[int]] = None	):
						snake_case__			:				Optional[Any]       =  [self.sep_token_id]
						snake_case__			:				Any       =  [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 	_lowercase					( self					:  str							,		__A					:  Union[Dict[str, EncodedInput], BatchEncoding]							,		__A					:  Optional[int] = None							,		__A					:  PaddingStrategy = PaddingStrategy.DO_NOT_PAD							,		__A					:  Optional[int] = None							,		__A					:  Optional[bool] = None							,		):
						snake_case__			:				Dict       =  super()._pad(
						    encoded_inputs=__A							,		max_length=__A							,		padding_strategy=__A							,		pad_to_multiple_of=__A							,		return_attention_mask=__A							,		)

						# Load from model defaults
						if return_attention_mask is None:
							snake_case__			:				Optional[Any]       =  "attention_mask" in self.model_input_names

						if return_attention_mask and "global_attention_mask" in encoded_inputs:
							snake_case__			:				List[Any]       =  encoded_inputs[self.model_input_names[0]]
							# `global_attention_mask` need to have the same length as other (sequential) inputs.
							snake_case__			:				Union[str, Any]       =  len(encoded_inputs["global_attention_mask"]	) != len(__A	)

							if needs_to_be_padded:
								snake_case__			:				Dict       =  len(__A	) - len(encoded_inputs["global_attention_mask"]	)

								if self.padding_side == "right":
									# Use `-1` since `0` in `global_attention_mask` means `local attention` instead of `not to attend`
									snake_case__			:				Optional[Any]       =  (
									    encoded_inputs["global_attention_mask"] + [-1] * difference
									)
								elif self.padding_side == "left":
									snake_case__			:				Optional[Any]       =  [-1] * difference + encoded_inputs[
									    "global_attention_mask"
									]
								else:
									raise ValueError("Invalid padding strategy:" + str(self.padding_side	)	)

						return encoded_inputs



