






"""simple docstring"""


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 						__UpperCamelCase     :
					def __init__(							self							:	int     , UpperCAmelCase							:	Optional[int]     , UpperCAmelCase							:	List[str]=3     , UpperCAmelCase							:	Optional[Any]=32     , UpperCAmelCase							:	Optional[int]=3     , UpperCAmelCase							:	str=10     , UpperCAmelCase							:	List[str]=[10, 20, 30, 40]     , UpperCAmelCase							:	Optional[int]=[1, 1, 2, 1]     , UpperCAmelCase							:	Tuple=True     , UpperCAmelCase							:	Dict=True     , UpperCAmelCase							:	Optional[Any]="relu"     , UpperCAmelCase							:	Optional[int]=3     , UpperCAmelCase							:	Optional[int]=None     , )	->       Optional[int]:
											lowerCAmelCase   :List[Any]						=			parent
											lowerCAmelCase   :List[str]						=			batch_size
											lowerCAmelCase   :List[Any]						=			image_size
											lowerCAmelCase   :Optional[int]						=			num_channels
											lowerCAmelCase   :Union[str, Any]						=			embeddings_size
											lowerCAmelCase   :Tuple						=			hidden_sizes
											lowerCAmelCase   :Union[str, Any]						=			depths
											lowerCAmelCase   :List[str]						=			is_training
											lowerCAmelCase   :int						=			use_labels
											lowerCAmelCase   :Any						=			hidden_act
											lowerCAmelCase   :Dict						=			num_labels
											lowerCAmelCase   :Dict						=			scope
											lowerCAmelCase   :Optional[Any]						=			len(__lowerCamelCase   )
					def        UpperCAmelCase__ (							self							:	Dict   )	->       Any:
											lowerCAmelCase   :int						=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

											lowerCAmelCase   :List[str]						=			None
											if self.use_labels:
																	lowerCAmelCase   :List[Any]						=			ids_tensor([self.batch_size]     , self.num_labels   )

											lowerCAmelCase   :Union[str, Any]						=			self.get_config()
											return config, pixel_values, labels
					def        UpperCAmelCase__ (							self							:	List[Any]   )	->       Union[str, Any]:
											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        UpperCAmelCase__ (							self							:	Optional[int]     , UpperCAmelCase							:	str     , UpperCAmelCase							:	List[Any]     , UpperCAmelCase							:	List[str]   )	->       List[str]:
											lowerCAmelCase   :List[str]						=			TFRegNetModel(config=__lowerCamelCase   )
											lowerCAmelCase   :List[str]						=			model(__lowerCamelCase     , training=__lowerCamelCase   )
											# 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        UpperCAmelCase__ (							self							:	List[Any]     , UpperCAmelCase							:	Optional[int]     , UpperCAmelCase							:	Union[str, Any]     , UpperCAmelCase							:	str   )	->       Tuple:
											lowerCAmelCase   :Union[str, Any]						=			self.num_labels
											lowerCAmelCase   :Any						=			TFRegNetForImageClassification(__lowerCamelCase   )
											lowerCAmelCase   :Dict						=			model(__lowerCamelCase     , labels=__lowerCamelCase     , training=__lowerCamelCase   )
											self.parent.assertEqual(result.logits.shape     , (self.batch_size, self.num_labels)   )
					def        UpperCAmelCase__ (							self							:	Dict   )	->       Any:
											lowerCAmelCase   :Dict						=			self.prepare_config_and_inputs()
											lowerCAmelCase	, lowerCAmelCase	, lowerCAmelCase   :Tuple						=			config_and_inputs
											lowerCAmelCase   :str						=			{'pixel_values': pixel_values}
											return config, inputs_dict




