


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



		"""simple docstring"""

		UpperCamelCase     :					Tuple			       =		XGLMConfig
		UpperCamelCase     :					Dict			       =		{}
		UpperCamelCase     :					Optional[int]			       =		"gelu"







		def __init__( self		,      A		,      A=14		,      A=7		,      A=True		,      A=True		,      A=True		,      A=99		,      A=32		,      A=2		,      A=4		,      A=37		,      A="gelu"		,      A=0.1		,      A=0.1		,      A=5_12		,      A=0.02		,      )  -> Any:







					'''simple docstring'''







					lowerCamelCase    		=  parent
					lowerCamelCase    		=  batch_size
					lowerCamelCase    		=  seq_length
					lowerCamelCase    		=  is_training
					lowerCamelCase    		=  use_input_mask
					lowerCamelCase    		=  use_labels
					lowerCamelCase    		=  vocab_size
					lowerCamelCase    		=  d_model
					lowerCamelCase    		=  num_hidden_layers
					lowerCamelCase    		=  num_attention_heads
					lowerCamelCase    		=  ffn_dim
					lowerCamelCase    		=  activation_function
					lowerCamelCase    		=  activation_dropout
					lowerCamelCase    		=  attention_dropout
					lowerCamelCase    		=  max_position_embeddings
					lowerCamelCase    		=  initializer_range
					lowerCamelCase    		=  None
					lowerCamelCase    		=  0
					lowerCamelCase    		=  2
					lowerCamelCase    		=  1







		def 					__A   ( self	)  -> List[str]:







					'''simple docstring'''







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







		def 					__A   ( self	)  -> List[str]:







					'''simple docstring'''







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

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

					lowerCamelCase    		=  self.get_config()

					lowerCamelCase    		=  floats_tensor([self.num_hidden_layers, self.num_attention_heads]		,      2	)

					return (
					    config,
					    input_ids,
					    input_mask,
					    head_mask,
					)







		def 					__A   ( self	)  -> Union[str, Any]:







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







		def 					__A   ( self	)  -> List[str]:







					'''simple docstring'''







					lowerCamelCase    		=  self.prepare_config_and_inputs()

					(
					    lowerCamelCase			
					)    		=  config_and_inputs

					lowerCamelCase    		=  {
					    'input_ids': input_ids,
					    'head_mask': head_mask,
					}

					return config, inputs_dict







@require_tf
class        __lowercase       (							a_			,							a_			,							unittest.TestCase  ):



		"""simple docstring"""

		UpperCamelCase     :					Optional[int]			       =		(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
		UpperCamelCase     :					List[str]			       =		(TFXGLMForCausalLM,) if is_tf_available() else ()
		UpperCamelCase     :					str			       =		(
		    {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
		)
		UpperCamelCase     :					List[Any]			       =		False
		UpperCamelCase     :					Optional[Any]			       =		False
		UpperCamelCase     :					Optional[int]			       =		False







		def 					__A   ( self	)  -> Dict:







					'''simple docstring'''







					lowerCamelCase    		=  TFXGLMModelTester(self	)
					lowerCamelCase    		=  ConfigTester(self		,      config_class=A		,      n_embd=37	)







		def 					__A   ( self	)  -> int:







					'''simple docstring'''







					self.config_tester.run_common_tests()







		@slow
		def 					__A   ( self	)  -> Optional[Any]:







					'''simple docstring'''







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







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







					'''simple docstring'''







					super().test_resize_token_embeddings()







@require_tf
class        __lowercase       (							unittest.TestCase  ):



		"""simple docstring"""

		@slow
		def 					__A   ( self		,      A=True	)  -> Optional[int]:







					'''simple docstring'''







					lowerCamelCase    		=  TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""	)
					lowerCamelCase    		=  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
					lowerCamelCase    		=  [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
					lowerCamelCase    		=  model.generate(A		,      do_sample=A		,      num_beams=1	)
					if verify_outputs:
								self.assertListEqual(output_ids[0].numpy().tolist()		,      A	)







		@slow
		def 					__A   ( self	)  -> str:







					'''simple docstring'''







					lowerCamelCase    		=  XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""	)
					lowerCamelCase    		=  TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""	)

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

					lowerCamelCase    		=  (
					    'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due'
					)
					self.assertEqual(A		,      A	)







		@slow
		def 					__A   ( self	)  -> Dict:







					'''simple docstring'''







					lowerCamelCase    		=  TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""	)
					lowerCamelCase    		=  XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""	)

					lowerCamelCase    		=  'left'

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

					lowerCamelCase    		=  tokenizer(A		,      return_tensors="""tf"""		,      padding=A	)
					lowerCamelCase    		=  inputs['input_ids']

					lowerCamelCase    		=  model.generate(input_ids=A		,      attention_mask=inputs["""attention_mask"""]		,      max_new_tokens=12	)

					lowerCamelCase    		=  tokenizer(sentences[0]		,      return_tensors="""tf"""	).input_ids
					lowerCamelCase    		=  model.generate(input_ids=A		,      max_new_tokens=12	)

					lowerCamelCase    		=  tokenizer(sentences[1]		,      return_tensors="""tf"""	).input_ids
					lowerCamelCase    		=  model.generate(input_ids=A		,      max_new_tokens=12	)

					lowerCamelCase    		=  tokenizer.batch_decode(A		,      skip_special_tokens=A	)
					lowerCamelCase    		=  tokenizer.decode(output_non_padded[0]		,      skip_special_tokens=A	)
					lowerCamelCase    		=  tokenizer.decode(output_padded[0]		,      skip_special_tokens=A	)

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





'''simple docstring'''






import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
 import torch_xla.core.xla_model as xm
 import torch_xla.debug.metrics as met






