





'''simple docstring'''

import unittest

from transformers import MraConfig, 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


if is_torch_available():
		import torch

		from transformers import (
		    MraForMaskedLM,
		    MraForMultipleChoice,
		    MraForQuestionAnswering,
		    MraForSequenceClassification,
		    MraForTokenClassification,
		    MraModel,
		)
		from transformers.models.mra.modeling_mra import MRA_PRETRAINED_MODEL_ARCHIVE_LIST



class 		lowerCamelCase__      :




					'''simple docstring'''



					def __init__(  self				:	Any     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	int=2     ,  UpperCamelCase_				:	List[Any]=8     ,  UpperCamelCase_				:	Dict=True     ,  UpperCamelCase_				:	Optional[int]=True     ,  UpperCamelCase_				:	Any=True     ,  UpperCamelCase_				:	int=True     ,  UpperCamelCase_				:	Any=99     ,  UpperCamelCase_				:	Any=16     ,  UpperCamelCase_				:	Optional[Any]=5     ,  UpperCamelCase_				:	List[str]=2     ,  UpperCamelCase_				:	Optional[int]=36     ,  UpperCamelCase_				:	int="gelu"     ,  UpperCamelCase_				:	Tuple=0.0     ,  UpperCamelCase_				:	List[str]=0.0     ,  UpperCamelCase_				:	Dict=512     ,  UpperCamelCase_				:	Any=16     ,  UpperCamelCase_				:	Tuple=2     ,  UpperCamelCase_				:	Any=0.02     ,  UpperCamelCase_				:	Tuple=3     ,  UpperCamelCase_				:	Optional[int]=4     ,  UpperCamelCase_				:	Optional[Any]=None     ,  )  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       Any       =		parent
						_lowercase					:       Tuple       =		batch_size
						_lowercase					:       Dict       =		seq_length
						_lowercase					:       str       =		is_training
						_lowercase					:       Tuple       =		use_input_mask
						_lowercase					:       int       =		use_token_type_ids
						_lowercase					:       str       =		use_labels
						_lowercase					:       Any       =		vocab_size
						_lowercase					:       Tuple       =		hidden_size
						_lowercase					:       Optional[Any]       =		num_hidden_layers
						_lowercase					:       Optional[Any]       =		num_attention_heads
						_lowercase					:       int       =		intermediate_size
						_lowercase					:       int       =		hidden_act
						_lowercase					:       Optional[int]       =		hidden_dropout_prob
						_lowercase					:       Any       =		attention_probs_dropout_prob
						_lowercase					:       int       =		max_position_embeddings
						_lowercase					:       Optional[Any]       =		type_vocab_size
						_lowercase					:       Dict       =		type_sequence_label_size
						_lowercase					:       Tuple       =		initializer_range
						_lowercase					:       str       =		num_labels
						_lowercase					:       str       =		num_choices
						_lowercase					:       Optional[int]       =		scope




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       Any:



						'''simple docstring'''





						_lowercase					:       str       =		ids_tensor([self.batch_size, self.seq_length]     ,  self.vocab_size					)

						_lowercase					:       int       =		None
						if self.use_input_mask:
							_lowercase					:       List[str]       =		random_attention_mask([self.batch_size, self.seq_length]					)

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

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

						_lowercase					:       List[Any]       =		self.get_config()

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




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       Optional[Any]:



						'''simple docstring'''





						return MraConfig(
						    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=UpperCamelCase_     ,  initializer_range=self.initializer_range     ,  )




					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       str:



						'''simple docstring'''





						_lowercase					:       List[str]       =		self.get_config()
						_lowercase					:       Optional[int]       =		300
						return config




					def 						__UpperCAmelCase     (  self				:	Any					)  ->       Any:



						'''simple docstring'''





						(
						    _lowercase    
						)					:       Any       =		self.prepare_config_and_inputs()

						_lowercase					:       Union[str, Any]       =		True
						_lowercase					:       Any       =		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]					)
						_lowercase					:       Any       =		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				:	int     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	str					)  ->       List[str]:



						'''simple docstring'''





						_lowercase					:       Any       =		MraModel(config=UpperCamelCase_					)
						model.to(UpperCamelCase_					)
						model.eval()
						_lowercase					:       str       =		model(UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_					)
						_lowercase					:       Optional[int]       =		model(UpperCamelCase_     ,  token_type_ids=UpperCamelCase_					)
						_lowercase					:       Tuple       =		model(UpperCamelCase_					)
						self.parent.assertEqual(result.last_hidden_state.shape     ,  (self.batch_size, self.seq_length, self.hidden_size)					)




					def 						__UpperCAmelCase     (  self				:	str     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	Any     ,  )  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		True
						_lowercase					:       Union[str, Any]       =		MraModel(UpperCamelCase_					)
						model.to(UpperCamelCase_					)
						model.eval()
						_lowercase					:       Optional[Any]       =		model(
						    UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  encoder_hidden_states=UpperCamelCase_     ,  encoder_attention_mask=UpperCamelCase_     ,  )
						_lowercase					:       Optional[Any]       =		model(
						    UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  encoder_hidden_states=UpperCamelCase_     ,  )
						_lowercase					:       List[str]       =		model(UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_					)
						self.parent.assertEqual(result.last_hidden_state.shape     ,  (self.batch_size, self.seq_length, self.hidden_size)					)




					def 						__UpperCAmelCase     (  self				:	List[Any]     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Union[str, Any]					)  ->       Union[str, Any]:



						'''simple docstring'''





						_lowercase					:       int       =		MraForMaskedLM(config=UpperCamelCase_					)
						model.to(UpperCamelCase_					)
						model.eval()
						_lowercase					:       str       =		model(UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  labels=UpperCamelCase_					)
						self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.seq_length, self.vocab_size)					)




					def 						__UpperCAmelCase     (  self				:	List[Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	Any					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       Union[str, Any]       =		MraForQuestionAnswering(config=UpperCamelCase_					)
						model.to(UpperCamelCase_					)
						model.eval()
						_lowercase					:       Union[str, Any]       =		model(
						    UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  start_positions=UpperCamelCase_     ,  end_positions=UpperCamelCase_     ,  )
						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				:	Any     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Any					)  ->       List[str]:



						'''simple docstring'''





						_lowercase					:       Union[str, Any]       =		self.num_labels
						_lowercase					:       Dict       =		MraForSequenceClassification(UpperCamelCase_					)
						model.to(UpperCamelCase_					)
						model.eval()
						_lowercase					:       Optional[int]       =		model(UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  labels=UpperCamelCase_					)
						self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_labels)					)




					def 						__UpperCAmelCase     (  self				:	str     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	int					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       Dict       =		self.num_labels
						_lowercase					:       int       =		MraForTokenClassification(config=UpperCamelCase_					)
						model.to(UpperCamelCase_					)
						model.eval()
						_lowercase					:       str       =		model(UpperCamelCase_     ,  attention_mask=UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  labels=UpperCamelCase_					)
						self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.seq_length, self.num_labels)					)




					def 						__UpperCAmelCase     (  self				:	str     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	List[Any]					)  ->       Any:



						'''simple docstring'''





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




					def 						__UpperCAmelCase     (  self				:	List[Any]					)  ->       Tuple:



						'''simple docstring'''





						_lowercase					:       int       =		self.prepare_config_and_inputs()
						(
						    _lowercase    
						)					:       str       =		config_and_inputs
						_lowercase					:       List[str]       =		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
						return config, inputs_dict






