


import unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     XLMForMultipleChoice,
     XLMForQuestionAnswering,
     XLMForQuestionAnsweringSimple,
     XLMForSequenceClassification,
     XLMForTokenClassification,
     XLMModel,
     XLMWithLMHeadModel,
 )
 from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST





class   __a      :
 def __init__(       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__=13    ,			lowerCAmelCase__=7    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=False    ,			lowerCAmelCase__=False    ,			lowerCAmelCase__=False    ,			lowerCAmelCase__=2    ,			lowerCAmelCase__=99    ,			lowerCAmelCase__=0    ,			lowerCAmelCase__=32    ,			lowerCAmelCase__=5    ,			lowerCAmelCase__=4    ,			lowerCAmelCase__=0.1    ,			lowerCAmelCase__=0.1    ,			lowerCAmelCase__=512    ,			lowerCAmelCase__=2    ,			lowerCAmelCase__=0.0_2    ,			lowerCAmelCase__=2    ,			lowerCAmelCase__=4    ,			lowerCAmelCase__="last"    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=None    ,			lowerCAmelCase__=0    ,			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       List[Any]        =       parent
        lowercase__:       Tuple        =       batch_size
        lowercase__:       Any        =       seq_length
        lowercase__:       List[Any]        =       is_training
        lowercase__:       List[str]        =       use_input_lengths
        lowercase__:       Any        =       use_token_type_ids
        lowercase__:       Tuple        =       use_labels
        lowercase__:       List[Any]        =       gelu_activation
        lowercase__:       Union[str, Any]        =       sinusoidal_embeddings
        lowercase__:       Optional[int]        =       causal
        lowercase__:       int        =       asm
        lowercase__:       Any        =       n_langs
        lowercase__:       Union[str, Any]        =       vocab_size
        lowercase__:       Dict        =       n_special
        lowercase__:       Dict        =       hidden_size
        lowercase__:       Any        =       num_hidden_layers
        lowercase__:       int        =       num_attention_heads
        lowercase__:       int        =       hidden_dropout_prob
        lowercase__:       List[Any]        =       attention_probs_dropout_prob
        lowercase__:       Dict        =       max_position_embeddings
        lowercase__:       Any        =       type_sequence_label_size
        lowercase__:       List[Any]        =       initializer_range
        lowercase__:       Optional[int]        =       num_labels
        lowercase__:       Dict        =       num_choices
        lowercase__:       Dict        =       summary_type
        lowercase__:       Tuple        =       use_proj
        lowercase__:       List[str]        =       scope
        lowercase__:       List[str]        =       bos_token_id
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[Any]:




        '''simple docstring'''
        lowercase__:       str        =       ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size			)
        lowercase__:       Union[str, Any]        =       random_attention_mask([self.batch_size, self.seq_length]			)

        lowercase__:       Optional[int]        =       None
        if self.use_input_lengths:
               lowercase__:       Optional[Any]        =       (
                   ids_tensor([self.batch_size]    ,			vocab_size=2			) + self.seq_length - 2
               )  # small variation of seq_length

        lowercase__:       Dict        =       None
        if self.use_token_type_ids:
               lowercase__:       Union[str, Any]        =       ids_tensor([self.batch_size, self.seq_length]    ,			self.n_langs			)

        lowercase__:       str        =       None
        lowercase__:       List[str]        =       None
        lowercase__:       Optional[int]        =       None
        if self.use_labels:
               lowercase__:       str        =       ids_tensor([self.batch_size]    ,			self.type_sequence_label_size			)
               lowercase__:       Tuple        =       ids_tensor([self.batch_size, self.seq_length]    ,			self.num_labels			)
               lowercase__:       Optional[Any]        =       ids_tensor([self.batch_size]    ,			2			).float()
               lowercase__:       Union[str, Any]        =       ids_tensor([self.batch_size]    ,			self.num_choices			)

        lowercase__:       Tuple        =       self.get_config()

        return (
            config,
            input_ids,
            token_type_ids,
            input_lengths,
            sequence_labels,
            token_labels,
            is_impossible_labels,
            choice_labels,
            input_mask,
        )
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        return 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    ,			num_labels=self.num_labels    ,			bos_token_id=self.bos_token_id    ,			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			) ->    str:




        '''simple docstring'''
        lowercase__:       List[Any]        =       XLMModel(config=lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       Optional[Any]        =       model(lowerCAmelCase__    ,			lengths=lowerCAmelCase__    ,			langs=lowerCAmelCase__			)
        lowercase__:       str        =       model(lowerCAmelCase__    ,			langs=lowerCAmelCase__			)
        lowercase__:       Tuple        =       model(lowerCAmelCase__			)
        self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			) ->    int:




        '''simple docstring'''
        lowercase__:       Dict        =       XLMWithLMHeadModel(lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()

        lowercase__:       List[Any]        =       model(lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__    ,			labels=lowerCAmelCase__			)
        self.parent.assertEqual(result.loss.shape    ,			()			)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			) ->    Union[str, Any]:




        '''simple docstring'''
        lowercase__:       str        =       XLMForQuestionAnsweringSimple(lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()

        lowercase__:       Tuple        =       model(lowerCAmelCase__			)

        lowercase__:       Optional[Any]        =       model(lowerCAmelCase__    ,			start_positions=lowerCAmelCase__    ,			end_positions=lowerCAmelCase__			)
        lowercase__:       Tuple        =       outputs
        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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       List[str]        =       XLMForQuestionAnswering(lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()

        lowercase__:       Any        =       model(lowerCAmelCase__			)

        lowercase__:       Tuple        =       model(
            lowerCAmelCase__    ,			start_positions=lowerCAmelCase__    ,			end_positions=lowerCAmelCase__    ,			cls_index=lowerCAmelCase__    ,			is_impossible=lowerCAmelCase__    ,			p_mask=lowerCAmelCase__    ,			)

        lowercase__:       int        =       model(
            lowerCAmelCase__    ,			start_positions=lowerCAmelCase__    ,			end_positions=lowerCAmelCase__    ,			cls_index=lowerCAmelCase__    ,			is_impossible=lowerCAmelCase__    ,			)

        ((lowercase__)      ,						):       Dict        =       result_with_labels.to_tuple()

        lowercase__:       str        =       model(lowerCAmelCase__    ,			start_positions=lowerCAmelCase__    ,			end_positions=lowerCAmelCase__			)

        ((lowercase__)      ,						):       Optional[Any]        =       result_with_labels.to_tuple()

        self.parent.assertEqual(result_with_labels.loss.shape    ,			()			)
        self.parent.assertEqual(result.start_top_log_probs.shape    ,			(self.batch_size, model.config.start_n_top)			)
        self.parent.assertEqual(result.start_top_index.shape    ,			(self.batch_size, model.config.start_n_top)			)
        self.parent.assertEqual(
            result.end_top_log_probs.shape    ,			(self.batch_size, model.config.start_n_top * model.config.end_n_top)			)
        self.parent.assertEqual(
            result.end_top_index.shape    ,			(self.batch_size, model.config.start_n_top * model.config.end_n_top)			)
        self.parent.assertEqual(result.cls_logits.shape    ,			(self.batch_size,)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			) ->    Any:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       XLMForSequenceClassification(lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()

        lowercase__:       Union[str, Any]        =       model(lowerCAmelCase__			)
        lowercase__:       int        =       model(lowerCAmelCase__    ,			labels=lowerCAmelCase__			)
        self.parent.assertEqual(result.loss.shape    ,			()			)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.type_sequence_label_size)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       Optional[int]        =       self.num_labels
        lowercase__:       Any        =       XLMForTokenClassification(lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()

        lowercase__:       Any        =       model(lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			labels=lowerCAmelCase__			)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.num_labels)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			) ->    Optional[Any]:




        '''simple docstring'''
        lowercase__:       Optional[int]        =       self.num_choices
        lowercase__:       List[str]        =       XLMForMultipleChoice(config=lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       Optional[Any]        =       input_ids.unsqueeze(1			).expand(-1    ,			self.num_choices    ,			-1			).contiguous()
        lowercase__:       List[Any]        =       token_type_ids.unsqueeze(1			).expand(-1    ,			self.num_choices    ,			-1			).contiguous()
        lowercase__:       Tuple        =       input_mask.unsqueeze(1			).expand(-1    ,			self.num_choices    ,			-1			).contiguous()
        lowercase__:       Dict        =       model(
            lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__    ,			labels=lowerCAmelCase__    ,			)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_choices)			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       Any        =       self.prepare_config_and_inputs()
        (
            (
            lowercase__
        )      ,						(
            lowercase__
        )      ,						(
            lowercase__
        )      ,						(
            lowercase__
        )      ,						(
            lowercase__
        )      ,						(
            lowercase__
        )      ,						(
            lowercase__
        )      ,						(
            lowercase__
        )      ,						(
            lowercase__
        )      ,						
        ):       Union[str, Any]        =       config_and_inputs
        lowercase__:       Any        =       {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'lengths': input_lengths}
        return config, inputs_dict





@require_torch
class   __a      (   __UpperCamelCase	,	__UpperCamelCase	,	__UpperCamelCase	,	unittest.TestCase				):
 __lowercase			:							Optional[int]       					=			(
     (
         XLMModel,
         XLMWithLMHeadModel,
         XLMForQuestionAnswering,
         XLMForSequenceClassification,
         XLMForQuestionAnsweringSimple,
         XLMForTokenClassification,
         XLMForMultipleChoice,
     )
     if is_torch_available()
     else ()
 )
 __lowercase			:							List[str]       					=			(
     (XLMWithLMHeadModel,) if is_torch_available() else ()
 )  # TODO (PVP): Check other models whether language generation is also applicable
 __lowercase			:							Dict       					=			(
     {
         'feature-extraction': XLMModel,
         'fill-mask': XLMWithLMHeadModel,
         'question-answering': XLMForQuestionAnsweringSimple,
         'text-classification': XLMForSequenceClassification,
         'text-generation': XLMWithLMHeadModel,
         'token-classification': XLMForTokenClassification,
         'zero-shot': XLMForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    Union[str, Any]:




        '''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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__=False			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       List[Any]        =       super()._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__    ,			return_labels=lowerCAmelCase__			)

        if return_labels:
               if model_class.__name__ == "XLMForQuestionAnswering":
                      lowercase__:       Dict        =       torch.zeros(
                          self.model_tester.batch_size    ,			dtype=torch.long    ,			device=lowerCAmelCase__			)
                      lowercase__:       List[Any]        =       torch.zeros(
                          self.model_tester.batch_size    ,			dtype=torch.long    ,			device=lowerCAmelCase__			)

        return inputs_dict
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       List[Any]        =       XLMModelTester(self			)
        lowercase__:       Dict        =       ConfigTester(self    ,			config_class=lowerCAmelCase__    ,			emb_dim=37			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        self.config_tester.run_common_tests()
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    str:




        '''simple docstring'''
        lowercase__:       Any        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_model(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    int:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_lm_head(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[Any]:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_simple_qa(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict:




        '''simple docstring'''
        lowercase__:       Optional[Any]        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_qa(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    int:




        '''simple docstring'''
        lowercase__:       Optional[int]        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_sequence_classif(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Tuple:




        '''simple docstring'''
        lowercase__:       List[str]        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_token_classif(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    str:




        '''simple docstring'''
        lowercase__:       Any        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_xlm_for_multiple_choice(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__=False    ,			lowerCAmelCase__=1			) ->    int:




        '''simple docstring'''
        self.assertIsInstance(lowerCAmelCase__    ,			lowerCAmelCase__			)
        self.assertListEqual(
            [isinstance(lowerCAmelCase__    ,			lowerCAmelCase__			) for iter_attentions in attentions]    ,			[True] * len(lowerCAmelCase__			)			)
        self.assertEqual(len(lowerCAmelCase__			)    ,			(max_length - min_length) * num_beam_groups			)

        for idx, iter_attentions in enumerate(lowerCAmelCase__			):
               # adds PAD dummy token
               lowercase__:       Dict        =       min_length + idx + 1
               lowercase__:       str        =       min_length + idx + 1

               lowercase__:       Optional[Any]        =       (
                   batch_size * num_beam_groups,
                   config.num_attention_heads,
                   tgt_len,
                   src_len,
               )
               # check attn size
               self.assertListEqual(
                   [layer_attention.shape for layer_attention in iter_attentions]    ,			[expected_shape] * len(lowerCAmelCase__			)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__=False    ,			lowerCAmelCase__=1			) ->    str:




        '''simple docstring'''
        self.assertIsInstance(lowerCAmelCase__    ,			lowerCAmelCase__			)
        self.assertListEqual(
            [isinstance(lowerCAmelCase__    ,			lowerCAmelCase__			) for iter_hidden_states in hidden_states]    ,			[True] * len(lowerCAmelCase__			)    ,			)
        self.assertEqual(len(lowerCAmelCase__			)    ,			(max_length - min_length) * num_beam_groups			)

        for idx, iter_hidden_states in enumerate(lowerCAmelCase__			):
               # adds PAD dummy token
               lowercase__:       str        =       min_length + idx + 1
               lowercase__:       List[str]        =       (batch_size * num_beam_groups, seq_len, config.hidden_size)
               # check hidden size
               self.assertListEqual(
                   [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states]    ,			[expected_shape] * len(lowerCAmelCase__			)    ,			)
        pass
 @slow
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    str:




        '''simple docstring'''
        for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__:       int        =       XLMModel.from_pretrained(lowerCAmelCase__			)
               self.assertIsNotNone(lowerCAmelCase__			)





@require_torch
class   __a      (   unittest.TestCase				):
 @slow
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Union[str, Any]:




        '''simple docstring'''
        lowercase__:       List[str]        =       XLMWithLMHeadModel.from_pretrained('xlm-mlm-en-2048'			)
        model.to(lowerCAmelCase__			)
        lowercase__:       Any        =       torch.tensor([[14, 447]]    ,			dtype=torch.long    ,			device=lowerCAmelCase__			)  # the president
        lowercase__:       Any        =       [
            14,
            447,
            14,
            447,
            14,
            447,
            14,
            447,
            14,
            447,
            14,
            447,
            14,
            447,
            14,
            447,
            14,
            447,
            14,
            447,
        ]  # 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
        lowercase__:       Union[str, Any]        =       model.generate(lowerCAmelCase__    ,			do_sample=lowerCAmelCase__			)
        self.assertListEqual(output_ids[0].cpu().numpy().tolist()    ,			lowerCAmelCase__			)






import json
import os
import shutil
import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoConfig, BertConfig, GPTaConfig
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / '''utils'''))

from test_module.custom_configuration import CustomConfig  # noqa E402


__lowerCAmelCase			=							{
    '''return_dict''': False,
    '''output_hidden_states''': True,
    '''output_attentions''': True,
    '''torchscript''': True,
    '''torch_dtype''': '''float16''',
    '''use_bfloat16''': True,
    '''tf_legacy_loss''': True,
    '''pruned_heads''': {'''a''': 1},
    '''tie_word_embeddings''': False,
    '''is_decoder''': True,
    '''cross_attention_hidden_size''': 1_28,
    '''add_cross_attention''': True,
    '''tie_encoder_decoder''': True,
    '''max_length''': 50,
    '''min_length''': 3,
    '''do_sample''': True,
    '''early_stopping''': True,
    '''num_beams''': 3,
    '''num_beam_groups''': 3,
    '''diversity_penalty''': 0.5,
    '''temperature''': 2.0,
    '''top_k''': 10,
    '''top_p''': 0.7,
    '''typical_p''': 0.2,
    '''repetition_penalty''': 0.8,
    '''length_penalty''': 0.8,
    '''no_repeat_ngram_size''': 5,
    '''encoder_no_repeat_ngram_size''': 5,
    '''bad_words_ids''': [1, 2, 3],
    '''num_return_sequences''': 3,
    '''chunk_size_feed_forward''': 5,
    '''output_scores''': True,
    '''return_dict_in_generate''': True,
    '''forced_bos_token_id''': 2,
    '''forced_eos_token_id''': 3,
    '''remove_invalid_values''': True,
    '''architectures''': ['''BertModel'''],
    '''finetuning_task''': '''translation''',
    '''id2label''': {0: '''label'''},
    '''label2id''': {'''label''': '''0'''},
    '''tokenizer_class''': '''BertTokenizerFast''',
    '''prefix''': '''prefix''',
    '''bos_token_id''': 6,
    '''pad_token_id''': 7,
    '''eos_token_id''': 8,
    '''sep_token_id''': 9,
    '''decoder_start_token_id''': 10,
    '''exponential_decay_length_penalty''': (5, 1.01),
    '''suppress_tokens''': [0, 1],
    '''begin_suppress_tokens''': 2,
    '''task_specific_params''': {'''translation''': '''some_params'''},
    '''problem_type''': '''regression''',
}





@is_staging_test
class   __a      (   unittest.TestCase				):
 @classmethod
 def 		SCREAMING_SNAKE_CASE__       (       cls			) ->    Any:




        '''simple docstring'''
        lowercase__:       List[Any]        =       TOKEN
        HfFolder.save_token(lowerCAmelCase__			)
 @classmethod
 def 		SCREAMING_SNAKE_CASE__       (       cls			) ->    str:




        '''simple docstring'''
        try:
               delete_repo(token=cls._token    ,			repo_id='test-config'			)
        except HTTPError:
               pass

        try:
               delete_repo(token=cls._token    ,			repo_id='valid_org/test-config-org'			)
        except HTTPError:
               pass

        try:
               delete_repo(token=cls._token    ,			repo_id='test-dynamic-config'			)
        except HTTPError:
               pass
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict:




        '''simple docstring'''
        lowercase__:       List[str]        =       BertConfig(
            vocab_size=99    ,			hidden_size=32    ,			num_hidden_layers=5    ,			num_attention_heads=4    ,			intermediate_size=37			)
        config.push_to_hub('test-config'    ,			use_auth_token=self._token			)

        lowercase__:       str        =       BertConfig.from_pretrained(F'{USER}/test-config'			)
        for k, v in config.to_dict().items():
               if k != "transformers_version":
                      self.assertEqual(lowerCAmelCase__    ,			getattr(lowerCAmelCase__    ,			lowerCAmelCase__			)			)

        # Reset repo
        delete_repo(token=self._token    ,			repo_id='test-config'			)

        # Push to hub via save_pretrained
        with tempfile.TemporaryDirectory() as tmp_dir:
               config.save_pretrained(lowerCAmelCase__    ,			repo_id='test-config'    ,			push_to_hub=lowerCAmelCase__    ,			use_auth_token=self._token			)

        lowercase__:       Dict        =       BertConfig.from_pretrained(F'{USER}/test-config'			)
        for k, v in config.to_dict().items():
               if k != "transformers_version":
                      self.assertEqual(lowerCAmelCase__    ,			getattr(lowerCAmelCase__    ,			lowerCAmelCase__			)			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       List[Any]        =       BertConfig(
            vocab_size=99    ,			hidden_size=32    ,			num_hidden_layers=5    ,			num_attention_heads=4    ,			intermediate_size=37			)
        config.push_to_hub('valid_org/test-config-org'    ,			use_auth_token=self._token			)

        lowercase__:       Tuple        =       BertConfig.from_pretrained('valid_org/test-config-org'			)
        for k, v in config.to_dict().items():
               if k != "transformers_version":
                      self.assertEqual(lowerCAmelCase__    ,			getattr(lowerCAmelCase__    ,			lowerCAmelCase__			)			)

        # Reset repo
        delete_repo(token=self._token    ,			repo_id='valid_org/test-config-org'			)

        # Push to hub via save_pretrained
        with tempfile.TemporaryDirectory() as tmp_dir:
               config.save_pretrained(
                   lowerCAmelCase__    ,			repo_id='valid_org/test-config-org'    ,			push_to_hub=lowerCAmelCase__    ,			use_auth_token=self._token			)

        lowercase__:       Union[str, Any]        =       BertConfig.from_pretrained('valid_org/test-config-org'			)
        for k, v in config.to_dict().items():
               if k != "transformers_version":
                      self.assertEqual(lowerCAmelCase__    ,			getattr(lowerCAmelCase__    ,			lowerCAmelCase__			)			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Tuple:




        '''simple docstring'''
        CustomConfig.register_for_auto_class()
        lowercase__:       Tuple        =       CustomConfig(attribute=42			)

        config.push_to_hub('test-dynamic-config'    ,			use_auth_token=self._token			)

        # This has added the proper auto_map field to the config
        self.assertDictEqual(config.auto_map    ,			{'AutoConfig': 'custom_configuration.CustomConfig'}			)

        lowercase__:       int        =       AutoConfig.from_pretrained(F'{USER}/test-dynamic-config'    ,			trust_remote_code=lowerCAmelCase__			)
        # Can't make an isinstance check because the new_config is from the FakeConfig class of a dynamic module
        self.assertEqual(new_config.__class__.__name__    ,			'CustomConfig'			)
        self.assertEqual(new_config.attribute    ,			42			)





class   __a      (   unittest.TestCase				):
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Tuple:




        '''simple docstring'''
        lowercase__:       Any        =       GPTaConfig()

        # attempt to modify each of int/float/bool/str config records and verify they were updated
        lowercase__:       List[Any]        =       c.n_embd + 1  # int
        lowercase__:       Any        =       c.resid_pdrop + 1.0  # float
        lowercase__:       Any        =       not c.scale_attn_weights  # bool
        lowercase__:       List[str]        =       c.summary_type + 'foo'  # str
        c.update_from_string(
            F'n_embd={n_embd},resid_pdrop={resid_pdrop},scale_attn_weights={scale_attn_weights},summary_type={summary_type}'			)
        self.assertEqual(lowerCAmelCase__    ,			c.n_embd    ,			'mismatch for key: n_embd'			)
        self.assertEqual(lowerCAmelCase__    ,			c.resid_pdrop    ,			'mismatch for key: resid_pdrop'			)
        self.assertEqual(lowerCAmelCase__    ,			c.scale_attn_weights    ,			'mismatch for key: scale_attn_weights'			)
        self.assertEqual(lowerCAmelCase__    ,			c.summary_type    ,			'mismatch for key: summary_type'			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Union[str, Any]:




        '''simple docstring'''
        lowercase__:       Any        =       PretrainedConfig()
        lowercase__:       Optional[int]        =       [key for key in base_config.__dict__ if key not in config_common_kwargs]
        # If this part of the test fails, you have arguments to addin config_common_kwargs above.
        self.assertListEqual(
            lowerCAmelCase__    ,			['is_encoder_decoder', '_name_or_path', '_commit_hash', 'transformers_version']			)
        lowercase__:       List[str]        =       [key for key, value in config_common_kwargs.items() if value == getattr(lowerCAmelCase__    ,			lowerCAmelCase__			)]
        if len(lowerCAmelCase__			) > 0:
               raise ValueError(
                   'The following keys are set with the default values in'
                   ' `test_configuration_common.config_common_kwargs` pick another value for them:'
                   F' {", ".join(lowerCAmelCase__			)}.'			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        with self.assertRaises(lowerCAmelCase__			):
               # config is in subfolder, the following should not work without specifying the subfolder
               lowercase__:       str        =       BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder'			)

        lowercase__:       str        =       BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert-subfolder'    ,			subfolder='bert'			)

        self.assertIsNotNone(lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        # A mock response for an HTTP head request to emulate server down
        lowercase__:       Optional[Any]        =       mock.Mock()
        lowercase__:       Tuple        =       500
        lowercase__:       Any        =       {}
        lowercase__:       Dict        =       HTTPError
        lowercase__:       Optional[Any]        =       {}

        # Download this model to make sure it's in the cache.
        lowercase__:       Optional[int]        =       BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert'			)

        # Under the mock environment we get a 500 error when trying to reach the model.
        with mock.patch('requests.Session.request'    ,			return_value=lowerCAmelCase__			) as mock_head:
               lowercase__:       List[Any]        =       BertConfig.from_pretrained('hf-internal-testing/tiny-random-bert'			)
               # This check we did call the fake head request
               mock_head.assert_called()
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Tuple:




        '''simple docstring'''
        # This test is for deprecated behavior and can be removed in v5
        lowercase__:       Tuple        =       BertConfig.from_pretrained(
            'https://huggingface.co/hf-internal-testing/tiny-random-bert/resolve/main/config.json'			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Union[str, Any]:




        '''simple docstring'''
        lowercase__:       Tuple        =       AutoConfig.from_pretrained('bert-base-cased'			)
        lowercase__:       Optional[Any]        =       ['config.4.0.0.json']

        with tempfile.TemporaryDirectory() as tmp_dir:
               configuration.save_pretrained(lowerCAmelCase__			)
               lowercase__:       Optional[int]        =       2
               json.dump(configuration.to_dict()    ,			open(os.path.join(lowerCAmelCase__    ,			'config.4.0.0.json'			)    ,			'w'			)			)

               # This should pick the new configuration file as the version of Transformers is > 4.0.0
               lowercase__:       str        =       AutoConfig.from_pretrained(lowerCAmelCase__			)
               self.assertEqual(new_configuration.hidden_size    ,			2			)

               # Will need to be adjusted if we reach v42 and this test is still here.
               # Should pick the old configuration file as the version of Transformers is < 4.42.0
               lowercase__:       Dict        =       ['config.42.0.0.json']
               lowercase__:       int        =       768
               configuration.save_pretrained(lowerCAmelCase__			)
               shutil.move(os.path.join(lowerCAmelCase__    ,			'config.4.0.0.json'			)    ,			os.path.join(lowerCAmelCase__    ,			'config.42.0.0.json'			)			)
               lowercase__:       Dict        =       AutoConfig.from_pretrained(lowerCAmelCase__			)
               self.assertEqual(new_configuration.hidden_size    ,			768			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Union[str, Any]:




        '''simple docstring'''
        # This repo has two configuration files, one for v4.0.0 and above with a different hidden size.
        lowercase__:       Optional[int]        =       'hf-internal-testing/test-two-configs'

        import transformers as new_transformers

        lowercase__:       Tuple        =       'v4.0.0'
        lowercase__      ,						lowercase__:       List[str]        =       new_transformers.models.auto.AutoConfig.from_pretrained(
            lowerCAmelCase__    ,			return_unused_kwargs=lowerCAmelCase__			)
        self.assertEqual(new_configuration.hidden_size    ,			2			)
        # This checks `_configuration_file` ia not kept in the kwargs by mistake.
        self.assertDictEqual(lowerCAmelCase__    ,			{}			)

        # Testing an older version by monkey-patching the version in the module it's used.
        import transformers as old_transformers

        lowercase__:       Union[str, Any]        =       'v3.0.0'
        lowercase__:       Optional[Any]        =       old_transformers.models.auto.AutoConfig.from_pretrained(lowerCAmelCase__			)
        self.assertEqual(old_configuration.hidden_size    ,			768			)


