


'''simple docstring'''


from __future__ import annotations

import unittest

from transformers import AutoTokenizer, GPTJConfig, is_tf_available
from transformers.testing_utils import require_tf, slow, tooslow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin
from ...utils.test_modeling_tf_core import TFCoreModelTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers.models.gptj.modeling_tf_gptj import (
				    TFGPTJForCausalLM,
				    TFGPTJForQuestionAnswering,
				    TFGPTJForSequenceClassification,
				    TFGPTJModel,
				    shape_list,
				)





class    lowercase__  :





							'''simple docstring'''


							def __init__(   self   , __snake_case  ):
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				parent
											_SCREAMING_SNAKE_CASE			:	Any							=				13
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				7
											_SCREAMING_SNAKE_CASE			:	Dict							=				True
											_SCREAMING_SNAKE_CASE			:	str							=				True
											_SCREAMING_SNAKE_CASE			:	Dict							=				True
											_SCREAMING_SNAKE_CASE			:	int							=				True
											_SCREAMING_SNAKE_CASE			:	Any							=				True
											_SCREAMING_SNAKE_CASE			:	Dict							=				99
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				32
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				4
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				2
											_SCREAMING_SNAKE_CASE			:	Tuple							=				4
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				37
											_SCREAMING_SNAKE_CASE			:	Tuple							=				"gelu"
											_SCREAMING_SNAKE_CASE			:	Tuple							=				0.1
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				0.1
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				512
											_SCREAMING_SNAKE_CASE			:	Any							=				16
											_SCREAMING_SNAKE_CASE			:	Dict							=				2
											_SCREAMING_SNAKE_CASE			:	Tuple							=				0.02
											_SCREAMING_SNAKE_CASE			:	Tuple							=				3
											_SCREAMING_SNAKE_CASE			:	str							=				4
											_SCREAMING_SNAKE_CASE			:	Tuple							=				None
											_SCREAMING_SNAKE_CASE			:	int							=				self.vocab_size - 1
											_SCREAMING_SNAKE_CASE			:	Dict							=				self.vocab_size - 1
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				self.vocab_size - 1






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	str							=				ids_tensor([self.batch_size, self.seq_length]   , self.vocab_size  )

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

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

											_SCREAMING_SNAKE_CASE			:	int							=				None
											if self.use_mc_token_ids:
															_SCREAMING_SNAKE_CASE			:	List[Any]							=				ids_tensor([self.batch_size, self.num_choices]   , self.seq_length  )

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

											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				GPTJConfig(
											    vocab_size=self.vocab_size   , n_embd=self.hidden_size   , n_layer=self.num_hidden_layers   , n_head=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   , n_positions=self.max_position_embeddings   , type_vocab_size=self.type_vocab_size   , initializer_range=self.initializer_range   , bos_token_id=self.bos_token_id   , eos_token_id=self.eos_token_id   , pad_token_id=self.pad_token_id   , rotary_dim=self.rotary_dim   , return_dict=__snake_case   , )

											_SCREAMING_SNAKE_CASE			:	List[str]							=				ids_tensor([self.num_hidden_layers, self.num_attention_heads]   , 2  )

											return (
											    config,
											    input_ids,
											    input_mask,
											    head_mask,
											    token_type_ids,
											    mc_token_ids,
											    sequence_labels,
											    token_labels,
											    choice_labels,
											)






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , *__snake_case  ):
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				TFGPTJModel(config=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				{
											    "input_ids": input_ids,
											    "attention_mask": input_mask,
											    "token_type_ids": token_type_ids,
											}
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				model(__snake_case  )

											_SCREAMING_SNAKE_CASE			:	Dict							=				[input_ids, None, input_mask]  # None is the input for 'past'
											_SCREAMING_SNAKE_CASE			:	List[str]							=				model(__snake_case  )

											_SCREAMING_SNAKE_CASE			:	str							=				model(__snake_case  )

											self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.hidden_size)  )






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , *__snake_case  ):
											_SCREAMING_SNAKE_CASE			:	Dict							=				TFGPTJModel(config=__snake_case  )

											# first forward pass
											_SCREAMING_SNAKE_CASE			:	List[str]							=				model(__snake_case   , token_type_ids=__snake_case   , use_cache=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				model(__snake_case   , token_type_ids=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	Dict							=				model(__snake_case   , token_type_ids=__snake_case   , use_cache=__snake_case  )

											self.parent.assertTrue(len(__snake_case  ) == len(__snake_case  )  )
											self.parent.assertTrue(len(__snake_case  ) == len(__snake_case  ) + 1  )

											_SCREAMING_SNAKE_CASE			:	Any							=				outputs.to_tuple()

											# create hypothetical next token and extent to next_input_ids
											_SCREAMING_SNAKE_CASE			:	Tuple							=				ids_tensor((self.batch_size, 1)   , config.vocab_size  )
											_SCREAMING_SNAKE_CASE			:	int							=				ids_tensor([self.batch_size, 1]   , self.type_vocab_size  )

											# append to next input_ids and token_type_ids
											_SCREAMING_SNAKE_CASE			:	Dict							=				tf.concat([input_ids, next_tokens]   , axis=-1  )
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				tf.concat([token_type_ids, next_token_types]   , axis=-1  )

											_SCREAMING_SNAKE_CASE			:	int							=				model(__snake_case   , token_type_ids=__snake_case  )["last_hidden_state"]
											_SCREAMING_SNAKE_CASE			:	Tuple							=				model(__snake_case   , token_type_ids=__snake_case   , past_key_values=__snake_case  )[
											    "last_hidden_state"
											]

											# select random slice
											_SCREAMING_SNAKE_CASE			:	List[str]							=				int(ids_tensor((1,)   , shape_list(__snake_case  )[-1]  )  )
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				output_from_no_past[:, -1, random_slice_idx]
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				output_from_past[:, 0, random_slice_idx]

											# test that outputs are equal for slice
											tf.debugging.assert_near(__snake_case   , __snake_case   , rtol=1e-6  )






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , *__snake_case  ):
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				TFGPTJModel(config=__snake_case  )

											# create attention mask
											_SCREAMING_SNAKE_CASE			:	Tuple							=				self.seq_length // 2
											_SCREAMING_SNAKE_CASE			:	Tuple							=				tf.ones((self.batch_size, half_seq_length)   , dtype=tf.intaa  )
											_SCREAMING_SNAKE_CASE			:	Any							=				tf.zeros((self.batch_size, self.seq_length - half_seq_length)   , dtype=tf.intaa  )
											_SCREAMING_SNAKE_CASE			:	List[str]							=				tf.concat([attn_mask_begin, attn_mask_end]   , axis=1  )

											# first forward pass
											_SCREAMING_SNAKE_CASE			:	Any							=				model(__snake_case   , attention_mask=__snake_case  ).to_tuple()

											# create hypothetical next token and extent to next_input_ids
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				ids_tensor((self.batch_size, 1)   , config.vocab_size  )

											# change a random masked slice from input_ids
											_SCREAMING_SNAKE_CASE			:	List[str]							=				ids_tensor((1,)   , __snake_case  ).numpy() + 1
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				ids_tensor((self.batch_size, self.seq_length)   , config.vocab_size  )
											_SCREAMING_SNAKE_CASE			:	str							=				tf.range(self.seq_length  ) == (self.seq_length - random_seq_idx_to_change)
											_SCREAMING_SNAKE_CASE			:	Dict							=				tf.transpose(
											    tf.broadcast_to(tf.expand_dims(__snake_case   , -1  )   , (self.seq_length, self.batch_size)  )  )
											_SCREAMING_SNAKE_CASE			:	int							=				tf.where(__snake_case   , __snake_case   , __snake_case  )

											# append to next input_ids and attn_mask
											_SCREAMING_SNAKE_CASE			:	int							=				tf.concat([input_ids, next_tokens]   , axis=-1  )
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				tf.concat([attn_mask, tf.ones((shape_list(__snake_case  )[0], 1)   , dtype=tf.intaa  )]   , axis=1  )

											# get two different outputs
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				model(__snake_case   , attention_mask=__snake_case  )["last_hidden_state"]
											_SCREAMING_SNAKE_CASE			:	int							=				model(__snake_case   , past_key_values=__snake_case   , attention_mask=__snake_case  )[
											    "last_hidden_state"
											]

											# select random slice
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				int(ids_tensor((1,)   , shape_list(__snake_case  )[-1]  )  )
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				output_from_no_past[:, -1, random_slice_idx]
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				output_from_past[:, 0, random_slice_idx]

											# test that outputs are equal for slice
											tf.debugging.assert_near(__snake_case   , __snake_case   , rtol=1e-12  )






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , *__snake_case  ):
											_SCREAMING_SNAKE_CASE			:	int							=				TFGPTJModel(config=__snake_case  )

											_SCREAMING_SNAKE_CASE			:	int							=				input_ids[:1, :]
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				input_mask[:1, :]
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				token_type_ids[:1, :]
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				1

											# first forward pass
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				model(__snake_case   , attention_mask=__snake_case   , token_type_ids=__snake_case   , use_cache=__snake_case  )

											_SCREAMING_SNAKE_CASE			:	List[str]							=				outputs.to_tuple()

											# create hypothetical next token and extent to next_input_ids
											_SCREAMING_SNAKE_CASE			:	int							=				ids_tensor((self.batch_size, 3)   , config.vocab_size  )
											_SCREAMING_SNAKE_CASE			:	str							=				ids_tensor((self.batch_size, 3)   , 2  )
											_SCREAMING_SNAKE_CASE			:	int							=				ids_tensor((self.batch_size, 3)   , self.type_vocab_size  )

											# append to next input_ids and token_type_ids
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				tf.concat([input_ids, next_tokens]   , axis=-1  )
											_SCREAMING_SNAKE_CASE			:	Tuple							=				tf.concat([input_mask, next_attn_mask]   , axis=-1  )
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				tf.concat([token_type_ids, next_token_types]   , axis=-1  )

											_SCREAMING_SNAKE_CASE			:	List[Any]							=				model(
											    __snake_case   , token_type_ids=__snake_case   , attention_mask=__snake_case  )["last_hidden_state"]
											_SCREAMING_SNAKE_CASE			:	List[str]							=				model(
											    __snake_case   , token_type_ids=__snake_case   , attention_mask=__snake_case   , past_key_values=__snake_case   , )["last_hidden_state"]
											self.parent.assertTrue(output_from_past.shape[1] == next_tokens.shape[1]  )

											# select random slice
											_SCREAMING_SNAKE_CASE			:	int							=				int(ids_tensor((1,)   , shape_list(__snake_case  )[-1]  )  )
											_SCREAMING_SNAKE_CASE			:	Tuple							=				output_from_no_past[:, -3:, random_slice_idx]
											_SCREAMING_SNAKE_CASE			:	List[str]							=				output_from_past[:, :, random_slice_idx]

											# test that outputs are equal for slice
											tf.debugging.assert_near(__snake_case   , __snake_case   , rtol=1e-3  )






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , *__snake_case  ):
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				TFGPTJForCausalLM(config=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				{
											    "input_ids": input_ids,
											    "attention_mask": input_mask,
											    "token_type_ids": token_type_ids,
											}
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				model(__snake_case  )
											self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.vocab_size)  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Tuple							=				self.prepare_config_and_inputs()

											(
											    _SCREAMING_SNAKE_CASE
											)			:	List[str]							=				config_and_inputs

											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				{
											    "input_ids": input_ids,
											    "token_type_ids": token_type_ids,
											    "attention_mask": input_mask,
											}
											return config, inputs_dict





