
"""simple docstring"""



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 		a_    :
    a   :					Union[str, Any]			     =					XGLMConfig
    a   :					List[str]			     =					{}
    a   :					Union[str, Any]			     =					'gelu'







    def __init__(      self      :							Dict  ,       __UpperCamelCase      :							Optional[int]  ,       __UpperCamelCase      :							Any=14  ,       __UpperCamelCase      :							int=7  ,       __UpperCamelCase      :							Dict=True  ,       __UpperCamelCase      :							Tuple=True  ,       __UpperCamelCase      :							Union[str, Any]=True  ,       __UpperCamelCase      :							Tuple=99  ,       __UpperCamelCase      :							Union[str, Any]=32  ,       __UpperCamelCase      :							List[str]=2  ,       __UpperCamelCase      :							Optional[Any]=4  ,       __UpperCamelCase      :							Union[str, Any]=37  ,       __UpperCamelCase      :							Dict="gelu"  ,       __UpperCamelCase      :							Union[str, Any]=0.1  ,       __UpperCamelCase      :							Tuple=0.1  ,       __UpperCamelCase      :							str=5_12  ,       __UpperCamelCase      :							int=0.0_2  ,       )		->Dict:






        '''simple docstring'''






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







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






        '''simple docstring'''






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







    def   _snake_case       (      self      :							str  )		->Dict:






        '''simple docstring'''






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

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

        _UpperCAmelCase        = self.get_config()

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

        return (
            config,
            input_ids,
            input_mask,
            head_mask,
        )







    def   _snake_case       (      self      :							int  )		->Optional[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=__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   _snake_case       (      self      :							List[Any]  )		->List[str]:






        '''simple docstring'''






        _UpperCAmelCase        = self.prepare_config_and_inputs()

        (
            (
            _UpperCAmelCase
        )     ,(
            _UpperCAmelCase
        )     ,(
            _UpperCAmelCase
        )     ,(
            _UpperCAmelCase
        )     ,
        )        = config_and_inputs

        _UpperCAmelCase        = {
            """input_ids""": input_ids,
            """head_mask""": head_mask,
        }

        return config, inputs_dict







@require_tf
class 		a_    (			_UpperCAmelCase  ,    _UpperCAmelCase  ,    unittest.TestCase						):
    a   :					int			     =					(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
    a   :					List[Any]			     =					(TFXGLMForCausalLM,) if is_tf_available() else ()
    a   :					str			     =					(
        {'feature-extraction': TFXGLMModel, 'text-generation': TFXGLMForCausalLM} if is_tf_available() else {}
    )
    a   :					Dict			     =					False
    a   :					Tuple			     =					False
    a   :					str			     =					False







    def   _snake_case       (      self      :							Optional[Any]  )		->Any:






        '''simple docstring'''






        _UpperCAmelCase        = TFXGLMModelTester(self  )
        _UpperCAmelCase        = ConfigTester(self  ,       config_class=__UpperCamelCase  ,       n_embd=37  )







    def   _snake_case       (      self      :							Optional[int]  )		->Dict:






        '''simple docstring'''






        self.config_tester.run_common_tests()







    @slow
    def   _snake_case       (      self      :							Union[str, Any]  )		->int:






        '''simple docstring'''






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







    @unittest.skip(reason="""Currently, model embeddings are going to undergo a major refactor."""  )
    def   _snake_case       (      self      :							int  )		->Optional[Any]:






        '''simple docstring'''






        super().test_resize_token_embeddings()







@require_tf
class 		a_    (			unittest.TestCase						):







    @slow
    def   _snake_case       (      self      :							List[Any]  ,       __UpperCamelCase      :							List[str]=True  )		->Union[str, Any]:






        '''simple docstring'''






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







    @slow
    def   _snake_case       (      self      :							List[str]  )		->str:






        '''simple docstring'''






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

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

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







    @slow
    def   _snake_case       (      self      :							int  )		->Any:






        '''simple docstring'''






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

        _UpperCAmelCase        = """left"""

        # use different length sentences to test batching
        _UpperCAmelCase        = [
            """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        = tokenizer(__UpperCamelCase  ,       return_tensors="""tf"""  ,       padding=__UpperCamelCase  )
        _UpperCAmelCase        = inputs["""input_ids"""]

        _UpperCAmelCase        = model.generate(input_ids=__UpperCamelCase  ,       attention_mask=inputs["""attention_mask"""]  ,       max_new_tokens=12  )

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

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

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

        _UpperCAmelCase        = [
            """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]  )

"""simple docstring"""



import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
 import torch

 from transformers.generation import DisjunctiveConstraint




@require_torch
class 		a_    (			unittest.TestCase						):







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






        '''simple docstring'''






        _UpperCAmelCase        = [[1, 2, 4], [1, 2, 3, 4]]
        _UpperCAmelCase        = DisjunctiveConstraint(__UpperCamelCase  )
        self.assertTrue(isinstance(dc.token_ids  ,       __UpperCamelCase  )  )

        with self.assertRaises(__UpperCamelCase  ):
            DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]  )  )

        with self.assertRaises(__UpperCamelCase  ):
            DisjunctiveConstraint([torch.LongTensor([1, 2, 4]  ), torch.LongTensor([1, 2, 3, 4, 5]  )]  )







    def   _snake_case       (      self      :							Optional[int]  )		->Dict:






        '''simple docstring'''






        _UpperCAmelCase        = [[1, 2], [1, 2, 3, 4]]

        with self.assertRaises(__UpperCamelCase  ):
            DisjunctiveConstraint(__UpperCamelCase  )  # fails here







    def   _snake_case       (      self      :							Optional[int]  )		->Dict:






        '''simple docstring'''






        _UpperCAmelCase        = [[1, 2, 3], [1, 2, 4]]

        _UpperCAmelCase        = DisjunctiveConstraint(__UpperCamelCase  )

        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase        = dc.update(1  )
        _UpperCAmelCase        = stepped is True and completed is False and reset is False
        self.assertTrue(__UpperCamelCase  )
        self.assertTrue(not dc.completed  )
        self.assertTrue(dc.current_seq == [1]  )

        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase        = dc.update(2  )
        _UpperCAmelCase        = stepped is True and completed is False and reset is False
        self.assertTrue(__UpperCamelCase  )
        self.assertTrue(not dc.completed  )
        self.assertTrue(dc.current_seq == [1, 2]  )

        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase        = dc.update(3  )
        _UpperCAmelCase        = stepped is True and completed is True and reset is False
        self.assertTrue(__UpperCamelCase  )
        self.assertTrue(dc.completed  )  # Completed!
        self.assertTrue(dc.current_seq == [1, 2, 3]  )







    def   _snake_case       (      self      :							List[Any]  )		->Any:






        '''simple docstring'''






        _UpperCAmelCase        = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

        _UpperCAmelCase        = DisjunctiveConstraint(__UpperCamelCase  )

        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase        = dc.update(1  )
        self.assertTrue(not dc.completed  )
        self.assertTrue(dc.current_seq == [1]  )

        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase        = dc.update(2  )
        self.assertTrue(not dc.completed  )
        self.assertTrue(dc.current_seq == [1, 2]  )

        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase        = dc.update(4  )
        self.assertTrue(not dc.completed  )
        self.assertTrue(dc.current_seq == [1, 2, 4]  )

        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase        = dc.update(5  )
        self.assertTrue(dc.completed  )  # Completed!
        self.assertTrue(dc.current_seq == [1, 2, 4, 5]  )

        dc.reset()

        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase        = dc.update(1  )
        self.assertTrue(not dc.completed  )
        self.assertTrue(dc.remaining() == 3  )
        self.assertTrue(dc.current_seq == [1]  )

        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase        = dc.update(2  )
        self.assertTrue(not dc.completed  )
        self.assertTrue(dc.remaining() == 2  )
        self.assertTrue(dc.current_seq == [1, 2]  )

        _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase        = dc.update(5  )
        self.assertTrue(dc.completed  )  # Completed!
        self.assertTrue(dc.remaining() == 0  )
        self.assertTrue(dc.current_seq == [1, 2, 5]  )