
from __future__ import annotations

import unittest

from transformers import MPNetConfig, 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.mpnet.modeling_tf_mpnet import (
						    TFMPNetForMaskedLM,
						    TFMPNetForMultipleChoice,
						    TFMPNetForQuestionAnswering,
						    TFMPNetForSequenceClassification,
						    TFMPNetForTokenClassification,
						    TFMPNetModel,
						)


class 	UpperCamelCase__       :

						'''simple docstring'''



						def __init__(  self  , A  , A=13  , A=7  , A=True  , A=True  , A=False  , A=True  , A=99  , A=64  , A=2  , A=4  , A=64  , A="gelu"  , A=0.1  , A=0.1  , A=5_12  , A=16  , A=2  , A=0.02  , A=3  , A=4  , A=None  , )  ->Optional[int]:
									UpperCAmelCase__		:Optional[Any] 		=    parent
									UpperCAmelCase__		:Any 		=    batch_size
									UpperCAmelCase__		:str 		=    seq_length
									UpperCAmelCase__		:Any 		=    is_training
									UpperCAmelCase__		:Union[str, Any] 		=    use_input_mask
									UpperCAmelCase__		:Optional[Any] 		=    use_token_type_ids
									UpperCAmelCase__		:int 		=    use_labels
									UpperCAmelCase__		:List[Any] 		=    vocab_size
									UpperCAmelCase__		:Optional[int] 		=    hidden_size
									UpperCAmelCase__		:int 		=    num_hidden_layers
									UpperCAmelCase__		:Optional[Any] 		=    num_attention_heads
									UpperCAmelCase__		:List[str] 		=    intermediate_size
									UpperCAmelCase__		:Optional[int] 		=    hidden_act
									UpperCAmelCase__		:Dict 		=    hidden_dropout_prob
									UpperCAmelCase__		:Dict 		=    attention_probs_dropout_prob
									UpperCAmelCase__		:List[str] 		=    max_position_embeddings
									UpperCAmelCase__		:Dict 		=    type_vocab_size
									UpperCAmelCase__		:int 		=    type_sequence_label_size
									UpperCAmelCase__		:Any 		=    initializer_range
									UpperCAmelCase__		:Tuple 		=    num_labels
									UpperCAmelCase__		:str 		=    num_choices
									UpperCAmelCase__		:Any 		=    scope


						def    A__       (  self	)  ->str:
									UpperCAmelCase__		:Optional[int] 		=    ids_tensor([self.batch_size, self.seq_length]  , self.vocab_size	)

									UpperCAmelCase__		:int 		=    None
									if self.use_input_mask:
												UpperCAmelCase__		:Any 		=    random_attention_mask([self.batch_size, self.seq_length]	)

									UpperCAmelCase__		:str 		=    None
									UpperCAmelCase__		:Union[str, Any] 		=    None
									UpperCAmelCase__		:Any 		=    None
									if self.use_labels:
												UpperCAmelCase__		:Union[str, Any] 		=    ids_tensor([self.batch_size]  , self.type_sequence_label_size	)
												UpperCAmelCase__		:Optional[int] 		=    ids_tensor([self.batch_size, self.seq_length]  , self.num_labels	)
												UpperCAmelCase__		:Optional[int] 		=    ids_tensor([self.batch_size]  , self.num_choices	)

									UpperCAmelCase__		:str 		=    MPNetConfig(
									    vocab_size=self.vocab_size  , hidden_size=self.hidden_size  , num_hidden_layers=self.num_hidden_layers  , num_attention_heads=self.num_attention_heads  , intermediate_size=self.intermediate_size  , hidden_act=self.hidden_act  , hidden_dropout_prob=self.hidden_dropout_prob  , attention_probs_dropout_prob=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    A__       (  self  , A  , A  , A  , A  , A  , A	)  ->Optional[Any]:
									UpperCAmelCase__		:Union[str, Any] 		=    TFMPNetModel(config=lowerCamelCase_	)
									UpperCAmelCase__		:Optional[int] 		=    {'input_ids': input_ids, 'attention_mask': input_mask}
									UpperCAmelCase__		:Tuple 		=    model(lowerCamelCase_	)
									UpperCAmelCase__		:List[Any] 		=    [input_ids, input_mask]
									UpperCAmelCase__		:Dict 		=    model(lowerCamelCase_	)
									self.parent.assertEqual(result.last_hidden_state.shape  , (self.batch_size, self.seq_length, self.hidden_size)	)


						def    A__       (  self  , A  , A  , A  , A  , A  , A	)  ->List[str]:
									UpperCAmelCase__		:int 		=    TFMPNetForMaskedLM(config=lowerCamelCase_	)
									UpperCAmelCase__		:Tuple 		=    {'input_ids': input_ids, 'attention_mask': input_mask}
									UpperCAmelCase__		:str 		=    model(lowerCamelCase_	)
									self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.seq_length, self.vocab_size)	)


						def    A__       (  self  , A  , A  , A  , A  , A  , A	)  ->str:
									UpperCAmelCase__		:List[str] 		=    TFMPNetForQuestionAnswering(config=lowerCamelCase_	)
									UpperCAmelCase__		:Any 		=    {
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									}
									UpperCAmelCase__		:str 		=    model(lowerCamelCase_	)
									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    A__       (  self  , A  , A  , A  , A  , A  , A	)  ->Dict:
									UpperCAmelCase__		:List[Any] 		=    self.num_labels
									UpperCAmelCase__		:Dict 		=    TFMPNetForSequenceClassification(lowerCamelCase_	)
									UpperCAmelCase__		:Any 		=    {'input_ids': input_ids, 'attention_mask': input_mask}
									UpperCAmelCase__		:List[str] 		=    model(lowerCamelCase_	)
									self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.num_labels)	)


						def    A__       (  self  , A  , A  , A  , A  , A  , A	)  ->Any:
									UpperCAmelCase__		:List[Any] 		=    self.num_choices
									UpperCAmelCase__		:Optional[Any] 		=    TFMPNetForMultipleChoice(lowerCamelCase_	)
									UpperCAmelCase__		:int 		=    tf.tile(tf.expand_dims(lowerCamelCase_  , 1	)  , (1, self.num_choices, 1)	)
									UpperCAmelCase__		:List[str] 		=    tf.tile(tf.expand_dims(lowerCamelCase_  , 1	)  , (1, self.num_choices, 1)	)
									UpperCAmelCase__		:Union[str, Any] 		=    {
									    'input_ids': multiple_choice_inputs_ids,
									    'attention_mask': multiple_choice_input_mask,
									}
									UpperCAmelCase__		:Any 		=    model(lowerCamelCase_	)
									self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.num_choices)	)


						def    A__       (  self  , A  , A  , A  , A  , A  , A	)  ->List[str]:
									UpperCAmelCase__		:int 		=    self.num_labels
									UpperCAmelCase__		:Optional[int] 		=    TFMPNetForTokenClassification(lowerCamelCase_	)
									UpperCAmelCase__		:List[Any] 		=    {'input_ids': input_ids, 'attention_mask': input_mask}
									UpperCAmelCase__		:str 		=    model(lowerCamelCase_	)
									self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.seq_length, self.num_labels)	)


						def    A__       (  self	)  ->int:
									UpperCAmelCase__		:Union[str, Any] 		=    self.prepare_config_and_inputs()
									((UpperCAmelCase__)		,       (UpperCAmelCase__)		,       (UpperCAmelCase__)		,       (UpperCAmelCase__)		,       (UpperCAmelCase__)		,       (UpperCAmelCase__))		:Any 		=    config_and_inputs
									UpperCAmelCase__		:Dict 		=    {'input_ids': input_ids, 'attention_mask': input_mask}
									return config, inputs_dict


