



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 				__snake_case  :


    '''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      ,							):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= parent
        SCREAMING_SNAKE_CASE__       							= batch_size
        SCREAMING_SNAKE_CASE__       							= seq_length
        SCREAMING_SNAKE_CASE__       							= is_training
        SCREAMING_SNAKE_CASE__       							= use_input_mask
        SCREAMING_SNAKE_CASE__       							= use_token_type_ids
        SCREAMING_SNAKE_CASE__       							= use_labels
        SCREAMING_SNAKE_CASE__       							= vocab_size
        SCREAMING_SNAKE_CASE__       							= hidden_size
        SCREAMING_SNAKE_CASE__       							= num_hidden_layers
        SCREAMING_SNAKE_CASE__       							= num_attention_heads
        SCREAMING_SNAKE_CASE__       							= intermediate_size
        SCREAMING_SNAKE_CASE__       							= hidden_act
        SCREAMING_SNAKE_CASE__       							= hidden_dropout_prob
        SCREAMING_SNAKE_CASE__       							= attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE__       							= max_position_embeddings
        SCREAMING_SNAKE_CASE__       							= type_vocab_size
        SCREAMING_SNAKE_CASE__       							= type_sequence_label_size
        SCREAMING_SNAKE_CASE__       							= initializer_range
        SCREAMING_SNAKE_CASE__       							= num_labels
        SCREAMING_SNAKE_CASE__       							= num_choices
        SCREAMING_SNAKE_CASE__       							= scope



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size    )

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

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

        SCREAMING_SNAKE_CASE__       							= 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    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= TFMPNetModel(config=A_    )
        SCREAMING_SNAKE_CASE__       							= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        SCREAMING_SNAKE_CASE__       							= [input_ids, input_mask]
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.hidden_size)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= TFMPNetForMaskedLM(config=A_    )
        SCREAMING_SNAKE_CASE__       							= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.vocab_size)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= TFMPNetForQuestionAnswering(config=A_    )
        SCREAMING_SNAKE_CASE__       							= {
            '''input_ids''': input_ids,
            '''attention_mask''': input_mask,
        }
        SCREAMING_SNAKE_CASE__       							= 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    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.num_labels
        SCREAMING_SNAKE_CASE__       							= TFMPNetForSequenceClassification(A_    )
        SCREAMING_SNAKE_CASE__       							= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.num_labels)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.num_choices
        SCREAMING_SNAKE_CASE__       							= TFMPNetForMultipleChoice(A_    )
        SCREAMING_SNAKE_CASE__       							= tf.tile(tf.expand_dims(A_      ,							1    )      ,							(1, self.num_choices, 1)    )
        SCREAMING_SNAKE_CASE__       							= tf.tile(tf.expand_dims(A_      ,							1    )      ,							(1, self.num_choices, 1)    )
        SCREAMING_SNAKE_CASE__       							= {
            '''input_ids''': multiple_choice_inputs_ids,
            '''attention_mask''': multiple_choice_input_mask,
        }
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.num_choices)    )



    def    lowercase_				(					self      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_      ,							A_    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.num_labels
        SCREAMING_SNAKE_CASE__       							= TFMPNetForTokenClassification(A_    )
        SCREAMING_SNAKE_CASE__       							= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
        SCREAMING_SNAKE_CASE__       							= model(A_    )
        self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.seq_length, self.num_labels)    )




    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.prepare_config_and_inputs()
        ((SCREAMING_SNAKE_CASE__)							,		(SCREAMING_SNAKE_CASE__)							,		(SCREAMING_SNAKE_CASE__)							,		(SCREAMING_SNAKE_CASE__)							,		(SCREAMING_SNAKE_CASE__)							,		(SCREAMING_SNAKE_CASE__))       							= config_and_inputs
        SCREAMING_SNAKE_CASE__       							= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
        return config, inputs_dict





@require_tf
class 				__snake_case  (     __SCREAMING_SNAKE_CASE ,    __SCREAMING_SNAKE_CASE ,    unittest.TestCase						):


    '''simple docstring'''







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



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= TFMPNetModelTester(self    )
        SCREAMING_SNAKE_CASE__       							= ConfigTester(self      ,							config_class=A_      ,							hidden_size=37    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        self.config_tester.run_common_tests()



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_model(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_masked_lm(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_question_answering(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_sequence_classification(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_multiple_choice(*A_    )



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_token_classification(*A_    )




    @slow
    def    lowercase_				(					self    ):





        '''simple docstring'''

        for model_name in ["microsoft/mpnet-base"]:
            SCREAMING_SNAKE_CASE__       							= TFMPNetModel.from_pretrained(A_    )
            self.assertIsNotNone(A_    )





@require_tf
class 				__snake_case  (     unittest.TestCase						):


    '''simple docstring'''







    @slow
    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''    )
        SCREAMING_SNAKE_CASE__       							= tf.constant([[0, 1, 2, 3, 4, 5]]    )
        SCREAMING_SNAKE_CASE__       							= model(A_    )[0]

        SCREAMING_SNAKE_CASE__       							= [1, 6, 7_68]
        self.assertEqual(output.shape      ,							A_    )

        SCREAMING_SNAKE_CASE__       							= tf.constant(
            [
                [
                    [-0.1067172, 0.08216473, 0.0024543],
                    [-0.03465879, 0.8354118, -0.03252288],
                    [-0.06569476, -0.12424111, -0.0494436],
                ]
            ]    )
        tf.debugging.assert_near(output[:, :3, :3]      ,							A_      ,							atol=1E-4    )







from __future__ import annotations

from collections.abc import Generator


def        __snake_case       (						lowerCAmelCase_  )							->	Generator[int, None, None]:
    if not isinstance(lowerCAmelCase_   ,       lowerCAmelCase_  ) or n < 1:
        raise Exception('''Sequence only defined for positive integers'''  )

    yield n
    while n != 1:
        if n % 2 == 0:
            n //= 2
        else:
            SCREAMING_SNAKE_CASE__       							= 3 * n + 1
        yield n


def        __snake_case       (						)							->	str:
    SCREAMING_SNAKE_CASE__       							= 4_3
    SCREAMING_SNAKE_CASE__       							= tuple(collatz_sequence(lowerCAmelCase_  )  )
    print(lowerCAmelCase_  )
    print(f'''Collatz sequence from {n} took {len(lowerCAmelCase_  )} steps.'''  )


if __name__ == "__main__":
  main()
