


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

 '''simple docstring'''

 __snake_case				=						XGLMConfig
 __snake_case				=						{}
 __snake_case				=						"gelu"



 def __init__(			self	,   _UpperCAmelCase	,   _UpperCAmelCase=14	,   _UpperCAmelCase=7	,   _UpperCAmelCase=True	,   _UpperCAmelCase=True	,   _UpperCAmelCase=True	,   _UpperCAmelCase=99	,   _UpperCAmelCase=32	,   _UpperCAmelCase=2	,   _UpperCAmelCase=4	,   _UpperCAmelCase=37	,   _UpperCAmelCase="gelu"	,   _UpperCAmelCase=0.1	,   _UpperCAmelCase=0.1	,   _UpperCAmelCase=5_12	,   _UpperCAmelCase=0.02	,   ):
       snake_case_ =   parent
       snake_case_ =   batch_size
       snake_case_ =   seq_length
       snake_case_ =   is_training
       snake_case_ =   use_input_mask
       snake_case_ =   use_labels
       snake_case_ =   vocab_size
       snake_case_ =   d_model
       snake_case_ =   num_hidden_layers
       snake_case_ =   num_attention_heads
       snake_case_ =   ffn_dim
       snake_case_ =   activation_function
       snake_case_ =   activation_dropout
       snake_case_ =   attention_dropout
       snake_case_ =   max_position_embeddings
       snake_case_ =   initializer_range
       snake_case_ =   None
       snake_case_ =   0
       snake_case_ =   2
       snake_case_ =   1



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



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   tf.clip_by_value(
           ids_tensor([self.batch_size, self.seq_length]	,   self.vocab_size      )	,   clip_value_min=0	,   clip_value_max=3      )

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

       snake_case_ =   self.get_config()

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

       return (
           config,
           input_ids,
           input_mask,
           head_mask,
       )



 def 		UpperCamelCase__  (			self      ):
       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=_UpperCAmelCase	,   bos_token_id=self.bos_token_id	,   eos_token_id=self.eos_token_id	,   pad_token_id=self.pad_token_id	,   return_dict=_UpperCAmelCase	,   )





 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   self.prepare_config_and_inputs()

       (
           (
           snake_case_
       ) ,					(
           snake_case_
       ) ,					(
           snake_case_
       ) ,					(
           snake_case_
       ) ,					
       ) =   config_and_inputs

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

       return config, inputs_dict







@require_tf
class lowerCAmelCase_		(				lowerCamelCase__      ,		lowerCamelCase__      ,		unittest.TestCase ):

 '''simple docstring'''

 __snake_case				=						(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
 __snake_case				=						(TFXGLMForCausalLM,) if is_tf_available() else ()
 __snake_case				=						(
     {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
 )
 __snake_case				=						False
 __snake_case				=						False
 __snake_case				=						False



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   TFXGLMModelTester(self      )
       snake_case_ =   ConfigTester(self	,   config_class=_UpperCAmelCase	,   n_embd=37      )



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



 @slow
 def 		UpperCamelCase__  (			self      ):
       for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             snake_case_ =   TFXGLMModel.from_pretrained(_UpperCAmelCase      )
             self.assertIsNotNone(_UpperCAmelCase      )





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







@require_tf
class lowerCAmelCase_		(				unittest.TestCase ):

 '''simple docstring'''

 @slow
 def 		UpperCamelCase__  (			self	,   _UpperCAmelCase=True      ):
       snake_case_ =   TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''      )
       snake_case_ =   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
       snake_case_ =   [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
       snake_case_ =   model.generate(_UpperCAmelCase	,   do_sample=_UpperCAmelCase	,   num_beams=1      )
       if verify_outputs:
             self.assertListEqual(output_ids[0].numpy().tolist()	,   _UpperCAmelCase      )



 @slow
 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''      )
       snake_case_ =   TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''      )

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

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





 @slow
 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''      )
       snake_case_ =   XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''      )

       snake_case_ =   '''left'''

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

       snake_case_ =   tokenizer(_UpperCAmelCase	,   return_tensors='''tf'''	,   padding=_UpperCAmelCase      )
       snake_case_ =   inputs['''input_ids''']

       snake_case_ =   model.generate(input_ids=_UpperCAmelCase	,   attention_mask=inputs['''attention_mask''']	,   max_new_tokens=12      )

       snake_case_ =   tokenizer(sentences[0]	,   return_tensors='''tf'''      ).input_ids
       snake_case_ =   model.generate(input_ids=_UpperCAmelCase	,   max_new_tokens=12      )

       snake_case_ =   tokenizer(sentences[1]	,   return_tensors='''tf'''      ).input_ids
       snake_case_ =   model.generate(input_ids=_UpperCAmelCase	,   max_new_tokens=12      )

       snake_case_ =   tokenizer.batch_decode(_UpperCAmelCase	,   skip_special_tokens=_UpperCAmelCase      )
       snake_case_ =   tokenizer.decode(output_non_padded[0]	,   skip_special_tokens=_UpperCAmelCase      )
       snake_case_ =   tokenizer.decode(output_padded[0]	,   skip_special_tokens=_UpperCAmelCase      )

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



