
'''simple docstring'''





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 			UpperCamelCase__	(     unittest.TestCase):
 UpperCAmelCase__     :     Optional[int]    					=       (JukeboxModel,) if is_torch_available() else ()
 UpperCAmelCase__     :     str    					=       'openai/jukebox-1b-lyrics'
 UpperCAmelCase__     :     Any    					=       {
     '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
 UpperCAmelCase__     :     List[str]    					=       [
     1_8_6_4, 1_5_3_6, 1_2_1_3, 1_8_7_0, 1_3_5_7, 1_5_3_6, 5_1_9, 8_8_0, 1_3_2_3, 7_8_9, 1_0_8_2, 5_3_4,
     1_0_0_0, 1_4_4_5, 1_1_0_5, 1_1_3_0, 9_6_7, 5_1_5, 1_4_3_4, 1_6_2_0, 5_3_4, 1_4_9_5, 2_8_3, 1_4_4_5,
     3_3_3, 1_3_0_7, 5_3_9, 1_6_3_1, 1_5_2_8, 3_7_5, 1_4_3_4, 6_7_3, 6_2_7, 7_1_0, 7_7_8, 1_8_8_3,
     1_4_0_5, 1_2_7_6, 1_4_5_5, 1_2_2_8
 ]

 UpperCAmelCase__     :     int    					=       [
     1_4_8_9, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3
 ]

 UpperCAmelCase__     :     Dict    					=       [
     1_1_2_5, 1_7_5_1, 6_9_7, 1_7_7_6, 1_1_4_1, 1_4_7_6, 3_9_1, 6_9_7, 1_1_2_5, 6_8_4, 8_6_7, 4_1_6,
     8_4_4, 1_3_7_2, 1_2_7_4, 7_1_7, 1_2_7_4, 8_4_4, 1_2_9_9, 1_4_1_9, 6_9_7, 1_3_7_0, 3_1_7, 1_1_2_5,
     1_9_1, 1_4_4_0, 1_3_7_0, 1_4_4_0, 1_3_7_0, 2_8_2, 1_6_2_1, 1_3_7_0, 3_6_8, 3_4_9, 8_6_7, 1_8_7_2,
     1_2_6_2, 8_6_9, 1_7_2_8, 7_4_7
 ]
 UpperCAmelCase__     :     List[Any]    					=       [
     4_1_6, 4_1_6, 1_1_2_5, 1_1_2_5, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6
 ]

 UpperCAmelCase__     :     str    					=       [
     1_7_5_5, 8_4_2, 3_0_7, 1_8_4_3, 1_0_2_2, 1_3_9_5, 2_3_4, 1_5_5_4, 8_0_6, 7_3_9, 1_0_2_2, 4_4_2,
     6_1_6, 5_5_6, 2_6_8, 1_4_9_9, 9_3_3, 4_5_7, 1_4_4_0, 1_8_3_7, 7_5_5, 9_8_5, 3_0_8, 9_0_2,
     2_9_3, 1_4_4_3, 1_6_7_1, 1_1_4_1, 1_5_3_3, 5_5_5, 1_5_6_2, 1_0_6_1, 2_8_7, 4_1_7, 1_0_2_2, 2_0_0_8,
     1_1_8_6, 1_0_1_5, 1_7_7_7, 2_6_8
 ]
 UpperCAmelCase__     :     Optional[Any]    					=       [
     8_5_4, 8_4_2, 1_3_5_3, 1_1_4, 1_3_5_3, 8_4_2, 1_8_5, 8_4_2, 1_8_5, 1_1_4, 5_9_1, 8_4_2,
     1_8_5, 4_1_7, 1_8_5, 8_4_2, 3_0_7, 8_4_2, 5_9_1, 8_4_2, 1_8_5, 8_4_2, 3_0_7, 8_4_2,
     5_9_1, 8_4_2, 1_3_5_3, 8_4_2, 1_8_5, 8_4_2, 5_9_1, 8_4_2, 5_9_1, 1_1_4, 5_9_1, 8_4_2,
     1_8_5, 8_4_2, 5_9_1, 8_9
 ]

 UpperCAmelCase__     :     Any    					=       [1_0_5_8_3_0_4, 0, 7_8_6_4_3_2, 7_1_6_9, 5_0_7, 7_6, 2_7, 4_0, 3_0, 7_6]

 UpperCAmelCase__     :     int    					=       [
     3_9_0, 1_1_6_0, 1_0_0_2, 1_9_0_7, 1_7_8_8, 1_7_8_8, 1_7_8_8, 1_9_0_7, 1_0_0_2, 1_0_0_2, 1_8_5_4, 1_0_0_2,
     1_0_0_2, 1_0_0_2, 1_0_0_2, 1_0_0_2, 1_0_0_2, 1_1_6_0, 1_1_6_0, 1_6_0_6, 5_9_6, 5_9_6, 1_1_6_0, 1_0_0_2,
     1_5_1_6, 5_9_6, 1_0_0_2, 1_0_0_2, 1_0_0_2, 1_9_0_7, 1_7_8_8, 1_7_8_8, 1_7_8_8, 1_8_5_4, 1_7_8_8, 1_9_0_7,
     1_9_0_7, 1_7_8_8, 5_9_6, 1_6_2_6
 ]
 UpperCAmelCase__     :     Optional[int]    					=       [
     1_2_3_6, 1_6_6_8, 1_4_8_4, 1_9_2_0, 1_8_4_8, 1_4_0_9, 1_3_9, 8_6_4, 1_8_2_8, 1_2_7_2, 1_5_9_9, 8_2_4,
     1_6_7_2, 1_3_9, 5_5_5, 1_4_8_4, 8_2_4, 1_9_2_0, 5_5_5, 5_9_6, 1_5_7_9, 1_5_9_9, 1_2_3_1, 1_5_9_9,
     1_6_3_7, 1_4_0_7, 2_1_2, 8_2_4, 1_5_9_9, 1_1_6, 1_4_3_3, 8_2_4, 2_5_8, 1_5_9_9, 1_4_3_3, 1_8_9_5,
     1_0_6_3, 1_4_3_3, 1_4_3_3, 1_5_9_9
 ]
 UpperCAmelCase__     :     int    					=       [
     1_6_8_4, 1_8_7_3, 1_1_1_9, 1_1_8_9, 3_9_5, 6_1_1, 1_9_0_1, 9_7_2, 8_9_0, 1_3_3_7, 1_3_9_2, 1_9_2_7,
     9_6, 9_7_2, 6_7_2, 7_8_0, 1_1_1_9, 8_9_0, 1_5_8, 7_7_1, 1_0_7_3, 1_9_2_7, 3_5_3, 1_3_3_1,
     1_2_6_9, 1_4_5_9, 1_3_3_3, 1_6_4_5, 8_1_2, 1_5_7_7, 1_3_3_7, 6_0_6, 3_5_3, 9_8_1, 1_4_6_6, 6_1_9,
     1_9_7, 3_9_1, 3_0_2, 1_9_3_0
 ]
 UpperCAmelCase__     :     Optional[int]    					=       [
     3_9_0, 1_1_6_0, 1_0_0_2, 1_9_0_7, 1_7_8_8, 1_7_8_8, 1_7_8_8, 1_9_0_7, 1_0_0_2, 1_0_0_2, 1_8_5_4, 1_0_0_2,
     1_0_0_2, 1_0_0_2, 1_0_0_2, 1_0_0_2, 1_0_0_2, 1_1_6_0, 1_1_6_0, 1_6_0_6, 5_9_6, 5_9_6, 1_1_6_0, 1_0_0_2,
     1_5_1_6, 5_9_6, 1_0_0_2, 1_0_0_2, 1_0_0_2, 1_9_0_7, 1_7_8_8, 1_7_8_8, 1_7_8_8, 1_8_5_4, 1_7_8_8, 1_9_0_7,
     1_9_0_7, 1_7_8_8, 5_9_6, 1_6_2_6
 ]
 UpperCAmelCase__     :     Any    					=       [
     -0.0_4_9_2, -0.0_5_2_4, -0.0_5_6_5, -0.0_6_4_0, -0.0_6_8_6, -0.0_6_8_4, -0.0_6_7_7, -0.0_6_6_4,
     -0.0_6_0_5, -0.0_4_9_0, -0.0_3_3_0, -0.0_1_6_8, -0.0_0_8_3, -0.0_0_7_5, -0.0_0_5_1, 0.0_0_2_5,
     0.0_1_3_6, 0.0_2_6_1, 0.0_3_8_6, 0.0_4_9_7, 0.0_5_8_0, 0.0_5_9_9, 0.0_5_8_3, 0.0_6_1_4,
     0.0_7_4_0, 0.0_8_8_9, 0.1_0_2_3, 0.1_1_6_2, 0.1_2_1_1, 0.1_2_1_2, 0.1_2_5_1, 0.1_3_3_6,
     0.1_5_0_2, 0.1_6_8_6, 0.1_8_8_3, 0.2_1_4_8, 0.2_3_6_3, 0.2_4_5_8, 0.2_5_0_7, 0.2_5_3_1
 ]
 UpperCAmelCase__     :     Dict    					=       [
     0.0_2_5_6, -0.0_5_4_4, 0.1_6_0_0, -0.0_0_3_2, 0.1_0_6_6, 0.0_8_2_5, -0.0_0_1_3, 0.3_4_4_0,
     0.0_2_1_0, 0.0_4_1_2, -0.1_7_7_7, -0.0_8_9_2, -0.0_1_6_4, 0.0_2_8_5, -0.0_6_1_3, -0.0_6_1_7,
     -0.0_1_3_7, -0.0_2_0_1, -0.0_1_7_5, 0.0_2_1_5, -0.0_6_2_7, 0.0_5_2_0, -0.0_7_3_0, 0.0_9_7_0,
     -0.0_1_0_0, 0.0_4_4_2, -0.0_5_8_6, 0.0_2_0_7, -0.0_0_1_5, -0.0_0_8_2
 ]
 UpperCAmelCase__     :     List[str]    					=       [
     0.0_4_1_5, 0.0_8_7_7, 0.0_0_2_2, -0.0_0_5_5, 0.0_7_5_1, 0.0_3_3_4, 0.0_3_2_4, -0.0_0_6_8,
     0.0_0_1_1, 0.0_0_1_7, -0.0_6_7_6, 0.0_6_5_5, -0.0_1_4_3, 0.0_3_9_9, 0.0_3_0_3, 0.0_7_4_3,
     -0.0_1_6_8, -0.0_3_9_4, -0.1_1_1_3, 0.0_1_2_4, 0.0_4_4_2, 0.0_2_6_7, -0.0_0_0_3, -0.1_5_3_6,
     -0.0_1_1_6, -0.1_8_3_7, -0.0_1_8_0, -0.1_0_2_6, -0.0_7_7_7, -0.0_4_5_6
 ]
 UpperCAmelCase__     :     Optional[Any]    					=       [
     7_6, 2_7, 4_0, 3_0, 7_6, 4_6, 4_4, 4_7, 4_0, 3_7, 3_8, 3_1, 4_5, 4_5, 7_6, 3_8, 3_1, 3_3,
     4_5, 7_6, 4_1, 3_2, 7_6, 4_5, 4_6, 4_1, 4_0, 3_1, 7_8, 7_6
 ]






 def 						lowercase_    (      self	:Tuple      )					->      List[Any]:







       '''simple docstring'''



       __A													=      JukeboxTokenizer.from_pretrained(self.model_id      )
       __A													=      tokenizer(**self.metas      )['input_ids']
       return tokens






 @slow
 def 						lowercase_    (      self	:Tuple      )					->      int:







       '''simple docstring'''



       __A													=      JukeboxModel.from_pretrained(self.model_id						,					min_duration=0      ).eval()
       __A													=      self.prepare_inputs()

       set_seed(0      )
       __A													=      [torch.zeros(1						,					0						,					dtype=torch.long      ).cpu() for _ in range(3      )]
       __A													=      model._sample(_A						,					_A						,					[0]						,					sample_length=40 * model.priors[0].raw_to_tokens						,					save_results=_A      )
       self.assertIn(zs[0][0].detach().cpu().tolist()						,					[self.EXPECTED_OUTPUT_2, self.EXPECTED_OUTPUT_2_PT_2]      )

       set_seed(0      )
       __A													=      model._sample(_A						,					_A						,					[1]						,					sample_length=40 * model.priors[1].raw_to_tokens						,					save_results=_A      )
       self.assertIn(zs[1][0].detach().cpu().tolist()						,					[self.EXPECTED_OUTPUT_1, self.EXPECTED_OUTPUT_1_PT_2]      )

       set_seed(0      )
       __A													=      model._sample(_A						,					_A						,					[2]						,					sample_length=40 * model.priors[2].raw_to_tokens						,					save_results=_A      )
       self.assertIn(zs[2][0].detach().cpu().tolist()						,					[self.EXPECTED_OUTPUT_0, self.EXPECTED_OUTPUT_0_PT_2]      )






 @slow
 def 						lowercase_    (      self	:Any      )					->      str:







       '''simple docstring'''



       __A													=      False
       __A													=      JukeboxModel.from_pretrained(self.model_id						,					min_duration=0      ).eval()

       __A													=      self.prepare_inputs()
       set_seed(0      )
       __A													=      [torch.zeros(1						,					0						,					dtype=torch.long      ) for _ in range(3      )]

       __A													=      model.priors[0]
       __A													=      0
       __A													=      top_prior.get_music_tokens_conds(_A						,					start=_A						,					end=start + top_prior.n_ctx      )
       __A													=      top_prior.get_metadata(labels[0].clone()						,					_A						,					1_058_304						,					0      )

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

       __A						,   __A						,   __A													=      top_prior.get_cond(_A						,					_A      )
       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 						lowercase_    (      self	:Dict      )					->      Union[str, Any]:







       '''simple docstring'''



       __A													=      False

       __A													=      JukeboxModel.from_pretrained(self.model_id						,					min_duration=0      ).eval()
       set_seed(0      )
       __A													=      torch.rand((1, 5_120, 1)      )
       __A													=      list(self.prepare_inputs()      )

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

       __A													=      torch.cat((zs[0], torch.zeros(1						,					2_048 - zs[0].shape[-1]      ))						,					dim=-1      ).long()
       __A													=      [upper_a, model.vqvae.encode(_A						,					start_level=1						,					bs_chunks=waveform.shape[0]      )[0], None]
       __A													=      model._sample(
           _A						,					_A						,					sample_levels=[1]						,					save_results=_A						,					sample_length=40 * model.priors[1].raw_to_tokens      )
       torch.testing.assert_allclose(zs[1][0][:40]						,					torch.tensor(self.EXPECTED_PRIMED_1      )      )

       __A													=      torch.cat((zs[1], torch.zeros(1						,					2_048 - zs[1].shape[-1]      ))						,					dim=-1      ).long()
       __A													=      [upper_a, upper_a, model.vqvae.encode(_A						,					start_level=0						,					bs_chunks=waveform.shape[0]      )[0]]
       __A													=      model._sample(
           _A						,					_A						,					sample_levels=[2]						,					save_results=_A						,					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 						lowercase_    (      self	:Union[str, Any]      )					->      str:







       '''simple docstring'''



       __A													=      JukeboxModel.from_pretrained(self.model_id						,					min_duration=0      ).eval()
       set_seed(0      )
       __A													=      torch.rand((1, 5_120, 1)      )
       with torch.no_grad():
             __A													=      model.vqvae.encode(_A						,					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():
             __A													=      model.vqvae.decode(_A						,					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 			UpperCamelCase__	(     unittest.TestCase):
 UpperCAmelCase__     :     int    					=       (JukeboxModel,) if is_torch_available() else ()
 UpperCAmelCase__     :     Optional[int]    					=       'openai/jukebox-5b-lyrics'
 UpperCAmelCase__     :     Any    					=       {
     '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
 UpperCAmelCase__     :     Union[str, Any]    					=       [
     1_4_8_9, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     1_4_8_9, 1_4_8_9, 1_4_8_9, 1_4_8_9, 1_1_5_0, 1_8_5_3, 1_5_0_9, 1_1_5_0, 1_3_5_7, 1_5_0_9, 6, 1_2_7_2
 ]
 UpperCAmelCase__     :     Dict    					=       [
     1_4_8_9, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3
 ]

 UpperCAmelCase__     :     Dict    					=       [
     1_1_2_5, 4_1_6, 1_1_2_5, 1_1_2_5, 1_1_2_5, 1_1_2_5, 1_1_2_5, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6
 ]
 UpperCAmelCase__     :     Optional[int]    					=       [
     4_1_6, 4_1_6, 1_1_2_5, 1_1_2_5, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6
 ]

 UpperCAmelCase__     :     List[str]    					=       [
     1_7_5_5, 1_0_6_1, 2_3_4, 1_7_5_5, 1_0_6_1, 1_7_5_5, 1_8_5, 2_9_0, 3_0_7, 3_0_7, 6_1_6, 6_1_6,
     6_1_6, 6_1_6, 6_1_6, 6_1_6, 3_0_7, 2_9_0, 4_1_7, 1_7_5_5, 2_3_4, 1_7_5_5, 1_8_5, 2_9_0,
     2_9_0, 2_9_0, 3_0_7, 6_1_6, 6_1_6, 6_1_6, 6_1_6, 6_1_6, 2_9_0, 2_3_4, 2_3_4, 1_7_5_5,
     2_3_4, 2_3_4, 1_7_5_5, 2_3_4, 1_8_5, 1_8_5, 3_0_7, 6_1_6, 6_1_6, 6_1_6, 6_1_6, 2_9_0,
     1_7_5_5, 1_7_5_5, 1_7_5_5, 2_3_4, 2_3_4, 1_7_5_5, 1_5_7_2, 2_9_0, 3_0_7, 6_1_6, 3_4, 6_1_6
 ]
 UpperCAmelCase__     :     Tuple    					=       [
     8_5_4, 8_4_2, 1_3_5_3, 1_1_4, 1_3_5_3, 8_4_2, 1_8_5, 8_4_2, 1_8_5, 1_1_4, 5_9_1, 8_4_2, 1_8_5,
     4_1_7, 1_8_5, 8_4_2, 3_0_7, 8_4_2, 5_9_1, 8_4_2, 1_8_5, 8_4_2, 1_8_5, 8_4_2, 5_9_1, 8_4_2,
     1_3_5_3, 8_4_2, 1_8_5, 8_4_2, 5_9_1, 8_4_2, 5_9_1, 1_1_4, 5_9_1, 8_4_2, 1_8_5, 8_4_2, 5_9_1,
     8_9, 5_9_1, 8_4_2, 5_9_1, 8_4_2, 5_9_1, 4_1_7, 1_3_7_2, 8_4_2, 1_3_7_2, 8_4_2, 3_4, 8_4_2,
     1_8_5, 8_9, 5_9_1, 8_4_2, 1_8_5, 8_4_2, 5_9_1, 6_3_2
 ]

 UpperCAmelCase__     :     List[Any]    					=       [
     1_4_8_9, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3
 ]
 UpperCAmelCase__     :     Optional[Any]    					=       [
     1_4_8_9, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3,
     6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 6_5_3, 1_8_5_3, 1_1_7_7, 1_5_3_6, 1_2_2_8,
     7_1_0, 4_7_5, 1_4_8_9, 1_2_2_9, 1_2_2_4, 2_3_1, 1_2_2_4, 2_5_2, 1_4_3_4, 6_5_3, 4_7_5,
     1_1_0_6, 1_8_7_7, 1_5_9_9, 1_2_2_8, 1_6_0_0, 1_6_8_3, 1_1_8_2, 1_8_5_3, 4_7_5, 1_8_6_4,
     2_5_2, 1_2_2_9, 1_4_3_4, 2_0_0_1
 ]

 UpperCAmelCase__     :     List[str]    					=       [
     1_1_2_5, 1_1_2_5, 4_1_6, 1_1_2_5, 1_1_2_5, 4_1_6, 1_1_2_5, 1_1_2_5, 4_1_6, 4_1_6, 1_1_2_5, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6,
     4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6, 4_1_6
 ]
 UpperCAmelCase__     :     List[str]    					=       [
     4_9_1, 1_7_5_5, 3_4, 1_6_1_3, 1_7_5_5, 4_1_7, 9_9_2, 1_6_1_3, 2_2_2, 8_4_2, 1_3_5_3, 1_6_1_3,
     8_4_4, 6_3_2, 1_8_5, 1_6_1_3, 8_4_4, 6_3_2, 1_8_5, 1_6_1_3, 1_8_5, 8_4_2, 6_7_7, 1_6_1_3,
     1_8_5, 1_1_4, 1_3_5_3, 1_6_1_3, 3_0_7, 8_9, 8_4_4, 1_6_1_3, 3_0_7, 1_3_3_2, 2_3_4, 1_9_7_9,
     3_0_7, 8_9, 1_3_5_3, 6_1_6, 3_4, 8_4_2, 1_8_5, 8_4_2, 3_4, 8_4_2, 1_8_5, 8_4_2,
     3_0_7, 1_1_4, 1_8_5, 8_9, 3_4, 1_2_6_8, 1_8_5, 8_9, 3_4, 8_4_2, 1_8_5, 8_9
 ]






 def 						lowercase_    (      self	:Tuple						,					_A	:Union[str, Any]      )					->      Union[str, Any]:







       '''simple docstring'''



       __A													=      JukeboxTokenizer.from_pretrained(_A      )
       __A													=      tokenizer(**self.metas      )['input_ids']
       return tokens






 @slow
 def 						lowercase_    (      self	:int      )					->      str:







       '''simple docstring'''



       __A													=      JukeboxModel.from_pretrained(self.model_id						,					min_duration=0      ).eval()
       __A													=      self.prepare_inputs(self.model_id      )

       set_seed(0      )
       __A													=      [torch.zeros(1						,					0						,					dtype=torch.long      ).cpu() for _ in range(3      )]
       __A													=      model._sample(_A						,					_A						,					[0]						,					sample_length=60 * model.priors[0].raw_to_tokens						,					save_results=_A      )
       self.assertIn(zs[0][0].detach().cpu().tolist()						,					[self.EXPECTED_OUTPUT_2, self.EXPECTED_OUTPUT_2_PT_2]      )

       set_seed(0      )
       __A													=      model._sample(_A						,					_A						,					[1]						,					sample_length=60 * model.priors[1].raw_to_tokens						,					save_results=_A      )
       self.assertIn(zs[1][0].detach().cpu().tolist()						,					[self.EXPECTED_OUTPUT_1, self.EXPECTED_OUTPUT_1_PT_2]      )

       set_seed(0      )
       __A													=      model._sample(_A						,					_A						,					[2]						,					sample_length=60 * model.priors[2].raw_to_tokens						,					save_results=_A      )
       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 						lowercase_    (      self	:Tuple      )					->      Tuple:







       '''simple docstring'''



       __A													=      JukeboxModel.from_pretrained(self.model_id						,					min_duration=0      ).eval()
       __A													=      [i.cuda() for i in self.prepare_inputs(self.model_id      )]

       set_seed(0      )
       model.priors[0].cuda()
       __A													=      [torch.zeros(1						,					0						,					dtype=torch.long      ).cuda() for _ in range(3      )]
       __A													=      model._sample(_A						,					_A						,					[0]						,					sample_length=60 * model.priors[0].raw_to_tokens						,					save_results=_A      )
       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()
       __A													=      model._sample(_A						,					_A						,					[1]						,					sample_length=60 * model.priors[1].raw_to_tokens						,					save_results=_A      )
       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()
       __A													=      model._sample(_A						,					_A						,					[2]						,					sample_length=60 * model.priors[2].raw_to_tokens						,					save_results=_A      )
       torch.testing.assert_allclose(zs[2][0].cpu()						,					torch.tensor(self.EXPECTED_GPU_OUTPUTS_0      )      )






 @slow
 def 						lowercase_    (      self	:Dict      )					->      Union[str, Any]:







       '''simple docstring'''



       __A													=      'ArthurZ/jukebox_prior_0'
       __A													=      JukeboxPrior.from_pretrained(_A						,					min_duration=0      ).eval().half().to('cuda'      )

       __A													=      self.prepare_inputs(_A      )[0].cuda()
       __A													=      model.get_metadata(_A						,					0						,					7_680						,					0      )
       set_seed(0      )
       __A													=      model.sample(1						,					metadata=_A						,					sample_tokens=60      )
       self.assertIn(outputs[0].cpu().tolist()						,					[self.EXPECTED_GPU_OUTPUTS_2, self.EXPECTED_GPU_OUTPUTS_2_PT_2]      )













'''simple docstring'''

import collections
import inspect
import unittest

from transformers import NatConfig
from transformers.testing_utils import require_natten, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch
   from torch import nn

   from transformers import NatBackbone, NatForImageClassification, NatModel
   from transformers.models.nat.modeling_nat import NAT_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor





class lowerCamelCase    :







  def __init__(		self  ,  lowercase__  ,  lowercase__=13  ,  lowercase__=64  ,  lowercase__=4  ,  lowercase__=3  ,  lowercase__=16  ,  lowercase__=[1, 2, 1]  ,  lowercase__=[2, 4, 8]  ,  lowercase__=3  ,  lowercase__=2.0  ,  lowercase__=True  ,  lowercase__=0.0  ,  lowercase__=0.0  ,  lowercase__=0.1  ,  lowercase__="gelu"  ,  lowercase__=True  ,  lowercase__=0.02  ,  lowercase__=1E-5  ,  lowercase__=True  ,  lowercase__=None  ,  lowercase__=True  ,  lowercase__=10  ,  lowercase__=["stage1", "stage2"]  ,  lowercase__=[1, 2]  ,  )		->    List[str]:






        """simple docstring"""





        _snake_case		:						Optional[int]         =  parent
        _snake_case		:						List[str]         =  batch_size
        _snake_case		:						int         =  image_size
        _snake_case		:						Union[str, Any]         =  patch_size
        _snake_case		:						List[Any]         =  num_channels
        _snake_case		:						Tuple         =  embed_dim
        _snake_case		:						Tuple         =  depths
        _snake_case		:						Optional[Any]         =  num_heads
        _snake_case		:						Union[str, Any]         =  kernel_size
        _snake_case		:						Optional[Any]         =  mlp_ratio
        _snake_case		:						Optional[int]         =  qkv_bias
        _snake_case		:						List[Any]         =  hidden_dropout_prob
        _snake_case		:						Optional[int]         =  attention_probs_dropout_prob
        _snake_case		:						List[str]         =  drop_path_rate
        _snake_case		:						Optional[Any]         =  hidden_act
        _snake_case		:						Any         =  patch_norm
        _snake_case		:						Any         =  layer_norm_eps
        _snake_case		:						Union[str, Any]         =  initializer_range
        _snake_case		:						List[Any]         =  is_training
        _snake_case		:						List[Any]         =  scope
        _snake_case		:						Dict         =  use_labels
        _snake_case		:						Tuple         =  num_labels
        _snake_case		:						Union[str, Any]         =  out_features
        _snake_case		:						Dict         =  out_indices







  def 					UpperCAmelCase_					(		self		)		->    int:






        """simple docstring"""





        _snake_case		:						List[str]         =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

        _snake_case		:						Union[str, Any]         =  None
        if self.use_labels:
              _snake_case		:						int         =  ids_tensor([self.batch_size]  ,  self.num_labels		)

        _snake_case		:						Union[str, Any]         =  self.get_config()

        return config, pixel_values, labels







  def 					UpperCAmelCase_					(		self		)		->    Optional[int]:






        """simple docstring"""





        return NatConfig(
            num_labels=self.num_labels  ,  image_size=self.image_size  ,  patch_size=self.patch_size  ,  num_channels=self.num_channels  ,  embed_dim=self.embed_dim  ,  depths=self.depths  ,  num_heads=self.num_heads  ,  kernel_size=self.kernel_size  ,  mlp_ratio=self.mlp_ratio  ,  qkv_bias=self.qkv_bias  ,  hidden_dropout_prob=self.hidden_dropout_prob  ,  attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,  drop_path_rate=self.drop_path_rate  ,  hidden_act=self.hidden_act  ,  patch_norm=self.patch_norm  ,  layer_norm_eps=self.layer_norm_eps  ,  initializer_range=self.initializer_range  ,  out_features=self.out_features  ,  out_indices=self.out_indices  ,  )







  def 					UpperCAmelCase_					(		self  ,  lowercase__  ,  lowercase__  ,  lowercase__		)		->    str:






        """simple docstring"""





        _snake_case		:						int         =  NatModel(config=lowercase__		)
        model.to(lowercase__		)
        model.eval()
        _snake_case		:						Optional[Any]         =  model(lowercase__		)

        _snake_case		:						Optional[int]         =  (config.image_size // config.patch_size) // (2 ** (len(config.depths		) - 1))
        _snake_case		:						str         =  int(config.embed_dim * 2 ** (len(config.depths		) - 1)		)

        self.parent.assertEqual(
            result.last_hidden_state.shape  ,  (self.batch_size, expected_height, expected_width, expected_dim)		)







  def 					UpperCAmelCase_					(		self  ,  lowercase__  ,  lowercase__  ,  lowercase__		)		->    Tuple:






        """simple docstring"""





        _snake_case		:						int         =  NatForImageClassification(lowercase__		)
        model.to(lowercase__		)
        model.eval()
        _snake_case		:						List[str]         =  model(lowercase__  ,  labels=lowercase__		)
        self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.num_labels)		)

        # test greyscale images
        _snake_case		:						str         =  1
        _snake_case		:						Dict         =  NatForImageClassification(lowercase__		)
        model.to(lowercase__		)
        model.eval()

        _snake_case		:						int         =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
        _snake_case		:						Optional[Any]         =  model(lowercase__		)
        self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.num_labels)		)







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






        """simple docstring"""





        _snake_case		:						int         =  NatBackbone(config=lowercase__		)
        model.to(lowercase__		)
        model.eval()
        _snake_case		:						Any         =  model(lowercase__		)

        # verify hidden states
        self.parent.assertEqual(len(result.feature_maps		)  ,  len(config.out_features		)		)
        self.parent.assertListEqual(list(result.feature_maps[0].shape		)  ,  [self.batch_size, model.channels[0], 16, 16]		)

        # verify channels
        self.parent.assertEqual(len(model.channels		)  ,  len(config.out_features		)		)

        # verify backbone works with out_features=None
        _snake_case		:						Dict         =  None
        _snake_case		:						Any         =  NatBackbone(config=lowercase__		)
        model.to(lowercase__		)
        model.eval()
        _snake_case		:						str         =  model(lowercase__		)

        # verify feature maps
        self.parent.assertEqual(len(result.feature_maps		)  ,  1		)
        self.parent.assertListEqual(list(result.feature_maps[0].shape		)  ,  [self.batch_size, model.channels[-1], 4, 4]		)

        # verify channels
        self.parent.assertEqual(len(model.channels		)  ,  1		)







  def 					UpperCAmelCase_					(		self		)		->    Any:






        """simple docstring"""





        _snake_case		:						Union[str, Any]         =  self.prepare_config_and_inputs()
        _snake_case  ,				_snake_case  ,				_snake_case		:						Optional[Any]         =  config_and_inputs
        _snake_case		:						List[Any]         =  {'''pixel_values''': pixel_values}
        return config, inputs_dict





@require_natten
@require_torch
class lowerCamelCase    (a__    ,   a__    ,   unittest.TestCase       ):
  _lowercase	:						int							       =					(
      (
          NatModel,
          NatForImageClassification,
          NatBackbone,
      )
      if is_torch_available()
      else ()
  )
  _lowercase	:						Optional[Any]							       =					(
      {"""feature-extraction""": NatModel, """image-classification""": NatForImageClassification}
      if is_torch_available()
      else {}
  )
  _lowercase	:						List[str]							       =					False

  _lowercase	:						str							       =					False
  _lowercase	:						Dict							       =					False
  _lowercase	:						Tuple							       =					False
  _lowercase	:						List[str]							       =					False







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






        """simple docstring"""





        _snake_case		:						List[str]         =  NatModelTester(self		)
        _snake_case		:						List[str]         =  ConfigTester(self  ,  config_class=lowercase__  ,  embed_dim=37		)







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






        """simple docstring"""





        self.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()







  def 					UpperCAmelCase_					(		self		)		->    int:






        """simple docstring"""





        return







  def 					UpperCAmelCase_					(		self		)		->    Optional[int]:






        """simple docstring"""





        _snake_case		:						Tuple         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase__		)







  def 					UpperCAmelCase_					(		self		)		->    Dict:






        """simple docstring"""





        _snake_case		:						Optional[int]         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowercase__		)







  def 					UpperCAmelCase_					(		self		)		->    Any:






        """simple docstring"""





        _snake_case		:						List[Any]         =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_backbone(*lowercase__		)







  @unittest.skip(reason='''Nat does not use inputs_embeds'''		)
  def 					UpperCAmelCase_					(		self		)		->    Optional[int]:






        """simple docstring"""





        pass







  @unittest.skip(reason='''Nat does not use feedforward chunking'''		)
  def 					UpperCAmelCase_					(		self		)		->    List[Any]:






        """simple docstring"""





        pass







  def 					UpperCAmelCase_					(		self		)		->    str:






        """simple docstring"""





        _snake_case  ,				_snake_case		:						Tuple         =  self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              _snake_case		:						List[Any]         =  model_class(lowercase__		)
              self.assertIsInstance(model.get_input_embeddings()  ,  (nn.Module)		)
              _snake_case		:						str         =  model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(lowercase__  ,  nn.Linear		)		)







  def 					UpperCAmelCase_					(		self		)		->    int:






        """simple docstring"""





        _snake_case  ,				_snake_case		:						Dict         =  self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              _snake_case		:						Union[str, Any]         =  model_class(lowercase__		)
              _snake_case		:						Tuple         =  inspect.signature(model.forward		)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              _snake_case		:						List[Any]         =  [*signature.parameters.keys()]

              _snake_case		:						str         =  ['''pixel_values''']
              self.assertListEqual(arg_names[:1]  ,  lowercase__		)







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






        """simple docstring"""





        self.skipTest('''Nat\'s attention operation is handled entirely by NATTEN.'''		)







  def 					UpperCAmelCase_					(		self  ,  lowercase__  ,  lowercase__  ,  lowercase__  ,  lowercase__		)		->    Any:






        """simple docstring"""





        _snake_case		:						str         =  model_class(lowercase__		)
        model.to(lowercase__		)
        model.eval()

        with torch.no_grad():
              _snake_case		:						Optional[Any]         =  model(**self._prepare_for_class(lowercase__  ,  lowercase__		)		)

        _snake_case		:						Any         =  outputs.hidden_states

        _snake_case		:						Optional[Any]         =  getattr(
            self.model_tester  ,  '''expected_num_hidden_layers'''  ,  len(self.model_tester.depths		) + 1		)
        self.assertEqual(len(lowercase__		)  ,  lowercase__		)

        # Nat has a different seq_length
        _snake_case		:						Tuple         =  (
            config.patch_size
            if isinstance(config.patch_size  ,  collections.abc.Iterable		)
            else (config.patch_size, config.patch_size)
        )

        _snake_case		:						int         =  image_size[0] // patch_size[0]
        _snake_case		:						Optional[Any]         =  image_size[1] // patch_size[1]

        self.assertListEqual(
            list(hidden_states[0].shape[-3:]		)  ,  [height, width, self.model_tester.embed_dim]  ,  )

        if model_class.__name__ != "NatBackbone":
              _snake_case		:						Dict         =  outputs.reshaped_hidden_states
              self.assertEqual(len(lowercase__		)  ,  lowercase__		)

              _snake_case  ,				_snake_case  ,				_snake_case  ,				_snake_case		:						Any         =  reshaped_hidden_states[0].shape
              _snake_case		:						str         =  (
                  reshaped_hidden_states[0].view(lowercase__  ,  lowercase__  ,  lowercase__  ,  lowercase__		).permute(0  ,  2  ,  3  ,  1		)
              )
              self.assertListEqual(
                  list(reshaped_hidden_states.shape[-3:]		)  ,  [height, width, self.model_tester.embed_dim]  ,  )







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






        """simple docstring"""





        _snake_case  ,				_snake_case		:						str         =  self.model_tester.prepare_config_and_inputs_for_common()

        _snake_case		:						Dict         =  (
            self.model_tester.image_size
            if isinstance(self.model_tester.image_size  ,  collections.abc.Iterable		)
            else (self.model_tester.image_size, self.model_tester.image_size)
        )

        for model_class in self.all_model_classes:
              _snake_case		:						Optional[Any]         =  True
              self.check_hidden_states_output(lowercase__  ,  lowercase__  ,  lowercase__  ,  lowercase__		)

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              _snake_case		:						str         =  True

              self.check_hidden_states_output(lowercase__  ,  lowercase__  ,  lowercase__  ,  lowercase__		)







  @slow
  def 					UpperCAmelCase_					(		self		)		->    Dict:






        """simple docstring"""





        for model_name in NAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _snake_case		:						Optional[int]         =  NatModel.from_pretrained(lowercase__		)
              self.assertIsNotNone(lowercase__		)







  def 					UpperCAmelCase_					(		self		)		->    Dict:






        """simple docstring"""





        _snake_case  ,				_snake_case		:						Tuple         =  self.model_tester.prepare_config_and_inputs_for_common()

        _snake_case		:						str         =  _config_zero_init(lowercase__		)
        for model_class in self.all_model_classes:
              _snake_case		:						int         =  model_class(config=lowercase__		)
              for name, param in model.named_parameters():
                    if "embeddings" not in name and param.requires_grad:
                          self.assertIn(
                              ((param.data.mean() * 1E9).round() / 1E9).item()  ,  [0.0, 1.0]  ,  msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''  ,  )





