





import gc
import importlib.metadata
import tempfile
import unittest

from packaging import version

from transformers import (
    AutoModel,
    AutoModelForCausalLM,
    AutoModelForSeqaSeqLM,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    BitsAndBytesConfig,
    pipeline,
)
from transformers.testing_utils import (
    is_torch_available,
    require_accelerate,
    require_bitsandbytes,
    require_torch,
    require_torch_gpu,
    require_torch_multi_gpu,
    slow,
)







def       UpperCAmelCase							(     _lowerCamelCase      :  List[Any]     ):





      '''simple docstring'''


      if model.config.model_type == "gpt2":
            return model.transformer.h[0].mlp.c_fc
      return model.transformer.h[0].mlp.dense_ah_to_h


if is_torch_available():
    import torch
    import torch.nn as nn



    class    _a     (				nn.Module							):






           """simple docstring"""

           def __init__(      self				:     Optional[int]							,	a				:     nn.Module							,	a				:     int    )	->Dict:
                 super().__init__()
                 SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					module
                 SCREAMING_SNAKE_CASE__     :				Dict   =					nn.Sequential(
                     nn.Linear(module.in_features							,	a							,	bias=a    )							,	nn.Linear(a							,	module.out_features							,	bias=a    )							,	)
                 SCREAMING_SNAKE_CASE__     :				List[Any]   =					(2.0 / (5 * min(module.in_features							,	module.out_features    ))) ** 0.5
                 nn.init.normal_(self.adapter[0].weight							,	std=a    )
                 nn.init.zeros_(self.adapter[1].weight    )
                 self.adapter.to(module.weight.device    )






           def      A_  (      self				:     List[str]							,	a				:     Any							,	*a				:     int							,	**a				:     Tuple    )	->Tuple:
                 return self.module(a							,	*a							,	**a    ) + self.adapter(a    )



@require_bitsandbytes
@require_accelerate
@require_torch
@require_torch_gpu
@slow
class    _a     (				unittest.TestCase							):






       """simple docstring"""

       snake_case_							 =						"bigscience/bloom-1b7"

       # Constant values
       snake_case_							 =						2.1_0_9_6_5_9_5_5_2_6_9_2_5_7_4

       snake_case_							 =						"Hello my name is"
       snake_case_							 =						set()
       EXPECTED_OUTPUTS.add("Hello my name is John and I am a professional photographer. I"							)
       EXPECTED_OUTPUTS.add("Hello my name is John.\nI am a friend of your father.\n"							)
       EXPECTED_OUTPUTS.add("Hello my name is John Doe, I am a student at the University"							)
       snake_case_							 =						10

       def      A_  (      self				:     Any    )	->Union[str, Any]:
             SCREAMING_SNAKE_CASE__     :				Tuple   =					AutoTokenizer.from_pretrained(self.model_name    )







