'''simple docstring'''





import copy
import os
from typing import Dict, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import add_start_docstrings, logging
from ..auto import AutoConfig


UpperCamelCase       :     Optional[Any]			=    logging.get_logger(__name__)


UpperCamelCase       :     Dict			=    {
    """suno/bark-small""": """https://huggingface.co/suno/bark-small/resolve/main/config.json""",
    """suno/bark""": """https://huggingface.co/suno/bark/resolve/main/config.json""",
}

UpperCamelCase       :     Dict			=    """
    This is the configuration class to store the configuration of a [`{model}`]. It is used to instantiate the model
    according to the specified arguments, defining the model architecture. Instantiating a configuration with the
    defaults will yield a similar configuration to that of the Bark [suno/bark](https://huggingface.co/suno/bark)
    architecture.

    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
    documentation from [`PretrainedConfig`] for more information.

    Args:
        block_size (`int`, *optional*, defaults to 1024):
            The maximum sequence length that this model might ever be used with. Typically set this to something large
            just in case (e.g., 512 or 1024 or 2048).
        input_vocab_size (`int`, *optional*, defaults to 10_048):
            Vocabulary size of a Bark sub-model. Defines the number of different tokens that can be represented by the
            `inputs_ids` passed when calling [`{model}`]. Defaults to 10_048 but should be carefully thought with
            regards to the chosen sub-model.
        output_vocab_size (`int`, *optional*, defaults to 10_048):
            Output vocabulary size of a Bark sub-model. Defines the number of different tokens that can be represented
            by the: `output_ids` when passing forward a [`{model}`]. Defaults to 10_048 but should be carefully thought
            with regards to the chosen sub-model.
        num_layers (`int`, *optional*, defaults to 12):
            Number of hidden layers in the given sub-model.
        num_heads (`int`, *optional*, defaults to 12):
            Number of attention heads for each attention layer in the Transformer architecture.
        hidden_size (`int`, *optional*, defaults to 768):
            Dimensionality of the \"intermediate\" (often named feed-forward) layer in the architecture.
        dropout (`float`, *optional*, defaults to 0.0):
            The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
        bias (`bool`, *optional*, defaults to `True`):
            Whether or not to use bias in the linear layers and layer norm layers.
        initializer_range (`float`, *optional*, defaults to 0.02):
            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
        use_cache (`bool`, *optional*, defaults to `True`):
            Whether or not the model should return the last key/values attentions (not used by all models).
"""



class   UpperCamelCase					(       a_       ):







    """simple docstring"""





    A     :    List[str]				=			"bark_module"
    A     :    Tuple				=			["past_key_values"]

    A     :    Optional[Any]				=			{
        "num_attention_heads": "num_heads",
        "num_hidden_layers": "num_layers",
        "vocab_size": "input_vocab_size",
        "window_size": "block_size",
    }





    def __init__(					self     :						str							,							UpperCAmelCase_     :						Dict=1_0_2_4							,							UpperCAmelCase_     :						Any=1_0_0_4_8							,							UpperCAmelCase_     :						Optional[int]=1_0_0_4_8							,							UpperCAmelCase_     :						str=1_2							,							UpperCAmelCase_     :						str=1_2							,							UpperCAmelCase_     :						str=7_6_8							,							UpperCAmelCase_     :						Any=0.0							,							UpperCAmelCase_     :						Dict=True							,							UpperCAmelCase_     :						Tuple=0.02							,							UpperCAmelCase_     :						Optional[Any]=True							,							**UpperCAmelCase_     :						str							,							):


         """simple docstring"""







         a					:							Tuple							= block_size
         a					:							Optional[Any]							= input_vocab_size
         a					:							Optional[Any]							= output_vocab_size
         a					:							int							= num_layers
         a					:							Optional[Any]							= num_heads
         a					:							List[Any]							= hidden_size
         a					:							Union[str, Any]							= dropout
         a					:							Union[str, Any]							= bias
         a					:							List[str]							= use_cache
         a					:							int							= initializer_range

         super().__init__(**UpperCAmelCase_)







    @classmethod
    def    SCREAMING_SNAKE_CASE_     (					cls     :						List[str]							,							UpperCAmelCase_     :						Union[str, os.PathLike]							,							UpperCAmelCase_     :						Optional[Union[str, os.PathLike]] = None							,							UpperCAmelCase_     :						bool = False							,							UpperCAmelCase_     :						bool = False							,							UpperCAmelCase_     :						Optional[Union[str, bool]] = None							,							UpperCAmelCase_     :						str = "main"							,							**UpperCAmelCase_     :						Tuple							,							):


         """simple docstring"""







         a					:							Tuple							= cache_dir
         a					:							List[str]							= force_download
         a					:							Dict							= local_files_only
         a					:							Union[str, Any]							= revision

         cls._set_token_in_kwargs(UpperCAmelCase_							,							UpperCAmelCase_)

         a							,			a					:							Tuple							= cls.get_config_dict(UpperCAmelCase_							,							**UpperCAmelCase_)

         # get the config dict if we are loading from Bark
         if config_dict.get('model_type') == "bark":
              a					:							Any							= config_dict[f"""{cls.model_type}_config"""]

         if "model_type" in config_dict and hasattr(cls							,							'model_type') and config_dict["model_type"] != cls.model_type:
              logger.warning(
                  f"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                  f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors.""")

         return cls.from_dict(UpperCAmelCase_							,							**UpperCAmelCase_)






