





"""simple docstring"""
import copy
import inspect

from transformers.testing_utils import require_torch, torch_device
from transformers.utils.backbone_utils import BackboneType






@require_torch
class       _SCREAMING_SNAKE_CASE:
       SCREAMING_SNAKE_CASE_     :   str					=						()
       SCREAMING_SNAKE_CASE_     :   Dict					=						True




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :List[Any]	       =						self.config_class

             # test default config
             __SCREAMING_SNAKE_CASE     :List[Any]	       =						config_class()
             self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE     :Optional[int]	       =						['''stem'''] + [f'''stage{idx}''' for idx in range(1	,len(config.depths							) + 1							)]
             self.assertEqual(config.stage_names	,SCREAMING_SNAKE_CASE__							)
             self.assertTrue(set(config.out_features							).issubset(set(config.stage_names							)							)							)

             # Test out_features and out_indices are correctly set
             # out_features and out_indices both None
             __SCREAMING_SNAKE_CASE     :Dict	       =						config_class(out_features=SCREAMING_SNAKE_CASE__	,out_indices=SCREAMING_SNAKE_CASE__							)
             self.assertEqual(config.out_features	,[config.stage_names[-1]]							)
             self.assertEqual(config.out_indices	,[len(config.stage_names							) - 1]							)

             # out_features and out_indices both set
             __SCREAMING_SNAKE_CASE     :List[Any]	       =						config_class(out_features=['''stem''', '''stage1''']	,out_indices=[0, 1]							)
             self.assertEqual(config.out_features	,['''stem''', '''stage1''']							)
             self.assertEqual(config.out_indices	,[0, 1]							)

             # Only out_features set
             __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						config_class(out_features=['''stage1''', '''stage3''']							)
             self.assertEqual(config.out_features	,['''stage1''', '''stage3''']							)
             self.assertEqual(config.out_indices	,[1, 3]							)

             # Only out_indices set
             __SCREAMING_SNAKE_CASE     :Tuple	       =						config_class(out_indices=[0, 2]							)
             self.assertEqual(config.out_features	,[config.stage_names[0], config.stage_names[2]]							)
             self.assertEqual(config.out_indices	,[0, 2]							)

             # Error raised when out_indices do not correspond to out_features
             with self.assertRaises(SCREAMING_SNAKE_CASE__							):
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						config_class(out_features=['''stage1''', '''stage2''']	,out_indices=[0, 2]							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Dict	       =						self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE     :str	       =						inspect.signature(model.forward							)
                   # signature.parameters is an OrderedDict => so arg_names order is deterministic
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						[*signature.parameters.keys()]
                   __SCREAMING_SNAKE_CASE     :List[str]	       =						['''pixel_values''']
                   self.assertListEqual(arg_names[:1]	,SCREAMING_SNAKE_CASE__							)




       def 					_UpperCamelCase  (		self							)    ->   str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :List[Any]	       =						self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   self.assertEqual(len(model.channels							)	,len(config.out_features							)							)
                   __SCREAMING_SNAKE_CASE     :Tuple	       =						model.num_features
                   __SCREAMING_SNAKE_CASE     :Tuple	       =						[config.stage_names.index(SCREAMING_SNAKE_CASE__							) for feat in config.out_features]
                   __SCREAMING_SNAKE_CASE     :Tuple	       =						[num_features[idx] for idx in out_indices]
                   self.assertListEqual(model.channels	,SCREAMING_SNAKE_CASE__							)

                   __SCREAMING_SNAKE_CASE     :str	       =						copy.deepcopy(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE     :List[Any]	       =						None
                   __SCREAMING_SNAKE_CASE     :Tuple	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   self.assertEqual(len(model.channels							)	,1							)
                   self.assertListEqual(model.channels	,[num_features[-1]]							)

                   __SCREAMING_SNAKE_CASE     :Any	       =						copy.deepcopy(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE     :int	       =						None
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   self.assertEqual(len(model.channels							)	,1							)
                   self.assertListEqual(model.channels	,[num_features[-1]]							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :List[Any]	       =						self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   model.to(SCREAMING_SNAKE_CASE__							)
                   model.eval()
                   __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						model(**SCREAMING_SNAKE_CASE__							)

                   self.assertEqual(len(result.feature_maps							)	,len(config.out_features							)							)
                   self.assertEqual(len(model.channels							)	,len(config.out_features							)							)
                   self.assertEqual(len(result.feature_maps							)	,len(config.out_indices							)							)
                   self.assertEqual(len(model.channels							)	,len(config.out_indices							)							)

                   # Check output of last stage is taken if out_features=None, out_indices=None
                   __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						copy.deepcopy(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						None
                   __SCREAMING_SNAKE_CASE     :Dict	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   model.to(SCREAMING_SNAKE_CASE__							)
                   model.eval()
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						model(**SCREAMING_SNAKE_CASE__							)

                   self.assertEqual(len(result.feature_maps							)	,1							)
                   self.assertEqual(len(model.channels							)	,1							)

                   __SCREAMING_SNAKE_CASE     :Dict	       =						copy.deepcopy(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE     :int	       =						None
                   __SCREAMING_SNAKE_CASE     :int	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   model.to(SCREAMING_SNAKE_CASE__							)
                   model.eval()
                   __SCREAMING_SNAKE_CASE     :int	       =						model(**SCREAMING_SNAKE_CASE__							)

                   self.assertEqual(len(result.feature_maps							)	,1							)
                   self.assertEqual(len(model.channels							)	,1							)

                   # Check backbone can be initialized with fresh weights
                   __SCREAMING_SNAKE_CASE     :int	       =						copy.deepcopy(SCREAMING_SNAKE_CASE__							)
                   __SCREAMING_SNAKE_CASE     :Dict	       =						False
                   __SCREAMING_SNAKE_CASE     :Dict	       =						model_class(SCREAMING_SNAKE_CASE__							)
                   model.to(SCREAMING_SNAKE_CASE__							)
                   model.eval()
                   __SCREAMING_SNAKE_CASE     :Any	       =						model(**SCREAMING_SNAKE_CASE__							)




       def 					_UpperCamelCase  (		self							)    ->   str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :List[str]	       =						self.model_tester.prepare_config_and_inputs_for_common()

             for backbone_class in self.all_model_classes:
                   __SCREAMING_SNAKE_CASE     :str	       =						backbone_class(SCREAMING_SNAKE_CASE__							)

                   self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__	,'''backbone_type'''							)							)
                   self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__	,'''stage_names'''							)							)
                   self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__	,'''num_features'''							)							)
                   self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__	,'''out_indices'''							)							)
                   self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__	,'''out_features'''							)							)
                   self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__	,'''out_feature_channels'''							)							)
                   self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__	,'''channels'''							)							)

                   self.assertIsInstance(backbone.backbone_type	,SCREAMING_SNAKE_CASE__							)
                   # Verify num_features has been initialized in the backbone init
                   self.assertIsNotNone(backbone.num_features							)
                   self.assertTrue(len(backbone.channels							) == len(backbone.out_indices							)							)
                   self.assertTrue(len(backbone.stage_names							) == len(backbone.num_features							)							)
                   self.assertTrue(len(backbone.channels							) <= len(backbone.num_features							)							)
                   self.assertTrue(len(backbone.out_feature_channels							) == len(backbone.stage_names							)							)




       def 					_UpperCamelCase  (		self							)    ->   Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Any	       =						self.model_tester.prepare_config_and_inputs_for_common()
             __SCREAMING_SNAKE_CASE     :Tuple	       =						inputs_dict['''pixel_values'''].shape[0]

             for backbone_class in self.all_model_classes:
                   __SCREAMING_SNAKE_CASE     :str	       =						backbone_class(SCREAMING_SNAKE_CASE__							)
                   backbone.to(SCREAMING_SNAKE_CASE__							)
                   backbone.eval()

                   __SCREAMING_SNAKE_CASE     :Dict	       =						backbone(**SCREAMING_SNAKE_CASE__							)

                   # Test default outputs and verify feature maps
                   self.assertIsInstance(outputs.feature_maps	,SCREAMING_SNAKE_CASE__							)
                   self.assertTrue(len(outputs.feature_maps							) == len(backbone.channels							)							)
                   for feature_map, n_channels in zip(outputs.feature_maps	,backbone.channels							):
                         self.assertTrue(feature_map.shape[:2]	,(batch_size, n_channels)							)
                   self.assertIsNone(outputs.hidden_states							)
                   self.assertIsNone(outputs.attentions							)

                   # Test output_hidden_states=True
                   __SCREAMING_SNAKE_CASE     :List[Any]	       =						backbone(**SCREAMING_SNAKE_CASE__	,output_hidden_states=SCREAMING_SNAKE_CASE__							)
                   self.assertIsNotNone(outputs.hidden_states							)
                   self.assertTrue(len(outputs.hidden_states							)	,len(backbone.stage_names							)							)
                   for hidden_state, n_channels in zip(outputs.hidden_states	,backbone.channels							):
                         self.assertTrue(hidden_state.shape[:2]	,(batch_size, n_channels)							)

                   # Test output_attentions=True
                   if self.has_attentions:
                         __SCREAMING_SNAKE_CASE     :Optional[int]	       =						backbone(**SCREAMING_SNAKE_CASE__	,output_attentions=SCREAMING_SNAKE_CASE__							)
                         self.assertIsNotNone(outputs.attentions							)






