

"""simple docstring"""




import unittest

from transformers.testing_utils import is_torch_available, require_torch, tooslow

from ...generation.test_utils import torch_device
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     CpmAntConfig,
     CpmAntForCausalLM,
     CpmAntModel,
     CpmAntTokenizer,
 )







@require_torch
class   __lowercase				:
 def __init__(							self				,							A_				,							A_=2				,							A_=8				,							A_=True				,							A_=False				,							A_=False				,							A_=False				,							A_=False				,							A_=99				,							A_=32				,							A_=3				,							A_=4				,							A_=37				,							A_=32				,							A_=128				,							A_=8				,							A_=8				,							A_=8				,							A_=1.0				,							A_=True				,							) ->int:



       '''simple docstring'''





       __lowerCAmelCase :						Optional[int] = parent
       __lowerCAmelCase :						Dict = batch_size
       __lowerCAmelCase :						Optional[int] = seq_length
       __lowerCAmelCase :						List[Any] = is_training
       __lowerCAmelCase :						Union[str, Any] = use_token_type_ids
       __lowerCAmelCase :						Tuple = use_input_mask
       __lowerCAmelCase :						List[Any] = use_labels
       __lowerCAmelCase :						Dict = use_mc_token_ids
       __lowerCAmelCase :						Dict = vocab_size
       __lowerCAmelCase :						List[Any] = hidden_size
       __lowerCAmelCase :						Tuple = num_hidden_layers
       __lowerCAmelCase :						Any = num_attention_heads
       __lowerCAmelCase :						str = intermediate_size
       __lowerCAmelCase :						Optional[Any] = num_buckets
       __lowerCAmelCase :						Optional[Any] = max_distance
       __lowerCAmelCase :						Tuple = prompt_length
       __lowerCAmelCase :						str = prompt_types
       __lowerCAmelCase :						List[Any] = segment_types
       __lowerCAmelCase :						Tuple = init_std
       __lowerCAmelCase :						int = return_dict
 def      UpperCamelCase__  (							self ) ->List[str]:



       '''simple docstring'''





       __lowerCAmelCase :						Tuple = {}
       __lowerCAmelCase :						Optional[Any] = ids_tensor([self.batch_size, self.seq_length]				,							self.vocab_size ).type(torch.intaa )
       __lowerCAmelCase :						Dict = False

       __lowerCAmelCase :						Dict = self.get_config()

       return (config, input_ids)
 def      UpperCamelCase__  (							self ) ->Dict:



       '''simple docstring'''





       return CpmAntConfig(
           vocab_size=self.vocab_size				,							hidden_size=self.hidden_size				,							num_hidden_layers=self.num_hidden_layers				,							num_attention_heads=self.num_attention_heads				,							dim_ff=self.intermediate_size				,							position_bias_num_buckets=self.num_buckets				,							position_bias_max_distance=self.max_distance				,							prompt_types=self.prompt_types				,							prompt_length=self.prompt_length				,							segment_types=self.segment_types				,							use_cache=A_				,							init_std=self.init_std				,							return_dict=self.return_dict				,							)
 def      UpperCamelCase__  (							self				,							A_				,							A_				,							*A_ ) ->Dict:



       '''simple docstring'''





       __lowerCAmelCase :						Dict = CpmAntModel(config=A_ )
       model.to(A_ )
       model.eval()

       __lowerCAmelCase :						List[str] = model(**A_ ).last_hidden_state

       self.parent.assertEqual(hidden_states.shape				,							(self.batch_size, self.seq_length, config.hidden_size) )
 def      UpperCamelCase__  (							self				,							A_				,							A_				,							*A_ ) ->Union[str, Any]:



       '''simple docstring'''





       __lowerCAmelCase :						Optional[int] = CpmAntForCausalLM(A_ )
       model.to(A_ )
       __lowerCAmelCase :						Optional[int] = input_ids['''input_ids'''].to(A_ )
       model.eval()

       __lowerCAmelCase :						int = model(**A_ )
       self.parent.assertEqual(
           model_output.logits.shape				,							(self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)				,							)





 def      UpperCamelCase__  (							self ) ->Dict:



       '''simple docstring'''





       __lowerCAmelCase :						Tuple = self.prepare_config_and_inputs()
       return config, inputs_dict







