
"""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 							lowerCamelCase       :


							"""simple docstring"""






							def __init__(						self :     str		, lowerCamelCase_ :     Tuple		, lowerCamelCase_ :     str=13		, lowerCamelCase_ :     Optional[int]=7		, lowerCamelCase_ :     Union[str, Any]=True		, lowerCamelCase_ :     Any=True		, lowerCamelCase_ :     int=True		, lowerCamelCase_ :     List[str]=99		, lowerCamelCase_ :     Union[str, Any]=32		, lowerCamelCase_ :     Dict=5		, lowerCamelCase_ :     Union[str, Any]=4		, lowerCamelCase_ :     str=37		, lowerCamelCase_ :     Union[str, Any]="gelu"		, lowerCamelCase_ :     Any=0.1		, lowerCamelCase_ :     Union[str, Any]=0.1		, lowerCamelCase_ :     Optional[int]=512		, lowerCamelCase_ :     Dict=16		, lowerCamelCase_ :     Optional[int]=2		, lowerCamelCase_ :     int=0.02		, lowerCamelCase_ :     Optional[int]=3		, lowerCamelCase_ :     Tuple=4		, lowerCamelCase_ :     Tuple=None		, ):
													UpperCAmelCase__		=    parent
													UpperCAmelCase__		=    batch_size
													UpperCAmelCase__		=    seq_length
													UpperCAmelCase__		=    is_training
													UpperCAmelCase__		=    use_input_mask
													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 					lowerCAmelCase     (						self :     int				):
													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
													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, input_mask, sequence_labels, token_labels, choice_labels






							def 					lowerCAmelCase     (						self :     Optional[int]				):
													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__		=    self.get_config()

													return config, input_ids, input_mask






							def 					lowerCAmelCase     (						self :     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 					lowerCAmelCase     (						self :     int		, lowerCamelCase_ :     str		, lowerCamelCase_ :     List[str]		, lowerCamelCase_ :     str		, lowerCamelCase_ :     Dict		, lowerCamelCase_ :     List[Any]		, lowerCamelCase_ :     Optional[int]				):
													UpperCAmelCase__		=    ErnieMModel(config=lowerCamelCase_				)
													model.to(lowerCamelCase_				)
													model.eval()
													UpperCAmelCase__		=    model(lowerCamelCase_		, return_dict=lowerCamelCase_				)
													self.parent.assertEqual(result.last_hidden_state.shape		, (self.batch_size, self.seq_length, self.hidden_size)				)






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






							def 					lowerCAmelCase     (						self :     str		, lowerCamelCase_ :     Any		, lowerCamelCase_ :     Optional[int]		, lowerCamelCase_ :     int		, lowerCamelCase_ :     Dict		, lowerCamelCase_ :     Optional[int]		, lowerCamelCase_ :     int				):
													UpperCAmelCase__		=    self.num_labels
													UpperCAmelCase__		=    ErnieMForTokenClassification(config=lowerCamelCase_				)
													model.to(lowerCamelCase_				)
													model.eval()
													input_ids.to(lowerCamelCase_				)
													input_mask.to(lowerCamelCase_				)
													token_labels.to(lowerCamelCase_				)

													UpperCAmelCase__		=    model(lowerCamelCase_		, attention_mask=lowerCamelCase_		, labels=lowerCamelCase_				)

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






							def 					lowerCAmelCase     (						self :     Dict		, lowerCamelCase_ :     str		, lowerCamelCase_ :     Any		, lowerCamelCase_ :     Tuple		, lowerCamelCase_ :     Tuple		, lowerCamelCase_ :     int		, lowerCamelCase_ :     Tuple				):
													UpperCAmelCase__		=    self.num_choices
													UpperCAmelCase__		=    ErnieMForMultipleChoice(config=lowerCamelCase_				)
													model.to(lowerCamelCase_				)
													model.eval()
													UpperCAmelCase__		=    input_ids.unsqueeze(1				).expand(-1		, self.num_choices		, -1				).contiguous()
													UpperCAmelCase__		=    input_mask.unsqueeze(1				).expand(-1		, self.num_choices		, -1				).contiguous()
													UpperCAmelCase__		=    model(
													    lowerCamelCase_		, attention_mask=lowerCamelCase_		, labels=lowerCamelCase_		, )
													self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.num_choices)				)






							def 					lowerCAmelCase     (						self :     List[str]				):
													UpperCAmelCase__		=    self.prepare_config_and_inputs()
													(
													    (
													    UpperCAmelCase__				
													)	,				(
													    UpperCAmelCase__				
													)	,				(
													    UpperCAmelCase__				
													)	,				(
													    UpperCAmelCase__				
													)	,				(
													    UpperCAmelCase__				
													)	,				(
													    UpperCAmelCase__				
													)	,								
													)		=    config_and_inputs
													UpperCAmelCase__		=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
													return config, inputs_dict




@require_torch
class 							lowerCamelCase       (			__SCREAMING_SNAKE_CASE				,					__SCREAMING_SNAKE_CASE				,					unittest.TestCase ):


							"""simple docstring"""
							SCREAMING_SNAKE_CASE										=      (
							    (
							        ErnieMModel,
							        ErnieMForMultipleChoice,
							        ErnieMForQuestionAnswering,
							        ErnieMForSequenceClassification,
							        ErnieMForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							SCREAMING_SNAKE_CASE										=      ()
							SCREAMING_SNAKE_CASE										=      (
							    {
							        """feature-extraction""": ErnieMModel,
							        """question-answering""": ErnieMForQuestionAnswering,
							        """text-classification""": ErnieMForSequenceClassification,
							        """token-classification""": ErnieMForTokenClassification,
							        """zero-shot""": ErnieMForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							SCREAMING_SNAKE_CASE										=      False






							def 					lowerCAmelCase     (						self :     List[Any]		, lowerCamelCase_ :     List[Any]		, lowerCamelCase_ :     Dict		, lowerCamelCase_ :     int		, lowerCamelCase_ :     Any		, lowerCamelCase_ :     Any				):
													if pipeline_test_casse_name == "QAPipelineTests":
																			return True

													return False






							def 					lowerCAmelCase     (						self :     str				):
													UpperCAmelCase__		=    ErnieMModelTester(self				)
													UpperCAmelCase__		=    ConfigTester(self		, config_class=lowerCamelCase_		, hidden_size=37				)






							def 					lowerCAmelCase     (						self :     Tuple				):
													self.config_tester.run_common_tests()






							def 					lowerCAmelCase     (						self :     Tuple				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Union[str, Any]				):
													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(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     int				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     int				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Optional[int]				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Tuple				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_information_extraction(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Any				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_				)






							@slow
							def 					lowerCAmelCase     (						self :     Optional[Any]				):
													for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			UpperCAmelCase__		=    ErnieMModel.from_pretrained(lowerCamelCase_				)
																			self.assertIsNotNone(lowerCamelCase_				)




@require_torch
class 							lowerCamelCase       (			unittest.TestCase ):


							"""simple docstring"""






							@slow
							def 					lowerCAmelCase     (						self :     int				):
													UpperCAmelCase__		=    ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''				)
													model.eval()
													UpperCAmelCase__		=    torch.tensor([[0, 1, 2, 3, 4, 5]]				)
													UpperCAmelCase__		=    model(lowerCamelCase_				)[0]

													# TODO Replace vocab size
													UpperCAmelCase__		=    768

													UpperCAmelCase__		=    torch.Size((1, 6, hidden_size)				)
													self.assertEqual(output.shape		, lowerCamelCase_				)

													UpperCAmelCase__		=    torch.tensor(
													    [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]]				)

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



"""simple docstring"""







from __future__ import annotations

from collections.abc import Callable
from typing import Generic, TypeVar

__a			 =   TypeVar("T")
__a			 =   TypeVar("U")
class 							lowerCamelCase       (			Generic[T, U] ):


							"""simple docstring"""






							def __init__(						self :     str		, lowerCamelCase_ :     T | None		, lowerCamelCase_ :     U | None				):
													UpperCAmelCase__		=    key
													UpperCAmelCase__		=    val
													UpperCAmelCase__		=    0
													UpperCAmelCase__		=    None
													UpperCAmelCase__		=    None






							def __repr__(						self :     Tuple				):
													return "Node: key: {}, val: {}, freq: {}, has next: {}, has prev: {}".format(
													    self.key		, self.val		, self.freq		, self.next is not None		, self.prev is not None				)




class 							lowerCamelCase       (			Generic[T, U] ):


							"""simple docstring"""






							def __init__(						self :     List[str]				):
													UpperCAmelCase__		=    DoubleLinkedListNode(lowerCamelCase_		, lowerCamelCase_				)
													UpperCAmelCase__		=    DoubleLinkedListNode(lowerCamelCase_		, lowerCamelCase_				)
													UpperCAmelCase__	,				UpperCAmelCase__		=    self.rear, self.head






							def __repr__(						self :     int				):
													UpperCAmelCase__		=    ['''DoubleLinkedList''']
													UpperCAmelCase__		=    self.head
													while node.next is not None:
																			rep.append(str(lowerCamelCase_				)				)
																			UpperCAmelCase__		=    node.next
													rep.append(str(self.rear				)				)
													return ",\n    ".join(lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     List[Any]		, lowerCamelCase_ :     DoubleLinkedListNode[T, U]				):
													UpperCAmelCase__		=    self.rear.prev

													# All nodes other than self.head are guaranteed to have non-None previous
													assert previous is not None

													UpperCAmelCase__		=    node
													UpperCAmelCase__		=    previous
													UpperCAmelCase__		=    node
													UpperCAmelCase__		=    self.rear
													node.freq += 1
													self._position_node(lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Optional[Any]		, lowerCamelCase_ :     DoubleLinkedListNode[T, U]				):

													while node.prev is not None and node.prev.freq > node.freq:
																			# swap node with previous node
																			UpperCAmelCase__		=    node.prev

																			UpperCAmelCase__		=    previous_node.prev
																			UpperCAmelCase__		=    node.prev
																			UpperCAmelCase__		=    previous_node
																			UpperCAmelCase__		=    node






							def 					lowerCAmelCase     (						self :     Union[str, Any]		, lowerCamelCase_ :     DoubleLinkedListNode[T, U]				):

													if node.prev is None or node.next is None:
																			return None

													UpperCAmelCase__		=    node.next
													UpperCAmelCase__		=    node.prev
													UpperCAmelCase__		=    None
													UpperCAmelCase__		=    None
													return node




class 							lowerCamelCase       (			Generic[T, U] ):


							"""simple docstring"""
							SCREAMING_SNAKE_CASE										=      {}






							def __init__(						self :     str		, lowerCamelCase_ :     int				):
													UpperCAmelCase__		=    DoubleLinkedList()
													UpperCAmelCase__		=    capacity
													UpperCAmelCase__		=    0
													UpperCAmelCase__		=    0
													UpperCAmelCase__		=    0
													UpperCAmelCase__		=    {}






							def __repr__(						self :     Tuple				):
													return (
													    f"""CacheInfo(hits={self.hits}, misses={self.miss}, """
													    f"""capacity={self.capacity}, current_size={self.num_keys})"""
													)






							def __contains__(						self :     Optional[Any]		, lowerCamelCase_ :     T				):
													return key in self.cache






							def 					lowerCAmelCase     (						self :     Any		, lowerCamelCase_ :     T				):

													if key in self.cache:
																			self.hits += 1
																			UpperCAmelCase__		=    self.cache[key]
																			UpperCAmelCase__		=    self.list.remove(self.cache[key]				)
																			assert node == value_node

																			# node is guaranteed not None because it is in self.cache
																			assert node is not None
																			self.list.add(lowerCamelCase_				)
																			return node.val
													self.miss += 1
													return None






							def 					lowerCAmelCase     (						self :     Tuple		, lowerCamelCase_ :     T		, lowerCamelCase_ :     U				):

													if key not in self.cache:
																			if self.num_keys >= self.capacity:
																									# delete first node when over capacity
																									UpperCAmelCase__		=    self.list.head.next

																									# guaranteed to have a non-None first node when num_keys > 0
																									# explain to type checker via assertions
																									assert first_node is not None
																									assert first_node.key is not None
																									assert self.list.remove(lowerCamelCase_				) is not None
																									# first_node guaranteed to be in list

																									del self.cache[first_node.key]
																									self.num_keys -= 1
																			UpperCAmelCase__		=    DoubleLinkedListNode(lowerCamelCase_		, lowerCamelCase_				)
																			self.list.add(self.cache[key]				)
																			self.num_keys += 1

													else:
																			UpperCAmelCase__		=    self.list.remove(self.cache[key]				)
																			assert node is not None  # node guaranteed to be in list
																			UpperCAmelCase__		=    value
																			self.list.add(lowerCamelCase_				)






							@classmethod
							def 					lowerCAmelCase     (						cls :     type[LFUCache[T, U]]		, lowerCamelCase_ :     int = 128				):

													def cache_decorator_inner(lowerCamelCase_ :     Callable[[T], U]				) -> Callable[..., U]:
																			def cache_decorator_wrapper(*lowerCamelCase_ :     T				) -> U:
																									if func not in cls.decorator_function_to_instance_map:
																															UpperCAmelCase__		=    LFUCache(lowerCamelCase_				)

																									UpperCAmelCase__		=    cls.decorator_function_to_instance_map[func].get(args[0]				)
																									if result is None:
																															UpperCAmelCase__		=    func(*lowerCamelCase_				)
																															cls.decorator_function_to_instance_map[func].put(args[0]		, lowerCamelCase_				)
																									return result

																			def cache_info() -> LFUCache[T, U]:
																									return cls.decorator_function_to_instance_map[func]

																			setattr(lowerCamelCase_		, '''cache_info'''		, lowerCamelCase_				)  # noqa: B010

																			return cache_decorator_wrapper

													return cache_decorator_inner


if __name__ == "__main__":
						import doctest

						doctest.testmod()