



"""simple docstring"""


from __future__ import annotations

import unittest

from transformers import DistilBertConfig, 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.models.distilbert.modeling_tf_distilbert import (
					    TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TFDistilBertForMaskedLM,
					    TFDistilBertForMultipleChoice,
					    TFDistilBertForQuestionAnswering,
					    TFDistilBertForSequenceClassification,
					    TFDistilBertForTokenClassification,
					    TFDistilBertModel,
					)
class 			_a :
			"""simple docstring"""



			def __init__(		self     :    Dict				,   __UpperCamelCase     :    Tuple				,   )->str:
							_UpperCAmelCase					=    parent
							_UpperCAmelCase					=    1_3
							_UpperCAmelCase					=    7
							_UpperCAmelCase					=    True
							_UpperCAmelCase					=    True
							_UpperCAmelCase					=    False
							_UpperCAmelCase					=    True
							_UpperCAmelCase					=    9_9
							_UpperCAmelCase					=    3_2
							_UpperCAmelCase					=    2
							_UpperCAmelCase					=    4
							_UpperCAmelCase					=    3_7
							_UpperCAmelCase					=    '''gelu'''
							_UpperCAmelCase					=    0.1
							_UpperCAmelCase					=    0.1
							_UpperCAmelCase					=    5_1_2
							_UpperCAmelCase					=    1_6
							_UpperCAmelCase					=    2
							_UpperCAmelCase					=    0.0_2
							_UpperCAmelCase					=    3
							_UpperCAmelCase					=    4
							_UpperCAmelCase					=    None



			def  lowercase__						(		self     :    Tuple   )->List[Any]:
							_UpperCAmelCase					=    ids_tensor([self.batch_size, self.seq_length]				,   self.vocab_size   )

							_UpperCAmelCase					=    None
							if self.use_input_mask:
											_UpperCAmelCase					=    random_attention_mask([self.batch_size, self.seq_length]   )

							_UpperCAmelCase					=    None
							_UpperCAmelCase					=    None
							_UpperCAmelCase					=    None
							if self.use_labels:
											_UpperCAmelCase					=    ids_tensor([self.batch_size]				,   self.type_sequence_label_size   )
											_UpperCAmelCase					=    ids_tensor([self.batch_size, self.seq_length]				,   self.num_labels   )
											_UpperCAmelCase					=    ids_tensor([self.batch_size]				,   self.num_choices   )

							_UpperCAmelCase					=    DistilBertConfig(
							    vocab_size=self.vocab_size				,   dim=self.hidden_size				,   n_layers=self.num_hidden_layers				,   n_heads=self.num_attention_heads				,   hidden_dim=self.intermediate_size				,   hidden_act=self.hidden_act				,   dropout=self.hidden_dropout_prob				,   attention_dropout=self.attention_probs_dropout_prob				,   max_position_embeddings=self.max_position_embeddings				,   initializer_range=self.initializer_range				,   )

							return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels



			def  lowercase__						(		self     :    Tuple				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Optional[Any]				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    Union[str, Any]   )->Dict:
							_UpperCAmelCase					=    TFDistilBertModel(config=__UpperCamelCase   )
							_UpperCAmelCase					=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}

							_UpperCAmelCase					=    model(__UpperCamelCase   )

							_UpperCAmelCase					=    [input_ids, input_mask]

							_UpperCAmelCase					=    model(__UpperCamelCase   )

							self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)   )



			def  lowercase__						(		self     :    Dict				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    Dict   )->str:
							_UpperCAmelCase					=    TFDistilBertForMaskedLM(config=__UpperCamelCase   )
							_UpperCAmelCase					=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							_UpperCAmelCase					=    model(__UpperCamelCase   )
							self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.seq_length, self.vocab_size)   )



			def  lowercase__						(		self     :    Dict				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    Optional[Any]				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    Tuple   )->int:
							_UpperCAmelCase					=    TFDistilBertForQuestionAnswering(config=__UpperCamelCase   )
							_UpperCAmelCase					=    {
							    '''input_ids''': input_ids,
							    '''attention_mask''': input_mask,
							}
							_UpperCAmelCase					=    model(__UpperCamelCase   )
							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  lowercase__						(		self     :    Tuple				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    Dict   )->Any:
							_UpperCAmelCase					=    self.num_labels
							_UpperCAmelCase					=    TFDistilBertForSequenceClassification(__UpperCamelCase   )
							_UpperCAmelCase					=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							_UpperCAmelCase					=    model(__UpperCamelCase   )
							self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.num_labels)   )



			def  lowercase__						(		self     :    int				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Union[str, Any]				,   __UpperCamelCase     :    int   )->Tuple:
							_UpperCAmelCase					=    self.num_choices
							_UpperCAmelCase					=    TFDistilBertForMultipleChoice(__UpperCamelCase   )
							_UpperCAmelCase					=    tf.tile(tf.expand_dims(__UpperCamelCase				,   1   )				,   (1, self.num_choices, 1)   )
							_UpperCAmelCase					=    tf.tile(tf.expand_dims(__UpperCamelCase				,   1   )				,   (1, self.num_choices, 1)   )
							_UpperCAmelCase					=    {
							    '''input_ids''': multiple_choice_inputs_ids,
							    '''attention_mask''': multiple_choice_input_mask,
							}
							_UpperCAmelCase					=    model(__UpperCamelCase   )
							self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.num_choices)   )



			def  lowercase__						(		self     :    List[Any]				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    Dict   )->Optional[Any]:
							_UpperCAmelCase					=    self.num_labels
							_UpperCAmelCase					=    TFDistilBertForTokenClassification(__UpperCamelCase   )
							_UpperCAmelCase					=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							_UpperCAmelCase					=    model(__UpperCamelCase   )
							self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.seq_length, self.num_labels)   )



			def  lowercase__						(		self     :    Any   )->str:
							_UpperCAmelCase					=    self.prepare_config_and_inputs()
							((_UpperCAmelCase)		,						(_UpperCAmelCase)		,						(_UpperCAmelCase)		,						(_UpperCAmelCase)		,						(_UpperCAmelCase)		,						(_UpperCAmelCase))					=    config_and_inputs
							_UpperCAmelCase					=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict



