





from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
		from transformers import (
		    AutoConfig,
		    BertConfig,
		    GPTaConfig,
		    TaConfig,
		    TFAutoModel,
		    TFAutoModelForCausalLM,
		    TFAutoModelForMaskedLM,
		    TFAutoModelForPreTraining,
		    TFAutoModelForQuestionAnswering,
		    TFAutoModelForSeqaSeqLM,
		    TFAutoModelForSequenceClassification,
		    TFAutoModelWithLMHead,
		    TFBertForMaskedLM,
		    TFBertForPreTraining,
		    TFBertForQuestionAnswering,
		    TFBertForSequenceClassification,
		    TFBertModel,
		    TFGPTaLMHeadModel,
		    TFRobertaForMaskedLM,
		    TFTaForConditionalGeneration,
		)
		from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
		from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
		from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
		from transformers import (
		    AutoModel,
		    AutoModelForCausalLM,
		    AutoModelForMaskedLM,
		    AutoModelForPreTraining,
		    AutoModelForQuestionAnswering,
		    AutoModelForSeqaSeqLM,
		    AutoModelForSequenceClassification,
		    AutoModelWithLMHead,
		    BertForMaskedLM,
		    BertForPreTraining,
		    BertForQuestionAnswering,
		    BertForSequenceClassification,
		    BertModel,
		    GPTaLMHeadModel,
		    RobertaForMaskedLM,
		    TaForConditionalGeneration,
		)


