






import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union
class 							__snake_case(       enum.Enum   ):
 lowercase			:							Any		   =		'''timm'''
 lowercase			:							Tuple		   =		'''transformers'''
def    a__    (	A		,					A		,					A       ):

   if stage_names is None:
      raise ValueError("""Stage_names must be set for transformers backbones"""       )

   if out_features is not None:
      if not isinstance(A_		,					(list,)       ):
         raise ValueError(F'''out_features must be a list {type(A_       )}'''       )
      if any(feat not in stage_names for feat in out_features       ):
         raise ValueError(F'''out_features must be a subset of stage_names: {stage_names} got {out_features}'''       )

   if out_indices is not None:
      if not isinstance(A_		,					(list, tuple)       ):
         raise ValueError(F'''out_indices must be a list or tuple, got {type(A_       )}'''       )
      if any(idx >= len(A_       ) for idx in out_indices       ):
         raise ValueError("""out_indices must be valid indices for stage_names {stage_names}, got {out_indices}"""       )

   if out_features is not None and out_indices is not None:
      if len(A_       ) != len(A_       ):
         raise ValueError("""out_features and out_indices should have the same length if both are set"""       )
      if out_features != [stage_names[idx] for idx in out_indices]:
         raise ValueError("""out_features and out_indices should correspond to the same stages if both are set"""       )
def    a__    (	A		,					A		,					A		,					):

   if out_indices is None and out_features is None:
      lowercase__    :						Optional[int]       =			[len(A_       ) - 1]
      lowercase__    :						List[str]       =			[stage_names[-1]]
   elif out_indices is None and out_features is not None:
      lowercase__    :						str       =			[stage_names.index(A_       ) for layer in out_features]
   elif out_features is None and out_indices is not None:
      lowercase__    :						Optional[int]       =			[stage_names[idx] for idx in out_indices]
   return out_features, out_indices
def    a__    (	A		,					A		,					A		,					):
   verify_out_features_out_indices(out_features=A_		,					out_indices=A_		,					stage_names=A_       )
   lowercase__    :						Tuple       =			_align_output_features_output_indices(
       out_features=A_		,					out_indices=A_		,					stage_names=A_       )
   # Verify that the aligned out_features and out_indices are valid
   verify_out_features_out_indices(out_features=A_		,					out_indices=A_		,					stage_names=A_       )
   return output_features, output_indices
