





'''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        __a   :

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







													"""simple docstring"""
													UpperCAmelCase_      :   Dict      =	parent
													UpperCAmelCase_      :   Optional[int]      =	batch_size
													UpperCAmelCase_      :   int      =	seq_length
													UpperCAmelCase_      :   Union[str, Any]      =	is_training
													UpperCAmelCase_      :   Dict      =	use_token_type_ids
													UpperCAmelCase_      :   Any      =	use_input_mask
													UpperCAmelCase_      :   int      =	use_labels
													UpperCAmelCase_      :   Optional[Any]      =	use_mc_token_ids
													UpperCAmelCase_      :   Any      =	vocab_size
													UpperCAmelCase_      :   Dict      =	hidden_size
													UpperCAmelCase_      :   List[str]      =	num_hidden_layers
													UpperCAmelCase_      :   Tuple      =	num_attention_heads
													UpperCAmelCase_      :   Dict      =	intermediate_size
													UpperCAmelCase_      :   str      =	num_buckets
													UpperCAmelCase_      :   Optional[Any]      =	max_distance
													UpperCAmelCase_      :   str      =	prompt_length
													UpperCAmelCase_      :   Optional[int]      =	prompt_types
													UpperCAmelCase_      :   int      =	segment_types
													UpperCAmelCase_      :   List[Any]      =	init_std
													UpperCAmelCase_      :   Any      =	return_dict

							def      UpperCAmelCase__						(				self				:  str )       ->							Tuple:







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[Any]      =	{}
													UpperCAmelCase_      :   List[Any]      =	ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size ).type(torch.intaa )
													UpperCAmelCase_      :   Tuple      =	False

													UpperCAmelCase_      :   Dict      =	self.get_config()

													return (config, input_ids)

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







													"""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=lowerCAmelCase__							,  init_std=self.init_std							,  return_dict=self.return_dict							,  )

							def      UpperCAmelCase__						(				self				:  List[Any]							,  __magic_name__				:  Dict							,  __magic_name__				:  Optional[Any]							,  *__magic_name__				:  List[Any] )       ->							Tuple:







													"""simple docstring"""
													UpperCAmelCase_      :   Dict      =	CpmAntModel(config=lowerCAmelCase__ )
													model.to(lowerCAmelCase__ )
													model.eval()

													UpperCAmelCase_      :   List[str]      =	model(**lowerCAmelCase__ ).last_hidden_state

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

							def      UpperCAmelCase__						(				self				:  Optional[int]							,  __magic_name__				:  List[str]							,  __magic_name__				:  Optional[int]							,  *__magic_name__				:  List[str] )       ->							Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[Any]      =	CpmAntForCausalLM(lowerCAmelCase__ )
													model.to(lowerCAmelCase__ )
													UpperCAmelCase_      :   Any      =	input_ids["""input_ids"""].to(lowerCAmelCase__ )
													model.eval()

													UpperCAmelCase_      :   Optional[int]      =	model(**lowerCAmelCase__ )
													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				:  Optional[Any] )       ->							Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_      :   Dict      =	self.prepare_config_and_inputs()
													return config, inputs_dict





@require_torch
class        __a   (__snake_case							,		__snake_case							,		unittest.TestCase  ):
							__a       : Any          =							(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
							__a       : int          =							(
							    {"feature-extraction": CpmAntModel, "text-generation": CpmAntForCausalLM} if is_torch_available() else {}
							)

							__a       : Any          =							False
							__a       : Union[str, Any]          =							False
							__a       : Optional[Any]          =							False
							__a       : str          =							False
							__a       : str          =							False

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







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[int]      =	CpmAntModelTester(self )
													UpperCAmelCase_      :   Dict      =	ConfigTester(self							,  config_class=lowerCAmelCase__ )

							def      UpperCAmelCase__						(				self				:  str )       ->							Dict:







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







													"""simple docstring"""
													unittest.skip('''CPMAnt doesn\'t support input_embeds.''' )(self.test_inputs_embeds )

							def      UpperCAmelCase__						(				self				:  Optional[int] )       ->							List[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				:  List[str] )       ->							List[str]:







													"""simple docstring"""
													UpperCAmelCase_      :   Optional[int]      =	self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_cpmant_model(lowerCAmelCase__							,  lowerCAmelCase__ )



							def      UpperCAmelCase__						(				self				:  str )       ->							Any:







													"""simple docstring"""
													UpperCAmelCase_      :   List[str]      =	self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_lm_head_model(lowerCAmelCase__							,  lowerCAmelCase__ )