@is_pt_tf_cross_test
class 			lowercase				(							unittest.TestCase     ):



						"""simple docstring"""

						@slow
						def 		__UpperCAmelCase       (    self			:		List[str]					):





										'''simple docstring'''







										for model_name in ["bert-base-uncased"]:
														_snake_case :			List[Any]  						=   AutoConfig.from_pretrained(lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			List[str]  						=   TFAutoModel.from_pretrained(lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			str  						=   AutoModel.from_pretrained(lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)







						@slow
						def 		__UpperCAmelCase       (    self			:		Dict					):





										'''simple docstring'''







										for model_name in ["bert-base-uncased"]:
														_snake_case :			List[Any]  						=   AutoConfig.from_pretrained(lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			int  						=   TFAutoModelForPreTraining.from_pretrained(lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Dict  						=   AutoModelForPreTraining.from_pretrained(lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)







						@slow
						def 		__UpperCAmelCase       (    self			:		Dict					):





										'''simple docstring'''







										for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_snake_case :			Dict  						=   AutoConfig.from_pretrained(lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Optional[int]  						=   TFAutoModelForCausalLM.from_pretrained(lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														_snake_case						,			_snake_case :			Tuple  						=   TFAutoModelForCausalLM.from_pretrained(
														    lowerCamelCase_ ,				output_loading_info=lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Optional[int]  						=   AutoModelForCausalLM.from_pretrained(lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														_snake_case						,			_snake_case :			Optional[Any]  						=   AutoModelForCausalLM.from_pretrained(
														    lowerCamelCase_ ,				output_loading_info=lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)







						@slow
						def 		__UpperCAmelCase       (    self			:		List[str]					):





										'''simple docstring'''







										for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_snake_case :			Dict  						=   AutoConfig.from_pretrained(lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			str  						=   TFAutoModelWithLMHead.from_pretrained(lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Dict  						=   AutoModelWithLMHead.from_pretrained(lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)







						@slow
						def 		__UpperCAmelCase       (    self			:		Dict					):





										'''simple docstring'''







										for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_snake_case :			List[Any]  						=   AutoConfig.from_pretrained(lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Union[str, Any]  						=   TFAutoModelForMaskedLM.from_pretrained(lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														_snake_case						,			_snake_case :			Union[str, Any]  						=   TFAutoModelForMaskedLM.from_pretrained(
														    lowerCamelCase_ ,				output_loading_info=lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Union[str, Any]  						=   AutoModelForMaskedLM.from_pretrained(lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														_snake_case						,			_snake_case :			Optional[int]  						=   AutoModelForMaskedLM.from_pretrained(
														    lowerCamelCase_ ,				output_loading_info=lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)







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





										'''simple docstring'''







										for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_snake_case :			int  						=   AutoConfig.from_pretrained(lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Optional[Any]  						=   TFAutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														_snake_case						,			_snake_case :			str  						=   TFAutoModelForSeqaSeqLM.from_pretrained(
														    lowerCamelCase_ ,				output_loading_info=lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Tuple  						=   AutoModelForSeqaSeqLM.from_pretrained(lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														_snake_case						,			_snake_case :			Dict  						=   AutoModelForSeqaSeqLM.from_pretrained(
														    lowerCamelCase_ ,				output_loading_info=lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)







						@slow
						def 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







										for model_name in ["bert-base-uncased"]:
														_snake_case :			Union[str, Any]  						=   AutoConfig.from_pretrained(lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Optional[int]  						=   TFAutoModelForSequenceClassification.from_pretrained(lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Optional[Any]  						=   AutoModelForSequenceClassification.from_pretrained(lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)







						@slow
						def 		__UpperCAmelCase       (    self			:		Union[str, Any]					):





										'''simple docstring'''







										for model_name in ["bert-base-uncased"]:
														_snake_case :			Optional[int]  						=   AutoConfig.from_pretrained(lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			Dict  						=   TFAutoModelForQuestionAnswering.from_pretrained(lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)

														_snake_case :			int  						=   AutoModelForQuestionAnswering.from_pretrained(lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
														self.assertIsNotNone(lowerCamelCase_					)
														self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)







						def 		__UpperCAmelCase       (    self			:		Optional[Any]					):





										'''simple docstring'''







										_snake_case :			Union[str, Any]  						=   TFAutoModelWithLMHead.from_pretrained(lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
										self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)
										self.assertEqual(model.num_parameters() ,				1_44_10					)
										self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase_					) ,				1_44_10					)

										_snake_case :			Optional[int]  						=   AutoModelWithLMHead.from_pretrained(lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
										self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)
										self.assertEqual(model.num_parameters() ,				1_44_10					)
										self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase_					) ,				1_44_10					)







						def 		__UpperCAmelCase       (    self			:		List[Any]					):





										'''simple docstring'''







										_snake_case :			Dict  						=   TFAutoModelWithLMHead.from_pretrained(lowerCamelCase_ ,				from_pt=lowerCamelCase_					)
										self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)
										self.assertEqual(model.num_parameters() ,				1_44_10					)
										self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase_					) ,				1_44_10					)

										_snake_case :			str  						=   AutoModelWithLMHead.from_pretrained(lowerCamelCase_ ,				from_tf=lowerCamelCase_					)
										self.assertIsInstance(lowerCamelCase_ ,				lowerCamelCase_					)
										self.assertEqual(model.num_parameters() ,				1_44_10					)
										self.assertEqual(model.num_parameters(only_trainable=lowerCamelCase_					) ,				1_44_10					)













import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
lowercase_		:				Optional[int]				=						object()

# For specifying empty leaf dict `{}`
lowercase_		:				List[Any]				=						object()

def       A__(		__lowerCAmelCase	,			__lowerCAmelCase ):
				_snake_case :			Optional[Any]  						=   tuple((re.compile(x + '$' ) for x in qs) )
				for i in range(len(__lowerCAmelCase ) - len(__lowerCAmelCase ) + 1 ):
								_snake_case :			Tuple  						=   [x.match(__lowerCAmelCase ) for x, y in zip(__lowerCAmelCase	,			ks[i:] )]
								if matches and all(__lowerCAmelCase ):
												return True
				return False

def       A__(		__lowerCAmelCase ):
				def replace(__lowerCAmelCase	,			__lowerCAmelCase ):
								for rule, replacement in rules:
												if _match(__lowerCAmelCase	,			__lowerCAmelCase ):
																return replacement
								return val

				return replace

def       A__(		):
				return [
				    # embeddings
				    (("transformer", "wpe", "embedding"), P('mp'	,			__lowerCAmelCase )),
				    (("transformer", "wte", "embedding"), P('mp'	,			__lowerCAmelCase )),
				    # atention
				    (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(__lowerCAmelCase	,			'mp' )),
				    (("attention", "out_proj", "kernel"), P('mp'	,			__lowerCAmelCase )),
				    (("attention", "out_proj", "bias"), None),
				    # mlp
				    (("mlp", "c_fc", "kernel"), P(__lowerCAmelCase	,			'mp' )),
				    (("mlp", "c_fc", "bias"), P('mp' )),
				    (("mlp", "c_proj", "kernel"), P('mp'	,			__lowerCAmelCase )),
				    (("mlp", "c_proj", "bias"), None),
				    # layer norms
				    ((r"ln_\d+", "bias"), None),
				    ((r"\d+", r"ln_\d+", "scale"), None),
				    (("ln_f", "bias"), None),
				    (("ln_f", "scale"), None),
				]

def       A__(		__lowerCAmelCase ):
				_snake_case :			Optional[Any]  						=   _get_partition_rules()
				_snake_case :			Optional[Any]  						=   _replacement_rules(__lowerCAmelCase )
				_snake_case :			str  						=   {k: _unmatched for k in flatten_dict(__lowerCAmelCase )}
				_snake_case :			str  						=   {k: replace(__lowerCAmelCase	,			__lowerCAmelCase ) for k, v in initd.items()}
				assert _unmatched not in result.values(), "Incomplete partition spec."
				return freeze(unflatten_dict(__lowerCAmelCase ) )