@require_torch
class 		lowerCamelCase__      (						A	,   unittest.TestCase     ):




					'''simple docstring'''



					A_						    =						(
					    (
					        MraModel,
					        MraForMaskedLM,
					        MraForMultipleChoice,
					        MraForQuestionAnswering,
					        MraForSequenceClassification,
					        MraForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					A_						    =						False
					A_						    =						False
					A_						    =						False
					A_						    =						False

					A_						    =						()




					def 						__UpperCAmelCase     (  self				:	int					)  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       Tuple       =		MraModelTester(self					)
						_lowercase					:       Optional[int]       =		ConfigTester(self     ,  config_class=UpperCamelCase_     ,  hidden_size=37					)




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       int:



						'''simple docstring'''





						self.config_tester.run_common_tests()




					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	List[Any]					)  ->       Dict:



						'''simple docstring'''





						_lowercase					:       List[str]       =		self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
							_lowercase					:       Optional[int]       =		type
							self.model_tester.create_and_check_model(*UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       int:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Any					)  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       List[str]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       Optional[Any]:



						'''simple docstring'''





						_lowercase					:       Any       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Any					)  ->       str:



						'''simple docstring'''





						_lowercase					:       List[str]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase_					)




					def 						__UpperCAmelCase     (  self				:	Any					)  ->       str:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*UpperCamelCase_					)




					@slow
					def 						__UpperCAmelCase     (  self				:	str					)  ->       Tuple:



						'''simple docstring'''





						for model_name in MRA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_lowercase					:       List[Any]       =		MraModel.from_pretrained(UpperCamelCase_					)
							self.assertIsNotNone(UpperCamelCase_					)




					@unittest.skip(reason='MRA does not output attentions'					)
					def 						__UpperCAmelCase     (  self				:	Optional[Any]					)  ->       str:



						'''simple docstring'''





						return