@require_natten
@require_vision
@require_torch
class lowerCamelCase    (unittest.TestCase       ):







  @cached_property
  def 					UpperCAmelCase_					(		self		)		->    int:






        """simple docstring"""





        return AutoImageProcessor.from_pretrained('''shi-labs/nat-mini-in1k-224'''		) if is_vision_available() else None







  @slow
  def 					UpperCAmelCase_					(		self		)		->    Dict:






        """simple docstring"""





        _snake_case		:						Tuple         =  NatForImageClassification.from_pretrained('''shi-labs/nat-mini-in1k-224'''		).to(lowercase__		)
        _snake_case		:						Optional[int]         =  self.default_image_processor

        _snake_case		:						List[str]         =  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
        _snake_case		:						List[Any]         =  image_processor(images=lowercase__  ,  return_tensors='''pt'''		).to(lowercase__		)

        # forward pass
        with torch.no_grad():
              _snake_case		:						Tuple         =  model(**lowercase__		)

        # verify the logits
        _snake_case		:						List[str]         =  torch.Size((1, 1_000)		)
        self.assertEqual(outputs.logits.shape  ,  lowercase__		)
        _snake_case		:						List[str]         =  torch.tensor([0.3_805, -0.8_676, -0.3_912]		).to(lowercase__		)
        self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,  lowercase__  ,  atol=1E-4		)		)





@require_torch
@require_natten
class lowerCamelCase    (unittest.TestCase    ,   a__       ):
  _lowercase	:						str							       =					(NatBackbone,) if is_torch_available() else ()
  _lowercase	:						List[Any]							       =					NatConfig







  def 					UpperCAmelCase_					(		self		)		->    Dict:






        """simple docstring"""





        _snake_case		:						Any         =  NatModelTester(self		)


