

from __future__ import annotations

from random import random



class    _a						:


     '''simple docstring'''






     def __init__(			self   ,	A__ = None      ):
      A__							:   Optional[int]					       =		value
      A__							:   Any					       =		random()
      A__							:   Node | None					       =		None
      A__							:   Node | None					       =		None






     def __repr__(			self      ):
      from pprint import pformat

      if self.left is None and self.right is None:
       return F"""'{self.value}: {self.prior:.5}'"""
      else:
       return pformat(
           {F"""{self.value}: {self.prior:.5}""": (self.left, self.right)}   ,	indent=1      )






     def __str__(			self      ):
      A__							:   str					       =		str(self.value      ) + """ """
      A__							:   Tuple					       =		str(self.left or """"""      )
      A__							:   int					       =		str(self.right or """"""      )
      return value + left + right



def 						UpperCamelCase (lowercase_:			Node | None   ,				lowercase_:			int )					->				tuple[Node | None, Node | None]:
 if root is None:  # None tree is split into 2 Nones
  return None, None
 elif root.value is None:
  return None, None
 else:
  if value < root.value:
   A__							:   List[str]					       =		split(root.left   ,				lowercase_ )
   return left, root
  else:
   A__							:   Tuple					       =		split(root.right   ,				lowercase_ )
   return root, right

def 						UpperCamelCase (lowercase_:			Node | None   ,				lowercase_:			Node | None )					->				Node | None:
 if (not left) or (not right):  # If one node is None, return the other
  return left or right
 elif left.prior < right.prior:
  A__							:   int					       =		merge(left.right   ,				lowercase_ )
  return left
 else:
  A__							:   Optional[int]					       =		merge(lowercase_   ,				right.left )
  return right

def 						UpperCamelCase (lowercase_:			Node | None   ,				lowercase_:			int )					->				Node | None:
 A__							:   Any					       =		Node(lowercase_ )
 A__							:   Dict					       =		split(lowercase_   ,				lowercase_ )
 return merge(merge(lowercase_   ,				lowercase_ )   ,				lowercase_ )

def 						UpperCamelCase (lowercase_:			Node | None   ,				lowercase_:			int )					->				Node | None:
 A__							:   List[Any]					       =		split(lowercase_   ,				value - 1 )
 A__							:   int					       =		split(lowercase_   ,				lowercase_ )
 return merge(lowercase_   ,				lowercase_ )

def 						UpperCamelCase (lowercase_:			Node | None )					->				None:
 if not root:  # None
  return
 else:
  inorder(root.left )
  print(root.value   ,				end=""",""" )
  inorder(root.right )

def 						UpperCamelCase (lowercase_:			Node | None   ,				lowercase_:			str )					->				Node | None:
 for arg in args.split():
  if arg[0] == "+":
   A__							:   Any					       =		insert(lowercase_   ,				int(arg[1:] ) )

  elif arg[0] == "-":
   A__							:   Union[str, Any]					       =		erase(lowercase_   ,				int(arg[1:] ) )

  else:
   print("""Unknown command""" )

 return root

def 						UpperCamelCase ()					->				None:
 A__							:   Optional[int]					       =		None
 print(
     """enter numbers to create a tree, + value to add value into treap, """
     """- value to erase all nodes with value. 'q' to quit. """ )

 A__							:   int					       =		input()
 while args != "q":
  A__							:   Tuple					       =		interact_treap(lowercase_   ,				lowercase_ )
  print(lowercase_ )
  A__							:   List[str]					       =		input()

 print("""good by!""" )


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 main()






import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
	    FalconForCausalLM,
	    FalconForQuestionAnswering,
	    FalconForSequenceClassification,
	    FalconForTokenClassification,
	    FalconModel,
	)



