

from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import RegNetConfig
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 TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST, TFRegNetForImageClassification, TFRegNetModel


if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor


class 			A						:







    """simple docstring"""





    def __init__(				self			:	int,lowercase_			:	Any,lowercase_			:	Any=3,lowercase_			:	Any=3_2,lowercase_			:	int=3,lowercase_			:	Union[str, Any]=1_0,lowercase_			:	Optional[Any]=[1_0, 2_0, 3_0, 4_0],lowercase_			:	Tuple=[1, 1, 2, 1],lowercase_			:	List[Any]=True,lowercase_			:	List[str]=True,lowercase_			:	Dict="relu",lowercase_			:	Union[str, Any]=3,lowercase_			:	List[str]=None,)->   Union[str, Any]:





        '''simple docstring'''







        A__   				=       parent
        A__   				=       batch_size
        A__   				=       image_size
        A__   				=       num_channels
        A__   				=       embeddings_size
        A__   				=       hidden_sizes
        A__   				=       depths
        A__   				=       is_training
        A__   				=       use_labels
        A__   				=       hidden_act
        A__   				=       num_labels
        A__   				=       scope
        A__   				=       len(lowercase_		)


    def 						snake_case__					(				self			:	Optional[Any]		)->   List[Any]:





        '''simple docstring'''







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

        A__   				=       None
        if self.use_labels:
            A__   				=       ids_tensor([self.batch_size],self.num_labels		)

        A__   				=       self.get_config()
        return config, pixel_values, labels


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





        '''simple docstring'''







        return RegNetConfig(
            num_channels=self.num_channels,embeddings_size=self.embeddings_size,hidden_sizes=self.hidden_sizes,depths=self.depths,hidden_act=self.hidden_act,num_labels=self.num_labels,)


    def 						snake_case__					(				self			:	Tuple,lowercase_			:	Tuple,lowercase_			:	Any,lowercase_			:	Dict		)->   Dict:





        '''simple docstring'''







        A__   				=       TFRegNetModel(config=lowercase_		)
        A__   				=       model(lowercase_,training=lowercase_		)
        # 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 // 3_2, self.image_size // 3_2),)


    def 						snake_case__					(				self			:	List[str],lowercase_			:	Optional[Any],lowercase_			:	Tuple,lowercase_			:	int		)->   List[Any]:





        '''simple docstring'''







        A__   				=       self.num_labels
        A__   				=       TFRegNetForImageClassification(lowercase_		)
        A__   				=       model(lowercase_,labels=lowercase_,training=lowercase_		)
        self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels)		)


    def 						snake_case__					(				self			:	Optional[Any]		)->   List[Any]:





        '''simple docstring'''







        A__   				=       self.prepare_config_and_inputs()
        A__				,						A__				,						A__   				=       config_and_inputs
        A__   				=       {'pixel_values': pixel_values}
        return config, inputs_dict