import numpy as np

import datasets


UpperCAmelCase    =					"""
Compute the Mahalanobis Distance

Mahalonobis distance is the distance between a point and a distribution.
And not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.
It was introduced by Prof. P. C. Mahalanobis in 1936
and has been used in various statistical applications ever since
[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]
"""

UpperCAmelCase    =					"""\
@article{de2000mahalanobis,
  title={The mahalanobis distance},
  author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},
  journal={Chemometrics and intelligent laboratory systems},
  volume={50},
  number={1},
  pages={1--18},
  year={2000},
  publisher={Elsevier}
}
"""

UpperCAmelCase    =					"""
Args:
    X: List of datapoints to be compared with the `reference_distribution`.
    reference_distribution: List of datapoints from the reference distribution we want to compare to.
Returns:
    mahalanobis: The Mahalonobis distance for each datapoint in `X`.
Examples:

    >>> mahalanobis_metric = datasets.load_metric(\"mahalanobis\")
    >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])
    >>> print(results)
    {'mahalanobis': array([0.5])}
"""







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION      ,		_KWARGS_DESCRIPTION )
class lowerCAmelCase_		(				datasets.Metric ):

 '''simple docstring'''

 def 		UpperCamelCase__  (			self      ):
       return datasets.MetricInfo(
           description=_DESCRIPTION	,   citation=_CITATION	,   inputs_description=_KWARGS_DESCRIPTION	,   features=datasets.Features(
               {
                   '''X''': datasets.Sequence(datasets.Value('''float'''	,   id='''sequence'''      )	,   id='''X'''      ),
               }      )	,   )





 def 		UpperCamelCase__  (			self	,   _UpperCAmelCase	,   _UpperCAmelCase      ):
       # convert to numpy arrays
       snake_case_ =   np.array(_UpperCAmelCase      )
       snake_case_ =   np.array(_UpperCAmelCase      )

       # Assert that arrays are 2D
       if len(X.shape      ) != 2:
             raise ValueError('''Expected `X` to be a 2D vector'''      )
       if len(reference_distribution.shape      ) != 2:
             raise ValueError('''Expected `reference_distribution` to be a 2D vector'''      )
       if reference_distribution.shape[0] < 2:
             raise ValueError(
                 '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension'''      )

       # Get mahalanobis distance for each prediction
       snake_case_ =   X - np.mean(_UpperCAmelCase      )
       snake_case_ =   np.cov(reference_distribution.T      )
       try:
             snake_case_ =   np.linalg.inv(_UpperCAmelCase      )
       except np.linalg.LinAlgError:
             snake_case_ =   np.linalg.pinv(_UpperCAmelCase      )
       snake_case_ =   np.dot(_UpperCAmelCase	,   _UpperCAmelCase      )
       snake_case_ =   np.dot(_UpperCAmelCase	,   X_minus_mu.T      ).diagonal()

       return {"mahalanobis": mahal_dist}