



import json
import os
import tempfile

from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available


if is_torch_available():
	import numpy as np
	import torch

if is_vision_available():
	from PIL import Image







def    lowerCAmelCase_  (lowerCAmelCase__:		int		,	lowerCAmelCase__:		List[str]=False		,	lowerCAmelCase__:		int=False		,	lowerCAmelCase__:		Optional[int]=False     ):





			"""simple docstring"""


			assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

			UpperCAmelCase_:		Dict       			=    []
			for i in range(image_processor_tester.batch_size     ):
						if equal_resolution:
									UpperCAmelCase_:		List[Any]       			=    image_processor_tester.max_resolution
						else:
									# To avoid getting image width/height 0
									UpperCAmelCase_:		str       			=    image_processor_tester.min_resolution
									if getattr(lowerCAmelCase__		,	"""size_divisor"""		,	lowerCAmelCase__     ):
												# If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
												UpperCAmelCase_:		Union[str, Any]       			=    max(image_processor_tester.size_divisor		,	lowerCAmelCase__     )
									UpperCAmelCase_			,       UpperCAmelCase_:		Any       			=    np.random.choice(np.arange(lowerCAmelCase__		,	image_processor_tester.max_resolution     )		,	2     )
						image_inputs.append(
						    np.random.randint(2_5_5		,	size=(image_processor_tester.num_channels, width, height)		,	dtype=np.uinta     )     )

			if not numpify and not torchify:
						# PIL expects the channel dimension as last dimension
						UpperCAmelCase_:		List[Any]       			=    [Image.fromarray(np.moveaxis(lowerCAmelCase__		,	0		,	-1     )     ) for image in image_inputs]

			if torchify:
						UpperCAmelCase_:		Union[str, Any]       			=    [torch.from_numpy(lowerCAmelCase__     ) for image in image_inputs]

			return image_inputs







def    lowerCAmelCase_  (lowerCAmelCase__:		str		,	lowerCAmelCase__:		Any=1_0		,	lowerCAmelCase__:		Tuple=1_0		,	lowerCAmelCase__:		Optional[Any]=False		,	lowerCAmelCase__:		Union[str, Any]=False     ):





			"""simple docstring"""


			UpperCAmelCase_:		Union[str, Any]       			=    []
			for i in range(image_processor_tester.num_frames     ):
						video.append(np.random.randint(2_5_5		,	size=(image_processor_tester.num_channels, width, height)		,	dtype=np.uinta     )     )

			if not numpify and not torchify:
						# PIL expects the channel dimension as last dimension
						UpperCAmelCase_:		int       			=    [Image.fromarray(np.moveaxis(lowerCAmelCase__		,	0		,	-1     )     ) for frame in video]

			if torchify:
						UpperCAmelCase_:		int       			=    [torch.from_numpy(lowerCAmelCase__     ) for frame in video]

			return video







def    lowerCAmelCase_  (lowerCAmelCase__:		List[Any]		,	lowerCAmelCase__:		Dict=False		,	lowerCAmelCase__:		Dict=False		,	lowerCAmelCase__:		Optional[int]=False     ):





			"""simple docstring"""


			assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

			UpperCAmelCase_:		Dict       			=    []
			for i in range(image_processor_tester.batch_size     ):
						if equal_resolution:
									UpperCAmelCase_:		Dict       			=    image_processor_tester.max_resolution
						else:
									UpperCAmelCase_			,       UpperCAmelCase_:		Any       			=    np.random.choice(
									    np.arange(image_processor_tester.min_resolution		,	image_processor_tester.max_resolution     )		,	2     )
									UpperCAmelCase_:		Dict       			=    prepare_video(
									    image_processor_tester=lowerCAmelCase__		,	width=lowerCAmelCase__		,	height=lowerCAmelCase__		,	numpify=lowerCAmelCase__		,	torchify=lowerCAmelCase__		,	)
						video_inputs.append(lowerCAmelCase__     )

			return video_inputs







