




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   :							Optional[Any]      ,		lowercase__   :							Optional[int]      ,		lowercase__   :							Optional[Any]=2      ,		lowercase__   :							Tuple=8      ,		lowercase__   :							Any=True      ,		lowercase__   :							Union[str, Any]=False      ,		lowercase__   :							Optional[int]=False      ,		lowercase__   :							int=False      ,		lowercase__   :							Optional[int]=False      ,		lowercase__   :							Dict=9_9      ,		lowercase__   :							List[Any]=3_2      ,		lowercase__   :							Optional[Any]=3      ,		lowercase__   :							List[Any]=4      ,		lowercase__   :							Optional[Any]=3_7      ,		lowercase__   :							Tuple=3_2      ,		lowercase__   :							Optional[int]=1_2_8      ,		lowercase__   :							str=8      ,		lowercase__   :							List[Any]=8      ,		lowercase__   :							Dict=8      ,		lowercase__   :							Optional[int]=1.0      ,		lowercase__   :							Optional[Any]=True      ,		):
       a_		    =  parent
       a_		    =  batch_size
       a_		    =  seq_length
       a_		    =  is_training
       a_		    =  use_token_type_ids
       a_		    =  use_input_mask
       a_		    =  use_labels
       a_		    =  use_mc_token_ids
       a_		    =  vocab_size
       a_		    =  hidden_size
       a_		    =  num_hidden_layers
       a_		    =  num_attention_heads
       a_		    =  intermediate_size
       a_		    =  num_buckets
       a_		    =  max_distance
       a_		    =  prompt_length
       a_		    =  prompt_types
       a_		    =  segment_types
       a_		    =  init_std
       a_		    =  return_dict



     def 				__magic_name__		(  self   :							List[Any]       ):
       a_		    =  {}
       a_		    =  ids_tensor([self.batch_size, self.seq_length]      ,		self.vocab_size       ).type(torch.intaa       )
       a_		    =  False

       a_		    =  self.get_config()

       return (config, input_ids)



     def 				__magic_name__		(  self   :							str       ):
       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=lowercase__      ,		init_std=self.init_std      ,		return_dict=self.return_dict      ,		)



     def 				__magic_name__		(  self   :							Tuple      ,		lowercase__   :							Optional[int]      ,		lowercase__   :							Optional[Any]      ,		*lowercase__   :							Optional[int]       ):
       a_		    =  CpmAntModel(config=lowercase__       )
       model.to(lowercase__       )
       model.eval()

       a_		    =  model(**lowercase__       ).last_hidden_state

       self.parent.assertEqual(hidden_states.shape      ,		(self.batch_size, self.seq_length, config.hidden_size)       )



     def 				__magic_name__		(  self   :							Optional[int]      ,		lowercase__   :							List[str]      ,		lowercase__   :							Any      ,		*lowercase__   :							List[str]       ):
       a_		    =  CpmAntForCausalLM(lowercase__       )
       model.to(lowercase__       )
       a_		    =  input_ids['''input_ids'''].to(lowercase__       )
       model.eval()

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



     def 				__magic_name__		(  self   :							Tuple       ):
       a_    , a_		    =  self.prepare_config_and_inputs()
       return config, inputs_dict



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

     _lowerCAmelCase		       =						False
     _lowerCAmelCase		       =						False
     _lowerCAmelCase		       =						False
     _lowerCAmelCase		       =						False
     _lowerCAmelCase		       =						False



     def 				__magic_name__		(  self   :							Tuple       ):
       a_		    =  CpmAntModelTester(self       )
       a_		    =  ConfigTester(self      ,		config_class=lowercase__       )



     def 				__magic_name__		(  self   :							Dict       ):
       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 				__magic_name__		(  self   :							List[str]       ):
       unittest.skip('''CPMAnt doesn\'t support input_embeds.'''       )(self.test_inputs_embeds       )



     def 				__magic_name__		(  self   :							int       ):
       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 				__magic_name__		(  self   :							Optional[Any]       ):
       a_    , a_		    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_cpmant_model(lowercase__      ,		lowercase__       )



     def 				__magic_name__		(  self   :							List[str]       ):
       a_    , a_		    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_lm_head_model(lowercase__      ,		lowercase__       )



@require_torch
class 							__lowercase   (			unittest.TestCase    ):



     @tooslow
     def 				__magic_name__		(  self   :							List[str]       ):
       a_		    =  '''今天天气真好！'''
       a_		    =  '''openbmb/cpm-ant-10b'''
       a_		    =  CpmAntModel.from_pretrained(lowercase__       )
       a_		    =  CpmAntTokenizer.from_pretrained(lowercase__       )
       a_		    =  tokenizer(lowercase__      ,		return_tensors='''pt'''       )
       a_		    =  model(**lowercase__       ).last_hidden_state

       a_		    =  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]      ,		lowercase__      ,		atol=1e-2       )       )



@require_torch
class 							__lowercase   (			unittest.TestCase    ):



     @tooslow
     def 				__magic_name__		(  self   :							Tuple       ):
       a_		    =  '''今天天气真好！'''
       a_		    =  '''openbmb/cpm-ant-10b'''
       a_		    =  CpmAntForCausalLM.from_pretrained(lowercase__       )
       a_		    =  CpmAntTokenizer.from_pretrained(lowercase__       )
       a_		    =  tokenizer(lowercase__      ,		return_tensors='''pt'''       )
       a_		    =  model(**lowercase__       ).logits

       a_		    =  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]      ,		lowercase__      ,		atol=1e-2       )       )



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



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






import io
import json
import subprocess


UpperCamelCase__       				=	[
    ['''en''', '''ru'''],
    ['''ru''', '''en'''],
    ['''en''', '''de'''],
    ['''de''', '''en'''],
]

UpperCamelCase__       				=	8






def 				UpperCAmelCase__  (							_A      ,      _A			):






  """simple docstring"""


  a_		    =  {}
  for src, tgt in pairs:
    a_		    =  f"{src}-{tgt}"
    a_		    =  f"sacrebleu -t wmt19 -l {pair} --echo src".split()
    a_		    =  subprocess.run(_A      ,      stdout=subprocess.PIPE			).stdout.decode('''utf-8'''			).splitlines()
    a_		    =  f"sacrebleu -t wmt19 -l {pair} --echo ref".split()
    a_		    =  subprocess.run(_A      ,      stdout=subprocess.PIPE			).stdout.decode('''utf-8'''			).splitlines()
    a_		    =  {'''src''': src_lines[:n_objs], '''tgt''': tgt_lines[:n_objs]}
  return text


UpperCamelCase__       				=	get_all_data(pairs, n_objs)
UpperCamelCase__       				=	'''./fsmt_val_data.json'''
with io.open(filename, '''w''', encoding='''utf-8''') as f:
  UpperCamelCase__       				=	json.dump(text, f, indent=2, ensure_ascii=False)