@require_torch
class        __a   (unittest.TestCase  ):

							@tooslow
							def      UpperCAmelCase__						(				self				:  Tuple )       ->							Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_      :   Dict      =	"""今天天气真好！"""
													UpperCAmelCase_      :   Tuple      =	"""openbmb/cpm-ant-10b"""
													UpperCAmelCase_      :   List[str]      =	CpmAntModel.from_pretrained(lowerCAmelCase__ )
													UpperCAmelCase_      :   List[str]      =	CpmAntTokenizer.from_pretrained(lowerCAmelCase__ )
													UpperCAmelCase_      :   Optional[Any]      =	tokenizer(lowerCAmelCase__							,  return_tensors='''pt''' )
													UpperCAmelCase_      :   Optional[Any]      =	model(**lowerCAmelCase__ ).last_hidden_state

													UpperCAmelCase_      :   Optional[int]      =	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, -1_1.3_3_2_4], [-1.0_1_0_7, -0.0_5_7_6, -5.9_5_7_7]]]							,  )
													self.assertTrue(torch.allclose(hidden_states[:, :3, :3]							,  lowerCAmelCase__							,  atol=1E-2 ) )





@require_torch
class        __a   (unittest.TestCase  ):

							@tooslow
							def      UpperCAmelCase__						(				self				:  Optional[Any] )       ->							Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_      :   List[Any]      =	"""今天天气真好！"""
													UpperCAmelCase_      :   int      =	"""openbmb/cpm-ant-10b"""
													UpperCAmelCase_      :   Optional[Any]      =	CpmAntForCausalLM.from_pretrained(lowerCAmelCase__ )
													UpperCAmelCase_      :   Optional[Any]      =	CpmAntTokenizer.from_pretrained(lowerCAmelCase__ )
													UpperCAmelCase_      :   List[Any]      =	tokenizer(lowerCAmelCase__							,  return_tensors='''pt''' )
													UpperCAmelCase_      :   Any      =	model(**lowerCAmelCase__ ).logits

													UpperCAmelCase_      :   Optional[Any]      =	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]							,  lowerCAmelCase__							,  atol=1E-2 ) )

							@tooslow
							def      UpperCAmelCase__						(				self				:  List[Any] )       ->							Optional[Any]:







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



							@tooslow
							def      UpperCAmelCase__						(				self				:  str )       ->							Any:







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






'''simple docstring'''

import json
import os
import shutil
import tempfile
import unittest

from transformers.models.wavaveca import WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor
from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_wavaveca import floats_list




