"""simple docstring"""







import inspect
import unittest

from transformers import DPTConfig
from transformers.file_utils import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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


if is_torch_available():
						import torch
						from torch import nn

						from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel
						from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import DPTImageProcessor



class 			_lowerCamelCase      :







				def __init__(			self     :							Optional[Any]      ,       lowerCamelCase_     :							Optional[Any]      ,       lowerCamelCase_     :							int=2      ,       lowerCamelCase_     :							Tuple=3_2      ,       lowerCamelCase_     :							Optional[int]=1_6      ,       lowerCamelCase_     :							Optional[Any]=3      ,       lowerCamelCase_     :							str=True      ,       lowerCamelCase_     :							str=True      ,       lowerCamelCase_     :							Tuple=3_2      ,       lowerCamelCase_     :							Dict=4      ,       lowerCamelCase_     :							int=[0, 1, 2, 3]      ,       lowerCamelCase_     :							Any=4      ,       lowerCamelCase_     :							Optional[Any]=3_7      ,       lowerCamelCase_     :							List[Any]="gelu"      ,       lowerCamelCase_     :							Optional[int]=0.1      ,       lowerCamelCase_     :							Tuple=0.1      ,       lowerCamelCase_     :							Optional[Any]=0.02      ,       lowerCamelCase_     :							Optional[Any]=3      ,       lowerCamelCase_     :							Optional[Any]=False      ,       lowerCamelCase_     :							Optional[Any]=None      ,       ):

					"""simple docstring"""






					_lowercase			:     Optional[Any]      					=				parent
					_lowercase			:     Any      					=				batch_size
					_lowercase			:     List[str]      					=				image_size
					_lowercase			:     Union[str, Any]      					=				patch_size
					_lowercase			:     Tuple      					=				num_channels
					_lowercase			:     int      					=				is_training
					_lowercase			:     List[str]      					=				use_labels
					_lowercase			:     Any      					=				hidden_size
					_lowercase			:     Optional[int]      					=				num_hidden_layers
					_lowercase			:     Optional[Any]      					=				backbone_out_indices
					_lowercase			:     Optional[int]      					=				num_attention_heads
					_lowercase			:     Any      					=				intermediate_size
					_lowercase			:     str      					=				hidden_act
					_lowercase			:     Any      					=				hidden_dropout_prob
					_lowercase			:     int      					=				attention_probs_dropout_prob
					_lowercase			:     List[str]      					=				initializer_range
					_lowercase			:     Union[str, Any]      					=				num_labels
					_lowercase			:     Optional[int]      					=				scope
					_lowercase			:     Union[str, Any]      					=				is_hybrid
					# sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
					_lowercase			:     int      					=				(image_size // patch_size) ** 2
					_lowercase			:     int      					=				num_patches + 1







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

					"""simple docstring"""






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

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

					_lowercase			:     Any      					=				self.get_config()

					return config, pixel_values, labels







				def 				__UpperCAmelCase       (			self     :							Optional[Any]    ):

					"""simple docstring"""






					return DPTConfig(
					    image_size=self.image_size      ,       patch_size=self.patch_size      ,       num_channels=self.num_channels      ,       hidden_size=self.hidden_size      ,       num_hidden_layers=self.num_hidden_layers      ,       backbone_out_indices=self.backbone_out_indices      ,       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      ,       is_decoder=lowerCamelCase_      ,       initializer_range=self.initializer_range      ,       is_hybrid=self.is_hybrid      ,       )







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

					"""simple docstring"""






					_lowercase			:     int      					=				DPTModel(config=lowerCamelCase_    )
					model.to(lowerCamelCase_    )
					model.eval()
					_lowercase			:     Tuple      					=				model(lowerCamelCase_    )
					self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)    )







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

					"""simple docstring"""






					_lowercase			:     int      					=				self.num_labels
					_lowercase			:     Optional[Any]      					=				DPTForDepthEstimation(lowerCamelCase_    )
					model.to(lowerCamelCase_    )
					model.eval()
					_lowercase			:     List[Any]      					=				model(lowerCamelCase_    )
					self.parent.assertEqual(result.predicted_depth.shape      ,       (self.batch_size, self.image_size, self.image_size)    )







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

					"""simple docstring"""






					_lowercase			:     Tuple      					=				self.num_labels
					_lowercase			:     Optional[Any]      					=				DPTForSemanticSegmentation(lowerCamelCase_    )
					model.to(lowerCamelCase_    )
					model.eval()
					_lowercase			:     Tuple      					=				model(lowerCamelCase_      ,       labels=lowerCamelCase_    )
					self.parent.assertEqual(
					    result.logits.shape      ,       (self.batch_size, self.num_labels, self.image_size, self.image_size)    )







				def 				__UpperCAmelCase       (			self     :							List[str]    ):

					"""simple docstring"""






					_lowercase			:     List[Any]      					=				self.prepare_config_and_inputs()
					_lowercase					,   _lowercase					,   _lowercase			:     Dict      					=				config_and_inputs
					_lowercase			:     Optional[Any]      					=				{'pixel_values': pixel_values}
					return config, inputs_dict



