

"""simple docstring"""




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   __lowercase      (     unittest.TestCase       ):

  def __init__(	self		:      str       ,A		:      Optional[int]       ,A		:      int=13       ,A		:      List[Any]=7       ,A		:      Optional[int]=True       ,A		:      Optional[int]=True       ,A		:      Union[str, Any]=True       ,A		:      Any=True       ,A		:      Optional[int]=99       ,A		:      Optional[Any]=32       ,A		:      int=5       ,A		:      str=4       ,A		:      str=37       ,A		:      Tuple="gelu"       ,A		:      Tuple=0.1       ,A		:      Dict=0.1       ,A		:      Dict=512       ,A		:      str=16       ,A		:      List[Any]=2       ,A		:      Tuple=0.0_2       ,A		:      Union[str, Any]=4       ,):






   '''simple docstring'''
   UpperCAmelCase__       :	str        =       parent
   UpperCAmelCase__       :	Optional[int]        =       batch_size
   UpperCAmelCase__       :	List[Any]        =       seq_length
   UpperCAmelCase__       :	int        =       is_training
   UpperCAmelCase__       :	List[Any]        =       use_attention_mask
   UpperCAmelCase__       :	str        =       use_token_type_ids
   UpperCAmelCase__       :	Any        =       use_labels
   UpperCAmelCase__       :	str        =       vocab_size
   UpperCAmelCase__       :	str        =       hidden_size
   UpperCAmelCase__       :	List[Any]        =       num_hidden_layers
   UpperCAmelCase__       :	Optional[Any]        =       num_attention_heads
   UpperCAmelCase__       :	Dict        =       intermediate_size
   UpperCAmelCase__       :	Tuple        =       hidden_act
   UpperCAmelCase__       :	Tuple        =       hidden_dropout_prob
   UpperCAmelCase__       :	Dict        =       attention_probs_dropout_prob
   UpperCAmelCase__       :	Dict        =       max_position_embeddings
   UpperCAmelCase__       :	Optional[int]        =       type_vocab_size
   UpperCAmelCase__       :	Any        =       type_sequence_label_size
   UpperCAmelCase__       :	Tuple        =       initializer_range
   UpperCAmelCase__       :	Optional[Any]        =       num_choices

  def  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	List[Any]        =       ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size     )

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

   UpperCAmelCase__       :	Any        =       None
   if self.use_token_type_ids:
    UpperCAmelCase__       :	Optional[int]        =       ids_tensor([self.batch_size, self.seq_length]       ,self.type_vocab_size     )

   UpperCAmelCase__       :	Dict        =       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=lowercase_       ,initializer_range=self.initializer_range       ,)

   return config, input_ids, token_type_ids, attention_mask

  def  __lowercase							(	self		:      List[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Union[str, Any]        =       self.prepare_config_and_inputs()
   UpperCAmelCase__ ,   UpperCAmelCase__ ,   UpperCAmelCase__ ,   UpperCAmelCase__       :	List[str]        =       config_and_inputs
   UpperCAmelCase__       :	Dict        =       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
   return config, inputs_dict




  def  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   UpperCAmelCase__       :	int        =       self.prepare_config_and_inputs()
   UpperCAmelCase__ ,   UpperCAmelCase__ ,   UpperCAmelCase__ ,   UpperCAmelCase__       :	Optional[int]        =       config_and_inputs

   UpperCAmelCase__       :	Union[str, Any]        =       True
   UpperCAmelCase__       :	str        =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]     )
   UpperCAmelCase__       :	List[str]        =       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   __lowercase      (     lowercase__						,      unittest.TestCase       ):
  snake_case_												=							True

  snake_case_												=							(
      (
          FlaxRobertaModel,
          FlaxRobertaForCausalLM,
          FlaxRobertaForMaskedLM,
          FlaxRobertaForSequenceClassification,
          FlaxRobertaForTokenClassification,
          FlaxRobertaForMultipleChoice,
          FlaxRobertaForQuestionAnswering,
      )
      if is_flax_available()
      else ()
  )

  def  __lowercase							(	self		:      str     ):






   '''simple docstring'''
   UpperCAmelCase__       :	Tuple        =       FlaxRobertaModelTester(self     )




  @slow
  def  __lowercase							(	self		:      Optional[Any]     ):






   '''simple docstring'''
   for model_class_name in self.all_model_classes:
    UpperCAmelCase__       :	Tuple        =       model_class_name.from_pretrained("""roberta-base"""       ,from_pt=lowercase_     )
    UpperCAmelCase__       :	Any        =       model(np.ones((1, 1)     )     )
    self.assertIsNotNone(lowercase_     )









