




import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    YosoForMaskedLM,
				    YosoForMultipleChoice,
				    YosoForQuestionAnswering,
				    YosoForSequenceClassification,
				    YosoForTokenClassification,
				    YosoModel,
				)
				from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST






class 					__lowerCAmelCase				:







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




									"""simple docstring"""


									_UpperCAmelCase									=  parent
									_UpperCAmelCase									=  batch_size
									_UpperCAmelCase									=  seq_length
									_UpperCAmelCase									=  is_training
									_UpperCAmelCase									=  use_input_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_labels
									_UpperCAmelCase									=  num_choices
									_UpperCAmelCase									=  scope







			def UpperCamelCase    ( self					:    Optional[int]   ):




									"""simple docstring"""


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

									_UpperCAmelCase									=  None
									if self.use_input_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									=  None
									_UpperCAmelCase									=  None
									_UpperCAmelCase									=  None
									if self.use_labels:
															_UpperCAmelCase									=  ids_tensor([self.batch_size]					,					self.type_sequence_label_size   )
															_UpperCAmelCase									=  ids_tensor([self.batch_size, self.seq_length]					,					self.num_labels   )
															_UpperCAmelCase									=  ids_tensor([self.batch_size]					,					self.num_choices   )

									_UpperCAmelCase									=  self.get_config()

									return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







			def UpperCamelCase    ( self					:    Optional[int]   ):




									"""simple docstring"""


									return YosoConfig(
									    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					,					)







			def UpperCamelCase    ( self					:    Tuple   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.get_config()
									_UpperCAmelCase									=  300
									return config







			def UpperCamelCase    ( self					:    List[Any]   ):




									"""simple docstring"""


									(
									    (
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				
									)									=  self.prepare_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,
									    input_mask,
									    sequence_labels,
									    token_labels,
									    choice_labels,
									    encoder_hidden_states,
									    encoder_attention_mask,
									)







			def UpperCamelCase    ( self					:    str					,					snake_case__					:    str					,					snake_case__					:    List[str]					,					snake_case__					:    int					,					snake_case__					:    Union[str, Any]					,					snake_case__					:    Any					,					snake_case__					:    Optional[Any]					,					snake_case__					:    List[str]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  YosoModel(config=snake_case__   )
									model.to(snake_case__   )
									model.eval()
									_UpperCAmelCase									=  model(snake_case__					,					attention_mask=snake_case__					,					token_type_ids=snake_case__   )
									_UpperCAmelCase									=  model(snake_case__					,					token_type_ids=snake_case__   )
									_UpperCAmelCase									=  model(snake_case__   )
									self.parent.assertEqual(result.last_hidden_state.shape					,					(self.batch_size, self.seq_length, self.hidden_size)   )







			def UpperCamelCase    ( self					:    int					,					snake_case__					:    Any					,					snake_case__					:    str					,					snake_case__					:    Optional[Any]					,					snake_case__					:    Union[str, Any]					,					snake_case__					:    Union[str, Any]					,					snake_case__					:    Dict					,					snake_case__					:    Optional[int]					,					snake_case__					:    Tuple					,					snake_case__					:    List[str]					,					):




									"""simple docstring"""


									_UpperCAmelCase									=  True
									_UpperCAmelCase									=  YosoModel(snake_case__   )
									model.to(snake_case__   )
									model.eval()
									_UpperCAmelCase									=  model(
									    snake_case__					,					attention_mask=snake_case__					,					token_type_ids=snake_case__					,					encoder_hidden_states=snake_case__					,					encoder_attention_mask=snake_case__					,					)
									_UpperCAmelCase									=  model(
									    snake_case__					,					attention_mask=snake_case__					,					token_type_ids=snake_case__					,					encoder_hidden_states=snake_case__					,					)
									_UpperCAmelCase									=  model(snake_case__					,					attention_mask=snake_case__					,					token_type_ids=snake_case__   )
									self.parent.assertEqual(result.last_hidden_state.shape					,					(self.batch_size, self.seq_length, self.hidden_size)   )







			def UpperCamelCase    ( self					:    Optional[int]					,					snake_case__					:    Optional[int]					,					snake_case__					:    str					,					snake_case__					:    List[Any]					,					snake_case__					:    Dict					,					snake_case__					:    Any					,					snake_case__					:    Dict					,					snake_case__					:    str   ):




									"""simple docstring"""


									_UpperCAmelCase									=  YosoForMaskedLM(config=snake_case__   )
									model.to(snake_case__   )
									model.eval()
									_UpperCAmelCase									=  model(snake_case__					,					attention_mask=snake_case__					,					token_type_ids=snake_case__					,					labels=snake_case__   )
									self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.seq_length, self.vocab_size)   )







			def UpperCamelCase    ( self					:    Dict					,					snake_case__					:    List[str]					,					snake_case__					:    Tuple					,					snake_case__					:    Dict					,					snake_case__					:    List[str]					,					snake_case__					:    str					,					snake_case__					:    str					,					snake_case__					:    List[str]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  YosoForQuestionAnswering(config=snake_case__   )
									model.to(snake_case__   )
									model.eval()
									_UpperCAmelCase									=  model(
									    snake_case__					,					attention_mask=snake_case__					,					token_type_ids=snake_case__					,					start_positions=snake_case__					,					end_positions=snake_case__					,					)
									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 UpperCamelCase    ( self					:    str					,					snake_case__					:    Any					,					snake_case__					:    Optional[Any]					,					snake_case__					:    List[Any]					,					snake_case__					:    int					,					snake_case__					:    Optional[int]					,					snake_case__					:    int					,					snake_case__					:    Optional[int]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.num_labels
									_UpperCAmelCase									=  YosoForSequenceClassification(snake_case__   )
									model.to(snake_case__   )
									model.eval()
									_UpperCAmelCase									=  model(snake_case__					,					attention_mask=snake_case__					,					token_type_ids=snake_case__					,					labels=snake_case__   )
									self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.num_labels)   )







			def UpperCamelCase    ( self					:    str					,					snake_case__					:    Optional[Any]					,					snake_case__					:    int					,					snake_case__					:    List[str]					,					snake_case__					:    Tuple					,					snake_case__					:    Optional[int]					,					snake_case__					:    Tuple					,					snake_case__					:    int   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.num_labels
									_UpperCAmelCase									=  YosoForTokenClassification(config=snake_case__   )
									model.to(snake_case__   )
									model.eval()
									_UpperCAmelCase									=  model(snake_case__					,					attention_mask=snake_case__					,					token_type_ids=snake_case__					,					labels=snake_case__   )
									self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.seq_length, self.num_labels)   )







			def UpperCamelCase    ( self					:    Any					,					snake_case__					:    Tuple					,					snake_case__					:    Dict					,					snake_case__					:    int					,					snake_case__					:    Dict					,					snake_case__					:    Optional[int]					,					snake_case__					:    Dict					,					snake_case__					:    Any   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.num_choices
									_UpperCAmelCase									=  YosoForMultipleChoice(config=snake_case__   )
									model.to(snake_case__   )
									model.eval()
									_UpperCAmelCase									=  input_ids.unsqueeze(1   ).expand(-1					,					self.num_choices					,					-1   ).contiguous()
									_UpperCAmelCase									=  token_type_ids.unsqueeze(1   ).expand(-1					,					self.num_choices					,					-1   ).contiguous()
									_UpperCAmelCase									=  input_mask.unsqueeze(1   ).expand(-1					,					self.num_choices					,					-1   ).contiguous()
									_UpperCAmelCase									=  model(
									    snake_case__					,					attention_mask=snake_case__					,					token_type_ids=snake_case__					,					labels=snake_case__					,					)
									self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.num_choices)   )







			def UpperCamelCase    ( self					:    Tuple   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.prepare_config_and_inputs()
									(
									    (
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				(
									    _UpperCAmelCase
									)							,				
									)									=  config_and_inputs
									_UpperCAmelCase									=  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
									return config, inputs_dict






@require_torch
class 					__lowerCAmelCase				( UpperCAmelCase__      ,	UpperCAmelCase__      ,	unittest.TestCase				):
			snake_case_   :				Optional[int]				  =  (
			    (
			        YosoModel,
			        YosoForMaskedLM,
			        YosoForMultipleChoice,
			        YosoForQuestionAnswering,
			        YosoForSequenceClassification,
			        YosoForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			snake_case_   :				int				  =  False
			snake_case_   :				Any				  =  False
			snake_case_   :				Union[str, Any]				  =  False

			snake_case_   :				Union[str, Any]				  =  ()
			snake_case_   :				Optional[Any]				  =  (
			    {
			        "feature-extraction": YosoModel,
			        "fill-mask": YosoForMaskedLM,
			        "question-answering": YosoForQuestionAnswering,
			        "text-classification": YosoForSequenceClassification,
			        "token-classification": YosoForTokenClassification,
			        "zero-shot": YosoForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)







			def UpperCamelCase    ( self					:    Optional[int]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  YosoModelTester(self   )
									_UpperCAmelCase									=  ConfigTester(self					,					config_class=snake_case__					,					hidden_size=37   )







			def UpperCamelCase    ( self					:    Dict   ):




									"""simple docstring"""


									self.config_tester.run_common_tests()







			def UpperCamelCase    ( self					:    Any   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*snake_case__   )







			def UpperCamelCase    ( self					:    List[Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
															_UpperCAmelCase									=  type
															self.model_tester.create_and_check_model(*snake_case__   )







			def UpperCamelCase    ( self					:    Dict   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*snake_case__   )







			def UpperCamelCase    ( self					:    Tuple   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*snake_case__   )







			def UpperCamelCase    ( self					:    List[Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*snake_case__   )







			def UpperCamelCase    ( self					:    int   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*snake_case__   )







			def UpperCamelCase    ( self					:    Any   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*snake_case__   )







			@slow
			def UpperCamelCase    ( self					:    Union[str, Any]   ):




									"""simple docstring"""


									for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_UpperCAmelCase									=  YosoModel.from_pretrained(snake_case__   )
															self.assertIsNotNone(snake_case__   )







			def UpperCamelCase    ( self					:    int   ):




									"""simple docstring"""


									return






@require_torch
class 					__lowerCAmelCase				( unittest.TestCase				):







			@slow
			def UpperCamelCase    ( self					:    Tuple   ):




									"""simple docstring"""


									_UpperCAmelCase									=  YosoModel.from_pretrained("uw-madison/yoso-4096"   )
									_UpperCAmelCase									=  torch.tensor([[0, 1, 2, 3, 4, 5]]   )

									with torch.no_grad():
															_UpperCAmelCase									=  model(snake_case__   )[0]

									_UpperCAmelCase									=  torch.Size((1, 6, 768)   )
									self.assertEqual(output.shape					,					snake_case__   )

									_UpperCAmelCase									=  torch.tensor(
									    [[[-0.0_611, 0.1_242, 0.0_840], [0.0_280, -0.0_048, 0.1_125], [0.0_106, 0.0_226, 0.0_751]]]   )

									self.assertTrue(torch.allclose(output[:, :3, :3]					,					snake_case__					,					atol=1e-4   )   )







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




									"""simple docstring"""


									_UpperCAmelCase									=  YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"   )
									_UpperCAmelCase									=  torch.tensor([[0, 1, 2, 3, 4, 5]]   )

									with torch.no_grad():
															_UpperCAmelCase									=  model(snake_case__   )[0]

									_UpperCAmelCase									=  50_265

									_UpperCAmelCase									=  torch.Size((1, 6, vocab_size)   )
									self.assertEqual(output.shape					,					snake_case__   )

									_UpperCAmelCase									=  torch.tensor(
									    [[[-2.1_313, -3.7_285, -2.2_407], [-2.7_047, -3.3_314, -2.6_408], [0.0_629, -2.5_166, -0.3_356]]]   )

									self.assertTrue(torch.allclose(output[:, :3, :3]					,					snake_case__					,					atol=1e-4   )   )







			@slow
			def UpperCamelCase    ( self					:    Union[str, Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"   )
									_UpperCAmelCase									=  torch.arange(4_096   ).unsqueeze(0   )

									with torch.no_grad():
															_UpperCAmelCase									=  model(snake_case__   )[0]

									_UpperCAmelCase									=  50_265

									_UpperCAmelCase									=  torch.Size((1, 4_096, vocab_size)   )
									self.assertEqual(output.shape					,					snake_case__   )

									_UpperCAmelCase									=  torch.tensor(
									    [[[-2.3_914, -4.3_742, -5.0_956], [-4.0_988, -4.2_384, -7.0_406], [-3.1_427, -3.7_192, -6.6_800]]]   )

									self.assertTrue(torch.allclose(output[:, :3, :3]					,					snake_case__					,					atol=1e-4   )   )










from math import exp


def 							__SCREAMING_SNAKE_CASE						(    snake_case_		):

						'''simple docstring'''


						return 8 * x - 2 * exp(-x		)


def 							__SCREAMING_SNAKE_CASE						(    snake_case_			,					snake_case_			,					snake_case_		):

						'''simple docstring'''


						_UpperCAmelCase									=  lower_bound
						_UpperCAmelCase									=  upper_bound
						for _ in range(0			,					snake_case_		):
												_UpperCAmelCase							,				_UpperCAmelCase									=  xa, xa - (f(snake_case_		) * (xa - xa)) / (f(snake_case_		) - f(snake_case_		))
						return xa


if __name__ == "__main__":
				print(f"""Example: {secant_method(1, 3, 2)}""")




