




'''simple docstring'''







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 			_a  :
       def __init__(	self						:	List[str]   ,				lowercase						:	Optional[int]   ,				lowercase						:	List[str]=None   ,				lowercase						:	Tuple=None   ,				lowercase						:	int=None   ,				lowercase						:	List[str]="resnet50"   ,				lowercase						:	Optional[Any]=3   ,				lowercase						:	str=32   ,				lowercase						:	List[Any]=3   ,				lowercase						:	int=True   ,				lowercase						:	Any=True   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		parent
           UpperCAmelCase			       =		out_indices if out_indices is not None else [4]
           UpperCAmelCase			       =		stage_names
           UpperCAmelCase			       =		out_features
           UpperCAmelCase			       =		backbone
           UpperCAmelCase			       =		batch_size
           UpperCAmelCase			       =		image_size
           UpperCAmelCase			       =		num_channels
           UpperCAmelCase			       =		use_pretrained_backbone
           UpperCAmelCase			       =		is_training
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           UpperCAmelCase			       =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)
           UpperCAmelCase			       =		self.get_config()

           return config, pixel_values
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           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     A      (	self						:	Tuple   ,				lowercase						:	List[Any]   ,				lowercase						:	Union[str, Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		TimmBackbone(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           with torch.no_grad():
               UpperCAmelCase			       =		model(lowercase				)
           self.parent.assertEqual(
               result.feature_map[-1].shape   ,				(self.batch_size, model.channels[-1], 14, 14)   ,				)







       def     A      (	self						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.prepare_config_and_inputs()
           UpperCAmelCase       ,     UpperCAmelCase			       =		config_and_inputs
           UpperCAmelCase			       =		{'''pixel_values''': pixel_values}
           return config, inputs_dict






@require_torch
@require_timm
class 			_a  (	__a							,							__a							,							__a							,							unittest.TestCase ):
       __a				:     Optional[int]   				=					(TimmBackbone,) if is_torch_available() else ()
       __a				:     Dict   				=					{"""feature-extraction""": TimmBackbone} if is_torch_available() else {}
       __a				:     Optional[Any]   				=					False
       __a				:     List[str]   				=					False
       __a				:     int   				=					False
       __a				:     Optional[Any]   				=					False
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		TimmBackboneModelTester(self				)
           UpperCAmelCase			       =		ConfigTester(self   ,				config_class=lowercase   ,				has_text_modality=lowercase				)
       def     A      (	self						:	str				):



           '''simple docstring'''

           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     A      (	self						:	Tuple				):



           '''simple docstring'''

           UpperCAmelCase			       =		'''resnet18'''
           UpperCAmelCase			       =		'''microsoft/resnet-18'''

           UpperCAmelCase			       =		AutoBackbone.from_pretrained(lowercase   ,				use_timm_backbone=lowercase				)
           UpperCAmelCase			       =		AutoBackbone.from_pretrained(lowercase				)

           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]				)

           UpperCAmelCase			       =		AutoBackbone.from_pretrained(lowercase   ,				use_timm_backbone=lowercase   ,				out_indices=[1, 2, 3]				)
           UpperCAmelCase			       =		AutoBackbone.from_pretrained(lowercase   ,				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     A      (	self						:	int				):



           '''simple docstring'''

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



           '''simple docstring'''

           pass
       @unittest.skip('''TimmBackbone initialization is managed on the timm side'''				)
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

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



           '''simple docstring'''

           pass
       @unittest.skip('''TimmBackbone models doesn\'t have inputs_embeds'''				)
       def     A      (	self						:	List[str]				):



           '''simple docstring'''

           pass
       @unittest.skip('''TimmBackbone model cannot be created without specifying a backbone checkpoint'''				)
       def     A      (	self						:	int				):



           '''simple docstring'''

           pass
       @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''				)
       def     A      (	self						:	Any				):



           '''simple docstring'''

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



           '''simple docstring'''

           pass
       @unittest.skip('''model weights aren\'t tied in TimmBackbone.'''				)
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           pass
       @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''				)
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           pass
       @unittest.skip('''Only checkpoints on timm can be loaded into TimmBackbone'''				)
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

           pass
       @unittest.skip('''TimmBackbone doesn\'t have hidden size info in its configuration.'''				)
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           pass
       @unittest.skip('''TimmBackbone doesn\'t support output_attentions.'''				)
       def     A      (	self						:	Tuple				):



           '''simple docstring'''

           pass
       @unittest.skip('''Safetensors is not supported by timm.'''				)
       def     A      (	self						:	Any				):



           '''simple docstring'''

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



           '''simple docstring'''

           pass
       def     A      (	self						:	Tuple				):



           '''simple docstring'''

           UpperCAmelCase       ,     UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs_for_common()

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

               UpperCAmelCase			       =		['''pixel_values''']
               self.assertListEqual(arg_names[:1]   ,				lowercase				)
       def     A      (	self						:	str				):



           '''simple docstring'''

           UpperCAmelCase       ,     UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs_for_common()
           UpperCAmelCase			       =		True
           UpperCAmelCase			       =		self.has_attentions

           # no need to test all models as different heads yield the same functionality
           UpperCAmelCase			       =		self.all_model_classes[0]
           UpperCAmelCase			       =		model_class(lowercase				)
           model.to(lowercase				)

           UpperCAmelCase			       =		self._prepare_for_class(lowercase   ,				lowercase				)
           UpperCAmelCase			       =		model(**lowercase				)
           UpperCAmelCase			       =		outputs[0][-1]

           # Encoder-/Decoder-only models
           UpperCAmelCase			       =		outputs.hidden_states[0]
           hidden_states.retain_grad()

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

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

           self.assertIsNotNone(hidden_states.grad				)

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







       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           UpperCAmelCase       ,     UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase			       =		model_class(lowercase				)
               model.to(lowercase				)
               model.eval()
               UpperCAmelCase			       =		model(**lowercase				)

               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
               UpperCAmelCase			       =		copy.deepcopy(lowercase				)
               UpperCAmelCase			       =		None
               UpperCAmelCase			       =		model_class(lowercase				)
               model.to(lowercase				)
               model.eval()
               UpperCAmelCase			       =		model(**lowercase				)

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

               # Check backbone can be initialized with fresh weights
               UpperCAmelCase			       =		copy.deepcopy(lowercase				)
               UpperCAmelCase			       =		False
               UpperCAmelCase			       =		model_class(lowercase				)
               model.to(lowercase				)
               model.eval()
               UpperCAmelCase			       =		model(**lowercase				)








import unittest

from transformers.models.xlm_prophetnet.tokenization_xlm_prophetnet import SPIECE_UNDERLINE, XLMProphetNetTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


lowerCamelCase											=       get_tests_dir('fixtures/test_sentencepiece.model')




@require_sentencepiece
class A  (   UpperCamelCase_    ,							unittest.TestCase     ):
      UpperCamelCase__							:  str      =XLMProphetNetTokenizer
      UpperCamelCase__							:  Any      =False
      UpperCamelCase__							:  Optional[Any]      =True






      def        lowerCamelCase       (					self						:     Tuple )					->						Optional[Any]:
             """simple docstring"""




             super().setUp()

             # We have a SentencePiece fixture for testing
             _lowerCamelCase       :		Union[str, Any]		=XLMProphetNetTokenizer(lowercase_			,   keep_accents=lowercase_ )
             tokenizer.save_pretrained(self.tmpdirname )






      def        lowerCamelCase       (					self						:     int )					->						List[str]:
             """simple docstring"""




             _lowerCamelCase       :		Tuple		='[PAD]'
             _lowerCamelCase       :		Dict		=0

             self.assertEqual(self.get_tokenizer()._convert_token_to_id(lowercase_ )			,   lowercase_ )
             self.assertEqual(self.get_tokenizer()._convert_id_to_token(lowercase_ )			,   lowercase_ )






      def        lowerCamelCase       (					self						:     Optional[Any] )					->						int:
             """simple docstring"""




             _lowerCamelCase       :		int		=list(self.get_tokenizer().get_vocab().keys() )

             self.assertEqual(vocab_keys[0]			,   '[PAD]' )
             self.assertEqual(vocab_keys[1]			,   '[CLS]' )
             self.assertEqual(vocab_keys[-1]			,   'j' )
             self.assertEqual(len(lowercase_ )			,   1012 )






      def        lowerCamelCase       (					self						:     Optional[int] )					->						Any:
             """simple docstring"""




             self.assertEqual(self.get_tokenizer().vocab_size			,   1012 )






      def        lowerCamelCase       (					self						:     Dict )					->						Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase       :		List[Any]		=XLMProphetNetTokenizer(lowercase_			,   keep_accents=lowercase_ )

             _lowerCamelCase       :		Any		=tokenizer.tokenize('This is a test' )
             self.assertListEqual(lowercase_			,   ['▁This', '▁is', '▁a', '▁t', 'est'] )

             self.assertListEqual(
                 tokenizer.convert_tokens_to_ids(lowercase_ )			,   [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]			,   )

             _lowerCamelCase       :		Dict		=tokenizer.tokenize('I was born in 92000, and this is falsé.' )
             self.assertListEqual(
                 lowercase_			,   [
                     SPIECE_UNDERLINE + 'I',
                     SPIECE_UNDERLINE + 'was',
                     SPIECE_UNDERLINE + 'b',
                     'or',
                     'n',
                     SPIECE_UNDERLINE + 'in',
                     SPIECE_UNDERLINE + '',
                     '9',
                     '2',
                     '0',
                     '0',
                     '0',
                     ',',
                     SPIECE_UNDERLINE + 'and',
                     SPIECE_UNDERLINE + 'this',
                     SPIECE_UNDERLINE + 'is',
                     SPIECE_UNDERLINE + 'f',
                     'al',
                     's',
                     'é',
                     '.',
                 ]			,   )
             _lowerCamelCase       :		Union[str, Any]		=tokenizer.convert_tokens_to_ids(lowercase_ )
             self.assertListEqual(
                 lowercase_			,   [
                     value + tokenizer.fairseq_offset
                     for value in [8, 21, 84, 55, 24, 19, 7, -9, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, -9, 4]
                 ]			,   )

             _lowerCamelCase       :		int		=tokenizer.convert_ids_to_tokens(lowercase_ )
             self.assertListEqual(
                 lowercase_			,   [
                     SPIECE_UNDERLINE + 'I',
                     SPIECE_UNDERLINE + 'was',
                     SPIECE_UNDERLINE + 'b',
                     'or',
                     'n',
                     SPIECE_UNDERLINE + 'in',
                     SPIECE_UNDERLINE + '',
                     '[UNK]',
                     '2',
                     '0',
                     '0',
                     '0',
                     ',',
                     SPIECE_UNDERLINE + 'and',
                     SPIECE_UNDERLINE + 'this',
                     SPIECE_UNDERLINE + 'is',
                     SPIECE_UNDERLINE + 'f',
                     'al',
                     's',
                     '[UNK]',
                     '.',
                 ]			,   )






      @cached_property
      def        lowerCamelCase       (					self						:     Any )					->						Optional[Any]:
             """simple docstring"""




             return XLMProphetNetTokenizer.from_pretrained('microsoft/xprophetnet-large-wiki100-cased' )






      @slow
      def        lowerCamelCase       (					self						:     List[str] )					->						Any:
             """simple docstring"""




             _lowerCamelCase       :		Optional[int]		='Hello World!'
             _lowerCamelCase       :		Optional[int]		=[3_5389, 6672, 49, 2]
             self.assertListEqual(lowercase_			,   self.big_tokenizer.encode(lowercase_ ) )






      @slow
      def        lowerCamelCase       (					self						:     Optional[int] )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		Dict		={'input_ids': [[1_1073, 8_2783, 18, 26, 8_2783, 549, 5_1540, 248, 1_7209, 1301, 217, 20, 21_5186, 1325, 147, 1_7209, 1301, 217, 20, 5_6370, 53, 12_2020, 20, 1_6477, 27, 8_7355, 4548, 20, 4728, 7_8392, 17, 15_9969, 18, 26, 2_4491, 629, 15, 538, 2_2704, 5439, 15, 2788, 2_4491, 9885, 15, 4_3534, 605, 15, 814, 1_8403, 3_3200, 29, 15, 4_3534, 2_4458, 1_2410, 111, 2_4966, 8_3669, 9637, 14_4068, 26, 850, 2_2346, 27, 147, 2_4966, 8_3669, 8_3490, 26, 3_9113, 735, 27, 689, 656, 2800, 1339, 4600, 53, 12_2020, 11_5785, 34, 816, 1339, 4_6887, 18, 147, 5_3905, 1951, 4_2238, 4_1170, 1_7732, 834, 436, 15, 2_7523, 9_8733, 217, 147, 5542, 4981, 930, 1_7347, 16, 2], [2_0091, 629, 94, 8_2786, 58, 490, 20, 1528, 84, 5_3905, 344, 8_0592, 11_0128, 1_8822, 5267, 1306, 62, 15_2537, 308, 7997, 401, 12_4427, 549, 3_5442, 225, 109, 1_5055, 2_5748, 147, 7119, 4_3712, 34, 767, 13_5366, 18, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [592, 6_3784, 11_9466, 17, 14_7808, 8_8214, 18, 656, 81, 32, 3296, 1_0280, 16, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
             # fmt: on

             self.tokenizer_integration_test_util(
                 expected_encoding=lowercase_			,   model_name='microsoft/xprophetnet-large-wiki100-cased'			,   revision='1acad1643ddd54a44df6a1b797ada8373685d90e'			,   )


