


import unittest

from transformers import FunnelConfig, FunnelTokenizer, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        MODEL_FOR_PRETRAINING_MAPPING,
        FunnelBaseModel,
        FunnelForMaskedLM,
        FunnelForMultipleChoice,
        FunnelForPreTraining,
        FunnelForQuestionAnswering,
        FunnelForSequenceClassification,
        FunnelForTokenClassification,
        FunnelModel,
    )







class      _lowercase					:


       """simple docstring"""




       def __init__(  self				:     Union[str, Any]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     Any=13   , __lowerCamelCase				:     str=7   , __lowerCamelCase				:     int=True   , __lowerCamelCase				:     Dict=True   , __lowerCamelCase				:     Tuple=True   , __lowerCamelCase				:     Dict=True   , __lowerCamelCase				:     List[Any]=99   , __lowerCamelCase				:     Tuple=[1, 1, 2]   , __lowerCamelCase				:     str=1   , __lowerCamelCase				:     List[Any]=32   , __lowerCamelCase				:     Any=4   , __lowerCamelCase				:     Optional[Any]=8   , __lowerCamelCase				:     Optional[int]=37   , __lowerCamelCase				:     Optional[int]="gelu_new"   , __lowerCamelCase				:     Union[str, Any]=0.1   , __lowerCamelCase				:     List[str]=0.1   , __lowerCamelCase				:     int=0.0   , __lowerCamelCase				:     Dict=512   , __lowerCamelCase				:     List[Any]=3   , __lowerCamelCase				:     Optional[int]=0.0_2   , __lowerCamelCase				:     str=3   , __lowerCamelCase				:     Optional[int]=4   , __lowerCamelCase				:     Union[str, Any]=None   , __lowerCamelCase				:     List[Any]=False   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Any       =    parent
             lowerCamelCase__						:							Optional[Any]       =    batch_size
             lowerCamelCase__						:							List[str]       =    seq_length
             lowerCamelCase__						:							Union[str, Any]       =    is_training
             lowerCamelCase__						:							Optional[int]       =    use_input_mask
             lowerCamelCase__						:							List[str]       =    use_token_type_ids
             lowerCamelCase__						:							List[str]       =    use_labels
             lowerCamelCase__						:							str       =    vocab_size
             lowerCamelCase__						:							Optional[Any]       =    block_sizes
             lowerCamelCase__						:							Any       =    num_decoder_layers
             lowerCamelCase__						:							int       =    d_model
             lowerCamelCase__						:							str       =    n_head
             lowerCamelCase__						:							int       =    d_head
             lowerCamelCase__						:							str       =    d_inner
             lowerCamelCase__						:							int       =    hidden_act
             lowerCamelCase__						:							str       =    hidden_dropout
             lowerCamelCase__						:							Optional[Any]       =    attention_dropout
             lowerCamelCase__						:							Dict       =    activation_dropout
             lowerCamelCase__						:							Dict       =    max_position_embeddings
             lowerCamelCase__						:							Tuple       =    type_vocab_size
             lowerCamelCase__						:							Union[str, Any]       =    2
             lowerCamelCase__						:							Dict       =    num_labels
             lowerCamelCase__						:							List[str]       =    num_choices
             lowerCamelCase__						:							List[Any]       =    scope
             lowerCamelCase__						:							List[Any]       =    initializer_std

             # Used in the tests to check the size of the first attention layer
             lowerCamelCase__						:							Tuple       =    n_head
             # Used in the tests to check the size of the first hidden state
             lowerCamelCase__						:							int       =    self.d_model
             # Used in the tests to check the number of output hidden states/attentions
             lowerCamelCase__						:							int       =    sum(self.block_sizes			) + (0 if base else self.num_decoder_layers)
             # FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
             # the last hidden state of the first block (which is the first hidden state of the decoder).
             if not base:
                   lowerCamelCase__						:							int       =    self.num_hidden_layers + 2





       def 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							List[Any]       =    ids_tensor([self.batch_size, self.seq_length]   , self.vocab_size			)

             lowerCamelCase__						:							Union[str, Any]       =    None
             if self.use_input_mask:
                   lowerCamelCase__						:							Optional[int]       =    ids_tensor([self.batch_size, self.seq_length]   , vocab_size=2			)

             lowerCamelCase__						:							Optional[Any]       =    None
             if self.use_token_type_ids:
                   lowerCamelCase__						:							str       =    ids_tensor([self.batch_size, self.seq_length]   , self.type_vocab_size			)

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

             lowerCamelCase__						:							str       =    self.get_config()

             return (
                 config,
                 input_ids,
                 token_type_ids,
                 input_mask,
                 sequence_labels,
                 token_labels,
                 choice_labels,
                 fake_token_labels,
             )





       def 		lowerCAmelCase	(  self				:     Optional[int]			):


             '''simple docstring'''



             return FunnelConfig(
                 vocab_size=self.vocab_size   , block_sizes=self.block_sizes   , num_decoder_layers=self.num_decoder_layers   , d_model=self.d_model   , n_head=self.n_head   , d_head=self.d_head   , d_inner=self.d_inner   , hidden_act=self.hidden_act   , hidden_dropout=self.hidden_dropout   , attention_dropout=self.attention_dropout   , activation_dropout=self.activation_dropout   , max_position_embeddings=self.max_position_embeddings   , type_vocab_size=self.type_vocab_size   , initializer_std=self.initializer_std   , )





       def 		lowerCAmelCase	(  self				:     Optional[Any]   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     str   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     Tuple   , __lowerCamelCase				:     str   , __lowerCamelCase				:     Tuple   , ):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    FunnelModel(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Any       =    model(__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase			)
             lowerCamelCase__						:							Union[str, Any]       =    model(__lowerCamelCase   , token_type_ids=__lowerCamelCase			)
             lowerCamelCase__						:							Any       =    model(__lowerCamelCase			)
             self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.d_model)			)

             lowerCamelCase__						:							str       =    False
             lowerCamelCase__						:							Union[str, Any]       =    model(__lowerCamelCase			)
             self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.d_model)			)

             lowerCamelCase__						:							Optional[Any]       =    False
             lowerCamelCase__						:							Any       =    model(__lowerCamelCase			)
             self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, self.seq_length, self.d_model)			)





       def 		lowerCAmelCase	(  self				:     Optional[int]   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     str   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     str   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     Dict   , __lowerCamelCase				:     Optional[Any]   , ):


             '''simple docstring'''



             lowerCamelCase__						:							List[str]       =    FunnelBaseModel(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							int       =    model(__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase			)
             lowerCamelCase__						:							Tuple       =    model(__lowerCamelCase   , token_type_ids=__lowerCamelCase			)
             lowerCamelCase__						:							List[Any]       =    model(__lowerCamelCase			)
             self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, 2, self.d_model)			)

             lowerCamelCase__						:							Dict       =    False
             lowerCamelCase__						:							str       =    model(__lowerCamelCase			)
             self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, 3, self.d_model)			)

             lowerCamelCase__						:							Union[str, Any]       =    False
             lowerCamelCase__						:							Dict       =    model(__lowerCamelCase			)
             self.parent.assertEqual(result.last_hidden_state.shape   , (self.batch_size, 2, self.d_model)			)





       def 		lowerCAmelCase	(  self				:     List[str]   , __lowerCamelCase				:     int   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     int   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     List[str]   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    self.num_labels
             lowerCamelCase__						:							List[str]       =    FunnelForPreTraining(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Tuple       =    model(__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase   , labels=__lowerCamelCase			)
             self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length)			)





       def 		lowerCAmelCase	(  self				:     Tuple   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     str   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     Tuple   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     str   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    FunnelForMaskedLM(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Union[str, Any]       =    model(__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase   , labels=__lowerCamelCase			)
             self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.vocab_size)			)





       def 		lowerCAmelCase	(  self				:     Any   , __lowerCamelCase				:     int   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     int   , __lowerCamelCase				:     str   , __lowerCamelCase				:     Tuple   , __lowerCamelCase				:     Dict   , __lowerCamelCase				:     Dict   , ):


             '''simple docstring'''



             lowerCamelCase__						:							str       =    self.num_labels
             lowerCamelCase__						:							Optional[Any]       =    FunnelForSequenceClassification(__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Any       =    model(__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase   , labels=__lowerCamelCase			)
             self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_labels)			)





       def 		lowerCAmelCase	(  self				:     Any   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     int   , __lowerCamelCase				:     Dict   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     List[Any]   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    self.num_choices
             lowerCamelCase__						:							Optional[Any]       =    FunnelForMultipleChoice(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Dict       =    input_ids.unsqueeze(1			).expand(-1   , self.num_choices   , -1			).contiguous()
             lowerCamelCase__						:							Union[str, Any]       =    token_type_ids.unsqueeze(1			).expand(-1   , self.num_choices   , -1			).contiguous()
             lowerCamelCase__						:							List[Any]       =    input_mask.unsqueeze(1			).expand(-1   , self.num_choices   , -1			).contiguous()
             lowerCamelCase__						:							Any       =    model(
                 __lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase   , labels=__lowerCamelCase   , )
             self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_choices)			)





       def 		lowerCAmelCase	(  self				:     Union[str, Any]   , __lowerCamelCase				:     int   , __lowerCamelCase				:     int   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Dict   , __lowerCamelCase				:     int   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     str   , ):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    self.num_labels
             lowerCamelCase__						:							List[Any]       =    FunnelForTokenClassification(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							str       =    model(__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase   , labels=__lowerCamelCase			)
             self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.num_labels)			)





       def 		lowerCAmelCase	(  self				:     Any   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     Tuple   , __lowerCamelCase				:     int   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     str   , __lowerCamelCase				:     Tuple   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    FunnelForQuestionAnswering(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Optional[int]       =    model(
                 __lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase   , start_positions=__lowerCamelCase   , end_positions=__lowerCamelCase   , )
             self.parent.assertEqual(result.start_logits.shape   , (self.batch_size, self.seq_length)			)
             self.parent.assertEqual(result.end_logits.shape   , (self.batch_size, self.seq_length)			)





       def 		lowerCAmelCase	(  self				:     str			):


             '''simple docstring'''



             lowerCamelCase__						:							Tuple       =    self.prepare_config_and_inputs()
             (
                 (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  
             )						:							List[str]       =    config_and_inputs
             lowerCamelCase__						:							Dict       =    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
             return config, inputs_dict







@require_torch
class      _lowercase					(				lowercase__				,    lowercase__				,    unittest.TestCase):


       """simple docstring"""




       A__   		=     False
       A__   		=     False
       A__   		=     (
           (
               FunnelModel,
               FunnelForMaskedLM,
               FunnelForPreTraining,
               FunnelForQuestionAnswering,
               FunnelForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       A__   		=     (
           {
               "feature-extraction": (FunnelBaseModel, FunnelModel),
               "fill-mask": FunnelForMaskedLM,
               "question-answering": FunnelForQuestionAnswering,
               "text-classification": FunnelForSequenceClassification,
               "token-classification": FunnelForTokenClassification,
               "zero-shot": FunnelForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )





       def 		lowerCAmelCase	(  self				:     List[Any]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     Tuple=False			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    super()._prepare_for_class(__lowerCamelCase   , __lowerCamelCase   , return_labels=__lowerCamelCase			)

             if return_labels:
                   if model_class in get_values(__lowerCamelCase			):
                         lowerCamelCase__						:							Any       =    torch.zeros(
                             (self.model_tester.batch_size, self.model_tester.seq_length)   , dtype=torch.long   , device=__lowerCamelCase			)
             return inputs_dict





       def 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							List[str]       =    FunnelModelTester(self			)
             lowerCamelCase__						:							Tuple       =    ConfigTester(self   , config_class=__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     str			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_pretraining(*__lowerCamelCase			)





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


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Optional[int]			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     str			):


             '''simple docstring'''



             lowerCamelCase__						:							List[str]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     int   , __lowerCamelCase				:     int			):


             '''simple docstring'''



             if hasattr(__lowerCamelCase   , "weight"			) and module.weight is not None:
                   module.weight.data.fill_(3			)
             if hasattr(__lowerCamelCase   , "bias"			) and module.bias is not None:
                   module.bias.data.fill_(3			)

             for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
                   if hasattr(__lowerCamelCase   , __lowerCamelCase			) and getattr(__lowerCamelCase   , __lowerCamelCase			) is not None:
                         lowerCamelCase__						:							Any       =    getattr(__lowerCamelCase   , __lowerCamelCase			)
                         weight.data.fill_(3			)







@require_torch
class      _lowercase					(				lowercase__				,    unittest.TestCase):


       """simple docstring"""




       A__   		=     False
       A__   		=     False
       A__   		=     (
           (FunnelBaseModel, FunnelForMultipleChoice, FunnelForSequenceClassification) if is_torch_available() else ()
       )





       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							Any       =    FunnelModelTester(self   , base=__lowerCamelCase			)
             lowerCamelCase__						:							Optional[int]       =    ConfigTester(self   , config_class=__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Dict			):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       def 		lowerCAmelCase	(  self				:     Any			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_base_model(*__lowerCamelCase			)





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


             '''simple docstring'''



             lowerCamelCase__						:							Tuple       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase			)





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


             '''simple docstring'''



             lowerCamelCase__						:							List[Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_multiple_choice(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__       ,  lowerCamelCase__						:							Union[str, Any]       =    self.model_tester.prepare_config_and_inputs_for_common()
             lowerCamelCase__						:							List[Any]       =    True

             for model_class in self.all_model_classes:
                   if model_class.__name__ == "FunnelBaseModel":
                         continue
                   lowerCamelCase__						:							str       =    model_class(__lowerCamelCase			)
                   model.to(__lowerCamelCase			)
                   model.train()
                   lowerCamelCase__						:							Union[str, Any]       =    self._prepare_for_class(__lowerCamelCase   , __lowerCamelCase   , return_labels=__lowerCamelCase			)
                   lowerCamelCase__						:							Optional[int]       =    model(**__lowerCamelCase			).loss
                   loss.backward()





       def 		lowerCAmelCase	(  self				:     List[str]   , __lowerCamelCase				:     List[Any]			):


             '''simple docstring'''



             if hasattr(__lowerCamelCase   , "weight"			) and module.weight is not None:
                   module.weight.data.fill_(3			)
             if hasattr(__lowerCamelCase   , "bias"			) and module.bias is not None:
                   module.bias.data.fill_(3			)

             for param in ["r_w_bias", "r_r_bias", "r_kernel", "r_s_bias", "seg_embed"]:
                   if hasattr(__lowerCamelCase   , __lowerCamelCase			) and getattr(__lowerCamelCase   , __lowerCamelCase			) is not None:
                         lowerCamelCase__						:							List[Any]       =    getattr(__lowerCamelCase   , __lowerCamelCase			)
                         weight.data.fill_(3			)







@require_torch
@require_sentencepiece
@require_tokenizers
class      _lowercase					(				unittest.TestCase):


       """simple docstring"""




       def 		lowerCAmelCase	(  self				:     str			):


             '''simple docstring'''



             lowerCamelCase__						:							List[str]       =    13
             lowerCamelCase__						:							Optional[Any]       =    7
             lowerCamelCase__						:							int       =    torch.arange(0   , batch_size * sequence_length			).long().reshape(__lowerCamelCase   , __lowerCamelCase			)
             lowerCamelCase__						:							str       =    [0, 1, 2, 3, 4, 5, 6, 4, 1, 3, 5, 0, 1]
             lowerCamelCase__						:							Optional[int]       =    torch.tensor([[2] + [0] * a + [1] * (sequence_length - a - 1) for a in lengths]			)

             lowerCamelCase__						:							Union[str, Any]       =    FunnelModel.from_pretrained("sgugger/funnel-random-tiny"			)
             lowerCamelCase__						:							Union[str, Any]       =    model(__lowerCamelCase   , token_type_ids=__lowerCamelCase			)[0].abs()

             lowerCamelCase__						:							Optional[int]       =    torch.tensor(2_3_4_4.8_3_5_2			)
             lowerCamelCase__						:							Tuple       =    torch.tensor(0.8_0_5_2			)
             self.assertTrue(torch.allclose(output.sum()   , __lowerCamelCase   , atol=1E-4			)			)
             self.assertTrue(torch.allclose(output.mean()   , __lowerCamelCase   , atol=1E-4			)			)

             lowerCamelCase__						:							str       =    torch.tensor([[1] * 7, [1] * 4 + [0] * 3] * 6 + [[0, 1, 1, 0, 0, 1, 1]]			)
             lowerCamelCase__						:							str       =    model(__lowerCamelCase   , attention_mask=__lowerCamelCase   , token_type_ids=__lowerCamelCase			)[0].abs()

             lowerCamelCase__						:							int       =    torch.tensor(2_3_4_3.8_4_2_5			)
             lowerCamelCase__						:							Any       =    torch.tensor(0.8_0_4_9			)
             self.assertTrue(torch.allclose(output.sum()   , __lowerCamelCase   , atol=1E-4			)			)
             self.assertTrue(torch.allclose(output.mean()   , __lowerCamelCase   , atol=1E-4			)			)





       @slow
       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    FunnelTokenizer.from_pretrained("huggingface/funnel-small"			)
             lowerCamelCase__						:							Dict       =    FunnelModel.from_pretrained("huggingface/funnel-small"			)
             lowerCamelCase__						:							Dict       =    tokenizer("Hello! I am the Funnel Transformer model."   , return_tensors="pt"			)
             lowerCamelCase__						:							str       =    model(**__lowerCamelCase			)[0]

             lowerCamelCase__						:							List[Any]       =    torch.tensor(2_3_5.7_2_4_6			)
             lowerCamelCase__						:							Tuple       =    torch.tensor(0.0_2_5_6			)
             self.assertTrue(torch.allclose(output.sum()   , __lowerCamelCase   , atol=1E-4			)			)
             self.assertTrue(torch.allclose(output.mean()   , __lowerCamelCase   , atol=1E-4			)			)






import unittest
from unittest import skip

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow
from transformers.trainer_utils import set_seed


if is_torch_available():
    import torch

    from transformers import JukeboxModel, JukeboxPrior, JukeboxTokenizer







@require_torch
class      _lowercase					(				unittest.TestCase):


       """simple docstring"""




       A__   		=     (JukeboxModel,) if is_torch_available() else ()
       A__   		=     "openai/jukebox-1b-lyrics"
       A__   		=     {
           "artist": "Zac Brown Band",
           "genres": "Country",
           "lyrics": "I met a traveller from an antique land,\n    Who said \"Two vast and trunkless legs of stone\n    Stand in the desert. . . . Near them, on the sand,\n    Half sunk a shattered visage lies, whose frown,\n    And wrinkled lip, and sneer of cold command,\n    Tell that its sculptor well those passions read\n    Which yet survive, stamped on these lifeless things,\n    The hand that mocked them, and the heart that fed;\n    And on the pedestal, these words appear:\n    My name is Ozymandias, King of Kings;\n    Look on my Works, ye Mighty, and despair!\n    Nothing beside remains. Round the decay\n    Of that colossal Wreck, boundless and bare\n    The lone and level sands stretch far away\n    ",
       }
       # fmt: off
       A__   		=     [
           18_64, 15_36, 12_13, 18_70, 13_57, 15_36, 5_19, 8_80, 13_23, 7_89, 10_82, 5_34,
           10_00, 14_45, 11_05, 11_30, 9_67, 5_15, 14_34, 16_20, 5_34, 14_95, 2_83, 14_45,
           3_33, 13_07, 5_39, 16_31, 15_28, 3_75, 14_34, 6_73, 6_27, 7_10, 7_78, 18_83,
           14_05, 12_76, 14_55, 12_28
       ]

       A__   		=     [
           14_89, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53
       ]

       A__   		=     [
           11_25, 17_51, 6_97, 17_76, 11_41, 14_76, 3_91, 6_97, 11_25, 6_84, 8_67, 4_16,
           8_44, 13_72, 12_74, 7_17, 12_74, 8_44, 12_99, 14_19, 6_97, 13_70, 3_17, 11_25,
           1_91, 14_40, 13_70, 14_40, 13_70, 2_82, 16_21, 13_70, 3_68, 3_49, 8_67, 18_72,
           12_62, 8_69, 17_28, 7_47
       ]
       A__   		=     [
           4_16, 4_16, 11_25, 11_25, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16
       ]

       A__   		=     [
           17_55, 8_42, 3_07, 18_43, 10_22, 13_95, 2_34, 15_54, 8_06, 7_39, 10_22, 4_42,
           6_16, 5_56, 2_68, 14_99, 9_33, 4_57, 14_40, 18_37, 7_55, 9_85, 3_08, 9_02,
           2_93, 14_43, 16_71, 11_41, 15_33, 5_55, 15_62, 10_61, 2_87, 4_17, 10_22, 20_08,
           11_86, 10_15, 17_77, 2_68
       ]
       A__   		=     [
           8_54, 8_42, 13_53, 1_14, 13_53, 8_42, 1_85, 8_42, 1_85, 1_14, 5_91, 8_42,
           1_85, 4_17, 1_85, 8_42, 3_07, 8_42, 5_91, 8_42, 1_85, 8_42, 3_07, 8_42,
           5_91, 8_42, 13_53, 8_42, 1_85, 8_42, 5_91, 8_42, 5_91, 1_14, 5_91, 8_42,
           1_85, 8_42, 5_91, 89
       ]

       A__   		=     [1_05_83_04, 0, 78_64_32, 71_69, 5_07, 76, 27, 40, 30, 76]

       A__   		=     [
           3_90, 11_60, 10_02, 19_07, 17_88, 17_88, 17_88, 19_07, 10_02, 10_02, 18_54, 10_02,
           10_02, 10_02, 10_02, 10_02, 10_02, 11_60, 11_60, 16_06, 5_96, 5_96, 11_60, 10_02,
           15_16, 5_96, 10_02, 10_02, 10_02, 19_07, 17_88, 17_88, 17_88, 18_54, 17_88, 19_07,
           19_07, 17_88, 5_96, 16_26
       ]
       A__   		=     [
           12_36, 16_68, 14_84, 19_20, 18_48, 14_09, 1_39, 8_64, 18_28, 12_72, 15_99, 8_24,
           16_72, 1_39, 5_55, 14_84, 8_24, 19_20, 5_55, 5_96, 15_79, 15_99, 12_31, 15_99,
           16_37, 14_07, 2_12, 8_24, 15_99, 1_16, 14_33, 8_24, 2_58, 15_99, 14_33, 18_95,
           10_63, 14_33, 14_33, 15_99
       ]
       A__   		=     [
           16_84, 18_73, 11_19, 11_89, 3_95, 6_11, 19_01, 9_72, 8_90, 13_37, 13_92, 19_27,
           96, 9_72, 6_72, 7_80, 11_19, 8_90, 1_58, 7_71, 10_73, 19_27, 3_53, 13_31,
           12_69, 14_59, 13_33, 16_45, 8_12, 15_77, 13_37, 6_06, 3_53, 9_81, 14_66, 6_19,
           1_97, 3_91, 3_02, 19_30
       ]
       A__   		=     [
           3_90, 11_60, 10_02, 19_07, 17_88, 17_88, 17_88, 19_07, 10_02, 10_02, 18_54, 10_02,
           10_02, 10_02, 10_02, 10_02, 10_02, 11_60, 11_60, 16_06, 5_96, 5_96, 11_60, 10_02,
           15_16, 5_96, 10_02, 10_02, 10_02, 19_07, 17_88, 17_88, 17_88, 18_54, 17_88, 19_07,
           19_07, 17_88, 5_96, 16_26
       ]
       A__   		=     [
           -0.0_492, -0.0_524, -0.0_565, -0.0_640, -0.0_686, -0.0_684, -0.0_677, -0.0_664,
           -0.0_605, -0.0_490, -0.0_330, -0.0_168, -0.0_083, -0.0_075, -0.0_051, 0.0_025,
           0.0_136, 0.0_261, 0.0_386, 0.0_497, 0.0_580, 0.0_599, 0.0_583, 0.0_614,
           0.0_740, 0.0_889, 0.1_023, 0.1_162, 0.1_211, 0.1_212, 0.1_251, 0.1_336,
           0.1_502, 0.1_686, 0.1_883, 0.2_148, 0.2_363, 0.2_458, 0.2_507, 0.2_531
       ]
       A__   		=     [
           0.0_256, -0.0_544, 0.1_600, -0.0_032, 0.1_066, 0.0_825, -0.0_013, 0.3_440,
           0.0_210, 0.0_412, -0.1_777, -0.0_892, -0.0_164, 0.0_285, -0.0_613, -0.0_617,
           -0.0_137, -0.0_201, -0.0_175, 0.0_215, -0.0_627, 0.0_520, -0.0_730, 0.0_970,
           -0.0_100, 0.0_442, -0.0_586, 0.0_207, -0.0_015, -0.0_082
       ]
       A__   		=     [
           0.0_415, 0.0_877, 0.0_022, -0.0_055, 0.0_751, 0.0_334, 0.0_324, -0.0_068,
           0.0_011, 0.0_017, -0.0_676, 0.0_655, -0.0_143, 0.0_399, 0.0_303, 0.0_743,
           -0.0_168, -0.0_394, -0.1_113, 0.0_124, 0.0_442, 0.0_267, -0.0_003, -0.1_536,
           -0.0_116, -0.1_837, -0.0_180, -0.1_026, -0.0_777, -0.0_456
       ]
       A__   		=     [
           76, 27, 40, 30, 76, 46, 44, 47, 40, 37, 38, 31, 45, 45, 76, 38, 31, 33,
           45, 76, 41, 32, 76, 45, 46, 41, 40, 31, 78, 76
       ]





       def 		lowerCAmelCase	(  self				:     Optional[int]			):


             '''simple docstring'''



             lowerCamelCase__						:							Tuple       =    JukeboxTokenizer.from_pretrained(self.model_id			)
             lowerCamelCase__						:							Dict       =    tokenizer(**self.metas			)["input_ids"]
             return tokens





       @slow
       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    JukeboxModel.from_pretrained(self.model_id   , min_duration=0			).eval()
             lowerCamelCase__						:							Optional[Any]       =    self.prepare_inputs()

             set_seed(0			)
             lowerCamelCase__						:							List[str]       =    [torch.zeros(1   , 0   , dtype=torch.long			).cpu() for _ in range(3			)]
             lowerCamelCase__						:							Union[str, Any]       =    model._sample(__lowerCamelCase   , __lowerCamelCase   , [0]   , sample_length=40 * model.priors[0].raw_to_tokens   , save_results=__lowerCamelCase			)
             self.assertIn(zs[0][0].detach().cpu().tolist()   , [self.EXPECTED_OUTPUT_2, self.EXPECTED_OUTPUT_2_PT_2]			)

             set_seed(0			)
             lowerCamelCase__						:							Optional[Any]       =    model._sample(__lowerCamelCase   , __lowerCamelCase   , [1]   , sample_length=40 * model.priors[1].raw_to_tokens   , save_results=__lowerCamelCase			)
             self.assertIn(zs[1][0].detach().cpu().tolist()   , [self.EXPECTED_OUTPUT_1, self.EXPECTED_OUTPUT_1_PT_2]			)

             set_seed(0			)
             lowerCamelCase__						:							List[str]       =    model._sample(__lowerCamelCase   , __lowerCamelCase   , [2]   , sample_length=40 * model.priors[2].raw_to_tokens   , save_results=__lowerCamelCase			)
             self.assertIn(zs[2][0].detach().cpu().tolist()   , [self.EXPECTED_OUTPUT_0, self.EXPECTED_OUTPUT_0_PT_2]			)





       @slow
       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             lowerCamelCase__						:							str       =    False
             lowerCamelCase__						:							Any       =    JukeboxModel.from_pretrained(self.model_id   , min_duration=0			).eval()

             lowerCamelCase__						:							Optional[Any]       =    self.prepare_inputs()
             set_seed(0			)
             lowerCamelCase__						:							List[Any]       =    [torch.zeros(1   , 0   , dtype=torch.long			) for _ in range(3			)]

             lowerCamelCase__						:							Optional[int]       =    model.priors[0]
             lowerCamelCase__						:							List[str]       =    0
             lowerCamelCase__						:							List[str]       =    top_prior.get_music_tokens_conds(__lowerCamelCase   , start=__lowerCamelCase   , end=start + top_prior.n_ctx			)
             lowerCamelCase__						:							List[Any]       =    top_prior.get_metadata(labels[0].clone()   , __lowerCamelCase   , 1058304   , 0			)

             self.assertIsNone(__lowerCamelCase			)
             self.assertListEqual(metadata.numpy()[0][:10].tolist()   , self.EXPECTED_Y_COND			)

             lowerCamelCase__       ,  lowerCamelCase__       ,  lowerCamelCase__						:							str       =    top_prior.get_cond(__lowerCamelCase   , __lowerCamelCase			)
             torch.testing.assert_allclose(
                 audio_conditioning[0][0][:30].detach()   , torch.tensor(self.EXPECTED_AUDIO_COND			)   , atol=1E-4   , rtol=1E-4			)
             torch.testing.assert_allclose(
                 metadata_conditioning[0][0][:30].detach()   , torch.tensor(self.EXPECTED_META_COND			)   , atol=1E-4   , rtol=1E-4			)
             torch.testing.assert_allclose(
                 lyric_tokens[0, :30].detach()   , torch.tensor(self.EXPECTED_LYRIC_COND			)   , atol=1E-4   , rtol=1E-4			)





       @slow
       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    False

             lowerCamelCase__						:							List[Any]       =    JukeboxModel.from_pretrained(self.model_id   , min_duration=0			).eval()
             set_seed(0			)
             lowerCamelCase__						:							int       =    torch.rand((1, 5120, 1)			)
             lowerCamelCase__						:							int       =    list(self.prepare_inputs()			)

             lowerCamelCase__						:							List[str]       =    [model.vqvae.encode(__lowerCamelCase   , start_level=2   , bs_chunks=waveform.shape[0]			)[0], None, None]
             lowerCamelCase__						:							str       =    model._sample(
                 __lowerCamelCase   , __lowerCamelCase   , sample_levels=[0]   , save_results=__lowerCamelCase   , sample_length=40 * model.priors[0].raw_to_tokens			)
             torch.testing.assert_allclose(zs[0][0][:40]   , torch.tensor(self.EXPECTED_PRIMED_0			)			)

             lowerCamelCase__						:							Tuple       =    torch.cat((zs[0], torch.zeros(1   , 2048 - zs[0].shape[-1]			))   , dim=-1			).long()
             lowerCamelCase__						:							Dict       =    [upper_a, model.vqvae.encode(__lowerCamelCase   , start_level=1   , bs_chunks=waveform.shape[0]			)[0], None]
             lowerCamelCase__						:							str       =    model._sample(
                 __lowerCamelCase   , __lowerCamelCase   , sample_levels=[1]   , save_results=__lowerCamelCase   , sample_length=40 * model.priors[1].raw_to_tokens			)
             torch.testing.assert_allclose(zs[1][0][:40]   , torch.tensor(self.EXPECTED_PRIMED_1			)			)

             lowerCamelCase__						:							List[Any]       =    torch.cat((zs[1], torch.zeros(1   , 2048 - zs[1].shape[-1]			))   , dim=-1			).long()
             lowerCamelCase__						:							Union[str, Any]       =    [upper_a, upper_a, model.vqvae.encode(__lowerCamelCase   , start_level=0   , bs_chunks=waveform.shape[0]			)[0]]
             lowerCamelCase__						:							str       =    model._sample(
                 __lowerCamelCase   , __lowerCamelCase   , sample_levels=[2]   , save_results=__lowerCamelCase   , sample_length=40 * model.priors[2].raw_to_tokens			)
             torch.testing.assert_allclose(zs[2][0][:40].cpu()   , torch.tensor(self.EXPECTED_PRIMED_2			)			)





       @slow
       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							int       =    JukeboxModel.from_pretrained(self.model_id   , min_duration=0			).eval()
             set_seed(0			)
             lowerCamelCase__						:							Dict       =    torch.rand((1, 5120, 1)			)
             with torch.no_grad():
                   lowerCamelCase__						:							Optional[int]       =    model.vqvae.encode(__lowerCamelCase   , start_level=2   , bs_chunks=x.shape[0]			)
             torch.testing.assert_allclose(zs[0][0]   , torch.tensor(self.EXPECTED_VQVAE_ENCODE			)			)

             with torch.no_grad():
                   lowerCamelCase__						:							Tuple       =    model.vqvae.decode(__lowerCamelCase   , start_level=2   , bs_chunks=x.shape[0]			)
             torch.testing.assert_allclose(x[0, :40, 0]   , torch.tensor(self.EXPECTED_VQVAE_DECODE			)   , atol=1E-4   , rtol=1E-4			)







@require_torch
class      _lowercase					(				unittest.TestCase):


       """simple docstring"""




       A__   		=     (JukeboxModel,) if is_torch_available() else ()
       A__   		=     "openai/jukebox-5b-lyrics"
       A__   		=     {
           "artist": "Zac Brown Band",
           "genres": "Country",
           "lyrics": "I met a traveller from an antique land,\n    Who said \"Two vast and trunkless legs of stone\n    Stand in the desert. . . . Near them, on the sand,\n    Half sunk a shattered visage lies, whose frown,\n    And wrinkled lip, and sneer of cold command,\n    Tell that its sculptor well those passions read\n    Which yet survive, stamped on these lifeless things,\n    The hand that mocked them, and the heart that fed;\n    And on the pedestal, these words appear:\n    My name is Ozymandias, King of Kings;\n    Look on my Works, ye Mighty, and despair!\n    Nothing beside remains. Round the decay\n    Of that colossal Wreck, boundless and bare\n    The lone and level sands stretch far away\n    ",
       }

       # fmt: off
       A__   		=     [
           14_89, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           14_89, 14_89, 14_89, 14_89, 11_50, 18_53, 15_09, 11_50, 13_57, 15_09, 6, 12_72
       ]
       A__   		=     [
           14_89, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53
       ]

       A__   		=     [
           11_25, 4_16, 11_25, 11_25, 11_25, 11_25, 11_25, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16
       ]
       A__   		=     [
           4_16, 4_16, 11_25, 11_25, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16
       ]

       A__   		=     [
           17_55, 10_61, 2_34, 17_55, 10_61, 17_55, 1_85, 2_90, 3_07, 3_07, 6_16, 6_16,
           6_16, 6_16, 6_16, 6_16, 3_07, 2_90, 4_17, 17_55, 2_34, 17_55, 1_85, 2_90,
           2_90, 2_90, 3_07, 6_16, 6_16, 6_16, 6_16, 6_16, 2_90, 2_34, 2_34, 17_55,
           2_34, 2_34, 17_55, 2_34, 1_85, 1_85, 3_07, 6_16, 6_16, 6_16, 6_16, 2_90,
           17_55, 17_55, 17_55, 2_34, 2_34, 17_55, 15_72, 2_90, 3_07, 6_16, 34, 6_16
       ]
       A__   		=     [
           8_54, 8_42, 13_53, 1_14, 13_53, 8_42, 1_85, 8_42, 1_85, 1_14, 5_91, 8_42, 1_85,
           4_17, 1_85, 8_42, 3_07, 8_42, 5_91, 8_42, 1_85, 8_42, 1_85, 8_42, 5_91, 8_42,
           13_53, 8_42, 1_85, 8_42, 5_91, 8_42, 5_91, 1_14, 5_91, 8_42, 1_85, 8_42, 5_91,
           89, 5_91, 8_42, 5_91, 8_42, 5_91, 4_17, 13_72, 8_42, 13_72, 8_42, 34, 8_42,
           1_85, 89, 5_91, 8_42, 1_85, 8_42, 5_91, 6_32
       ]

       A__   		=     [
           14_89, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53
       ]
       A__   		=     [
           14_89, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53,
           6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 6_53, 18_53, 11_77, 15_36, 12_28,
           7_10, 4_75, 14_89, 12_29, 12_24, 2_31, 12_24, 2_52, 14_34, 6_53, 4_75,
           11_06, 18_77, 15_99, 12_28, 16_00, 16_83, 11_82, 18_53, 4_75, 18_64,
           2_52, 12_29, 14_34, 20_01
       ]

       A__   		=     [
           11_25, 11_25, 4_16, 11_25, 11_25, 4_16, 11_25, 11_25, 4_16, 4_16, 11_25, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16,
           4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16, 4_16
       ]
       A__   		=     [
           4_91, 17_55, 34, 16_13, 17_55, 4_17, 9_92, 16_13, 2_22, 8_42, 13_53, 16_13,
           8_44, 6_32, 1_85, 16_13, 8_44, 6_32, 1_85, 16_13, 1_85, 8_42, 6_77, 16_13,
           1_85, 1_14, 13_53, 16_13, 3_07, 89, 8_44, 16_13, 3_07, 13_32, 2_34, 19_79,
           3_07, 89, 13_53, 6_16, 34, 8_42, 1_85, 8_42, 34, 8_42, 1_85, 8_42,
           3_07, 1_14, 1_85, 89, 34, 12_68, 1_85, 89, 34, 8_42, 1_85, 89
       ]





       def 		lowerCAmelCase	(  self				:     Tuple   , __lowerCamelCase				:     List[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							str       =    JukeboxTokenizer.from_pretrained(__lowerCamelCase			)
             lowerCamelCase__						:							List[Any]       =    tokenizer(**self.metas			)["input_ids"]
             return tokens





       @slow
       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							List[str]       =    JukeboxModel.from_pretrained(self.model_id   , min_duration=0			).eval()
             lowerCamelCase__						:							Dict       =    self.prepare_inputs(self.model_id			)

             set_seed(0			)
             lowerCamelCase__						:							int       =    [torch.zeros(1   , 0   , dtype=torch.long			).cpu() for _ in range(3			)]
             lowerCamelCase__						:							Union[str, Any]       =    model._sample(__lowerCamelCase   , __lowerCamelCase   , [0]   , sample_length=60 * model.priors[0].raw_to_tokens   , save_results=__lowerCamelCase			)
             self.assertIn(zs[0][0].detach().cpu().tolist()   , [self.EXPECTED_OUTPUT_2, self.EXPECTED_OUTPUT_2_PT_2]			)

             set_seed(0			)
             lowerCamelCase__						:							List[Any]       =    model._sample(__lowerCamelCase   , __lowerCamelCase   , [1]   , sample_length=60 * model.priors[1].raw_to_tokens   , save_results=__lowerCamelCase			)
             self.assertIn(zs[1][0].detach().cpu().tolist()   , [self.EXPECTED_OUTPUT_1, self.EXPECTED_OUTPUT_1_PT_2]			)

             set_seed(0			)
             lowerCamelCase__						:							Tuple       =    model._sample(__lowerCamelCase   , __lowerCamelCase   , [2]   , sample_length=60 * model.priors[2].raw_to_tokens   , save_results=__lowerCamelCase			)
             self.assertIn(zs[2][0].detach().cpu().tolist()   , [self.EXPECTED_OUTPUT_0, self.EXPECTED_OUTPUT_0_PT_2]			)





       @slow
       @skip("Not enough GPU memory on CI runners"			)
       def 		lowerCAmelCase	(  self				:     Union[str, Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    JukeboxModel.from_pretrained(self.model_id   , min_duration=0			).eval()
             lowerCamelCase__						:							Optional[int]       =    [i.cuda() for i in self.prepare_inputs(self.model_id			)]

             set_seed(0			)
             model.priors[0].cuda()
             lowerCamelCase__						:							Tuple       =    [torch.zeros(1   , 0   , dtype=torch.long			).cuda() for _ in range(3			)]
             lowerCamelCase__						:							List[Any]       =    model._sample(__lowerCamelCase   , __lowerCamelCase   , [0]   , sample_length=60 * model.priors[0].raw_to_tokens   , save_results=__lowerCamelCase			)
             torch.testing.assert_allclose(zs[0][0].cpu()   , torch.tensor(self.EXPECTED_GPU_OUTPUTS_2			)			)
             model.priors[0].cpu()

             set_seed(0			)
             model.priors[1].cuda()
             lowerCamelCase__						:							Dict       =    model._sample(__lowerCamelCase   , __lowerCamelCase   , [1]   , sample_length=60 * model.priors[1].raw_to_tokens   , save_results=__lowerCamelCase			)
             torch.testing.assert_allclose(zs[1][0].cpu()   , torch.tensor(self.EXPECTED_GPU_OUTPUTS_1			)			)
             model.priors[1].cpu()

             set_seed(0			)
             model.priors[2].cuda()
             lowerCamelCase__						:							Tuple       =    model._sample(__lowerCamelCase   , __lowerCamelCase   , [2]   , sample_length=60 * model.priors[2].raw_to_tokens   , save_results=__lowerCamelCase			)
             torch.testing.assert_allclose(zs[2][0].cpu()   , torch.tensor(self.EXPECTED_GPU_OUTPUTS_0			)			)





       @slow
       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             lowerCamelCase__						:							Tuple       =    "ArthurZ/jukebox_prior_0"
             lowerCamelCase__						:							Optional[Any]       =    JukeboxPrior.from_pretrained(__lowerCamelCase   , min_duration=0			).eval().half().to("cuda"			)

             lowerCamelCase__						:							Dict       =    self.prepare_inputs(__lowerCamelCase			)[0].cuda()
             lowerCamelCase__						:							Union[str, Any]       =    model.get_metadata(__lowerCamelCase   , 0   , 7680   , 0			)
             set_seed(0			)
             lowerCamelCase__						:							Optional[int]       =    model.sample(1   , metadata=__lowerCamelCase   , sample_tokens=60			)
             self.assertIn(outputs[0].cpu().tolist()   , [self.EXPECTED_GPU_OUTPUTS_2, self.EXPECTED_GPU_OUTPUTS_2_PT_2]			)