class    _a						:


					'''simple docstring'''






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






					def   __A      (			self      ):
						A__							:   Any					       =		ids_tensor([self.batch_size, self.seq_length]   ,	self.vocab_size      )

						A__							:   Any					       =		None
						if self.use_input_mask:
							A__							:   int					       =		random_attention_mask([self.batch_size, self.seq_length]      )

						A__							:   str					       =		None

						A__							:   Union[str, Any]					       =		None
						A__							:   List[str]					       =		None
						A__							:   Optional[Any]					       =		None
						if self.use_labels:
							A__							:   Any					       =		ids_tensor([self.batch_size]   ,	self.type_sequence_label_size      )
							A__							:   List[str]					       =		ids_tensor([self.batch_size, self.seq_length]   ,	self.num_labels      )
							A__							:   List[Any]					       =		ids_tensor([self.batch_size]   ,	self.num_choices      )

						A__							:   Dict					       =		self.get_config()

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






					def   __A      (			self      ):
						return FalconConfig(
						    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=A__   ,	initializer_range=self.initializer_range   ,	pad_token_id=1   ,	new_decoder_architecture=A__   ,	)






					def   __A      (			self   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__      ):
						A__							:   List[str]					       =		FalconModel(config=A__      )
						model.to(A__      )
						model.eval()
						A__							:   int					       =		model(A__   ,	attention_mask=A__      )
						A__							:   Union[str, Any]					       =		model(A__      )
						self.parent.assertEqual(result.last_hidden_state.shape   ,	(self.batch_size, self.seq_length, self.hidden_size)      )






					def   __A      (			self   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	):
						A__							:   Union[str, Any]					       =		True
						A__							:   Union[str, Any]					       =		FalconModel(A__      )
						model.to(A__      )
						model.eval()
						A__							:   Tuple					       =		model(
						    A__   ,	attention_mask=A__   ,	encoder_hidden_states=A__   ,	encoder_attention_mask=A__   ,	)
						A__							:   Union[str, Any]					       =		model(
						    A__   ,	attention_mask=A__   ,	encoder_hidden_states=A__   ,	)
						A__							:   List[str]					       =		model(A__   ,	attention_mask=A__      )
						self.parent.assertEqual(result.last_hidden_state.shape   ,	(self.batch_size, self.seq_length, self.hidden_size)      )






					def   __A      (			self   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	):
						A__							:   Any					       =		FalconForCausalLM(config=A__      )
						model.to(A__      )
						model.eval()
						A__							:   Tuple					       =		model(A__   ,	attention_mask=A__   ,	labels=A__      )
						self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.seq_length, self.vocab_size)      )






					def   __A      (			self   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	A__   ,	):
						A__							:   Optional[Any]					       =		True
						A__							:   Union[str, Any]					       =		True
						A__							:   int					       =		FalconForCausalLM(config=A__      )
						model.to(A__      )
						model.eval()

						# first forward pass
						A__							:   List[Any]					       =		model(
						    A__   ,	attention_mask=A__   ,	encoder_hidden_states=A__   ,	encoder_attention_mask=A__   ,	use_cache=A__   ,	)
						A__							:   Tuple					       =		outputs.past_key_values

						# create hypothetical multiple next token and extent to next_input_ids
						A__							:   Tuple					       =		ids_tensor((self.batch_size, 3)   ,	config.vocab_size      )
						A__							:   List[Any]					       =		ids_tensor((self.batch_size, 3)   ,	vocab_size=2      )

						# append to next input_ids and
						A__							:   Optional[int]					       =		torch.cat([input_ids, next_tokens]   ,	dim=-1      )
						A__							:   List[str]					       =		torch.cat([input_mask, next_mask]   ,	dim=-1      )

						A__							:   Optional[int]					       =		model(
						    A__   ,	attention_mask=A__   ,	encoder_hidden_states=A__   ,	encoder_attention_mask=A__   ,	output_hidden_states=A__   ,	)["""hidden_states"""][0]
						A__							:   Any					       =		model(
						    A__   ,	attention_mask=A__   ,	encoder_hidden_states=A__   ,	encoder_attention_mask=A__   ,	past_key_values=A__   ,	output_hidden_states=A__   ,	)["""hidden_states"""][0]

						# select random slice
						A__							:   List[Any]					       =		ids_tensor((1,)   ,	output_from_past.shape[-1]      ).item()
						A__							:   List[str]					       =		output_from_no_past[:, -3:, random_slice_idx].detach()
						A__							:   Optional[Any]					       =		output_from_past[:, :, random_slice_idx].detach()

						self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]      )

						# test that outputs are equal for slice
						self.parent.assertTrue(torch.allclose(A__   ,	A__   ,	atol=1e-3      )      )






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



