



'''simple docstring'''




import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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 (
						    MODEL_FOR_PRETRAINING_MAPPING,
						    AlbertForMaskedLM,
						    AlbertForMultipleChoice,
						    AlbertForPreTraining,
						    AlbertForQuestionAnswering,
						    AlbertForSequenceClassification,
						    AlbertForTokenClassification,
						    AlbertModel,
						)
						from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST







class 	_lowerCAmelCase							:


	def __init__(self       ,      lowercase       ,      lowercase=13       ,      lowercase=7       ,      lowercase=True       ,      lowercase=True       ,      lowercase=True       ,      lowercase=True       ,      lowercase=99       ,      lowercase=16       ,      lowercase=36       ,      lowercase=6       ,      lowercase=6       ,      lowercase=6       ,      lowercase=37       ,      lowercase="gelu"       ,      lowercase=0.1       ,      lowercase=0.1       ,      lowercase=512       ,      lowercase=16       ,      lowercase=2       ,      lowercase=0.02       ,      lowercase=3       ,      lowercase=4       ,      lowercase=None       ,      ):
				A_	:						List[Any]	=     parent
				A_	:						List[Any]	=     batch_size
				A_	:						Any	=     seq_length
				A_	:						Union[str, Any]	=     is_training
				A_	:						str	=     use_input_mask
				A_	:						Any	=     use_token_type_ids
				A_	:						str	=     use_labels
				A_	:						Optional[int]	=     vocab_size
				A_	:						Optional[int]	=     embedding_size
				A_	:						str	=     hidden_size
				A_	:						List[Any]	=     num_hidden_layers
				A_	:						str	=     num_hidden_groups
				A_	:						Tuple	=     num_attention_heads
				A_	:						Union[str, Any]	=     intermediate_size
				A_	:						Optional[Any]	=     hidden_act
				A_	:						List[Any]	=     hidden_dropout_prob
				A_	:						List[str]	=     attention_probs_dropout_prob
				A_	:						str	=     max_position_embeddings
				A_	:						Optional[Any]	=     type_vocab_size
				A_	:						Union[str, Any]	=     type_sequence_label_size
				A_	:						Optional[int]	=     initializer_range
				A_	:						Union[str, Any]	=     num_labels
				A_	:						Optional[Any]	=     num_choices
				A_	:						Tuple	=     scope


	def     _a	(self							):
				A_	:						int	=     ids_tensor([self.batch_size, self.seq_length]       ,      self.vocab_size							)

				A_	:						Union[str, Any]	=     None
				if self.use_input_mask:
							A_	:						List[str]	=     random_attention_mask([self.batch_size, self.seq_length]							)

				A_	:						List[str]	=     None
				if self.use_token_type_ids:
							A_	:						str	=     ids_tensor([self.batch_size, self.seq_length]       ,      self.type_vocab_size							)

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

				A_	:						Optional[Any]	=     self.get_config()

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


	def     _a	(self							):
				return AlbertConfig(
				    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       ,      initializer_range=self.initializer_range       ,      num_hidden_groups=self.num_hidden_groups       ,      )


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase							):
				A_	:						List[str]	=     AlbertModel(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_	:						str	=     model(lowercase       ,      attention_mask=lowercase       ,      token_type_ids=lowercase							)
				A_	:						str	=     model(lowercase       ,      token_type_ids=lowercase							)
				A_	:						Optional[int]	=     model(lowercase							)
				self.parent.assertEqual(result.last_hidden_state.shape       ,      (self.batch_size, self.seq_length, self.hidden_size)							)
				self.parent.assertEqual(result.pooler_output.shape       ,      (self.batch_size, self.hidden_size)							)


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase							):
				A_	:						Dict	=     AlbertForPreTraining(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_	:						Optional[Any]	=     model(
				    lowercase       ,      attention_mask=lowercase       ,      token_type_ids=lowercase       ,      labels=lowercase       ,      sentence_order_label=lowercase       ,      )
				self.parent.assertEqual(result.prediction_logits.shape       ,      (self.batch_size, self.seq_length, self.vocab_size)							)
				self.parent.assertEqual(result.sop_logits.shape       ,      (self.batch_size, config.num_labels)							)


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase							):
				A_	:						Union[str, Any]	=     AlbertForMaskedLM(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_	:						Any	=     model(lowercase       ,      attention_mask=lowercase       ,      token_type_ids=lowercase       ,      labels=lowercase							)
				self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.seq_length, self.vocab_size)							)


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase							):
				A_	:						Optional[Any]	=     AlbertForQuestionAnswering(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_	:						Optional[int]	=     model(
				    lowercase       ,      attention_mask=lowercase       ,      token_type_ids=lowercase       ,      start_positions=lowercase       ,      end_positions=lowercase       ,      )
				self.parent.assertEqual(result.start_logits.shape       ,      (self.batch_size, self.seq_length)							)
				self.parent.assertEqual(result.end_logits.shape       ,      (self.batch_size, self.seq_length)							)


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase							):
				A_	:						Optional[int]	=     self.num_labels
				A_	:						Tuple	=     AlbertForSequenceClassification(lowercase							)
				model.to(lowercase							)
				model.eval()
				A_	:						Union[str, Any]	=     model(lowercase       ,      attention_mask=lowercase       ,      token_type_ids=lowercase       ,      labels=lowercase							)
				self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.num_labels)							)


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase							):
				A_	:						int	=     self.num_labels
				A_	:						Optional[int]	=     AlbertForTokenClassification(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_	:						List[Any]	=     model(lowercase       ,      attention_mask=lowercase       ,      token_type_ids=lowercase       ,      labels=lowercase							)
				self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.seq_length, self.num_labels)							)


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase       ,      lowercase							):
				A_	:						Dict	=     self.num_choices
				A_	:						Dict	=     AlbertForMultipleChoice(config=lowercase							)
				model.to(lowercase							)
				model.eval()
				A_	:						Optional[Any]	=     input_ids.unsqueeze(1							).expand(-1       ,      self.num_choices       ,      -1							).contiguous()
				A_	:						Any	=     token_type_ids.unsqueeze(1							).expand(-1       ,      self.num_choices       ,      -1							).contiguous()
				A_	:						int	=     input_mask.unsqueeze(1							).expand(-1       ,      self.num_choices       ,      -1							).contiguous()
				A_	:						Tuple	=     model(
				    lowercase       ,      attention_mask=lowercase       ,      token_type_ids=lowercase       ,      labels=lowercase       ,      )
				self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.num_choices)							)


	def     _a	(self							):
				A_	:						Optional[Any]	=     self.prepare_config_and_inputs()
				(
				    (
				    A_
				),    (
				    A_
				),    (
				    A_
				),    (
				    A_
				),    (
				    A_
				),    (
				    A_
				),    (
				    A_
				),    
				)	:						List[str]	=     config_and_inputs
				A_	:						int	=     {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
				return config, inputs_dict







@require_torch
class 	_lowerCAmelCase							(			__UpperCAmelCase      ,			__UpperCAmelCase      ,			unittest.TestCase ):
	__SCREAMING_SNAKE_CASE   :		Any					=					(
	    (
	        AlbertModel,
	        AlbertForPreTraining,
	        AlbertForMaskedLM,
	        AlbertForMultipleChoice,
	        AlbertForSequenceClassification,
	        AlbertForTokenClassification,
	        AlbertForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	__SCREAMING_SNAKE_CASE   :		Any					=					(
	    {
	        'feature-extraction': AlbertModel,
	        'fill-mask': AlbertForMaskedLM,
	        'question-answering': AlbertForQuestionAnswering,
	        'text-classification': AlbertForSequenceClassification,
	        'token-classification': AlbertForTokenClassification,
	        'zero-shot': AlbertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	__SCREAMING_SNAKE_CASE   :		Optional[Any]					=					True


	def     _a	(self       ,      lowercase       ,      lowercase       ,      lowercase=False							):
				A_	:						int	=     super()._prepare_for_class(lowercase       ,      lowercase       ,      return_labels=lowercase							)

				if return_labels:
							if model_class in get_values(lowercase							):
										A_	:						Any	=     torch.zeros(
										    (self.model_tester.batch_size, self.model_tester.seq_length)       ,      dtype=torch.long       ,      device=lowercase							)
										A_	:						List[str]	=     torch.zeros(
										    self.model_tester.batch_size       ,      dtype=torch.long       ,      device=lowercase							)
				return inputs_dict


	def     _a	(self							):
				A_	:						Tuple	=     AlbertModelTester(self							)
				A_	:						List[str]	=     ConfigTester(self       ,      config_class=lowercase       ,      hidden_size=37							)


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


	def     _a	(self							):
				A_	:						Optional[int]	=     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowercase							)


	def     _a	(self							):
				A_	:						str	=     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_pretraining(*lowercase							)


	def     _a	(self							):
				A_	:						Union[str, Any]	=     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_lm(*lowercase							)


	def     _a	(self							):
				A_	:						Optional[Any]	=     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_multiple_choice(*lowercase							)


	def     _a	(self							):
				A_	:						Tuple	=     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*lowercase							)


	def     _a	(self							):
				A_	:						Any	=     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*lowercase							)


	def     _a	(self							):
				A_	:						int	=     self.model_tester.prepare_config_and_inputs()
				for type in ["absolute", "relative_key", "relative_key_query"]:
							A_	:						Dict	=     type
							self.model_tester.create_and_check_model(*lowercase							)


	@slow
	def     _a	(self							):
				for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							A_	:						List[Any]	=     AlbertModel.from_pretrained(lowercase							)
							self.assertIsNotNone(lowercase							)







@require_torch
class 	_lowerCAmelCase							(			unittest.TestCase ):


	@slow
	def     _a	(self							):
				A_	:						Dict	=     AlbertModel.from_pretrained("""albert-base-v2"""							)
				A_	:						List[str]	=     torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]							)
				A_	:						int	=     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
				with torch.no_grad():
							A_	:						Tuple	=     model(lowercase       ,      attention_mask=lowercase							)[0]
				A_	:						str	=     torch.Size((1, 11, 768)							)
				self.assertEqual(output.shape       ,      lowercase							)
				A_	:						Union[str, Any]	=     torch.tensor(
				    [[[-0.65_13, 1.50_35, -0.27_66], [-0.65_15, 1.50_46, -0.27_80], [-0.65_12, 1.50_49, -0.27_84]]]							)

				self.assertTrue(torch.allclose(output[:, 1:4, 1:4]       ,      lowercase       ,      atol=1E-4							)							)