@require_tf
class 			A						(							_UpperCAmelCase   ,							_UpperCAmelCase   ,							unittest.TestCase			):







    """simple docstring"""





    lowerCamelCase :    str		     =							(TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
    lowerCamelCase :    Tuple		     =							(
        {'feature-extraction': TFRegNetModel, 'image-classification': TFRegNetForImageClassification}
        if is_tf_available()
        else {}
    )

    lowerCamelCase :    List[str]		     =							False
    lowerCamelCase :    List[Any]		     =							False
    lowerCamelCase :    Tuple		     =							False
    lowerCamelCase :    str		     =							False
    lowerCamelCase :    Optional[int]		     =							False


    def 						snake_case__					(				self			:	Dict		)->   Tuple:





        '''simple docstring'''







        A__   				=       TFRegNetModelTester(self		)
        A__   				=       ConfigTester(self,config_class=lowercase_,has_text_modality=lowercase_		)


    def 						snake_case__					(				self			:	List[str]		)->   List[str]:





        '''simple docstring'''







        return


    @unittest.skip(reason='RegNet does not use inputs_embeds'		)
    def 						snake_case__					(				self			:	List[str]		)->   Dict:





        '''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 						snake_case__					(				self			:	Optional[Any]		)->   str:





        '''simple docstring'''







        super().test_keras_fit()


    @unittest.skip(reason='RegNet does not support input and output embeddings'		)
    def 						snake_case__					(				self			:	Union[str, Any]		)->   Tuple:





        '''simple docstring'''







        pass


    def 						snake_case__					(				self			:	List[str]		)->   Any:





        '''simple docstring'''







        A__				,						A__   				=       self.model_tester.prepare_config_and_inputs_for_common()

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

            A__   				=       ['pixel_values']
            self.assertListEqual(arg_names[:1],lowercase_		)


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





        '''simple docstring'''







        A__   				=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase_		)


    def 						snake_case__					(				self			:	str		)->   List[Any]:





        '''simple docstring'''







        def check_hidden_states_output(lowercase_			:	str,lowercase_			:	Optional[int],lowercase_			:	List[str]		):
            A__   				=       model_class(lowercase_		)
            A__   				=       model(**self._prepare_for_class(lowercase_,lowercase_		),training=lowercase_		)

            A__   				=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

            A__   				=       self.model_tester.num_stages
            self.assertEqual(len(lowercase_		),expected_num_stages + 1		)

            # RegNet'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 // 2, self.model_tester.image_size // 2],)

        A__				,						A__   				=       self.model_tester.prepare_config_and_inputs_for_common()
        A__   				=       ['basic', 'bottleneck']
        for model_class in self.all_model_classes:
            for layer_type in layers_type:
                A__   				=       layer_type
                A__   				=       True
                check_hidden_states_output(lowercase_,lowercase_,lowercase_		)

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

                check_hidden_states_output(lowercase_,lowercase_,lowercase_		)


    def 						snake_case__					(				self			:	List[str]		)->   Dict:





        '''simple docstring'''







        A__				,						A__   				=       self.model_tester.prepare_config_and_inputs_for_common()

        def check_equivalence(lowercase_			:	Optional[Any],lowercase_			:	List[Any],lowercase_			:	str,lowercase_			:	int={}		):
            A__   				=       model(lowercase_,return_dict=lowercase_,**lowercase_		)
            A__   				=       model(lowercase_,return_dict=lowercase_,**lowercase_		).to_tuple()

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

                recursive_check(lowercase_,lowercase_		)

        for model_class in self.all_model_classes:
            A__   				=       model_class(lowercase_		)

            A__   				=       self._prepare_for_class(lowercase_,lowercase_		)
            A__   				=       self._prepare_for_class(lowercase_,lowercase_		)
            check_equivalence(lowercase_,lowercase_,lowercase_		)

            A__   				=       self._prepare_for_class(lowercase_,lowercase_,return_labels=lowercase_		)
            A__   				=       self._prepare_for_class(lowercase_,lowercase_,return_labels=lowercase_		)
            check_equivalence(lowercase_,lowercase_,lowercase_		)

            A__   				=       self._prepare_for_class(lowercase_,lowercase_		)
            A__   				=       self._prepare_for_class(lowercase_,lowercase_		)
            check_equivalence(lowercase_,lowercase_,lowercase_,{'output_hidden_states': True}		)

            A__   				=       self._prepare_for_class(lowercase_,lowercase_,return_labels=lowercase_		)
            A__   				=       self._prepare_for_class(lowercase_,lowercase_,return_labels=lowercase_		)
            check_equivalence(lowercase_,lowercase_,lowercase_,{'output_hidden_states': True}		)


    def 						snake_case__					(				self			:	Optional[int]		)->   str:





        '''simple docstring'''







        A__   				=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowercase_		)


    @slow
    def 						snake_case__					(				self			:	List[str]		)->   int:





        '''simple docstring'''







        for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A__   				=       TFRegNetModel.from_pretrained(lowercase_		)
            self.assertIsNotNone(lowercase_		)




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





    '''simple docstring'''






    A__   				=       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
    return image


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







    """simple docstring"""





    @cached_property
    def 						snake_case__					(				self			:	List[Any]		)->   List[Any]:





        '''simple docstring'''







        return (
            AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)
            if is_vision_available()
            else None
        )


    @slow
    def 						snake_case__					(				self			:	Dict		)->   Optional[int]:





        '''simple docstring'''







        A__   				=       TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)

        A__   				=       self.default_image_processor
        A__   				=       prepare_img()
        A__   				=       image_processor(images=lowercase_,return_tensors='tf'		)

        # forward pass
        A__   				=       model(**lowercase_,training=lowercase_		)

        # verify the logits
        A__   				=       tf.TensorShape((1, 1_0_0_0)		)
        self.assertEqual(outputs.logits.shape,lowercase_		)

        A__   				=       tf.constant([-0.4_180, -1.5_051, -3.4_836]		)

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




import os
import unittest
from tempfile import TemporaryDirectory

import torch
import torch.nn as nn

from accelerate.utils import (
    OffloadedWeightsLoader,
    extract_submodules_state_dict,
    load_offloaded_weight,
    offload_state_dict,
    offload_weight,
)


class 			A						(							nn.Module			):







    """simple docstring"""





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





        '''simple docstring'''







        super().__init__()
        A__   				=       nn.Linear(3,4		)
        A__   				=       nn.BatchNormad(4		)
        A__   				=       nn.Linear(4,5		)


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





        '''simple docstring'''







        return self.lineara(self.batchnorm(self.lineara(lowercase_		)		)		)




class 			A						(							unittest.TestCase			):







    """simple docstring"""





    def 						snake_case__					(				self			:	List[str]		)->   Any:





        '''simple docstring'''







        A__   				=       ModelForTest()
        with TemporaryDirectory() as tmp_dir:
            offload_state_dict(lowercase_,model.state_dict()		)
            A__   				=       os.path.join(lowercase_,'index.json'		)
            self.assertTrue(os.path.isfile(lowercase_		)		)
            # TODO: add tests on what is inside the index

            for key in ["linear1.weight", "linear1.bias", "linear2.weight", "linear2.bias"]:
                A__   				=       os.path.join(lowercase_,F'{key}.dat'		)
                self.assertTrue(os.path.isfile(lowercase_		)		)
                # TODO: add tests on the fact weights are properly loaded


    def 						snake_case__					(				self			:	List[Any]		)->   Optional[int]:





        '''simple docstring'''







        A__   				=       [torch.floataa, torch.floataa, torch.bfloataa]

        for dtype in dtypes:
            A__   				=       torch.randn(2,3,dtype=lowercase_		)
            with TemporaryDirectory() as tmp_dir:
                A__   				=       offload_weight(lowercase_,'weight',lowercase_,{}		)
                A__   				=       os.path.join(lowercase_,'weight.dat'		)
                self.assertTrue(os.path.isfile(lowercase_		)		)
                self.assertDictEqual(lowercase_,{'weight': {'shape': [2, 3], 'dtype': str(lowercase_		).split('.'		)[1]}}		)

                A__   				=       load_offloaded_weight(lowercase_,index['weight']		)
                self.assertTrue(torch.equal(lowercase_,lowercase_		)		)


    def 						snake_case__					(				self			:	Optional[int]		)->   Optional[Any]:





        '''simple docstring'''







        A__   				=       ModelForTest()
        A__   				=       model.state_dict()
        A__   				=       {k: v for k, v in state_dict.items() if 'linear2' not in k}
        A__   				=       {k: v for k, v in state_dict.items() if 'linear2' in k}

        with TemporaryDirectory() as tmp_dir:
            offload_state_dict(lowercase_,lowercase_		)
            A__   				=       OffloadedWeightsLoader(state_dict=lowercase_,save_folder=lowercase_		)

            # Every key is there with the right value
            self.assertEqual(sorted(lowercase_		),sorted(state_dict.keys()		)		)
            for key, param in state_dict.items():
                self.assertTrue(torch.allclose(lowercase_,weight_map[key]		)		)

        A__   				=       {k: v for k, v in state_dict.items() if 'weight' in k}
        A__   				=       {k: v for k, v in state_dict.items() if 'weight' not in k}

        with TemporaryDirectory() as tmp_dir:
            offload_state_dict(lowercase_,lowercase_		)
            A__   				=       OffloadedWeightsLoader(state_dict=lowercase_,save_folder=lowercase_		)

            # Every key is there with the right value
            self.assertEqual(sorted(lowercase_		),sorted(state_dict.keys()		)		)
            for key, param in state_dict.items():
                self.assertTrue(torch.allclose(lowercase_,weight_map[key]		)		)

        with TemporaryDirectory() as tmp_dir:
            offload_state_dict(lowercase_,lowercase_		)
            # Duplicates are removed
            A__   				=       OffloadedWeightsLoader(state_dict=lowercase_,save_folder=lowercase_		)

            # Every key is there with the right value
            self.assertEqual(sorted(lowercase_		),sorted(state_dict.keys()		)		)
            for key, param in state_dict.items():
                self.assertTrue(torch.allclose(lowercase_,weight_map[key]		)		)


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





        '''simple docstring'''







        A__   				=       {'a.1': 0, 'a.10': 1, 'a.2': 2}
        A__   				=       extract_submodules_state_dict(lowercase_,['a.1', 'a.2']		)
        self.assertDictEqual(lowercase_,{'a.1': 0, 'a.2': 2}		)

        A__   				=       {'a.1.a': 0, 'a.10.a': 1, 'a.2.a': 2}
        A__   				=       extract_submodules_state_dict(lowercase_,['a.1', 'a.2']		)
        self.assertDictEqual(lowercase_,{'a.1.a': 0, 'a.2.a': 2}		)

