





'''simple docstring'''






import unittest

import numpy as np

from transformers import AlbertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
      import jax.numpy as jnp

      from transformers.models.albert.modeling_flax_albert import (
          FlaxAlbertForMaskedLM,
          FlaxAlbertForMultipleChoice,
          FlaxAlbertForPreTraining,
          FlaxAlbertForQuestionAnswering,
          FlaxAlbertForSequenceClassification,
          FlaxAlbertForTokenClassification,
          FlaxAlbertModel,
      )







class 		UpperCAmelCase   (				unittest.TestCase):


  """simple docstring"""







  def __init__(				self    :					int							,	UpperCamelCase__    :					str							,	UpperCamelCase__    :					Any=13							,	UpperCamelCase__    :					Optional[int]=7							,	UpperCamelCase__    :					Optional[int]=True							,	UpperCamelCase__    :					int=True							,	UpperCamelCase__    :					Dict=True							,	UpperCamelCase__    :					int=True							,	UpperCamelCase__    :					Union[str, Any]=99							,	UpperCamelCase__    :					str=32							,	UpperCamelCase__    :					str=5							,	UpperCamelCase__    :					List[Any]=4							,	UpperCamelCase__    :					List[Any]=37							,	UpperCamelCase__    :					int="gelu"							,	UpperCamelCase__    :					Optional[int]=0.1							,	UpperCamelCase__    :					Tuple=0.1							,	UpperCamelCase__    :					Dict=512							,	UpperCamelCase__    :					Any=16							,	UpperCamelCase__    :					int=2							,	UpperCamelCase__    :					Optional[Any]=0.02							,	UpperCamelCase__    :					str=4							,	)     ->						Tuple:
    _UpperCamelCase       			=parent
    _UpperCamelCase       			=batch_size
    _UpperCamelCase       			=seq_length
    _UpperCamelCase       			=is_training
    _UpperCamelCase       			=use_attention_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_choices


  def   UpperCamelCase__      (				self    :					int  )     ->						Optional[int]:
    _UpperCamelCase       			=ids_tensor([self.batch_size, self.seq_length]							,	self.vocab_size  )

    _UpperCamelCase       			=None
    if self.use_attention_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       			=AlbertConfig(
        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=UpperCamelCase__							,	initializer_range=self.initializer_range							,	)

    return config, input_ids, token_type_ids, attention_mask




  def   UpperCamelCase__      (				self    :					int  )     ->						List[str]:
    _UpperCamelCase       			=self.prepare_config_and_inputs()
    _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase       			=config_and_inputs
    _UpperCamelCase       			={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
    return config, inputs_dict







@require_flax
class 		UpperCAmelCase   (				lowercase_   ,     unittest.TestCase):


  """simple docstring"""







  lowerCAmelCase_					=				(
      (
          FlaxAlbertModel,
          FlaxAlbertForPreTraining,
          FlaxAlbertForMaskedLM,
          FlaxAlbertForMultipleChoice,
          FlaxAlbertForQuestionAnswering,
          FlaxAlbertForSequenceClassification,
          FlaxAlbertForTokenClassification,
          FlaxAlbertForQuestionAnswering,
      )
      if is_flax_available()
      else ()
  )


  def   UpperCamelCase__      (				self    :					Tuple  )     ->						Optional[int]:
    _UpperCamelCase       			=FlaxAlbertModelTester(self  )




  @slow
  def   UpperCamelCase__      (				self    :					List[str]  )     ->						Union[str, Any]:
    for model_class_name in self.all_model_classes:
      _UpperCamelCase       			=model_class_name.from_pretrained('''albert-base-v2'''  )
      _UpperCamelCase       			=model(np.ones((1, 1)  )  )
      self.assertIsNotNone(UpperCamelCase__  )







@require_flax
class 		UpperCAmelCase   (				unittest.TestCase):


  """simple docstring"""







  @slow
  def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						str:
    _UpperCamelCase       			=FlaxAlbertModel.from_pretrained('''albert-base-v2'''  )
    _UpperCamelCase       			=np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]  )
    _UpperCamelCase       			=np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
    _UpperCamelCase       			=model(UpperCamelCase__							,	attention_mask=UpperCamelCase__  )[0]
    _UpperCamelCase       			=(1, 11, 768)
    self.assertEqual(output.shape							,	UpperCamelCase__  )
    _UpperCamelCase       			=np.array(
        [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]  )

    self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]							,	UpperCamelCase__							,	atol=1E-4  )  )













'''simple docstring'''






from __future__ import annotations

import sys
from collections import deque
from typing import Generic, TypeVar

__lowerCamelCase						: Optional[int]		=   TypeVar('T')







class 		UpperCAmelCase   (				Generic[T]):


  """simple docstring"""







  lowerCAmelCase_					=				42  # Cache store of keys
  lowerCAmelCase_					=				42  # References of the keys in cache
  lowerCAmelCase_					=				10  # Maximum capacity of cache


  def __init__(				self    :					Union[str, Any]							,	UpperCamelCase__    :					int  )     ->						None:
    _UpperCamelCase       			=deque()
    _UpperCamelCase       			=set()
    if not n:
      _UpperCamelCase       			=sys.maxsize
    elif n < 0:
      raise ValueError('''n should be an integer greater than 0.'''  )
    else:
      _UpperCamelCase       			=n


  def   UpperCamelCase__      (				self    :					List[str]							,	UpperCamelCase__    :					T  )     ->						None:
    if x not in self.key_reference:
      if len(self.dq_store  ) == LRUCache._MAX_CAPACITY:
        _UpperCamelCase       			=self.dq_store.pop()
        self.key_reference.remove(UpperCamelCase__  )
    else:
      self.dq_store.remove(UpperCamelCase__  )

    self.dq_store.appendleft(UpperCamelCase__  )
    self.key_reference.add(UpperCamelCase__  )


  def   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						None:
    for k in self.dq_store:
      print(UpperCamelCase__  )




  def __repr__(				self    :					str  )     ->						str:
    return F'''LRUCache({self._MAX_CAPACITY}) => {list(self.dq_store  )}'''


if __name__ == "__main__":
      import doctest

      doctest.testmod()

      __lowerCamelCase						: LRUCache[str | int]		=   LRUCache(4)
      lru_cache.refer('A')
      lru_cache.refer(2)
      lru_cache.refer(3)
      lru_cache.refer('A')
      lru_cache.refer(4)
      lru_cache.refer(5)
      lru_cache.display()

      print(lru_cache)
      assert str(lru_cache) == "LRUCache(4) => [5, 4, 'A', 3]"






