



import unittest

from parameterized import parameterized

from transformers import AutoTokenizer, GPTNeoXConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    GPTNeoXForCausalLM,
			    GPTNeoXForQuestionAnswering,
			    GPTNeoXForSequenceClassification,
			    GPTNeoXForTokenClassification,
			    GPTNeoXModel,
			)



class 	lowerCamelCase      :







					'''simple docstring'''







					def __init__(			self    ,	lowerCAmelCase    ,	lowerCAmelCase=13    ,	lowerCAmelCase=7    ,	lowerCAmelCase=True    ,	lowerCAmelCase=True    ,	lowerCAmelCase=True    ,	lowerCAmelCase=True    ,	lowerCAmelCase=99    ,	lowerCAmelCase=64    ,	lowerCAmelCase=5    ,	lowerCAmelCase=4    ,	lowerCAmelCase=37    ,	lowerCAmelCase="gelu"    ,	lowerCAmelCase=0.1    ,	lowerCAmelCase=0.1    ,	lowerCAmelCase=512    ,	lowerCAmelCase=16    ,	lowerCAmelCase=2    ,	lowerCAmelCase=0.02    ,	lowerCAmelCase=3    ,	lowerCAmelCase=4    ,	lowerCAmelCase=None    ,	):
							UpperCAmelCase_						=				parent
							UpperCAmelCase_						=				batch_size
							UpperCAmelCase_						=				seq_length
							UpperCAmelCase_						=				is_training
							UpperCAmelCase_						=				use_input_mask
							UpperCAmelCase_						=				use_token_type_ids
							UpperCAmelCase_						=				use_labels
							UpperCAmelCase_						=				vocab_size
							UpperCAmelCase_						=				hidden_size
							UpperCAmelCase_						=				num_hidden_layers
							UpperCAmelCase_						=				num_attention_heads
							UpperCAmelCase_						=				intermediate_size
							UpperCAmelCase_						=				hidden_act
							UpperCAmelCase_						=				hidden_dropout_prob
							UpperCAmelCase_						=				attention_probs_dropout_prob
							UpperCAmelCase_						=				max_position_embeddings
							UpperCAmelCase_						=				type_vocab_size
							UpperCAmelCase_						=				type_sequence_label_size
							UpperCAmelCase_						=				initializer_range
							UpperCAmelCase_						=				num_labels
							UpperCAmelCase_						=				num_choices
							UpperCAmelCase_						=				scope
							UpperCAmelCase_						=				vocab_size - 1



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

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

							UpperCAmelCase_						=				None
							if self.use_labels:
									UpperCAmelCase_						=				ids_tensor([self.batch_size, self.seq_length]    ,	self.num_labels     )

							UpperCAmelCase_						=				self.get_config()

							return config, input_ids, input_mask, token_labels



					def    A__		(			self     ):
							return GPTNeoXConfig(
							    vocab_size=self.vocab_size    ,	hidden_size=self.hidden_size    ,	num_hidden_layers=self.num_hidden_layers    ,	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    ,	max_position_embeddings=self.max_position_embeddings    ,	type_vocab_size=self.type_vocab_size    ,	is_decoder=lowerCAmelCase    ,	initializer_range=self.initializer_range    ,	pad_token_id=self.pad_token_id    ,	)



					def    A__		(			self     ):
							UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_						=				self.prepare_config_and_inputs()

							UpperCAmelCase_						=				True

							return config, input_ids, input_mask, token_labels



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				GPTNeoXModel(config=lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				model(lowerCAmelCase    ,	attention_mask=lowerCAmelCase     )
							UpperCAmelCase_						=				model(lowerCAmelCase     )
							self.parent.assertEqual(result.last_hidden_state.shape    ,	(self.batch_size, self.seq_length, self.hidden_size)     )



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				True
							UpperCAmelCase_						=				GPTNeoXModel(lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				model(lowerCAmelCase    ,	attention_mask=lowerCAmelCase     )
							self.parent.assertEqual(result.last_hidden_state.shape    ,	(self.batch_size, self.seq_length, self.hidden_size)     )



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				GPTNeoXForCausalLM(config=lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				model(lowerCAmelCase    ,	attention_mask=lowerCAmelCase    ,	labels=lowerCAmelCase     )
							self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.seq_length, self.vocab_size)     )



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				self.num_labels
							UpperCAmelCase_						=				GPTNeoXForQuestionAnswering(lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				model(lowerCAmelCase    ,	attention_mask=lowerCAmelCase     )
							self.parent.assertEqual(result.start_logits.shape    ,	(self.batch_size, self.seq_length)     )
							self.parent.assertEqual(result.end_logits.shape    ,	(self.batch_size, self.seq_length)     )



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				self.num_labels
							UpperCAmelCase_						=				GPTNeoXForSequenceClassification(lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				ids_tensor([self.batch_size]    ,	self.type_sequence_label_size     )
							UpperCAmelCase_						=				model(lowerCAmelCase    ,	attention_mask=lowerCAmelCase    ,	labels=lowerCAmelCase     )
							self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.num_labels)     )



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				self.num_labels
							UpperCAmelCase_						=				GPTNeoXForTokenClassification(lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()
							UpperCAmelCase_						=				model(lowerCAmelCase    ,	attention_mask=lowerCAmelCase    ,	labels=lowerCAmelCase     )
							self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.seq_length, self.num_labels)     )



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				True
							UpperCAmelCase_						=				GPTNeoXForCausalLM(config=lowerCAmelCase     )
							model.to(lowerCAmelCase     )
							model.eval()

							# first forward pass
							UpperCAmelCase_						=				model(lowerCAmelCase    ,	attention_mask=lowerCAmelCase    ,	use_cache=lowerCAmelCase     )
							UpperCAmelCase_						=				outputs.past_key_values

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

							# append to next input_ids and
							UpperCAmelCase_						=				torch.cat([input_ids, next_tokens]    ,	dim=-1     )
							UpperCAmelCase_						=				torch.cat([input_mask, next_mask]    ,	dim=-1     )

							UpperCAmelCase_						=				model(lowerCAmelCase    ,	attention_mask=lowerCAmelCase    ,	output_hidden_states=lowerCAmelCase     )
							UpperCAmelCase_						=				output_from_no_past["hidden_states"][0]
							UpperCAmelCase_						=				model(
							    lowerCAmelCase    ,	attention_mask=lowerCAmelCase    ,	past_key_values=lowerCAmelCase    ,	output_hidden_states=lowerCAmelCase    ,	)["hidden_states"][0]

							# select random slice
							UpperCAmelCase_						=				ids_tensor((1,)    ,	output_from_past.shape[-1]     ).item()
							UpperCAmelCase_						=				output_from_no_past[:, -3:, random_slice_idx].detach()
							UpperCAmelCase_						=				output_from_past[:, :, random_slice_idx].detach()

							self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]     )

							# test that outputs are equal for slice
							self.parent.assertTrue(torch.allclose(lowerCAmelCase    ,	lowerCAmelCase    ,	atol=1e-3     )     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.prepare_config_and_inputs()
							UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_						=				config_and_inputs
							UpperCAmelCase_						=				{"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict





@require_torch
class 	lowerCamelCase      (						lowercase__,     lowercase__,     lowercase__,     unittest.TestCase  ):







					'''simple docstring'''







					lowerCAmelCase_				:    List[Any]									=  (
					    (
					        GPTNeoXModel,
					        GPTNeoXForCausalLM,
					        GPTNeoXForQuestionAnswering,
					        GPTNeoXForSequenceClassification,
					        GPTNeoXForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					lowerCAmelCase_				:    str									=  (GPTNeoXForCausalLM,) if is_torch_available() else ()
					lowerCAmelCase_				:    Union[str, Any]									=  (
					    {
					        'feature-extraction': GPTNeoXModel,
					        'question-answering': GPTNeoXForQuestionAnswering,
					        'text-classification': GPTNeoXForSequenceClassification,
					        'text-generation': GPTNeoXForCausalLM,
					        'token-classification': GPTNeoXForTokenClassification,
					        'zero-shot': GPTNeoXForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					lowerCAmelCase_				:    Union[str, Any]									=  False
					lowerCAmelCase_				:    int									=  False
					lowerCAmelCase_				:    Dict									=  False
					lowerCAmelCase_				:    Any									=  False



					def    A__		(			self     ):
							UpperCAmelCase_						=				GPTNeoXModelTester(self     )
							UpperCAmelCase_						=				ConfigTester(self    ,	config_class=lowerCAmelCase    ,	hidden_size=64    ,	num_attention_heads=8     )



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



					def    A__		(			self     ):
							UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     )



					def    A__		(			self     ):
							# This regression test was failing with PyTorch < 1.3
							UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs_for_decoder()

							UpperCAmelCase_						=				None

							self.model_tester.create_and_check_model_as_decoder(lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_					,						UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCAmelCase    ,	lowerCAmelCase    ,	lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_causal_lm(*lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase     )



					def    A__		(			self     ):
							UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase     )



					@unittest.skip(reason="Feed forward chunking is not implemented"     )
					def    A__		(			self     ):
							pass



					@parameterized.expand([("linear",), ("dynamic",)]     )
					def    A__		(			self    ,	lowerCAmelCase     ):
							UpperCAmelCase_					,						UpperCAmelCase_						=				self.model_tester.prepare_config_and_inputs_for_common()
							UpperCAmelCase_						=				ids_tensor([1, 10]    ,	config.vocab_size     )
							UpperCAmelCase_						=				ids_tensor([1, int(config.max_position_embeddings * 1.5     )]    ,	config.vocab_size     )

							set_seed(42     )  # Fixed seed at init time so the two models get the same random weights
							UpperCAmelCase_						=				GPTNeoXModel(lowerCAmelCase     )
							original_model.to(lowerCAmelCase     )
							original_model.eval()
							UpperCAmelCase_						=				original_model(lowerCAmelCase     ).last_hidden_state
							UpperCAmelCase_						=				original_model(lowerCAmelCase     ).last_hidden_state

							set_seed(42     )  # Fixed seed at init time so the two models get the same random weights
							UpperCAmelCase_						=				{"type": scaling_type, "factor": 10.0}
							UpperCAmelCase_						=				GPTNeoXModel(lowerCAmelCase     )
							scaled_model.to(lowerCAmelCase     )
							scaled_model.eval()
							UpperCAmelCase_						=				scaled_model(lowerCAmelCase     ).last_hidden_state
							UpperCAmelCase_						=				scaled_model(lowerCAmelCase     ).last_hidden_state

							# Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
							# maximum sequence length, so the outputs for the short input should match.
							if scaling_type == "dynamic":
									self.assertTrue(torch.allclose(lowerCAmelCase    ,	lowerCAmelCase    ,	atol=1e-5     )     )
							else:
									self.assertFalse(torch.allclose(lowerCAmelCase    ,	lowerCAmelCase    ,	atol=1e-5     )     )

							# The output should be different for long inputs
							self.assertFalse(torch.allclose(lowerCAmelCase    ,	lowerCAmelCase    ,	atol=1e-5     )     )





@require_torch
class 	lowerCamelCase      (						unittest.TestCase  ):







					'''simple docstring'''







					@slow
					def    A__		(			self     ):
							UpperCAmelCase_						=				AutoTokenizer.from_pretrained("EleutherAI/pythia-410m-deduped"     )
							for checkpointing in [True, False]:
									UpperCAmelCase_						=				GPTNeoXForCausalLM.from_pretrained("EleutherAI/pythia-410m-deduped"     )

									if checkpointing:
											model.gradient_checkpointing_enable()
									else:
											model.gradient_checkpointing_disable()
									model.to(lowerCAmelCase     )

									UpperCAmelCase_						=				tokenizer("My favorite food is"    ,	return_tensors="pt"     ).to(lowerCAmelCase     )
									# The hub repo. is updated on 2023-04-04, resulting in poor outputs.
									# See: https://github.com/huggingface/transformers/pull/24193
									UpperCAmelCase_						=				"My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI'm not sure"

									UpperCAmelCase_						=				model.generate(**lowerCAmelCase    ,	do_sample=lowerCAmelCase    ,	max_new_tokens=20     )
									UpperCAmelCase_						=				tokenizer.batch_decode(lowerCAmelCase     )[0]

									self.assertEqual(lowerCAmelCase    ,	lowerCAmelCase     )






from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
			from PIL import Image

			from ..image_utils import load_image

if is_tf_available():
			from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING

if is_torch_available():
			import torch

			from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING

SCREAMING_SNAKE_CASE     				=						logging.get_logger(__name__)



@add_end_docstrings(lowercase__  )
class 	lowerCamelCase      (						lowercase__  ):







					'''simple docstring'''







					def __init__(			self    ,	*lowerCAmelCase    ,	**lowerCAmelCase     ):
							super().__init__(*lowerCAmelCase    ,	**lowerCAmelCase     )
							requires_backends(self    ,	"vision"     )
							self.check_model_type(
							    TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == "tf" else MODEL_FOR_VISION_2_SEQ_MAPPING     )



					def    A__		(			self    ,	lowerCAmelCase=None    ,	lowerCAmelCase=None    ,	lowerCAmelCase=None     ):
							UpperCAmelCase_						=				{}
							UpperCAmelCase_						=				{}

							if prompt is not None:
									UpperCAmelCase_						=				prompt

							if generate_kwargs is not None:
									UpperCAmelCase_						=				generate_kwargs
							if max_new_tokens is not None:
									if "generate_kwargs" not in forward_kwargs:
											UpperCAmelCase_						=				{}
									if "max_new_tokens" in forward_kwargs["generate_kwargs"]:
											raise ValueError(
											    "'max_new_tokens' is defined twice, once in 'generate_kwargs' and once as a direct parameter,"
											    " please use only one"     )
									UpperCAmelCase_						=				max_new_tokens
							return preprocess_params, forward_kwargs, {}



					def __call__(			self    ,	lowerCAmelCase    ,	**lowerCAmelCase     ):
							return super().__call__(lowerCAmelCase    ,	**lowerCAmelCase     )



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase=None     ):
							UpperCAmelCase_						=				load_image(lowerCAmelCase     )

							if prompt is not None:
									if not isinstance(lowerCAmelCase    ,	lowerCAmelCase     ):
											raise ValueError(
											    f'''Received an invalid text input, got - {type(lowerCAmelCase     )} - but expected a single string. '''
											    "Note also that one single text can be provided for conditional image to text generation."     )

									UpperCAmelCase_						=				self.model.config.model_type

									if model_type == "git":
											UpperCAmelCase_						=				self.image_processor(images=lowerCAmelCase    ,	return_tensors=self.framework     )
											UpperCAmelCase_						=				self.tokenizer(text=lowerCAmelCase    ,	add_special_tokens=lowerCAmelCase     ).input_ids
											UpperCAmelCase_						=				[self.tokenizer.cls_token_id] + input_ids
											UpperCAmelCase_						=				torch.tensor(lowerCAmelCase     ).unsqueeze(0     )
											model_inputs.update({"input_ids": input_ids}     )

									elif model_type == "pix2struct":
											UpperCAmelCase_						=				self.image_processor(images=lowerCAmelCase    ,	header_text=lowerCAmelCase    ,	return_tensors=self.framework     )

									elif model_type != "vision-encoder-decoder":
											# vision-encoder-decoder does not support conditional generation
											UpperCAmelCase_						=				self.image_processor(images=lowerCAmelCase    ,	return_tensors=self.framework     )
											UpperCAmelCase_						=				self.tokenizer(lowerCAmelCase    ,	return_tensors=self.framework     )
											model_inputs.update(lowerCAmelCase     )

									else:
											raise ValueError(f'''Model type {model_type} does not support conditional text generation'''     )

							else:
									UpperCAmelCase_						=				self.image_processor(images=lowerCAmelCase    ,	return_tensors=self.framework     )

							if self.model.config.model_type == "git" and prompt is None:
									UpperCAmelCase_						=				None

							return model_inputs



					def    A__		(			self    ,	lowerCAmelCase    ,	lowerCAmelCase=None     ):
							# Git model sets `model_inputs["input_ids"] = None` in `preprocess` (when `prompt=None`). In batch model, the
							# pipeline will group them into a list of `None`, which fail `_forward`. Avoid this by checking it first.
							if (
							    "input_ids" in model_inputs
							    and isinstance(model_inputs["input_ids"]    ,	lowerCAmelCase     )
							    and all(x is None for x in model_inputs["input_ids"]     )
							):
									UpperCAmelCase_						=				None

							if generate_kwargs is None:
									UpperCAmelCase_						=				{}
							# FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py`
							#  parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas
							#  the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name`
							#  in the `_prepare_model_inputs` method.
							UpperCAmelCase_						=				model_inputs.pop(self.model.main_input_name     )
							UpperCAmelCase_						=				self.model.generate(lowerCAmelCase    ,	**lowerCAmelCase    ,	**lowerCAmelCase     )
							return model_outputs



					def    A__		(			self    ,	lowerCAmelCase     ):
							UpperCAmelCase_						=				[]
							for output_ids in model_outputs:
									UpperCAmelCase_						=				{
									    "generated_text": self.tokenizer.decode(
									        lowerCAmelCase    ,	skip_special_tokens=lowerCAmelCase    ,	)
									}
									records.append(lowerCAmelCase     )
							return records

