
import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
							import transformer_engine.pytorch as te



def 			UpperCamelCase       (				lowerCAmelCase__					,     lowerCAmelCase__=True					,     lowerCAmelCase__=True					,     lowerCAmelCase__=True						):
					'''simple docstring'''






					if not is_fpa_available():
										raise ImportError('''Using `convert_model` requires transformer_engine to be installed.'''						)
					for name, module in model.named_children():
										if isinstance(lowerCAmelCase__					,     nn.Linear						) and to_transformer_engine and _convert_linear:
															# Return early if the linear layer weights are not multiples of 16
															if any(p % 16 != 0 for p in module.weight.shape						):
																				return
															lowercase		      =		module.bias is not None
															lowercase		      =		te.Linear(
															    module.in_features					,     module.out_features					,     bias=lowerCAmelCase__					,     params_dtype=module.weight.dtype						)
															lowercase		      =		module.weight.data.clone()
															if has_bias:
																				lowercase		      =		module.bias.data.clone()

															setattr(lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__						)
										elif isinstance(lowerCAmelCase__					,     nn.LayerNorm						) and to_transformer_engine and _convert_ln:
															lowercase		      =		te.LayerNorm(module.normalized_shape[0]					,     eps=module.eps					,     params_dtype=module.weight.dtype						)
															lowercase		      =		module.weight.data.clone()
															lowercase		      =		module.bias.data.clone()

															setattr(lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__						)
										elif isinstance(lowerCAmelCase__					,     te.Linear						) and not to_transformer_engine and _convert_linear:
															lowercase		      =		module.bias is not None
															lowercase		      =		nn.Linear(
															    module.in_features					,     module.out_features					,     bias=lowerCAmelCase__					,     params_dtype=module.weight.dtype						)
															lowercase		      =		module.weight.data.clone()
															if has_bias:
																				lowercase		      =		module.bias.data.clone()

															setattr(lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__						)
										elif isinstance(lowerCAmelCase__					,     te.LayerNorm						) and not to_transformer_engine and _convert_ln:
															lowercase		      =		nn.LayerNorm(module.normalized_shape[0]					,     eps=module.eps					,     params_dtype=module.weight.dtype						)
															lowercase		      =		module.weight.data.clone()
															lowercase		      =		module.bias.data.clone()

															setattr(lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__						)
										else:
															convert_model(
															    lowerCAmelCase__					,     to_transformer_engine=lowerCAmelCase__					,     _convert_linear=_convert_linear					,     _convert_ln=_convert_ln					,     )



def 			UpperCamelCase       (				lowerCAmelCase__						):
					'''simple docstring'''






					if not is_fpa_available():
										raise ImportError('''Using `has_transformer_engine_layers` requires transformer_engine to be installed.'''						)
					for m in model.modules():
										if isinstance(lowerCAmelCase__					,     (te.LayerNorm, te.Linear)						):
															return True
					return False








def 			__SCREAMING_SNAKE_CASE		(		UpperCamelCase		:      int     ,					UpperCamelCase		:      list[tuple[int, int, int]]	)     ->	list[tuple[int, int, int]]:



	"""simple docstring"""






	a_     			=   sorted(UpperCamelCase     ,					key=lambda UpperCamelCase	: edge[2]	)

	a_     			=   list(range(UpperCamelCase	)	)

	def find_parent(UpperCamelCase		:      Any	):
		if i != parent[i]:
			a_     			=   find_parent(parent[i]	)
		return parent[i]

	a_     			=   0
	a_     			=   []

	for edge in edges:
		a_     			=   find_parent(edge[0]	)
		a_     			=   find_parent(edge[1]	)
		if parent_a != parent_b:
			minimum_spanning_tree_cost += edge[2]
			minimum_spanning_tree.append(UpperCamelCase	)
			a_     			=   parent_b

	return minimum_spanning_tree


if __name__ == "__main__":  # pragma: no cover
			_A     ,				_A							     =       list(map(int, input().strip().split()))
			_A							     =       []

			for _ in range(num_edges):
						_A     ,				_A     ,				_A							     =       (int(x) for x in input().strip().split())
						edges.append((nodea, nodea, cost))

			kruskal(num_nodes, edges)