
"""simple docstring"""




import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs



class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				def __init__(self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=None   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=None   ,       **SCREAMING_SNAKE_CASE__    )					->		List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   parent
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   config_class
											SCREAMING_SNAKE_CASE__						:       int           =   has_text_modality
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   kwargs
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   common_properties



				def 	__magic_name__    (self    )					->		int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   self.config_class(**self.inputs_dict    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   (
											    ["""hidden_size""", """num_attention_heads""", """num_hidden_layers"""]
											    if self.common_properties is None
											    else self.common_properties
											)

											# Add common fields for text models
											if self.has_text_modality:
																		common_properties.extend(["""vocab_size"""]    )

											# Test that config has the common properties as getters
											for prop in common_properties:
																		self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )   ,       msg=F'''`{prop}` does not exist'''    )

											# Test that config has the common properties as setter
											for idx, name in enumerate(SCREAMING_SNAKE_CASE__    ):
																		try:
																									setattr(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )
																									self.parent.assertEqual(
																									    getattr(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )   ,       SCREAMING_SNAKE_CASE__   ,       msg=F'''`{name} value {idx} expected, but was {getattr(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )}'''    )
																		except NotImplementedError:
																									# Some models might not be able to implement setters for common_properties
																									# In that case, a NotImplementedError is raised
																									pass

        # Test if config class can be called with Config(prop_name=..)
											for idx, name in enumerate(SCREAMING_SNAKE_CASE__    ):
																		try:
																									SCREAMING_SNAKE_CASE__						:       Tuple           =   self.config_class(**{name: idx}    )
																									self.parent.assertEqual(
																									    getattr(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )   ,       SCREAMING_SNAKE_CASE__   ,       msg=F'''`{name} value {idx} expected, but was {getattr(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )}'''    )
																		except NotImplementedError:
																									# Some models might not be able to implement setters for common_properties
																									# In that case, a NotImplementedError is raised
																									pass



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.config_class(**self.inputs_dict    )
											SCREAMING_SNAKE_CASE__						:       Any           =   json.loads(config.to_json_string()    )
											for key, value in self.inputs_dict.items():
																		self.parent.assertEqual(obj[key]   ,       SCREAMING_SNAKE_CASE__    )



				def 	__magic_name__    (self    )					->		Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   self.config_class(**self.inputs_dict    )

											with tempfile.TemporaryDirectory() as tmpdirname:
																		SCREAMING_SNAKE_CASE__						:       Any           =   os.path.join(SCREAMING_SNAKE_CASE__   ,       """config.json"""    )
																		config_first.to_json_file(SCREAMING_SNAKE_CASE__    )
																		SCREAMING_SNAKE_CASE__						:       str           =   self.config_class.from_json_file(SCREAMING_SNAKE_CASE__    )

											self.parent.assertEqual(config_second.to_dict()   ,       config_first.to_dict()    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Tuple           =   self.config_class(**self.inputs_dict    )

											with tempfile.TemporaryDirectory() as tmpdirname:
																		config_first.save_pretrained(SCREAMING_SNAKE_CASE__    )
																		SCREAMING_SNAKE_CASE__						:       Any           =   self.config_class.from_pretrained(SCREAMING_SNAKE_CASE__    )

											self.parent.assertEqual(config_second.to_dict()   ,       config_first.to_dict()    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.config_class(**self.inputs_dict    )

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   """test"""
											with tempfile.TemporaryDirectory() as tmpdirname:
																		SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   os.path.join(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    )
																		config_first.save_pretrained(SCREAMING_SNAKE_CASE__    )
																		SCREAMING_SNAKE_CASE__						:       Any           =   self.config_class.from_pretrained(SCREAMING_SNAKE_CASE__   ,       subfolder=SCREAMING_SNAKE_CASE__    )

											self.parent.assertEqual(config_second.to_dict()   ,       config_first.to_dict()    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.config_class(**self.inputs_dict   ,       num_labels=5    )
											self.parent.assertEqual(len(config.idalabel    )   ,       5    )
											self.parent.assertEqual(len(config.labelaid    )   ,       5    )

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   3
											self.parent.assertEqual(len(config.idalabel    )   ,       3    )
											self.parent.assertEqual(len(config.labelaid    )   ,       3    )



				def 	__magic_name__    (self    )					->		Any:



											"""simple docstring"""




											if self.config_class.is_composition:
																		return
											SCREAMING_SNAKE_CASE__						:       str           =   self.config_class()
											self.parent.assertIsNotNone(SCREAMING_SNAKE_CASE__    )



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   copy.deepcopy(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.config_class(**SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       str           =   []
											for key, value in config_common_kwargs.items():
																		if key == "torch_dtype":
																									if not is_torch_available():
																																continue
																									else:
																																import torch

																																if config.torch_dtype != torch.floataa:
																																							wrong_values.append(("""torch_dtype""", config.torch_dtype, torch.floataa)    )
																		elif getattr(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    ) != value:
																									wrong_values.append((key, getattr(SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__    ), value)    )

											if len(SCREAMING_SNAKE_CASE__    ) > 0:
																		SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   """\n""".join([F'''- {v[0]}: got {v[1]} instead of {v[2]}''' for v in wrong_values]    )
																		raise ValueError(F'''The following keys were not properly set in the config:\n{errors}'''    )



				def 	__magic_name__    (self    )					->		Optional[Any]:



											"""simple docstring"""




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







# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
  import torch





class  __lowerCAmelCase     (					TensorFormatter[Mapping, """torch.Tensor""", Mapping]							):




  def __init__(				self							,   lowerCAmelCase=None							,   **lowerCAmelCase					)  -> Optional[Any]:
        '''simple docstring'''






        super().__init__(features=lowerCAmelCase					)
        _lowercase	       =torch_tensor_kwargs
        import torch  # noqa import torch at initialization




  def   A__     (				self							,   lowerCAmelCase					)  -> int:
        '''simple docstring'''






        import torch

        if isinstance(lowerCAmelCase							,   lowerCAmelCase					) and column:
              if all(
                  isinstance(lowerCAmelCase							,   torch.Tensor					) and x.shape == column[0].shape and x.dtype == column[0].dtype
                  for x in column					):
                    return torch.stack(lowerCAmelCase					)
        return column




  def   A__     (				self							,   lowerCAmelCase					)  -> str:
        '''simple docstring'''






        import torch

        if isinstance(lowerCAmelCase							,   (str, bytes, type(lowerCAmelCase					))					):
              return value
        elif isinstance(lowerCAmelCase							,   (np.character, np.ndarray)					) and np.issubdtype(value.dtype							,   np.character					):
              return value.tolist()

        _lowercase	       ={}

        if isinstance(lowerCAmelCase							,   (np.number, np.ndarray)					) and np.issubdtype(value.dtype							,   np.integer					):
              _lowercase	       ={'dtype': torch.intaa}
        elif isinstance(lowerCAmelCase							,   (np.number, np.ndarray)					) and np.issubdtype(value.dtype							,   np.floating					):
              _lowercase	       ={'dtype': torch.floataa}
        elif config.PIL_AVAILABLE and "PIL" in sys.modules:
              import PIL.Image

              if isinstance(lowerCAmelCase							,   PIL.Image.Image					):
                    _lowercase	       =np.asarray(lowerCAmelCase					)
        return torch.tensor(lowerCAmelCase							,   **{**default_dtype, **self.torch_tensor_kwargs}					)




  def   A__     (				self							,   lowerCAmelCase					)  -> str:
        '''simple docstring'''






        import torch

        # support for torch, tf, jax etc.
        if hasattr(lowerCAmelCase							,   '__array__'					) and not isinstance(lowerCAmelCase							,   torch.Tensor					):
              _lowercase	       =data_struct.__array__()
        # support for nested types like struct of list of struct
        if isinstance(lowerCAmelCase							,   np.ndarray					):
              if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
                    return self._consolidate([self.recursive_tensorize(lowerCAmelCase					) for substruct in data_struct]					)
        elif isinstance(lowerCAmelCase							,   (list, tuple)					):
              return self._consolidate([self.recursive_tensorize(lowerCAmelCase					) for substruct in data_struct]					)
        return self._tensorize(lowerCAmelCase					)




  def   A__     (				self							,   lowerCAmelCase					)  -> Tuple:
        '''simple docstring'''






        return map_nested(self._recursive_tensorize							,   lowerCAmelCase							,   map_list=lowerCAmelCase					)




  def   A__     (				self							,   lowerCAmelCase					)  -> Mapping:
        '''simple docstring'''






        _lowercase	       =self.numpy_arrow_extractor().extract_row(lowerCAmelCase					)
        _lowercase	       =self.python_features_decoder.decode_row(lowerCAmelCase					)
        return self.recursive_tensorize(lowerCAmelCase					)




  def   A__     (				self							,   lowerCAmelCase					)  -> "torch.Tensor":
        '''simple docstring'''






        _lowercase	       =self.numpy_arrow_extractor().extract_column(lowerCAmelCase					)
        _lowercase	       =self.python_features_decoder.decode_column(lowerCAmelCase							,   pa_table.column_names[0]					)
        _lowercase	       =self.recursive_tensorize(lowerCAmelCase					)
        _lowercase	       =self._consolidate(lowerCAmelCase					)
        return column




  def   A__     (				self							,   lowerCAmelCase					)  -> Mapping:
        '''simple docstring'''






        _lowercase	       =self.numpy_arrow_extractor().extract_batch(lowerCAmelCase					)
        _lowercase	       =self.python_features_decoder.decode_batch(lowerCAmelCase					)
        _lowercase	       =self.recursive_tensorize(lowerCAmelCase					)
        for column_name in batch:
              _lowercase	       =self._consolidate(batch[column_name]					)
        return batch