'''simple docstring'''




# Author: OMKAR PATHAK, Nwachukwu Chidiebere

# Use a Python dictionary to construct the graph.
from __future__ import annotations

from pprint import pformat
from typing import Generic, TypeVar

lowerCamelCase							:str				    =					TypeVar('''T''')







class 	_lowerCAmelCase							(			Generic[T] ):


	def __init__(self       ,      lowercase = True							):
				A_	:						dict[T, list[T]]	=     {}  # dictionary of lists
				A_	:						Any	=     directed


	def     _a	(self       ,      lowercase       ,      lowercase							):

				if not self.directed:  # For undirected graphs
							# if both source vertex and destination vertex are both present in the
							# adjacency list, add destination vertex to source vertex list of adjacent
							# vertices and add source vertex to destination vertex list of adjacent
							# vertices.
							if source_vertex in self.adj_list and destination_vertex in self.adj_list:
										self.adj_list[source_vertex].append(lowercase							)
										self.adj_list[destination_vertex].append(lowercase							)
							# if only source vertex is present in adjacency list, add destination vertex
							# to source vertex list of adjacent vertices, then create a new vertex with
							# destination vertex as key and assign a list containing the source vertex
							# as it's first adjacent vertex.
							elif source_vertex in self.adj_list:
										self.adj_list[source_vertex].append(lowercase							)
										A_	:						Dict	=     [source_vertex]
							# if only destination vertex is present in adjacency list, add source vertex
							# to destination vertex list of adjacent vertices, then create a new vertex
							# with source vertex as key and assign a list containing the source vertex
							# as it's first adjacent vertex.
							elif destination_vertex in self.adj_list:
										self.adj_list[destination_vertex].append(lowercase							)
										A_	:						int	=     [destination_vertex]
							# if both source vertex and destination vertex are not present in adjacency
							# list, create a new vertex with source vertex as key and assign a list
							# containing the destination vertex as it's first adjacent vertex also
							# create a new vertex with destination vertex as key and assign a list
							# containing the source vertex as it's first adjacent vertex.
							else:
										A_	:						Optional[Any]	=     [destination_vertex]
										A_	:						Tuple	=     [source_vertex]
				else:  # For directed graphs
							# if both source vertex and destination vertex are present in adjacency
							# list, add destination vertex to source vertex list of adjacent vertices.
							if source_vertex in self.adj_list and destination_vertex in self.adj_list:
										self.adj_list[source_vertex].append(lowercase							)
							# if only source vertex is present in adjacency list, add destination
							# vertex to source vertex list of adjacent vertices and create a new vertex
							# with destination vertex as key, which has no adjacent vertex
							elif source_vertex in self.adj_list:
										self.adj_list[source_vertex].append(lowercase							)
										A_	:						Tuple	=     []
							# if only destination vertex is present in adjacency list, create a new
							# vertex with source vertex as key and assign a list containing destination
							# vertex as first adjacent vertex
							elif destination_vertex in self.adj_list:
										A_	:						Tuple	=     [destination_vertex]
							# if both source vertex and destination vertex are not present in adjacency
							# list, create a new vertex with source vertex as key and a list containing
							# destination vertex as it's first adjacent vertex. Then create a new vertex
							# with destination vertex as key, which has no adjacent vertex
							else:
										A_	:						int	=     [destination_vertex]
										A_	:						List[str]	=     []

				return self


	def __repr__(self							):
				return pformat(self.adj_list							)