class    _a     (				lowercase__							):






       """simple docstring"""

       def      A_  (      self				:     List[str]    )	->List[str]:
             super().setUp()

             # Models and tokenizer
             SCREAMING_SNAKE_CASE__     :				int   =					AutoModelForCausalLM.from_pretrained(
                 self.model_name							,	torch_dtype=torch.floataa							,	device_map="auto"    )
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					AutoModelForCausalLM.from_pretrained(self.model_name							,	load_in_abit=a							,	device_map="auto"    )

       def      A_  (      self				:     str    )	->Optional[Any]:
             del self.model_fpaa
             del self.model_abit

             gc.collect()
             torch.cuda.empty_cache()

       def      A_  (      self				:     Any    )	->Union[str, Any]:
             SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					self.model_abit.config

             self.assertTrue(hasattr(a							,	"quantization_config"    )    )

             SCREAMING_SNAKE_CASE__     :				List[Any]   =					config.to_dict()
             SCREAMING_SNAKE_CASE__     :				List[str]   =					config.to_diff_dict()

             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					config.to_json_string()

       def      A_  (      self				:     Optional[Any]    )	->Optional[Any]:
             from bitsandbytes.nn import Paramsabit

             SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					self.model_fpaa.get_memory_footprint()
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					self.model_abit.get_memory_footprint()

             self.assertAlmostEqual(mem_fpaa / mem_abit							,	self.EXPECTED_RELATIVE_DIFFERENCE    )
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					get_some_linear_layer(self.model_abit    )
             self.assertTrue(linear.weight.__class__ == Paramsabit    )

       def      A_  (      self				:     str    )	->Tuple:
             from transformers import TaPreTrainedModel

             self.model_fpaa.get_memory_footprint()
             self.model_abit.get_memory_footprint()

             for name, module in self.model_abit.named_modules():
                   if isinstance(a							,	torch.nn.Linear    ):
                         if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules:
                               # 4-bit parameters are packed in uint8 variables
                               self.assertTrue(module.weight.dtype == torch.uinta    )

       def      A_  (      self				:     Optional[Any]    )	->Optional[Any]:
             SCREAMING_SNAKE_CASE__     :				str   =					self.tokenizer(self.input_text							,	return_tensors="pt"    )
             SCREAMING_SNAKE_CASE__     :				List[str]   =					self.model_abit.generate(input_ids=encoded_input["input_ids"].to(0    )							,	max_new_tokens=10    )

             self.assertIn(self.tokenizer.decode(output_sequences[0]							,	skip_special_tokens=a    )							,	self.EXPECTED_OUTPUTS    )

       def      A_  (      self				:     Optional[Any]    )	->str:
             SCREAMING_SNAKE_CASE__     :				List[str]   =					BitsAndBytesConfig()
             SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					True

             SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					AutoModelForCausalLM.from_pretrained(
                 self.model_name							,	quantization_config=a							,	device_map="auto"    )

             SCREAMING_SNAKE_CASE__     :				int   =					self.tokenizer(self.input_text							,	return_tensors="pt"    )
             SCREAMING_SNAKE_CASE__     :				int   =					model_abit_from_config.generate(
                 input_ids=encoded_input["input_ids"].to(0    )							,	max_new_tokens=10    )

             self.assertIn(self.tokenizer.decode(output_sequences[0]							,	skip_special_tokens=a    )							,	self.EXPECTED_OUTPUTS    )

       def      A_  (      self				:     str    )	->Dict:







             with self.assertRaises(a    ), tempfile.TemporaryDirectory() as tmpdirname:
                   self.model_abit.save_pretrained(a    )

       def      A_  (      self				:     List[str]    )	->Dict:
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					BitsAndBytesConfig()

             with self.assertRaises(a    ):
                   SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					AutoModelForCausalLM.from_pretrained(
                       self.model_name							,	quantization_config=a							,	load_in_abit=a							,	device_map="auto"							,	bnb_abit_quant_type="nf4"							,	)

       def      A_  (      self				:     str    )	->Optional[Any]:







             with self.assertRaises(a    ):
                   # Tries with `str`
                   self.model_abit.to("cpu"    )

             with self.assertRaises(a    ):
                   # Tries with a `dtype``
                   self.model_abit.to(torch.floataa    )

             with self.assertRaises(a    ):
                   # Tries with a `device`
                   self.model_abit.to(torch.device("cuda:0"    )    )

             with self.assertRaises(a    ):
                   # Tries with a `device`
                   self.model_abit.float()

             with self.assertRaises(a    ):
                   # Tries with a `device`
                   self.model_abit.half()

             # Test if we did not break anything
             SCREAMING_SNAKE_CASE__     :				str   =					self.tokenizer(self.input_text							,	return_tensors="pt"    )

             SCREAMING_SNAKE_CASE__     :				List[str]   =					self.model_fpaa.to(torch.floataa    )
             SCREAMING_SNAKE_CASE__     :				Tuple   =					self.model_fpaa.generate(input_ids=encoded_input["input_ids"].to(0    )							,	max_new_tokens=10    )

             # Check this does not throw an error
             SCREAMING_SNAKE_CASE__     :				List[Any]   =					self.model_fpaa.to("cpu"    )

             # Check this does not throw an error
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					self.model_fpaa.half()

             # Check this does not throw an error
             SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					self.model_fpaa.float()






       def      A_  (      self				:     str    )	->List[Any]:
             SCREAMING_SNAKE_CASE__     :				List[Any]   =					AutoModelForSeqaSeqLM.from_pretrained("t5-small"							,	load_in_abit=a							,	device_map="auto"    )
             self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa    )