@add_start_docstrings(
    BARK_SUBMODELCONFIG_START_DOCSTRING.format(config="BarkSemanticConfig"    ,     model="BarkSemanticModel"       )    ,     "\n    Example:\n\n    ```python\n    >>> from transformers import BarkSemanticConfig, BarkSemanticModel\n\n    >>> # Initializing a Bark sub-module style configuration\n    >>> configuration = BarkSemanticConfig()\n\n    >>> # Initializing a model (with random weights) from the suno/bark style configuration\n    >>> model = BarkSemanticModel(configuration)\n\n    >>> # Accessing the model configuration\n    >>> configuration = model.config\n    ```"    ,     )
class   UpperCamelCase					(       a_       ):







    """simple docstring"""





    A     :    Any				=			"semantic"






@add_start_docstrings(
    BARK_SUBMODELCONFIG_START_DOCSTRING.format(config="BarkCoarseConfig"    ,     model="BarkCoarseModel"       )    ,     "\n    Example:\n\n    ```python\n    >>> from transformers import BarkCoarseConfig, BarkCoarseModel\n\n    >>> # Initializing a Bark sub-module style configuration\n    >>> configuration = BarkCoarseConfig()\n\n    >>> # Initializing a model (with random weights) from the suno/bark style configuration\n    >>> model = BarkCoarseModel(configuration)\n\n    >>> # Accessing the model configuration\n    >>> configuration = model.config\n    ```"    ,     )
class   UpperCamelCase					(       a_       ):







    """simple docstring"""





    A     :    str				=			"coarse_acoustics"






@add_start_docstrings(
    BARK_SUBMODELCONFIG_START_DOCSTRING.format(config="BarkFineConfig"    ,     model="BarkFineModel"       )    ,     "\n        n_codes_total (`int`, *optional*, defaults to 8):\n            The total number of audio codebooks predicted. Used in the fine acoustics sub-model.\n        n_codes_given (`int`, *optional*, defaults to 1):\n            The number of audio codebooks predicted in the coarse acoustics sub-model. Used in the acoustics\n            sub-models.\n    Example:\n\n    ```python\n    >>> from transformers import BarkFineConfig, BarkFineModel\n\n    >>> # Initializing a Bark sub-module style configuration\n    >>> configuration = BarkFineConfig()\n\n    >>> # Initializing a model (with random weights) from the suno/bark style configuration\n    >>> model = BarkFineModel(configuration)\n\n    >>> # Accessing the model configuration\n    >>> configuration = model.config\n    ```"    ,     )
class   UpperCamelCase					(       a_       ):







    """simple docstring"""





    A     :    Optional[Any]				=			"fine_acoustics"





    def __init__(					self     :						List[Any]							,							UpperCAmelCase_     :						List[str]=True							,							UpperCAmelCase_     :						int=8							,							UpperCAmelCase_     :						List[str]=1							,							**UpperCAmelCase_     :						Union[str, Any]):


         """simple docstring"""







         a					:							List[Any]							= n_codes_total
         a					:							Any							= n_codes_given

         super().__init__(tie_word_embeddings=UpperCAmelCase_							,							**UpperCAmelCase_)