@require_torch
class   __lowercase				(_UpperCAmelCase		,       _UpperCAmelCase		,       unittest.TestCase			):
 _UpperCamelCase        =  (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
 _UpperCamelCase        =  (
     {"""feature-extraction""": CpmAntModel, """text-generation""": CpmAntForCausalLM} if is_torch_available() else {}
 )

 _UpperCamelCase        =  False
 _UpperCamelCase        =  False
 _UpperCamelCase        =  False
 _UpperCamelCase        =  False
 _UpperCamelCase        =  False
 def      UpperCamelCase__  (							self ) ->int:



       '''simple docstring'''





       __lowerCAmelCase :						Optional[int] = CpmAntModelTester(self )
       __lowerCAmelCase :						Optional[Any] = ConfigTester(self				,							config_class=A_ )
 def      UpperCamelCase__  (							self ) ->str:



       '''simple docstring'''





       self.config_tester.create_and_test_config_common_properties()
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()
 def      UpperCamelCase__  (							self ) ->Optional[Any]:



       '''simple docstring'''





       unittest.skip('''CPMAnt doesn\'t support input_embeds.''' )(self.test_inputs_embeds )
 def      UpperCamelCase__  (							self ) ->Any:



       '''simple docstring'''





       unittest.skip(
           '''CPMAnt doesn\'t support retain grad in hidden_states or attentions, because prompt management will peel off the output.hidden_states from graph.\
                 So is attentions. We strongly recommand you use loss to tune model.''' )(self.test_retain_grad_hidden_states_attentions )
 def      UpperCamelCase__  (							self ) ->Optional[Any]:



       '''simple docstring'''





       __lowerCAmelCase :						int = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_cpmant_model(A_				,							A_ )





 def      UpperCamelCase__  (							self ) ->Any:



       '''simple docstring'''





       __lowerCAmelCase :						Any = self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_lm_head_model(A_				,							A_ )







@require_torch
class   __lowercase				(unittest.TestCase			):
 @tooslow
 def      UpperCamelCase__  (							self ) ->Any:



       '''simple docstring'''





       __lowerCAmelCase :						Any = '''今天天气真好！'''
       __lowerCAmelCase :						Tuple = '''openbmb/cpm-ant-10b'''
       __lowerCAmelCase :						List[str] = CpmAntModel.from_pretrained(A_ )
       __lowerCAmelCase :						Any = CpmAntTokenizer.from_pretrained(A_ )
       __lowerCAmelCase :						Optional[int] = tokenizer(A_				,							return_tensors='''pt''' )
       __lowerCAmelCase :						Union[str, Any] = model(**A_ ).last_hidden_state

       __lowerCAmelCase :						Optional[Any] = torch.tensor(
           [[[6.1_708, 5.9_244, 1.0_835], [6.5_207, 6.2_893, -11.3_324], [-1.0_107, -0.0_576, -5.9_577]]]				,							)
       self.assertTrue(torch.allclose(hidden_states[:, :3, :3]				,							A_				,							atol=1e-2 ) )







@require_torch
class   __lowercase				(unittest.TestCase			):
 @tooslow
 def      UpperCamelCase__  (							self ) ->Optional[Any]:



       '''simple docstring'''





       __lowerCAmelCase :						str = '''今天天气真好！'''
       __lowerCAmelCase :						Any = '''openbmb/cpm-ant-10b'''
       __lowerCAmelCase :						Union[str, Any] = CpmAntForCausalLM.from_pretrained(A_ )
       __lowerCAmelCase :						List[str] = CpmAntTokenizer.from_pretrained(A_ )
       __lowerCAmelCase :						Any = tokenizer(A_				,							return_tensors='''pt''' )
       __lowerCAmelCase :						Union[str, Any] = model(**A_ ).logits

       __lowerCAmelCase :						Dict = torch.tensor(
           [[[-6.4_267, -6.4_083, -6.3_958], [-5.8_802, -5.9_447, -5.7_811], [-5.3_896, -5.4_820, -5.4_295]]]				,							)
       self.assertTrue(torch.allclose(hidden_states[:, :3, :3]				,							A_				,							atol=1e-2 ) )
 @tooslow
 def      UpperCamelCase__  (							self ) ->str:



       '''simple docstring'''





       __lowerCAmelCase :						Dict = '''openbmb/cpm-ant-10b'''
       __lowerCAmelCase :						List[Any] = CpmAntForCausalLM.from_pretrained(A_ )
       __lowerCAmelCase :						Union[str, Any] = CpmAntTokenizer.from_pretrained(A_ )
       __lowerCAmelCase :						str = '''今天天气不错，'''
       __lowerCAmelCase :						List[Any] = '''今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的'''
       __lowerCAmelCase :						Any = tokenizer(A_				,							return_tensors='''pt''' )
       __lowerCAmelCase :						Optional[Any] = model.generate(**A_ )
       __lowerCAmelCase :						Tuple = tokenizer.batch_decode(A_ )
       self.assertEqual(A_				,							A_ )





 @tooslow
 def      UpperCamelCase__  (							self ) ->Dict:



       '''simple docstring'''





       __lowerCAmelCase :						List[str] = '''openbmb/cpm-ant-10b'''
       __lowerCAmelCase :						Tuple = CpmAntForCausalLM.from_pretrained(A_ )
       __lowerCAmelCase :						str = CpmAntTokenizer.from_pretrained(A_ )
       __lowerCAmelCase :						List[Any] = ['''今天天气不错，''', '''新年快乐，万事如意！''']
       __lowerCAmelCase :						str = [
           '''今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的''',
           '''新年快乐，万事如意！在这辞旧迎新的美好时刻，我谨代表《农村新技术》杂志社全体同仁，向一直以来关心、支持《农村新技术》杂志发展的各级领导、各界朋友和广大读者致以最诚挚的''',
       ]
       __lowerCAmelCase :						Optional[Any] = tokenizer(A_				,							return_tensors='''pt'''				,							padding=A_ )
       __lowerCAmelCase :						Tuple = model.generate(**A_ )
       __lowerCAmelCase :						Optional[int] = tokenizer.batch_decode(A_ )
       self.assertEqual(A_				,							A_ )





import argparse
import copy
import json
import multiprocessing
import os
import time

from create_dummy_models import COMPOSITE_MODELS, create_tiny_models
from huggingface_hub import ModelFilter, hf_api

import transformers
from transformers import AutoFeatureExtractor, AutoImageProcessor, AutoTokenizer
from transformers.image_processing_utils import BaseImageProcessor







def 		_lowercase			(					):
      __lowerCAmelCase :						Union[str, Any] = set()
      # Each auto modeling files contains multiple mappings. Let's get them in a dynamic way.
      for module_name in ["modeling_auto", "modeling_tf_auto", "modeling_flax_auto"]:
            __lowerCAmelCase :						Any = getattr(transformers.models.auto       ,     lowercase__       ,     lowercase__						)
            if module is None:
                  continue
            # all mappings in a single auto modeling file
            __lowerCAmelCase :						Optional[int] = [
                x
                for x in dir(lowercase__						)
                if x.endswith('''_MAPPING_NAMES'''						)
                and (x.startswith('''MODEL_'''						) or x.startswith('''TF_MODEL_'''						) or x.startswith('''FLAX_MODEL_'''						))
            ]
            for name in mapping_names:
                  __lowerCAmelCase :						int = getattr(lowercase__       ,     lowercase__						)
                  if mapping is not None:
                        for v in mapping.values():
                              if isinstance(lowercase__       ,     (list, tuple)						):
                                    model_names.update(lowercase__						)
                              elif isinstance(lowercase__       ,     lowercase__						):
                                    model_names.add(lowercase__						)

      return sorted(lowercase__						)







def 		_lowercase			(					):
      # All model names defined in auto mappings
      __lowerCAmelCase :						int = set(get_all_model_names()						)

      with open('''tests/utils/tiny_model_summary.json'''						) as fp:
            __lowerCAmelCase :						Tuple = json.load(lowercase__						)
      __lowerCAmelCase :						Tuple = set()
      for model_base_name in tiny_model_info:
            tiny_models_names.update(tiny_model_info[model_base_name]['''model_classes''']						)

      # Remove a tiny model name if one of its framework implementation hasn't yet a tiny version on the Hub.
      __lowerCAmelCase :						List[Any] = model_names.difference(lowercase__						)
      for model_name in copy.copy(lowercase__						):
            if not model_name.startswith('''TF'''						) and f"""TF{model_name}""" in not_on_hub:
                  tiny_models_names.remove(lowercase__						)
            elif model_name.startswith('''TF'''						) and model_name[2:] in not_on_hub:
                  tiny_models_names.remove(lowercase__						)

      return sorted(lowercase__						)







def 		_lowercase			(					lowercase__						):
      __lowerCAmelCase :						Union[str, Any] = COMPOSITE_MODELS.values()

      # All tiny model base names on Hub
      __lowerCAmelCase :						Optional[Any] = get_all_model_names()
      __lowerCAmelCase :						str = hf_api.list_models(
          filter=ModelFilter(
              author='''hf-internal-testing'''       ,     )						)
      __lowerCAmelCase :						List[str] = set()
      for x in models:
            __lowerCAmelCase :						str = x.modelId
            __lowerCAmelCase,							__lowerCAmelCase :						List[Any] = model.split('''/'''						)
            if not model.startswith('''tiny-random-'''						):
                  continue
            __lowerCAmelCase :						Dict = model.replace('''tiny-random-'''       ,     ''''''						)
            if not model[0].isupper():
                  continue
            if model not in model_names and model not in special_models:
                  continue
            _models.add(lowercase__						)

      __lowerCAmelCase :						Optional[Any] = sorted(_models						)
      # All tiny model names on Hub
      __lowerCAmelCase :						List[str] = {}
      for model in models:
            __lowerCAmelCase :						int = f"""hf-internal-testing/tiny-random-{model}"""
            __lowerCAmelCase :						Dict = model.split('''-'''						)[0]
            try:
                  __lowerCAmelCase :						Optional[Any] = hf_api.repo_info(lowercase__						)
                  __lowerCAmelCase :						str = {
                      '''tokenizer_classes''': set(),
                      '''processor_classes''': set(),
                      '''model_classes''': set(),
                      '''sha''': repo_info.sha,
                  }
            except Exception:
                  continue
            try:
                  time.sleep(1						)
                  __lowerCAmelCase :						str = AutoTokenizer.from_pretrained(lowercase__						)
                  content["tokenizer_classes"].add(tokenizer_fast.__class__.__name__						)
            except Exception:
                  pass
            try:
                  time.sleep(1						)
                  __lowerCAmelCase :						Any = AutoTokenizer.from_pretrained(lowercase__       ,     use_fast=lowercase__						)
                  content["tokenizer_classes"].add(tokenizer_slow.__class__.__name__						)
            except Exception:
                  pass
            try:
                  time.sleep(1						)
                  __lowerCAmelCase :						str = AutoImageProcessor.from_pretrained(lowercase__						)
                  content["processor_classes"].add(img_p.__class__.__name__						)
            except Exception:
                  pass
            try:
                  time.sleep(1						)
                  __lowerCAmelCase :						int = AutoFeatureExtractor.from_pretrained(lowercase__						)
                  if not isinstance(lowercase__       ,     lowercase__						):
                        content["processor_classes"].add(feat_p.__class__.__name__						)
            except Exception:
                  pass
            try:
                  time.sleep(1						)
                  __lowerCAmelCase :						int = getattr(lowercase__       ,     lowercase__						)
                  __lowerCAmelCase :						str = model_class.from_pretrained(lowercase__						)
                  content["model_classes"].add(m.__class__.__name__						)
            except Exception:
                  pass
            try:
                  time.sleep(1						)
                  __lowerCAmelCase :						Optional[Any] = getattr(lowercase__       ,     f"""TF{model}"""						)
                  __lowerCAmelCase :						Any = model_class.from_pretrained(lowercase__						)
                  content["model_classes"].add(m.__class__.__name__						)
            except Exception:
                  pass

            __lowerCAmelCase :						Tuple = sorted(content['''tokenizer_classes''']						)
            __lowerCAmelCase :						List[Any] = sorted(content['''processor_classes''']						)
            __lowerCAmelCase :						int = sorted(content['''model_classes''']						)

            __lowerCAmelCase :						Union[str, Any] = content
            with open(os.path.join(lowercase__       ,     '''hub_tiny_model_summary.json'''						)       ,     '''w'''						) as fp:
                  json.dump(lowercase__       ,     lowercase__       ,     ensure_ascii=lowercase__       ,     indent=4						)


if __name__ == "__main__":
 _UpperCamelCase			      =		argparse.ArgumentParser()
 parser.add_argument("--num_workers", default=1, type=int, help="The number of workers to run.")
 _UpperCamelCase			      =		parser.parse_args()

 # This has to be `spawn` to avoid hanging forever!
 multiprocessing.set_start_method("spawn")

 _UpperCamelCase			      =		"tiny_models"
 _UpperCamelCase			      =		True
 _UpperCamelCase			      =		None
 _UpperCamelCase			      =		get_tiny_model_names_from_repo()
 _UpperCamelCase			      =		True
 _UpperCamelCase			      =		True
 _UpperCamelCase			      =		"hf-internal-testing"

 create_tiny_models(
     output_path,
     all,
     model_types,
     models_to_skip,
     no_check,
     upload,
     organization,
     token=os.environ.get("TOKEN", None),
     num_workers=args.num_workers,
 )


