






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						:







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







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    parent
									_SCREAMING_SNAKE_CASE       =    batch_size
									_SCREAMING_SNAKE_CASE       =    seq_length
									_SCREAMING_SNAKE_CASE       =    is_training
									_SCREAMING_SNAKE_CASE       =    use_token_type_ids
									_SCREAMING_SNAKE_CASE       =    use_input_mask
									_SCREAMING_SNAKE_CASE       =    use_labels
									_SCREAMING_SNAKE_CASE       =    use_mc_token_ids
									_SCREAMING_SNAKE_CASE       =    vocab_size
									_SCREAMING_SNAKE_CASE       =    hidden_size
									_SCREAMING_SNAKE_CASE       =    num_hidden_layers
									_SCREAMING_SNAKE_CASE       =    num_attention_heads
									_SCREAMING_SNAKE_CASE       =    intermediate_size
									_SCREAMING_SNAKE_CASE       =    num_buckets
									_SCREAMING_SNAKE_CASE       =    max_distance
									_SCREAMING_SNAKE_CASE       =    prompt_length
									_SCREAMING_SNAKE_CASE       =    prompt_types
									_SCREAMING_SNAKE_CASE       =    segment_types
									_SCREAMING_SNAKE_CASE       =    init_std
									_SCREAMING_SNAKE_CASE       =    return_dict







			def 				UpperCamelCase (      self:    Optional[int]	):







									'''simple docstring'''




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

									_SCREAMING_SNAKE_CASE       =    self.get_config()

									return (config, input_ids)







			def 				UpperCamelCase (      self:    Optional[int]	):







									'''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 				UpperCamelCase (      self:    Tuple ,      UpperCAmelCase_:    List[str] ,      UpperCAmelCase_:    int ,      *UpperCAmelCase_:    Union[str, Any]	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    CpmAntModel(config=UpperCAmelCase_	)
									model.to(UpperCAmelCase_	)
									model.eval()

									_SCREAMING_SNAKE_CASE       =    model(**UpperCAmelCase_	).last_hidden_state

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







			def 				UpperCamelCase (      self:    str ,      UpperCAmelCase_:    int ,      UpperCAmelCase_:    Optional[Any] ,      *UpperCAmelCase_:    Tuple	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    CpmAntForCausalLM(UpperCAmelCase_	)
									model.to(UpperCAmelCase_	)
									_SCREAMING_SNAKE_CASE       =    input_ids["""input_ids"""].to(UpperCAmelCase_	)
									model.eval()

									_SCREAMING_SNAKE_CASE       =    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 				UpperCamelCase (      self:    Union[str, Any]	):







									'''simple docstring'''




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



@require_torch
class  __UpperCAmelCase						(_UpperCAmelCase			,_UpperCAmelCase			,unittest.TestCase       ):
			__snake_case							: List[Any]							=				(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
			__snake_case							: Any							=				(
			    {"feature-extraction": CpmAntModel, "text-generation": CpmAntForCausalLM} if is_torch_available() else {}
			)

			__snake_case							: Tuple							=				False
			__snake_case							: int							=				False
			__snake_case							: str							=				False
			__snake_case							: List[str]							=				False
			__snake_case							: str							=				False







			def 				UpperCamelCase (      self:    Optional[Any]	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE       =    CpmAntModelTester(self	)
									_SCREAMING_SNAKE_CASE       =    ConfigTester(self ,      config_class=UpperCAmelCase_	)







			def 				UpperCamelCase (      self:    Optional[int]	):







									'''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:    int	):







									'''simple docstring'''




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







			def 				UpperCamelCase (      self:    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 				UpperCamelCase (      self:    str	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_cpmant_model(UpperCAmelCase_ ,      UpperCAmelCase_	)







			def 				UpperCamelCase (      self:    int	):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE       =    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(UpperCAmelCase_ ,      UpperCAmelCase_	)



@require_torch
class  __UpperCAmelCase						(unittest.TestCase       ):







			@tooslow
			def 				UpperCamelCase (      self:    List[str]	):







									'''simple docstring'''




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

									_SCREAMING_SNAKE_CASE       =    torch.tensor(
									    [[[6.17_08, 5.92_44, 1.08_35], [6.52_07, 6.28_93, -11.33_24], [-1.01_07, -0.05_76, -5.95_77]]] ,      )
									self.assertTrue(torch.allclose(hidden_states[:, :3, :3] ,      UpperCAmelCase_ ,      atol=1E-2	)	)



@require_torch
class  __UpperCAmelCase						(unittest.TestCase       ):







			@tooslow
			def 				UpperCamelCase (      self:    Any	):







									'''simple docstring'''




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

									_SCREAMING_SNAKE_CASE       =    torch.tensor(
									    [[[-6.42_67, -6.40_83, -6.39_58], [-5.88_02, -5.94_47, -5.78_11], [-5.38_96, -5.48_20, -5.42_95]]] ,      )
									self.assertTrue(torch.allclose(hidden_states[:, :3, :3] ,      UpperCAmelCase_ ,      atol=1E-2	)	)







			@tooslow
			def 				UpperCamelCase (      self:    Dict	):







									'''simple docstring'''




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







			@tooslow
			def 				UpperCamelCase (      self:    Optional[int]	):







									'''simple docstring'''




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









from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
		if not (is_transformers_available() and is_torch_available() and is_transformers_version('''>=''', '''4.27.0''')):
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		from ...utils.dummy_torch_and_transformers_objects import (
		    AudioLDMPipeline,
		)
else:
		from .pipeline_audioldm import AudioLDMPipeline