@require_torch
class 		lowerCamelCase__      (						unittest.TestCase     ):




					'''simple docstring'''



					@slow
					def 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		MraModel.from_pretrained('uw-madison/mra-base-512-4'					)
						_lowercase					:       Optional[Any]       =		torch.arange(256					).unsqueeze(0					)

						with torch.no_grad():
							_lowercase					:       str       =		model(UpperCamelCase_					)[0]

						_lowercase					:       str       =		torch.Size((1, 256, 768)					)
						self.assertEqual(output.shape     ,  UpperCamelCase_					)

						_lowercase					:       List[Any]       =		torch.tensor(
						    [[[-0.01_40, 0.08_30, -0.03_81], [0.15_46, 0.14_02, 0.02_20], [0.11_62, 0.08_51, 0.01_65]]]					)

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




					@slow
					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       str:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		MraForMaskedLM.from_pretrained('uw-madison/mra-base-512-4'					)
						_lowercase					:       Union[str, Any]       =		torch.arange(256					).unsqueeze(0					)

						with torch.no_grad():
							_lowercase					:       Union[str, Any]       =		model(UpperCamelCase_					)[0]

						_lowercase					:       int       =		5_0265

						_lowercase					:       Optional[int]       =		torch.Size((1, 256, vocab_size)					)
						self.assertEqual(output.shape     ,  UpperCamelCase_					)

						_lowercase					:       Dict       =		torch.tensor(
						    [[[9.25_95, -3.60_38, 11.88_19], [9.38_69, -3.26_93, 11.09_56], [11.85_24, -3.49_38, 13.12_10]]]					)

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




					@slow
					def 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       Union[str, Any]       =		MraForMaskedLM.from_pretrained('uw-madison/mra-base-4096-8-d3'					)
						_lowercase					:       Optional[int]       =		torch.arange(4096					).unsqueeze(0					)

						with torch.no_grad():
							_lowercase					:       List[str]       =		model(UpperCamelCase_					)[0]

						_lowercase					:       Optional[Any]       =		5_0265

						_lowercase					:       Dict       =		torch.Size((1, 4096, vocab_size)					)
						self.assertEqual(output.shape     ,  UpperCamelCase_					)

						_lowercase					:       int       =		torch.tensor(
						    [[[5.47_89, -2.35_64, 7.50_64], [7.90_67, -1.33_69, 9.96_68], [9.07_12, -1.81_06, 7.03_80]]]					)

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





'''simple docstring'''

from __future__ import annotations

import requests







def 						__UpperCamelCase				(							_lowercase					) ->			dict:
	_lowercase					:       Optional[int]       =		f'''https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty'''
	return requests.get(_lowercase					).json()







def 						__UpperCamelCase				(							_lowercase = 10					) ->			list[dict]:
	_lowercase					:       Union[str, Any]       =		'https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'
	_lowercase					:       Optional[Any]       =		requests.get(_lowercase					).json()[:max_stories]
	return [get_hackernews_story(_lowercase					) for story_id in story_ids]







def 						__UpperCamelCase				(							_lowercase = 10					) ->			str:
	_lowercase					:       Tuple       =		hackernews_top_stories(_lowercase					)
	return "\n".join('* [{title}]({url})'.format(**_lowercase					) for story in stories					)


if __name__ == "__main__":
		print(hackernews_top_stories_as_markdown())