class   UpperCamelCase					(       a_       ):







    """simple docstring"""





    A     :    Optional[int]				=			"bark"
    A     :    Tuple				=			True





    def __init__(					self     :						str							,							UpperCAmelCase_     :						Dict = None							,							UpperCAmelCase_     :						Dict = None							,							UpperCAmelCase_     :						Dict = None							,							UpperCAmelCase_     :						Dict = None							,							UpperCAmelCase_     :						Optional[Any]=0.02							,							**UpperCAmelCase_     :						List[str]							,							):


         """simple docstring"""







         if semantic_config is None:
              a					:							int							= {}
              logger.info('semantic_config is None. initializing the semantic model with default values.')

         if coarse_acoustics_config is None:
              a					:							List[Any]							= {}
              logger.info('coarse_acoustics_config is None. initializing the coarse model with default values.')

         if fine_acoustics_config is None:
              a					:							int							= {}
              logger.info('fine_acoustics_config is None. initializing the fine model with default values.')

         if codec_config is None:
              a					:							List[Any]							= {}
              logger.info('codec_config is None. initializing the codec model with default values.')

         a					:							Any							= BarkSemanticConfig(**UpperCAmelCase_)
         a					:							Dict							= BarkCoarseConfig(**UpperCAmelCase_)
         a					:							List[str]							= BarkFineConfig(**UpperCAmelCase_)
         a					:							Tuple							= AutoConfig.for_model(**UpperCAmelCase_)

         a					:							List[Any]							= initializer_range

         super().__init__(**UpperCAmelCase_)





    @classmethod
    def    SCREAMING_SNAKE_CASE_     (					cls     :						Union[str, Any]							,							UpperCAmelCase_     :						BarkSemanticConfig							,							UpperCAmelCase_     :						BarkCoarseConfig							,							UpperCAmelCase_     :						BarkFineConfig							,							UpperCAmelCase_     :						AutoConfig							,							**UpperCAmelCase_     :						List[str]							,							):


         """simple docstring"""







         return cls(
             semantic_config=semantic_config.to_dict()							,							coarse_acoustics_config=coarse_acoustics_config.to_dict()							,							fine_acoustics_config=fine_acoustics_config.to_dict()							,							codec_config=codec_config.to_dict()							,							**UpperCAmelCase_							,							)







    def    SCREAMING_SNAKE_CASE_     (					self     :						Union[str, Any]):


         """simple docstring"""







         a					:							Optional[Any]							= copy.deepcopy(self.__dict__)

         a					:							str							= self.semantic_config.to_dict()
         a					:							List[Any]							= self.coarse_acoustics_config.to_dict()
         a					:							Union[str, Any]							= self.fine_acoustics_config.to_dict()
         a					:							List[str]							= self.codec_config.to_dict()

         a					:							str							= self.__class__.model_type
         return output


'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import CTRLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
      import tensorflow as tf

      from transformers.models.ctrl.modeling_tf_ctrl import (
          TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFCTRLForSequenceClassification,
          TFCTRLLMHeadModel,
          TFCTRLModel,
      )