@require_tf
class    lowercase__  (						__A					,					__A					,					__A					,					unittest.TestCase ):





							'''simple docstring'''


							A_							:				List[Any]				     =  (
							    (TFGPTJForCausalLM, TFGPTJForSequenceClassification, TFGPTJForQuestionAnswering, TFGPTJModel)
							    if is_tf_available()
							    else ()
							)

							A_							:				str				     =  (TFGPTJForCausalLM,) if is_tf_available() else ()
							A_							:				Any				     =  (
							    {
							        """feature-extraction""": TFGPTJModel,
							        """question-answering""": TFGPTJForQuestionAnswering,
							        """text-classification""": TFGPTJForSequenceClassification,
							        """text-generation""": TFGPTJForCausalLM,
							        """zero-shot""": TFGPTJForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							A_							:				str				     =  False
							A_							:				Any				     =  False
							A_							:				str				     =  False
							A_							:				Tuple				     =  False






							def UpperCAmelCase_     (   self   , __snake_case   , __snake_case   , __snake_case   , __snake_case   , __snake_case  ):

											if (
											    pipeline_test_casse_name == "QAPipelineTests"
											    and tokenizer_name is not None
											    and not tokenizer_name.endswith("""Fast"""  )
											):
															# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
															# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
															# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
															return True

											return False






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Any							=				TFGPTJModelTester(self  )
											_SCREAMING_SNAKE_CASE			:	int							=				ConfigTester(self   , config_class=__snake_case   , n_embd=37  )






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






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	int							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_gptj_model(*__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_gptj_model_past(*__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_gptj_model_attention_mask_past(*__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	List[str]							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_gptj_model_past_large_inputs(*__snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_gptj_lm_head_model(*__snake_case  )






							@slow
							@unittest.skipIf(
							    not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""  )  ) > 0   , """skip testing on GPU for now to avoid GPU OOM."""   , )
							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				TFGPTJModel.from_pretrained("""EleutherAI/gpt-j-6B"""   , from_pt=__snake_case  )
											self.assertIsNotNone(__snake_case  )






							@unittest.skip(reason="""Currently, model embeddings are going to undergo a major refactor."""  )
							def UpperCAmelCase_     (   self  ):
											super().test_resize_token_embeddings()





@require_tf
@tooslow
# Marked as @tooslow due to GPU OOM -- but still useful to run locally. Requires ~39GB of RAM.
class    lowercase__  (						unittest.TestCase ):





							'''simple docstring'''


							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B"""   , from_pt=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				tf.convert_to_tensor([[464, 3290]]   , dtype=tf.intaa  )  # The dog
											# fmt: off
											# The dog is a man's best friend. It is a loyal companion, and it is a friend
											_SCREAMING_SNAKE_CASE			:	str							=				[464, 3290, 318, 257, 582, 338, 1266, 1545, 13, 632, 318, 257, 9112, 1_5185, 11, 290, 340, 318, 257, 1545]
											# fmt: on
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				model.generate(__snake_case   , do_sample=__snake_case  )
											self.assertListEqual(output_ids[0].numpy().tolist()   , __snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Tuple							=				AutoTokenizer.from_pretrained("""EleutherAI/gpt-j-6B"""   , revision="""float16"""  )
											_SCREAMING_SNAKE_CASE			:	int							=				TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B"""   , revision="""float16"""   , from_pt=__snake_case  )

											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				tokenizer("""Today is a nice day and"""   , return_tensors="""tf"""  )
											# forces the generation to happen on CPU, to avoid GPU-related quirks
											with tf.device(""":/CPU:0"""  ):
															_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				model.generate(**__snake_case   , do_sample=__snake_case   , seed=[42, 0]  )
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				tokenizer.decode(output_ids[0]   , skip_special_tokens=__snake_case  )

											_SCREAMING_SNAKE_CASE			:	Tuple							=				"Today is a nice day and I’m going to go for a walk. I’"
											self.assertEqual(__snake_case   , __snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	str							=				TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B"""   , revision="""float16"""   , from_pt=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	Optional[int]							=				AutoTokenizer.from_pretrained("""EleutherAI/gpt-j-6B"""   , revision="""float16"""  )

											_SCREAMING_SNAKE_CASE			:	Any							=				"left"

											# Define PAD Token = EOS Token = 50256
											_SCREAMING_SNAKE_CASE			:	Tuple							=				tokenizer.eos_token
											_SCREAMING_SNAKE_CASE			:	Tuple							=				model.config.eos_token_id

											# use different length sentences to test batching
											_SCREAMING_SNAKE_CASE			:	str							=				[
											    "Hello, my dog is a little",
											    "Today, I",
											]
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				[
											    "Hello, my dog is a little over a year old and has been diagnosed with hip dysplasia",
											    "Today, I’m going to be talking about a topic that’",
											]
											return model, tokenizer, sentences, expected_output_sentences






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				self._get_beam_search_test_objects()

											_SCREAMING_SNAKE_CASE			:	List[Any]							=				tokenizer(__snake_case   , return_tensors="""tf"""   , padding=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	str							=				model.generate(**__snake_case   , do_sample=__snake_case   , num_beams=2  )
											_SCREAMING_SNAKE_CASE			:	Dict							=				tokenizer.batch_decode(__snake_case   , skip_special_tokens=__snake_case  )
											self.assertListEqual(__snake_case   , __snake_case  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Tuple							=				self._get_beam_search_test_objects()

											_SCREAMING_SNAKE_CASE			:	List[Any]							=				tokenizer(__snake_case   , return_tensors="""tf"""   , padding=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	int							=				tokenizer(sentences[0]   , return_tensors="""tf"""  )
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				model.generate(**__snake_case   , do_sample=__snake_case   , num_beams=2  )
											_SCREAMING_SNAKE_CASE			:	List[Any]							=				(
											    shape_list(inputs_non_padded["""input_ids"""]  )[-1]
											    - tf.reduce_sum(tf.cast(inputs["""attention_mask"""][-1]   , tf.intaa  )  ).numpy()
											)
											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				tokenizer(sentences[1]   , return_tensors="""tf"""  )
											_SCREAMING_SNAKE_CASE			:	Dict							=				model.generate(
											    **__snake_case   , do_sample=__snake_case   , num_beams=2   , max_length=model.config.max_length - num_paddings  )
											_SCREAMING_SNAKE_CASE			:	Dict							=				tokenizer.decode(output_non_padded[0]   , skip_special_tokens=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				tokenizer.decode(output_padded[0]   , skip_special_tokens=__snake_case  )
											self.assertListEqual(__snake_case   , [non_padded_sentence, padded_sentence]  )






							def UpperCAmelCase_     (   self  ):
											_SCREAMING_SNAKE_CASE			:	Union[str, Any]							=				self._get_beam_search_test_objects()

											_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				tokenizer(__snake_case   , return_tensors="""tf"""   , padding=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	Any							=				tf.function(model.generate   , jit_compile=__snake_case  )
											_SCREAMING_SNAKE_CASE			:	Tuple							=				xla_generate(**__snake_case   , do_sample=__snake_case   , num_beams=2  )
											_SCREAMING_SNAKE_CASE			:	Any							=				tokenizer.batch_decode(__snake_case   , skip_special_tokens=__snake_case  )
											self.assertListEqual(__snake_case   , __snake_case  )



'''simple docstring'''


import bsa
import requests







def       snake_case_       (  SCREAMING_SNAKE_CASE__					):







				"""simple docstring"""





				return {
				    "name": soup.ha.a.text,
				    "genre": soup.find("""span"""							,				class_="""genre"""					).text.strip(),
				    "rating": soup.strong.text,
				    "page_link": f"""https://www.imdb.com{soup.a.get("href"					)}""",
				}







def       snake_case_       (  SCREAMING_SNAKE_CASE__ = 5					):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE			:	List[str]							=				int(float(SCREAMING_SNAKE_CASE__					)					)
				if num_movies < 1:
								return ()
				_SCREAMING_SNAKE_CASE			:	Optional[Any]							=				(
				    """https://www.imdb.com/search/title?title_type="""
				    f"""feature&sort=num_votes,desc&count={num_movies}"""
				)
				_SCREAMING_SNAKE_CASE			:	Any							=				bsa.BeautifulSoup(requests.get(SCREAMING_SNAKE_CASE__					).content							,				"""html.parser"""					)
				return tuple(
				    get_movie_data_from_soup(SCREAMING_SNAKE_CASE__					)
				    for movie in source.find_all("""div"""							,				class_="""lister-item mode-advanced"""					)					)


if __name__ == "__main__":
				import json

				UpperCAmelCase_		:       List[str]							       =		int(input('How many movies would you like to see? '))
				print(
				    ', '.join(
				        json.dumps(movie, indent=4) for movie in get_imdb_top_movies(num_movies)
				    )
				)




