'''simple docstring'''




import unittest

from transformers import EsmConfig, is_torch_available
from transformers.testing_utils import TestCasePlus, 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 EsmForMaskedLM, EsmForSequenceClassification, EsmForTokenClassification, EsmModel
						from transformers.models.esm.modeling_esm import (
						    ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
						    EsmEmbeddings,
						    create_position_ids_from_input_ids,
						)






class 							a__:


			'''simple docstring'''






			def __init__(  self			,       __lowerCAmelCase			,       __lowerCAmelCase=13			,       __lowerCAmelCase=7			,       __lowerCAmelCase=False			,       __lowerCAmelCase=True			,       __lowerCAmelCase=False			,       __lowerCAmelCase=True			,       __lowerCAmelCase=33			,       __lowerCAmelCase=32			,       __lowerCAmelCase=5			,       __lowerCAmelCase=4			,       __lowerCAmelCase=37			,       __lowerCAmelCase="gelu"			,       __lowerCAmelCase=0.1			,       __lowerCAmelCase=0.1			,       __lowerCAmelCase=512			,       __lowerCAmelCase=16			,       __lowerCAmelCase=2			,       __lowerCAmelCase=0.02			,       __lowerCAmelCase=3			,       __lowerCAmelCase=4			,       __lowerCAmelCase=None			,       ):




					"""simple docstring"""


					lowerCAmelCase     =    parent
					lowerCAmelCase     =    batch_size
					lowerCAmelCase     =    seq_length
					lowerCAmelCase     =    is_training
					lowerCAmelCase     =    use_input_mask
					lowerCAmelCase     =    use_token_type_ids
					lowerCAmelCase     =    use_labels
					lowerCAmelCase     =    vocab_size
					lowerCAmelCase     =    hidden_size
					lowerCAmelCase     =    num_hidden_layers
					lowerCAmelCase     =    num_attention_heads
					lowerCAmelCase     =    intermediate_size
					lowerCAmelCase     =    hidden_act
					lowerCAmelCase     =    hidden_dropout_prob
					lowerCAmelCase     =    attention_probs_dropout_prob
					lowerCAmelCase     =    max_position_embeddings
					lowerCAmelCase     =    type_vocab_size
					lowerCAmelCase     =    type_sequence_label_size
					lowerCAmelCase     =    initializer_range
					lowerCAmelCase     =    num_labels
					lowerCAmelCase     =    num_choices
					lowerCAmelCase     =    scope



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    ids_tensor([self.batch_size, self.seq_length]			,       self.vocab_size)

					lowerCAmelCase     =    None
					if self.use_input_mask:
							lowerCAmelCase     =    random_attention_mask([self.batch_size, self.seq_length])

					lowerCAmelCase     =    None
					lowerCAmelCase     =    None
					lowerCAmelCase     =    None
					if self.use_labels:
							lowerCAmelCase     =    ids_tensor([self.batch_size]			,       self.type_sequence_label_size)
							lowerCAmelCase     =    ids_tensor([self.batch_size, self.seq_length]			,       self.num_labels)
							lowerCAmelCase     =    ids_tensor([self.batch_size]			,       self.num_choices)

					lowerCAmelCase     =    self.get_config()

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



			def 				a_			(  self):




					"""simple docstring"""


					return EsmConfig(
					    vocab_size=self.vocab_size			,       hidden_size=self.hidden_size			,       pad_token_id=1			,       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			,       )



			def 				a_			(  self			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase     =    EsmModel(config=__lowerCAmelCase)
					model.to(__lowerCAmelCase)
					model.eval()
					lowerCAmelCase     =    model(__lowerCAmelCase			,       attention_mask=__lowerCAmelCase)
					lowerCAmelCase     =    model(__lowerCAmelCase)
					lowerCAmelCase     =    model(__lowerCAmelCase)

					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			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase     =    EsmForMaskedLM(config=__lowerCAmelCase)
					model.to(__lowerCAmelCase)
					model.eval()
					lowerCAmelCase     =    model(__lowerCAmelCase			,       attention_mask=__lowerCAmelCase			,       labels=__lowerCAmelCase)
					self.parent.assertEqual(result.logits.shape			,       (self.batch_size, self.seq_length, self.vocab_size))



			def 				a_			(  self			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase			,       __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase     =    self.num_labels
					lowerCAmelCase     =    EsmForTokenClassification(config=__lowerCAmelCase)
					model.to(__lowerCAmelCase)
					model.eval()
					lowerCAmelCase     =    model(__lowerCAmelCase			,       attention_mask=__lowerCAmelCase			,       labels=__lowerCAmelCase)
					self.parent.assertEqual(result.logits.shape			,       (self.batch_size, self.seq_length, self.num_labels))







			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    self.prepare_config_and_inputs()
					(
					    (
					    lowerCAmelCase
					)   ,	(
					    lowerCAmelCase
					)   ,	(
					    lowerCAmelCase
					)   ,	(
					    lowerCAmelCase
					)   ,	(
					    lowerCAmelCase
					)   ,	(
					    lowerCAmelCase
					)   ,	
					)     =    config_and_inputs
					lowerCAmelCase     =    {"""input_ids""": input_ids, """attention_mask""": input_mask}
					return config, inputs_dict







@require_torch
class 							a__(   lowerCAmelCase__					,		lowerCAmelCase__					,		unittest.TestCase      ):


			'''simple docstring'''






			UpperCAmelCase_     :	Optional[Any]						=			False

			UpperCAmelCase_     :	Optional[Any]						=			(
			    (
			        EsmForMaskedLM,
			        EsmModel,
			        EsmForSequenceClassification,
			        EsmForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCAmelCase_     :	List[Any]						=			()
			UpperCAmelCase_     :	Tuple						=			(
			    {
			        '''feature-extraction''': EsmModel,
			        '''fill-mask''': EsmForMaskedLM,
			        '''text-classification''': EsmForSequenceClassification,
			        '''token-classification''': EsmForTokenClassification,
			        '''zero-shot''': EsmForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			UpperCAmelCase_     :	List[str]						=			True



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    EsmModelTester(self)
					lowerCAmelCase     =    ConfigTester(self			,       config_class=__lowerCAmelCase			,       hidden_size=37)



			def 				a_			(  self):




					"""simple docstring"""


					self.config_tester.run_common_tests()



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__lowerCAmelCase)



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
							lowerCAmelCase     =    type
							self.model_tester.create_and_check_model(*__lowerCAmelCase)



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase)



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase)



			@slow
			def 				a_			(  self):




					"""simple docstring"""


					for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCAmelCase     =    EsmModel.from_pretrained(__lowerCAmelCase)
							self.assertIsNotNone(__lowerCAmelCase)



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    self.model_tester.prepare_config_and_inputs()[0]
					lowerCAmelCase     =    EsmEmbeddings(config=__lowerCAmelCase)

					lowerCAmelCase     =    torch.as_tensor([[12, 31, 13, model.padding_idx]])
					lowerCAmelCase     =    torch.as_tensor(
					    [
					        [
					            0 + model.padding_idx + 1,
					            1 + model.padding_idx + 1,
					            2 + model.padding_idx + 1,
					            model.padding_idx,
					        ]
					    ])
					lowerCAmelCase     =    create_position_ids_from_input_ids(__lowerCAmelCase			,       model.padding_idx)
					self.assertEqual(position_ids.shape			,       expected_positions.shape)
					self.assertTrue(torch.all(torch.eq(__lowerCAmelCase			,       __lowerCAmelCase)))



			def 				a_			(  self):




					"""simple docstring"""


					lowerCAmelCase     =    self.model_tester.prepare_config_and_inputs()[0]
					lowerCAmelCase     =    EsmEmbeddings(config=__lowerCAmelCase)

					lowerCAmelCase     =    torch.empty(2			,       4			,       30)
					lowerCAmelCase     =    [
					    0 + embeddings.padding_idx + 1,
					    1 + embeddings.padding_idx + 1,
					    2 + embeddings.padding_idx + 1,
					    3 + embeddings.padding_idx + 1,
					]
					lowerCAmelCase     =    torch.as_tensor([expected_single_positions, expected_single_positions])
					lowerCAmelCase     =    embeddings.create_position_ids_from_inputs_embeds(__lowerCAmelCase)
					self.assertEqual(position_ids.shape			,       expected_positions.shape)
					self.assertTrue(torch.all(torch.eq(__lowerCAmelCase			,       __lowerCAmelCase)))



			@unittest.skip("""Esm does not support embedding resizing""")
			def 				a_			(  self):




					"""simple docstring"""


					pass



			@unittest.skip("""Esm does not support embedding resizing""")
			def 				a_			(  self):




					"""simple docstring"""


					pass







			@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""")
			def 				a_			(  self):




					"""simple docstring"""


					pass







@require_torch
class 							a__(   lowerCAmelCase__      ):


			'''simple docstring'''






			@slow
			def 				a_			(  self):




					"""simple docstring"""


					with torch.no_grad():
							lowerCAmelCase     =    EsmForMaskedLM.from_pretrained("""facebook/esm2_t6_8M_UR50D""")
							model.eval()
							lowerCAmelCase     =    torch.tensor([[0, 1, 2, 3, 4, 5]])
							lowerCAmelCase     =    model(__lowerCAmelCase)[0]

							lowerCAmelCase     =    33

							lowerCAmelCase     =    torch.Size((1, 6, vocab_size))
							self.assertEqual(output.shape			,       __lowerCAmelCase)

							lowerCAmelCase     =    torch.tensor(
							    [[[8.9215, -10.5898, -6.4671], [-6.3967, -13.9114, -1.1212], [-7.7812, -13.9516, -3.7406]]])
							self.assertTrue(torch.allclose(output[:, :3, :3]			,       __lowerCAmelCase			,       atol=1E-4))







			@slow
			def 				a_			(  self):




					"""simple docstring"""


					with torch.no_grad():
							lowerCAmelCase     =    EsmModel.from_pretrained("""facebook/esm2_t6_8M_UR50D""")
							model.eval()

							lowerCAmelCase     =    torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]])
							lowerCAmelCase     =    model(__lowerCAmelCase)[0]
							# compare the actual values for a slice.
							lowerCAmelCase     =    torch.tensor(
							    [[[0.1444, 0.5413, 0.3248], [0.3034, 0.0053, 0.3108], [0.3228, -0.2499, 0.3415]]])
							self.assertTrue(torch.allclose(output[:, :3, :3]			,       __lowerCAmelCase			,       atol=1E-4))





'''simple docstring'''




# A Bipartite Graph is a graph whose vertices can be divided into two independent sets,
# U and V such that every edge (u, v) either connects a vertex from U to V or a vertex
# from V to U. In other words, for every edge (u, v), either u belongs to U and v to V,
# or u belongs to V and v to U. We can also say that there is no edge that connects
# vertices of same set.






def 							snake_case__				(     _A:   Dict					)       ->      str:

		'''simple docstring'''


		lowerCAmelCase     =    [False] * len(_A					)
		lowerCAmelCase     =    [-1] * len(_A					)

		def dfs(_A:   Dict ,  _A:   Tuple					):
				lowerCAmelCase     =    True
				lowerCAmelCase     =    c
				for u in graph[v]:
						if not visited[u]:
								dfs(_A ,  1 - c					)

		for i in range(len(_A					)					):
				if not visited[i]:
						dfs(_A ,  0					)

		for i in range(len(_A					)					):
				for j in graph[i]:
						if color[i] == color[j]:
								return False

		return True


# Adjacency list of graph
__lowercase        =    {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: []}




print(check_bipartite_dfs(graph))




