
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 							UpperCAmelCase   :




	'''simple docstring'''





	def __init__( self      :				int							,A      :				Tuple				):
					__A         =				parent
					__A         =				13
					__A         =				7
					__A         =				True
					__A         =				True
					__A         =				True
					__A         =				True
					__A         =				True
					__A         =				99
					__A         =				32
					__A         =				4
					__A         =				2
					__A         =				4
					__A         =				37
					__A         =				"gelu"
					__A         =				0.1
					__A         =				0.1
					__A         =				5_12
					__A         =				16
					__A         =				2
					__A         =				0.02
					__A         =				3
					__A         =				4
					__A         =				None
					__A         =				self.vocab_size - 1
					__A         =				self.vocab_size - 1
					__A         =				self.vocab_size - 1






	def        UpperCamelCase_						( self      :				Optional[Any]				):
					__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
					if self.use_mc_token_ids:
									__A         =				ids_tensor([self.batch_size, self.num_choices]							,self.seq_length				)

					__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         =				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=a_							,)

					__A         =				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      :				Optional[Any]							,A      :				Tuple							,A      :				Optional[Any]							,A      :				List[Any]							,A      :				Dict							,A      :				Optional[int]							,*A      :				Union[str, Any]				):
					__A         =				TFGPTJModel(config=a_				)
					__A         =				{
					    "input_ids": input_ids,
					    "attention_mask": input_mask,
					    "token_type_ids": token_type_ids,
					}
					__A         =				model(a_				)

					__A         =				[input_ids, None, input_mask]  # None is the input for 'past'
					__A         =				model(a_				)

					__A         =				model(a_				)

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






	def        UpperCamelCase_						( self      :				str							,A      :				str							,A      :				List[str]							,A      :				Optional[Any]							,A      :				str							,A      :				Optional[int]							,*A      :				List[str]				):
					__A         =				TFGPTJModel(config=a_				)

					# first forward pass
					__A         =				model(a_							,token_type_ids=a_							,use_cache=a_				)
					__A         =				model(a_							,token_type_ids=a_				)
					__A         =				model(a_							,token_type_ids=a_							,use_cache=a_				)

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

					__A    ,							__A         =				outputs.to_tuple()

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

					# append to next input_ids and token_type_ids
					__A         =				tf.concat([input_ids, next_tokens]							,axis=-1				)
					__A         =				tf.concat([token_type_ids, next_token_types]							,axis=-1				)

					__A         =				model(a_							,token_type_ids=a_				)["last_hidden_state"]
					__A         =				model(a_							,token_type_ids=a_							,past_key_values=a_				)[
					    "last_hidden_state"
					]

					# select random slice
					__A         =				int(ids_tensor((1,)							,shape_list(a_				)[-1]				)				)
					__A         =				output_from_no_past[:, -1, random_slice_idx]
					__A         =				output_from_past[:, 0, random_slice_idx]

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






	def        UpperCamelCase_						( self      :				Optional[int]							,A      :				str							,A      :				str							,A      :				Union[str, Any]							,A      :				Dict							,A      :				Optional[int]							,*A      :				str				):
					__A         =				TFGPTJModel(config=a_				)

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

					# first forward pass
					__A    ,							__A         =				model(a_							,attention_mask=a_				).to_tuple()

					# create hypothetical next token and extent to next_input_ids
					__A         =				ids_tensor((self.batch_size, 1)							,config.vocab_size				)

					# change a random masked slice from input_ids
					__A         =				ids_tensor((1,)							,a_				).numpy() + 1
					__A         =				ids_tensor((self.batch_size, self.seq_length)							,config.vocab_size				)
					__A         =				tf.range(self.seq_length				) == (self.seq_length - random_seq_idx_to_change)
					__A         =				tf.transpose(
					    tf.broadcast_to(tf.expand_dims(a_							,-1				)							,(self.seq_length, self.batch_size)				)				)
					__A         =				tf.where(a_							,a_							,a_				)

					# append to next input_ids and attn_mask
					__A         =				tf.concat([input_ids, next_tokens]							,axis=-1				)
					__A         =				tf.concat([attn_mask, tf.ones((shape_list(a_				)[0], 1)							,dtype=tf.intaa				)]							,axis=1				)

					# get two different outputs
					__A         =				model(a_							,attention_mask=a_				)["last_hidden_state"]
					__A         =				model(a_							,past_key_values=a_							,attention_mask=a_				)[
					    "last_hidden_state"
					]

					# select random slice
					__A         =				int(ids_tensor((1,)							,shape_list(a_				)[-1]				)				)
					__A         =				output_from_no_past[:, -1, random_slice_idx]
					__A         =				output_from_past[:, 0, random_slice_idx]

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






	def        UpperCamelCase_						( self      :				Union[str, Any]							,A      :				List[str]							,A      :				List[str]							,A      :				Optional[int]							,A      :				int							,A      :				Tuple							,*A      :				int				):
					__A         =				TFGPTJModel(config=a_				)

					__A         =				input_ids[:1, :]
					__A         =				input_mask[:1, :]
					__A         =				token_type_ids[:1, :]
					__A         =				1

					# first forward pass
					__A         =				model(a_							,attention_mask=a_							,token_type_ids=a_							,use_cache=a_				)

					__A    ,							__A         =				outputs.to_tuple()

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

					# append to next input_ids and token_type_ids
					__A         =				tf.concat([input_ids, next_tokens]							,axis=-1				)
					__A         =				tf.concat([input_mask, next_attn_mask]							,axis=-1				)
					__A         =				tf.concat([token_type_ids, next_token_types]							,axis=-1				)

					__A         =				model(
					    a_							,token_type_ids=a_							,attention_mask=a_				)["last_hidden_state"]
					__A         =				model(
					    a_							,token_type_ids=a_							,attention_mask=a_							,past_key_values=a_							,)["last_hidden_state"]
					self.parent.assertTrue(output_from_past.shape[1] == next_tokens.shape[1]				)

					# select random slice
					__A         =				int(ids_tensor((1,)							,shape_list(a_				)[-1]				)				)
					__A         =				output_from_no_past[:, -3:, random_slice_idx]
					__A         =				output_from_past[:, :, random_slice_idx]

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






	def        UpperCamelCase_						( self      :				Optional[int]							,A      :				Optional[int]							,A      :				Dict							,A      :				str							,A      :				Optional[int]							,A      :				List[str]							,*A      :				List[Any]				):
					__A         =				TFGPTJForCausalLM(config=a_				)
					__A         =				{
					    "input_ids": input_ids,
					    "attention_mask": input_mask,
					    "token_type_ids": token_type_ids,
					}
					__A         =				model(a_				)
					self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)				)







	def        UpperCamelCase_						( self      :				List[str]				):
					__A         =				self.prepare_config_and_inputs()

					(
					    (
					    __A      
					)    ,							(
					    __A      
					)    ,							(
					    __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_tf
class 							UpperCAmelCase   (						__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    unittest.TestCase						):




	'''simple docstring'''





	snake_case_      					=  (
	    (TFGPTJForCausalLM, TFGPTJForSequenceClassification, TFGPTJForQuestionAnswering, TFGPTJModel)
	    if is_tf_available()
	    else ()
	)

	snake_case_      					=  (TFGPTJForCausalLM,) if is_tf_available() else ()
	snake_case_      					=  (
	    {
	        '''feature-extraction''': TFGPTJModel,
	        '''question-answering''': TFGPTJForQuestionAnswering,
	        '''text-classification''': TFGPTJForSequenceClassification,
	        '''text-generation''': TFGPTJForCausalLM,
	        '''zero-shot''': TFGPTJForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	snake_case_      					=  False
	snake_case_      					=  False
	snake_case_      					=  False
	snake_case_      					=  False






	def        UpperCamelCase_						( self      :				str							,A      :				Dict							,A      :				Any							,A      :				Union[str, Any]							,A      :				Dict							,A      :				Optional[Any]				):
					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      :				Dict				):
					__A         =				TFGPTJModelTester(self				)
					__A         =				ConfigTester(self							,config_class=a_							,n_embd=37				)






	def        UpperCamelCase_						( self      :				Union[str, Any]				):
					self.config_tester.run_common_tests()






	def        UpperCamelCase_						( self      :				Tuple				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_gptj_model(*a_				)






	def        UpperCamelCase_						( self      :				List[Any]				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_gptj_model_past(*a_				)






	def        UpperCamelCase_						( self      :				Optional[Any]				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_gptj_model_attention_mask_past(*a_				)






	def        UpperCamelCase_						( self      :				Optional[int]				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_gptj_model_past_large_inputs(*a_				)






	def        UpperCamelCase_						( self      :				str				):
					__A         =				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_gptj_lm_head_model(*a_				)






	@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      :				Union[str, Any]				):
					__A         =				TFGPTJModel.from_pretrained("EleutherAI/gpt-j-6B"							,from_pt=a_				)
					self.assertIsNotNone(a_				)







	@unittest.skip(reason="Currently, model embeddings are going to undergo a major refactor."				)
	def        UpperCamelCase_						( self      :				int				):
					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 							UpperCAmelCase   (						unittest.TestCase						):




	'''simple docstring'''





	def        UpperCamelCase_						( self      :				int				):
					__A         =				TFGPTJForCausalLM.from_pretrained("EleutherAI/gpt-j-6B"							,from_pt=a_				)
					__A         =				tf.convert_to_tensor([[4_64, 32_90]]							,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
					__A         =				[4_64, 32_90, 3_18, 2_57, 5_82, 3_38, 12_66, 15_45, 13, 6_32, 3_18, 2_57, 91_12, 1_51_85, 11, 2_90, 3_40, 3_18, 2_57, 15_45]
					# fmt: on
					__A         =				model.generate(a_							,do_sample=a_				)
					self.assertListEqual(output_ids[0].numpy().tolist()							,a_				)






	def        UpperCamelCase_						( self      :				Any				):
					__A         =				AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B"							,revision="float16"				)
					__A         =				TFGPTJForCausalLM.from_pretrained("EleutherAI/gpt-j-6B"							,revision="float16"							,from_pt=a_				)

					__A         =				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"				):
									__A         =				model.generate(**a_							,do_sample=a_							,seed=[42, 0]				)
					__A         =				tokenizer.decode(output_ids[0]							,skip_special_tokens=a_				)

					__A         =				"Today is a nice day and I’m going to go for a walk. I’"
					self.assertEqual(a_							,a_				)






	def        UpperCamelCase_						( self      :				Optional[int]				):
					__A         =				TFGPTJForCausalLM.from_pretrained("EleutherAI/gpt-j-6B"							,revision="float16"							,from_pt=a_				)
					__A         =				AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B"							,revision="float16"				)

					__A         =				"left"

					# Define PAD Token = EOS Token = 50256
					__A         =				tokenizer.eos_token
					__A         =				model.config.eos_token_id

					# use different length sentences to test batching
					__A         =				[
					    "Hello, my dog is a little",
					    "Today, I",
					]
					__A         =				[
					    "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      :				str				):
					# Confirms that we get the expected results with left-padded beam search
					__A    ,							__A    ,							__A    ,							__A         =				self._get_beam_search_test_objects()

					__A         =				tokenizer(a_							,return_tensors="tf"							,padding=a_				)
					__A         =				model.generate(**a_							,do_sample=a_							,num_beams=2				)
					__A         =				tokenizer.batch_decode(a_							,skip_special_tokens=a_				)
					self.assertListEqual(a_							,a_				)






	def        UpperCamelCase_						( self      :				Any				):
					# Confirms that left-padding is working properly
					__A    ,							__A    ,							__A    ,							__A         =				self._get_beam_search_test_objects()

					__A         =				tokenizer(a_							,return_tensors="tf"							,padding=a_				)
					__A         =				tokenizer(sentences[0]							,return_tensors="tf"				)
					__A         =				model.generate(**a_							,do_sample=a_							,num_beams=2				)
					__A         =				(
					    shape_list(inputs_non_padded["input_ids"]				)[-1]
					    - tf.reduce_sum(tf.cast(inputs["attention_mask"][-1]							,tf.intaa				)				).numpy()
					)
					__A         =				tokenizer(sentences[1]							,return_tensors="tf"				)
					__A         =				model.generate(
					    **a_							,do_sample=a_							,num_beams=2							,max_length=model.config.max_length - num_paddings				)
					__A         =				tokenizer.decode(output_non_padded[0]							,skip_special_tokens=a_				)
					__A         =				tokenizer.decode(output_padded[0]							,skip_special_tokens=a_				)
					self.assertListEqual(a_							,[non_padded_sentence, padded_sentence]				)







	def        UpperCamelCase_						( self      :				Tuple				):
					# Confirms that XLA is working properly
					__A    ,							__A    ,							__A    ,							__A         =				self._get_beam_search_test_objects()

					__A         =				tokenizer(a_							,return_tensors="tf"							,padding=a_				)
					__A         =				tf.function(model.generate							,jit_compile=a_				)
					__A         =				xla_generate(**a_							,do_sample=a_							,num_beams=2				)
					__A         =				tokenizer.batch_decode(a_							,skip_special_tokens=a_				)
					self.assertListEqual(a_							,a_				)


from datetime import date

import requests

SCREAMING_SNAKE_CASE							:str											=       'https://api.carbonintensity.org.uk/intensity'


def 		UpperCAmelCase    (			)	->			str:







				"""simple docstring"""



				__A         =				requests.get(a_		).json()["data"][0]
				return last_half_hour["intensity"]["actual"]


def 		UpperCAmelCase    (			a_	,     a_		)	->			list:







				"""simple docstring"""



				return requests.get(F'''{BASE_URL}/{start}/{end}'''		).json()["data"]


if __name__ == "__main__":
					for entry in fetch_from_to(start=date(2020, 10, 1), end=date(2020, 10, 3)):
										print('from {from} to {to}: {intensity[actual]}'.format(**entry))
					print(f'''{fetch_last_half_hour() = }''')