from importlib import import_module

from .logging import get_logger


_lowerCAmelCase:						str   				=       get_logger(__name__)




class       lowercase_					:





				def __init__(				self       ,  lowercase_       ,  lowercase_=None)  -> Tuple:
											a__      =attrs or []
											if module is not None:
																		for key in module.__dict__:
																									if key in attrs or not key.startswith('__'):
																																setattr(self       ,  lowercase_       ,  getattr(lowercase_       ,  lowercase_))
											a__      =module._original_module if isinstance(lowercase_       ,  _PatchedModuleObj) else module







class       lowercase_					:
				snake_case	    =[]





				def __init__(				self       ,  lowercase_       ,  lowercase_       ,  lowercase_       ,  lowercase_=None)  -> List[str]:
											a__      =obj
											a__      =target
											a__      =new
											a__      =target.split('.')[0]
											a__      ={}
											a__      =attrs or []





				def __enter__(				self)  -> Optional[int]:
											*a__    ,     a__      =self.target.split('.')

											# Patch modules:
											# it's used to patch attributes of submodules like "os.path.join";
											# in this case we need to patch "os" and "os.path"

											for i in range(len(lowercase_)):
																		try:
																									a__      =import_module('.'.join(submodules[: i + 1]))
																		except ModuleNotFoundError:
																									continue
																		# We iterate over all the globals in self.obj in case we find "os" or "os.path"
																		for attr in self.obj.__dir__():
																									a__      =getattr(self.obj       ,  lowercase_)
																									# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
																									# This allows to patch renamed modules like "from os import path as ospath".
																									if obj_attr is submodule or (
																									    (isinstance(lowercase_       ,  _PatchedModuleObj) and obj_attr._original_module is submodule)
																									):
																																a__      =obj_attr
																																# patch at top level
																																setattr(self.obj       ,  lowercase_       ,  _PatchedModuleObj(lowercase_       ,  attrs=self.attrs))
																																a__      =getattr(self.obj       ,  lowercase_)
																																# construct lower levels patches
																																for key in submodules[i + 1 :]:
																																							setattr(lowercase_       ,  lowercase_       ,  _PatchedModuleObj(getattr(lowercase_       ,  lowercase_       ,  lowercase_)       ,  attrs=self.attrs))
																																							a__      =getattr(lowercase_       ,  lowercase_)
																																# finally set the target attribute
																																setattr(lowercase_       ,  lowercase_       ,  self.new)

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

											if submodules:  # if it's an attribute of a submodule like "os.path.join"
																		try:
																									a__      =getattr(import_module('.'.join(lowercase_))       ,  lowercase_)
																		except (AttributeError, ModuleNotFoundError):
																									return
																		# We iterate over all the globals in self.obj in case we find "os.path.join"
																		for attr in self.obj.__dir__():
																									# We don't check for the name of the global, but rather if its value *is* "os.path.join".
																									# This allows to patch renamed attributes like "from os.path import join as pjoin".
																									if getattr(self.obj       ,  lowercase_) is attr_value:
																																a__      =getattr(self.obj       ,  lowercase_)
																																setattr(self.obj       ,  lowercase_       ,  self.new)
											elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
																		a__      =globals()['__builtins__'][target_attr]
																		setattr(self.obj       ,  lowercase_       ,  self.new)
											else:
																		raise RuntimeError(F"""Tried to patch attribute {target_attr} instead of a submodule.""")





				def __exit__(				self       ,  *lowercase_)  -> str:
											for attr in list(self.original):
																		setattr(self.obj       ,  lowercase_       ,  self.original.pop(lowercase_))





				def 						__UpperCamelCase    (				self)  -> Any:
											self.__enter__()
											self._active_patches.append(self)





				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											try:
																		self._active_patches.remove(self)
											except ValueError:
																		# If the patch hasn't been started this will fail
																		return None

											return self.__exit__()

