
'''simple docstring'''

import random
import shutil
import tempfile
import unittest

import numpy as np

from transformers import TaTokenizer, TaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torch
from transformers.utils.import_utils import is_speech_available, is_torch_available


if is_torch_available():
   pass

if is_speech_available():
   from transformers import EncodecFeatureExtractor, MusicgenProcessor


__UpperCAmelCase												=   random.Random()



def        SCREAMING_SNAKE_CASE_  (	snake_case_				:	List[Any]   ,     snake_case_				:	Optional[Any]=1.0   ,     snake_case_				:	Dict=None   ,     snake_case_				:	Any=None							)       ->	List[str]:
 if rng is None:
  SCREAMING_SNAKE_CASE  :  List[str]								=		global_rng

 SCREAMING_SNAKE_CASE  :  Optional[Any]								=		[]
 for batch_idx in range(shape[0]							):
  values.append([]							)
  for _ in range(shape[1]							):
   values[-1].append(rng.random() * scale							)

 return values





@require_torch
@require_sentencepiece
class 		_a     (							unittest.TestCase  ):







   """simple docstring"""




   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  List[Any]								=		'facebook/musicgen-small'
    SCREAMING_SNAKE_CASE  :  Dict								=		tempfile.mkdtemp()






   def 					__a			(       self    ,**__SCREAMING_SNAKE_CASE      ):
    return TaTokenizer.from_pretrained(self.checkpoint    ,**__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self    ,**__SCREAMING_SNAKE_CASE      ):
    return EncodecFeatureExtractor.from_pretrained(self.checkpoint    ,**__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    shutil.rmtree(self.tmpdirname      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		self.get_tokenizer()
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		self.get_feature_extractor()

    SCREAMING_SNAKE_CASE  :  Dict								=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,feature_extractor=__SCREAMING_SNAKE_CASE      )

    processor.save_pretrained(self.tmpdirname      )
    SCREAMING_SNAKE_CASE  :  Tuple								=		MusicgenProcessor.from_pretrained(self.tmpdirname      )

    self.assertEqual(processor.tokenizer.get_vocab()    ,tokenizer.get_vocab()      )
    self.assertIsInstance(processor.tokenizer    ,__SCREAMING_SNAKE_CASE      )

    self.assertEqual(processor.feature_extractor.to_json_string()    ,feature_extractor.to_json_string()      )
    self.assertIsInstance(processor.feature_extractor    ,__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  int								=		MusicgenProcessor(tokenizer=self.get_tokenizer()    ,feature_extractor=self.get_feature_extractor()      )
    processor.save_pretrained(self.tmpdirname      )

    SCREAMING_SNAKE_CASE  :  Optional[int]								=		self.get_tokenizer(bos_token='(BOS)'    ,eos_token='(EOS)'      )
    SCREAMING_SNAKE_CASE  :  Dict								=		self.get_feature_extractor(do_normalize=__SCREAMING_SNAKE_CASE    ,padding_value=1.0      )

    SCREAMING_SNAKE_CASE  :  Optional[int]								=		MusicgenProcessor.from_pretrained(
        self.tmpdirname    ,bos_token='(BOS)'    ,eos_token='(EOS)'    ,do_normalize=__SCREAMING_SNAKE_CASE    ,padding_value=1.0      )

    self.assertEqual(processor.tokenizer.get_vocab()    ,tokenizer_add_kwargs.get_vocab()      )
    self.assertIsInstance(processor.tokenizer    ,__SCREAMING_SNAKE_CASE      )

    self.assertEqual(processor.feature_extractor.to_json_string()    ,feature_extractor_add_kwargs.to_json_string()      )
    self.assertIsInstance(processor.feature_extractor    ,__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  List[Any]								=		self.get_feature_extractor()
    SCREAMING_SNAKE_CASE  :  int								=		self.get_tokenizer()

    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,feature_extractor=__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  List[Any]								=		floats_list((3, 1000)      )

    SCREAMING_SNAKE_CASE  :  str								=		feature_extractor(__SCREAMING_SNAKE_CASE    ,return_tensors='np'      )
    SCREAMING_SNAKE_CASE  :  List[str]								=		processor(__SCREAMING_SNAKE_CASE    ,return_tensors='np'      )

    for key in input_feat_extract.keys():
     self.assertAlmostEqual(input_feat_extract[key].sum()    ,input_processor[key].sum()    ,delta=1e-2      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		self.get_feature_extractor()
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		self.get_tokenizer()

    SCREAMING_SNAKE_CASE  :  str								=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,feature_extractor=__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  List[str]								=		'This is a test string'

    SCREAMING_SNAKE_CASE  :  Tuple								=		processor(text=__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  Any								=		tokenizer(__SCREAMING_SNAKE_CASE      )

    for key in encoded_tok.keys():
     self.assertListEqual(encoded_tok[key]    ,encoded_processor[key]      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Tuple								=		self.get_feature_extractor()
    SCREAMING_SNAKE_CASE  :  List[str]								=		self.get_tokenizer()

    SCREAMING_SNAKE_CASE  :  List[Any]								=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,feature_extractor=__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  Tuple								=		[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

    SCREAMING_SNAKE_CASE  :  List[Any]								=		processor.batch_decode(sequences=__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		tokenizer.batch_decode(__SCREAMING_SNAKE_CASE      )

    self.assertListEqual(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  int								=		self.get_feature_extractor()
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		self.get_tokenizer()

    SCREAMING_SNAKE_CASE  :  str								=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,feature_extractor=__SCREAMING_SNAKE_CASE      )

    self.assertListEqual(
        processor.model_input_names    ,feature_extractor.model_input_names    ,msg='`processor` and `feature_extractor` model input names do not match'    ,)






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		self.get_feature_extractor(padding_side='left'      )
    SCREAMING_SNAKE_CASE  :  Any								=		self.get_tokenizer()

    SCREAMING_SNAKE_CASE  :  Tuple								=		MusicgenProcessor(tokenizer=__SCREAMING_SNAKE_CASE    ,feature_extractor=__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  Dict								=		[floats_list((1, x)      )[0] for x in range(5    ,20    ,5      )]
    SCREAMING_SNAKE_CASE  :  Any								=		processor(__SCREAMING_SNAKE_CASE      ).padding_mask

    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		np.asarray(floats_list((3, 20)      )      )[:, None, :]
    SCREAMING_SNAKE_CASE  :  Any								=		processor.batch_decode(__SCREAMING_SNAKE_CASE    ,padding_mask=__SCREAMING_SNAKE_CASE      )

    self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )

    for audio in decoded_audios:
     self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,np.ndarray      )

    self.assertTrue(decoded_audios[0].shape == (1, 10)      )
    self.assertTrue(decoded_audios[1].shape == (1, 15)      )
    self.assertTrue(decoded_audios[2].shape == (1, 20)      )




'''simple docstring'''

from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


__UpperCAmelCase												=   logging.get_logger(__name__)

__UpperCAmelCase												=   {
    'gpt2': 'https://huggingface.co/gpt2/resolve/main/config.json',
    'gpt2-medium': 'https://huggingface.co/gpt2-medium/resolve/main/config.json',
    'gpt2-large': 'https://huggingface.co/gpt2-large/resolve/main/config.json',
    'gpt2-xl': 'https://huggingface.co/gpt2-xl/resolve/main/config.json',
    'distilgpt2': 'https://huggingface.co/distilgpt2/resolve/main/config.json',
}


class 		_a     (							SCREAMING_SNAKE_CASE  ):







   """simple docstring"""




   A		= 'gpt2'
   A		= ['past_key_values']
   A		= {
       'hidden_size': 'n_embd',
       'max_position_embeddings': 'n_positions',
       'num_attention_heads': 'n_head',
       'num_hidden_layers': 'n_layer',
   }






   def __init__(       self    ,__SCREAMING_SNAKE_CASE=50257    ,__SCREAMING_SNAKE_CASE=1024    ,__SCREAMING_SNAKE_CASE=768    ,__SCREAMING_SNAKE_CASE=12    ,__SCREAMING_SNAKE_CASE=12    ,__SCREAMING_SNAKE_CASE=None    ,__SCREAMING_SNAKE_CASE="gelu_new"    ,__SCREAMING_SNAKE_CASE=0.1    ,__SCREAMING_SNAKE_CASE=0.1    ,__SCREAMING_SNAKE_CASE=0.1    ,__SCREAMING_SNAKE_CASE=1e-5    ,__SCREAMING_SNAKE_CASE=0.02    ,__SCREAMING_SNAKE_CASE="cls_index"    ,__SCREAMING_SNAKE_CASE=True    ,__SCREAMING_SNAKE_CASE=None    ,__SCREAMING_SNAKE_CASE=True    ,__SCREAMING_SNAKE_CASE=0.1    ,__SCREAMING_SNAKE_CASE=True    ,__SCREAMING_SNAKE_CASE=True    ,__SCREAMING_SNAKE_CASE=50256    ,__SCREAMING_SNAKE_CASE=50256    ,__SCREAMING_SNAKE_CASE=False    ,__SCREAMING_SNAKE_CASE=False    ,**__SCREAMING_SNAKE_CASE    ,):
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		vocab_size
    SCREAMING_SNAKE_CASE  :  Tuple								=		n_positions
    SCREAMING_SNAKE_CASE  :  int								=		n_embd
    SCREAMING_SNAKE_CASE  :  Dict								=		n_layer
    SCREAMING_SNAKE_CASE  :  Dict								=		n_head
    SCREAMING_SNAKE_CASE  :  List[Any]								=		n_inner
    SCREAMING_SNAKE_CASE  :  Dict								=		activation_function
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		resid_pdrop
    SCREAMING_SNAKE_CASE  :  str								=		embd_pdrop
    SCREAMING_SNAKE_CASE  :  int								=		attn_pdrop
    SCREAMING_SNAKE_CASE  :  Tuple								=		layer_norm_epsilon
    SCREAMING_SNAKE_CASE  :  Tuple								=		initializer_range
    SCREAMING_SNAKE_CASE  :  Any								=		summary_type
    SCREAMING_SNAKE_CASE  :  List[Any]								=		summary_use_proj
    SCREAMING_SNAKE_CASE  :  int								=		summary_activation
    SCREAMING_SNAKE_CASE  :  int								=		summary_first_dropout
    SCREAMING_SNAKE_CASE  :  List[Any]								=		summary_proj_to_labels
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		scale_attn_weights
    SCREAMING_SNAKE_CASE  :  Any								=		use_cache
    SCREAMING_SNAKE_CASE  :  List[str]								=		scale_attn_by_inverse_layer_idx
    SCREAMING_SNAKE_CASE  :  Any								=		reorder_and_upcast_attn

    SCREAMING_SNAKE_CASE  :  Tuple								=		bos_token_id
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		eos_token_id

    super().__init__(bos_token_id=__SCREAMING_SNAKE_CASE    ,eos_token_id=__SCREAMING_SNAKE_CASE    ,**__SCREAMING_SNAKE_CASE      )



class 		_a     (							SCREAMING_SNAKE_CASE  ):







   """simple docstring"""




   def __init__(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE = "default"    ,__SCREAMING_SNAKE_CASE = None    ,__SCREAMING_SNAKE_CASE = False    ,):
    super().__init__(__SCREAMING_SNAKE_CASE    ,task=__SCREAMING_SNAKE_CASE    ,patching_specs=__SCREAMING_SNAKE_CASE    ,use_past=__SCREAMING_SNAKE_CASE      )
    if not getattr(self._config    ,'pad_token_id'    ,__SCREAMING_SNAKE_CASE      ):
     # TODO: how to do that better?
     SCREAMING_SNAKE_CASE  :  int								=		0






   @property
   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Dict								=		OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}      )
    if self.use_past:
     self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE    ,direction='inputs'      )
     SCREAMING_SNAKE_CASE  :  Dict								=		{0: 'batch', 1: 'past_sequence + sequence'}
    else:
     SCREAMING_SNAKE_CASE  :  Optional[Any]								=		{0: 'batch', 1: 'sequence'}

    return common_inputs






   @property
   def 					__a			(       self      ):
    return self._config.n_layer






   @property
   def 					__a			(       self      ):
    return self._config.n_head






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE = -1    ,__SCREAMING_SNAKE_CASE = -1    ,__SCREAMING_SNAKE_CASE = False    ,__SCREAMING_SNAKE_CASE = None    ,):
    SCREAMING_SNAKE_CASE  :  List[str]								=		super(__SCREAMING_SNAKE_CASE    ,self      ).generate_dummy_inputs(
        __SCREAMING_SNAKE_CASE    ,batch_size=__SCREAMING_SNAKE_CASE    ,seq_length=__SCREAMING_SNAKE_CASE    ,is_pair=__SCREAMING_SNAKE_CASE    ,framework=__SCREAMING_SNAKE_CASE      )

    # We need to order the input in the way they appears in the forward()
    SCREAMING_SNAKE_CASE  :  int								=		OrderedDict({'input_ids': common_inputs['input_ids']}      )

    # Need to add the past_keys
    if self.use_past:
     if not is_torch_available():
      raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'      )
     else:
      import torch

      SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  List[str]								=		common_inputs['input_ids'].shape
      # Not using the same length for past_key_values
      SCREAMING_SNAKE_CASE  :  str								=		seqlen + 2
      SCREAMING_SNAKE_CASE  :  Dict								=		(
          batch,
          self.num_attention_heads,
          past_key_values_length,
          self._config.hidden_size // self.num_attention_heads,
      )
      SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		[
          (torch.zeros(__SCREAMING_SNAKE_CASE      ), torch.zeros(__SCREAMING_SNAKE_CASE      )) for _ in range(self.num_layers      )
      ]

    SCREAMING_SNAKE_CASE  :  Optional[int]								=		common_inputs['attention_mask']
    if self.use_past:
     SCREAMING_SNAKE_CASE  :  Tuple								=		ordered_inputs['attention_mask'].dtype
     SCREAMING_SNAKE_CASE  :  Any								=		torch.cat(
         [ordered_inputs['attention_mask'], torch.ones(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,dtype=__SCREAMING_SNAKE_CASE      )]    ,dim=1      )

    return ordered_inputs






   @property
   def 					__a			(       self      ):
    return 13


