



import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    YosoForMaskedLM,
				    YosoForMultipleChoice,
				    YosoForQuestionAnswering,
				    YosoForSequenceClassification,
				    YosoForTokenClassification,
				    YosoModel,
				)
				from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST



class       _lowercase				:





	"""simple docstring"""




	def __init__(self						,			lowerCamelCase_						,			lowerCamelCase_=13						,			lowerCamelCase_=7						,			lowerCamelCase_=True						,			lowerCamelCase_=True						,			lowerCamelCase_=True						,			lowerCamelCase_=True						,			lowerCamelCase_=99						,			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"""

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




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

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

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

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

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

						a   	=  self.get_config()

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




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						return YosoConfig(
						    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						,			is_decoder=lowerCamelCase_						,			initializer_range=self.initializer_range						,			)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.get_config()
						a   	=  300
						return config




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						(
						    (
						    a
						)	,							(
						    a
						)	,							(
						    a
						)	,							(
						    a
						)	,							(
						    a
						)	,							(
						    a
						)	,							(
						    a
						)	,							
						)   	=  self.prepare_config_and_inputs()

						a   	=  True
						a   	=  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
						a   	=  ids_tensor([self.batch_size, self.seq_length]						,			vocab_size=2						)

						return (
						    config,
						    input_ids,
						    token_type_ids,
						    input_mask,
						    sequence_labels,
						    token_labels,
						    choice_labels,
						    encoder_hidden_states,
						    encoder_attention_mask,
						)




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  YosoModel(config=lowerCamelCase_						)
						model.to(lowerCamelCase_						)
						model.eval()
						a   	=  model(lowerCamelCase_						,			attention_mask=lowerCamelCase_						,			token_type_ids=lowerCamelCase_						)
						a   	=  model(lowerCamelCase_						,			token_type_ids=lowerCamelCase_						)
						a   	=  model(lowerCamelCase_						)
						self.parent.assertEqual(result.last_hidden_state.shape						,			(self.batch_size, self.seq_length, self.hidden_size)						)




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			):
						"""simple docstring"""

						a   	=  True
						a   	=  YosoModel(lowerCamelCase_						)
						model.to(lowerCamelCase_						)
						model.eval()
						a   	=  model(
						    lowerCamelCase_						,			attention_mask=lowerCamelCase_						,			token_type_ids=lowerCamelCase_						,			encoder_hidden_states=lowerCamelCase_						,			encoder_attention_mask=lowerCamelCase_						,			)
						a   	=  model(
						    lowerCamelCase_						,			attention_mask=lowerCamelCase_						,			token_type_ids=lowerCamelCase_						,			encoder_hidden_states=lowerCamelCase_						,			)
						a   	=  model(lowerCamelCase_						,			attention_mask=lowerCamelCase_						,			token_type_ids=lowerCamelCase_						)
						self.parent.assertEqual(result.last_hidden_state.shape						,			(self.batch_size, self.seq_length, self.hidden_size)						)




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  YosoForMaskedLM(config=lowerCamelCase_						)
						model.to(lowerCamelCase_						)
						model.eval()
						a   	=  model(lowerCamelCase_						,			attention_mask=lowerCamelCase_						,			token_type_ids=lowerCamelCase_						,			labels=lowerCamelCase_						)
						self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.vocab_size)						)




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  YosoForQuestionAnswering(config=lowerCamelCase_						)
						model.to(lowerCamelCase_						)
						model.eval()
						a   	=  model(
						    lowerCamelCase_						,			attention_mask=lowerCamelCase_						,			token_type_ids=lowerCamelCase_						,			start_positions=lowerCamelCase_						,			end_positions=lowerCamelCase_						,			)
						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 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  self.num_labels
						a   	=  YosoForSequenceClassification(lowerCamelCase_						)
						model.to(lowerCamelCase_						)
						model.eval()
						a   	=  model(lowerCamelCase_						,			attention_mask=lowerCamelCase_						,			token_type_ids=lowerCamelCase_						,			labels=lowerCamelCase_						)
						self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_labels)						)




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  self.num_labels
						a   	=  YosoForTokenClassification(config=lowerCamelCase_						)
						model.to(lowerCamelCase_						)
						model.eval()
						a   	=  model(lowerCamelCase_						,			attention_mask=lowerCamelCase_						,			token_type_ids=lowerCamelCase_						,			labels=lowerCamelCase_						)
						self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.num_labels)						)




	def 	UpperCamelCase_			(self						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						,			lowerCamelCase_						):
						"""simple docstring"""

						a   	=  self.num_choices
						a   	=  YosoForMultipleChoice(config=lowerCamelCase_						)
						model.to(lowerCamelCase_						)
						model.eval()
						a   	=  input_ids.unsqueeze(1						).expand(-1						,			self.num_choices						,			-1						).contiguous()
						a   	=  token_type_ids.unsqueeze(1						).expand(-1						,			self.num_choices						,			-1						).contiguous()
						a   	=  input_mask.unsqueeze(1						).expand(-1						,			self.num_choices						,			-1						).contiguous()
						a   	=  model(
						    lowerCamelCase_						,			attention_mask=lowerCamelCase_						,			token_type_ids=lowerCamelCase_						,			labels=lowerCamelCase_						,			)
						self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_choices)						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

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




@require_torch
class       _lowercase				(  lowerCAmelCase, lowerCAmelCase, unittest.TestCase      ):





	"""simple docstring"""
	__A		      =				(
	    (
	        YosoModel,
	        YosoForMaskedLM,
	        YosoForMultipleChoice,
	        YosoForQuestionAnswering,
	        YosoForSequenceClassification,
	        YosoForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	__A		      =				False
	__A		      =				False
	__A		      =				False

	__A		      =				()
	__A		      =				(
	    {
	        "feature-extraction": YosoModel,
	        "fill-mask": YosoForMaskedLM,
	        "question-answering": YosoForQuestionAnswering,
	        "text-classification": YosoForSequenceClassification,
	        "token-classification": YosoForTokenClassification,
	        "zero-shot": YosoForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  YosoModelTester(self						)
						a   	=  ConfigTester(self						,			config_class=lowerCamelCase_						,			hidden_size=37						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						self.config_tester.run_common_tests()




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

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




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_						)




	@slow
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											a   	=  YosoModel.from_pretrained(lowerCamelCase_						)
											self.assertIsNotNone(lowerCamelCase_						)




	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						return




@require_torch
class       _lowercase				(  unittest.TestCase      ):





	"""simple docstring"""




	@slow
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  YosoModel.from_pretrained("uw-madison/yoso-4096"						)
						a   	=  torch.tensor([[0, 1, 2, 3, 4, 5]]						)

						with torch.no_grad():
											a   	=  model(lowerCamelCase_						)[0]

						a   	=  torch.Size((1, 6, 768)						)
						self.assertEqual(output.shape						,			lowerCamelCase_						)

						a   	=  torch.tensor(
						    [[[-0.0611, 0.1242, 0.0840], [0.0280, -0.0048, 0.1125], [0.0106, 0.0226, 0.0751]]]						)

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




	@slow
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"						)
						a   	=  torch.tensor([[0, 1, 2, 3, 4, 5]]						)

						with torch.no_grad():
											a   	=  model(lowerCamelCase_						)[0]

						a   	=  50265

						a   	=  torch.Size((1, 6, vocab_size)						)
						self.assertEqual(output.shape						,			lowerCamelCase_						)

						a   	=  torch.tensor(
						    [[[-2.1313, -3.7285, -2.2407], [-2.7047, -3.3314, -2.6408], [0.0629, -2.5166, -0.3356]]]						)

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




	@slow
	def 	UpperCamelCase_			(self						):
						"""simple docstring"""

						a   	=  YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"						)
						a   	=  torch.arange(4096						).unsqueeze(0						)

						with torch.no_grad():
											a   	=  model(lowerCamelCase_						)[0]

						a   	=  50265

						a   	=  torch.Size((1, 4096, vocab_size)						)
						self.assertEqual(output.shape						,			lowerCamelCase_						)

						a   	=  torch.tensor(
						    [[[-2.3914, -4.3742, -5.0956], [-4.0988, -4.2384, -7.0406], [-3.1427, -3.7192, -6.6800]]]						)

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





from __future__ import annotations

from collections import defaultdict
from dataclasses import dataclass



@dataclass
class       _lowercase				:





	"""simple docstring"""
	__A		      =				42
	__A		      =				None
	__A		      =				None






def        a( )						->			TreeNode:
					"""simple docstring"""





					return TreeNode(3					,	TreeNode(9			)					,	TreeNode(20					,	TreeNode(15			)					,	TreeNode(7			)			)			)






def        a( A			:       TreeNode			)						->			list[int]:
					"""simple docstring"""





					def depth_first_search(
					    A			:       TreeNode | None					,	A			:       int					,	A			:       list[int]			) -> None:
										if not root:
															return

										if depth == len(A			):
															right_view.append(root.val			)

										depth_first_search(root.right					,	depth + 1					,	A			)
										depth_first_search(root.left					,	depth + 1					,	A			)

					a   	=  []
					if not root:
										return right_view

					depth_first_search(A					,	0					,	A			)
					return right_view






def        a( A			:       TreeNode			)						->			list[int]:
					"""simple docstring"""





					def depth_first_search(
					    A			:       TreeNode | None					,	A			:       int					,	A			:       list[int]			) -> None:
										if not root:
															return

										if depth == len(A			):
															left_view.append(root.val			)

										depth_first_search(root.left					,	depth + 1					,	A			)
										depth_first_search(root.right					,	depth + 1					,	A			)

					a   	=  []
					if not root:
										return left_view

					depth_first_search(A					,	0					,	A			)
					return left_view






def        a( A			:       TreeNode			)						->			list[int]:
					"""simple docstring"""





					def breadth_first_search(A			:       TreeNode					,	A			:       list[int]			) -> None:
										a   	=  [(root, 0)]
										a   	=  defaultdict(A			)

										while queue:
															a   	=  queue.pop(0			)
															a	,							a   	=  first

															lookup[hd].append(node.val			)

															if node.left:
																				queue.append((node.left, hd - 1)			)
															if node.right:
																				queue.append((node.right, hd + 1)			)

										for pair in sorted(lookup.items()					,	key=lambda A			: each[0]			):
															top_view.append(pair[1][0]			)

					a   	=  []
					if not root:
										return top_view

					breadth_first_search(A					,	A			)
					return top_view






def        a( A			:       TreeNode			)						->			list[int]:
					"""simple docstring"""





					from collections import defaultdict

					def breadth_first_search(A			:       TreeNode					,	A			:       list[int]			) -> None:
										a   	=  [(root, 0)]
										a   	=  defaultdict(A			)

										while queue:
															a   	=  queue.pop(0			)
															a	,							a   	=  first
															lookup[hd].append(node.val			)

															if node.left:
																				queue.append((node.left, hd - 1)			)
															if node.right:
																				queue.append((node.right, hd + 1)			)

										for pair in sorted(lookup.items()					,	key=lambda A			: each[0]			):
															bottom_view.append(pair[1][-1]			)

					a   	=  []
					if not root:
										return bottom_view

					breadth_first_search(A					,	A			)
					return bottom_view


if __name__ == "__main__":
				import doctest

				doctest.testmod()
