





"""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 			_UpperCAmelCase  :



      '''simple docstring'''
      lowercase_				:			Any        =       ()
      lowercase_				:			List[Any]        =       True

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Optional[Any]    						=  self.config_class

        # test default config
        A_  :	str    						=  config_class()
        self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )
        A_  :	Any    						=  ['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
        A_  :	List[str]    						=  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
        A_  :	int    						=  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
        A_  :	List[str]    						=  config_class(out_features=['stage1', 'stage3']  )
        self.assertEqual(config.out_features ,				['stage1', 'stage3']  )
        self.assertEqual(config.out_indices ,				[1, 3]  )

        # Only out_indices set
        A_  :	Optional[int]    						=  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  ):
          A_  :	List[str]    						=  config_class(out_features=['stage1', 'stage2'] ,				out_indices=[0, 2]  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

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

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

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_						,	A_  :	Union[str, Any]    						=  self.model_tester.prepare_config_and_inputs_for_common()

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

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

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

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_						,	A_  :	Union[str, Any]    						=  self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          A_  :	Dict    						=  model_class(__SCREAMING_SNAKE_CASE  )
          model.to(__SCREAMING_SNAKE_CASE  )
          model.eval()
          A_  :	Dict    						=  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
          A_  :	Union[str, Any]    						=  copy.deepcopy(__SCREAMING_SNAKE_CASE  )
          A_  :	Dict    						=  None
          A_  :	Union[str, Any]    						=  model_class(__SCREAMING_SNAKE_CASE  )
          model.to(__SCREAMING_SNAKE_CASE  )
          model.eval()
          A_  :	Union[str, Any]    						=  model(**__SCREAMING_SNAKE_CASE  )

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

          A_  :	Dict    						=  copy.deepcopy(__SCREAMING_SNAKE_CASE  )
          A_  :	int    						=  None
          A_  :	List[Any]    						=  model_class(__SCREAMING_SNAKE_CASE  )
          model.to(__SCREAMING_SNAKE_CASE  )
          model.eval()
          A_  :	Tuple    						=  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
          A_  :	List[str]    						=  copy.deepcopy(__SCREAMING_SNAKE_CASE  )
          A_  :	Optional[Any]    						=  False
          A_  :	Optional[Any]    						=  model_class(__SCREAMING_SNAKE_CASE  )
          model.to(__SCREAMING_SNAKE_CASE  )
          model.eval()
          A_  :	Dict    						=  model(**__SCREAMING_SNAKE_CASE  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

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

        for backbone_class in self.all_model_classes:
          A_  :	Any    						=  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  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_						,	A_  :	str    						=  self.model_tester.prepare_config_and_inputs_for_common()
        A_  :	Dict    						=  inputs_dict['pixel_values'].shape[0]

        for backbone_class in self.all_model_classes:
          A_  :	Union[str, Any]    						=  backbone_class(__SCREAMING_SNAKE_CASE  )
          backbone.to(__SCREAMING_SNAKE_CASE  )
          backbone.eval()

          A_  :	List[Any]    						=  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
          A_  :	Optional[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:
            A_  :	str    						=  backbone(**__SCREAMING_SNAKE_CASE ,				output_attentions=__SCREAMING_SNAKE_CASE  )
            self.assertIsNotNone(outputs.attentions  )











"""simple docstring"""




from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  if not inplace:
    A_  :	Optional[int]    						=  ma + ma
  else:
    ma += ma

  return ma





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  A_  :	Tuple    						=  -1 * len(_UpperCAmelCase )
  A_  :	List[str]    						=  list(range(len(tensor.shape[:zero_index] ) ) )
  return tensor.permute(first_inds + [zero_index + i for i in inds] )





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  return t.reshape(t.shape[:-no_dims] + (-1,) )





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase = 1E-4 ):






  """simple docstring"""





  A_  :	Optional[Any]    						=  mask.expand(*value.shape )
  return torch.sum(mask * value			, dim=_UpperCAmelCase ) / (eps + torch.sum(_UpperCAmelCase			, dim=_UpperCAmelCase ))





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase = 2.3_125			, _UpperCAmelCase = 21.6_875			, _UpperCAmelCase = 64 ):






  """simple docstring"""





  A_  :	List[str]    						=  torch.linspace(_UpperCAmelCase			, _UpperCAmelCase			, no_bins - 1			, device=pts.device )
  A_  :	Optional[Any]    						=  torch.sqrt(torch.sum((pts.unsqueeze(-2 ) - pts.unsqueeze(-3 )) ** 2			, dim=-1 ) )
  return torch.bucketize(_UpperCAmelCase			, _UpperCAmelCase )





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  A_  :	Any    						=  dicts[0]
  A_  :	int    						=  {}
  for k, v in first.items():
    A_  :	Dict    						=  [d[k] for d in dicts]
    if isinstance(_UpperCAmelCase			, _UpperCAmelCase ):
      A_  :	List[Any]    						=  dict_multimap(_UpperCAmelCase			, _UpperCAmelCase )
    else:
      A_  :	Union[str, Any]    						=  fn(_UpperCAmelCase )

  return new_dict





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  A_  :	Tuple    						=  v_bins.view(((1,) * len(x.shape )) + (len(_UpperCAmelCase ),) )
  A_  :	Any    						=  x[..., None] - reshaped_bins
  A_  :	List[str]    						=  torch.argmin(torch.abs(_UpperCAmelCase )			, dim=-1 )
  return nn.functional.one_hot(_UpperCAmelCase			, num_classes=len(_UpperCAmelCase ) ).float()





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase = 0			, _UpperCAmelCase = 0 ):






  """simple docstring"""





  A_  :	List[Union[slice, torch.Tensor]]    						=  []
  for i, s in enumerate(data.shape[:no_batch_dims] ):
    A_  :	List[Any]    						=  torch.arange(_UpperCAmelCase )
    A_  :	Dict    						=  r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape ) - i - 1))) )
    ranges.append(_UpperCAmelCase )

  A_  :	List[Union[slice, torch.Tensor]]    						=  [slice(_UpperCAmelCase ) for _ in range(len(data.shape ) - no_batch_dims )]
  A_  :	Optional[Any]    						=  inds
  ranges.extend(_UpperCAmelCase )
  # Matt note: Editing this to get around the behaviour of using a list as an array index changing
  # in recent Numpy versions
  return data[tuple(_UpperCAmelCase )]


lowerCamelCase_					:						Union[str, Any]      = TypeVar('T')





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  A_  :	Dict[Any, Union[dict, list, tuple, Any]]    						=  {}
  for k, v in dic.items():
    if isinstance(_UpperCAmelCase			, _UpperCAmelCase ):
      A_  :	int    						=  dict_map(_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase )
    else:
      A_  :	Any    						=  tree_map(_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase )

  return new_dict





@overload
def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  ...





@overload
def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  ...





@overload
def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  ...





@overload
def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  ...





def 						UpperCAmelCase__		(					_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase ):






  """simple docstring"""





  if isinstance(_UpperCAmelCase			, _UpperCAmelCase ):
    return dict_map(_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase )
  elif isinstance(_UpperCAmelCase			, _UpperCAmelCase ):
    return [tree_map(_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase ) for x in tree]
  elif isinstance(_UpperCAmelCase			, _UpperCAmelCase ):
    return tuple(tree_map(_UpperCAmelCase			, _UpperCAmelCase			, _UpperCAmelCase ) for x in tree )
  elif isinstance(_UpperCAmelCase			, _UpperCAmelCase ):
    return fn(_UpperCAmelCase )
  else:
    print(type(_UpperCAmelCase ) )
    raise ValueError('Not supported' )





lowerCamelCase_					:						Optional[int]      = partial(tree_map, leaf_type=torch.Tensor)