@require_tf
class 						__UpperCamelCase     (     __SCREAMING_SNAKE_CASE   ,     __SCREAMING_SNAKE_CASE   ,     unittest.TestCase						):
					lowercase_    :				Dict      =  (TFRegNetModel, TFRegNetForImageClassification) if is_tf_available() else ()
					lowercase_    :				int      =  (
					    {'''feature-extraction''': TFRegNetModel, '''image-classification''': TFRegNetForImageClassification}
					    if is_tf_available()
					    else {}
					)

					lowercase_    :				Optional[Any]      =  False
					lowercase_    :				Optional[Any]      =  False
					lowercase_    :				Union[str, Any]      =  False
					lowercase_    :				Optional[int]      =  False
					lowercase_    :				List[Any]      =  False
					def        UpperCAmelCase__ (							self							:	int   )	->       str:
											lowerCAmelCase   :int						=			TFRegNetModelTester(self   )
											lowerCAmelCase   :List[Any]						=			ConfigTester(self     , config_class=__lowerCamelCase     , has_text_modality=__lowerCamelCase   )
					def        UpperCAmelCase__ (							self							:	List[str]   )	->       Any:
											return
					@unittest.skip(reason='RegNet does not use inputs_embeds'   )
					def        UpperCAmelCase__ (							self							:	List[Any]   )	->       List[Any]:
											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        UpperCAmelCase__ (							self							:	Union[str, Any]   )	->       Optional[int]:
											super().test_keras_fit()
					@unittest.skip(reason='RegNet does not support input and output embeddings'   )
					def        UpperCAmelCase__ (							self							:	str   )	->       Dict:
											pass
					def        UpperCAmelCase__ (							self							:	int   )	->       List[str]:
											lowerCAmelCase	, lowerCAmelCase   :Union[str, Any]						=			self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowerCAmelCase   :Optional[int]						=			['pixel_values']
																	self.assertListEqual(arg_names[:1]     , __lowerCamelCase   )
					def        UpperCAmelCase__ (							self							:	Optional[int]   )	->       List[Any]:
											lowerCAmelCase   :str						=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__lowerCamelCase   )
					def        UpperCAmelCase__ (							self							:	Tuple   )	->       Union[str, Any]:
											def check_hidden_states_output(UpperCAmelCase							:	Any     , UpperCAmelCase							:	List[Any]     , UpperCAmelCase							:	Tuple   ):
																	lowerCAmelCase   :List[Any]						=			model_class(__lowerCamelCase   )
																	lowerCAmelCase   :Tuple						=			model(**self._prepare_for_class(__lowerCamelCase     , __lowerCamelCase   )     , training=__lowerCamelCase   )

																	lowerCAmelCase   :List[str]						=			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																	lowerCAmelCase   :Optional[int]						=			self.model_tester.num_stages
																	self.assertEqual(len(__lowerCamelCase   )     , 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]     , )

											lowerCAmelCase	, lowerCAmelCase   :str						=			self.model_tester.prepare_config_and_inputs_for_common()
											lowerCAmelCase   :Tuple						=			['basic', 'bottleneck']
											for model_class in self.all_model_classes:
																	for layer_type in layers_type:
																							lowerCAmelCase   :Union[str, Any]						=			layer_type
																							lowerCAmelCase   :Any						=			True
																							check_hidden_states_output(__lowerCamelCase     , __lowerCamelCase     , __lowerCamelCase   )

																							# check that output_hidden_states also work using config
																							del inputs_dict["output_hidden_states"]
																							lowerCAmelCase   :Union[str, Any]						=			True

																							check_hidden_states_output(__lowerCamelCase     , __lowerCamelCase     , __lowerCamelCase   )
					def        UpperCAmelCase__ (							self							:	Dict   )	->       int:
											lowerCAmelCase	, lowerCAmelCase   :Any						=			self.model_tester.prepare_config_and_inputs_for_common()

											def check_equivalence(UpperCAmelCase							:	List[str]     , UpperCAmelCase							:	str     , UpperCAmelCase							:	Union[str, Any]     , UpperCAmelCase							:	Optional[Any]={}   ):
																	lowerCAmelCase   :Optional[Any]						=			model(__lowerCamelCase     , return_dict=__lowerCamelCase     , **__lowerCamelCase   )
																	lowerCAmelCase   :List[str]						=			model(__lowerCamelCase     , return_dict=__lowerCamelCase     , **__lowerCamelCase   ).to_tuple()

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

																							recursive_check(__lowerCamelCase     , __lowerCamelCase   )

											for model_class in self.all_model_classes:
																	lowerCAmelCase   :str						=			model_class(__lowerCamelCase   )

																	lowerCAmelCase   :Any						=			self._prepare_for_class(__lowerCamelCase     , __lowerCamelCase   )
																	lowerCAmelCase   :Optional[int]						=			self._prepare_for_class(__lowerCamelCase     , __lowerCamelCase   )
																	check_equivalence(__lowerCamelCase     , __lowerCamelCase     , __lowerCamelCase   )

																	lowerCAmelCase   :Union[str, Any]						=			self._prepare_for_class(__lowerCamelCase     , __lowerCamelCase     , return_labels=__lowerCamelCase   )
																	lowerCAmelCase   :Dict						=			self._prepare_for_class(__lowerCamelCase     , __lowerCamelCase     , return_labels=__lowerCamelCase   )
																	check_equivalence(__lowerCamelCase     , __lowerCamelCase     , __lowerCamelCase   )

																	lowerCAmelCase   :Tuple						=			self._prepare_for_class(__lowerCamelCase     , __lowerCamelCase   )
																	lowerCAmelCase   :str						=			self._prepare_for_class(__lowerCamelCase     , __lowerCamelCase   )
																	check_equivalence(__lowerCamelCase     , __lowerCamelCase     , __lowerCamelCase     , {'output_hidden_states': True}   )

																	lowerCAmelCase   :List[str]						=			self._prepare_for_class(__lowerCamelCase     , __lowerCamelCase     , return_labels=__lowerCamelCase   )
																	lowerCAmelCase   :str						=			self._prepare_for_class(__lowerCamelCase     , __lowerCamelCase     , return_labels=__lowerCamelCase   )
																	check_equivalence(__lowerCamelCase     , __lowerCamelCase     , __lowerCamelCase     , {'output_hidden_states': True}   )
					def        UpperCAmelCase__ (							self							:	Tuple   )	->       Optional[Any]:
											lowerCAmelCase   :str						=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase   )
					@slow
					def        UpperCAmelCase__ (							self							:	Any   )	->       Any:
											for model_name in TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowerCAmelCase   :Union[str, Any]						=			TFRegNetModel.from_pretrained(__lowerCamelCase   )
																	self.assertIsNotNone(__lowerCamelCase   )