@require_bitsandbytes
@require_accelerate
@require_torch
@require_torch_gpu
@slow
class    _a     (				unittest.TestCase							):






       """simple docstring"""

       @classmethod
       def      A_  (      cls				:     Optional[int]    )	->Union[str, Any]:
             SCREAMING_SNAKE_CASE__     :				str   =					't5-small'
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					'google/flan-t5-small'  # flan-t5 uses dense-act instead of dense-relu-dense
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					AutoTokenizer.from_pretrained(cls.model_name    )
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					'Translate in German: Hello, my dog is cute'

       def      A_  (      self				:     Optional[int]    )	->Dict:
             gc.collect()
             torch.cuda.empty_cache()

       def      A_  (      self				:     Any    )	->List[str]:
             from transformers import TaForConditionalGeneration

             SCREAMING_SNAKE_CASE__     :				Any   =					TaForConditionalGeneration._keep_in_fpaa_modules
             SCREAMING_SNAKE_CASE__     :				Tuple   =					None

             # test with `t5-small`
             SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					TaForConditionalGeneration.from_pretrained(self.model_name							,	load_in_abit=a							,	device_map="auto"    )
             SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					self.tokenizer(self.input_text							,	return_tensors="pt"    ).to(0    )
             SCREAMING_SNAKE_CASE__     :				Any   =					model.generate(**a    )

             # test with `flan-t5-small`
             SCREAMING_SNAKE_CASE__     :				List[Any]   =					TaForConditionalGeneration.from_pretrained(
                 self.dense_act_model_name							,	load_in_abit=a							,	device_map="auto"    )
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					self.tokenizer(self.input_text							,	return_tensors="pt"    ).to(0    )
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					model.generate(**a    )
             SCREAMING_SNAKE_CASE__     :				Any   =					modules






       def      A_  (      self				:     List[str]    )	->Tuple:
             import bitsandbytes as bnb

             from transformers import TaForConditionalGeneration

             # test with `t5-small`
             SCREAMING_SNAKE_CASE__     :				Tuple   =					TaForConditionalGeneration.from_pretrained(self.model_name							,	load_in_abit=a							,	device_map="auto"    )

             # there was a bug with decoders - this test checks that it is fixed
             self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q							,	bnb.nn.Linearabit    )    )

             SCREAMING_SNAKE_CASE__     :				List[Any]   =					self.tokenizer(self.input_text							,	return_tensors="pt"    ).to(0    )
             SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					model.generate(**a    )

             # test with `flan-t5-small`
             SCREAMING_SNAKE_CASE__     :				Any   =					TaForConditionalGeneration.from_pretrained(
                 self.dense_act_model_name							,	load_in_abit=a							,	device_map="auto"    )
             SCREAMING_SNAKE_CASE__     :				Tuple   =					self.tokenizer(self.input_text							,	return_tensors="pt"    ).to(0    )
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					model.generate(**a    )