"""simple docstring"""
import json
import os
import tempfile

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features import ArrayaD
from utils import generate_examples, get_duration


lowerCamelCase_          =	(3_0, 4_8_7)
lowerCamelCase_          =	(3_6, 1_0_2_4)
lowerCamelCase_          =	(1_0_0, 1_0_0)
lowerCamelCase_          =	1_0_0

lowerCamelCase_          =	datasets.Features(
    {"text": ArrayaD(SHAPE_TEST_1, dtype="float32"), "image": ArrayaD(SHAPE_TEST_2, dtype="float32")}
)

lowerCamelCase_    ,  lowerCamelCase_          =	os.path.split(__file__)
lowerCamelCase_          =	os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json"))



@get_duration
def   __lowerCamelCase     (							a_  :	Optional[Any]				,     a_  :	List[str]				,     a_  :	List[str]							)    ->   List[str]:
      with ArrowWriter(features=a_				,     path=os.path.join(a_				,     '''beta.arrow'''							)							) as writer:
            for key, record in dummy_data:
                  __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						my_features.encode_example(a_							)
                  writer.write(a_							)
            __SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE     :Optional[Any]	       =						writer.finalize()



@get_duration
def   __lowerCamelCase     (							a_  :	Tuple				,     a_  :	Optional[Any]							)    ->   Dict:
      __SCREAMING_SNAKE_CASE     :Tuple	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      for _ in dataset:
            pass



