
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 	UpperCAmelCase_      :


  '''simple docstring'''







  def __init__(			self      :			Optional[Any]      ,							UpperCamelCase__      :			List[str]      ,							UpperCamelCase__      :			Optional[int]=2      ,							UpperCamelCase__      :			Any=8      ,							UpperCamelCase__      :			Dict=True      ,							UpperCamelCase__      :			Dict=False      ,							UpperCamelCase__      :			Optional[int]=False      ,							UpperCamelCase__      :			Dict=False      ,							UpperCamelCase__      :			Union[str, Any]=False      ,							UpperCamelCase__      :			List[str]=99      ,							UpperCamelCase__      :			Any=32      ,							UpperCamelCase__      :			Union[str, Any]=3      ,							UpperCamelCase__      :			int=4      ,							UpperCamelCase__      :			str=37      ,							UpperCamelCase__      :			Optional[Any]=32      ,							UpperCamelCase__      :			Tuple=128      ,							UpperCamelCase__      :			Optional[Any]=8      ,							UpperCamelCase__      :			List[Any]=8      ,							UpperCamelCase__      :			str=8      ,							UpperCamelCase__      :			Optional[int]=1.0      ,							UpperCamelCase__      :			Tuple=True      ,							)      ->     Any:







    """simple docstring"""






    __magic_name__    					=	parent
    __magic_name__    					=	batch_size
    __magic_name__    					=	seq_length
    __magic_name__    					=	is_training
    __magic_name__    					=	use_token_type_ids
    __magic_name__    					=	use_input_mask
    __magic_name__    					=	use_labels
    __magic_name__    					=	use_mc_token_ids
    __magic_name__    					=	vocab_size
    __magic_name__    					=	hidden_size
    __magic_name__    					=	num_hidden_layers
    __magic_name__    					=	num_attention_heads
    __magic_name__    					=	intermediate_size
    __magic_name__    					=	num_buckets
    __magic_name__    					=	max_distance
    __magic_name__    					=	prompt_length
    __magic_name__    					=	prompt_types
    __magic_name__    					=	segment_types
    __magic_name__    					=	init_std
    __magic_name__    					=	return_dict
  def 							_lowercase							(			self      :			Any   )      ->     Tuple:







    """simple docstring"""






    __magic_name__    					=	{}
    __magic_name__    					=	ids_tensor([self.batch_size, self.seq_length]      ,							self.vocab_size   ).type(torch.intaa   )
    __magic_name__    					=	False

    __magic_name__    					=	self.get_config()

    return (config, input_ids)
  def 							_lowercase							(			self      :			str   )      ->     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=UpperCamelCase__      ,							init_std=self.init_std      ,							return_dict=self.return_dict      ,							)
  def 							_lowercase							(			self      :			List[Any]      ,							UpperCamelCase__      :			Optional[Any]      ,							UpperCamelCase__      :			List[str]      ,							*UpperCamelCase__      :			Tuple   )      ->     Dict:







    """simple docstring"""






    __magic_name__    					=	CpmAntModel(config=UpperCamelCase__   )
    model.to(UpperCamelCase__   )
    model.eval()

    __magic_name__    					=	model(**UpperCamelCase__   ).last_hidden_state

    self.parent.assertEqual(hidden_states.shape      ,							(self.batch_size, self.seq_length, config.hidden_size)   )
  def 							_lowercase							(			self      :			int      ,							UpperCamelCase__      :			int      ,							UpperCamelCase__      :			str      ,							*UpperCamelCase__      :			List[str]   )      ->     int:







    """simple docstring"""






    __magic_name__    					=	CpmAntForCausalLM(UpperCamelCase__   )
    model.to(UpperCamelCase__   )
    __magic_name__    					=	input_ids["""input_ids"""].to(UpperCamelCase__   )
    model.eval()

    __magic_name__    					=	model(**UpperCamelCase__   )
    self.parent.assertEqual(
        model_output.logits.shape      ,							(self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)      ,							)





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







    """simple docstring"""






    __magic_name__					,   __magic_name__    					=	self.prepare_config_and_inputs()
    return config, inputs_dict







