




"""simple docstring"""

import unittest

from transformers import ErnieMConfig, 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, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    ErnieMForInformationExtraction,
		    ErnieMForMultipleChoice,
		    ErnieMForQuestionAnswering,
		    ErnieMForSequenceClassification,
		    ErnieMForTokenClassification,
		    ErnieMModel,
		)
		from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST
class     A__      :




	def __init__(   self  :	int      ,    lowercase  :	Any      ,    lowercase  :	Tuple=13      ,    lowercase  :	Any=7      ,    lowercase  :	Optional[Any]=True      ,    lowercase  :	Union[str, Any]=True      ,    lowercase  :	Dict=True      ,    lowercase  :	Union[str, Any]=99      ,    lowercase  :	Dict=32      ,    lowercase  :	Optional[int]=5      ,    lowercase  :	Optional[int]=4      ,    lowercase  :	str=37      ,    lowercase  :	str="gelu"      ,    lowercase  :	Optional[Any]=0.1      ,    lowercase  :	int=0.1      ,    lowercase  :	Optional[int]=512      ,    lowercase  :	Any=16      ,    lowercase  :	str=2      ,    lowercase  :	Any=0.02      ,    lowercase  :	Dict=3      ,    lowercase  :	List[Any]=4      ,    lowercase  :	Any=None      ,    )					-> int:
						__snake_case          =	parent
						__snake_case          =	batch_size
						__snake_case          =	seq_length
						__snake_case          =	is_training
						__snake_case          =	use_input_mask
						__snake_case          =	use_labels
						__snake_case          =	vocab_size
						__snake_case          =	hidden_size
						__snake_case          =	num_hidden_layers
						__snake_case          =	num_attention_heads
						__snake_case          =	intermediate_size
						__snake_case          =	hidden_act
						__snake_case          =	hidden_dropout_prob
						__snake_case          =	attention_probs_dropout_prob
						__snake_case          =	max_position_embeddings
						__snake_case          =	type_vocab_size
						__snake_case          =	type_sequence_label_size
						__snake_case          =	initializer_range
						__snake_case          =	num_labels
						__snake_case          =	num_choices
						__snake_case          =	scope




	def 							a_		(   self  :	int	)					-> Tuple:
						__snake_case          =	ids_tensor([self.batch_size, self.seq_length]      ,    self.vocab_size	)

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

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

						__snake_case          =	self.get_config()

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




	def 							a_		(   self  :	List[Any]	)					-> Optional[int]:
						__snake_case          =	ids_tensor([self.batch_size, self.seq_length]      ,    self.vocab_size	)

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

						return config, input_ids, input_mask




	def 							a_		(   self  :	int	)					-> Optional[Any]:
						return ErnieMConfig(
						    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      ,    initializer_range=self.initializer_range      ,    )




	def 							a_		(   self  :	List[str]      ,    lowercase  :	Dict      ,    lowercase  :	List[Any]      ,    lowercase  :	Optional[Any]      ,    lowercase  :	Dict      ,    lowercase  :	Tuple      ,    lowercase  :	List[Any]	)					-> List[str]:
						__snake_case          =	ErnieMModel(config=lowercase	)
						model.to(lowercase	)
						model.eval()
						__snake_case          =	model(lowercase      ,    return_dict=lowercase	)
						self.parent.assertEqual(result.last_hidden_state.shape      ,    (self.batch_size, self.seq_length, self.hidden_size)	)




	def 							a_		(   self  :	Optional[Any]      ,    lowercase  :	str      ,    lowercase  :	List[str]      ,    lowercase  :	int      ,    lowercase  :	Dict      ,    lowercase  :	Union[str, Any]      ,    lowercase  :	Tuple	)					-> int:
						__snake_case          =	ErnieMForQuestionAnswering(config=lowercase	)
						model.to(lowercase	)
						model.eval()
						__snake_case          =	model(
						    lowercase      ,    attention_mask=lowercase      ,    start_positions=lowercase      ,    end_positions=lowercase      ,    )
						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 							a_		(   self  :	Dict      ,    lowercase  :	Union[str, Any]      ,    lowercase  :	Union[str, Any]      ,    lowercase  :	Optional[int]      ,    lowercase  :	int      ,    lowercase  :	Optional[Any]      ,    lowercase  :	Tuple	)					-> List[str]:
						__snake_case          =	ErnieMForInformationExtraction(config=lowercase	)
						model.to(lowercase	)
						model.eval()
						__snake_case          =	torch.ones_like(lowercase      ,    dtype=torch.floataa	)
						__snake_case          =	model(
						    lowercase      ,    attention_mask=lowercase      ,    start_positions=lowercase      ,    end_positions=lowercase      ,    )
						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 							a_		(   self  :	int      ,    lowercase  :	str      ,    lowercase  :	Optional[int]      ,    lowercase  :	Any      ,    lowercase  :	List[Any]      ,    lowercase  :	List[Any]      ,    lowercase  :	List[str]	)					-> Tuple:
						__snake_case          =	self.num_labels
						__snake_case          =	ErnieMForSequenceClassification(lowercase	)
						model.to(lowercase	)
						model.eval()
						__snake_case          =	model(lowercase      ,    attention_mask=lowercase      ,    labels=lowercase	)
						self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.num_labels)	)




	def 							a_		(   self  :	Union[str, Any]      ,    lowercase  :	List[Any]      ,    lowercase  :	Optional[int]      ,    lowercase  :	str      ,    lowercase  :	Dict      ,    lowercase  :	Dict      ,    lowercase  :	Optional[Any]	)					-> Any:
						__snake_case          =	self.num_labels
						__snake_case          =	ErnieMForTokenClassification(config=lowercase	)
						model.to(lowercase	)
						model.eval()
						input_ids.to(lowercase	)
						input_mask.to(lowercase	)
						token_labels.to(lowercase	)

						__snake_case          =	model(lowercase      ,    attention_mask=lowercase      ,    labels=lowercase	)

						self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.seq_length, self.num_labels)	)




	def 							a_		(   self  :	Optional[int]      ,    lowercase  :	Any      ,    lowercase  :	Optional[Any]      ,    lowercase  :	Tuple      ,    lowercase  :	List[Any]      ,    lowercase  :	Any      ,    lowercase  :	Optional[Any]	)					-> Union[str, Any]:
						__snake_case          =	self.num_choices
						__snake_case          =	ErnieMForMultipleChoice(config=lowercase	)
						model.to(lowercase	)
						model.eval()
						__snake_case          =	input_ids.unsqueeze(1	).expand(-1      ,    self.num_choices      ,    -1	).contiguous()
						__snake_case          =	input_mask.unsqueeze(1	).expand(-1      ,    self.num_choices      ,    -1	).contiguous()
						__snake_case          =	model(
						    lowercase      ,    attention_mask=lowercase      ,    labels=lowercase      ,    )
						self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.num_choices)	)




	def 							a_		(   self  :	Tuple	)					-> List[str]:
						__snake_case          =	self.prepare_config_and_inputs()
						(
						    (
						    __snake_case      
						)      ,      (
						    __snake_case      
						)      ,      (
						    __snake_case      
						)      ,      (
						    __snake_case      
						)      ,      (
						    __snake_case      
						)      ,      (
						    __snake_case      
						)      ,            
						)          =	config_and_inputs
						__snake_case          =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
						return config, inputs_dict