def        UpperCAmelCase      (					):





						'''simple docstring'''






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







@require_tf
@require_vision
class 						__UpperCamelCase     (     unittest.TestCase						):
					@cached_property
					def        UpperCAmelCase__ (							self							:	str   )	->       str:
											return (
											    AutoImageProcessor.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )
											    if is_vision_available()
											    else None
											)
					@slow
					def        UpperCAmelCase__ (							self							:	Tuple   )	->       Optional[int]:
											lowerCAmelCase   :List[Any]						=			TFRegNetForImageClassification.from_pretrained(TF_REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )

											lowerCAmelCase   :Union[str, Any]						=			self.default_image_processor
											lowerCAmelCase   :Union[str, Any]						=			prepare_img()
											lowerCAmelCase   :Dict						=			image_processor(images=__lowerCamelCase     , return_tensors='tf'   )

											# forward pass
											lowerCAmelCase   :List[Any]						=			model(**__lowerCamelCase     , training=__lowerCamelCase   )

											# verify the logits
											lowerCAmelCase   :Tuple						=			tf.TensorShape((1, 1000)   )
											self.assertEqual(outputs.logits.shape     , __lowerCamelCase   )

											lowerCAmelCase   :Optional[Any]						=			tf.constant([-0.4_1_8_0, -1.5_0_5_1, -3.4_8_3_6]   )

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




"""simple docstring"""






import collections
import os
from typing import List, Optional, Tuple

from transformers.utils import is_jieba_available, requires_backends


if is_jieba_available():
     import jieba

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


snake_case   					=				logging.get_logger(__name__)

snake_case   					=				{'''vocab_file''': '''vocab.txt'''}

