




import unittest

import numpy as np

from transformers import BertConfig, 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.bert.modeling_flax_bert import (
	    FlaxBertForMaskedLM,
	    FlaxBertForMultipleChoice,
	    FlaxBertForNextSentencePrediction,
	    FlaxBertForPreTraining,
	    FlaxBertForQuestionAnswering,
	    FlaxBertForSequenceClassification,
	    FlaxBertForTokenClassification,
	    FlaxBertModel,
	)



class 							SCREAMING_SNAKE_CASE__		(       unittest.TestCase		):





					"""simple docstring"""






					def __init__(      self  :						str							,	__A  :						Optional[int]							,	__A  :						Optional[int]=1_3							,	__A  :						Optional[Any]=7							,	__A  :						List[str]=True							,	__A  :						int=True							,	__A  :						int=True							,	__A  :						Union[str, Any]=True							,	__A  :						Union[str, Any]=9_9							,	__A  :						List[Any]=3_2							,	__A  :						Optional[Any]=5							,	__A  :						int=4							,	__A  :						List[str]=3_7							,	__A  :						Any="gelu"							,	__A  :						Tuple=0.1							,	__A  :						Tuple=0.1							,	__A  :						Union[str, Any]=5_1_2							,	__A  :						Tuple=1_6							,	__A  :						List[Any]=2							,	__A  :						List[str]=0.0_2							,	__A  :						List[str]=4							,	):
						snake_case__				:   Union[str, Any]     =						parent
						snake_case__				:   Any     =						batch_size
						snake_case__				:   Optional[int]     =						seq_length
						snake_case__				:   List[str]     =						is_training
						snake_case__				:   Optional[int]     =						use_attention_mask
						snake_case__				:   Dict     =						use_token_type_ids
						snake_case__				:   Optional[int]     =						use_labels
						snake_case__				:   Tuple     =						vocab_size
						snake_case__				:   Union[str, Any]     =						hidden_size
						snake_case__				:   List[str]     =						num_hidden_layers
						snake_case__				:   Optional[int]     =						num_attention_heads
						snake_case__				:   Optional[Any]     =						intermediate_size
						snake_case__				:   Optional[int]     =						hidden_act
						snake_case__				:   List[str]     =						hidden_dropout_prob
						snake_case__				:   Tuple     =						attention_probs_dropout_prob
						snake_case__				:   Dict     =						max_position_embeddings
						snake_case__				:   List[str]     =						type_vocab_size
						snake_case__				:   str     =						type_sequence_label_size
						snake_case__				:   List[str]     =						initializer_range
						snake_case__				:   Union[str, Any]     =						num_choices



					def  _lowercase  (      self  :						Optional[int] ):
						snake_case__				:   Optional[int]     =						ids_tensor([self.batch_size, self.seq_length]							,	self.vocab_size )

						snake_case__				:   List[Any]     =						None
						if self.use_attention_mask:
							snake_case__				:   Union[str, Any]     =						random_attention_mask([self.batch_size, self.seq_length] )

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

						snake_case__				:   Optional[int]     =						BertConfig(
						    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=_snake_case							,	initializer_range=self.initializer_range							,	)

						return config, input_ids, token_type_ids, attention_mask



					def  _lowercase  (      self  :						int ):
						snake_case__				:   Any     =						self.prepare_config_and_inputs()
						snake_case__,		snake_case__,		snake_case__,		snake_case__				:   Optional[Any]     =						config_and_inputs
						snake_case__				:   Tuple     =						{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
						return config, inputs_dict






					def  _lowercase  (      self  :						str ):
						snake_case__				:   str     =						self.prepare_config_and_inputs()
						snake_case__,		snake_case__,		snake_case__,		snake_case__				:   List[str]     =						config_and_inputs

						snake_case__				:   Tuple     =						True
						snake_case__				:   List[str]     =						floats_tensor([self.batch_size, self.seq_length, self.hidden_size] )
						snake_case__				:   Any     =						ids_tensor([self.batch_size, self.seq_length]							,	vocab_size=2 )

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



@require_flax
class 							SCREAMING_SNAKE_CASE__		(       UpperCAmelCase_				,     unittest.TestCase		):





					"""simple docstring"""






					a_			=	True

					a_			=	(
					    (
					        FlaxBertModel,
					        FlaxBertForPreTraining,
					        FlaxBertForMaskedLM,
					        FlaxBertForMultipleChoice,
					        FlaxBertForQuestionAnswering,
					        FlaxBertForNextSentencePrediction,
					        FlaxBertForSequenceClassification,
					        FlaxBertForTokenClassification,
					        FlaxBertForQuestionAnswering,
					    )
					    if is_flax_available()
					    else ()
					)



					def  _lowercase  (      self  :						Any ):
						snake_case__				:   Optional[int]     =						FlaxBertModelTester(self )






					@slow
					def  _lowercase  (      self  :						int ):
						snake_case__				:   List[Any]     =						FlaxBertModel.from_pretrained("bert-base-cased" )
						snake_case__				:   Optional[int]     =						model(np.ones((1, 1) ) )
						self.assertIsNotNone(_snake_case )











import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
	import torch

	from transformers.activations import gelu_new, gelu_python, get_activation



@require_torch
class 							SCREAMING_SNAKE_CASE__		(       unittest.TestCase		):





					"""simple docstring"""






					def  _lowercase  (      self  :						Tuple ):
						snake_case__				:   List[str]     =						torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0] )
						snake_case__				:   Tuple     =						get_activation("gelu" )
						self.assertTrue(torch.allclose(gelu_python(__A )							,	torch_builtin(__A ) ) )
						self.assertFalse(torch.allclose(gelu_python(__A )							,	gelu_new(__A ) ) )



					def  _lowercase  (      self  :						Dict ):
						snake_case__				:   str     =						torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0] )
						snake_case__				:   Union[str, Any]     =						get_activation("gelu" )
						snake_case__				:   int     =						get_activation("gelu_10" )

						snake_case__				:   Optional[int]     =						torch_builtin(__A )
						snake_case__				:   Dict     =						geluaa(__A )

						snake_case__				:   Optional[Any]     =						torch.where(y_gelu_aa < 1_0.0							,	1							,	0 )

						self.assertTrue(torch.max(__A ).item() == 1_0.0 )
						self.assertTrue(torch.allclose(y_gelu * clipped_mask							,	y_gelu_aa * clipped_mask ) )



					def  _lowercase  (      self  :						str ):
						get_activation("gelu" )
						get_activation("gelu_10" )
						get_activation("gelu_fast" )
						get_activation("gelu_new" )
						get_activation("gelu_python" )
						get_activation("gelu_pytorch_tanh" )
						get_activation("linear" )
						get_activation("mish" )
						get_activation("quick_gelu" )
						get_activation("relu" )
						get_activation("sigmoid" )
						get_activation("silu" )
						get_activation("swish" )
						get_activation("tanh" )
						with self.assertRaises(__A ):
							get_activation("bogus" )
						with self.assertRaises(__A ):
							get_activation(__A )






					def  _lowercase  (      self  :						List[str] ):
						snake_case__				:   List[str]     =						get_activation("gelu" )
						snake_case__				:   Any     =						1
						snake_case__				:   Union[str, Any]     =						get_activation("gelu" )
						self.assertEqual(acta.a							,	1 )
						with self.assertRaises(__A ):
							snake_case__				:   int     =						acta.a



