


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 _lowerCamelCase:
       lowercase_ :	Dict     =			XGLMConfig
       lowercase_ :	Optional[Any]     =			{}
       lowercase_ :	Dict     =			"""gelu"""
       def __init__(					self,      lowerCamelCase,      lowerCamelCase=14,      lowerCamelCase=7,      lowerCamelCase=True,      lowerCamelCase=True,      lowerCamelCase=True,      lowerCamelCase=99,      lowerCamelCase=32,      lowerCamelCase=2,      lowerCamelCase=4,      lowerCamelCase=37,      lowerCamelCase="gelu",      lowerCamelCase=0.1,      lowerCamelCase=0.1,      lowerCamelCase=5_12,      lowerCamelCase=0.0_2,      )       ->   List[Any]:




           """simple docstring"""

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




           """simple docstring"""

           return XGLMConfig.from_pretrained('facebook/xglm-564M')
       def 			UpperCamelCase     (					self)       ->   int:




           """simple docstring"""

           _lowercase       :							Tuple      		=		tf.clip_by_value(
               ids_tensor([self.batch_size, self.seq_length],      self.vocab_size),      clip_value_min=0,      clip_value_max=3)

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

           _lowercase       :							List[Any]      		=		self.get_config()

           _lowercase       :							int      		=		floats_tensor([self.num_hidden_layers, self.num_attention_heads],      2)

           return (
               config,
               input_ids,
               input_mask,
               head_mask,
           )
       def 			UpperCamelCase     (					self)       ->   int:




           """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=lowerCamelCase,      bos_token_id=self.bos_token_id,      eos_token_id=self.eos_token_id,      pad_token_id=self.pad_token_id,      return_dict=lowerCamelCase,      )




       def 			UpperCamelCase     (					self)       ->   Any:




           """simple docstring"""

           _lowercase       :							Any      		=		self.prepare_config_and_inputs()

           (
               (
               _lowercase
           )		,				(
               _lowercase
           )		,				(
               _lowercase
           )		,				(
               _lowercase
           )		,				
           )       :							Optional[Any]      		=		config_and_inputs

           _lowercase       :							Tuple      		=		{
               'input_ids': input_ids,
               'head_mask': head_mask,
           }

           return config, inputs_dict






@require_tf
class _lowerCamelCase(						_a, _a, unittest.TestCase   ):
       lowercase_ :	int     =			(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
       lowercase_ :	Optional[Any]     =			(TFXGLMForCausalLM,) if is_tf_available() else ()
       lowercase_ :	Union[str, Any]     =			(
           {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {}
       )
       lowercase_ :	Dict     =			False
       lowercase_ :	Dict     =			False
       lowercase_ :	Optional[int]     =			False
       def 			UpperCamelCase     (					self)       ->   int:




           """simple docstring"""

           _lowercase       :							Any      		=		TFXGLMModelTester(self)
           _lowercase       :							int      		=		ConfigTester(self,      config_class=lowerCamelCase,      n_embd=37)
       def 			UpperCamelCase     (					self)       ->   Optional[int]:




           """simple docstring"""

           self.config_tester.run_common_tests()
       @slow
       def 			UpperCamelCase     (					self)       ->   Optional[int]:




           """simple docstring"""

           for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _lowercase       :							str      		=		TFXGLMModel.from_pretrained(lowerCamelCase)
               self.assertIsNotNone(lowerCamelCase)




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




           """simple docstring"""

           super().test_resize_token_embeddings()






@require_tf
class _lowerCamelCase(						unittest.TestCase   ):
       @slow
       def 			UpperCamelCase     (					self,      lowerCamelCase=True)       ->   List[str]:




           """simple docstring"""

           _lowercase       :							List[Any]      		=		TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M')
           _lowercase       :							Optional[Any]      		=		tf.convert_to_tensor([[2, 2_68, 98_65]],      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
           _lowercase       :							List[Any]      		=		[2, 2_68, 98_65, 67, 11, 19_88, 5_72_52, 98_65, 5, 9_84, 67, 19_88, 21_38_38, 16_58, 53, 7_04_46, 33, 66_57, 2_78, 15_81]
           # fmt: on
           _lowercase       :							Union[str, Any]      		=		model.generate(lowerCamelCase,      do_sample=lowerCamelCase,      num_beams=1)
           if verify_outputs:
               self.assertListEqual(output_ids[0].numpy().tolist(),      lowerCamelCase)
       @slow
       def 			UpperCamelCase     (					self)       ->   Any:




           """simple docstring"""

           _lowercase       :							Union[str, Any]      		=		XGLMTokenizer.from_pretrained('facebook/xglm-564M')
           _lowercase       :							List[str]      		=		TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M')

           tf.random.set_seed(0)
           _lowercase       :							Tuple      		=		tokenizer('Today is a nice day and',      return_tensors='tf')
           _lowercase       :							List[Any]      		=		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'):
               _lowercase       :							List[Any]      		=		model.generate(lowerCamelCase,      do_sample=lowerCamelCase,      seed=[7, 0])
           _lowercase       :							str      		=		tokenizer.decode(output_ids[0],      skip_special_tokens=lowerCamelCase)

           _lowercase       :							List[Any]      		=		(
               'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due'
           )
           self.assertEqual(lowerCamelCase,      lowerCamelCase)




       @slow
       def 			UpperCamelCase     (					self)       ->   Tuple:




           """simple docstring"""

           _lowercase       :							Union[str, Any]      		=		TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M')
           _lowercase       :							int      		=		XGLMTokenizer.from_pretrained('facebook/xglm-564M')

           _lowercase       :							List[str]      		=		'left'

           # use different length sentences to test batching
           _lowercase       :							Tuple      		=		[
               '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',
           ]

           _lowercase       :							str      		=		tokenizer(lowerCamelCase,      return_tensors='tf',      padding=lowerCamelCase)
           _lowercase       :							Optional[int]      		=		inputs['input_ids']

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

           _lowercase       :							Tuple      		=		tokenizer(sentences[0],      return_tensors='tf').input_ids
           _lowercase       :							Optional[Any]      		=		model.generate(input_ids=lowerCamelCase,      max_new_tokens=12)

           _lowercase       :							Tuple      		=		tokenizer(sentences[1],      return_tensors='tf').input_ids
           _lowercase       :							int      		=		model.generate(input_ids=lowerCamelCase,      max_new_tokens=12)

           _lowercase       :							str      		=		tokenizer.batch_decode(lowerCamelCase,      skip_special_tokens=lowerCamelCase)
           _lowercase       :							Any      		=		tokenizer.decode(output_non_padded[0],      skip_special_tokens=lowerCamelCase)
           _lowercase       :							List[Any]      		=		tokenizer.decode(output_padded[0],      skip_special_tokens=lowerCamelCase)

           _lowercase       :							Dict      		=		[
               '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(lowerCamelCase,      lowerCamelCase)
           self.assertListEqual(lowerCamelCase,      [non_padded_sentence, padded_sentence])






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():
 import tensorflow as tf

 from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
 from ..tf_utils import stable_softmax

if is_torch_available():
 from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

SCREAMING_SNAKE_CASE       :      List[str]												= logging.get_logger(__name__)






@add_end_docstrings(_a   )
class _lowerCamelCase(						_a   ):
       def __init__(					self,      *lowerCamelCase,      **lowerCamelCase)       ->   int:




           """simple docstring"""

           super().__init__(*lowerCamelCase,      **lowerCamelCase)
           requires_backends(self,      'vision')
           self.check_model_type(
               TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
               if self.framework == 'tf'
               else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING)
       def 			UpperCamelCase     (					self,      lowerCamelCase=None)       ->   int:




           """simple docstring"""

           _lowercase       :							Dict      		=		{}
           if top_k is not None:
               _lowercase       :							List[str]      		=		top_k
           return {}, {}, postprocess_params
       def __call__(					self,      lowerCamelCase,      **lowerCamelCase)       ->   Tuple:




           """simple docstring"""

           return super().__call__(lowerCamelCase,      **lowerCamelCase)
       def 			UpperCamelCase     (					self,      lowerCamelCase)       ->   str:




           """simple docstring"""

           _lowercase       :							Optional[Any]      		=		load_image(lowerCamelCase)
           _lowercase       :							List[str]      		=		self.image_processor(images=lowerCamelCase,      return_tensors=self.framework)
           return model_inputs
       def 			UpperCamelCase     (					self,      lowerCamelCase)       ->   List[str]:




           """simple docstring"""

           _lowercase       :							Optional[int]      		=		self.model(**lowerCamelCase)
           return model_outputs




       def 			UpperCamelCase     (					self,      lowerCamelCase,      lowerCamelCase=5)       ->   Dict:




           """simple docstring"""

           if top_k > self.model.config.num_labels:
               _lowercase       :							List[Any]      		=		self.model.config.num_labels

           if self.framework == "pt":
               _lowercase       :							int      		=		model_outputs.logits.softmax(-1)[0]
               _lowercase		,				_lowercase       :							Union[str, Any]      		=		probs.topk(lowerCamelCase)
           elif self.framework == "tf":
               _lowercase       :							int      		=		stable_softmax(model_outputs.logits,      axis=-1)[0]
               _lowercase       :							List[Any]      		=		tf.math.top_k(lowerCamelCase,      k=lowerCamelCase)
               _lowercase		,				_lowercase       :							Any      		=		topk.values.numpy(), topk.indices.numpy()
           else:
               raise ValueError(F'''Unsupported framework: {self.framework}''')

           _lowercase       :							str      		=		scores.tolist()
           _lowercase       :							str      		=		ids.tolist()
           return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(lowerCamelCase,      lowerCamelCase)]


