"""simple docstring"""







import inspect
import unittest

from transformers import is_torch_available, is_vision_available
from transformers.models.auto import get_values
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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import MODEL_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
						from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import PoolFormerImageProcessor



class 			_lowerCamelCase      (__lowerCamelCase  ):







				def 				__UpperCAmelCase       (			self     :							List[Any]    ):

					"""simple docstring"""






					_lowercase			:     int      					=				self.config_class(**self.inputs_dict    )
					self.parent.assertTrue(hasattr(lowerCamelCase_      ,       'hidden_sizes'    )    )
					self.parent.assertTrue(hasattr(lowerCamelCase_      ,       'num_encoder_blocks'    )    )



class 			_lowerCamelCase      :







				def __init__(			self     :							Optional[Any]      ,       lowerCamelCase_     :							str      ,       lowerCamelCase_     :							Optional[Any]=1_3      ,       lowerCamelCase_     :							Union[str, Any]=6_4      ,       lowerCamelCase_     :							Dict=3      ,       lowerCamelCase_     :							List[Any]=4      ,       lowerCamelCase_     :							int=[2, 2, 2, 2]      ,       lowerCamelCase_     :							Optional[Any]=[8, 4, 2, 1]      ,       lowerCamelCase_     :							Union[str, Any]=[1_6, 3_2, 6_4, 1_2_8]      ,       lowerCamelCase_     :							List[str]=[1, 4, 8, 1_6]      ,       lowerCamelCase_     :							List[str]=False      ,       lowerCamelCase_     :							int=True      ,       lowerCamelCase_     :							Optional[Any]="gelu"      ,       lowerCamelCase_     :							Optional[int]=0.1      ,       lowerCamelCase_     :							List[str]=0.02      ,       lowerCamelCase_     :							List[Any]=3      ,       lowerCamelCase_     :							List[str]=None      ,       ):

					"""simple docstring"""






					_lowercase			:     List[str]      					=				parent
					_lowercase			:     List[Any]      					=				batch_size
					_lowercase			:     Tuple      					=				image_size
					_lowercase			:     Union[str, Any]      					=				num_channels
					_lowercase			:     str      					=				num_encoder_blocks
					_lowercase			:     Tuple      					=				sr_ratios
					_lowercase			:     Union[str, Any]      					=				depths
					_lowercase			:     str      					=				hidden_sizes
					_lowercase			:     int      					=				downsampling_rates
					_lowercase			:     Optional[Any]      					=				is_training
					_lowercase			:     List[str]      					=				use_labels
					_lowercase			:     Union[str, Any]      					=				hidden_act
					_lowercase			:     str      					=				hidden_dropout_prob
					_lowercase			:     Optional[int]      					=				initializer_range
					_lowercase			:     int      					=				num_labels
					_lowercase			:     Optional[int]      					=				scope







				def 				__UpperCAmelCase       (			self     :							List[str]    ):

					"""simple docstring"""






					_lowercase			:     List[Any]      					=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

					_lowercase			:     Any      					=				None
					if self.use_labels:
						_lowercase			:     Optional[Any]      					=				ids_tensor([self.batch_size, self.image_size, self.image_size]      ,       self.num_labels    )

					_lowercase			:     str      					=				PoolFormerConfig(
					    image_size=self.image_size      ,       num_channels=self.num_channels      ,       num_encoder_blocks=self.num_encoder_blocks      ,       depths=self.depths      ,       hidden_sizes=self.hidden_sizes      ,       hidden_act=self.hidden_act      ,       hidden_dropout_prob=self.hidden_dropout_prob      ,       initializer_range=self.initializer_range      ,       )

					return config, pixel_values, labels







				def 				__UpperCAmelCase       (			self     :							Union[str, Any]      ,       lowerCamelCase_     :							Union[str, Any]      ,       lowerCamelCase_     :							str      ,       lowerCamelCase_     :							Optional[Any]    ):

					"""simple docstring"""






					_lowercase			:     int      					=				PoolFormerModel(config=lowerCamelCase_    )
					model.to(lowerCamelCase_    )
					model.eval()
					_lowercase			:     Any      					=				model(lowerCamelCase_    )
					_lowercase			:     Optional[int]      					=				self.image_size // 32.0
					self.parent.assertEqual(
					    result.last_hidden_state.shape      ,       (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)    )







				def 				__UpperCAmelCase       (			self     :							Tuple    ):

					"""simple docstring"""






					_lowercase			:     Any      					=				self.prepare_config_and_inputs()
					_lowercase					,   _lowercase					,   _lowercase			:     Union[str, Any]      					=				config_and_inputs
					_lowercase			:     int      					=				{'pixel_values': pixel_values}
					return config, inputs_dict