snake_case   					=				{
    '''vocab_file''': {
        '''openbmb/cpm-ant-10b''': '''https://huggingface.co/openbmb/cpm-ant-10b/blob/main/vocab.txt''',
    },
}

snake_case   					=				{
    '''openbmb/cpm-ant-10b''': 1_0_2_4,
}




def 			snake_case		(		lowerCAmelCase_	)						->				int:
  _snake_case   	=      collections.OrderedDict()
  with open(lowerCAmelCase_  ,						'''r'''  ,						encoding='''utf-8'''	) as reader:
    _snake_case   	=      reader.readlines()
  for index, token in enumerate(lowerCAmelCase_	):
    _snake_case   	=      token.rstrip('''\n'''	)
    _snake_case   	=      index
  return vocab



class     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE  ):



   def __init__(       self    :	Union[str, Any]							,       __lowerCamelCase    :	int							,       __lowerCamelCase    :	List[str]="<unk>"							,       __lowerCamelCase    :	Tuple=2_0_0				):



     """simple docstring"""
     _snake_case   	=      vocab
     _snake_case   	=      unk_token
     _snake_case   	=      max_input_chars_per_word





   def    __UpperCAmelCase    (       self    :	Any							,       __lowerCamelCase    :	str				):



     """simple docstring"""
     _snake_case   	=      list(__lowerCamelCase				)
     if len(__lowerCamelCase				) > self.max_input_chars_per_word:
       return [self.unk_token]

     _snake_case   	=      0
     _snake_case   	=      []
     while start < len(__lowerCamelCase				):
       _snake_case   	=      len(__lowerCamelCase				)
       _snake_case   	=      None
       while start < end:
         _snake_case   	=      ''''''.join(chars[start:end]				)
         if substr in self.vocab:
           _snake_case   	=      substr
           break
         end -= 1
       if cur_substr is None:
         sub_tokens.append(self.unk_token				)
         start += 1
       else:
         sub_tokens.append(__lowerCamelCase				)
         _snake_case   	=      end

     return sub_tokens