class    _a     (				lowercase__							):






       """simple docstring"""

       def      A_  (      self				:     Optional[Any]    )	->List[str]:
             super().setUp()
             # model_name
             SCREAMING_SNAKE_CASE__     :				Dict   =					'bigscience/bloom-560m'
             SCREAMING_SNAKE_CASE__     :				int   =					't5-small'

             # Different types of model

             SCREAMING_SNAKE_CASE__     :				str   =					AutoModel.from_pretrained(self.model_name							,	load_in_abit=a							,	device_map="auto"    )
             # Sequence classification model
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					AutoModelForSequenceClassification.from_pretrained(
                 self.model_name							,	load_in_abit=a							,	device_map="auto"    )
             # CausalLM model
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					AutoModelForCausalLM.from_pretrained(self.model_name							,	load_in_abit=a							,	device_map="auto"    )
             # Seq2seq model
             SCREAMING_SNAKE_CASE__     :				str   =					AutoModelForSeqaSeqLM.from_pretrained(
                 self.seq_to_seq_name							,	load_in_abit=a							,	device_map="auto"    )

       def      A_  (      self				:     List[Any]    )	->Optional[int]:
             del self.base_model
             del self.sequence_model
             del self.model_abit
             del self.seq_to_seq_model

             gc.collect()
             torch.cuda.empty_cache()






       def      A_  (      self				:     Optional[int]    )	->List[str]:
             from bitsandbytes.nn import Paramsabit

             self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit    )

             # Other heads should be nn.Parameter
             self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter    )
             self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter    )
             self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter    )







class    _a     (				lowercase__							):






       """simple docstring"""

       def      A_  (      self				:     int    )	->List[Any]:
             super().setUp()

       def      A_  (      self				:     List[Any]    )	->Dict:
             del self.pipe

             gc.collect()
             torch.cuda.empty_cache()






       def      A_  (      self				:     List[str]    )	->Optional[Any]:
             SCREAMING_SNAKE_CASE__     :				int   =					pipeline(
                 "text-generation"							,	model=self.model_name							,	model_kwargs={"device_map": "auto", "load_in_4bit": True, "torch_dtype": torch.floataa}							,	max_new_tokens=self.MAX_NEW_TOKENS							,	)

             # Real second forward pass
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					self.pipe(self.input_text    )
             self.assertIn(pipeline_output[0]["generated_text"]							,	self.EXPECTED_OUTPUTS    )







@require_torch_multi_gpu
class    _a     (				lowercase__							):






       """simple docstring"""

       def      A_  (      self				:     Optional[Any]    )	->int:
             super().setUp()






       def      A_  (      self				:     Optional[Any]    )	->Optional[int]:
             SCREAMING_SNAKE_CASE__     :				List[Any]   =					AutoModelForCausalLM.from_pretrained(
                 self.model_name							,	load_in_abit=a							,	device_map="balanced"    )

             # Check correct device map
             self.assertEqual(set(model_parallel.hf_device_map.values()    )							,	{0, 1}    )

             # Check that inference pass works on the model
             SCREAMING_SNAKE_CASE__     :				Optional[int]   =					self.tokenizer(self.input_text							,	return_tensors="pt"    )

             # Second real batch
             SCREAMING_SNAKE_CASE__     :				Any   =					model_parallel.generate(input_ids=encoded_input["input_ids"].to(0    )							,	max_new_tokens=10    )
             self.assertIn(self.tokenizer.decode(output_parallel[0]							,	skip_special_tokens=a    )							,	self.EXPECTED_OUTPUTS    )







