



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    UpperCAmelCase__:

   '''simple docstring'''




   def __init__(    self	:      Any      ,					lowerCAmelCase	:      str      ,					lowerCAmelCase	:      Optional[Any]=None      ,					lowerCAmelCase	:      str=None      ,					lowerCAmelCase	:      str=None      ,					lowerCAmelCase	:      Any="resnet50"      ,					lowerCAmelCase	:      Optional[Any]=3      ,					lowerCAmelCase	:      Any=32      ,					lowerCAmelCase	:      List[Any]=3      ,					lowerCAmelCase	:      Union[str, Any]=True      ,					lowerCAmelCase	:      str=True      ,					)						->					int:




         """simple docstring"""

         lowercase__  =	parent
         lowercase__  =	out_indices if out_indices is not None else [4]
         lowercase__  =	stage_names
         lowercase__  =	out_features
         lowercase__  =	backbone
         lowercase__  =	batch_size
         lowercase__  =	image_size
         lowercase__  =	num_channels
         lowercase__  =	use_pretrained_backbone
         lowercase__  =	is_training


   def       UpperCAmelCase		(    self	:      str)						->					List[Any]:




         """simple docstring"""

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

         return config, pixel_values


   def       UpperCAmelCase		(    self	:      List[Any])						->					List[Any]:




         """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       UpperCAmelCase		(    self	:      Optional[int]      ,					lowerCAmelCase	:      Optional[Any]      ,					lowerCAmelCase	:      Tuple)						->					Any:




         """simple docstring"""

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



   def       UpperCAmelCase		(    self	:      Optional[Any])						->					int:




         """simple docstring"""

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



@require_torch
@require_timm
class    UpperCAmelCase__(				lowerCamelCase  ,				lowerCamelCase  ,				lowerCamelCase  ,				unittest.TestCase						):

   '''simple docstring'''




   A     :	int												=     (TimmBackbone,) if is_torch_available() else ()
   A     :	str												=     {"feature-extraction": TimmBackbone} if is_torch_available() else {}
   A     :	int												=     False
   A     :	int												=     False
   A     :	Union[str, Any]												=     False
   A     :	Tuple												=     False


   def       UpperCAmelCase		(    self	:      Tuple)						->					Optional[int]:




         """simple docstring"""

         lowercase__  =	TimmBackboneModelTester(self)
         lowercase__  =	ConfigTester(self      ,					config_class=lowerCAmelCase      ,					has_text_modality=lowerCAmelCase)


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




         """simple docstring"""

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

         lowercase__  =	AutoBackbone.from_pretrained(lowerCAmelCase      ,					use_timm_backbone=lowerCAmelCase)
         lowercase__  =	AutoBackbone.from_pretrained(lowerCAmelCase)

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

         lowercase__  =	AutoBackbone.from_pretrained(lowerCAmelCase      ,					use_timm_backbone=lowerCAmelCase      ,					out_indices=[1, 2, 3])
         lowercase__  =	AutoBackbone.from_pretrained(lowerCAmelCase      ,					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       UpperCAmelCase		(    self	:      Any)						->					Optional[Any]:




         """simple docstring"""

         pass


   @unittest.skip('TimmBackbone doesn\'t have num_hidden_layers attribute')
   def       UpperCAmelCase		(    self	:      List[str])						->					Optional[int]:




         """simple docstring"""

         pass


   @unittest.skip('TimmBackbone initialization is managed on the timm side')
   def       UpperCAmelCase		(    self	:      Optional[int])						->					Optional[int]:




         """simple docstring"""

         pass


   @unittest.skip('TimmBackbone models doesn\'t have inputs_embeds')
   def       UpperCAmelCase		(    self	:      int)						->					List[str]:




         """simple docstring"""

         pass


   @unittest.skip('TimmBackbone models doesn\'t have inputs_embeds')
   def       UpperCAmelCase		(    self	:      Tuple)						->					int:




         """simple docstring"""

         pass


   @unittest.skip('TimmBackbone model cannot be created without specifying a backbone checkpoint')
   def       UpperCAmelCase		(    self	:      int)						->					Tuple:




         """simple docstring"""

         pass


   @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
   def       UpperCAmelCase		(    self	:      Tuple)						->					str:




         """simple docstring"""

         pass


   @unittest.skip('model weights aren\'t tied in TimmBackbone.')
   def       UpperCAmelCase		(    self	:      Dict)						->					Tuple:




         """simple docstring"""

         pass


   @unittest.skip('model weights aren\'t tied in TimmBackbone.')
   def       UpperCAmelCase		(    self	:      Tuple)						->					Union[str, Any]:




         """simple docstring"""

         pass


   @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
   def       UpperCAmelCase		(    self	:      List[str])						->					List[Any]:




         """simple docstring"""

         pass


   @unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
   def       UpperCAmelCase		(    self	:      Optional[int])						->					Tuple:




         """simple docstring"""

         pass


   @unittest.skip('TimmBackbone doesn\'t have hidden size info in its configuration.')
   def       UpperCAmelCase		(    self	:      Optional[Any])						->					List[Any]:




         """simple docstring"""

         pass


   @unittest.skip('TimmBackbone doesn\'t support output_attentions.')
   def       UpperCAmelCase		(    self	:      List[str])						->					List[str]:




         """simple docstring"""

         pass


   @unittest.skip('Safetensors is not supported by timm.')
   def       UpperCAmelCase		(    self	:      Tuple)						->					List[Any]:




         """simple docstring"""

         pass


   @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
   def       UpperCAmelCase		(    self	:      Dict)						->					Optional[int]:




         """simple docstring"""

         pass


   def       UpperCAmelCase		(    self	:      Union[str, Any])						->					Dict:




         """simple docstring"""

         lowercase__,						lowercase__  =	self.model_tester.prepare_config_and_inputs_for_common()

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

               lowercase__  =	['pixel_values']
               self.assertListEqual(arg_names[:1]      ,					lowerCAmelCase)


   def       UpperCAmelCase		(    self	:      Union[str, Any])						->					Optional[Any]:




         """simple docstring"""

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

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

         lowercase__  =	self._prepare_for_class(lowerCAmelCase      ,					lowerCAmelCase)
         lowercase__  =	model(**lowerCAmelCase)
         lowercase__  =	outputs[0][-1]

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

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

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

         self.assertIsNotNone(hidden_states.grad)

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



   def       UpperCAmelCase		(    self	:      str)						->					Tuple:




         """simple docstring"""

         lowercase__,						lowercase__  =	self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
               lowercase__  =	model_class(lowerCAmelCase)
               model.to(lowerCAmelCase)
               model.eval()
               lowercase__  =	model(**lowerCAmelCase)

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

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

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





import json
import os
import unittest

from transformers.models.ctrl.tokenization_ctrl import VOCAB_FILES_NAMES, CTRLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin



class    UpperCAmelCase__(				lowerCamelCase  ,				unittest.TestCase						):

   '''simple docstring'''




   A     :	int												=     CTRLTokenizer
   A     :	Tuple												=     False
   A     :	Any												=     False


   def       UpperCAmelCase		(    self	:      int)						->					Any:




         """simple docstring"""

         super().setUp()

         # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
         lowercase__  =	['adapt', 're@@', 'a@@', 'apt', 'c@@', 't', '<unk>']
         lowercase__  =	dict(zip(lowerCAmelCase      ,					range(len(lowerCAmelCase))))
         lowercase__  =	['#version: 0.2', 'a p', 'ap t</w>', 'r e', 'a d', 'ad apt</w>', '']
         lowercase__  =	{'unk_token': '<unk>'}

         lowercase__  =	os.path.join(self.tmpdirname      ,					VOCAB_FILES_NAMES['vocab_file'])
         lowercase__  =	os.path.join(self.tmpdirname      ,					VOCAB_FILES_NAMES['merges_file'])
         with open(self.vocab_file      ,					'w'      ,					encoding='utf-8') as fp:
               fp.write(json.dumps(lowerCAmelCase) + '\n')
         with open(self.merges_file      ,					'w'      ,					encoding='utf-8') as fp:
               fp.write('\n'.join(lowerCAmelCase))


   def       UpperCAmelCase		(    self	:      int      ,					**lowerCAmelCase	:      str)						->					Dict:




         """simple docstring"""

         kwargs.update(self.special_tokens_map)
         return CTRLTokenizer.from_pretrained(self.tmpdirname      ,					**lowerCAmelCase)


   def       UpperCAmelCase		(    self	:      List[str]      ,					lowerCAmelCase	:      Optional[int])						->					Optional[Any]:




         """simple docstring"""

         lowercase__  =	'adapt react readapt apt'
         lowercase__  =	'adapt react readapt apt'
         return input_text, output_text



   def       UpperCAmelCase		(    self	:      Optional[int])						->					str:




         """simple docstring"""

         lowercase__  =	CTRLTokenizer(self.vocab_file      ,					self.merges_file      ,					**self.special_tokens_map)
         lowercase__  =	'adapt react readapt apt'
         lowercase__  =	'adapt re@@ a@@ c@@ t re@@ adapt apt'.split()
         lowercase__  =	tokenizer.tokenize(lowerCAmelCase)
         self.assertListEqual(lowerCAmelCase      ,					lowerCAmelCase)

         lowercase__  =	tokens + [tokenizer.unk_token]

         lowercase__  =	[0, 1, 2, 4, 5, 1, 0, 3, 6]
         self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase)      ,					lowerCAmelCase)