class 							_a							:
			A					=  None

			def     __snake_case					(self				)					->      Any:
						UpperCAmelCase_:		Tuple       			=    self.image_processing_class(**self.image_processor_dict				)
						UpperCAmelCase_:		str       			=    json.loads(image_processor.to_json_string()				)
						for key, value in self.image_processor_dict.items():
									self.assertEqual(obj[key],      SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      Optional[Any]:
						UpperCAmelCase_:		Any       			=    self.image_processing_class(**self.image_processor_dict				)

						with tempfile.TemporaryDirectory() as tmpdirname:
									UpperCAmelCase_:		str       			=    os.path.join(SCREAMING_SNAKE_CASE_,      """image_processor.json"""				)
									image_processor_first.to_json_file(SCREAMING_SNAKE_CASE_				)
									UpperCAmelCase_:		str       			=    self.image_processing_class.from_json_file(SCREAMING_SNAKE_CASE_				)

						self.assertEqual(image_processor_second.to_dict(),      image_processor_first.to_dict()				)

			def     __snake_case					(self				)					->      Optional[Any]:
						UpperCAmelCase_:		Optional[Any]       			=    self.image_processing_class(**self.image_processor_dict				)

						with tempfile.TemporaryDirectory() as tmpdirname:
									UpperCAmelCase_:		Dict       			=    image_processor_first.save_pretrained(SCREAMING_SNAKE_CASE_				)[0]
									check_json_file_has_correct_format(SCREAMING_SNAKE_CASE_				)
									UpperCAmelCase_:		List[str]       			=    self.image_processing_class.from_pretrained(SCREAMING_SNAKE_CASE_				)

						self.assertEqual(image_processor_second.to_dict(),      image_processor_first.to_dict()				)

			def     __snake_case					(self				)					->      Tuple:
						UpperCAmelCase_:		int       			=    self.image_processing_class()
						self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)

			@require_torch
			@require_vision
			def     __snake_case					(self				)					->      List[Any]:
						if self.test_cast_dtype is not None:
									# Initialize image_processor
									UpperCAmelCase_:		Tuple       			=    self.image_processing_class(**self.image_processor_dict				)

									# create random PyTorch tensors
									UpperCAmelCase_:		List[Any]       			=    prepare_image_inputs(self.image_processor_tester,      equal_resolution=SCREAMING_SNAKE_CASE_,      torchify=SCREAMING_SNAKE_CASE_				)

									UpperCAmelCase_:		Dict       			=    image_processor(SCREAMING_SNAKE_CASE_,      return_tensors="""pt"""				)
									# for layoutLM compatiblity
									self.assertEqual(encoding.pixel_values.device,      torch.device("""cpu"""				)				)
									self.assertEqual(encoding.pixel_values.dtype,      torch.floataa				)

									UpperCAmelCase_:		Union[str, Any]       			=    image_processor(SCREAMING_SNAKE_CASE_,      return_tensors="""pt"""				).to(torch.floataa				)
									self.assertEqual(encoding.pixel_values.device,      torch.device("""cpu"""				)				)
									self.assertEqual(encoding.pixel_values.dtype,      torch.floataa				)

									UpperCAmelCase_:		Tuple       			=    image_processor(SCREAMING_SNAKE_CASE_,      return_tensors="""pt"""				).to("""cpu""",      torch.bfloataa				)
									self.assertEqual(encoding.pixel_values.device,      torch.device("""cpu"""				)				)
									self.assertEqual(encoding.pixel_values.dtype,      torch.bfloataa				)

									with self.assertRaises(SCREAMING_SNAKE_CASE_				):
												UpperCAmelCase_:		Any       			=    image_processor(SCREAMING_SNAKE_CASE_,      return_tensors="""pt"""				).to(torch.bfloataa,      """cpu"""				)

									# Try with text + image feature
									UpperCAmelCase_:		Dict       			=    image_processor(SCREAMING_SNAKE_CASE_,      return_tensors="""pt"""				)
									encoding.update({"""input_ids""": torch.LongTensor([[1, 2, 3], [4, 5, 6]]				)}				)
									UpperCAmelCase_:		Tuple       			=    encoding.to(torch.floataa				)

									self.assertEqual(encoding.pixel_values.device,      torch.device("""cpu"""				)				)
									self.assertEqual(encoding.pixel_values.dtype,      torch.floataa				)
									self.assertEqual(encoding.input_ids.dtype,      torch.long				)











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

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

			def     __snake_case					(self				)					->      Tuple:
						UpperCAmelCase_:		str       			=    ids_tensor([self.batch_size, self.seq_length],      self.vocab_size				)

						UpperCAmelCase_:		Tuple       			=    None
						if self.use_input_mask:
									UpperCAmelCase_:		int       			=    random_attention_mask([self.batch_size, self.seq_length]				)

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

						UpperCAmelCase_:		Optional[Any]       			=    None
						if self.use_mc_token_ids:
									UpperCAmelCase_:		str       			=    ids_tensor([self.batch_size, self.num_choices],      self.seq_length				)

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

						UpperCAmelCase_:		List[str]       			=    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=SCREAMING_SNAKE_CASE_,      )

						UpperCAmelCase_:		int       			=    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     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      *SCREAMING_SNAKE_CASE_				)					->      Dict:
						UpperCAmelCase_:		Any       			=    TFGPTJModel(config=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		str       			=    {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						UpperCAmelCase_:		str       			=    model(SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		Optional[int]       			=    [input_ids, None, input_mask]  # None is the input for 'past'
						UpperCAmelCase_:		List[str]       			=    model(SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		Tuple       			=    model(SCREAMING_SNAKE_CASE_				)

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

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      *SCREAMING_SNAKE_CASE_				)					->      Union[str, Any]:
						UpperCAmelCase_:		List[Any]       			=    TFGPTJModel(config=SCREAMING_SNAKE_CASE_				)

						# first forward pass
						UpperCAmelCase_:		Dict       			=    model(SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      use_cache=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		Optional[Any]       			=    model(SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		Optional[Any]       			=    model(SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      use_cache=SCREAMING_SNAKE_CASE_				)

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

						UpperCAmelCase_			,       UpperCAmelCase_:		List[Any]       			=    outputs.to_tuple()

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

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

						UpperCAmelCase_:		Tuple       			=    model(SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_				)["""last_hidden_state"""]
						UpperCAmelCase_:		List[str]       			=    model(SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      past_key_values=SCREAMING_SNAKE_CASE_				)[
						    """last_hidden_state"""
						]

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

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

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      *SCREAMING_SNAKE_CASE_				)					->      int:
						UpperCAmelCase_:		List[str]       			=    TFGPTJModel(config=SCREAMING_SNAKE_CASE_				)

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

						# first forward pass
						UpperCAmelCase_			,       UpperCAmelCase_:		Optional[Any]       			=    model(SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_				).to_tuple()

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

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

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

						# get two different outputs
						UpperCAmelCase_:		List[Any]       			=    model(SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_				)["""last_hidden_state"""]
						UpperCAmelCase_:		Optional[Any]       			=    model(SCREAMING_SNAKE_CASE_,      past_key_values=SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_				)[
						    """last_hidden_state"""
						]

						# select random slice
						UpperCAmelCase_:		Union[str, Any]       			=    int(ids_tensor((1,),      shape_list(SCREAMING_SNAKE_CASE_				)[-1]				)				)
						UpperCAmelCase_:		Tuple       			=    output_from_no_past[:, -1, random_slice_idx]
						UpperCAmelCase_:		int       			=    output_from_past[:, 0, random_slice_idx]

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

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      *SCREAMING_SNAKE_CASE_				)					->      Union[str, Any]:
						UpperCAmelCase_:		int       			=    TFGPTJModel(config=SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		Optional[int]       			=    input_ids[:1, :]
						UpperCAmelCase_:		Tuple       			=    input_mask[:1, :]
						UpperCAmelCase_:		Union[str, Any]       			=    token_type_ids[:1, :]
						UpperCAmelCase_:		Any       			=    1

						# first forward pass
						UpperCAmelCase_:		Optional[Any]       			=    model(SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      use_cache=SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_			,       UpperCAmelCase_:		Optional[int]       			=    outputs.to_tuple()

						# create hypothetical next token and extent to next_input_ids
						UpperCAmelCase_:		Dict       			=    ids_tensor((self.batch_size, 3),      config.vocab_size				)
						UpperCAmelCase_:		List[str]       			=    ids_tensor((self.batch_size, 3),      2				)
						UpperCAmelCase_:		Optional[int]       			=    ids_tensor((self.batch_size, 3),      self.type_vocab_size				)

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

						UpperCAmelCase_:		str       			=    model(
						    SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_				)["""last_hidden_state"""]
						UpperCAmelCase_:		Optional[Any]       			=    model(
						    SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_,      past_key_values=SCREAMING_SNAKE_CASE_,      )["""last_hidden_state"""]
						self.parent.assertTrue(output_from_past.shape[1] == next_tokens.shape[1]				)

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

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

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

			def     __snake_case					(self				)					->      Optional[Any]:
						UpperCAmelCase_:		Optional[int]       			=    self.prepare_config_and_inputs()

						(
						    (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       
						):		Optional[int]       			=    config_and_inputs

						UpperCAmelCase_:		int       			=    {
						    """input_ids""": input_ids,
						    """token_type_ids""": token_type_ids,
						    """attention_mask""": input_mask,
						}
						return config, inputs_dict







@require_tf
class 							_a							(      _lowerCAmelCase   ,		_lowerCAmelCase   ,		_lowerCAmelCase   ,		unittest.TestCase				):
			A					=  (
			    (TFGPTJForCausalLM, TFGPTJForSequenceClassification, TFGPTJForQuestionAnswering, TFGPTJModel)
			    if is_tf_available()
			    else ()
			)

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

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)					->      int:
						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     __snake_case					(self				)					->      Tuple:
						UpperCAmelCase_:		Union[str, Any]       			=    TFGPTJModelTester(self				)
						UpperCAmelCase_:		str       			=    ConfigTester(self,      config_class=SCREAMING_SNAKE_CASE_,      n_embd=37				)

			def     __snake_case					(self				)					->      Optional[Any]:
						self.config_tester.run_common_tests()

			def     __snake_case					(self				)					->      Optional[int]:
						UpperCAmelCase_:		Optional[Any]       			=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_gptj_model(*SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      Tuple:
						UpperCAmelCase_:		Tuple       			=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_gptj_model_past(*SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      Any:
						UpperCAmelCase_:		Dict       			=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_gptj_model_attention_mask_past(*SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      Dict:
						UpperCAmelCase_:		List[Any]       			=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_gptj_model_past_large_inputs(*SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      str:
						UpperCAmelCase_:		Dict       			=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_gptj_lm_head_model(*SCREAMING_SNAKE_CASE_				)

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

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

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

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

						UpperCAmelCase_:		Tuple       			=    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"""				):
									UpperCAmelCase_:		List[Any]       			=    model.generate(**SCREAMING_SNAKE_CASE_,      do_sample=SCREAMING_SNAKE_CASE_,      seed=[42, 0]				)
						UpperCAmelCase_:		Dict       			=    tokenizer.decode(output_ids[0],      skip_special_tokens=SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		Union[str, Any]       			=    """Today is a nice day and I’m going to go for a walk. I’"""
						self.assertEqual(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      List[Any]:
						UpperCAmelCase_:		List[Any]       			=    TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B""",      revision="""float16""",      from_pt=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		int       			=    AutoTokenizer.from_pretrained("""EleutherAI/gpt-j-6B""",      revision="""float16"""				)

						UpperCAmelCase_:		int       			=    """left"""

						# Define PAD Token = EOS Token = 50256
						UpperCAmelCase_:		int       			=    tokenizer.eos_token
						UpperCAmelCase_:		Optional[int]       			=    model.config.eos_token_id

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

			def     __snake_case					(self				)					->      Any:
						# Confirms that we get the expected results with left-padded beam search
						UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_:		Optional[Any]       			=    self._get_beam_search_test_objects()

						UpperCAmelCase_:		Optional[int]       			=    tokenizer(SCREAMING_SNAKE_CASE_,      return_tensors="""tf""",      padding=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		Dict       			=    model.generate(**SCREAMING_SNAKE_CASE_,      do_sample=SCREAMING_SNAKE_CASE_,      num_beams=2				)
						UpperCAmelCase_:		Optional[Any]       			=    tokenizer.batch_decode(SCREAMING_SNAKE_CASE_,      skip_special_tokens=SCREAMING_SNAKE_CASE_				)
						self.assertListEqual(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      List[Any]:
						# Confirms that left-padding is working properly
						UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_:		List[Any]       			=    self._get_beam_search_test_objects()

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

			def     __snake_case					(self				)					->      str:
						# Confirms that XLA is working properly
						UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_:		Optional[int]       			=    self._get_beam_search_test_objects()

						UpperCAmelCase_:		Tuple       			=    tokenizer(SCREAMING_SNAKE_CASE_,      return_tensors="""tf""",      padding=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		int       			=    tf.function(model.generate,      jit_compile=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		Union[str, Any]       			=    xla_generate(**SCREAMING_SNAKE_CASE_,      do_sample=SCREAMING_SNAKE_CASE_,      num_beams=2				)
						UpperCAmelCase_:		Any       			=    tokenizer.batch_decode(SCREAMING_SNAKE_CASE_,      skip_special_tokens=SCREAMING_SNAKE_CASE_				)
						self.assertListEqual(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)