class    _a     (				lowercase__							):






       """simple docstring"""

       def      A_  (      self				:     Union[str, Any]    )	->Union[str, Any]:
             SCREAMING_SNAKE_CASE__     :				Tuple   =					'facebook/opt-350m'
             super().setUp()






       def      A_  (      self				:     List[str]    )	->Union[str, Any]:







             if version.parse(importlib.metadata.version("bitsandbytes"    )    ) < version.parse("0.37.0"    ):
                   return

             # Step 1: freeze all parameters
             SCREAMING_SNAKE_CASE__     :				List[Any]   =					AutoModelForCausalLM.from_pretrained(self.model_name							,	load_in_abit=a    )

             self.assertEqual(set(model.hf_device_map.values()    )							,	{torch.cuda.current_device()}    )

             for param in model.parameters():
                   SCREAMING_SNAKE_CASE__     :				Any   =					False  # freeze the model - train adapters later
                   if param.ndim == 1:
                         # cast the small parameters (e.g. layernorm) to fp32 for stability
                         SCREAMING_SNAKE_CASE__     :				List[str]   =					param.data.to(torch.floataa    )

        # Step 2: add adapters
             for _, module in model.named_modules():
                   if "OPTAttention" in repr(type(a    )    ):
                         SCREAMING_SNAKE_CASE__     :				Optional[int]   =					LoRALayer(module.q_proj							,	rank=16    )
                         SCREAMING_SNAKE_CASE__     :				Optional[int]   =					LoRALayer(module.k_proj							,	rank=16    )
                         SCREAMING_SNAKE_CASE__     :				Any   =					LoRALayer(module.v_proj							,	rank=16    )

        # Step 3: dummy batch
             SCREAMING_SNAKE_CASE__     :				List[Any]   =					self.tokenizer("Test batch "							,	return_tensors="pt"    ).to(0    )

             # Step 4: Check if the gradient is not None
             with torch.cuda.amp.autocast():
                   SCREAMING_SNAKE_CASE__     :				Tuple   =					model.forward(**a    )
                   out.logits.norm().backward()

             for module in model.modules():
                   if isinstance(a							,	a    ):
                         self.assertTrue(module.adapter[1].weight.grad is not None    )
                         self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0    )
                   elif isinstance(a							,	nn.Embedding    ):
                         self.assertTrue(module.weight.grad is None    )







class    _a     (				lowercase__							):






       """simple docstring"""

       snake_case_							 =						"gpt2-xl"
       snake_case_							 =						3.3_1_9_1_8_5_4_8_5_4_1_5_2_1_8_7






import sys
from collections import defaultdict



class    _a     :






							"""simple docstring"""

							def __init__(      self				:     Any    )	->Dict:
													SCREAMING_SNAKE_CASE__     :				Tuple   =					[]

							def      A_  (      self				:     int							,	a				:     List[str]    )	->Dict:
													return self.node_position[vertex]

							def      A_  (      self				:     Optional[Any]							,	a				:     Any							,	a				:     List[str]    )	->Optional[Any]:
													SCREAMING_SNAKE_CASE__     :				str   =					pos

							def      A_  (      self				:     List[Any]							,	a				:     List[str]							,	a				:     Dict							,	a				:     Dict							,	a				:     List[Any]    )	->Optional[int]:
													if start > size // 2 - 1:
																			return
													else:
																			if 2 * start + 2 >= size:
																									SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					2 * start + 1
																			else:
																									if heap[2 * start + 1] < heap[2 * start + 2]:
																															SCREAMING_SNAKE_CASE__     :				Dict   =					2 * start + 1
																									else:
																															SCREAMING_SNAKE_CASE__     :				Tuple   =					2 * start + 2
																			if heap[smallest_child] < heap[start]:
																									SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__     :				int   =					heap[smallest_child], positions[smallest_child]
																									SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__     :				Optional[int]   =					(
																									    heap[start],
																									    positions[start],
																									)
																									SCREAMING_SNAKE_CASE__,							SCREAMING_SNAKE_CASE__     :				Tuple   =					temp, tempa

																									SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					self.get_position(positions[smallest_child]    )
																									self.set_position(
																									    positions[smallest_child]							,	self.get_position(positions[start]    )    )
																									self.set_position(positions[start]							,	a    )

																									self.top_to_bottom(a							,	a							,	a							,	a    )

							def      A_  (      self				:     Union[str, Any]							,	a				:     Tuple							,	a				:     Tuple							,	a				:     Union[str, Any]							,	a				:     List[Any]    )	->Optional[int]:
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					position[index]

													while index != 0:
																			SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					int((index - 2) / 2    ) if index % 2 == 0 else int((index - 1) / 2    )

																			if val < heap[parent]:
																									SCREAMING_SNAKE_CASE__     :				List[Any]   =					heap[parent]
																									SCREAMING_SNAKE_CASE__     :				str   =					position[parent]
																									self.set_position(position[parent]							,	a    )
																			else:
																									SCREAMING_SNAKE_CASE__     :				int   =					val
																									SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					temp
																									self.set_position(a							,	a    )
																									break
																			SCREAMING_SNAKE_CASE__     :				Optional[int]   =					parent
													else:
																			SCREAMING_SNAKE_CASE__     :				int   =					val
																			SCREAMING_SNAKE_CASE__     :				List[str]   =					temp
																			self.set_position(a							,	0    )

							def      A_  (      self				:     Union[str, Any]							,	a				:     int							,	a				:     List[str]    )	->Union[str, Any]:
													SCREAMING_SNAKE_CASE__     :				List[str]   =					len(a    ) // 2 - 1
													for i in range(a							,	-1							,	-1    ):
																			self.top_to_bottom(a							,	a							,	len(a    )							,	a    )






							def      A_  (      self				:     Dict							,	a				:     List[Any]							,	a				:     Dict    )	->Optional[int]:
													SCREAMING_SNAKE_CASE__     :				Any   =					positions[0]
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					sys.maxsize
													self.top_to_bottom(a							,	0							,	len(a    )							,	a    )
													return temp







