






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



	'''simple docstring'''




	def __init__(	self	,      a	,      a=2	,      a=8	,      a=True	,      a=False	,      a=False	,      a=False	,      a=False	,      a=99	,      a=32	,      a=3	,      a=4	,      a=37	,      a=32	,      a=1_28	,      a=8	,      a=8	,      a=8	,      a=1.0	,      a=True	,      )	->   str:
								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 							_UpperCamelCase      (	self )	->   Optional[Any]:
								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 							_UpperCamelCase      (	self )	->   Optional[int]:
								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=a	,      init_std=self.init_std	,      return_dict=self.return_dict	,      )




	def 							_UpperCamelCase      (	self	,      a	,      a	,      *a )	->   Union[str, Any]:
								snake_case_           =							CpmAntModel(config=a )
								model.to(a )
								model.eval()

								snake_case_           =							model(**a ).last_hidden_state

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




	def 							_UpperCamelCase      (	self	,      a	,      a	,      *a )	->   Tuple:
								snake_case_           =							CpmAntForCausalLM(a )
								model.to(a )
								snake_case_           =							input_ids["""input_ids"""].to(a )
								model.eval()

								snake_case_           =							model(**a )
								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[int]:
								snake_case_           =							self.prepare_config_and_inputs()
								return config, inputs_dict






@require_torch
class 			UpperCamelCase_				(      UpperCAmelCase__	, UpperCAmelCase__	, unittest.TestCase    ):



	'''simple docstring'''




	lowerCAmelCase													=			(CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
	lowerCAmelCase													=			(
	    {'feature-extraction': CpmAntModel, 'text-generation': CpmAntForCausalLM} if is_torch_available() else {}
	)

	lowerCAmelCase													=			False
	lowerCAmelCase													=			False
	lowerCAmelCase													=			False
	lowerCAmelCase													=			False
	lowerCAmelCase													=			False




	def 							_UpperCamelCase      (	self )	->   Any:
								snake_case_           =							CpmAntModelTester(self )
								snake_case_           =							ConfigTester(self	,      config_class=a )




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




	def 							_UpperCamelCase      (	self )	->   int:
								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 							_UpperCamelCase      (	self )	->   List[str]:
								snake_case_           =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_cpmant_model(a	,      a )






	def 							_UpperCamelCase      (	self )	->   Optional[int]:
								snake_case_           =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(a	,      a )






@require_torch
class 			UpperCamelCase_				(      unittest.TestCase    ):



	'''simple docstring'''




	@tooslow
	def 							_UpperCamelCase      (	self )	->   Dict:
								snake_case_           =							"""今天天气真好！"""
								snake_case_           =							"""openbmb/cpm-ant-10b"""
								snake_case_           =							CpmAntModel.from_pretrained(a )
								snake_case_           =							CpmAntTokenizer.from_pretrained(a )
								snake_case_           =							tokenizer(a	,      return_tensors='pt' )
								snake_case_           =							model(**a ).last_hidden_state

								snake_case_           =							torch.tensor(
								    [[[6.1_708, 5.9_244, 1.0_835], [6.5_207, 6.2_893, -11.33_24], [-1.0_107, -0.0_576, -5.9_577]]]	,      )
								self.assertTrue(torch.allclose(hidden_states[:, :3, :3]	,      a	,      atol=1E-2 ) )






@require_torch
class 			UpperCamelCase_				(      unittest.TestCase    ):



	'''simple docstring'''




	@tooslow
	def 							_UpperCamelCase      (	self )	->   Dict:
								snake_case_           =							"""今天天气真好！"""
								snake_case_           =							"""openbmb/cpm-ant-10b"""
								snake_case_           =							CpmAntForCausalLM.from_pretrained(a )
								snake_case_           =							CpmAntTokenizer.from_pretrained(a )
								snake_case_           =							tokenizer(a	,      return_tensors='pt' )
								snake_case_           =							model(**a ).logits

								snake_case_           =							torch.tensor(
								    [[[-6.4_267, -6.4_083, -6.3_958], [-5.8_802, -5.9_447, -5.7_811], [-5.3_896, -5.4_820, -5.4_295]]]	,      )
								self.assertTrue(torch.allclose(hidden_states[:, :3, :3]	,      a	,      atol=1E-2 ) )




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






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













from __future__ import annotations

from typing import Any





def 						__UpperCAmelCase      (					*a_):
							if all(_check_not_integer(a_) for m in matrix_s):
														for i in matrix_s[1:]:
																					_verify_matrix_sizes(matrix_s[0]							,					a_)
														return [[sum(a_) for t in zip(*a_)] for m in zip(*a_)]
							raise TypeError('Expected a matrix, got int/list instead')





def 						__UpperCAmelCase      (					a_							,					a_):
							if (
							    _check_not_integer(a_)
							    and _check_not_integer(a_)
							    and _verify_matrix_sizes(a_							,					a_)
							):
														return [[i - j for i, j in zip(*a_)] for m in zip(a_							,					a_)]
							raise TypeError('Expected a matrix, got int/list instead')





def 						__UpperCAmelCase      (					a_							,					a_):
							return [[x * n for x in row] for row in matrix]





def 						__UpperCAmelCase      (					a_							,					a_):
							if _check_not_integer(a_) and _check_not_integer(a_):
														snake_case_      ,		snake_case_           =							_verify_matrix_sizes(a_							,					a_)

							if cols[0] != rows[1]:
														snake_case_           =							(
														    'Cannot multiply matrix of dimensions '
														    f'''({rows[0]},{cols[0]}) and ({rows[1]},{cols[1]})'''
														)
														raise ValueError(a_)
							return [
							    [sum(m * n for m, n in zip(a_							,					a_)) for j in zip(*a_)] for i in matrix_a
							]





def 						__UpperCAmelCase      (					a_):
							snake_case_           =							int(a_)
							return [[int(row == column) for column in range(a_)] for row in range(a_)]





def 						__UpperCAmelCase      (					a_							,					a_ = True):
							if _check_not_integer(a_):
														if return_map:
																					return map(a_							,					zip(*a_))
														else:
																					return list(map(a_							,					zip(*a_)))
							raise TypeError('Expected a matrix, got int/list instead')





def 						__UpperCAmelCase      (					a_							,					a_							,					a_):
							snake_case_           =							matrix[:row] + matrix[row + 1 :]
							return [row[:column] + row[column + 1 :] for row in minor]





def 						__UpperCAmelCase      (					a_):
							if len(a_) == 1:
														return matrix[0][0]

							return sum(
							    x * determinant(minor(a_							,					0							,					a_)) * (-1) ** i
							    for i, x in enumerate(matrix[0]))





def 						__UpperCAmelCase      (					a_):
							snake_case_           =							determinant(a_)
							if det == 0:
														return None

							snake_case_           =							[
							    [determinant(minor(a_							,					a_							,					a_)) for j in range(len(a_))]
							    for i in range(len(a_))
							]

							snake_case_           =							[
							    [x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])]
							    for row in range(len(a_))
							]
							snake_case_           =							list(transpose(a_))
							return scalar_multiply(a_							,					1 / det)