@require_torch
class 	UpperCAmelCase_      (			_A  ,	_A  ,	unittest.TestCase					):


  '''simple docstring'''







  a__   	=		(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
  a__   	=		(
      {"""feature-extraction""": CpmAntModel, """text-generation""": CpmAntForCausalLM} if is_torch_available() else {}
  )

  a__   	=		False
  a__   	=		False
  a__   	=		False
  a__   	=		False
  a__   	=		False
  def 							_lowercase							(			self      :			List[str]   )      ->     Any:







    """simple docstring"""






    __magic_name__    					=	CpmAntModelTester(self   )
    __magic_name__    					=	ConfigTester(self      ,							config_class=UpperCamelCase__   )
  def 							_lowercase							(			self      :			Tuple   )      ->     Any:







    """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 							_lowercase							(			self      :			str   )      ->     str:







    """simple docstring"""






    unittest.skip("""CPMAnt doesn't support input_embeds."""   )(self.test_inputs_embeds   )
  def 							_lowercase							(			self      :			Optional[int]   )      ->     Union[str, 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 							_lowercase							(			self      :			Any   )      ->     Tuple:







    """simple docstring"""






    __magic_name__					,   __magic_name__    					=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_cpmant_model(UpperCamelCase__      ,							UpperCamelCase__   )





  def 							_lowercase							(			self      :			Optional[Any]   )      ->     List[Any]:







    """simple docstring"""






    __magic_name__					,   __magic_name__    					=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_lm_head_model(UpperCamelCase__      ,							UpperCamelCase__   )







@require_torch
class 	UpperCAmelCase_      (			unittest.TestCase					):


  '''simple docstring'''







  @tooslow
  def 							_lowercase							(			self      :			Any   )      ->     int:







    """simple docstring"""






    __magic_name__    					=	"""今天天气真好！"""
    __magic_name__    					=	"""openbmb/cpm-ant-10b"""
    __magic_name__    					=	CpmAntModel.from_pretrained(UpperCamelCase__   )
    __magic_name__    					=	CpmAntTokenizer.from_pretrained(UpperCamelCase__   )
    __magic_name__    					=	tokenizer(UpperCamelCase__      ,							return_tensors="""pt"""   )
    __magic_name__    					=	model(**UpperCamelCase__   ).last_hidden_state

    __magic_name__    					=	torch.tensor(
        [[[6.1708, 5.9244, 1.0835], [6.5207, 6.2893, -11.3324], [-1.0107, -0.0576, -5.9577]]]      ,							)
    self.assertTrue(torch.allclose(hidden_states[:, :3, :3]      ,							UpperCamelCase__      ,							atol=1E-2   )   )







@require_torch
class 	UpperCAmelCase_      (			unittest.TestCase					):


  '''simple docstring'''







  @tooslow
  def 							_lowercase							(			self      :			Any   )      ->     List[str]:







    """simple docstring"""






    __magic_name__    					=	"""今天天气真好！"""
    __magic_name__    					=	"""openbmb/cpm-ant-10b"""
    __magic_name__    					=	CpmAntForCausalLM.from_pretrained(UpperCamelCase__   )
    __magic_name__    					=	CpmAntTokenizer.from_pretrained(UpperCamelCase__   )
    __magic_name__    					=	tokenizer(UpperCamelCase__      ,							return_tensors="""pt"""   )
    __magic_name__    					=	model(**UpperCamelCase__   ).logits

    __magic_name__    					=	torch.tensor(
        [[[-6.4267, -6.4083, -6.3958], [-5.8802, -5.9447, -5.7811], [-5.3896, -5.4820, -5.4295]]]      ,							)
    self.assertTrue(torch.allclose(hidden_states[:, :3, :3]      ,							UpperCamelCase__      ,							atol=1E-2   )   )
  @tooslow
  def 							_lowercase							(			self      :			int   )      ->     List[str]:







    """simple docstring"""






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





  @tooslow
  def 							_lowercase							(			self      :			Dict   )      ->     Optional[Any]:







    """simple docstring"""






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



def a__   (	A_ ):






  '''simple docstring'''





  __magic_name__    					=	str(A_ ).strip()
  if not bin_string:
    raise ValueError("""Empty string was passed to the function""" )
  __magic_name__    					=	bin_string[0] == """-"""
  if is_negative:
    __magic_name__    					=	bin_string[1:]
  if not all(char in """01""" for char in bin_string ):
    raise ValueError("""Non-binary value was passed to the function""" )
  __magic_name__    					=	0
  for char in bin_string:
    __magic_name__    					=	2 * decimal_number + int(A_ )
  return -decimal_number if is_negative else decimal_number


if __name__ == "__main__":
    from doctest import testmod

    testmod()