@require_tf
class 	UpperCamelCase__       (  UpperCAmelCase__						,			UpperCAmelCase__						,			unittest.TestCase):

						'''simple docstring'''



						__a     :    Any												=			(
						    (
						        TFMPNetForMaskedLM,
						        TFMPNetForMultipleChoice,
						        TFMPNetForQuestionAnswering,
						        TFMPNetForSequenceClassification,
						        TFMPNetForTokenClassification,
						        TFMPNetModel,
						    )
						    if is_tf_available()
						    else ()
						)
						__a     :    int												=			(
						    {
						        """feature-extraction""": TFMPNetModel,
						        """fill-mask""": TFMPNetForMaskedLM,
						        """question-answering""": TFMPNetForQuestionAnswering,
						        """text-classification""": TFMPNetForSequenceClassification,
						        """token-classification""": TFMPNetForTokenClassification,
						        """zero-shot""": TFMPNetForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						__a     :    List[str]												=			False
						__a     :    Optional[int]												=			False


						def    A__       (  self	)  ->Optional[Any]:
									UpperCAmelCase__		:List[Any] 		=    TFMPNetModelTester(self	)
									UpperCAmelCase__		:Dict 		=    ConfigTester(self  , config_class=lowerCamelCase_  , hidden_size=37	)


						def    A__       (  self	)  ->Optional[int]:
									self.config_tester.run_common_tests()


						def    A__       (  self	)  ->int:
									UpperCAmelCase__		:List[Any] 		=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_model(*lowerCamelCase_	)


						def    A__       (  self	)  ->List[Any]:
									UpperCAmelCase__		:Dict 		=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_masked_lm(*lowerCamelCase_	)


						def    A__       (  self	)  ->int:
									UpperCAmelCase__		:Optional[Any] 		=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_question_answering(*lowerCamelCase_	)


						def    A__       (  self	)  ->Any:
									UpperCAmelCase__		:Any 		=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowerCamelCase_	)


						def    A__       (  self	)  ->Dict:
									UpperCAmelCase__		:List[str] 		=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowerCamelCase_	)


						def    A__       (  self	)  ->str:
									UpperCAmelCase__		:Dict 		=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_mpnet_for_token_classification(*lowerCamelCase_	)


						@slow
						def    A__       (  self	)  ->Optional[Any]:

									for model_name in ["microsoft/mpnet-base"]:
												UpperCAmelCase__		:List[str] 		=    TFMPNetModel.from_pretrained(lowerCamelCase_	)
												self.assertIsNotNone(lowerCamelCase_	)


