

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 					A__:
						"""simple docstring"""






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





						def  UpperCamelCase__				(						self						)    ->   Union[str, Any]:
									a_	:	Union[str, Any]          =   {}
									a_	:	int          =   ids_tensor([self.batch_size, self.seq_length]     ,			self.vocab_size						).type(torch.intaa						)
									a_	:	List[str]          =   False

									a_	:	List[str]          =   self.get_config()

									return (config, input_ids)





						def  UpperCamelCase__				(						self						)    ->   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=_lowercase     ,			init_std=self.init_std     ,			return_dict=self.return_dict     ,			)





						def  UpperCamelCase__				(						self     ,			_lowercase     ,			_lowercase     ,			*_lowercase						)    ->   Tuple:
									a_	:	Optional[int]          =   CpmAntModel(config=_lowercase						)
									model.to(_lowercase						)
									model.eval()

									a_	:	List[str]          =   model(**_lowercase						).last_hidden_state

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





						def  UpperCamelCase__				(						self     ,			_lowercase     ,			_lowercase     ,			*_lowercase						)    ->   Tuple:
									a_	:	List[str]          =   CpmAntForCausalLM(_lowercase						)
									model.to(_lowercase						)
									a_	:	List[Any]          =   input_ids["""input_ids"""].to(_lowercase						)
									model.eval()

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







@require_torch
class 					A__(a_,     a_,     unittest.TestCase       ):
						"""simple docstring"""






						_A						:							Tuple									= (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
						_A						:							Union[str, Any]									= (
						    {'''feature-extraction''': CpmAntModel, '''text-generation''': CpmAntForCausalLM} if is_torch_available() else {}
						)

						_A						:							Optional[int]									= False
						_A						:							int									= False
						_A						:							Optional[Any]									= False
						_A						:							Dict									= False
						_A						:							Tuple									= False





						def  UpperCamelCase__				(						self						)    ->   Optional[Any]:
									a_	:	Any          =   CpmAntModelTester(self						)
									a_	:	List[Any]          =   ConfigTester(self     ,			config_class=_lowercase						)





						def  UpperCamelCase__				(						self						)    ->   Union[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  UpperCamelCase__				(						self						)    ->   Optional[Any]:
									unittest.skip("""CPMAnt doesn't support input_embeds."""						)(self.test_inputs_embeds						)





						def  UpperCamelCase__				(						self						)    ->   Dict:
									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						)    ->   Union[str, Any]:
									a_       ,				a_	:	Optional[int]          =   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_cpmant_model(_lowercase     ,			_lowercase						)





						def  UpperCamelCase__				(						self						)    ->   Optional[Any]:
									a_       ,				a_	:	Optional[int]          =   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(_lowercase     ,			_lowercase						)







@require_torch
class 					A__(unittest.TestCase       ):
						"""simple docstring"""






						@tooslow
						def  UpperCamelCase__				(						self						)    ->   Optional[int]:
									a_	:	Union[str, Any]          =   """今天天气真好！"""
									a_	:	List[Any]          =   """openbmb/cpm-ant-10b"""
									a_	:	str          =   CpmAntModel.from_pretrained(_lowercase						)
									a_	:	Optional[Any]          =   CpmAntTokenizer.from_pretrained(_lowercase						)
									a_	:	Union[str, Any]          =   tokenizer(_lowercase     ,			return_tensors="""pt"""						)
									a_	:	Optional[Any]          =   model(**_lowercase						).last_hidden_state

									a_	:	List[str]          =   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]     ,			_lowercase     ,			atol=1e-2						)						)







@require_torch
class 					A__(unittest.TestCase       ):
						"""simple docstring"""






						@tooslow
						def  UpperCamelCase__				(						self						)    ->   Any:
									a_	:	Union[str, Any]          =   """今天天气真好！"""
									a_	:	Tuple          =   """openbmb/cpm-ant-10b"""
									a_	:	List[str]          =   CpmAntForCausalLM.from_pretrained(_lowercase						)
									a_	:	str          =   CpmAntTokenizer.from_pretrained(_lowercase						)
									a_	:	int          =   tokenizer(_lowercase     ,			return_tensors="""pt"""						)
									a_	:	Tuple          =   model(**_lowercase						).logits

									a_	:	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]     ,			_lowercase     ,			atol=1e-2						)						)





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





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