@require_tf
class 			_a (					lowerCAmelCase			,							lowerCAmelCase			,							unittest.TestCase):
			"""simple docstring"""
			UpperCamelCase__			      =	(
			    (
			        TFDistilBertModel,
			        TFDistilBertForMaskedLM,
			        TFDistilBertForQuestionAnswering,
			        TFDistilBertForSequenceClassification,
			        TFDistilBertForTokenClassification,
			        TFDistilBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else None
			)
			UpperCamelCase__			      =	(
			    {
			        """feature-extraction""": TFDistilBertModel,
			        """fill-mask""": TFDistilBertForMaskedLM,
			        """question-answering""": TFDistilBertForQuestionAnswering,
			        """text-classification""": TFDistilBertForSequenceClassification,
			        """token-classification""": TFDistilBertForTokenClassification,
			        """zero-shot""": TFDistilBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			UpperCamelCase__			      =	False
			UpperCamelCase__			      =	False



			def  lowercase__						(		self     :    Any   )->str:
							_UpperCAmelCase					=    TFDistilBertModelTester(self   )
							_UpperCAmelCase					=    ConfigTester(self				,   config_class=__UpperCamelCase				,   dim=3_7   )



			def  lowercase__						(		self     :    Optional[int]   )->Union[str, Any]:
							self.config_tester.run_common_tests()



			def  lowercase__						(		self     :    str   )->List[Any]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_model(*__UpperCamelCase   )



			def  lowercase__						(		self     :    List[str]   )->Optional[int]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_masked_lm(*__UpperCamelCase   )



			def  lowercase__						(		self     :    List[Any]   )->Tuple:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_question_answering(*__UpperCamelCase   )



			def  lowercase__						(		self     :    Optional[int]   )->Optional[Any]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_sequence_classification(*__UpperCamelCase   )



			def  lowercase__						(		self     :    Dict   )->List[str]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_multiple_choice(*__UpperCamelCase   )



			def  lowercase__						(		self     :    Any   )->int:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_distilbert_for_token_classification(*__UpperCamelCase   )



			@slow
			def  lowercase__						(		self     :    List[str]   )->Any:
							for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]   ):
											_UpperCAmelCase					=    TFDistilBertModel.from_pretrained(__UpperCamelCase   )
											self.assertIsNotNone(__UpperCamelCase   )



@require_tf
class 			_a (					unittest.TestCase):
			"""simple docstring"""



			@slow
			def  lowercase__						(		self     :    Any   )->int:
							_UpperCAmelCase					=    TFDistilBertModel.from_pretrained('''distilbert-base-uncased'''   )
							_UpperCAmelCase					=    tf.constant([[0, 1, 2, 3, 4, 5]]   )
							_UpperCAmelCase					=    model(__UpperCamelCase   )[0]

							_UpperCAmelCase					=    [1, 6, 7_6_8]
							self.assertEqual(output.shape				,   __UpperCamelCase   )

							_UpperCAmelCase					=    tf.constant(
							    [
							        [
							            [0.1_9_2_6_1_8_8_5, -0.1_3_7_3_2_9_5_5, 0.4_1_1_9_7_9_9],
							            [0.2_2_1_5_0_1_5_6, -0.0_7_4_2_2_6_6_1, 0.3_9_0_3_7_2_0_4],
							            [0.2_2_7_5_6_0_1_8, -0.0_8_9_6_4_1_4, 0.3_7_0_1_4_6_7],
							        ]
							    ]   )
							tf.debugging.assert_near(output[:, :3, :3]				,   __UpperCamelCase				,   atol=1e-4   )








"""simple docstring"""


import argparse
from typing import Dict

import tensorflow as tf
import torch
from tqdm import tqdm

from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration


__A   :							Tuple										=   [
    # tf -> hf
    ("/", "."),
    ("layer_", "layers."),
    ("kernel", "weight"),
    ("beta", "bias"),
    ("gamma", "weight"),
    ("pegasus", "model"),
]
__A   :							int										=   [
    (".output.dense", ".fc2"),
    ("intermediate.LayerNorm", "final_layer_norm"),
    ("intermediate.dense", "fc1"),
]

__A   :							Tuple										=   (
    INIT_COMMON
    + [
        ("attention.self.LayerNorm", "self_attn_layer_norm"),
        ("attention.output.dense", "self_attn.out_proj"),
        ("attention.self", "self_attn"),
        ("attention.encdec.LayerNorm", "encoder_attn_layer_norm"),
        ("attention.encdec_output.dense", "encoder_attn.out_proj"),
        ("attention.encdec", "encoder_attn"),
        ("key", "k_proj"),
        ("value", "v_proj"),
        ("query", "q_proj"),
        ("decoder.LayerNorm", "decoder.layernorm_embedding"),
    ]
    + END_COMMON
)

__A   :							Optional[int]										=   (
    INIT_COMMON
    + [
        ("embeddings.word_embeddings", "shared.weight"),
        ("embeddings.position_embeddings", "embed_positions.weight"),
        ("attention.self.LayerNorm", "self_attn_layer_norm"),
        ("attention.output.dense", "self_attn.output"),
        ("attention.self", "self_attn.self"),
        ("encoder.LayerNorm", "encoder.layernorm_embedding"),
    ]
    + END_COMMON
)

__A   :							Union[str, Any]										=   [
    "encdec/key/bias",
    "encdec/query/bias",
    "encdec/value/bias",
    "self/key/bias",
    "self/query/bias",
    "self/value/bias",
    "encdec_output/dense/bias",
    "attention/output/dense/bias",
]







def  lowercase	(      _SCREAMING_SNAKE_CASE   :						Optional[int]					,     _SCREAMING_SNAKE_CASE   :						List[Any]				):






				'''simple docstring'''



				for tf_name, hf_name in patterns:
								_UpperCAmelCase					=    k.replace(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
				return k







def  lowercase	(      _SCREAMING_SNAKE_CASE   :						dict					,     _SCREAMING_SNAKE_CASE   :						dict				):






				'''simple docstring'''



				_UpperCAmelCase					=    BigBirdPegasusConfig(**_SCREAMING_SNAKE_CASE				)
				_UpperCAmelCase					=    BigBirdPegasusForConditionalGeneration(_SCREAMING_SNAKE_CASE				)
				_UpperCAmelCase					=    torch_model.state_dict()
				_UpperCAmelCase					=    {}

				# separating decoder weights
				_UpperCAmelCase					=    {k: tf_weights[k] for k in tf_weights if k.startswith('''pegasus/decoder'''				)}
				_UpperCAmelCase					=    {k: tf_weights[k] for k in tf_weights if not k.startswith('''pegasus/decoder'''				)}

				for k, v in tqdm(decoder_weights.items()					,     '''tf -> hf conversion'''				):
								_UpperCAmelCase					=    [k.endswith(_SCREAMING_SNAKE_CASE				) for ending in KEYS_TO_IGNORE]
								if any(_SCREAMING_SNAKE_CASE				):
												continue
								_UpperCAmelCase					=    DECODER_PATTERNS
								_UpperCAmelCase					=    rename_state_dict_key(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
								if new_k not in state_dict:
												raise ValueError(f'could not find new key {new_k} in state dict. (converted from {k})'				)
								if any(True if i in k else False for i in ['''dense''', '''query''', '''key''', '''value''']				):
												_UpperCAmelCase					=    v.T
								_UpperCAmelCase					=    torch.from_numpy(_SCREAMING_SNAKE_CASE				)
								assert v.shape == state_dict[new_k].shape, f'{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}'

				for k, v in tqdm(remaining_weights.items()					,     '''tf -> hf conversion'''				):
								_UpperCAmelCase					=    [k.endswith(_SCREAMING_SNAKE_CASE				) for ending in KEYS_TO_IGNORE]
								if any(_SCREAMING_SNAKE_CASE				):
												continue
								_UpperCAmelCase					=    REMAINING_PATTERNS
								_UpperCAmelCase					=    rename_state_dict_key(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
								if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings":
												raise ValueError(f'could not find new key {new_k} in state dict. (converted from {k})'				)
								if any(True if i in k else False for i in ['''dense''', '''query''', '''key''', '''value''']				):
												_UpperCAmelCase					=    v.T
								_UpperCAmelCase					=    torch.from_numpy(_SCREAMING_SNAKE_CASE				)
								if k != "pegasus/embeddings/position_embeddings":
												assert v.shape == state_dict[new_k].shape, f'{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}'

				_UpperCAmelCase					=    mapping['''model.embed_positions.weight''']
				_UpperCAmelCase					=    mapping.pop('''model.embed_positions.weight'''				)
				_UpperCAmelCase		,						_UpperCAmelCase					=    torch_model.load_state_dict(_SCREAMING_SNAKE_CASE					,     strict=_SCREAMING_SNAKE_CASE				)
				_UpperCAmelCase					=    [
				    k
				    for k in missing
				    if k
				    not in [
				        '''final_logits_bias''',
				        '''model.encoder.embed_tokens.weight''',
				        '''model.decoder.embed_tokens.weight''',
				        '''lm_head.weight''',
				    ]
				]
				assert unexpected_missing == [], f'no matches found for the following torch keys {unexpected_missing}'
				assert extra == [], f'no matches found for the following tf keys {extra}'
				return torch_model







def  lowercase	(      _SCREAMING_SNAKE_CASE   :						Any				):






				'''simple docstring'''



				_UpperCAmelCase					=    tf.train.list_variables(_SCREAMING_SNAKE_CASE				)
				_UpperCAmelCase					=    {}
				_UpperCAmelCase					=    ['''global_step''']
				for name, shape in tqdm(_SCREAMING_SNAKE_CASE					,     desc='''converting tf checkpoint to dict'''				):
								_UpperCAmelCase					=    any(pat in name for pat in ignore_name				)
								if skip_key:
												continue
								_UpperCAmelCase					=    tf.train.load_variable(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
								_UpperCAmelCase					=    array
				return tf_weights







def  lowercase	(      _SCREAMING_SNAKE_CASE   :						str					,     _SCREAMING_SNAKE_CASE   :						str					,     _SCREAMING_SNAKE_CASE   :						dict				):






				'''simple docstring'''



				_UpperCAmelCase					=    get_tf_weights_as_numpy(_SCREAMING_SNAKE_CASE				)
				_UpperCAmelCase					=    convert_bigbird_pegasus(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
				torch_model.save_pretrained(_SCREAMING_SNAKE_CASE				)


if __name__ == "__main__":
					__A   :							Tuple										=   argparse.ArgumentParser()
					parser.add_argument("--tf_ckpt_path", type=str, help="passed to tf.train.list_variables")
					parser.add_argument("--save_dir", default=None, type=str, help="Path to the output PyTorch model.")
					__A   :							Dict										=   parser.parse_args()
					__A   :							Tuple										=   {}
					convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)



