


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 	_snake_case						:




 def __init__(	self      ,						_lowerCamelCase      ,						_lowerCamelCase=2      ,						_lowerCamelCase=8      ,						_lowerCamelCase=True      ,						_lowerCamelCase=False      ,						_lowerCamelCase=False      ,						_lowerCamelCase=False      ,						_lowerCamelCase=False      ,						_lowerCamelCase=99      ,						_lowerCamelCase=32      ,						_lowerCamelCase=3      ,						_lowerCamelCase=4      ,						_lowerCamelCase=37      ,						_lowerCamelCase=32      ,						_lowerCamelCase=128      ,						_lowerCamelCase=8      ,						_lowerCamelCase=8      ,						_lowerCamelCase=8      ,						_lowerCamelCase=1.0      ,						_lowerCamelCase=True      ,						):
       a		:Union[str, Any]											= parent
       a		:str											= batch_size
       a		:List[str]											= seq_length
       a		:Optional[int]											= is_training
       a		:List[Any]											= use_token_type_ids
       a		:Any											= use_input_mask
       a		:List[Any]											= use_labels
       a		:Optional[Any]											= use_mc_token_ids
       a		:List[str]											= vocab_size
       a		:List[Any]											= hidden_size
       a		:Any											= num_hidden_layers
       a		:str											= num_attention_heads
       a		:int											= intermediate_size
       a		:Union[str, Any]											= num_buckets
       a		:Union[str, Any]											= max_distance
       a		:List[str]											= prompt_length
       a		:int											= prompt_types
       a		:str											= segment_types
       a		:List[str]											= init_std
       a		:Union[str, Any]											= return_dict




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:str											= {}
       a		:Tuple											= ids_tensor([self.batch_size, self.seq_length]      ,						self.vocab_size					).type(torch.intaa					)
       a		:str											= False

       a		:Any											= self.get_config()

       return (config, input_ids)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       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  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						*_lowerCamelCase					):
       a		:Dict											= CpmAntModel(config=_lowerCamelCase					)
       model.to(_lowerCamelCase					)
       model.eval()

       a		:Dict											= model(**_lowerCamelCase					).last_hidden_state

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




 def  SCREAMING_SNAKE_CASE__       (	self      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						*_lowerCamelCase					):
       a		:Dict											= CpmAntForCausalLM(_lowerCamelCase					)
       model.to(_lowerCamelCase					)
       a		:Optional[Any]											= input_ids['''input_ids'''].to(_lowerCamelCase					)
       model.eval()

       a		:Any											= 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  SCREAMING_SNAKE_CASE__       (	self					):
       a							,       a		:Dict											= self.prepare_config_and_inputs()
       return config, inputs_dict







@require_torch
class 	_snake_case						(   _snake_case		,					_snake_case		,					unittest.TestCase							):
 SCREAMING_SNAKE_CASE__   = (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
 SCREAMING_SNAKE_CASE__   = (
     {'feature-extraction': CpmAntModel, 'text-generation': CpmAntForCausalLM} if is_torch_available() else {}
 )

 SCREAMING_SNAKE_CASE__   = False
 SCREAMING_SNAKE_CASE__   = False
 SCREAMING_SNAKE_CASE__   = False
 SCREAMING_SNAKE_CASE__   = False
 SCREAMING_SNAKE_CASE__   = False




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:Optional[int]											= CpmAntModelTester(self					)
       a		:Optional[Any]											= ConfigTester(self      ,						config_class=_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       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  SCREAMING_SNAKE_CASE__       (	self					):
       unittest.skip('''CPMAnt doesn\'t support input_embeds.'''					)(self.test_inputs_embeds					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       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  SCREAMING_SNAKE_CASE__       (	self					):
       a							,       a		:Optional[int]											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_cpmant_model(_lowerCamelCase      ,						_lowerCamelCase					)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a							,       a		:Optional[int]											= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_lm_head_model(_lowerCamelCase      ,						_lowerCamelCase					)







@require_torch
class 	_snake_case						(   unittest.TestCase							):




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

       a		:int											= 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]      ,						_lowerCamelCase      ,						atol=1e-2					)					)







@require_torch
class 	_snake_case						(   unittest.TestCase							):




 @tooslow
 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:List[Any]											= '''今天天气真好！'''
       a		:int											= '''openbmb/cpm-ant-10b'''
       a		:Dict											= CpmAntForCausalLM.from_pretrained(_lowerCamelCase					)
       a		:Dict											= CpmAntTokenizer.from_pretrained(_lowerCamelCase					)
       a		:List[Any]											= tokenizer(_lowerCamelCase      ,						return_tensors='''pt'''					)
       a		:Tuple											= model(**_lowerCamelCase					).logits

       a		:Optional[int]											= 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]      ,						_lowerCamelCase      ,						atol=1e-2					)					)




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




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









import copy
from typing import Dict

from ...generation.configuration_utils import GenerationConfig
from ...utils import logging


snake_case       :					Dict        =  logging.get_logger(__name__)