class    lowerCamelCase__  (   unittest.TestCase     ):



   def  _UpperCamelCase		(    self     )				->     str:
        """simple docstring"""





        _UpperCamelCase  :Any       ="""<pad> <s> </s> <unk> | E T A O N I H S R D L U M W C F G Y P B V K ' X J Q Z""".split(""" """     )
        _UpperCamelCase  :Optional[Any]       =dict(zip(lowerCAmelCase__					,      range(len(lowerCAmelCase__     )     )     )     )

        _UpperCamelCase  :int       ={
            """pad_token""": """<pad>""",
            """unk_token""": """<unk>""",
            """bos_token""": """<s>""",
            """eos_token""": """</s>""",
        }
        _UpperCamelCase  :Union[str, Any]       ={
            """feature_size""": 1,
            """padding_value""": 0.0,
            """sampling_rate""": 16_000,
            """return_attention_mask""": False,
            """do_normalize""": True,
        }

        _UpperCamelCase  :List[str]       =tempfile.mkdtemp()
        _UpperCamelCase  :Optional[Any]       =os.path.join(self.tmpdirname					,      VOCAB_FILES_NAMES["""vocab_file"""]     )
        _UpperCamelCase  :Tuple       =os.path.join(self.tmpdirname					,      lowerCAmelCase__     )
        with open(self.vocab_file					,      """w"""					,      encoding="""utf-8"""     ) as fp:
             fp.write(json.dumps(lowerCAmelCase__     ) + """\n"""     )

        with open(self.feature_extraction_file					,      """w"""					,      encoding="""utf-8"""     ) as fp:
             fp.write(json.dumps(lowerCAmelCase__     ) + """\n"""     )



   def  _UpperCamelCase		(    self					,      **lowerCAmelCase__     )				->     List[str]:
        """simple docstring"""





        _UpperCamelCase  :int       =self.add_kwargs_tokens_map.copy()
        kwargs.update(lowerCAmelCase__     )
        return WavaVecaCTCTokenizer.from_pretrained(self.tmpdirname					,      **lowerCAmelCase__     )



   def  _UpperCamelCase		(    self					,      **lowerCAmelCase__     )				->     str:
        """simple docstring"""





        return WavaVecaFeatureExtractor.from_pretrained(self.tmpdirname					,      **lowerCAmelCase__     )



   def  _UpperCamelCase		(    self     )				->     List[str]:
        """simple docstring"""





        shutil.rmtree(self.tmpdirname     )



   def  _UpperCamelCase		(    self     )				->     int:
        """simple docstring"""





        _UpperCamelCase  :Union[str, Any]       =self.get_tokenizer()
        _UpperCamelCase  :int       =self.get_feature_extractor()

        _UpperCamelCase  :Union[str, Any]       =WavaVecaProcessor(tokenizer=lowerCAmelCase__					,      feature_extractor=lowerCAmelCase__     )

        processor.save_pretrained(self.tmpdirname     )
        _UpperCamelCase  :Dict       =WavaVecaProcessor.from_pretrained(self.tmpdirname     )

        self.assertEqual(processor.tokenizer.get_vocab()					,      tokenizer.get_vocab()     )
        self.assertIsInstance(processor.tokenizer					,      lowerCAmelCase__     )

        self.assertEqual(processor.feature_extractor.to_json_string()					,      feature_extractor.to_json_string()     )
        self.assertIsInstance(processor.feature_extractor					,      lowerCAmelCase__     )



   def  _UpperCamelCase		(    self     )				->     Tuple:
        """simple docstring"""





        _UpperCamelCase  :Union[str, Any]       =WavaVecaProcessor(tokenizer=self.get_tokenizer()					,      feature_extractor=self.get_feature_extractor()     )
        processor.save_pretrained(self.tmpdirname     )

        _UpperCamelCase  :Optional[int]       =self.get_tokenizer(bos_token="""(BOS)"""					,      eos_token="""(EOS)"""     )
        _UpperCamelCase  :Optional[int]       =self.get_feature_extractor(do_normalize=lowerCAmelCase__					,      padding_value=1.0     )

        _UpperCamelCase  :Optional[int]       =WavaVecaProcessor.from_pretrained(
            self.tmpdirname					,      bos_token="""(BOS)"""					,      eos_token="""(EOS)"""					,      do_normalize=lowerCAmelCase__					,      padding_value=1.0     )

        self.assertEqual(processor.tokenizer.get_vocab()					,      tokenizer_add_kwargs.get_vocab()     )
        self.assertIsInstance(processor.tokenizer					,      lowerCAmelCase__     )

        self.assertEqual(processor.feature_extractor.to_json_string()					,      feature_extractor_add_kwargs.to_json_string()     )
        self.assertIsInstance(processor.feature_extractor					,      lowerCAmelCase__     )



   def  _UpperCamelCase		(    self     )				->     int:
        """simple docstring"""





        _UpperCamelCase  :List[Any]       =self.get_feature_extractor()
        _UpperCamelCase  :List[str]       =self.get_tokenizer()

        _UpperCamelCase  :Any       =WavaVecaProcessor(tokenizer=lowerCAmelCase__					,      feature_extractor=lowerCAmelCase__     )

        _UpperCamelCase  :Tuple       =floats_list((3, 1_000)     )

        _UpperCamelCase  :Tuple       =feature_extractor(lowerCAmelCase__					,      return_tensors="""np"""     )
        _UpperCamelCase  :Union[str, Any]       =processor(lowerCAmelCase__					,      return_tensors="""np"""     )

        for key in input_feat_extract.keys():
             self.assertAlmostEqual(input_feat_extract[key].sum()					,      input_processor[key].sum()					,      delta=1e-2     )



   def  _UpperCamelCase		(    self     )				->     Tuple:
        """simple docstring"""





        _UpperCamelCase  :Any       =self.get_feature_extractor()
        _UpperCamelCase  :List[Any]       =self.get_tokenizer()

        _UpperCamelCase  :Tuple       =WavaVecaProcessor(tokenizer=lowerCAmelCase__					,      feature_extractor=lowerCAmelCase__     )

        _UpperCamelCase  :Optional[Any]       ="""This is a test string"""

        _UpperCamelCase  :Union[str, Any]       =processor(text=lowerCAmelCase__     )

        _UpperCamelCase  :Union[str, Any]       =tokenizer(lowerCAmelCase__     )

        for key in encoded_tok.keys():
             self.assertListEqual(encoded_tok[key]					,      encoded_processor[key]     )



   def  _UpperCamelCase		(    self     )				->     Optional[Any]:
        """simple docstring"""





        _UpperCamelCase  :int       =self.get_feature_extractor()
        _UpperCamelCase  :Any       =self.get_tokenizer()

        _UpperCamelCase  :str       =WavaVecaProcessor(tokenizer=lowerCAmelCase__					,      feature_extractor=lowerCAmelCase__     )

        _UpperCamelCase  :Optional[int]       =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

        _UpperCamelCase  :List[Any]       =processor.batch_decode(lowerCAmelCase__     )
        _UpperCamelCase  :Any       =tokenizer.batch_decode(lowerCAmelCase__     )

        self.assertListEqual(lowerCAmelCase__					,      lowerCAmelCase__     )




   def  _UpperCamelCase		(    self     )				->     Union[str, Any]:
        """simple docstring"""





        _UpperCamelCase  :List[Any]       =self.get_feature_extractor()
        _UpperCamelCase  :Any       =self.get_tokenizer()

        _UpperCamelCase  :List[str]       =WavaVecaProcessor(tokenizer=lowerCAmelCase__					,      feature_extractor=lowerCAmelCase__     )

        self.assertListEqual(
            processor.model_input_names					,      feature_extractor.model_input_names					,      msg="""`processor` and `feature_extractor` model input names do not match"""					,      )