@require_tf
class 	UpperCamelCase__       (  unittest.TestCase):

						'''simple docstring'''



						@slow
						def    A__       (  self	)  ->int:
									UpperCAmelCase__		:List[Any] 		=    TFMPNetModel.from_pretrained('microsoft/mpnet-base'	)
									UpperCAmelCase__		:Optional[Any] 		=    tf.constant([[0, 1, 2, 3, 4, 5]]	)
									UpperCAmelCase__		:Tuple 		=    model(lowerCamelCase_	)[0]

									UpperCAmelCase__		:List[Any] 		=    [1, 6, 7_68]
									self.assertEqual(output.shape  , lowerCamelCase_	)

									UpperCAmelCase__		:Optional[Any] 		=    tf.constant(
									    [
									        [
									            [-0.106_7172, 0.0821_6473, 0.002_4543],
									            [-0.0346_5879, 0.835_4118, -0.0325_2288],
									            [-0.0656_9476, -0.1242_4111, -0.049_4436],
									        ]
									    ]	)
									tf.debugging.assert_near(output[:, :3, :3]  , lowerCamelCase_  , atol=1e-4	)










from __future__ import annotations

from collections import defaultdict
from dataclasses import dataclass



@dataclass
class       _lowercase				:





	"""simple docstring"""
	__A		      =				42
	__A		      =				None
	__A		      =				None






def        a( )						->			TreeNode:
					"""simple docstring"""





					return TreeNode(3					,	TreeNode(9			)					,	TreeNode(20					,	TreeNode(15			)					,	TreeNode(7			)			)			)






def        a( A			:       TreeNode			)						->			list[int]:
					"""simple docstring"""





					def depth_first_search(
					    A			:       TreeNode | None					,	A			:       int					,	A			:       list[int]			) -> None:
										if not root:
															return

										if depth == len(A			):
															right_view.append(root.val			)

										depth_first_search(root.right					,	depth + 1					,	A			)
										depth_first_search(root.left					,	depth + 1					,	A			)

					a   	=  []
					if not root:
										return right_view

					depth_first_search(A					,	0					,	A			)
					return right_view






def        a( A			:       TreeNode			)						->			list[int]:
					"""simple docstring"""





					def depth_first_search(
					    A			:       TreeNode | None					,	A			:       int					,	A			:       list[int]			) -> None:
										if not root:
															return

										if depth == len(A			):
															left_view.append(root.val			)

										depth_first_search(root.left					,	depth + 1					,	A			)
										depth_first_search(root.right					,	depth + 1					,	A			)

					a   	=  []
					if not root:
										return left_view

					depth_first_search(A					,	0					,	A			)
					return left_view






def        a( A			:       TreeNode			)						->			list[int]:
					"""simple docstring"""





					def breadth_first_search(A			:       TreeNode					,	A			:       list[int]			) -> None:
										a   	=  [(root, 0)]
										a   	=  defaultdict(A			)

										while queue:
															a   	=  queue.pop(0			)
															a	,							a   	=  first

															lookup[hd].append(node.val			)

															if node.left:
																				queue.append((node.left, hd - 1)			)
															if node.right:
																				queue.append((node.right, hd + 1)			)

										for pair in sorted(lookup.items()					,	key=lambda A			: each[0]			):
															top_view.append(pair[1][0]			)

					a   	=  []
					if not root:
										return top_view

					breadth_first_search(A					,	A			)
					return top_view






def        a( A			:       TreeNode			)						->			list[int]:
					"""simple docstring"""





					from collections import defaultdict

					def breadth_first_search(A			:       TreeNode					,	A			:       list[int]			) -> None:
										a   	=  [(root, 0)]
										a   	=  defaultdict(A			)

										while queue:
															a   	=  queue.pop(0			)
															a	,							a   	=  first
															lookup[hd].append(node.val			)

															if node.left:
																				queue.append((node.left, hd - 1)			)
															if node.right:
																				queue.append((node.right, hd + 1)			)

										for pair in sorted(lookup.items()					,	key=lambda A			: each[0]			):
															bottom_view.append(pair[1][-1]			)

					a   	=  []
					if not root:
										return bottom_view

					breadth_first_search(A					,	A			)
					return bottom_view


if __name__ == "__main__":
				import doctest

				doctest.testmod()