@require_torch
class 			_lowerCamelCase      (__lowerCamelCase    ,				__lowerCamelCase    ,				unittest.TestCase  ):
				_snake_case  =	(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
				_snake_case  =	(
				    {"feature-extraction": PoolFormerModel, "image-classification": PoolFormerForImageClassification}
				    if is_torch_available()
				    else {}
				)

				_snake_case  =	False
				_snake_case  =	False
				_snake_case  =	False
				_snake_case  =	False
				_snake_case  =	False







				def 				__UpperCAmelCase       (			self     :							Any    ):

					"""simple docstring"""






					_lowercase			:     Union[str, Any]      					=				PoolFormerModelTester(self    )
					_lowercase			:     int      					=				PoolFormerConfigTester(self      ,       config_class=lowerCamelCase_    )







				def 				__UpperCAmelCase       (			self     :							Dict    ):

					"""simple docstring"""






					self.config_tester.run_common_tests()







				def 				__UpperCAmelCase       (			self     :							int    ):

					"""simple docstring"""






					_lowercase			:     List[str]      					=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowerCamelCase_    )







				@unittest.skip('PoolFormer does not use inputs_embeds'    )
				def 				__UpperCAmelCase       (			self     :							List[Any]    ):

					"""simple docstring"""






					pass







				@unittest.skip('PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'    )
				def 				__UpperCAmelCase       (			self     :							Dict    ):

					"""simple docstring"""






					pass







				def 				__UpperCAmelCase       (			self     :							List[str]    ):

					"""simple docstring"""






					def check_hidden_states_output(lowerCamelCase_     :							int      ,       lowerCamelCase_     :							Union[str, Any]      ,       lowerCamelCase_     :							Optional[Any]    ):
						_lowercase			:     Tuple      					=				model_class(lowerCamelCase_    )
						model.to(lowerCamelCase_    )
						model.eval()

						with torch.no_grad():
							_lowercase			:     Any      					=				model(**self._prepare_for_class(lowerCamelCase_      ,       lowerCamelCase_    )    )

						_lowercase			:     str      					=				outputs.hidden_states

						_lowercase			:     List[Any]      					=				self.model_tester.num_encoder_blocks
						self.assertEqual(len(lowerCamelCase_    )      ,       lowerCamelCase_    )

						# verify the first hidden states (first block)
						self.assertListEqual(
						    list(hidden_states[0].shape[-3:]    )      ,       [
						        self.model_tester.hidden_sizes[0],
						        self.model_tester.image_size // 4,
						        self.model_tester.image_size // 4,
						    ]      ,       )

					_lowercase					,   _lowercase			:     Optional[int]      					=				self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						_lowercase			:     Any      					=				True
						check_hidden_states_output(lowerCamelCase_      ,       lowerCamelCase_      ,       lowerCamelCase_    )

						# check that output_hidden_states also work using config
						del inputs_dict["output_hidden_states"]
						_lowercase			:     Union[str, Any]      					=				True

						check_hidden_states_output(lowerCamelCase_      ,       lowerCamelCase_      ,       lowerCamelCase_    )







				def 				__UpperCAmelCase       (			self     :							Optional[Any]    ):

					"""simple docstring"""






					if not self.model_tester.is_training:
						return

					_lowercase					,   _lowercase			:     Optional[int]      					=				self.model_tester.prepare_config_and_inputs_for_common()
					_lowercase			:     List[Any]      					=				True

					for model_class in self.all_model_classes:
						if model_class in get_values(lowerCamelCase_    ):
							continue
						_lowercase			:     Any      					=				model_class(lowerCamelCase_    )
						model.to(lowerCamelCase_    )
						model.train()
						_lowercase			:     Union[str, Any]      					=				self._prepare_for_class(lowerCamelCase_      ,       lowerCamelCase_      ,       return_labels=lowerCamelCase_    )
						_lowercase			:     List[str]      					=				model(**lowerCamelCase_    ).loss
						loss.backward()







				def 				__UpperCAmelCase       (			self     :							List[Any]    ):

					"""simple docstring"""






					_lowercase					,   _lowercase			:     Any      					=				self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						_lowercase			:     Optional[int]      					=				model_class(lowerCamelCase_    )
						_lowercase			:     Optional[Any]      					=				inspect.signature(model.forward    )
						# signature.parameters is an OrderedDict => so arg_names order is deterministic
						_lowercase			:     Dict      					=				[*signature.parameters.keys()]

						_lowercase			:     Any      					=				['pixel_values']
						self.assertListEqual(arg_names[:1]      ,       lowerCamelCase_    )







				@slow
				def 				__UpperCAmelCase       (			self     :							Any    ):

					"""simple docstring"""






					for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						_lowercase			:     Dict      					=				PoolFormerModel.from_pretrained(lowerCamelCase_    )
						self.assertIsNotNone(lowerCamelCase_    )



