


'''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  _lowercase   :
   def __init__(	self					:			Any					,      SCREAMING_SNAKE_CASE_					:			Optional[Any]					,      SCREAMING_SNAKE_CASE_					:			Optional[int]=2					,      SCREAMING_SNAKE_CASE_					:			List[str]=8					,      SCREAMING_SNAKE_CASE_					:			List[Any]=True					,      SCREAMING_SNAKE_CASE_					:			Optional[int]=False					,      SCREAMING_SNAKE_CASE_					:			List[str]=False					,      SCREAMING_SNAKE_CASE_					:			List[str]=False					,      SCREAMING_SNAKE_CASE_					:			str=False					,      SCREAMING_SNAKE_CASE_					:			Optional[Any]=99					,      SCREAMING_SNAKE_CASE_					:			List[str]=32					,      SCREAMING_SNAKE_CASE_					:			str=3					,      SCREAMING_SNAKE_CASE_					:			Union[str, Any]=4					,      SCREAMING_SNAKE_CASE_					:			Tuple=37					,      SCREAMING_SNAKE_CASE_					:			Dict=32					,      SCREAMING_SNAKE_CASE_					:			str=128					,      SCREAMING_SNAKE_CASE_					:			List[str]=8					,      SCREAMING_SNAKE_CASE_					:			int=8					,      SCREAMING_SNAKE_CASE_					:			Optional[Any]=8					,      SCREAMING_SNAKE_CASE_					:			Optional[int]=1.0					,      SCREAMING_SNAKE_CASE_					:			str=True					,      )   ->       Optional[Any]:
        __snake_case    			=	parent
        __snake_case    			=	batch_size
        __snake_case    			=	seq_length
        __snake_case    			=	is_training
        __snake_case    			=	use_token_type_ids
        __snake_case    			=	use_input_mask
        __snake_case    			=	use_labels
        __snake_case    			=	use_mc_token_ids
        __snake_case    			=	vocab_size
        __snake_case    			=	hidden_size
        __snake_case    			=	num_hidden_layers
        __snake_case    			=	num_attention_heads
        __snake_case    			=	intermediate_size
        __snake_case    			=	num_buckets
        __snake_case    			=	max_distance
        __snake_case    			=	prompt_length
        __snake_case    			=	prompt_types
        __snake_case    			=	segment_types
        __snake_case    			=	init_std
        __snake_case    			=	return_dict
   def a     (	self					:			Union[str, Any]			)   ->       List[str]:
        __snake_case    			=	{}
        __snake_case    			=	ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size			).type(torch.intaa			)
        __snake_case    			=	False

        __snake_case    			=	self.get_config()

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

        __snake_case    			=	model(**_snake_case			).last_hidden_state

        self.parent.assertEqual(hidden_states.shape					,      (self.batch_size, self.seq_length, config.hidden_size)			)
   def a     (	self					:			List[Any]					,      SCREAMING_SNAKE_CASE_					:			Optional[int]					,      SCREAMING_SNAKE_CASE_					:			List[Any]					,      *SCREAMING_SNAKE_CASE_					:			Optional[Any]			)   ->       List[str]:
        __snake_case    			=	CpmAntForCausalLM(_snake_case			)
        model.to(_snake_case			)
        __snake_case    			=	input_ids['''input_ids'''].to(_snake_case			)
        model.eval()

        __snake_case    			=	model(**_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					:			Union[str, Any]			)   ->       List[str]:
        __snake_case    			=	self.prepare_config_and_inputs()
        return config, inputs_dict




@require_torch
class  _lowercase   (  a_	,				a_	,				unittest.TestCase       ):
   _SCREAMING_SNAKE_CASE     :     Union[str, Any]									=  (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
   _SCREAMING_SNAKE_CASE     :     Union[str, Any]									=  (
       {'feature-extraction': CpmAntModel, 'text-generation': CpmAntForCausalLM} if is_torch_available() else {}
   )

   _SCREAMING_SNAKE_CASE     :     Optional[int]									=  False
   _SCREAMING_SNAKE_CASE     :     Any									=  False
   _SCREAMING_SNAKE_CASE     :     Dict									=  False
   _SCREAMING_SNAKE_CASE     :     int									=  False
   _SCREAMING_SNAKE_CASE     :     Optional[Any]									=  False
   def a     (	self					:			List[str]			)   ->       Tuple:
        __snake_case    			=	CpmAntModelTester(self			)
        __snake_case    			=	ConfigTester(self					,      config_class=_snake_case			)
   def a     (	self					:			List[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					:			Any			)   ->       List[Any]:
        unittest.skip('CPMAnt doesn\'t support input_embeds.'			)(self.test_inputs_embeds			)
   def a     (	self					:			List[str]			)   ->       Tuple:
        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.\\n                 So is attentions. We strongly recommand you use loss to tune model.'			)(self.test_retain_grad_hidden_states_attentions			)
   def a     (	self					:			Optional[int]			)   ->       Union[str, Any]:
        __snake_case    			=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_cpmant_model(_snake_case					,      _snake_case			)





   def a     (	self					:			str			)   ->       int:
        __snake_case    			=	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(_snake_case					,      _snake_case			)




@require_torch
class  _lowercase   (  unittest.TestCase       ):
   @tooslow
   def a     (	self					:			List[str]			)   ->       int:
        __snake_case    			=	'''今天天气真好！'''
        __snake_case    			=	'''openbmb/cpm-ant-10b'''
        __snake_case    			=	CpmAntModel.from_pretrained(_snake_case			)
        __snake_case    			=	CpmAntTokenizer.from_pretrained(_snake_case			)
        __snake_case    			=	tokenizer(_snake_case					,      return_tensors='pt'			)
        __snake_case    			=	model(**_snake_case			).last_hidden_state

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




@require_torch
class  _lowercase   (  unittest.TestCase       ):
   @tooslow
   def a     (	self					:			str			)   ->       int:
        __snake_case    			=	'''今天天气真好！'''
        __snake_case    			=	'''openbmb/cpm-ant-10b'''
        __snake_case    			=	CpmAntForCausalLM.from_pretrained(_snake_case			)
        __snake_case    			=	CpmAntTokenizer.from_pretrained(_snake_case			)
        __snake_case    			=	tokenizer(_snake_case					,      return_tensors='pt'			)
        __snake_case    			=	model(**_snake_case			).logits

        __snake_case    			=	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]					,      _snake_case					,      atol=1e-2			)			)
   @tooslow
   def a     (	self					:			str			)   ->       List[Any]:
        __snake_case    			=	'''openbmb/cpm-ant-10b'''
        __snake_case    			=	CpmAntForCausalLM.from_pretrained(_snake_case			)
        __snake_case    			=	CpmAntTokenizer.from_pretrained(_snake_case			)
        __snake_case    			=	'''今天天气不错，'''
        __snake_case    			=	'''今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的'''
        __snake_case    			=	tokenizer(_snake_case					,      return_tensors='pt'			)
        __snake_case    			=	model.generate(**_snake_case			)
        __snake_case    			=	tokenizer.batch_decode(_snake_case			)
        self.assertEqual(_snake_case					,      _snake_case			)





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








'''simple docstring'''




import torch

from diffusers import EulerAncestralDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest




class  _lowercase   (  __lowercase       ):
   _SCREAMING_SNAKE_CASE     :     List[str]									=  (EulerAncestralDiscreteScheduler,)
   _SCREAMING_SNAKE_CASE     :     Dict									=  1_0
   def a     (	self					:			List[str]					,      **SCREAMING_SNAKE_CASE_					:			Union[str, Any]			)   ->       List[str]:
        __snake_case    			=	{
            'num_train_timesteps': 1100,
            'beta_start': 0.0_0_0_1,
            'beta_end': 0.0_2,
            'beta_schedule': 'linear',
        }

        config.update(**SCREAMING_SNAKE_CASE_			)
        return config
   def a     (	self					:			Optional[int]			)   ->       int:
        for timesteps in [10, 50, 100, 1000]:
             self.check_over_configs(num_train_timesteps=SCREAMING_SNAKE_CASE_			)
   def a     (	self					:			Any			)   ->       List[Any]:
        for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1]					,      [0.0_0_0_2, 0.0_0_2, 0.0_2]			):
             self.check_over_configs(beta_start=SCREAMING_SNAKE_CASE_					,      beta_end=SCREAMING_SNAKE_CASE_			)
   def a     (	self					:			List[str]			)   ->       int:
        for schedule in ["linear", "scaled_linear"]:
             self.check_over_configs(beta_schedule=SCREAMING_SNAKE_CASE_			)
   def a     (	self					:			Dict			)   ->       Optional[Any]:
        for prediction_type in ["epsilon", "v_prediction"]:
             self.check_over_configs(prediction_type=SCREAMING_SNAKE_CASE_			)
   def a     (	self					:			List[Any]			)   ->       str:
        __snake_case    			=	self.scheduler_classes[0]
        __snake_case    			=	self.get_scheduler_config()
        __snake_case    			=	scheduler_class(**SCREAMING_SNAKE_CASE_			)

        scheduler.set_timesteps(self.num_inference_steps			)

        __snake_case    			=	torch.manual_seed(0			)

        __snake_case    			=	self.dummy_model()
        __snake_case    			=	self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
        __snake_case    			=	sample.to(SCREAMING_SNAKE_CASE_			)

        for i, t in enumerate(scheduler.timesteps			):
             __snake_case    			=	scheduler.scale_model_input(SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_			)

             __snake_case    			=	model(SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_			)

             __snake_case    			=	scheduler.step(SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_					,      generator=SCREAMING_SNAKE_CASE_			)
             __snake_case    			=	output.prev_sample

        __snake_case    			=	torch.sum(torch.abs(SCREAMING_SNAKE_CASE_			)			)
        __snake_case    			=	torch.mean(torch.abs(SCREAMING_SNAKE_CASE_			)			)

        assert abs(result_sum.item() - 1_5_2.3_1_9_2			) < 1e-2
        assert abs(result_mean.item() - 0.1_9_8_3			) < 1e-3
   def a     (	self					:			List[Any]			)   ->       Optional[Any]:
        __snake_case    			=	self.scheduler_classes[0]
        __snake_case    			=	self.get_scheduler_config(prediction_type='v_prediction'			)
        __snake_case    			=	scheduler_class(**SCREAMING_SNAKE_CASE_			)

        scheduler.set_timesteps(self.num_inference_steps			)

        __snake_case    			=	torch.manual_seed(0			)

        __snake_case    			=	self.dummy_model()
        __snake_case    			=	self.dummy_sample_deter * scheduler.init_noise_sigma
        __snake_case    			=	sample.to(SCREAMING_SNAKE_CASE_			)

        for i, t in enumerate(scheduler.timesteps			):
             __snake_case    			=	scheduler.scale_model_input(SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_			)

             __snake_case    			=	model(SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_			)

             __snake_case    			=	scheduler.step(SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_					,      generator=SCREAMING_SNAKE_CASE_			)
             __snake_case    			=	output.prev_sample

        __snake_case    			=	torch.sum(torch.abs(SCREAMING_SNAKE_CASE_			)			)
        __snake_case    			=	torch.mean(torch.abs(SCREAMING_SNAKE_CASE_			)			)

        assert abs(result_sum.item() - 1_0_8.4_4_3_9			) < 1e-2
        assert abs(result_mean.item() - 0.1_4_1_2			) < 1e-3





   def a     (	self					:			Optional[int]			)   ->       Dict:
        __snake_case    			=	self.scheduler_classes[0]
        __snake_case    			=	self.get_scheduler_config()
        __snake_case    			=	scheduler_class(**SCREAMING_SNAKE_CASE_			)

        scheduler.set_timesteps(self.num_inference_steps					,      device=SCREAMING_SNAKE_CASE_			)
        __snake_case    			=	torch.manual_seed(0			)

        __snake_case    			=	self.dummy_model()
        __snake_case    			=	self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
        __snake_case    			=	sample.to(SCREAMING_SNAKE_CASE_			)

        for t in scheduler.timesteps:
             __snake_case    			=	scheduler.scale_model_input(SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_			)

             __snake_case    			=	model(SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_			)

             __snake_case    			=	scheduler.step(SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_					,      SCREAMING_SNAKE_CASE_					,      generator=SCREAMING_SNAKE_CASE_			)
             __snake_case    			=	output.prev_sample

        __snake_case    			=	torch.sum(torch.abs(SCREAMING_SNAKE_CASE_			)			)
        __snake_case    			=	torch.mean(torch.abs(SCREAMING_SNAKE_CASE_			)			)

        assert abs(result_sum.item() - 1_5_2.3_1_9_2			) < 1e-2
        assert abs(result_mean.item() - 0.1_9_8_3			) < 1e-3




