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					:


 '''simple docstring'''






 def __init__(				self       :     Dict				,	SCREAMING_SNAKE_CASE__       :     List[Any]				,	SCREAMING_SNAKE_CASE__       :     List[str]=2				,	SCREAMING_SNAKE_CASE__       :     int=8				,	SCREAMING_SNAKE_CASE__       :     Any=True				,	SCREAMING_SNAKE_CASE__       :     Tuple=False				,	SCREAMING_SNAKE_CASE__       :     Optional[Any]=False				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=False				,	SCREAMING_SNAKE_CASE__       :     List[str]=False				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=99				,	SCREAMING_SNAKE_CASE__       :     Any=32				,	SCREAMING_SNAKE_CASE__       :     Tuple=3				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=4				,	SCREAMING_SNAKE_CASE__       :     str=37				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=32				,	SCREAMING_SNAKE_CASE__       :     Any=1_28				,	SCREAMING_SNAKE_CASE__       :     List[str]=8				,	SCREAMING_SNAKE_CASE__       :     Dict=8				,	SCREAMING_SNAKE_CASE__       :     str=8				,	SCREAMING_SNAKE_CASE__       :     str=1.0				,	SCREAMING_SNAKE_CASE__       :     Optional[int]=True				,	)  ->   Optional[int]:
  __lowerCAmelCase    							=						parent
  __lowerCAmelCase    							=						batch_size
  __lowerCAmelCase    							=						seq_length
  __lowerCAmelCase    							=						is_training
  __lowerCAmelCase    							=						use_token_type_ids
  __lowerCAmelCase    							=						use_input_mask
  __lowerCAmelCase    							=						use_labels
  __lowerCAmelCase    							=						use_mc_token_ids
  __lowerCAmelCase    							=						vocab_size
  __lowerCAmelCase    							=						hidden_size
  __lowerCAmelCase    							=						num_hidden_layers
  __lowerCAmelCase    							=						num_attention_heads
  __lowerCAmelCase    							=						intermediate_size
  __lowerCAmelCase    							=						num_buckets
  __lowerCAmelCase    							=						max_distance
  __lowerCAmelCase    							=						prompt_length
  __lowerCAmelCase    							=						prompt_types
  __lowerCAmelCase    							=						segment_types
  __lowerCAmelCase    							=						init_std
  __lowerCAmelCase    							=						return_dict
 def a (				self       :     Dict							)  ->   Dict:
  __lowerCAmelCase    							=						{}
  __lowerCAmelCase    							=						ids_tensor([self.batch_size, self.seq_length]				,	self.vocab_size							).type(torch.intaa							)
  __lowerCAmelCase    							=						False

  __lowerCAmelCase    							=						self.get_config()

  return (config, input_ids)
 def a (				self       :     Dict							)  ->   List[Any]:
  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=SCREAMING_SNAKE_CASE__				,	init_std=self.init_std				,	return_dict=self.return_dict				,	)
 def a (				self       :     Tuple				,	SCREAMING_SNAKE_CASE__       :     List[str]				,	SCREAMING_SNAKE_CASE__       :     int				,	*SCREAMING_SNAKE_CASE__       :     List[Any]							)  ->   Tuple:
  __lowerCAmelCase    							=						CpmAntModel(config=SCREAMING_SNAKE_CASE__							)
  model.to(SCREAMING_SNAKE_CASE__							)
  model.eval()

  __lowerCAmelCase    							=						model(**SCREAMING_SNAKE_CASE__							).last_hidden_state

  self.parent.assertEqual(hidden_states.shape				,	(self.batch_size, self.seq_length, config.hidden_size)							)
 def a (				self       :     Union[str, Any]				,	SCREAMING_SNAKE_CASE__       :     List[Any]				,	SCREAMING_SNAKE_CASE__       :     int				,	*SCREAMING_SNAKE_CASE__       :     List[Any]							)  ->   List[Any]:
  __lowerCAmelCase    							=						CpmAntForCausalLM(SCREAMING_SNAKE_CASE__							)
  model.to(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						input_ids["""input_ids"""].to(SCREAMING_SNAKE_CASE__							)
  model.eval()

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





 def a (				self       :     Tuple							)  ->   List[str]:
  __lowerCAmelCase							,	__lowerCAmelCase    							=						self.prepare_config_and_inputs()
  return config, inputs_dict







@require_torch
class     _lowercase					(    UpperCAmelCase__  ,							UpperCAmelCase__  ,							unittest.TestCase      ):


 '''simple docstring'''






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

 _SCREAMING_SNAKE_CASE					:	Optional[Any]				     = False
 _SCREAMING_SNAKE_CASE					:	Dict				     = False
 _SCREAMING_SNAKE_CASE					:	Optional[int]				     = False
 _SCREAMING_SNAKE_CASE					:	List[Any]				     = False
 _SCREAMING_SNAKE_CASE					:	List[str]				     = False
 def a (				self       :     Dict							)  ->   Optional[Any]:
  __lowerCAmelCase    							=						CpmAntModelTester(self							)
  __lowerCAmelCase    							=						ConfigTester(self				,	config_class=SCREAMING_SNAKE_CASE__							)
 def a (				self       :     Dict							)  ->   Union[str, Any]:
  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 a (				self       :     Optional[int]							)  ->   List[str]:
  unittest.skip("""CPMAnt doesn't support input_embeds."""							)(self.test_inputs_embeds							)
 def a (				self       :     Tuple							)  ->   List[Any]:
  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 a (				self       :     Tuple							)  ->   Optional[Any]:
  __lowerCAmelCase							,	__lowerCAmelCase    							=						self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_cpmant_model(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)





 def a (				self       :     int							)  ->   Dict:
  __lowerCAmelCase							,	__lowerCAmelCase    							=						self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_lm_head_model(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)







@require_torch
class     _lowercase					(    unittest.TestCase      ):


 '''simple docstring'''






 @tooslow
 def a (				self       :     Tuple							)  ->   Optional[int]:
  __lowerCAmelCase    							=						"""今天天气真好！"""
  __lowerCAmelCase    							=						"""openbmb/cpm-ant-10b"""
  __lowerCAmelCase    							=						CpmAntModel.from_pretrained(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						CpmAntTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						tokenizer(SCREAMING_SNAKE_CASE__				,	return_tensors="""pt"""							)
  __lowerCAmelCase    							=						model(**SCREAMING_SNAKE_CASE__							).last_hidden_state

  __lowerCAmelCase    							=						torch.tensor(
      [[[6.1_7_0_8, 5.9_2_4_4, 1.0_8_3_5], [6.5_2_0_7, 6.2_8_9_3, -11.33_24], [-1.0_1_0_7, -0.0_5_7_6, -5.9_5_7_7]]]				,	)
  self.assertTrue(torch.allclose(hidden_states[:, :3, :3]				,	SCREAMING_SNAKE_CASE__				,	atol=1e-2							)							)







@require_torch
class     _lowercase					(    unittest.TestCase      ):


 '''simple docstring'''






 @tooslow
 def a (				self       :     Any							)  ->   Optional[Any]:
  __lowerCAmelCase    							=						"""今天天气真好！"""
  __lowerCAmelCase    							=						"""openbmb/cpm-ant-10b"""
  __lowerCAmelCase    							=						CpmAntForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						CpmAntTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						tokenizer(SCREAMING_SNAKE_CASE__				,	return_tensors="""pt"""							)
  __lowerCAmelCase    							=						model(**SCREAMING_SNAKE_CASE__							).logits

  __lowerCAmelCase    							=						torch.tensor(
      [[[-6.4_2_6_7, -6.4_0_8_3, -6.3_9_5_8], [-5.8_8_0_2, -5.9_4_4_7, -5.7_8_1_1], [-5.3_8_9_6, -5.4_8_2_0, -5.4_2_9_5]]]				,	)
  self.assertTrue(torch.allclose(hidden_states[:, :3, :3]				,	SCREAMING_SNAKE_CASE__				,	atol=1e-2							)							)
 @tooslow
 def a (				self       :     List[Any]							)  ->   int:
  __lowerCAmelCase    							=						"""openbmb/cpm-ant-10b"""
  __lowerCAmelCase    							=						CpmAntForCausalLM.from_pretrained(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						CpmAntTokenizer.from_pretrained(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						"""今天天气不错，"""
  __lowerCAmelCase    							=						"""今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的"""
  __lowerCAmelCase    							=						tokenizer(SCREAMING_SNAKE_CASE__				,	return_tensors="""pt"""							)
  __lowerCAmelCase    							=						model.generate(**SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase    							=						tokenizer.batch_decode(SCREAMING_SNAKE_CASE__							)
  self.assertEqual(SCREAMING_SNAKE_CASE__				,	SCREAMING_SNAKE_CASE__							)





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







'''simple docstring'''

import argparse

from transformers import CanineConfig, CanineModel, CanineTokenizer, load_tf_weights_in_canine
from transformers.utils import logging


logging.set_verbosity_info()







def 					UpperCamelCase_						( snake_case_	:	List[str]						,		snake_case_	:	Optional[Any]				) ->					Tuple:






 '''simple docstring'''

 __lowerCAmelCase    							=						CanineConfig()
 __lowerCAmelCase    							=						CanineModel(snake_case_				)
 model.eval()

 print(f"""Building PyTorch model from configuration: {config}"""				)

 # Load weights from tf checkpoint
 load_tf_weights_in_canine(snake_case_						,		snake_case_						,		snake_case_				)

 # Save pytorch-model (weights and configuration)
 print(f"""Save PyTorch model to {pytorch_dump_path}"""				)
 model.save_pretrained(snake_case_				)

 # Save tokenizer files
 __lowerCAmelCase    							=						CanineTokenizer()
 print(f"""Save tokenizer files to {pytorch_dump_path}"""				)
 tokenizer.save_pretrained(snake_case_				)


if __name__ == "__main__":
 _A      :				int 				=    argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '''--tf_checkpoint_path''',
     default=None,
     type=str,
     required=True,
     help='''Path to the TensorFlow checkpoint. Should end with model.ckpt''',
 )
 parser.add_argument(
     '''--pytorch_dump_path''',
     default=None,
     type=str,
     required=True,
     help='''Path to a folder where the PyTorch model will be placed.''',
 )
 _A      :				Dict 				=    parser.parse_args()
 convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.pytorch_dump_path)