class 							__snake_case:
 lowercase			:							str		   =		None



 def     UpperCAmelCase     (						self : Optional[Any]			,		UpperCAmelCase__ : List[str]						):

    """simple docstring"""






    if getattr(self			,		"""_backbone"""			,		UpperCAmelCase__						) is None:
       raise ValueError("""self._backbone must be set before calling _init_timm_backbone"""						)

    # These will diagree with the defaults for the transformers models e.g. for resnet50
    # the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
    # the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
    lowercase__    :						Any       =			[stage['module'] for stage in self._backbone.feature_info.info]
    lowercase__    :						Tuple       =			[stage['num_chs'] for stage in self._backbone.feature_info.info]
    lowercase__    :						int       =			self._backbone.feature_info.out_indices
    lowercase__    :						Optional[int]       =			self._backbone.feature_info.module_name()

    # We verify the out indices and out features are valid
    verify_out_features_out_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=self.stage_names						)
    lowercase__    :						Union[str, Any]       =			out_features, out_indices



 def     UpperCAmelCase     (						self : Optional[Any]			,		UpperCAmelCase__ : Dict						):

    """simple docstring"""






    lowercase__    :						Tuple       =			getattr(UpperCAmelCase__			,		"""stage_names"""						)
    lowercase__    :						List[str]       =			getattr(UpperCAmelCase__			,		"""out_features"""			,		UpperCAmelCase__						)
    lowercase__    :						str       =			getattr(UpperCAmelCase__			,		"""out_indices"""			,		UpperCAmelCase__						)

    lowercase__    :						Tuple       =			stage_names
    lowercase__    :						int       =			get_aligned_output_features_output_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=UpperCAmelCase__						)
    # Number of channels for each stage. This is set in the transformer backbone model init
    lowercase__    :						List[Any]       =			None



 def     UpperCAmelCase     (						self : Union[str, Any]			,		UpperCAmelCase__ : Tuple						):

    """simple docstring"""






    lowercase__    :						List[Any]       =			config

    lowercase__    :						Tuple       =			getattr(UpperCAmelCase__			,		"""use_timm_backbone"""			,		UpperCAmelCase__						)
    lowercase__    :						Dict       =			BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

    if self.backbone_type == BackboneType.TIMM:
       self._init_timm_backbone(UpperCAmelCase__						)
    elif self.backbone_type == BackboneType.TRANSFORMERS:
       self._init_transformers_backbone(UpperCAmelCase__						)
    else:
       raise ValueError(f'''backbone_type {self.backbone_type} not supported.'''						)



 @property
 def     UpperCAmelCase     (						self : Union[str, Any]						):

    """simple docstring"""






    return self._out_features



 @out_features.setter
 def     UpperCAmelCase     (						self : Tuple			,		UpperCAmelCase__ : List[str]						):

    """simple docstring"""






    lowercase__    :						Any       =			get_aligned_output_features_output_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=self.stage_names						)



 @property
 def     UpperCAmelCase     (						self : Tuple						):

    """simple docstring"""






    return self._out_indices



 @out_indices.setter
 def     UpperCAmelCase     (						self : Optional[Any]			,		UpperCAmelCase__ : Union[Tuple[int], List[int]]						):

    """simple docstring"""






    lowercase__    :						Optional[int]       =			get_aligned_output_features_output_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=self.stage_names						)



 @property
 def     UpperCAmelCase     (						self : Tuple						):

    """simple docstring"""






    return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names						)}



 @property
 def     UpperCAmelCase     (						self : Any						):

    """simple docstring"""






    return [self.out_feature_channels[name] for name in self.out_features]



 def     UpperCAmelCase     (						self : Optional[Any]			,		*UpperCAmelCase__ : Any			,		**UpperCAmelCase__ : Any						):

    """simple docstring"""






    lowercase__    :						Dict       =			dict(inspect.signature(self.forward						).parameters						)
    lowercase__    :						Dict       =			{k: v for k, v in kwargs.items() if k in signature}
    return self(*UpperCAmelCase__			,		**UpperCAmelCase__						)



 def     UpperCAmelCase     (						self : Any			,		UpperCAmelCase__ : Optional[Any]			,		UpperCAmelCase__ : Optional[bool] = None			,		UpperCAmelCase__ : Optional[bool] = None			,		UpperCAmelCase__ : Optional[bool] = None			,		):

    """simple docstring"""






    raise NotImplementedError("""This method should be implemented by the derived class."""						)







 def     UpperCAmelCase     (						self : List[Any]						):

    """simple docstring"""






    lowercase__    :						List[str]       =			super().to_dict()
    lowercase__    :						Optional[int]       =			output.pop("""_out_features"""						)
    lowercase__    :						Optional[Any]       =			output.pop("""_out_indices"""						)
    return output
class 							__snake_case:



 @property
 def     UpperCAmelCase     (						self : Any						):

    """simple docstring"""






    return self._out_features



 @out_features.setter
 def     UpperCAmelCase     (						self : List[Any]			,		UpperCAmelCase__ : List[str]						):

    """simple docstring"""






    lowercase__    :						Any       =			get_aligned_output_features_output_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=self.stage_names						)



 @property
 def     UpperCAmelCase     (						self : List[Any]						):

    """simple docstring"""






    return self._out_indices



 @out_indices.setter
 def     UpperCAmelCase     (						self : str			,		UpperCAmelCase__ : Union[Tuple[int], List[int]]						):

    """simple docstring"""






    lowercase__    :						List[Any]       =			get_aligned_output_features_output_indices(
        out_features=UpperCAmelCase__			,		out_indices=UpperCAmelCase__			,		stage_names=self.stage_names						)







 def     UpperCAmelCase     (						self : List[str]						):

    """simple docstring"""






    lowercase__    :						Optional[Any]       =			super().to_dict()
    lowercase__    :						Optional[int]       =			output.pop("""_out_features"""						)
    lowercase__    :						Optional[Any]       =			output.pop("""_out_indices"""						)
    return output










"""simple docstring"""


from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import (
       TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFXLMForMultipleChoice,
       TFXLMForQuestionAnsweringSimple,
       TFXLMForSequenceClassification,
       TFXLMForTokenClassification,
       TFXLMModel,
       TFXLMWithLMHeadModel,
       XLMConfig,
   )