class   UpperCamelCase					(       a_       ):







    """simple docstring"""





    def __init__(					self     :						Dict							,							UpperCAmelCase_     :						List[str]							,							):


         """simple docstring"""







         a					:							Any							= parent
         a					:							Tuple							= 1_3
         a					:							Any							= 7
         a					:							Any							= True
         a					:							Optional[Any]							= True
         a					:							Optional[Any]							= True
         a					:							Dict							= True
         a					:							List[str]							= True
         a					:							Dict							= 9_9
         a					:							Tuple							= 3_2
         a					:							Optional[int]							= 2
         a					:							str							= 4
         a					:							str							= 3_7
         a					:							Union[str, Any]							= 'gelu'
         a					:							Any							= 0.1
         a					:							Any							= 0.1
         a					:							List[Any]							= 5_1_2
         a					:							Any							= 1_6
         a					:							int							= 2
         a					:							Dict							= 0.02
         a					:							Optional[int]							= 3
         a					:							Union[str, Any]							= 4
         a					:							int							= None
         a					:							Tuple							= self.vocab_size - 1





    def    SCREAMING_SNAKE_CASE_     (					self     :						int):


         """simple docstring"""







         a					:							Optional[int]							= ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size)

         a					:							List[str]							= None
         if self.use_input_mask:
              a					:							Optional[Any]							= random_attention_mask([self.batch_size, self.seq_length])

         a					:							List[Any]							= None
         if self.use_token_type_ids:
              a					:							Any							= ids_tensor([self.batch_size, self.seq_length]							,							self.type_vocab_size)

         a					:							List[str]							= None
         if self.use_mc_token_ids:
              a					:							List[str]							= ids_tensor([self.batch_size, self.num_choices]							,							self.seq_length)

         a					:							str							= None
         a					:							Optional[Any]							= None
         a					:							Optional[int]							= None
         if self.use_labels:
              a					:							Tuple							= ids_tensor([self.batch_size]							,							self.type_sequence_label_size)
              a					:							Any							= ids_tensor([self.batch_size, self.seq_length]							,							self.num_labels)
              a					:							List[Any]							= ids_tensor([self.batch_size]							,							self.num_choices)

         a					:							Dict							= CTRLConfig(
             vocab_size=self.vocab_size							,							n_embd=self.hidden_size							,							n_layer=self.num_hidden_layers							,							n_head=self.num_attention_heads							,							n_positions=self.max_position_embeddings							,							pad_token_id=self.pad_token_id							,							)

         a					:							Tuple							= ids_tensor([self.num_hidden_layers, self.num_attention_heads]							,							2)

         return (
             config,
             input_ids,
             input_mask,
             head_mask,
             token_type_ids,
             mc_token_ids,
             sequence_labels,
             token_labels,
             choice_labels,
         )





    def    SCREAMING_SNAKE_CASE_     (					self     :						Union[str, Any]							,							UpperCAmelCase_     :						Optional[Any]							,							UpperCAmelCase_     :						Optional[Any]							,							UpperCAmelCase_     :						Dict							,							UpperCAmelCase_     :						List[Any]							,							UpperCAmelCase_     :						str							,							*UpperCAmelCase_     :						List[str]):


         """simple docstring"""







         a					:							Union[str, Any]							= TFCTRLModel(config=UpperCAmelCase_)
         a					:							Dict							= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
         a					:							Tuple							= model(UpperCAmelCase_)

         a					:							str							= [input_ids, None, input_mask]  # None is the input for 'past'
         a					:							Any							= model(UpperCAmelCase_)

         a					:							List[str]							= model(UpperCAmelCase_)

         self.parent.assertEqual(result.last_hidden_state.shape							,							(self.batch_size, self.seq_length, self.hidden_size))





    def    SCREAMING_SNAKE_CASE_     (					self     :						List[str]							,							UpperCAmelCase_     :						Optional[Any]							,							UpperCAmelCase_     :						List[str]							,							UpperCAmelCase_     :						Dict							,							UpperCAmelCase_     :						Tuple							,							UpperCAmelCase_     :						Dict							,							*UpperCAmelCase_     :						str):


         """simple docstring"""







         a					:							Optional[int]							= TFCTRLLMHeadModel(config=UpperCAmelCase_)
         a					:							Tuple							= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
         a					:							int							= model(UpperCAmelCase_)
         self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.vocab_size))





    def    SCREAMING_SNAKE_CASE_     (					self     :						Tuple							,							UpperCAmelCase_     :						str							,							UpperCAmelCase_     :						Optional[Any]							,							UpperCAmelCase_     :						int							,							UpperCAmelCase_     :						Tuple							,							UpperCAmelCase_     :						Optional[Any]							,							*UpperCAmelCase_     :						List[str]):


         """simple docstring"""







         a					:							Tuple							= self.num_labels
         a					:							List[str]							= ids_tensor([self.batch_size]							,							self.type_sequence_label_size)
         a					:							int							= {
             'input_ids': input_ids,
             'token_type_ids': token_type_ids,
             'labels': sequence_labels,
         }
         a					:							str							= TFCTRLForSequenceClassification(UpperCAmelCase_)
         a					:							Union[str, Any]							= model(UpperCAmelCase_)
         self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_labels))







    def    SCREAMING_SNAKE_CASE_     (					self     :						int):


         """simple docstring"""







         a					:							Optional[int]							= self.prepare_config_and_inputs()

         (
             (
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			(
             a
         )							,			
         )					:							int							= config_and_inputs

         a					:							Tuple							= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
         return config, inputs_dict






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







    """simple docstring"""





    A     :    str				=			(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
    A     :    Optional[int]				=			(TFCTRLLMHeadModel,) if is_tf_available() else ()
    A     :    str				=			(
        {
            "feature-extraction": TFCTRLModel,
            "text-classification": TFCTRLForSequenceClassification,
            "text-generation": TFCTRLLMHeadModel,
            "zero-shot": TFCTRLForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    A     :    int				=			False
    A     :    Optional[Any]				=			False





    def    SCREAMING_SNAKE_CASE_     (					self     :						Union[str, Any]							,							UpperCAmelCase_     :						Dict							,							UpperCAmelCase_     :						Any							,							UpperCAmelCase_     :						Optional[Any]							,							UpperCAmelCase_     :						Tuple							,							UpperCAmelCase_     :						Optional[Any]):


         """simple docstring"""







         if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
              # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
              # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
              # config could not be created.
              return True

         return False





    def    SCREAMING_SNAKE_CASE_     (					self     :						Optional[Any]):


         """simple docstring"""







         a					:							Any							= TFCTRLModelTester(self)
         a					:							Dict							= ConfigTester(self							,							config_class=UpperCAmelCase_							,							n_embd=3_7)





    def    SCREAMING_SNAKE_CASE_     (					self     :						List[Any]):


         """simple docstring"""







         self.config_tester.run_common_tests()





    def    SCREAMING_SNAKE_CASE_     (					self     :						str):


         """simple docstring"""







         a					:							Optional[Any]							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase_)





    def    SCREAMING_SNAKE_CASE_     (					self     :						Dict):


         """simple docstring"""







         a					:							List[Any]							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_ctrl_lm_head(*UpperCAmelCase_)





    def    SCREAMING_SNAKE_CASE_     (					self     :						List[str]):


         """simple docstring"""







         a					:							str							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_ctrl_for_sequence_classification(*UpperCAmelCase_)





    def    SCREAMING_SNAKE_CASE_     (					self     :						Optional[Any]):


         """simple docstring"""







         a							,			a					:							Optional[Any]							= self.model_tester.prepare_config_and_inputs_for_common()
         a					:							int							= [TFCTRLLMHeadModel]
         a					:							int							= [TFCTRLForSequenceClassification]

         for model_class in self.all_model_classes:
              a					:							int							= model_class(UpperCAmelCase_)
              model.build()  # may be needed for the get_bias() call below
              assert isinstance(model.get_input_embeddings()							,							tf.keras.layers.Layer)

              if model_class in list_lm_models:
                   a					:							Optional[Any]							= model.get_output_embeddings()
                   assert isinstance(UpperCAmelCase_							,							tf.keras.layers.Layer)
                   a					:							int							= model.get_bias()
                   assert isinstance(UpperCAmelCase_							,							UpperCAmelCase_)
                   for k, v in name.items():
                        assert isinstance(UpperCAmelCase_							,							tf.Variable)
              elif model_class in list_other_models_with_output_ebd:
                   a					:							List[str]							= model.get_output_embeddings()
                   assert isinstance(UpperCAmelCase_							,							tf.keras.layers.Layer)
                   a					:							List[Any]							= model.get_bias()
                   assert name is None
              else:
                   a					:							List[str]							= model.get_output_embeddings()
                   assert x is None
                   a					:							Optional[Any]							= model.get_bias()
                   assert name is None







    @slow
    def    SCREAMING_SNAKE_CASE_     (					self     :						Dict):


         """simple docstring"""







         for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              a					:							Dict							= TFCTRLModel.from_pretrained(UpperCAmelCase_)
              self.assertIsNotNone(UpperCAmelCase_)






@require_tf
class   UpperCamelCase					(       unittest.TestCase       ):







    """simple docstring"""





    @slow
    def    SCREAMING_SNAKE_CASE_     (					self     :						Tuple):


         """simple docstring"""







         a					:							List[Any]							= TFCTRLLMHeadModel.from_pretrained('ctrl')
         a					:							Union[str, Any]							= tf.convert_to_tensor([[1_1_8_5_9, 0, 1_6_1_1, 8]]							,							dtype=tf.intaa)  # Legal the president is
         a					:							List[Any]							= [
             1_1_8_5_9,
             0,
             1_6_1_1,
             8,
             5,
             1_5_0,
             2_6_4_4_9,
             2,
             1_9,
             3_4_8,
             4_6_9,
             3,
             2_5_9_5,
             4_8,
             2_0_7_4_0,
             2_4_6_5_3_3,
             2_4_6_5_3_3,
             1_9,
             3_0,
             5,
         ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

         a					:							List[str]							= model.generate(UpperCAmelCase_							,							do_sample=UpperCAmelCase_)
         self.assertListEqual(output_ids[0].numpy().tolist()							,							UpperCAmelCase_)