@get_duration
def   __lowerCamelCase     (							a_  :	List[str]				,     a_  :	List[str]							)    ->   List[Any]:
      __SCREAMING_SNAKE_CASE     :Optional[int]	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      dataset.set_format('''numpy'''							)
      for _ in dataset:
            pass



@get_duration
def   __lowerCamelCase     (							a_  :	Tuple				,     a_  :	Optional[int]							)    ->   Optional[int]:
      __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						10
      __SCREAMING_SNAKE_CASE     :Dict	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      for i in range(0				,     len(a_							)				,     a_							):
            __SCREAMING_SNAKE_CASE     :Dict	       =						dataset[i : i + batch_size]



@get_duration
def   __lowerCamelCase     (							a_  :	Any				,     a_  :	Optional[Any]							)    ->   Optional[Any]:
      __SCREAMING_SNAKE_CASE     :List[Any]	       =						10
      __SCREAMING_SNAKE_CASE     :List[str]	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      dataset.set_format('''numpy'''							)
      for i in range(0				,     len(a_							)				,     a_							):
            __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						dataset[i : i + batch_size]



@get_duration
def   __lowerCamelCase     (							a_  :	List[Any]				,     a_  :	Optional[Any]							)    ->   List[Any]:
      __SCREAMING_SNAKE_CASE     :Optional[int]	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      for col in feats:
            __SCREAMING_SNAKE_CASE     :Tuple	       =						dataset[col]