def       UpperCAmelCase							(     _lowerCamelCase      :  str     ):





						'''simple docstring'''


						SCREAMING_SNAKE_CASE__     :				Optional[int]   =					Heap()

						SCREAMING_SNAKE_CASE__     :				Any   =					[0] * len(_lowerCamelCase     )
						SCREAMING_SNAKE_CASE__     :				Any   =					[-1] * len(_lowerCamelCase     )  # Neighboring Tree Vertex of selected vertex
						# Minimum Distance of explored vertex with neighboring vertex of partial tree
						# formed in graph
						SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					[]  # Heap of Distance of vertices from their neighboring vertex
						SCREAMING_SNAKE_CASE__     :				str   =					[]

						for vertex in range(len(_lowerCamelCase     )     ):
												distance_tv.append(sys.maxsize     )
												positions.append(_lowerCamelCase     )
												heap.node_position.append(_lowerCamelCase     )

						SCREAMING_SNAKE_CASE__     :				List[Any]   =					[]
						SCREAMING_SNAKE_CASE__     :				int   =					1
						SCREAMING_SNAKE_CASE__     :				int   =					sys.maxsize
						for neighbor, distance in adjacency_list[0]:
												SCREAMING_SNAKE_CASE__     :				int   =					0
												SCREAMING_SNAKE_CASE__     :				List[str]   =					distance
						heap.heapify(_lowerCamelCase			,   _lowerCamelCase     )

						for _ in range(1			,   len(_lowerCamelCase     )     ):
												SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					heap.delete_minimum(_lowerCamelCase			,   _lowerCamelCase     )
												if visited[vertex] == 0:
																		tree_edges.append((nbr_tv[vertex], vertex)     )
																		SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					1
																		for neighbor, distance in adjacency_list[vertex]:
																								if (
																								    visited[neighbor] == 0
																								    and distance < distance_tv[heap.get_position(_lowerCamelCase     )]
																								):
																														SCREAMING_SNAKE_CASE__     :				Any   =					distance
																														heap.bottom_to_top(
																														    _lowerCamelCase			,   heap.get_position(_lowerCamelCase     )			,   _lowerCamelCase			,   _lowerCamelCase     )
																														SCREAMING_SNAKE_CASE__     :				str   =					vertex
						return tree_edges


if __name__ == "__main__":  # pragma: no cover
				# < --------- Prims Algorithm --------- >
				__lowercase       :Union[str, Any]       =  int(input("Enter number of edges: ").strip())
				__lowercase       :Dict       =  defaultdict(list)
				for _ in range(edges_number):
								__lowercase       :Any       =  [int(x) for x in input().strip().split()]
								adjacency_list[edge[0]].append([edge[1], edge[2]])
								adjacency_list[edge[1]].append([edge[0], edge[2]])
				print(prisms_algorithm(adjacency_list))