





import unittest

import numpy as np

from transformers import RobertaConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
   from transformers.models.roberta.modeling_flax_roberta import (
       FlaxRobertaForCausalLM,
       FlaxRobertaForMaskedLM,
       FlaxRobertaForMultipleChoice,
       FlaxRobertaForQuestionAnswering,
       FlaxRobertaForSequenceClassification,
       FlaxRobertaForTokenClassification,
       FlaxRobertaModel,
   )




class 			__UpperCamelCase    (							unittest.TestCase    ):





     def __init__(     self				,							_UpperCamelCase				,							_UpperCamelCase=13				,							_UpperCamelCase=7				,							_UpperCamelCase=True				,							_UpperCamelCase=True				,							_UpperCamelCase=True				,							_UpperCamelCase=True				,							_UpperCamelCase=99				,							_UpperCamelCase=32				,							_UpperCamelCase=5				,							_UpperCamelCase=4				,							_UpperCamelCase=37				,							_UpperCamelCase="gelu"				,							_UpperCamelCase=0.1				,							_UpperCamelCase=0.1				,							_UpperCamelCase=512				,							_UpperCamelCase=16				,							_UpperCamelCase=2				,							_UpperCamelCase=0.02				,							_UpperCamelCase=4				,							):
        _UpperCAmelCase           =   parent
        _UpperCAmelCase           =   batch_size
        _UpperCAmelCase           =   seq_length
        _UpperCAmelCase           =   is_training
        _UpperCAmelCase           =   use_attention_mask
        _UpperCAmelCase           =   use_token_type_ids
        _UpperCAmelCase           =   use_labels
        _UpperCAmelCase           =   vocab_size
        _UpperCAmelCase           =   hidden_size
        _UpperCAmelCase           =   num_hidden_layers
        _UpperCAmelCase           =   num_attention_heads
        _UpperCAmelCase           =   intermediate_size
        _UpperCAmelCase           =   hidden_act
        _UpperCAmelCase           =   hidden_dropout_prob
        _UpperCAmelCase           =   attention_probs_dropout_prob
        _UpperCAmelCase           =   max_position_embeddings
        _UpperCAmelCase           =   type_vocab_size
        _UpperCAmelCase           =   type_sequence_label_size
        _UpperCAmelCase           =   initializer_range
        _UpperCAmelCase           =   num_choices





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   ids_tensor([self.batch_size, self.seq_length]				,							self.vocab_size					)

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

        _UpperCAmelCase           =   None
        if self.use_token_type_ids:
           _UpperCAmelCase           =   ids_tensor([self.batch_size, self.seq_length]				,							self.type_vocab_size					)

        _UpperCAmelCase           =   RobertaConfig(
            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				,							type_vocab_size=self.type_vocab_size				,							is_decoder=_UpperCamelCase				,							initializer_range=self.initializer_range				,							)

        return config, input_ids, token_type_ids, attention_mask





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.prepare_config_and_inputs()
        _UpperCAmelCase       ,      _UpperCAmelCase       ,      _UpperCAmelCase       ,      _UpperCAmelCase           =   config_and_inputs
        _UpperCAmelCase           =   {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
        return config, inputs_dict





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   self.prepare_config_and_inputs()
        _UpperCAmelCase       ,      _UpperCAmelCase       ,      _UpperCAmelCase       ,      _UpperCAmelCase           =   config_and_inputs

        _UpperCAmelCase           =   True
        _UpperCAmelCase           =   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]					)
        _UpperCAmelCase           =   ids_tensor([self.batch_size, self.seq_length]				,							vocab_size=2					)

        return (
            config,
            input_ids,
            token_type_ids,
            encoder_hidden_states,
            encoder_attention_mask,
        )




@require_flax
class 			__UpperCamelCase    (							A__	, unittest.TestCase    ):
     __A	:  Union[str, Any]							  =   True

     __A	:  str							  =   (
         (
             FlaxRobertaModel,
             FlaxRobertaForCausalLM,
             FlaxRobertaForMaskedLM,
             FlaxRobertaForSequenceClassification,
             FlaxRobertaForTokenClassification,
             FlaxRobertaForMultipleChoice,
             FlaxRobertaForQuestionAnswering,
         )
         if is_flax_available()
         else ()
     )





     def 		UpperCamelCase(     self					):
        _UpperCAmelCase           =   FlaxRobertaModelTester(self					)





     @slow
     def 		UpperCamelCase(     self					):
        for model_class_name in self.all_model_classes:
           _UpperCAmelCase           =   model_class_name.from_pretrained('''roberta-base'''				,							from_pt=_UpperCamelCase					)
           _UpperCAmelCase           =   model(np.ones((1, 1)					)					)
           self.assertIsNotNone(_UpperCamelCase					)






from ..utils import DummyObject, requires_backends




class 			__UpperCamelCase    (							metaclass=A__    ):
     __A	:  str							  =   ["""torch""", """scipy"""]





     def __init__(     self				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        requires_backends(self				,							['''torch''', '''scipy''']					)





     @classmethod
     def 		UpperCamelCase(     cls				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        requires_backends(cls				,							['''torch''', '''scipy''']					)





     @classmethod
     def 		UpperCamelCase(     cls				,							*_UpperCamelCase				,							**_UpperCamelCase					):
        requires_backends(cls				,							['''torch''', '''scipy''']					)