class    __magic_name__				(						lowerCAmelCase							):






       def __init__(     self    ,				*snake_case    ,				snake_case=None    ,				snake_case=None    ,				**snake_case)		->    Tuple:




          '''simple docstring'''
          super().__init__(*snake_case    ,				**snake_case)
          _UpperCAmelCase  :  Any				       =eval_examples
          _UpperCAmelCase  :  Optional[int]				       =post_process_function






       def      lowerCAmelCase						(     self    ,				snake_case = None    ,				snake_case=None    ,				snake_case = None    ,				snake_case = "eval"    ,				**snake_case    ,				)		->    Dict[str, float]:




          '''simple docstring'''
          _UpperCAmelCase  :  Tuple				       =gen_kwargs.copy()
          _UpperCAmelCase  :  int				       =(
              gen_kwargs['max_length'] if gen_kwargs.get('max_length') is not None else self.args.generation_max_length
          )
          _UpperCAmelCase  :  Dict				       =(
              gen_kwargs['num_beams'] if gen_kwargs.get('num_beams') is not None else self.args.generation_num_beams
          )
          _UpperCAmelCase  :  Dict				       =gen_kwargs

          _UpperCAmelCase  :  Tuple				       =self.eval_dataset if eval_dataset is None else eval_dataset
          _UpperCAmelCase  :  Union[str, Any]				       =self.get_eval_dataloader(snake_case)
          _UpperCAmelCase  :  Tuple				       =self.eval_examples if eval_examples is None else eval_examples

          # Temporarily disable metric computation, we will do it in the loop here.
          _UpperCAmelCase  :  Dict				       =self.compute_metrics
          _UpperCAmelCase  :  Optional[Any]				       =None
          _UpperCAmelCase  :  str				       =time.time()
          _UpperCAmelCase  :  int				       =self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
          try:
             _UpperCAmelCase  :  str				       =eval_loop(
                 snake_case    ,				description='Evaluation'    ,				prediction_loss_only=True if compute_metrics is None else None    ,				ignore_keys=snake_case    ,				metric_key_prefix=snake_case    ,				)
          finally:
             _UpperCAmelCase  :  str				       =compute_metrics
          _UpperCAmelCase  :  Optional[int]				       =self.args.eval_batch_size * self.args.world_size
          if f"{metric_key_prefix}_jit_compilation_time" in output.metrics:
             start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"]
          output.metrics.update(
              speed_metrics(
                  snake_case    ,				snake_case    ,				num_samples=output.num_samples    ,				num_steps=math.ceil(output.num_samples / total_batch_size)    ,				))

          if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
             # Only the main node write the results by default
             _UpperCAmelCase  :  Tuple				       =self.post_process_function(snake_case    ,				snake_case    ,				snake_case)
             _UpperCAmelCase  :  Union[str, Any]				       =self.compute_metrics(snake_case)

             # Prefix all keys with metric_key_prefix + '_'
             for key in list(metrics.keys()):
                if not key.startswith(f"{metric_key_prefix}_"):
                   _UpperCAmelCase  :  Any				       =metrics.pop(snake_case)

             metrics.update(output.metrics)
          else:
             _UpperCAmelCase  :  str				       =output.metrics

          if self.args.should_log:
             # Only the main node log the results by default
             self.log(snake_case)

          if self.args.tpu_metrics_debug or self.args.debug:
             # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
             xm.master_print(met.metrics_report())

          _UpperCAmelCase  :  Any				       =self.callback_handler.on_evaluate(self.args    ,				self.state    ,				self.control    ,				snake_case)
          return metrics






       def      lowerCAmelCase						(     self    ,				snake_case    ,				snake_case    ,				snake_case=None    ,				snake_case = "test"    ,				**snake_case)		->    Dict:




          '''simple docstring'''
          _UpperCAmelCase  :  Optional[int]				       =gen_kwargs.copy()

          _UpperCAmelCase  :  int				       =self.get_test_dataloader(snake_case)

          # Temporarily disable metric computation, we will do it in the loop here.
          _UpperCAmelCase  :  str				       =self.compute_metrics
          _UpperCAmelCase  :  Union[str, Any]				       =None
          _UpperCAmelCase  :  Union[str, Any]				       =time.time()
          _UpperCAmelCase  :  Tuple				       =self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
          try:
             _UpperCAmelCase  :  Any				       =eval_loop(
                 snake_case    ,				description='Prediction'    ,				prediction_loss_only=True if compute_metrics is None else None    ,				ignore_keys=snake_case    ,				metric_key_prefix=snake_case    ,				)
          finally:
             _UpperCAmelCase  :  Any				       =compute_metrics

          _UpperCAmelCase  :  Dict				       =self.args.eval_batch_size * self.args.world_size
          if f"{metric_key_prefix}_jit_compilation_time" in output.metrics:
             start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"]
          output.metrics.update(
              speed_metrics(
                  snake_case    ,				snake_case    ,				num_samples=output.num_samples    ,				num_steps=math.ceil(output.num_samples / total_batch_size)    ,				))
          if self.post_process_function is None or self.compute_metrics is None:
             return output

          _UpperCAmelCase  :  Tuple				       =self.post_process_function(snake_case    ,				snake_case    ,				snake_case    ,				'predict')
          _UpperCAmelCase  :  Union[str, Any]				       =self.compute_metrics(snake_case)

          # Prefix all keys with metric_key_prefix + '_'
          for key in list(metrics.keys()):
             if not key.startswith(f"{metric_key_prefix}_"):
                _UpperCAmelCase  :  Any				       =metrics.pop(snake_case)
          metrics.update(output.metrics)
          return PredictionOutput(predictions=predictions.predictions    ,				label_ids=predictions.label_ids    ,				metrics=snake_case)