class     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE  ):
   A__		:	List[str]           =  VOCAB_FILES_NAMES
   A__		:	str           =  PRETRAINED_VOCAB_FILES_MAP
   A__		:	str           =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   A__		:	Union[str, Any]           =  ['''input_ids''', '''attention_mask''']
   A__		:	Optional[int]           =  False



   def __init__(       self    :	Dict							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	str="<d>"							,       __lowerCamelCase    :	Tuple="</d>"							,       __lowerCamelCase    :	Tuple="<s>"							,       __lowerCamelCase    :	int="</s>"							,       __lowerCamelCase    :	List[str]="<pad>"							,       __lowerCamelCase    :	int="<unk>"							,       __lowerCamelCase    :	int="</n>"							,       __lowerCamelCase    :	Tuple="</_>"							,       __lowerCamelCase    :	Optional[Any]="left"							,       **__lowerCamelCase    :	str							,       ):



     """simple docstring"""
     requires_backends(self							,       ['''jieba''']				)
     super().__init__(
         bod_token=__lowerCamelCase							,       eod_token=__lowerCamelCase							,       bos_token=__lowerCamelCase							,       eos_token=__lowerCamelCase							,       pad_token=__lowerCamelCase							,       unk_token=__lowerCamelCase							,       line_token=__lowerCamelCase							,       space_token=__lowerCamelCase							,       padding_side=__lowerCamelCase							,       **__lowerCamelCase							,       )
     _snake_case   	=      bod_token
     _snake_case   	=      eod_token
     _snake_case   	=      load_vocab(__lowerCamelCase				)
     _snake_case   	=      self.encoder[space_token]
     _snake_case   	=      self.encoder[line_token]

     del self.encoder[space_token]
     del self.encoder[line_token]

     _snake_case   	=      collections.OrderedDict(sorted(self.encoder.items()							,       key=lambda __lowerCamelCase				: x[1]				)				)
     _snake_case   	=      {v: k for k, v in self.encoder.items()}

     _snake_case   	=      WordpieceTokenizer(vocab=self.encoder							,       unk_token=self.unk_token				)



   @property
   def    __UpperCAmelCase    (       self    :	List[Any]				):



     """simple docstring"""
     return self.encoder[self.bod_token]



   @property
   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     return self.encoder[self.eod_token]



   @property
   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



     """simple docstring"""
     return self.encoder["\n"]



   @property
   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



     """simple docstring"""
     return len(self.encoder				)



   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     return dict(self.encoder							,       **self.added_tokens_encoder				)



   def    __UpperCAmelCase    (       self    :	Optional[Any]							,       __lowerCamelCase    :	Optional[int]				):



     """simple docstring"""
     _snake_case   	=      []
     for x in jieba.cut(__lowerCamelCase							,       cut_all=__lowerCamelCase				):
       output_tokens.extend(self.wordpiece_tokenizer.tokenize(__lowerCamelCase				)				)
     return output_tokens



   def    __UpperCAmelCase    (       self    :	Optional[Any]							,       __lowerCamelCase    :	Union[str, Any]							,       **__lowerCamelCase    :	Optional[Any]				):



     """simple docstring"""
     _snake_case   	=      [i for i in token_ids if i >= 0]
     _snake_case   	=      [
         x for x in token_ids if x != self.pad_token_id and x != self.eos_token_id and x != self.bos_token_id
     ]
     return super()._decode(__lowerCamelCase							,       **__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Dict							,       __lowerCamelCase    :	Union[str, Any]				):



     """simple docstring"""
     return token in self.encoder



   def    __UpperCAmelCase    (       self    :	Tuple							,       __lowerCamelCase    :	List[str]				):



     """simple docstring"""
     return "".join(__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Dict							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     return self.encoder.get(__lowerCamelCase							,       self.encoder.get(self.unk_token				)				)



   def    __UpperCAmelCase    (       self    :	Optional[Any]							,       __lowerCamelCase    :	int				):



     """simple docstring"""
     return self.decoder.get(__lowerCamelCase							,       self.unk_token				)



   def    __UpperCAmelCase    (       self    :	Tuple							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Optional[str] = None				):



     """simple docstring"""
     if os.path.isdir(__lowerCamelCase				):
       _snake_case   	=      os.path.join(
           __lowerCamelCase							,       (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']				)
     else:
       _snake_case   	=      (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory
     _snake_case   	=      0
     if " " in self.encoder:
       _snake_case   	=      self.encoder[''' ''']
       del self.encoder[" "]
     if "\n" in self.encoder:
       _snake_case   	=      self.encoder['''\n''']
       del self.encoder["\n"]
     _snake_case   	=      collections.OrderedDict(sorted(self.encoder.items()							,       key=lambda __lowerCamelCase				: x[1]				)				)
     with open(__lowerCamelCase							,       '''w'''							,       encoding='''utf-8'''				) as writer:
       for token, token_index in self.encoder.items():
         if index != token_index:
           logger.warning(
               f"""Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."""
               ''' Please check that the vocabulary is not corrupted!'''				)
           _snake_case   	=      token_index
         writer.write(token + '''\n'''				)
         index += 1
     return (vocab_file,)



   def    __UpperCAmelCase    (       self    :	List[str]							,       __lowerCamelCase    :	List[int]							,       __lowerCamelCase    :	List[int] = None				):



     """simple docstring"""
     if token_ids_a is None:
       return [self.bos_token_id] + token_ids_a
     return [self.bos_token_id] + token_ids_a + [self.bos_token_id] + token_ids_a





   def    __UpperCAmelCase    (       self    :	str							,       __lowerCamelCase    :	List[int]							,       __lowerCamelCase    :	Optional[List[int]] = None							,       __lowerCamelCase    :	bool = False				):



     """simple docstring"""

     if already_has_special_tokens:
       return super().get_special_tokens_mask(
           token_ids_a=__lowerCamelCase							,       token_ids_a=__lowerCamelCase							,       already_has_special_tokens=__lowerCamelCase				)

     if token_ids_a is not None:
       return [1] + ([0] * len(__lowerCamelCase				)) + [1] + ([0] * len(__lowerCamelCase				))
     return [1] + ([0] * len(__lowerCamelCase				))