def 						__UpperCAmelCase      (					a_):
							return not isinstance(a_							,					a_) and not isinstance(matrix[0]							,					a_)





def 						__UpperCAmelCase      (					a_):
							return len(a_), len(matrix[0])





def 						__UpperCAmelCase      (					a_							,					a_):
							snake_case_           =							_shape(a_) + _shape(a_)
							if shape[0] != shape[3] or shape[1] != shape[2]:
														snake_case_           =							(
														    'operands could not be broadcast together with shape '
														    f'''({shape[0], shape[1]}), ({shape[2], shape[3]})'''
														)
														raise ValueError(a_)
							return (shape[0], shape[2]), (shape[1], shape[3])





def 						__UpperCAmelCase      (					):
							snake_case_           =							[[12, 10], [3, 9]]
							snake_case_           =							[[3, 4], [7, 4]]
							snake_case_           =							[[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
							snake_case_           =							[[3, 0, 2], [2, 0, -2], [0, 1, 1]]
							print(f'''Add Operation, {add(a_							,					a_) = } \n''')
							print(
							    f'''Multiply Operation, {multiply(a_							,					a_) = } \n'''							,					)
							print(f'''Identity: {identity(5)}\n''')
							print(f'''Minor of {matrix_c} = {minor(a_							,					1							,					2)} \n''')
							print(f'''Determinant of {matrix_b} = {determinant(a_)} \n''')
							print(f'''Inverse of {matrix_d} = {inverse(a_)}\n''')


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	main()