@get_duration
def   __lowerCamelCase     (							a_  :	int				,     a_  :	List[str]							)    ->   str:
      __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						datasets.Dataset.from_file(
          filename=os.path.join(a_				,     '''beta.arrow'''							)				,     info=datasets.DatasetInfo(features=a_							)							)
      dataset.set_format('''numpy'''							)
      for col in feats:
            __SCREAMING_SNAKE_CASE     :List[Any]	       =						dataset[col]



def   __lowerCamelCase     (							)    ->   Union[str, Any]:
      __SCREAMING_SNAKE_CASE     :Tuple	       =						{}
      __SCREAMING_SNAKE_CASE     :int	       =						(
          read_unformated,
          read_formatted_as_numpy,
          read_batch_unformated,
          read_batch_formatted_as_numpy,
          read_col_unformated,
          read_col_formatted_as_numpy,
      )
      with tempfile.TemporaryDirectory() as tmp_dir:
            __SCREAMING_SNAKE_CASE     :Dict	       =						datasets.Features({'''image''': ArrayaD(a_				,     dtype='''float32'''							)}							)
            __SCREAMING_SNAKE_CASE     :Any	       =						generate_examples(features=a_				,     num_examples=a_							)
            __SCREAMING_SNAKE_CASE     :Dict	       =						write(a_				,     a_				,     a_							)
            for read_func in read_functions:
                  __SCREAMING_SNAKE_CASE     :str	       =						read_func(a_				,     a_							)

      with tempfile.TemporaryDirectory() as tmp_dir:
            # don't use fixed length for fair comparison
            # feats = datasets.Features(
            #     {"image": datasets.Sequence(datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[1]), SPEED_TEST_SHAPE[0])}
            # )
            __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						datasets.Features({'''image''': datasets.Sequence(datasets.Sequence(datasets.Value('''float32'''							)							)							)}							)
            __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						generate_examples(
                features=a_				,     num_examples=a_				,     seq_shapes={'''image''': SPEED_TEST_SHAPE}							)
            __SCREAMING_SNAKE_CASE     :Tuple	       =						write(a_				,     a_				,     a_							)
            for read_func in read_functions:
                  __SCREAMING_SNAKE_CASE     :Tuple	       =						read_func(a_				,     a_							)

      with tempfile.TemporaryDirectory() as tmp_dir:
            # don't use fixed length for fair comparison
            # feats = datasets.Features(
            #     {"image": datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1])}
            # )
            __SCREAMING_SNAKE_CASE     :Dict	       =						datasets.Features({'''image''': datasets.Sequence(datasets.Value('''float32'''							)							)}							)
            __SCREAMING_SNAKE_CASE     :Dict	       =						generate_examples(
                features=a_				,     num_examples=a_				,     seq_shapes={'''image''': [SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1]]}				,     )
            __SCREAMING_SNAKE_CASE     :Any	       =						write(a_				,     a_				,     a_							)
            for read_func in read_functions:
                  __SCREAMING_SNAKE_CASE     :str	       =						read_func(a_				,     a_							)

      with open(a_				,     '''wb'''							) as f:
            f.write(json.dumps(a_							).encode('''utf-8'''							)							)


if __name__ == "__main__":  # useful to run the profiler
  benchmark_array_xd()