def   __lowerCAmelCase(		):






	"""simple docstring"""





	_lowercase			:     Any      					=				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
	return image







@require_torch
class 			_lowerCamelCase      (unittest.TestCase  ):







				@slow
				def 				__UpperCAmelCase       (			self     :							int    ):

					"""simple docstring"""






					_lowercase			:     Tuple      					=				PoolFormerImageProcessor()
					_lowercase			:     List[str]      					=				PoolFormerForImageClassification.from_pretrained('sail/poolformer_s12'    ).to(lowerCamelCase_    )

					_lowercase			:     Any      					=				image_processor(images=prepare_img()      ,       return_tensors='pt'    ).to(lowerCamelCase_    )

					# forward pass
					with torch.no_grad():
						_lowercase			:     List[str]      					=				model(**lowerCamelCase_    )

					# verify the logits
					_lowercase			:     Optional[int]      					=				torch.Size((1, 1_0_0_0)    )
					self.assertEqual(outputs.logits.shape      ,       lowerCamelCase_    )

					_lowercase			:     Optional[int]      					=				torch.tensor([-0.6113, 0.1685, -0.0492]    ).to(lowerCamelCase_    )
					self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,       lowerCamelCase_      ,       atol=1E-4    )    )




"""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 			_lowerCamelCase      :







				def __init__(			self     :							Dict      ,       lowerCamelCase_     :							Union[str, Any]    ):

					"""simple docstring"""






					_lowercase			:     Optional[int]      					=				parent
					_lowercase			:     Tuple      					=				1_3
					_lowercase			:     List[str]      					=				7
					_lowercase			:     int      					=				True
					_lowercase			:     int      					=				True
					_lowercase			:     List[str]      					=				True
					_lowercase			:     Dict      					=				True
					_lowercase			:     Dict      					=				True
					_lowercase			:     Tuple      					=				9_9
					_lowercase			:     List[Any]      					=				3_2
					_lowercase			:     Tuple      					=				4
					_lowercase			:     Dict      					=				2
					_lowercase			:     Optional[Any]      					=				4
					_lowercase			:     List[str]      					=				3_7
					_lowercase			:     int      					=				'gelu'
					_lowercase			:     Dict      					=				0.1
					_lowercase			:     Tuple      					=				0.1
					_lowercase			:     Tuple      					=				5_1_2
					_lowercase			:     Optional[Any]      					=				1_6
					_lowercase			:     str      					=				2
					_lowercase			:     Tuple      					=				0.02
					_lowercase			:     Optional[int]      					=				3
					_lowercase			:     Any      					=				4
					_lowercase			:     Any      					=				None
					_lowercase			:     str      					=				self.vocab_size - 1
					_lowercase			:     str      					=				self.vocab_size - 1
					_lowercase			:     Optional[int]      					=				self.vocab_size - 1







				def 				__UpperCAmelCase       (			self     :							List[str]    ):

					"""simple docstring"""






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

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

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

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

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

					_lowercase			:     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=lowerCamelCase_      ,       )

					_lowercase			:     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     :							Optional[int]      ,       lowerCamelCase_     :							Tuple      ,       lowerCamelCase_     :							int      ,       lowerCamelCase_     :							int      ,       lowerCamelCase_     :							Any      ,       lowerCamelCase_     :							List[str]      ,       *lowerCamelCase_     :							int    ):

					"""simple docstring"""






					_lowercase			:     Dict      					=				TFGPTJModel(config=lowerCamelCase_    )
					_lowercase			:     int      					=				{
					    'input_ids': input_ids,
					    'attention_mask': input_mask,
					    'token_type_ids': token_type_ids,
					}
					_lowercase			:     Optional[Any]      					=				model(lowerCamelCase_    )

					_lowercase			:     Any      					=				[input_ids, None, input_mask]  # None is the input for 'past'
					_lowercase			:     Union[str, Any]      					=				model(lowerCamelCase_    )

					_lowercase			:     Optional[int]      					=				model(lowerCamelCase_    )

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







				def 				__UpperCAmelCase       (			self     :							Optional[Any]      ,       lowerCamelCase_     :							List[Any]      ,       lowerCamelCase_     :							List[Any]      ,       lowerCamelCase_     :							Optional[Any]      ,       lowerCamelCase_     :							Tuple      ,       lowerCamelCase_     :							Union[str, Any]      ,       *lowerCamelCase_     :							int    ):

					"""simple docstring"""






					_lowercase			:     Union[str, Any]      					=				TFGPTJModel(config=lowerCamelCase_    )

					# first forward pass
					_lowercase			:     str      					=				model(lowerCamelCase_      ,       token_type_ids=lowerCamelCase_      ,       use_cache=lowerCamelCase_    )
					_lowercase			:     List[Any]      					=				model(lowerCamelCase_      ,       token_type_ids=lowerCamelCase_    )
					_lowercase			:     int      					=				model(lowerCamelCase_      ,       token_type_ids=lowerCamelCase_      ,       use_cache=lowerCamelCase_    )

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

					_lowercase					,   _lowercase			:     Any      					=				outputs.to_tuple()

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

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

					_lowercase			:     List[Any]      					=				model(lowerCamelCase_      ,       token_type_ids=lowerCamelCase_    )['last_hidden_state']
					_lowercase			:     Dict      					=				model(lowerCamelCase_      ,       token_type_ids=lowerCamelCase_      ,       past_key_values=lowerCamelCase_    )[
					    'last_hidden_state'
					]

					# select random slice
					_lowercase			:     Dict      					=				int(ids_tensor((1,)      ,       shape_list(lowerCamelCase_    )[-1]    )    )
					_lowercase			:     Optional[Any]      					=				output_from_no_past[:, -1, random_slice_idx]
					_lowercase			:     Tuple      					=				output_from_past[:, 0, random_slice_idx]

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







				def 				__UpperCAmelCase       (			self     :							Tuple      ,       lowerCamelCase_     :							Optional[int]      ,       lowerCamelCase_     :							List[str]      ,       lowerCamelCase_     :							int      ,       lowerCamelCase_     :							int      ,       lowerCamelCase_     :							Optional[int]      ,       *lowerCamelCase_     :							str    ):

					"""simple docstring"""






					_lowercase			:     Tuple      					=				TFGPTJModel(config=lowerCamelCase_    )

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

					# first forward pass
					_lowercase					,   _lowercase			:     str      					=				model(lowerCamelCase_      ,       attention_mask=lowerCamelCase_    ).to_tuple()

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

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

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

					# get two different outputs
					_lowercase			:     str      					=				model(lowerCamelCase_      ,       attention_mask=lowerCamelCase_    )['last_hidden_state']
					_lowercase			:     List[str]      					=				model(lowerCamelCase_      ,       past_key_values=lowerCamelCase_      ,       attention_mask=lowerCamelCase_    )[
					    'last_hidden_state'
					]

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

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







				def 				__UpperCAmelCase       (			self     :							Optional[int]      ,       lowerCamelCase_     :							int      ,       lowerCamelCase_     :							str      ,       lowerCamelCase_     :							Union[str, Any]      ,       lowerCamelCase_     :							Union[str, Any]      ,       lowerCamelCase_     :							Optional[int]      ,       *lowerCamelCase_     :							int    ):

					"""simple docstring"""






					_lowercase			:     Tuple      					=				TFGPTJModel(config=lowerCamelCase_    )

					_lowercase			:     Dict      					=				input_ids[:1, :]
					_lowercase			:     List[str]      					=				input_mask[:1, :]
					_lowercase			:     Optional[Any]      					=				token_type_ids[:1, :]
					_lowercase			:     int      					=				1

					# first forward pass
					_lowercase			:     int      					=				model(lowerCamelCase_      ,       attention_mask=lowerCamelCase_      ,       token_type_ids=lowerCamelCase_      ,       use_cache=lowerCamelCase_    )

					_lowercase					,   _lowercase			:     Any      					=				outputs.to_tuple()

					# create hypothetical next token and extent to next_input_ids
					_lowercase			:     Union[str, Any]      					=				ids_tensor((self.batch_size, 3)      ,       config.vocab_size    )
					_lowercase			:     Union[str, Any]      					=				ids_tensor((self.batch_size, 3)      ,       2    )
					_lowercase			:     Any      					=				ids_tensor((self.batch_size, 3)      ,       self.type_vocab_size    )

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

					_lowercase			:     str      					=				model(
					    lowerCamelCase_      ,       token_type_ids=lowerCamelCase_      ,       attention_mask=lowerCamelCase_    )['last_hidden_state']
					_lowercase			:     List[Any]      					=				model(
					    lowerCamelCase_      ,       token_type_ids=lowerCamelCase_      ,       attention_mask=lowerCamelCase_      ,       past_key_values=lowerCamelCase_      ,       )['last_hidden_state']
					self.parent.assertTrue(output_from_past.shape[1] == next_tokens.shape[1]    )

					# select random slice
					_lowercase			:     Tuple      					=				int(ids_tensor((1,)      ,       shape_list(lowerCamelCase_    )[-1]    )    )
					_lowercase			:     List[str]      					=				output_from_no_past[:, -3:, random_slice_idx]
					_lowercase			:     Optional[Any]      					=				output_from_past[:, :, random_slice_idx]

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







				def 				__UpperCAmelCase       (			self     :							List[Any]      ,       lowerCamelCase_     :							Optional[Any]      ,       lowerCamelCase_     :							Optional[Any]      ,       lowerCamelCase_     :							str      ,       lowerCamelCase_     :							int      ,       lowerCamelCase_     :							Dict      ,       *lowerCamelCase_     :							List[Any]    ):

					"""simple docstring"""






					_lowercase			:     Tuple      					=				TFGPTJForCausalLM(config=lowerCamelCase_    )
					_lowercase			:     Optional[int]      					=				{
					    'input_ids': input_ids,
					    'attention_mask': input_mask,
					    'token_type_ids': token_type_ids,
					}
					_lowercase			:     Optional[int]      					=				model(lowerCamelCase_    )
					self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size)    )







				def 				__UpperCAmelCase       (			self     :							Any    ):

					"""simple docstring"""






					_lowercase			:     int      					=				self.prepare_config_and_inputs()

					(
					    (
					    _lowercase
					)					,   (
					    _lowercase
					)					,   (
					    _lowercase
					)					,   (
					    _lowercase
					)					,   (
					    _lowercase
					)					,   (
					    _lowercase
					)					,   (
					    _lowercase
					)					,   (
					    _lowercase
					)					,   (
					    _lowercase
					)					,   
					)			:     str      					=				config_and_inputs

					_lowercase			:     Tuple      					=				{
					    'input_ids': input_ids,
					    'token_type_ids': token_type_ids,
					    'attention_mask': input_mask,
					}
					return config, inputs_dict



@require_tf
class 			_lowerCamelCase      (__lowerCamelCase    ,				__lowerCamelCase    ,				__lowerCamelCase    ,				unittest.TestCase  ):
				_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     :							Optional[Any]      ,       lowerCamelCase_     :							Union[str, Any]      ,       lowerCamelCase_     :							Any      ,       lowerCamelCase_     :							Optional[int]      ,       lowerCamelCase_     :							str      ,       lowerCamelCase_     :							str    ):

					"""simple docstring"""






					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     :							str    ):

					"""simple docstring"""






					_lowercase			:     str      					=				TFGPTJModelTester(self    )
					_lowercase			:     List[Any]      					=				ConfigTester(self      ,       config_class=lowerCamelCase_      ,       n_embd=3_7    )







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

					"""simple docstring"""






					self.config_tester.run_common_tests()







				def 				__UpperCAmelCase       (			self     :							Optional[Any]    ):

					"""simple docstring"""






					_lowercase			:     Union[str, Any]      					=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_gptj_model(*lowerCamelCase_    )







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

					"""simple docstring"""






					_lowercase			:     str      					=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_gptj_model_past(*lowerCamelCase_    )







				def 				__UpperCAmelCase       (			self     :							Optional[int]    ):

					"""simple docstring"""






					_lowercase			:     str      					=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_gptj_model_attention_mask_past(*lowerCamelCase_    )







				def 				__UpperCAmelCase       (			self     :							Optional[int]    ):

					"""simple docstring"""






					_lowercase			:     str      					=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_gptj_model_past_large_inputs(*lowerCamelCase_    )







				def 				__UpperCAmelCase       (			self     :							Tuple    ):

					"""simple docstring"""






					_lowercase			:     Union[str, Any]      					=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_gptj_lm_head_model(*lowerCamelCase_    )







				@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     :							Any    ):

					"""simple docstring"""






					_lowercase			:     Optional[int]      					=				TFGPTJModel.from_pretrained('EleutherAI/gpt-j-6B'      ,       from_pt=lowerCamelCase_    )
					self.assertIsNotNone(lowerCamelCase_    )







				@unittest.skip(reason='Currently, model embeddings are going to undergo a major refactor.'    )
				def 				__UpperCAmelCase       (			self     :							Optional[int]    ):

					"""simple docstring"""






					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 			_lowerCamelCase      (unittest.TestCase  ):







				def 				__UpperCAmelCase       (			self     :							List[str]    ):

					"""simple docstring"""






					_lowercase			:     int      					=				TFGPTJForCausalLM.from_pretrained('EleutherAI/gpt-j-6B'      ,       from_pt=lowerCamelCase_    )
					_lowercase			:     Any      					=				tf.convert_to_tensor([[4_6_4, 3_2_9_0]]      ,       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
					_lowercase			:     Any      					=				[4_6_4, 3_2_9_0, 3_1_8, 2_5_7, 5_8_2, 3_3_8, 1_2_6_6, 1_5_4_5, 1_3, 6_3_2, 3_1_8, 2_5_7, 9_1_1_2, 1_5_1_8_5, 1_1, 2_9_0, 3_4_0, 3_1_8, 2_5_7, 1_5_4_5]
					# fmt: on
					_lowercase			:     Any      					=				model.generate(lowerCamelCase_      ,       do_sample=lowerCamelCase_    )
					self.assertListEqual(output_ids[0].numpy().tolist()      ,       lowerCamelCase_    )







				def 				__UpperCAmelCase       (			self     :							str    ):

					"""simple docstring"""






					_lowercase			:     int      					=				AutoTokenizer.from_pretrained('EleutherAI/gpt-j-6B'      ,       revision='float16'    )
					_lowercase			:     int      					=				TFGPTJForCausalLM.from_pretrained('EleutherAI/gpt-j-6B'      ,       revision='float16'      ,       from_pt=lowerCamelCase_    )

					_lowercase			:     List[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'    ):
						_lowercase			:     str      					=				model.generate(**lowerCamelCase_      ,       do_sample=lowerCamelCase_      ,       seed=[4_2, 0]    )
					_lowercase			:     Optional[int]      					=				tokenizer.decode(output_ids[0]      ,       skip_special_tokens=lowerCamelCase_    )

					_lowercase			:     Tuple      					=				'Today is a nice day and I’m going to go for a walk. I’'
					self.assertEqual(lowerCamelCase_      ,       lowerCamelCase_    )







				def 				__UpperCAmelCase       (			self     :							Optional[int]    ):

					"""simple docstring"""






					_lowercase			:     List[Any]      					=				TFGPTJForCausalLM.from_pretrained('EleutherAI/gpt-j-6B'      ,       revision='float16'      ,       from_pt=lowerCamelCase_    )
					_lowercase			:     Dict      					=				AutoTokenizer.from_pretrained('EleutherAI/gpt-j-6B'      ,       revision='float16'    )

					_lowercase			:     List[str]      					=				'left'

					# Define PAD Token = EOS Token = 50256
					_lowercase			:     List[str]      					=				tokenizer.eos_token
					_lowercase			:     Any      					=				model.config.eos_token_id

					# use different length sentences to test batching
					_lowercase			:     Optional[Any]      					=				[
					    'Hello, my dog is a little',
					    'Today, I',
					]
					_lowercase			:     List[str]      					=				[
					    '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     :							Any    ):

					"""simple docstring"""






					_lowercase					,   _lowercase					,   _lowercase					,   _lowercase			:     List[str]      					=				self._get_beam_search_test_objects()

					_lowercase			:     Optional[Any]      					=				tokenizer(lowerCamelCase_      ,       return_tensors='tf'      ,       padding=lowerCamelCase_    )
					_lowercase			:     Optional[Any]      					=				model.generate(**lowerCamelCase_      ,       do_sample=lowerCamelCase_      ,       num_beams=2    )
					_lowercase			:     List[Any]      					=				tokenizer.batch_decode(lowerCamelCase_      ,       skip_special_tokens=lowerCamelCase_    )
					self.assertListEqual(lowerCamelCase_      ,       lowerCamelCase_    )







				def 				__UpperCAmelCase       (			self     :							Optional[Any]    ):

					"""simple docstring"""






					_lowercase					,   _lowercase					,   _lowercase					,   _lowercase			:     List[Any]      					=				self._get_beam_search_test_objects()

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







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

					"""simple docstring"""






					_lowercase					,   _lowercase					,   _lowercase					,   _lowercase			:     int      					=				self._get_beam_search_test_objects()

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