@require_torch
class     A__      (      lowercase_     ,	lowercase_     ,	unittest.TestCase     ):
	A_							:				Optional[Any]						     =      (
	    (
	        ErnieMModel,
	        ErnieMForMultipleChoice,
	        ErnieMForQuestionAnswering,
	        ErnieMForSequenceClassification,
	        ErnieMForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	A_							:				Any						     =      ()
	A_							:				int						     =      (
	    {
	        """feature-extraction""": ErnieMModel,
	        """question-answering""": ErnieMForQuestionAnswering,
	        """text-classification""": ErnieMForSequenceClassification,
	        """token-classification""": ErnieMForTokenClassification,
	        """zero-shot""": ErnieMForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	A_							:				str						     =      False




	def 							a_		(   self  :	Optional[int]      ,    lowercase  :	Dict      ,    lowercase  :	Tuple      ,    lowercase  :	str      ,    lowercase  :	str      ,    lowercase  :	Dict	)					-> Optional[Any]:
						if pipeline_test_casse_name == "QAPipelineTests":
											return True

						return False




	def 							a_		(   self  :	int	)					-> Optional[int]:
						__snake_case          =	ErnieMModelTester(self	)
						__snake_case          =	ConfigTester(self      ,    config_class=lowercase      ,    hidden_size=37	)




	def 							a_		(   self  :	int	)					-> Union[str, Any]:
						self.config_tester.run_common_tests()




	def 							a_		(   self  :	Tuple	)					-> Tuple:
						__snake_case          =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase	)




	def 							a_		(   self  :	Union[str, Any]	)					-> str:
						__snake_case          =	self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
											__snake_case          =	type
											self.model_tester.create_and_check_model(*lowercase	)




	def 							a_		(   self  :	str	)					-> List[Any]:
						__snake_case          =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*lowercase	)




	def 							a_		(   self  :	Optional[int]	)					-> Optional[int]:
						__snake_case          =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*lowercase	)




	def 							a_		(   self  :	Optional[int]	)					-> Optional[int]:
						__snake_case          =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*lowercase	)




	def 							a_		(   self  :	Optional[int]	)					-> List[Any]:
						__snake_case          =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_information_extraction(*lowercase	)




	def 							a_		(   self  :	Any	)					-> str:
						__snake_case          =	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*lowercase	)




	@slow
	def 							a_		(   self  :	Optional[Any]	)					-> Optional[int]:
						for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__snake_case          =	ErnieMModel.from_pretrained(lowercase	)
											self.assertIsNotNone(lowercase	)