@require_torch
class 			_lowerCamelCase      (__lowerCamelCase    ,				__lowerCamelCase    ,				unittest.TestCase  ):
				_snake_case  =	(DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
				_snake_case  =	(
				    {
				        "depth-estimation": DPTForDepthEstimation,
				        "feature-extraction": DPTModel,
				        "image-segmentation": DPTForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				_snake_case  =	False
				_snake_case  =	False
				_snake_case  =	False







				def 				__UpperCAmelCase       (			self     :							Tuple    ):

					"""simple docstring"""






					_lowercase			:     List[Any]      					=				DPTModelTester(self    )
					_lowercase			:     Optional[Any]      					=				ConfigTester(self      ,       config_class=lowerCamelCase_      ,       has_text_modality=lowerCamelCase_      ,       hidden_size=3_7    )







				def 				__UpperCAmelCase       (			self     :							str    ):

					"""simple docstring"""






					self.config_tester.run_common_tests()







				@unittest.skip(reason='DPT does not use inputs_embeds'    )
				def 				__UpperCAmelCase       (			self     :							List[str]    ):

					"""simple docstring"""






					pass







				def 				__UpperCAmelCase       (			self     :							Optional[Any]    ):

					"""simple docstring"""






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

					for model_class in self.all_model_classes:
						_lowercase			:     Union[str, Any]      					=				model_class(lowerCamelCase_    )
						self.assertIsInstance(model.get_input_embeddings()      ,       (nn.Module)    )
						_lowercase			:     Union[str, Any]      					=				model.get_output_embeddings()
						self.assertTrue(x is None or isinstance(lowerCamelCase_      ,       nn.Linear    )    )







				def 				__UpperCAmelCase       (			self     :							Optional[Any]    ):

					"""simple docstring"""






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

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

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







				def 				__UpperCAmelCase       (			self     :							int    ):

					"""simple docstring"""






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







				def 				__UpperCAmelCase       (			self     :							Any    ):

					"""simple docstring"""






					_lowercase			:     Optional[Any]      					=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_depth_estimation(*lowerCamelCase_    )







				def 				__UpperCAmelCase       (			self     :							Any    ):

					"""simple docstring"""






					_lowercase			:     Tuple      					=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_semantic_segmentation(*lowerCamelCase_    )







				def 				__UpperCAmelCase       (			self     :							List[Any]    ):

					"""simple docstring"""






					for model_class in self.all_model_classes:
						if model_class.__name__ == "DPTForDepthEstimation":
							continue

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

						if model_class in get_values(lowerCamelCase_    ):
							continue

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







				def 				__UpperCAmelCase       (			self     :							Optional[Any]    ):

					"""simple docstring"""






					for model_class in self.all_model_classes:
						if model_class.__name__ == "DPTForDepthEstimation":
							continue

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

						if model_class in get_values(lowerCamelCase_    ) or not model_class.supports_gradient_checkpointing:
							continue
						_lowercase			:     Dict      					=				model_class(lowerCamelCase_    )
						model.to(lowerCamelCase_    )
						model.gradient_checkpointing_enable()
						model.train()
						_lowercase			:     Dict      					=				self._prepare_for_class(lowerCamelCase_      ,       lowerCamelCase_      ,       return_labels=lowerCamelCase_    )
						_lowercase			:     Dict      					=				model(**lowerCamelCase_    ).loss
						loss.backward()







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

					"""simple docstring"""






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

					_lowercase			:     List[str]      					=				_config_zero_init(lowerCamelCase_    )
					for model_class in self.all_model_classes:
						_lowercase			:     Union[str, Any]      					=				model_class(config=lowerCamelCase_    )
						# Skip the check for the backbone
						_lowercase			:     Optional[int]      					=				[]
						for name, module in model.named_modules():
							if module.__class__.__name__ == "DPTViTHybridEmbeddings":
								_lowercase			:     Optional[Any]      					=				[F'''{name}.{key}''' for key in module.state_dict().keys()]
								break

						for name, param in model.named_parameters():
							if param.requires_grad:
								if name in backbone_params:
									continue
								self.assertIn(
								    ((param.data.mean() * 1E9).round() / 1E9).item()      ,       [0.0, 1.0]      ,       msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''      ,       )







				@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'    )
				def 				__UpperCAmelCase       (			self     :							Any    ):

					"""simple docstring"""






					pass







				@slow
				def 				__UpperCAmelCase       (			self     :							int    ):

					"""simple docstring"""






					for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						_lowercase			:     Any      					=				DPTModel.from_pretrained(lowerCamelCase_    )
						self.assertIsNotNone(lowerCamelCase_    )



def   __lowerCAmelCase(		):






	"""simple docstring"""





	_lowercase			:     Optional[int]      					=				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      )
	return image







@require_torch
@require_vision
@slow
class 			_lowerCamelCase      (unittest.TestCase  ):







				def 				__UpperCAmelCase       (			self     :							Optional[int]    ):

					"""simple docstring"""






					_lowercase			:     List[Any]      					=				DPTImageProcessor.from_pretrained('Intel/dpt-large'    )
					_lowercase			:     int      					=				DPTForDepthEstimation.from_pretrained('Intel/dpt-large'    ).to(lowerCamelCase_    )

					_lowercase			:     Optional[int]      					=				prepare_img()
					_lowercase			:     str      					=				image_processor(images=lowerCamelCase_      ,       return_tensors='pt'    ).to(lowerCamelCase_    )

					# forward pass
					with torch.no_grad():
						_lowercase			:     Union[str, Any]      					=				model(**lowerCamelCase_    )
						_lowercase			:     Optional[Any]      					=				outputs.predicted_depth

					# verify the predicted depth
					_lowercase			:     int      					=				torch.Size((1, 3_8_4, 3_8_4)    )
					self.assertEqual(predicted_depth.shape      ,       lowerCamelCase_    )

					_lowercase			:     Any      					=				torch.tensor(
					    [[6.3199, 6.3629, 6.4148], [6.3850, 6.3615, 6.4166], [6.3519, 6.3176, 6.3575]]    ).to(lowerCamelCase_    )

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







				def 				__UpperCAmelCase       (			self     :							int    ):

					"""simple docstring"""






					_lowercase			:     str      					=				DPTImageProcessor.from_pretrained('Intel/dpt-large-ade'    )
					_lowercase			:     List[str]      					=				DPTForSemanticSegmentation.from_pretrained('Intel/dpt-large-ade'    ).to(lowerCamelCase_    )

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

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

					# verify the logits
					_lowercase			:     Dict      					=				torch.Size((1, 1_5_0, 4_8_0, 4_8_0)    )
					self.assertEqual(outputs.logits.shape      ,       lowerCamelCase_    )

					_lowercase			:     List[str]      					=				torch.tensor(
					    [[4.0480, 4.2420, 4.4360], [4.3124, 4.5693, 4.8261], [4.5768, 4.8965, 5.2163]]    ).to(lowerCamelCase_    )

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







				def 				__UpperCAmelCase       (			self     :							int    ):

					"""simple docstring"""






					_lowercase			:     List[Any]      					=				DPTImageProcessor.from_pretrained('Intel/dpt-large-ade'    )
					_lowercase			:     int      					=				DPTForSemanticSegmentation.from_pretrained('Intel/dpt-large-ade'    ).to(lowerCamelCase_    )

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

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

					_lowercase			:     List[Any]      					=				outputs.logits.detach().cpu()

					_lowercase			:     Optional[Any]      					=				image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase_      ,       target_sizes=[(5_0_0, 3_0_0)]    )
					_lowercase			:     str      					=				torch.Size((5_0_0, 3_0_0)    )
					self.assertEqual(segmentation[0].shape      ,       lowerCamelCase_    )

					_lowercase			:     List[str]      					=				image_processor.post_process_semantic_segmentation(outputs=lowerCamelCase_    )
					_lowercase			:     Union[str, Any]      					=				torch.Size((4_8_0, 4_8_0)    )
					self.assertEqual(segmentation[0].shape      ,       lowerCamelCase_    )




"""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_    )



