
'''simple docstring'''
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    UpperCAmelCase_				(	unittest.TestCase					):





 def        snake_case__		(    self):
     snake_case_ :    Optional[int]							=						torch.tensor([-1_00, -1, -0.1, 0, 0.1, 1.0, 1_00])
     snake_case_ :    Any							=						get_activation("gelu")
     self.assertTrue(torch.allclose(gelu_python(lowercase_) ,       torch_builtin(lowercase_)))
     self.assertFalse(torch.allclose(gelu_python(lowercase_) ,       gelu_new(lowercase_)))





 def        snake_case__		(    self):
     snake_case_ :    Tuple							=						torch.tensor([-1_00, -1, -0.1, 0, 0.1, 1.0, 1_00])
     snake_case_ :    List[Any]							=						get_activation("gelu")
     snake_case_ :    List[Any]							=						get_activation("gelu_10")

     snake_case_ :    str							=						torch_builtin(lowercase_)
     snake_case_ :    Optional[int]							=						geluaa(lowercase_)

     snake_case_ :    List[Any]							=						torch.where(y_gelu_aa < 10.0 ,       1 ,       0)

     self.assertTrue(torch.max(lowercase_).item() == 10.0)
     self.assertTrue(torch.allclose(y_gelu * clipped_mask ,       y_gelu_aa * clipped_mask))





 def        snake_case__		(    self):
     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(lowercase_):
         get_activation("bogus")
     with self.assertRaises(lowercase_):
         get_activation(lowercase_)





 def        snake_case__		(    self):
     snake_case_ :    Tuple							=						get_activation("gelu")
     snake_case_ :    Dict							=						1
     snake_case_ :    Dict							=						get_activation("gelu")
     self.assertEqual(acta.a ,       1)
     with self.assertRaises(lowercase_):
         snake_case_ :    int							=						acta.a






'''simple docstring'''
from __future__ import annotations

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

a_											=						TypeVar("T")
a_											=						TypeVar("U")






class    UpperCAmelCase_				(	Generic[T, U]					):





 def __init__(    self ,       lowercase_ ,       lowercase_):
     snake_case_ :    Any     			=						key
     snake_case_ :    List[Any]     			=						val
     snake_case_ :    DoubleLinkedListNode[T, U] | None     			=						None
     snake_case_ :    DoubleLinkedListNode[T, U] | None     			=						None





 def __repr__(    self):
     return (
         F'Node: key: {self.key}, val: {self.val}, '
         F'has next: {bool(self.next)}, has prev: {bool(self.prev)}'
     )






class    UpperCAmelCase_				(	Generic[T, U]					):





 def __init__(    self):
     snake_case_ :    DoubleLinkedListNode[T, U]     			=						DoubleLinkedListNode(lowercase_ ,       lowercase_)
     snake_case_ :    DoubleLinkedListNode[T, U]     			=						DoubleLinkedListNode(lowercase_ ,       lowercase_)
     snake_case_			, snake_case_ :    Union[str, Any]     			=						self.rear, self.head





 def __repr__(    self):
     snake_case_ :    Dict     			=						["DoubleLinkedList"]
     snake_case_ :    Dict     			=						self.head
     while node.next is not None:
         rep.append(str(lowercase_))
         snake_case_ :    List[str]     			=						node.next
     rep.append(str(self.rear))
     return ",\n    ".join(lowercase_)





 def        snake_case__		(    self ,       lowercase_):
     snake_case_ :    List[str]     			=						self.rear.prev

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

     snake_case_ :    Tuple     			=						node
     snake_case_ :    str     			=						previous
     snake_case_ :    Optional[Any]     			=						node
     snake_case_ :    Any     			=						self.rear





 def        snake_case__		(    self ,       lowercase_):

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

     snake_case_ :    Union[str, Any]     			=						node.next
     snake_case_ :    Optional[int]     			=						node.prev
     snake_case_ :    str     			=						None
     snake_case_ :    int     			=						None
     return node






class    UpperCAmelCase_				(	Generic[T, U]					):
 UpperCAmelCase_		    =						{}





 def __init__(    self ,       lowercase_):
     snake_case_ :    DoubleLinkedList[T, U]     			=						DoubleLinkedList()
     snake_case_ :    List[str]     			=						capacity
     snake_case_ :    Any     			=						0
     snake_case_ :    Dict     			=						0
     snake_case_ :    Union[str, Any]     			=						0
     snake_case_ :    dict[T, DoubleLinkedListNode[T, U]]     			=						{}





 def __repr__(    self):
     return (
         F'CacheInfo(hits={self.hits}, misses={self.miss}, '
         F'capacity={self.capacity}, current size={self.num_keys})'
     )





 def __contains__(    self ,       lowercase_):
     return key in self.cache





 def        snake_case__		(    self ,       lowercase_):
     # Note: pythonic interface would throw KeyError rather than return None

     if key in self.cache:
         self.hits += 1
         snake_case_ :    DoubleLinkedListNode[T, U]     			=						self.cache[key]
         snake_case_ :    Optional[Any]     			=						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(lowercase_)
         return node.val
     self.miss += 1
     return None





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_):

     if key not in self.cache:
         if self.num_keys >= self.capacity:
             # delete first node (oldest) when over capacity
             snake_case_ :    Optional[Any]     			=						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(lowercase_) is not None
             )  # node guaranteed to be in list assert node.key is not None

             del self.cache[first_node.key]
             self.num_keys -= 1
         snake_case_ :    Dict     			=						DoubleLinkedListNode(lowercase_ ,       lowercase_)
         self.list.add(self.cache[key])
         self.num_keys += 1

     else:
         # bump node to the end of the list, update value
         snake_case_ :    List[Any]     			=						self.list.remove(self.cache[key])
         assert node is not None  # node guaranteed to be in list
         snake_case_ :    Optional[Any]     			=						value
         self.list.add(lowercase_)





 @classmethod
 def        snake_case__		(    cls ,       lowercase_ = 1_28):

     def cache_decorator_inner(lowercase_) -> Callable[..., U]:
         def cache_decorator_wrapper(*lowercase_) -> U:
             if func not in cls.decorator_function_to_instance_map:
                 snake_case_ :    List[str]     			=						LRUCache(lowercase_)

             snake_case_ :    Optional[Any]     			=						cls.decorator_function_to_instance_map[func].get(args[0])
             if result is None:
                 snake_case_ :    Any     			=						func(*lowercase_)
                 cls.decorator_function_to_instance_map[func].put(args[0] ,       lowercase_)
             return result

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

         setattr(lowercase_ ,       "cache_info" ,       lowercase_)  # noqa: B010

         return cache_decorator_wrapper

     return cache_decorator_inner


if __name__ == "__main__":
  import doctest

  doctest.testmod()