class 		snake_case    :





    def __init__(				self      :   Union[str, Any]							,						A      :   Optional[Any]							,						):


      '''simple docstring'''




      a	:     Union[str, Any]							=  parent
      a	:     str							=  1_3
      a	:     Optional[int]							=  7
      a	:     List[str]							=  True
      a	:     int							=  True
      a	:     Optional[Any]							=  True
      a	:     Tuple							=  True
      a	:     List[str]							=  True
      a	:     Union[str, Any]							=  False
      a	:     Optional[Any]							=  False
      a	:     List[str]							=  False
      a	:     Union[str, Any]							=  2
      a	:     Optional[int]							=  9_9
      a	:     Optional[Any]							=  0
      a	:     Tuple							=  3_2
      a	:     Union[str, Any]							=  2
      a	:     Optional[Any]							=  4
      a	:     Optional[int]							=  0.1
      a	:     List[str]							=  0.1
      a	:     int							=  5_1_2
      a	:     List[Any]							=  1_6
      a	:     Dict							=  2
      a	:     List[str]							=  0.02
      a	:     str							=  3
      a	:     Optional[Any]							=  4
      a	:     Optional[Any]							=  'last'
      a	:     Any							=  True
      a	:     Union[str, Any]							=  None
      a	:     int							=  0





    def 							lowerCamelCase__ (				self      :   Optional[int]     ):


      '''simple docstring'''




      a	:     Dict							=  ids_tensor([self.batch_size, self.seq_length]							,						self.vocab_size     )
      a	:     Dict							=  random_attention_mask([self.batch_size, self.seq_length]							,						dtype=tf.floataa     )

      a	:     Union[str, Any]							=  None
      if self.use_input_lengths:
        a	:     List[str]							=  (
            ids_tensor([self.batch_size]							,						vocab_size=2     ) + self.seq_length - 2
        )  # small variation of seq_length

      a	:     Optional[Any]							=  None
      if self.use_token_type_ids:
        a	:     str							=  ids_tensor([self.batch_size, self.seq_length]							,						self.n_langs     )

      a	:     Optional[int]							=  None
      a	:     Optional[Any]							=  None
      a	:     Tuple							=  None
      if self.use_labels:
        a	:     Union[str, Any]							=  ids_tensor([self.batch_size]							,						self.type_sequence_label_size     )
        a	:     Any							=  ids_tensor([self.batch_size, self.seq_length]							,						self.num_labels     )
        a	:     Dict							=  ids_tensor([self.batch_size]							,						2							,						dtype=tf.floataa     )
        a	:     Tuple							=  ids_tensor([self.batch_size]							,						self.num_choices     )

      a	:     str							=  XLMConfig(
          vocab_size=self.vocab_size							,						n_special=self.n_special							,						emb_dim=self.hidden_size							,						n_layers=self.num_hidden_layers							,						n_heads=self.num_attention_heads							,						dropout=self.hidden_dropout_prob							,						attention_dropout=self.attention_probs_dropout_prob							,						gelu_activation=self.gelu_activation							,						sinusoidal_embeddings=self.sinusoidal_embeddings							,						asm=self.asm							,						causal=self.causal							,						n_langs=self.n_langs							,						max_position_embeddings=self.max_position_embeddings							,						initializer_range=self.initializer_range							,						summary_type=self.summary_type							,						use_proj=self.use_proj							,						bos_token_id=self.bos_token_id							,						)

      return (
          config,
          input_ids,
          token_type_ids,
          input_lengths,
          sequence_labels,
          token_labels,
          is_impossible_labels,
          choice_labels,
          input_mask,
      )





    def 							lowerCamelCase__ (				self      :   List[str]							,						A      :   Union[str, Any]							,						A      :   Optional[Any]							,						A      :   List[str]							,						A      :   List[Any]							,						A      :   Union[str, Any]							,						A      :   Dict							,						A      :   Optional[int]							,						A      :   List[Any]							,						A      :   Optional[int]							,						):


      '''simple docstring'''




      a	:     int							=  TFXLMModel(config=A     )
      a	:     Optional[Any]							=  {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
      a	:     Dict							=  model(A     )

      a	:     List[str]							=  [input_ids, input_mask]
      a	:     Union[str, Any]							=  model(A     )
      self.parent.assertEqual(result.last_hidden_state.shape							,						(self.batch_size, self.seq_length, self.hidden_size)     )





    def 							lowerCamelCase__ (				self      :   str							,						A      :   List[Any]							,						A      :   List[Any]							,						A      :   int							,						A      :   int							,						A      :   Any							,						A      :   List[Any]							,						A      :   Tuple							,						A      :   str							,						A      :   List[str]							,						):


      '''simple docstring'''




      a	:     Union[str, Any]							=  TFXLMWithLMHeadModel(A     )

      a	:     str							=  {'input_ids': input_ids, 'lengths': input_lengths, 'langs': token_type_ids}
      a	:     List[str]							=  model(A     )

      a	:     Union[str, Any]							=  outputs

      self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.seq_length, self.vocab_size)     )





    def 							lowerCamelCase__ (				self      :   List[str]							,						A      :   int							,						A      :   List[str]							,						A      :   Tuple							,						A      :   Dict							,						A      :   Dict							,						A      :   str							,						A      :   Union[str, Any]							,						A      :   Optional[Any]							,						A      :   Union[str, Any]							,						):


      '''simple docstring'''




      a	:     Any							=  TFXLMForQuestionAnsweringSimple(A     )

      a	:     Optional[Any]							=  {'input_ids': input_ids, 'lengths': input_lengths}

      a	:     Any							=  model(A     )

      self.parent.assertEqual(result.start_logits.shape							,						(self.batch_size, self.seq_length)     )
      self.parent.assertEqual(result.end_logits.shape							,						(self.batch_size, self.seq_length)     )





    def 							lowerCamelCase__ (				self      :   Optional[int]							,						A      :   Optional[int]							,						A      :   Dict							,						A      :   int							,						A      :   Any							,						A      :   List[Any]							,						A      :   str							,						A      :   List[Any]							,						A      :   Dict							,						A      :   List[str]							,						):


      '''simple docstring'''




      a	:     Dict							=  TFXLMForSequenceClassification(A     )

      a	:     Optional[int]							=  {'input_ids': input_ids, 'lengths': input_lengths}

      a	:     Tuple							=  model(A     )

      self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.type_sequence_label_size)     )





    def 							lowerCamelCase__ (				self      :   int							,						A      :   Optional[Any]							,						A      :   str							,						A      :   Dict							,						A      :   Optional[Any]							,						A      :   List[str]							,						A      :   List[str]							,						A      :   List[Any]							,						A      :   List[Any]							,						A      :   str							,						):


      '''simple docstring'''




      a	:     Any							=  self.num_labels
      a	:     int							=  TFXLMForTokenClassification(config=A     )
      a	:     Union[str, Any]							=  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      a	:     int							=  model(A     )
      self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.seq_length, self.num_labels)     )





    def 							lowerCamelCase__ (				self      :   Optional[Any]							,						A      :   Any							,						A      :   Optional[Any]							,						A      :   List[Any]							,						A      :   Dict							,						A      :   List[Any]							,						A      :   Any							,						A      :   List[Any]							,						A      :   List[str]							,						A      :   str							,						):


      '''simple docstring'''




      a	:     Tuple							=  self.num_choices
      a	:     List[str]							=  TFXLMForMultipleChoice(config=A     )
      a	:     List[str]							=  tf.tile(tf.expand_dims(A							,						1     )							,						(1, self.num_choices, 1)     )
      a	:     str							=  tf.tile(tf.expand_dims(A							,						1     )							,						(1, self.num_choices, 1)     )
      a	:     Union[str, Any]							=  tf.tile(tf.expand_dims(A							,						1     )							,						(1, self.num_choices, 1)     )
      a	:     str							=  {
          'input_ids': multiple_choice_inputs_ids,
          'attention_mask': multiple_choice_input_mask,
          'token_type_ids': multiple_choice_token_type_ids,
      }
      a	:     str							=  model(A     )
      self.parent.assertEqual(result.logits.shape							,						(self.batch_size, self.num_choices)     )





    def 							lowerCamelCase__ (				self      :   Any     ):


      '''simple docstring'''




      a	:     Union[str, Any]							=  self.prepare_config_and_inputs()
      (
          (
          a
      ),	(
          a
      ),	(
          a
      ),	(
          a
      ),	(
          a
      ),	(
          a
      ),	(
          a
      ),	(
          a
      ),	(
          a
      ),	
      )	:     Dict							=  config_and_inputs
      a	:     Tuple							=  {
          'input_ids': input_ids,
          'token_type_ids': token_type_ids,
          'langs': token_type_ids,
          'lengths': input_lengths,
      }
      return config, inputs_dict