class 	_snake_case						(   _snake_case							):
 SCREAMING_SNAKE_CASE__   = 'semantic'




 def __init__(	self      ,						_lowerCamelCase=1_0000      ,						_lowerCamelCase=True      ,						_lowerCamelCase=768      ,						_lowerCamelCase=False      ,						_lowerCamelCase=False      ,						_lowerCamelCase=False      ,						_lowerCamelCase=False      ,						_lowerCamelCase=0.7      ,						_lowerCamelCase=True      ,						_lowerCamelCase=1_0048      ,						_lowerCamelCase=12_9595      ,						_lowerCamelCase=12_9599      ,						_lowerCamelCase=1_0000      ,						_lowerCamelCase=256      ,						_lowerCamelCase=49.9      ,						**_lowerCamelCase      ,						):
       super().__init__(
           temperature=_lowerCamelCase      ,						do_sample=_lowerCamelCase      ,						eos_token_id=_lowerCamelCase      ,						renormalize_logits=_lowerCamelCase      ,						max_new_tokens=_lowerCamelCase      ,						output_scores=_lowerCamelCase      ,						return_dict_in_generate=_lowerCamelCase      ,						output_hidden_states=_lowerCamelCase      ,						output_attentions=_lowerCamelCase      ,						**_lowerCamelCase      ,						)

       a		:Dict											= text_encoding_offset
       a		:Any											= text_pad_token
       a		:List[Any]											= eos_token_id
       a		:str											= semantic_infer_token
       a		:Any											= semantic_vocab_size
       a		:Any											= max_input_semantic_length
       a		:Dict											= semantic_rate_hz







class 	_snake_case						(   _snake_case							):
 SCREAMING_SNAKE_CASE__   = 'coarse_acoustics'




 def __init__(	self      ,						_lowerCamelCase=True      ,						_lowerCamelCase=False      ,						_lowerCamelCase=False      ,						_lowerCamelCase=False      ,						_lowerCamelCase=False      ,						_lowerCamelCase=0.7      ,						_lowerCamelCase=True      ,						_lowerCamelCase=1_2048      ,						_lowerCamelCase=75      ,						_lowerCamelCase=2      ,						_lowerCamelCase=1_2050      ,						_lowerCamelCase=256      ,						_lowerCamelCase = 630      ,						_lowerCamelCase = 60      ,						**_lowerCamelCase      ,						):
       super().__init__(
           temperature=_lowerCamelCase      ,						do_sample=_lowerCamelCase      ,						renormalize_logits=_lowerCamelCase      ,						output_scores=_lowerCamelCase      ,						return_dict_in_generate=_lowerCamelCase      ,						output_hidden_states=_lowerCamelCase      ,						output_attentions=_lowerCamelCase      ,						**_lowerCamelCase      ,						)

       a		:Optional[Any]											= coarse_semantic_pad_token
       a		:Dict											= coarse_rate_hz
       a		:List[str]											= n_coarse_codebooks
       a		:List[Any]											= coarse_infer_token
       a		:Optional[Any]											= max_coarse_input_length
       a		:Any											= max_coarse_history
       a		:Optional[int]											= sliding_window_len







class 	_snake_case						(   _snake_case							):
 SCREAMING_SNAKE_CASE__   = 'fine_acoustics'




 def __init__(	self      ,						_lowerCamelCase=0.5      ,						_lowerCamelCase=512      ,						_lowerCamelCase=1024      ,						_lowerCamelCase=8      ,						**_lowerCamelCase      ,						):
       super().__init__(temperature=_lowerCamelCase					)

       a		:Union[str, Any]											= max_fine_history_length
       a		:int											= max_fine_input_length
       a		:str											= n_fine_codebooks







class 	_snake_case						(   _snake_case							):
 SCREAMING_SNAKE_CASE__   = 'bark'
 SCREAMING_SNAKE_CASE__   = True




 def __init__(	self      ,						_lowerCamelCase = None      ,						_lowerCamelCase = None      ,						_lowerCamelCase = None      ,						_lowerCamelCase=2_4000      ,						_lowerCamelCase=1024      ,						**_lowerCamelCase      ,						):
       if semantic_config is None:
             a		:str											= {}
             logger.info('''semantic_config is None. initializing the semantic model with default values.'''					)

       if coarse_acoustics_config is None:
             a		:List[str]											= {}
             logger.info('''coarse_acoustics_config is None. initializing the coarse model with default values.'''					)

       if fine_acoustics_config is None:
             a		:Any											= {}
             logger.info('''fine_acoustics_config is None. initializing the fine model with default values.'''					)

       a		:Any											= BarkSemanticGenerationConfig(**_lowerCamelCase					)
       a		:int											= BarkCoarseGenerationConfig(**_lowerCamelCase					)
       a		:Optional[int]											= BarkFineGenerationConfig(**_lowerCamelCase					)

       a		:Tuple											= sample_rate
       a		:Dict											= codebook_size




 @classmethod
 def  SCREAMING_SNAKE_CASE__       (	cls      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						_lowerCamelCase      ,						**_lowerCamelCase      ,						):
       return cls(
           semantic_config=semantic_config.to_dict()      ,						coarse_acoustics_config=coarse_acoustics_config.to_dict()      ,						fine_acoustics_config=fine_acoustics_config.to_dict()      ,						**_lowerCamelCase      ,						)




 def  SCREAMING_SNAKE_CASE__       (	self					):
       a		:int											= copy.deepcopy(self.__dict__					)

       a		:List[Any]											= self.semantic_config.to_dict()
       a		:Union[str, Any]											= self.coarse_acoustics_config.to_dict()
       a		:int											= self.fine_acoustics_config.to_dict()

       a		:Tuple											= self.__class__.model_type
       return output





