



'''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      :       List[str]				,lowerCamelCase__      :       List[Any]				,lowerCamelCase__      :       Optional[Any]=2				,lowerCamelCase__      :       Dict=8				,lowerCamelCase__      :       Tuple=True				,lowerCamelCase__      :       List[str]=False				,lowerCamelCase__      :       Union[str, Any]=False				,lowerCamelCase__      :       Any=False				,lowerCamelCase__      :       List[Any]=False				,lowerCamelCase__      :       int=99				,lowerCamelCase__      :       Dict=32				,lowerCamelCase__      :       List[str]=3				,lowerCamelCase__      :       Any=4				,lowerCamelCase__      :       Optional[Any]=37				,lowerCamelCase__      :       Tuple=32				,lowerCamelCase__      :       Union[str, Any]=128				,lowerCamelCase__      :       List[Any]=8				,lowerCamelCase__      :       str=8				,lowerCamelCase__      :       List[str]=8				,lowerCamelCase__      :       Any=1.0				,lowerCamelCase__      :       Dict=True				,):







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





	def 			UpperCamelCase_						(				self      :       str				):







							'''simple docstring'''
							_UpperCamelCase      :	List[Any]				       =     {}
							_UpperCamelCase      :	Dict				       =     ids_tensor([self.batch_size, self.seq_length]				,self.vocab_size				).type(torch.intaa				)
							_UpperCamelCase      :	List[Any]				       =     False

							_UpperCamelCase      :	Optional[Any]				       =     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=lowerCamelCase__				,init_std=self.init_std				,return_dict=self.return_dict				,)





	def 			UpperCamelCase_						(				self      :       int				,lowerCamelCase__      :       Dict				,lowerCamelCase__      :       Union[str, Any]				,*lowerCamelCase__      :       Union[str, Any]				):







							'''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      :       Dict				,lowerCamelCase__      :       List[str]				,lowerCamelCase__      :       Optional[Any]				,*lowerCamelCase__      :       Dict				):







							'''simple docstring'''
							_UpperCamelCase      :	Optional[int]				       =     CpmAntForCausalLM(lowerCamelCase__				)
							model.to(lowerCamelCase__				)
							_UpperCamelCase      :	List[str]				       =     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      :       Tuple				):







							'''simple docstring'''
							_UpperCamelCase       ,    _UpperCamelCase      :	List[str]				       =     self.prepare_config_and_inputs()
							return config, inputs_dict






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

	lowercase__						       =    False
	lowercase__						       =    False
	lowercase__						       =    False
	lowercase__						       =    False
	lowercase__						       =    False





	def 			UpperCamelCase_						(				self      :       Dict				):







							'''simple docstring'''
							_UpperCamelCase      :	List[Any]				       =     CpmAntModelTester(self				)
							_UpperCamelCase      :	Optional[int]				       =     ConfigTester(self				,config_class=lowerCamelCase__				)





	def 			UpperCamelCase_						(				self      :       Union[str, Any]				):







							'''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      :       Optional[Any]				):







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





	def 			UpperCamelCase_						(				self      :       Dict				):







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







							'''simple docstring'''
							_UpperCamelCase       ,    _UpperCamelCase      :	List[str]				       =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_cpmant_model(lowerCamelCase__				,lowerCamelCase__				)





	def 			UpperCamelCase_						(				self      :       Union[str, Any]				):







							'''simple docstring'''
							_UpperCamelCase       ,    _UpperCamelCase      :	Optional[Any]				       =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_lm_head_model(lowerCamelCase__				,lowerCamelCase__				)






@require_torch
class 							lowercase__			(							unittest.TestCase     ):





	@tooslow
	def 			UpperCamelCase_						(				self      :       Any				):







							'''simple docstring'''
							_UpperCamelCase      :	int				       =     '今天天气真好！'
							_UpperCamelCase      :	Dict				       =     'openbmb/cpm-ant-10b'
							_UpperCamelCase      :	Dict				       =     CpmAntModel.from_pretrained(lowerCamelCase__				)
							_UpperCamelCase      :	Any				       =     CpmAntTokenizer.from_pretrained(lowerCamelCase__				)
							_UpperCamelCase      :	Optional[int]				       =     tokenizer(lowerCamelCase__				,return_tensors='pt'				)
							_UpperCamelCase      :	List[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 							lowercase__			(							unittest.TestCase     ):





	@tooslow
	def 			UpperCamelCase_						(				self      :       Optional[Any]				):







							'''simple docstring'''
							_UpperCamelCase      :	Tuple				       =     '今天天气真好！'
							_UpperCamelCase      :	Union[str, Any]				       =     'openbmb/cpm-ant-10b'
							_UpperCamelCase      :	str				       =     CpmAntForCausalLM.from_pretrained(lowerCamelCase__				)
							_UpperCamelCase      :	Optional[Any]				       =     CpmAntTokenizer.from_pretrained(lowerCamelCase__				)
							_UpperCamelCase      :	Any				       =     tokenizer(lowerCamelCase__				,return_tensors='pt'				)
							_UpperCamelCase      :	Union[str, 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      :       str				):







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





	@tooslow
	def 			UpperCamelCase_						(				self      :       int				):







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







'''simple docstring'''
from __future__ import annotations

from queue import Queue






class 							lowercase__			:





	def __init__(				self      :       int				):







							'''simple docstring'''
							_UpperCamelCase      :	dict[int, list[int]]				       =     {}





	def 			UpperCamelCase_						(				self      :       int				):







							'''simple docstring'''
							for i in self.vertices:
													print(lowerCamelCase__				,' : '				,' -> '.join([str(lowerCamelCase__				) for j in self.vertices[i]]				)				)





	def 			UpperCamelCase_						(				self      :       Optional[Any]				,lowerCamelCase__      :       int				,lowerCamelCase__      :       int				):







							'''simple docstring'''
							if from_vertex in self.vertices:
													self.vertices[from_vertex].append(lowerCamelCase__				)
							else:
													_UpperCamelCase      :	Any				       =     [to_vertex]





	def 			UpperCamelCase_						(				self      :       int				,lowerCamelCase__      :       int				):







							'''simple docstring'''
							_UpperCamelCase      :	Dict				       =     set()

							# create a first in first out queue to store all the vertices for BFS
							_UpperCamelCase      :	Queue				       =     Queue()

							# mark the source node as visited and enqueue it
							visited.add(lowerCamelCase__				)
							queue.put(lowerCamelCase__				)

							while not queue.empty():
													_UpperCamelCase      :	Optional[Any]				       =     queue.get()

													# loop through all adjacent vertex and enqueue it if not yet visited
													for adjacent_vertex in self.vertices[vertex]:
																			if adjacent_vertex not in visited:
																									queue.put(lowerCamelCase__				)
																									visited.add(lowerCamelCase__				)
							return visited


if __name__ == "__main__":
					from doctest import testmod

					testmod(verbose=True)

					snake_case_				:			int						 =						Graph()
					g.add_edge(0, 1)
					g.add_edge(0, 2)
					g.add_edge(1, 2)
					g.add_edge(2, 0)
					g.add_edge(2, 3)
					g.add_edge(3, 3)

					g.print_graph()
					# 0  :  1 -> 2
					# 1  :  2
					# 2  :  0 -> 3
					# 3  :  3

					assert sorted(g.bfs(2)) == [0, 1, 2, 3]


