





"""simple docstring"""


from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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


if is_tf_available():
   import tensorflow as tf

   from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
   from PIL import Image

   from transformers import ConvNextImageProcessor



class        lowercase_			:

       def __init__(     self				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE=13				,   __SCREAMING_SNAKE_CASE=32				,   __SCREAMING_SNAKE_CASE=3				,   __SCREAMING_SNAKE_CASE=4				,   __SCREAMING_SNAKE_CASE=[10, 20, 30, 40]				,   __SCREAMING_SNAKE_CASE=[2, 2, 3, 2]				,   __SCREAMING_SNAKE_CASE=True				,   __SCREAMING_SNAKE_CASE=True				,   __SCREAMING_SNAKE_CASE=37				,   __SCREAMING_SNAKE_CASE="gelu"				,   __SCREAMING_SNAKE_CASE=10				,   __SCREAMING_SNAKE_CASE=0.02				,   __SCREAMING_SNAKE_CASE=3				,   __SCREAMING_SNAKE_CASE=None				,   ):






        '''simple docstring'''

        snake_case__      							=			parent
        snake_case__      							=			batch_size
        snake_case__      							=			image_size
        snake_case__      							=			num_channels
        snake_case__      							=			num_stages
        snake_case__      							=			hidden_sizes
        snake_case__      							=			depths
        snake_case__      							=			is_training
        snake_case__      							=			use_labels
        snake_case__      							=			intermediate_size
        snake_case__      							=			hidden_act
        snake_case__      							=			type_sequence_label_size
        snake_case__      							=			initializer_range
        snake_case__      							=			scope

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

        snake_case__      							=			None
        if self.use_labels:
         snake_case__      							=			ids_tensor([self.batch_size]				,   self.type_sequence_label_size       )

        snake_case__      							=			self.get_config()

        return config, pixel_values, labels

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        return ConvNextConfig(
            num_channels=self.num_channels				,   hidden_sizes=self.hidden_sizes				,   depths=self.depths				,   num_stages=self.num_stages				,   hidden_act=self.hidden_act				,   is_decoder=__SCREAMING_SNAKE_CASE				,   initializer_range=self.initializer_range				,   )

       def 						_SCREAMING_SNAKE_CASE(     self				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       ):






        '''simple docstring'''

        snake_case__      							=			TFConvNextModel(config=__SCREAMING_SNAKE_CASE       )
        snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   training=__SCREAMING_SNAKE_CASE       )
        # expected last hidden states: B, C, H // 32, W // 32
        self.parent.assertEqual(
            result.last_hidden_state.shape				,   (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)				,   )

       def 						_SCREAMING_SNAKE_CASE(     self				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       ):






        '''simple docstring'''

        snake_case__      							=			self.type_sequence_label_size
        snake_case__      							=			TFConvNextForImageClassification(__SCREAMING_SNAKE_CASE       )
        snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   labels=__SCREAMING_SNAKE_CASE				,   training=__SCREAMING_SNAKE_CASE       )
        self.parent.assertEqual(result.logits.shape				,   (self.batch_size, self.type_sequence_label_size)       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			self.prepare_config_and_inputs()
        snake_case__							,				snake_case__							,				snake_case__      							=			config_and_inputs
        snake_case__      							=			{"pixel_values": pixel_values}
        return config, inputs_dict



@require_tf
class        lowercase_			(       __lowerCAmelCase			,__lowerCAmelCase			,unittest.TestCase							):
       snake_case__			:							Optional[Any]      							=       (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
       snake_case__			:							str      							=       (
           {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
           if is_tf_available()
           else {}
       )

       snake_case__			:							Optional[Any]      							=       False
       snake_case__			:							Any      							=       False
       snake_case__			:							Union[str, Any]      							=       False
       snake_case__			:							List[str]      							=       False
       snake_case__			:							Optional[Any]      							=       False

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			TFConvNextModelTester(self       )
        snake_case__      							=			ConfigTester(
            self				,   config_class=__SCREAMING_SNAKE_CASE				,   has_text_modality=__SCREAMING_SNAKE_CASE				,   hidden_size=37				,   )

       @unittest.skip(reason="ConvNext does not use inputs_embeds"       )
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        pass

       @unittest.skipIf(
           not is_tf_available() or len(tf.config.list_physical_devices("GPU"       )       ) == 0				,   reason="TF does not support backprop for grouped convolutions on CPU."				,   )
       @slow
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        super().test_keras_fit()

       @unittest.skip(reason="ConvNext does not support input and output embeddings"       )
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        pass

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__							,				snake_case__      							=			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
         snake_case__      							=			model_class(__SCREAMING_SNAKE_CASE       )
         snake_case__      							=			inspect.signature(model.call       )
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         snake_case__      							=			[*signature.parameters.keys()]

         snake_case__      							=			["pixel_values"]
         self.assertListEqual(arg_names[:1]				,   __SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE       )

       @unittest.skipIf(
           not is_tf_available() or len(tf.config.list_physical_devices("GPU"       )       ) == 0				,   reason="TF does not support backprop for grouped convolutions on CPU."				,   )
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        super().test_dataset_conversion()

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        def check_hidden_states_output(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       ):
         snake_case__      							=			model_class(__SCREAMING_SNAKE_CASE       )

         snake_case__      							=			model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )       )
         snake_case__      							=			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

         snake_case__      							=			self.model_tester.num_stages
         self.assertEqual(len(__SCREAMING_SNAKE_CASE       )				,   expected_num_stages + 1       )

         # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
         self.assertListEqual(
             list(hidden_states[0].shape[-2:]       )				,   [self.model_tester.image_size // 4, self.model_tester.image_size // 4]				,   )

        snake_case__							,				snake_case__      							=			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
         snake_case__      							=			True
         check_hidden_states_output(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         snake_case__      							=			True

         check_hidden_states_output(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__							,				snake_case__      							=			self.model_tester.prepare_config_and_inputs_for_common()

        def check_equivalence(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE={}       ):
         snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   return_dict=__SCREAMING_SNAKE_CASE				,   **__SCREAMING_SNAKE_CASE       )
         snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   return_dict=__SCREAMING_SNAKE_CASE				,   **__SCREAMING_SNAKE_CASE       ).to_tuple()

         def recursive_check(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       ):
          if isinstance(__SCREAMING_SNAKE_CASE				,   (List, Tuple)       ):
           for tuple_iterable_value, dict_iterable_value in zip(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       ):
            recursive_check(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
          elif tuple_object is None:
           return
          else:
           self.assertTrue(
               all(tf.equal(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )       )				,   msg=(
                   "Tuple and dict output are not equal. Difference:"
                   F" {tf.math.reduce_max(tf.abs(tuple_object - dict_object       )       )}"
               )				,   )

          recursive_check(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

        for model_class in self.all_model_classes:
         snake_case__      							=			model_class(__SCREAMING_SNAKE_CASE       )

         snake_case__      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
         snake_case__      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
         check_equivalence(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         snake_case__      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   return_labels=__SCREAMING_SNAKE_CASE       )
         snake_case__      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   return_labels=__SCREAMING_SNAKE_CASE       )
         check_equivalence(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

         snake_case__      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
         snake_case__      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
         check_equivalence(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   {"output_hidden_states": True}       )

         snake_case__      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   return_labels=__SCREAMING_SNAKE_CASE       )
         snake_case__      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   return_labels=__SCREAMING_SNAKE_CASE       )
         check_equivalence(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   {"output_hidden_states": True}       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE       )

       @slow
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"       )
        self.assertIsNotNone(__SCREAMING_SNAKE_CASE       )





def 							a__     (					)      ->					Optional[Any]:

 '''simple docstring'''






 snake_case__      							=			Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
 return image



@require_tf
@require_vision
class        lowercase_			(       unittest.TestCase							):

       @cached_property
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"       ) if is_vision_available() else None

       @slow
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"       )

        snake_case__      							=			self.default_image_processor
        snake_case__      							=			prepare_img()
        snake_case__      							=			image_processor(images=__SCREAMING_SNAKE_CASE				,   return_tensors="tf"       )

        # forward pass
        snake_case__      							=			model(**__SCREAMING_SNAKE_CASE       )

        # verify the logits
        snake_case__      							=			tf.TensorShape((1, 1000)       )
        self.assertEqual(outputs.logits.shape				,   __SCREAMING_SNAKE_CASE       )

        snake_case__      							=			tf.constant([-0.0260, -0.4739, 0.1911]       )

        tf.debugging.assert_near(outputs.logits[0, :3]				,   __SCREAMING_SNAKE_CASE				,   atol=1e-4       )








"""simple docstring"""


import copy
from typing import Dict

from ...generation.configuration_utils import GenerationConfig
from ...utils import logging


a__   :       str   						=			logging.get_logger(__name__)



class        lowercase_			(       __lowerCAmelCase							):
       snake_case__			:							Dict										=       """semantic"""

       def __init__(     self				,   __SCREAMING_SNAKE_CASE=1_0000				,   __SCREAMING_SNAKE_CASE=True				,   __SCREAMING_SNAKE_CASE=768				,   __SCREAMING_SNAKE_CASE=False				,   __SCREAMING_SNAKE_CASE=False				,   __SCREAMING_SNAKE_CASE=False				,   __SCREAMING_SNAKE_CASE=False				,   __SCREAMING_SNAKE_CASE=0.7				,   __SCREAMING_SNAKE_CASE=True				,   __SCREAMING_SNAKE_CASE=1_0048				,   __SCREAMING_SNAKE_CASE=12_9595				,   __SCREAMING_SNAKE_CASE=12_9599				,   __SCREAMING_SNAKE_CASE=1_0000				,   __SCREAMING_SNAKE_CASE=256				,   __SCREAMING_SNAKE_CASE=49.9				,   **__SCREAMING_SNAKE_CASE				,   ):






        '''simple docstring'''

        super().__init__(
            temperature=__SCREAMING_SNAKE_CASE				,   do_sample=__SCREAMING_SNAKE_CASE				,   eos_token_id=__SCREAMING_SNAKE_CASE				,   renormalize_logits=__SCREAMING_SNAKE_CASE				,   max_new_tokens=__SCREAMING_SNAKE_CASE				,   output_scores=__SCREAMING_SNAKE_CASE				,   return_dict_in_generate=__SCREAMING_SNAKE_CASE				,   output_hidden_states=__SCREAMING_SNAKE_CASE				,   output_attentions=__SCREAMING_SNAKE_CASE				,   **__SCREAMING_SNAKE_CASE				,   )

        snake_case__      							=			text_encoding_offset
        snake_case__      							=			text_pad_token
        snake_case__      							=			eos_token_id
        snake_case__      							=			semantic_infer_token
        snake_case__      							=			semantic_vocab_size
        snake_case__      							=			max_input_semantic_length
        snake_case__      							=			semantic_rate_hz



class        lowercase_			(       __lowerCAmelCase							):
       snake_case__			:							Union[str, Any]										=       """coarse_acoustics"""

       def __init__(     self				,   __SCREAMING_SNAKE_CASE=True				,   __SCREAMING_SNAKE_CASE=False				,   __SCREAMING_SNAKE_CASE=False				,   __SCREAMING_SNAKE_CASE=False				,   __SCREAMING_SNAKE_CASE=False				,   __SCREAMING_SNAKE_CASE=0.7				,   __SCREAMING_SNAKE_CASE=True				,   __SCREAMING_SNAKE_CASE=1_2048				,   __SCREAMING_SNAKE_CASE=75				,   __SCREAMING_SNAKE_CASE=2				,   __SCREAMING_SNAKE_CASE=1_2050				,   __SCREAMING_SNAKE_CASE=256				,   __SCREAMING_SNAKE_CASE = 630				,   __SCREAMING_SNAKE_CASE = 60				,   **__SCREAMING_SNAKE_CASE				,   ):






        '''simple docstring'''

        super().__init__(
            temperature=__SCREAMING_SNAKE_CASE				,   do_sample=__SCREAMING_SNAKE_CASE				,   renormalize_logits=__SCREAMING_SNAKE_CASE				,   output_scores=__SCREAMING_SNAKE_CASE				,   return_dict_in_generate=__SCREAMING_SNAKE_CASE				,   output_hidden_states=__SCREAMING_SNAKE_CASE				,   output_attentions=__SCREAMING_SNAKE_CASE				,   **__SCREAMING_SNAKE_CASE				,   )

        snake_case__      							=			coarse_semantic_pad_token
        snake_case__      							=			coarse_rate_hz
        snake_case__      							=			n_coarse_codebooks
        snake_case__      							=			coarse_infer_token
        snake_case__      							=			max_coarse_input_length
        snake_case__      							=			max_coarse_history
        snake_case__      							=			sliding_window_len



class        lowercase_			(       __lowerCAmelCase							):
       snake_case__			:							Union[str, Any]										=       """fine_acoustics"""

       def __init__(     self				,   __SCREAMING_SNAKE_CASE=0.5				,   __SCREAMING_SNAKE_CASE=512				,   __SCREAMING_SNAKE_CASE=1024				,   __SCREAMING_SNAKE_CASE=8				,   **__SCREAMING_SNAKE_CASE				,   ):






        '''simple docstring'''

        super().__init__(temperature=__SCREAMING_SNAKE_CASE       )

        snake_case__      							=			max_fine_history_length
        snake_case__      							=			max_fine_input_length
        snake_case__      							=			n_fine_codebooks



class        lowercase_			(       __lowerCAmelCase							):
       snake_case__			:							List[str]										=       """bark"""
       snake_case__			:							Dict										=       True

       def __init__(     self				,   __SCREAMING_SNAKE_CASE = None				,   __SCREAMING_SNAKE_CASE = None				,   __SCREAMING_SNAKE_CASE = None				,   __SCREAMING_SNAKE_CASE=2_4000				,   __SCREAMING_SNAKE_CASE=1024				,   **__SCREAMING_SNAKE_CASE				,   ):






        '''simple docstring'''

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

        if coarse_acoustics_config is None:
         snake_case__      							=			{}
         logger.info("coarse_acoustics_config is None. initializing the coarse model with default values."       )

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

        snake_case__      							=			BarkSemanticGenerationConfig(**__SCREAMING_SNAKE_CASE       )
        snake_case__      							=			BarkCoarseGenerationConfig(**__SCREAMING_SNAKE_CASE       )
        snake_case__      							=			BarkFineGenerationConfig(**__SCREAMING_SNAKE_CASE       )

        snake_case__      							=			sample_rate
        snake_case__      							=			codebook_size

       @classmethod
       def 						_SCREAMING_SNAKE_CASE(     cls				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   **__SCREAMING_SNAKE_CASE				,   ):






        '''simple docstring'''

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

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			copy.deepcopy(self.__dict__       )

        snake_case__      							=			self.semantic_config.to_dict()
        snake_case__      							=			self.coarse_acoustics_config.to_dict()
        snake_case__      							=			self.fine_acoustics_config.to_dict()

        snake_case__      							=			self.__class__.model_type
        return output

