




from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
		from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


UpperCAmelCase_						=logging.get_logger(__name__)

UpperCAmelCase_						={
    """bigscience/bloom""": """https://huggingface.co/bigscience/bloom/resolve/main/config.json""",
    """bigscience/bloom-560m""": """https://huggingface.co/bigscience/bloom-560m/blob/main/config.json""",
    """bigscience/bloom-1b1""": """https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json""",
    """bigscience/bloom-1b7""": """https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json""",
    """bigscience/bloom-3b""": """https://huggingface.co/bigscience/bloom-3b/blob/main/config.json""",
    """bigscience/bloom-7b1""": """https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json""",
}


class 							__UpperCamelCase			( __UpperCAmelCase  ):





				'''simple docstring'''



				__a							: Union[str, Any]										="""bloom"""
				__a							: Any										=["""past_key_values"""]
				__a							: str										={
				    """num_hidden_layers""": """n_layer""",
				    """num_attention_heads""": """n_head""",
				}



				def __init__( self       , UpperCAmelCase_=25_08_80       , UpperCAmelCase_=64       , UpperCAmelCase_=2       , UpperCAmelCase_=8       , UpperCAmelCase_=1E-5       , UpperCAmelCase_=0.02       , UpperCAmelCase_=True       , UpperCAmelCase_=1       , UpperCAmelCase_=2       , UpperCAmelCase_=False       , UpperCAmelCase_=0.0       , UpperCAmelCase_=0.0       , UpperCAmelCase_=1       , UpperCAmelCase_=False       , **UpperCAmelCase_       , ):
											lowerCAmelCase   =		vocab_size
											# Backward compatibility with n_embed kwarg
											lowerCAmelCase   =		kwargs.pop('''n_embed'''       , UpperCAmelCase_						)
											lowerCAmelCase   =		hidden_size if n_embed is None else n_embed
											lowerCAmelCase   =		n_layer
											lowerCAmelCase   =		n_head
											lowerCAmelCase   =		layer_norm_epsilon
											lowerCAmelCase   =		initializer_range
											lowerCAmelCase   =		use_cache
											lowerCAmelCase   =		pretraining_tp
											lowerCAmelCase   =		apply_residual_connection_post_layernorm
											lowerCAmelCase   =		hidden_dropout
											lowerCAmelCase   =		attention_dropout

											lowerCAmelCase   =		bos_token_id
											lowerCAmelCase   =		eos_token_id
											lowerCAmelCase   =		slow_but_exact

											super().__init__(bos_token_id=UpperCAmelCase_       , eos_token_id=UpperCAmelCase_       , **UpperCAmelCase_						)




class 							__UpperCamelCase			( __UpperCAmelCase  ):





				'''simple docstring'''



				__a							: List[Any]										=version.parse("""1.12"""  )



				def __init__( self       , UpperCAmelCase_       , UpperCAmelCase_ = "default"       , UpperCAmelCase_ = None       , UpperCAmelCase_ = False       , ):
											super().__init__(UpperCAmelCase_       , task=UpperCAmelCase_       , patching_specs=UpperCAmelCase_       , use_past=UpperCAmelCase_						)
											if not getattr(self._config       , '''pad_token_id'''       , UpperCAmelCase_						):
																		# TODO: how to do that better?
																		lowerCAmelCase   =		0



				@property
				def       __snake_case		( self						):
											lowerCAmelCase   =		OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}						)
											if self.use_past:
																		# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
																		self.fill_with_past_key_values_(UpperCAmelCase_       , direction='''inputs'''       , inverted_values_shape=UpperCAmelCase_						)
																		lowerCAmelCase   =		{0: '''batch''', 1: '''past_sequence + sequence'''}
											else:
																		lowerCAmelCase   =		{0: '''batch''', 1: '''sequence'''}

											return common_inputs



				@property
				def       __snake_case		( self						):
											return self._config.n_layer



				@property
				def       __snake_case		( self						):
											return self._config.n_head



				@property
				def       __snake_case		( self						):
											return 1E-3



				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_ = -1       , UpperCAmelCase_ = -1       , UpperCAmelCase_ = False       , UpperCAmelCase_ = None       , ):
											lowerCAmelCase   =		super(UpperCAmelCase_       , self						).generate_dummy_inputs(
											    UpperCAmelCase_       , batch_size=UpperCAmelCase_       , seq_length=UpperCAmelCase_       , is_pair=UpperCAmelCase_       , framework=UpperCAmelCase_						)

											# We need to order the input in the way they appears in the forward()
											lowerCAmelCase   =		OrderedDict({'''input_ids''': common_inputs['''input_ids''']}						)

											# Need to add the past_keys
											if self.use_past:
																		if not is_torch_available():
																									raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''						)
																		else:
																									import torch

																									lowerCAmelCase   ,     lowerCAmelCase   =		common_inputs['''input_ids'''].shape
																									# Not using the same length for past_key_values
																									lowerCAmelCase   =		seqlen + 2
																									lowerCAmelCase   =		self._config.hidden_size // self.num_attention_heads
																									lowerCAmelCase   =		(
																									    batch * self.num_attention_heads,
																									    head_dim,
																									    past_key_values_length,
																									)
																									lowerCAmelCase   =		(
																									    batch * self.num_attention_heads,
																									    past_key_values_length,
																									    head_dim,
																									)
																									lowerCAmelCase   =		[
																									    (torch.zeros(UpperCAmelCase_						), torch.zeros(UpperCAmelCase_						)) for _ in range(self.num_layers						)
																									]

											lowerCAmelCase   =		common_inputs['''attention_mask''']
											if self.use_past:
																		lowerCAmelCase   =		ordered_inputs['''attention_mask'''].dtype
																		lowerCAmelCase   =		torch.cat(
																		    [ordered_inputs['''attention_mask'''], torch.ones(UpperCAmelCase_       , UpperCAmelCase_       , dtype=UpperCAmelCase_						)]       , dim=1						)

											return ordered_inputs






				@property
				def       __snake_case		( self						):
											return 13