@require_torch
class    _a						(__magic_name__		,       __magic_name__		,       __magic_name__		,       unittest.TestCase    ):


					'''simple docstring'''






					UpperCAmelCase__:       List[Any]  							=	(
					    (
					        FalconModel,
					        FalconForCausalLM,
					        FalconForSequenceClassification,
					        FalconForTokenClassification,
					        FalconForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					UpperCAmelCase__:       Tuple  							=	(FalconForCausalLM,) if is_torch_available() else ()
					UpperCAmelCase__:       Optional[int]  							=	(
					    {
					        '''feature-extraction''': FalconModel,
					        '''text-classification''': FalconForSequenceClassification,
					        '''text-generation''': FalconForCausalLM,
					        '''question-answering''': FalconForQuestionAnswering,
					        '''token-classification''': FalconForTokenClassification,
					        '''zero-shot''': FalconForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					UpperCAmelCase__:       str  							=	False
					UpperCAmelCase__:       int  							=	False






					def   __A      (			self      ):
						A__							:   List[Any]					       =		FalconModelTester(self      )
						A__							:   Union[str, Any]					       =		ConfigTester(self   ,	config_class=A__   ,	hidden_size=37      )






					def   __A      (			self      ):
						self.config_tester.run_common_tests()






					def   __A      (			self      ):
						A__							:   Tuple					       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*A__      )






					def   __A      (			self      ):
						A__       ,       *A__							:   List[Any]					       =		self.model_tester.prepare_config_and_inputs()
						for alibi in [True, False]:
							A__							:   Tuple					       =		alibi
							self.model_tester.create_and_check_model(A__   ,	*A__      )






					def   __A      (			self      ):
						A__       ,       A__							:   Tuple					       =		self.model_tester.prepare_config_and_inputs_for_common()
						A__							:   Optional[int]					       =		3
						A__							:   int					       =		input_dict["""input_ids"""]
						A__							:   int					       =		input_ids.ne(1      ).to(A__      )
						A__							:   Union[str, Any]					       =		ids_tensor([self.model_tester.batch_size]   ,	self.model_tester.type_sequence_label_size      )
						A__							:   Optional[int]					       =		FalconForSequenceClassification(A__      )
						model.to(A__      )
						model.eval()
						A__							:   int					       =		model(A__   ,	attention_mask=A__   ,	labels=A__      )
						self.assertEqual(result.logits.shape   ,	(self.model_tester.batch_size, self.model_tester.num_labels)      )






					def   __A      (			self      ):
						A__       ,       A__							:   str					       =		self.model_tester.prepare_config_and_inputs_for_common()
						A__							:   Dict					       =		3
						A__							:   Tuple					       =		"""single_label_classification"""
						A__							:   List[Any]					       =		input_dict["""input_ids"""]
						A__							:   Dict					       =		input_ids.ne(1      ).to(A__      )
						A__							:   Dict					       =		ids_tensor([self.model_tester.batch_size]   ,	self.model_tester.type_sequence_label_size      )
						A__							:   Any					       =		FalconForSequenceClassification(A__      )
						model.to(A__      )
						model.eval()
						A__							:   Any					       =		model(A__   ,	attention_mask=A__   ,	labels=A__      )
						self.assertEqual(result.logits.shape   ,	(self.model_tester.batch_size, self.model_tester.num_labels)      )






					def   __A      (			self      ):
						A__       ,       A__							:   Optional[Any]					       =		self.model_tester.prepare_config_and_inputs_for_common()
						A__							:   List[str]					       =		input_dict["""input_ids"""]
						A__							:   List[str]					       =		FalconForCausalLM(A__      )
						model.to(A__      )
						model.eval()
						A__							:   Any					       =		model(A__   ,	use_cache=A__      )
						A__							:   Any					       =		input_ids.shape[0]
						A__							:   Union[str, Any]					       =		model._convert_to_rw_cache(result.past_key_values      )
						A__							:   int					       =		model._convert_cache_to_standard_format(A__   ,	A__      )
						for layer in range(len(A__      )      ):
							for tensor_idx in range(2      ):
								self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3      )
								self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4      )
								self.assertTrue(
								    torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]      )      )






					def   __A      (			self      ):
						A__       ,       A__							:   Dict					       =		self.model_tester.prepare_config_and_inputs_for_common()
						A__							:   Optional[Any]					       =		3
						A__							:   List[Any]					       =		"""multi_label_classification"""
						A__							:   Tuple					       =		input_dict["""input_ids"""]
						A__							:   List[Any]					       =		input_ids.ne(1      ).to(A__      )
						A__							:   Optional[Any]					       =		ids_tensor(
						    [self.model_tester.batch_size, config.num_labels]   ,	self.model_tester.type_sequence_label_size      ).to(torch.float      )
						A__							:   Optional[int]					       =		FalconForSequenceClassification(A__      )
						model.to(A__      )
						model.eval()
						A__							:   List[Any]					       =		model(A__   ,	attention_mask=A__   ,	labels=A__      )
						self.assertEqual(result.logits.shape   ,	(self.model_tester.batch_size, self.model_tester.num_labels)      )






					def   __A      (			self      ):
						# Falcon can have different numbers of KV-heads than the number of query heads, so we need
						# to override this test to use the right head counts.
						for model_class in self.all_generative_model_classes:
							A__       ,       A__							:   Tuple					       =		self.model_tester.prepare_config_and_inputs_for_common()

							# If it doesn't support cache, pass the test
							if not hasattr(A__   ,	"""use_cache"""      ):
								return

							A__							:   Optional[Any]					       =		model_class(A__      ).to(A__      )
							if "use_cache" not in inputs:
								A__							:   Optional[int]					       =		True
							A__							:   List[Any]					       =		model(**A__      )

							# If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
							if "past_key_values" not in outputs:
								return

							A__							:   str					       =		(
							    getattr(A__   ,	"""decoder_layers"""   ,	A__      )
							    or getattr(A__   ,	"""num_decoder_layers"""   ,	A__      )
							    or config.num_hidden_layers
							)
							A__							:   Dict					       =		getattr(A__   ,	"""num_kv_heads"""   ,	config.num_attention_heads      )
							A__							:   List[str]					       =		getattr(A__   ,	"""d_model"""   ,	config.hidden_size      )
							A__							:   Union[str, Any]					       =		embed_dim // num_attention_heads

							A__							:   str					       =		outputs["""past_key_values"""]
							self.assertEqual(len(A__      )   ,	A__      )

							A__       ,       A__							:   int					       =		inputs["""input_ids"""].shape
							for i in range(A__      ):
								if config.new_decoder_architecture:
									A__							:   Any					       =		config.num_attention_heads
								elif config.multi_query:
									A__							:   List[Any]					       =		1
								self.assertEqual(len(past_kv[0]      )   ,	2      )  # K V for the decoder = 2
								self.assertEqual(
								    past_kv[i][0].shape   ,	(batch_size, num_attention_heads, seq_length, per_head_embed_dim)      )
								self.assertEqual(
								    past_kv[i][1].shape   ,	(batch_size, num_attention_heads, seq_length, per_head_embed_dim)      )



