





"""simple docstring"""




from __future__ import annotations

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

UpperCamelCase_									=TypeVar("""T""")
UpperCamelCase_									=TypeVar("""U""")




class 			_a						(				Generic[T, U]    ):






				def __init__(			self		:   Tuple,			lowerCAmelCase__		:   List[Any],			lowerCAmelCase__		:   List[Any]							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : List[Any]         =   key
										_UpperCamelCase      : Union[str, Any]         =   val
										_UpperCamelCase      : DoubleLinkedListNode[T, U] | None         =   None
										_UpperCamelCase      : DoubleLinkedListNode[T, U] | None         =   None






				def __repr__(			self		:   List[str]							)     ->				Tuple:




										'''simple docstring'''



										return (
										    f"""Node: key: {self.key}, val: {self.val}, """
										    f"""has next: {bool(self.next							)}, has prev: {bool(self.prev							)}"""
										)




class 			_a						(				Generic[T, U]    ):






				def __init__(			self		:   Tuple							)     ->				Tuple:




										'''simple docstring'''



										_UpperCamelCase      : DoubleLinkedListNode[T, U]         =   DoubleLinkedListNode(lowercase_,			lowercase_							)
										_UpperCamelCase      : DoubleLinkedListNode[T, U]         =   DoubleLinkedListNode(lowercase_,			lowercase_							)
										_UpperCamelCase      : Optional[int]         =   self.rear, self.head






				def __repr__(			self		:   str							)     ->				int:




										'''simple docstring'''



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






				def     a     (			self		:   Union[str, Any],			lowerCAmelCase__		:   str							)     ->				Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.rear.prev

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

										_UpperCamelCase      : str         =   node
										_UpperCamelCase      : Tuple         =   previous
										_UpperCamelCase      : List[str]         =   node
										_UpperCamelCase      : Tuple         =   self.rear






				def     a     (			self		:   int,			lowerCAmelCase__		:   Tuple							)     ->				str:




										'''simple docstring'''



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

										_UpperCamelCase      : Optional[Any]         =   node.next
										_UpperCamelCase      : Dict         =   node.prev
										_UpperCamelCase      : Any         =   None
										_UpperCamelCase      : str         =   None
										return node




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






				def __init__(			self		:   Any,			lowerCAmelCase__		:   List[str]							)     ->				Dict:




										'''simple docstring'''



										_UpperCamelCase      : DoubleLinkedList[T, U]         =   DoubleLinkedList()
										_UpperCamelCase      : List[str]         =   capacity
										_UpperCamelCase      : List[str]         =   0
										_UpperCamelCase      : int         =   0
										_UpperCamelCase      : str         =   0
										_UpperCamelCase      : dict[T, DoubleLinkedListNode[T, U]]         =   {}






				def __repr__(			self		:   List[Any]							)     ->				int:




										'''simple docstring'''



										return (
										    f"""CacheInfo(hits={self.hits}, misses={self.miss}, """
										    f"""capacity={self.capacity}, current size={self.num_keys})"""
										)






				def __contains__(			self		:   Any,			lowerCAmelCase__		:   Dict							)     ->				int:




										'''simple docstring'''



										return key in self.cache






				def     a     (			self		:   str,			lowerCAmelCase__		:   Dict							)     ->				Tuple:




										'''simple docstring'''



										if key in self.cache:
																self.hits += 1
																_UpperCamelCase      : DoubleLinkedListNode[T, U]         =   self.cache[key]
																_UpperCamelCase      : Tuple         =   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     a     (			self		:   List[Any],			lowerCAmelCase__		:   Union[str, Any],			lowerCAmelCase__		:   Optional[Any]							)     ->				Optional[int]:




										'''simple docstring'''



										if key not in self.cache:
																if self.num_keys >= self.capacity:
																						# delete first node (oldest) when over capacity
																						_UpperCamelCase      : List[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
																_UpperCamelCase      : Optional[int]         =   DoubleLinkedListNode(lowercase_,			lowercase_							)
																self.list.add(self.cache[key]							)
																self.num_keys += 1

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






				@classmethod
				def     a     (			cls		:   int,			lowerCAmelCase__		:   List[str] = 1_2_8							)     ->				int:




										'''simple docstring'''



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

																						_UpperCamelCase      : Dict         =   cls.decorator_function_to_instance_map[func].get(args[0]							)
																						if result is None:
																												_UpperCamelCase      : Optional[int]         =   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()










"""simple docstring"""




import numpy as np
import torch
from imwatermark import WatermarkEncoder


# Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66
UpperCamelCase_									=0b1_0_1_1_0_0_1_1_1_1_1_0_1_1_0_0_1_0_0_1_0_0_0_0_0_1_1_1_1_0_1_1_1_0_1_1_0_0_0_1_1_0_0_1_1_1_1_0
# bin(x)[2:] gives bits of x as str, use int to convert them to 0/1
UpperCamelCase_									=[int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]]




class 			_a						:






				def __init__(			self		:   str							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : str         =   WATERMARK_BITS
										_UpperCamelCase      : Optional[int]         =   WatermarkEncoder()

										self.encoder.set_watermark('''bits''',			self.watermark							)






				def     snake_case     (			self		:   Dict,			lowerCAmelCase__		:   torch.FloatTensor							)     ->				int:




										'''simple docstring'''



										if images.shape[-1] < 2_5_6:
																return images

										_UpperCamelCase      : Union[str, Any]         =   (2_5_5 * (images / 2 + 0.5)).cpu().permute(0,			2,			3,			1							).float().numpy()

										_UpperCamelCase      : List[str]         =   [self.encoder.encode(lowerCAmelCase__,			'''dwtDct'''							) for image in images]

										_UpperCamelCase      : Dict         =   torch.from_numpy(np.array(lowerCAmelCase__							)							).permute(0,			3,			1,			2							)

										_UpperCamelCase      : Optional[int]         =   torch.clamp(2 * (images / 2_5_5 - 0.5),			min=-1.0,			max=1.0							)
										return images



