


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

__UpperCAmelCase : Union[str, Any] 			=				logging.get_logger(__name__)



@add_end_docstrings(__lowerCamelCase)
class    UpperCAmelCase_	(							__lowerCamelCase):
       '''simple docstring'''






       def __init__(     self ,					*__SCREAMING_SNAKE_CASE ,					**__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              super().__init__(*UpperCamelCase_ ,					**UpperCamelCase_						)
              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  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE=None ,					__SCREAMING_SNAKE_CASE=None ,					__SCREAMING_SNAKE_CASE=None						):




              """simple docstring"""




              UpperCamelCase    :	Union[str, Any]   	=		{}
              UpperCamelCase    :	Optional[Any]   	=		{}

              if prompt is not None:
                     UpperCamelCase    :	List[str]   	=		prompt

              if generate_kwargs is not None:
                     UpperCamelCase    :	List[Any]   	=		generate_kwargs
              if max_new_tokens is not None:
                     if "generate_kwargs" not in forward_kwargs:
                            UpperCamelCase    :	int   	=		{}
                     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    :	Any   	=		max_new_tokens
              return preprocess_params, forward_kwargs, {}






       def __call__(     self ,					__SCREAMING_SNAKE_CASE ,					**__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              return super().__call__(UpperCamelCase_ ,					**UpperCamelCase_						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=None						):




              """simple docstring"""




              UpperCamelCase    :	Tuple   	=		load_image(UpperCamelCase_						)

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

                     UpperCamelCase    :	Union[str, Any]   	=		self.model.config.model_type

                     if model_type == "git":
                            UpperCamelCase    :	Any   	=		self.image_processor(images=UpperCamelCase_ ,					return_tensors=self.framework						)
                            UpperCamelCase    :	Any   	=		self.tokenizer(text=UpperCamelCase_ ,					add_special_tokens=UpperCamelCase_						).input_ids
                            UpperCamelCase    :	Optional[int]   	=		[self.tokenizer.cls_token_id] + input_ids
                            UpperCamelCase    :	List[str]   	=		torch.tensor(UpperCamelCase_						).unsqueeze(0						)
                            model_inputs.update({'''input_ids''': input_ids}						)

                     elif model_type == "pix2struct":
                            UpperCamelCase    :	Union[str, Any]   	=		self.image_processor(images=UpperCamelCase_ ,					header_text=UpperCamelCase_ ,					return_tensors=self.framework						)

                     elif model_type != "vision-encoder-decoder":
                            # vision-encoder-decoder does not support conditional generation
                            UpperCamelCase    :	Any   	=		self.image_processor(images=UpperCamelCase_ ,					return_tensors=self.framework						)
                            UpperCamelCase    :	Union[str, Any]   	=		self.tokenizer(UpperCamelCase_ ,					return_tensors=self.framework						)
                            model_inputs.update(UpperCamelCase_						)

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

              else:
                     UpperCamelCase    :	List[Any]   	=		self.image_processor(images=UpperCamelCase_ ,					return_tensors=self.framework						)

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

              return model_inputs






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=None						):




              """simple docstring"""




              if (
                  "input_ids" in model_inputs
                  and isinstance(model_inputs['''input_ids'''] ,					UpperCamelCase_						)
                  and all(x is None for x in model_inputs['''input_ids''']						)
              ):
                     UpperCamelCase    :	List[str]   	=		None

              if generate_kwargs is None:
                     UpperCamelCase    :	Dict   	=		{}
              # 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    :	List[Any]   	=		model_inputs.pop(self.model.main_input_name						)
              UpperCamelCase    :	List[Any]   	=		self.model.generate(UpperCamelCase_ ,					**UpperCamelCase_ ,					**UpperCamelCase_						)
              return model_outputs






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	str   	=		[]
              for output_ids in model_outputs:
                     UpperCamelCase    :	Optional[int]   	=		{
                         '''generated_text''': self.tokenizer.decode(
                             UpperCamelCase_ ,					skip_special_tokens=UpperCamelCase_ ,					)
                     }
                     records.append(UpperCamelCase_						)
              return records





from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers.models.xglm.modeling_tf_xglm import (
      TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFXGLMForCausalLM,
      TFXGLMModel,
  )



