


from typing import List, Optional, Union

import numpy as np

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


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



class    UpperCAmelCase_	(							_a):
       '''simple docstring'''
       __UpperCamelCase      :     Any							=					["input_values", "padding_mask"]






       def __init__(     self ,					__SCREAMING_SNAKE_CASE = 1 ,					__SCREAMING_SNAKE_CASE = 24_000 ,					__SCREAMING_SNAKE_CASE = 0.0 ,					__SCREAMING_SNAKE_CASE = None ,					__SCREAMING_SNAKE_CASE = None ,					**__SCREAMING_SNAKE_CASE ,					):




              """simple docstring"""




              super().__init__(feature_size=__SCREAMING_SNAKE_CASE ,					sampling_rate=__SCREAMING_SNAKE_CASE ,					padding_value=__SCREAMING_SNAKE_CASE ,					**__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	str   	=		chunk_length_s
              UpperCamelCase    :	str   	=		overlap






       @property
       def  _lowercase   (     self						):




              """simple docstring"""




              if self.chunk_length_s is None:
                     return None
              else:
                     return int(self.chunk_length_s * self.sampling_rate						)






       @property
       def  _lowercase   (     self						):




              """simple docstring"""




              if self.chunk_length_s is None or self.overlap is None:
                     return None
              else:
                     return max(1 ,					int((1.0 - self.overlap) * self.chunk_length						)						)






       def __call__(     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE = None ,					__SCREAMING_SNAKE_CASE = False ,					__SCREAMING_SNAKE_CASE = None ,					__SCREAMING_SNAKE_CASE = None ,					__SCREAMING_SNAKE_CASE = None ,					):




              """simple docstring"""




              if sampling_rate is not None:
                     if sampling_rate != self.sampling_rate:
                            raise ValueError(
                                f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of"""
                                f""" {self.sampling_rate}. Please make sure that the provided audio input was sampled with"""
                                f""" {self.sampling_rate} and not {sampling_rate}."""						)
              else:
                     logger.warning(
                         '''It is strongly recommended to pass the `sampling_rate` argument to this function. '''
                         '''Failing to do so can result in silent errors that might be hard to debug.'''						)

              if padding and truncation:
                     raise ValueError('''Both padding and truncation were set. Make sure you only set one.'''						)
              elif padding is None:
                     # by default let's pad the inputs
                     UpperCamelCase    :	int   	=		True

              UpperCamelCase    :	Any   	=		bool(
                  isinstance(__SCREAMING_SNAKE_CASE ,					(list, tuple)						) and (isinstance(raw_audio[0] ,					(np.ndarray, tuple, list)						))						)

              if is_batched:
                     UpperCamelCase    :	Any   	=		[np.asarray(__SCREAMING_SNAKE_CASE ,					dtype=np.floataa						).T for audio in raw_audio]
              elif not is_batched and not isinstance(__SCREAMING_SNAKE_CASE ,					np.ndarray						):
                     UpperCamelCase    :	List[str]   	=		np.asarray(__SCREAMING_SNAKE_CASE ,					dtype=np.floataa						)
              elif isinstance(__SCREAMING_SNAKE_CASE ,					np.ndarray						) and raw_audio.dtype is np.dtype(np.floataa						):
                     UpperCamelCase    :	Optional[int]   	=		raw_audio.astype(np.floataa						)

              # always return batch
              if not is_batched:
                     UpperCamelCase    :	Tuple   	=		[np.asarray(__SCREAMING_SNAKE_CASE						).T]

              # verify inputs are valid
              for idx, example in enumerate(__SCREAMING_SNAKE_CASE						):
                     if example.ndim > 2:
                            raise ValueError(f"""Expected input shape (channels, length) but got shape {example.shape}"""						)
                     if self.feature_size == 1 and example.ndim != 1:
                            raise ValueError(f"""Expected mono audio but example has {example.shape[-1]} channels"""						)
                     if self.feature_size == 2 and example.shape[-1] != 2:
                            raise ValueError(f"""Expected stereo audio but example has {example.shape[-1]} channels"""						)

              UpperCamelCase    :	List[Any]   	=		None
              UpperCamelCase    :	Optional[int]   	=		BatchFeature({'''input_values''': raw_audio}						)
              if self.chunk_stride is not None and self.chunk_length is not None and max_length is None:
                     if truncation:
                            UpperCamelCase    :	Optional[Any]   	=		min(array.shape[0] for array in raw_audio						)
                            UpperCamelCase    :	Tuple   	=		int(np.floor(max_length / self.chunk_stride						)						)
                            UpperCamelCase    :	str   	=		(nb_step - 1) * self.chunk_stride + self.chunk_length
                     elif padding:
                            UpperCamelCase    :	List[str]   	=		max(array.shape[0] for array in raw_audio						)
                            UpperCamelCase    :	Tuple   	=		int(np.ceil(max_length / self.chunk_stride						)						)
                            UpperCamelCase    :	Any   	=		(nb_step - 1) * self.chunk_stride + self.chunk_length
                            UpperCamelCase    :	Dict   	=		'''max_length'''
                     else:
                            UpperCamelCase    :	List[str]   	=		input_values

        # normal padding on batch
              if padded_inputs is None:
                     UpperCamelCase    :	List[str]   	=		self.pad(
                         __SCREAMING_SNAKE_CASE ,					max_length=__SCREAMING_SNAKE_CASE ,					truncation=__SCREAMING_SNAKE_CASE ,					padding=__SCREAMING_SNAKE_CASE ,					return_attention_mask=__SCREAMING_SNAKE_CASE ,					)
                     if padding:
                            UpperCamelCase    :	str   	=		padded_inputs.pop('''attention_mask'''						)

              UpperCamelCase    :	Any   	=		[]
              for example in padded_inputs.pop('''input_values'''						):
                     if self.feature_size == 1:
                            UpperCamelCase    :	Optional[int]   	=		example[..., None]
                     input_values.append(example.T						)

              UpperCamelCase    :	Optional[Any]   	=		input_values
              if return_tensors is not None:
                     UpperCamelCase    :	List[str]   	=		padded_inputs.convert_to_tensors(__SCREAMING_SNAKE_CASE						)

              return padded_inputs





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]						)