@require_torch
class    _a						(unittest.TestCase    ):


					'''simple docstring'''






					@slow
					def   __A      (			self      ):
						A__							:   Dict					       =		AutoTokenizer.from_pretrained("""Rocketknight1/falcon-rw-1b"""      )
						A__							:   List[Any]					       =		FalconForCausalLM.from_pretrained("""Rocketknight1/falcon-rw-1b"""      )
						model.eval()
						model.to(A__      )
						A__							:   Optional[int]					       =		tokenizer("""My favorite food is"""   ,	return_tensors="""pt"""      ).to(A__      )

						A__							:   Optional[Any]					       =		(
						    """My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."""
						)

						A__							:   Any					       =		model.generate(**A__   ,	do_sample=A__   ,	max_new_tokens=19      )
						A__							:   Optional[int]					       =		tokenizer.batch_decode(A__      )[0]

						self.assertEqual(A__   ,	A__      )






					@slow
					def   __A      (			self      ):
						# The big models are way too big for the CI, so we use tiny random models that resemble their
						# architectures but with much smaller and fewer layers
						for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
							A__							:   Dict					       =		AutoTokenizer.from_pretrained(A__      )
							A__							:   List[str]					       =		FalconForCausalLM.from_pretrained(A__      )
							model.eval()
							model.to(A__      )
							A__							:   Union[str, Any]					       =		tokenizer("""My favorite food is"""   ,	return_tensors="""pt"""      ).to(A__      )

							# We just test that these run without errors - the models are randomly initialized
							# and so the actual text outputs will be garbage
							model.generate(**A__   ,	do_sample=A__   ,	max_new_tokens=4      )
							model.generate(**A__   ,	do_sample=A__   ,	max_new_tokens=4      )
							model.generate(**A__   ,	num_beams=2   ,	max_new_tokens=4      )






					@slow
					def   __A      (			self      ):
						# The big models are way too big for the CI, so we use tiny random models that resemble their
						# architectures but with much smaller and fewer layers
						with torch.no_grad():
							for repo in [
							    "Rocketknight1/falcon-rw-1b",
							    "Rocketknight1/tiny-random-falcon-7b",
							    "Rocketknight1/tiny-random-falcon-40b",
							]:
								A__							:   Dict					       =		AutoTokenizer.from_pretrained(A__      )
								A__							:   Any					       =		FalconForCausalLM.from_pretrained(A__      )
								model.eval()
								model.to(device=A__      )
								A__							:   List[str]					       =		tokenizer("""My favorite food is"""   ,	return_tensors="""pt"""      ).to(A__      )

								# Test results are the same with and without cache
								A__							:   Tuple					       =		model.generate(**A__   ,	do_sample=A__   ,	max_new_tokens=20   ,	use_cache=A__      )
								A__							:   Optional[Any]					       =		model.generate(**A__   ,	do_sample=A__   ,	max_new_tokens=20   ,	use_cache=A__      )
								self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0      )