@require_tf
class    UpperCAmelCase_	:
       '''simple docstring'''
       __UpperCamelCase      :     Any							=					XGLMConfig
       __UpperCamelCase      :     Dict							=					{}
       __UpperCamelCase      :     List[str]							=					"gelu"






       def __init__(     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=14 ,					__SCREAMING_SNAKE_CASE=7 ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=99 ,					__SCREAMING_SNAKE_CASE=32 ,					__SCREAMING_SNAKE_CASE=2 ,					__SCREAMING_SNAKE_CASE=4 ,					__SCREAMING_SNAKE_CASE=37 ,					__SCREAMING_SNAKE_CASE="gelu" ,					__SCREAMING_SNAKE_CASE=0.1 ,					__SCREAMING_SNAKE_CASE=0.1 ,					__SCREAMING_SNAKE_CASE=512 ,					__SCREAMING_SNAKE_CASE=0.02 ,					):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		parent
              UpperCamelCase    :	Optional[int]   	=		batch_size
              UpperCamelCase    :	str   	=		seq_length
              UpperCamelCase    :	List[str]   	=		is_training
              UpperCamelCase    :	Tuple   	=		use_input_mask
              UpperCamelCase    :	Union[str, Any]   	=		use_labels
              UpperCamelCase    :	int   	=		vocab_size
              UpperCamelCase    :	Optional[int]   	=		d_model
              UpperCamelCase    :	Any   	=		num_hidden_layers
              UpperCamelCase    :	List[str]   	=		num_attention_heads
              UpperCamelCase    :	Optional[Any]   	=		ffn_dim
              UpperCamelCase    :	Optional[int]   	=		activation_function
              UpperCamelCase    :	List[str]   	=		activation_dropout
              UpperCamelCase    :	Any   	=		attention_dropout
              UpperCamelCase    :	str   	=		max_position_embeddings
              UpperCamelCase    :	Union[str, Any]   	=		initializer_range
              UpperCamelCase    :	int   	=		None
              UpperCamelCase    :	Dict   	=		0
              UpperCamelCase    :	int   	=		2
              UpperCamelCase    :	Any   	=		1






       def  _lowercase   (     self						):




              """simple docstring"""




              return XGLMConfig.from_pretrained('''facebook/xglm-564M'''						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[int]   	=		tf.clip_by_value(
                  ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size						) ,					clip_value_min=0 ,					clip_value_max=3						)

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

              UpperCamelCase    :	Tuple   	=		self.get_config()

              UpperCamelCase    :	str   	=		floats_tensor([self.num_hidden_layers, self.num_attention_heads] ,					2						)

              return (
                  config,
                  input_ids,
                  input_mask,
                  head_mask,
              )






       def  _lowercase   (     self						):




              """simple docstring"""




              return XGLMConfig(
                  vocab_size=self.vocab_size ,					d_model=self.hidden_size ,					num_layers=self.num_hidden_layers ,					attention_heads=self.num_attention_heads ,					ffn_dim=self.ffn_dim ,					activation_function=self.activation_function ,					activation_dropout=self.activation_dropout ,					attention_dropout=self.attention_dropout ,					max_position_embeddings=self.max_position_embeddings ,					initializer_range=self.initializer_range ,					use_cache=__SCREAMING_SNAKE_CASE ,					bos_token_id=self.bos_token_id ,					eos_token_id=self.eos_token_id ,					pad_token_id=self.pad_token_id ,					return_dict=__SCREAMING_SNAKE_CASE ,					)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[str]   	=		self.prepare_config_and_inputs()

              (
                  (
                  UpperCamelCase
              )	, (
                  UpperCamelCase
              )	, (
                  UpperCamelCase
              )	, (
                  UpperCamelCase
              )	, 
              )    :	Dict   	=		config_and_inputs

              UpperCamelCase    :	List[str]   	=		{
                  '''input_ids''': input_ids,
                  '''head_mask''': head_mask,
              }

              return config, inputs_dict