@require_tf
class 		snake_case    (    UpperCAmelCase  ,					UpperCAmelCase  ,					unittest.TestCase			):
    __magic_name__       	=	(
        (
            TFXLMModel,
            TFXLMWithLMHeadModel,
            TFXLMForSequenceClassification,
            TFXLMForQuestionAnsweringSimple,
            TFXLMForTokenClassification,
            TFXLMForMultipleChoice,
        )
        if is_tf_available()
        else ()
    )
    __magic_name__       	=	(
        (TFXLMWithLMHeadModel,) if is_tf_available() else ()
    )  # TODO (PVP): Check other models whether language generation is also applicable
    __magic_name__       	=	(
        {
            '''feature-extraction''': TFXLMModel,
            '''fill-mask''': TFXLMWithLMHeadModel,
            '''question-answering''': TFXLMForQuestionAnsweringSimple,
            '''text-classification''': TFXLMForSequenceClassification,
            '''text-generation''': TFXLMWithLMHeadModel,
            '''token-classification''': TFXLMForTokenClassification,
            '''zero-shot''': TFXLMForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    __magic_name__       	=	False
    __magic_name__       	=	False





    def 							lowerCamelCase__ (				self      :   Any							,						A      :   List[Any]							,						A      :   Optional[Any]							,						A      :   str							,						A      :   Optional[Any]							,						A      :   List[str]     ):


      '''simple docstring'''




      if (
          pipeline_test_casse_name == "QAPipelineTests"
          and tokenizer_name is not None
          and not tokenizer_name.endswith('Fast'     )
      ):
        # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
        # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
        # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
        return True

      return False





    def 							lowerCamelCase__ (				self      :   int     ):


      '''simple docstring'''




      a	:     str							=  TFXLMModelTester(self     )
      a	:     int							=  ConfigTester(self							,						config_class=A							,						emb_dim=3_7     )





    def 							lowerCamelCase__ (				self      :   str     ):


      '''simple docstring'''




      self.config_tester.run_common_tests()





    def 							lowerCamelCase__ (				self      :   int     ):


      '''simple docstring'''




      a	:     Optional[int]							=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_model(*A     )





    def 							lowerCamelCase__ (				self      :   Union[str, Any]     ):


      '''simple docstring'''




      a	:     Optional[Any]							=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_lm_head(*A     )





    def 							lowerCamelCase__ (				self      :   List[Any]     ):


      '''simple docstring'''




      a	:     Optional[int]							=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_qa(*A     )





    def 							lowerCamelCase__ (				self      :   int     ):


      '''simple docstring'''




      a	:     List[str]							=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_sequence_classif(*A     )





    def 							lowerCamelCase__ (				self      :   Any     ):


      '''simple docstring'''




      a	:     str							=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_for_token_classification(*A     )





    def 							lowerCamelCase__ (				self      :   Tuple     ):


      '''simple docstring'''




      a	:     Tuple							=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_xlm_for_multiple_choice(*A     )





    @slow
    def 							lowerCamelCase__ (				self      :   Optional[int]     ):


      '''simple docstring'''




      for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a	:     Any							=  TFXLMModel.from_pretrained(A     )
        self.assertIsNotNone(A     )






@require_tf
class 		snake_case    (    unittest.TestCase			):





    @slow
    def 							lowerCamelCase__ (				self      :   Dict     ):


      '''simple docstring'''




      a	:     List[Any]							=  TFXLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048'     )
      a	:     Optional[int]							=  tf.convert_to_tensor([[1_4, 4_4_7]]							,						dtype=tf.intaa     )  # the president
      a	:     List[Any]							=  [
          1_4,
          4_4_7,
          1_4,
          4_4_7,
          1_4,
          4_4_7,
          1_4,
          4_4_7,
          1_4,
          4_4_7,
          1_4,
          4_4_7,
          1_4,
          4_4_7,
          1_4,
          4_4_7,
          1_4,
          4_4_7,
          1_4,
          4_4_7,
      ]  # the president the president the president the president the president the president the president the president the president the president
      # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
      a	:     Optional[Any]							=  model.generate(A							,						do_sample=A     )
      self.assertListEqual(output_ids[0].numpy().tolist()							,						A     )