import argparse
import os


__snake_case							:			List[Any]      		= {
    """pytorch""": {
        """1.13""": {
            """torch""": """1.13.1""",
            """torchvision""": """0.14.1""",
            """torchaudio""": """0.13.1""",
            """python""": 3.9,
            """cuda""": """cu116""",
            """install""": (
                """python3 -m pip install --no-cache-dir -U torch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1"""
                """ --extra-index-url https://download.pytorch.org/whl/cu116"""
            ),
            """base_image""": """nvidia/cuda:11.6.2-cudnn8-devel-ubuntu20.04""",
        },
        """1.12""": {
            """torch""": """1.12.1""",
            """torchvision""": """0.13.1""",
            """torchaudio""": """0.12.1""",
            """python""": 3.9,
            """cuda""": """cu113""",
            """install""": (
                """python3 -m pip install --no-cache-dir -U torch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1"""
                """ --extra-index-url https://download.pytorch.org/whl/cu113"""
            ),
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """1.11""": {
            """torch""": """1.11.0""",
            """torchvision""": """0.12.0""",
            """torchaudio""": """0.11.0""",
            """python""": 3.9,
            """cuda""": """cu113""",
            """install""": (
                """python3 -m pip install --no-cache-dir -U torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0"""
                """ --extra-index-url https://download.pytorch.org/whl/cu113"""
            ),
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """1.10""": {
            """torch""": """1.10.2""",
            """torchvision""": """0.11.3""",
            """torchaudio""": """0.10.2""",
            """python""": 3.9,
            """cuda""": """cu113""",
            """install""": (
                """python3 -m pip install --no-cache-dir -U torch==1.10.2 torchvision==0.11.3 torchaudio==0.10.2"""
                """ --extra-index-url https://download.pytorch.org/whl/cu113"""
            ),
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        # torchaudio < 0.10 has no CUDA-enabled binary distributions
        """1.9""": {
            """torch""": """1.9.1""",
            """torchvision""": """0.10.1""",
            """torchaudio""": """0.9.1""",
            """python""": 3.9,
            """cuda""": """cu111""",
            """install""": (
                """python3 -m pip install --no-cache-dir -U torch==1.9.1 torchvision==0.10.1 torchaudio==0.9.1"""
                """ --extra-index-url https://download.pytorch.org/whl/cu111"""
            ),
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
    },
    """tensorflow""": {
        """2.11""": {
            """tensorflow""": """2.11.1""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.11.1""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.10""": {
            """tensorflow""": """2.10.1""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.10.1""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.9""": {
            """tensorflow""": """2.9.3""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.9.3""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.8""": {
            """tensorflow""": """2.8.2""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.8.2""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.7""": {
            """tensorflow""": """2.7.3""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.7.3""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.6""": {
            """tensorflow""": """2.6.5""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.6.5""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.5""": {
            """tensorflow""": """2.5.3""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.5.3""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
    },
}


if __name__ == "__main__":
	__snake_case							:			List[str]      		= argparse.ArgumentParser("""Choose the framework and version to install""")
	parser.add_argument(
	    """--framework""", help="""The framework to install. Should be `torch` or `tensorflow`""", type=str, required=True
	)
	parser.add_argument("""--version""", help="""The version of the framework to install.""", type=str, required=True)
	__snake_case							:			int      		= parser.parse_args()

	__snake_case							:			Optional[Any]      		= past_versions_testing[args.framework][args.version]

	os.system(F"""echo \"export INSTALL_CMD=\'{info["install"]}\'\" >> ~/.profile""")
	print(F"""echo \"export INSTALL_CMD=\'{info["install"]}\'\" >> ~/.profile""")

	__snake_case							:			Dict      		= """"""
	if args.framework == "pytorch":
		__snake_case							:			Union[str, Any]      		= info["""cuda"""]
	os.system(F"""echo \"export CUDA='{cuda}'\" >> ~/.profile""")
	print(F"""echo \"export CUDA='{cuda}'\" >> ~/.profile""")