@require_tf
class    UpperCAmelCase_	(							_a,     _a,     unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase      :     Optional[Any]							=					(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
       __UpperCamelCase      :     Union[str, Any]							=					(TFXGLMForCausalLM,) if is_tf_available() else ()
       __UpperCamelCase      :     Any							=					(
           {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
       )
       __UpperCamelCase      :     Optional[int]							=					False
       __UpperCamelCase      :     List[Any]							=					False
       __UpperCamelCase      :     List[Any]							=					False






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[Any]   	=		TFXGLMModelTester(self						)
              UpperCamelCase    :	Any   	=		ConfigTester(self ,					config_class=__SCREAMING_SNAKE_CASE ,					n_embd=37						)






       def  _lowercase   (     self						):




              """simple docstring"""




              self.config_tester.run_common_tests()






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     UpperCamelCase    :	List[Any]   	=		TFXGLMModel.from_pretrained(__SCREAMING_SNAKE_CASE						)
                     self.assertIsNotNone(__SCREAMING_SNAKE_CASE						)






       @unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.'''						)
       def  _lowercase   (     self						):




              """simple docstring"""




              super().test_resize_token_embeddings()




@require_tf
class    UpperCAmelCase_	(							unittest.TestCase):
       '''simple docstring'''






       @slow
       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE=True						):




              """simple docstring"""




              UpperCamelCase    :	List[str]   	=		TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''						)
              UpperCamelCase    :	List[Any]   	=		tf.convert_to_tensor([[2, 268, 9_865]] ,					dtype=tf.intaa						)  # The dog
              # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
              # fmt: off
              UpperCamelCase    :	str   	=		[2, 268, 9_865, 67, 11, 1_988, 57_252, 9_865, 5, 984, 67, 1_988, 213_838, 1_658, 53, 70_446, 33, 6_657, 278, 1_581]
              # fmt: on
              UpperCamelCase    :	Union[str, Any]   	=		model.generate(__SCREAMING_SNAKE_CASE ,					do_sample=__SCREAMING_SNAKE_CASE ,					num_beams=1						)
              if verify_outputs:
                     self.assertListEqual(output_ids[0].numpy().tolist() ,					__SCREAMING_SNAKE_CASE						)






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	str   	=		XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''						)
              UpperCamelCase    :	List[str]   	=		TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''						)

              tf.random.set_seed(0						)
              UpperCamelCase    :	Tuple   	=		tokenizer('''Today is a nice day and''' ,					return_tensors='''tf'''						)
              UpperCamelCase    :	int   	=		tokenized.input_ids
              # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
              with tf.device(''':/CPU:0'''						):
                     UpperCamelCase    :	str   	=		model.generate(__SCREAMING_SNAKE_CASE ,					do_sample=__SCREAMING_SNAKE_CASE ,					seed=[7, 0]						)
              UpperCamelCase    :	Dict   	=		tokenizer.decode(output_ids[0] ,					skip_special_tokens=__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Optional[int]   	=		(
                  '''Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due'''
              )
              self.assertEqual(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						)






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''						)
              UpperCamelCase    :	Tuple   	=		XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''						)

              UpperCamelCase    :	Tuple   	=		'''left'''

              # use different length sentences to test batching
              UpperCamelCase    :	Any   	=		[
                  '''This is an extremelly long sentence that only exists to test the ability of the model to cope with '''
                  '''left-padding, such as in batched generation. The output for the sequence below should be the same '''
                  '''regardless of whether left padding is applied or not. When''',
                  '''Hello, my dog is a little''',
              ]

              UpperCamelCase    :	List[Any]   	=		tokenizer(__SCREAMING_SNAKE_CASE ,					return_tensors='''tf''' ,					padding=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	List[str]   	=		inputs['''input_ids''']

              UpperCamelCase    :	Optional[int]   	=		model.generate(input_ids=__SCREAMING_SNAKE_CASE ,					attention_mask=inputs['''attention_mask'''] ,					max_new_tokens=12						)

              UpperCamelCase    :	Optional[int]   	=		tokenizer(sentences[0] ,					return_tensors='''tf'''						).input_ids
              UpperCamelCase    :	Optional[Any]   	=		model.generate(input_ids=__SCREAMING_SNAKE_CASE ,					max_new_tokens=12						)

              UpperCamelCase    :	str   	=		tokenizer(sentences[1] ,					return_tensors='''tf'''						).input_ids
              UpperCamelCase    :	List[Any]   	=		model.generate(input_ids=__SCREAMING_SNAKE_CASE ,					max_new_tokens=12						)

              UpperCamelCase    :	Any   	=		tokenizer.batch_decode(__SCREAMING_SNAKE_CASE ,					skip_special_tokens=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	List[str]   	=		tokenizer.decode(output_non_padded[0] ,					skip_special_tokens=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Tuple   	=		tokenizer.decode(output_padded[0] ,					skip_special_tokens=__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Optional[int]   	=		[
                  '''This is an extremelly long sentence that only exists to test the ability of the model to cope with '''
                  '''left-padding, such as in batched generation. The output for the sequence below should be the same '''
                  '''regardless of whether left padding is applied or not. When left padding is applied, the sequence will be '''
                  '''a single''',
                  '''Hello, my dog is a little bit of a shy one, but he is very friendly''',
              ]
              self.assertListEqual(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						)
              self.assertListEqual(__SCREAMING_SNAKE_CASE ,					[non_padded_sentence, padded_sentence]						)