@require_torch
class     A__      (      unittest.TestCase     ):




	@slow
	def 							a_		(   self  :	Optional[Any]	)					-> List[str]:
						__snake_case          =	ErnieMModel.from_pretrained("""susnato/ernie-m-base_pytorch"""	)
						model.eval()
						__snake_case          =	torch.tensor([[0, 1, 2, 3, 4, 5]]	)
						__snake_case          =	model(lowercase	)[0]

						# TODO Replace vocab size
						__snake_case          =	768

						__snake_case          =	torch.Size((1, 6, hidden_size)	)
						self.assertEqual(output.shape      ,    lowercase	)

						__snake_case          =	torch.tensor(
						    [[[-0.00_12, 0.12_45, -0.02_14], [-0.07_42, 0.02_44, -0.07_71], [-0.03_33, 0.11_64, -0.15_54]]]	)

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








"""simple docstring"""

from functools import lru_cache




@lru_cache
def       snake_case__	(       lowerCAmelCase   :		int		):
					if not isinstance(lowerCAmelCase				,					lowerCAmelCase		):
										raise TypeError("""k must be an integer."""		)
					if k < 0:
										raise ValueError("""k integer must be greater or equal to zero."""		)
					if k == 0:
										return 0
					elif k == 1:
										return 1
					else:
										return fibonacci(k - 1		) + fibonacci(k - 2		)




def       snake_case__	(       lowerCAmelCase   :		list				,					lowerCAmelCase   :		int		):
					__snake_case          =	len(lowerCAmelCase		)
					# Find m such that F_m >= n where F_i is the i_th fibonacci number.
					__snake_case          =	0
					while True:
										if fibonacci(lowerCAmelCase		) >= len_list:
															__snake_case          =	i
															break
										i += 1
					__snake_case          =	0
					while fibb_k > 0:
										__snake_case          =	min(
										    offset + fibonacci(fibb_k - 1		)				,					len_list - 1		)  # Prevent out of range
										__snake_case          =	arr[index_k]
										if item_k_1 == val:
															return index_k
										elif val < item_k_1:
															fibb_k -= 1
										elif val > item_k_1:
															offset += fibonacci(fibb_k - 1		)
															fibb_k -= 2
					else:
										return -1


if __name__ == "__main__":
		import doctest

		doctest.testmod()


