

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_	:	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_	:	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_	:	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, -11.3_324], [-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						)





# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


__snake_case							:			Any      		= {
    """configuration_convnextv2""": [
        """CONVNEXTV2_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """ConvNextV2Config""",
    ]
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__snake_case							:			Dict      		= [
	    """CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST""",
	    """ConvNextV2ForImageClassification""",
	    """ConvNextV2Model""",
	    """ConvNextV2PreTrainedModel""",
	    """ConvNextV2Backbone""",
	]


if TYPE_CHECKING:
	from .configuration_convnextva import (
	    CONVNEXTV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    ConvNextVaConfig,
	)

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_convnextva import (
		    CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST,
		    ConvNextVaBackbone,
		    ConvNextVaForImageClassification,
		    ConvNextVaModel,
		    ConvNextVaPreTrainedModel,
		)

else:
	import sys

	__snake_case							:			Dict      		= _LazyModule(__name__, globals()["""__file__"""], _import_structure)