from __future__ import annotations

from typing import Generic, TypeVar

UpperCAmelCase_						=TypeVar("""T""")


class 							__UpperCamelCase			( Generic[T]  ):





				'''simple docstring'''



				def __init__( self       , UpperCAmelCase_						):
											lowerCAmelCase   =		data
											lowerCAmelCase   =		self
											lowerCAmelCase   =		0




class 							__UpperCamelCase			( Generic[T]  ):





				'''simple docstring'''



				def __init__( self						):
											# map from node name to the node object
											lowerCAmelCase   =		{}



				def       __snake_case		( self       , UpperCAmelCase_						):
											# create a new set with x as its member
											lowerCAmelCase   =		DisjointSetTreeNode(UpperCAmelCase_						)



				def       __snake_case		( self       , UpperCAmelCase_						):
											# find the set x belongs to (with path-compression)
											lowerCAmelCase   =		self.map[data]
											if elem_ref != elem_ref.parent:
																		lowerCAmelCase   =		self.find_set(elem_ref.parent.data						)
											return elem_ref.parent



				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_						):
											# helper function for union operation
											if nodea.rank > nodea.rank:
																		lowerCAmelCase   =		nodea
											else:
																		lowerCAmelCase   =		nodea
																		if nodea.rank == nodea.rank:
																									nodea.rank += 1






				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_						):
											# merge 2 disjoint sets
											self.link(self.find_set(UpperCAmelCase_						)       , self.find_set(UpperCAmelCase_						)						)




class 							__UpperCamelCase			( Generic[T]  ):





				'''simple docstring'''



				def __init__( self						):
											# connections: map from the node to the neighbouring nodes (with weights)
											lowerCAmelCase   =		{}



				def       __snake_case		( self       , UpperCAmelCase_						):
											# add a node ONLY if its not present in the graph
											if node not in self.connections:
																		lowerCAmelCase   =		{}



				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_						):
											# add an edge with the given weight
											self.add_node(UpperCAmelCase_						)
											self.add_node(UpperCAmelCase_						)
											lowerCAmelCase   =		weight
											lowerCAmelCase   =		weight






				def       __snake_case		( self						):
											lowerCAmelCase   =		[]
											lowerCAmelCase   =		set()
											for start in self.connections:
																		for end in self.connections[start]:
																									if (start, end) not in seen:
																																seen.add((end, start)						)
																																edges.append((start, end, self.connections[start][end])						)
											edges.sort(key=lambda UpperCAmelCase_						: x[2]						)

											# creating the disjoint set
											lowerCAmelCase   =		DisjointSetTree[T]()
											for node in self.connections:
																		disjoint_set.make_set(UpperCAmelCase_						)

											# MST generation
											lowerCAmelCase   =		0
											lowerCAmelCase   =		0
											lowerCAmelCase   =		GraphUndirectedWeighted[T]()
											while num_edges < len(self.connections						) - 1:
																		lowerCAmelCase   ,     lowerCAmelCase   ,     lowerCAmelCase   =		edges[index]
																		index += 1
																		lowerCAmelCase   =		disjoint_set.find_set(UpperCAmelCase_						)
																		lowerCAmelCase   =		disjoint_set.find_set(UpperCAmelCase_						)
																		if parent_u != parent_v:
																									num_edges += 1
																									graph.add_edge(UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_						)
																									disjoint_set.union(UpperCAmelCase_       , UpperCAmelCase_						)
											return graph



