





'''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       lowerCamelCase       :


					def __init__(       self						:	Any							,  __snake_case						:	Optional[Any]							,  __snake_case						:	Any=2							,  __snake_case						:	str=8							,  __snake_case						:	List[str]=True							,  __snake_case						:	Union[str, Any]=False							,  __snake_case						:	Dict=False							,  __snake_case						:	str=False							,  __snake_case						:	List[str]=False							,  __snake_case						:	Any=99							,  __snake_case						:	List[str]=32							,  __snake_case						:	Optional[int]=3							,  __snake_case						:	Optional[int]=4							,  __snake_case						:	Optional[int]=37							,  __snake_case						:	Optional[int]=32							,  __snake_case						:	Dict=1_28							,  __snake_case						:	Dict=8							,  __snake_case						:	Dict=8							,  __snake_case						:	str=8							,  __snake_case						:	List[str]=1.0							,  __snake_case						:	List[str]=True							,  ):





											'''simple docstring'''





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


					def  SCREAMING_SNAKE_CASE_		(       self						:	Any							):





											'''simple docstring'''





											_snake_case: str	       =		{}
											_snake_case: Union[str, Any]	       =		ids_tensor([self.batch_size, self.seq_length]							,  self.vocab_size							).type(torch.intaa							)
											_snake_case: Optional[int]	       =		False

											_snake_case: Dict	       =		self.get_config()

											return (config, input_ids)


					def  SCREAMING_SNAKE_CASE_		(       self						:	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  SCREAMING_SNAKE_CASE_		(       self						:	Union[str, Any]							,  __snake_case						:	Dict							,  __snake_case						:	Any							,  *__snake_case						:	Optional[int]							):





											'''simple docstring'''





											_snake_case: Optional[Any]	       =		CpmAntModel(config=lowerCamelCase_							)
											model.to(lowerCamelCase_							)
											model.eval()

											_snake_case: int	       =		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						:	Optional[Any]							,  __snake_case						:	List[Any]							,  __snake_case						:	Optional[int]							,  *__snake_case						:	List[Any]							):





											'''simple docstring'''





											_snake_case: str	       =		CpmAntForCausalLM(lowerCamelCase_							)
											model.to(lowerCamelCase_							)
											_snake_case: List[Any]	       =		input_ids["""input_ids"""].to(lowerCamelCase_							)
											model.eval()

											_snake_case: str	       =		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						:	Dict							):





											'''simple docstring'''





											_snake_case: int	       =		self.prepare_config_and_inputs()
											return config, inputs_dict







@require_torch
class       lowerCamelCase       (    __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						:	int							):





											'''simple docstring'''





											_snake_case: Union[str, Any]	       =		CpmAntModelTester(self							)
											_snake_case: List[str]	       =		ConfigTester(self							,  config_class=lowerCamelCase_							)


					def  SCREAMING_SNAKE_CASE_		(       self						:	Dict							):





											'''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  SCREAMING_SNAKE_CASE_		(       self						:	str							):





											'''simple docstring'''





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


					def  SCREAMING_SNAKE_CASE_		(       self						:	Optional[int]							):





											'''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.\\n                 So is attentions. We strongly recommand you use loss to tune model.'							)(self.test_retain_grad_hidden_states_attentions							)


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





											'''simple docstring'''





											_snake_case: List[str]	       =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_cpmant_model(lowerCamelCase_							,  lowerCamelCase_							)




					def  SCREAMING_SNAKE_CASE_		(       self						:	int							):





											'''simple docstring'''





											_snake_case: Optional[Any]	       =		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_lm_head_model(lowerCamelCase_							,  lowerCamelCase_							)







@require_torch
class       lowerCamelCase       (    unittest.TestCase	):


					@tooslow
					def  SCREAMING_SNAKE_CASE_		(       self						:	List[str]							):





											'''simple docstring'''





											_snake_case: str	       =		"""今天天气真好！"""
											_snake_case: Dict	       =		"""openbmb/cpm-ant-10b"""
											_snake_case: Dict	       =		CpmAntModel.from_pretrained(lowerCamelCase_							)
											_snake_case: int	       =		CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
											_snake_case: str	       =		tokenizer(lowerCamelCase_							,  return_tensors='pt'							)
											_snake_case: Tuple	       =		model(**lowerCamelCase_							).last_hidden_state

											_snake_case: List[Any]	       =		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]							,  lowerCamelCase_							,  atol=1e-2							)							)







@require_torch
class       lowerCamelCase       (    unittest.TestCase	):


					@tooslow
					def  SCREAMING_SNAKE_CASE_		(       self						:	List[str]							):





											'''simple docstring'''





											_snake_case: Optional[Any]	       =		"""今天天气真好！"""
											_snake_case: Optional[int]	       =		"""openbmb/cpm-ant-10b"""
											_snake_case: Union[str, Any]	       =		CpmAntForCausalLM.from_pretrained(lowerCamelCase_							)
											_snake_case: Any	       =		CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
											_snake_case: str	       =		tokenizer(lowerCamelCase_							,  return_tensors='pt'							)
											_snake_case: Dict	       =		model(**lowerCamelCase_							).logits

											_snake_case: Any	       =		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]							,  lowerCamelCase_							,  atol=1e-2							)							)


					@tooslow
					def  SCREAMING_SNAKE_CASE_		(       self						:	Optional[int]							):





											'''simple docstring'''





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




					@tooslow
					def  SCREAMING_SNAKE_CASE_		(       self						:	int							):





											'''simple docstring'''





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










from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


UpperCamelCase_									=   logging.get_logger(__name__)







class       _snake_case				(     __snake_case ):






   '''simple docstring'''






   A__      : Optional[Any]       =						["pixel_values"]


   def __init__(    self:     Optional[Any]  ,lowerCamelCase_:     bool = True  ,lowerCamelCase_:     Optional[Dict[str, int]] = None  ,lowerCamelCase_:     PILImageResampling = PILImageResampling.BILINEAR  ,lowerCamelCase_:     bool = True  ,lowerCamelCase_:     Union[int, float] = 1 / 255  ,lowerCamelCase_:     bool = True  ,lowerCamelCase_:     Optional[Union[float, List[float]]] = None  ,lowerCamelCase_:     Optional[Union[float, List[float]]] = None  ,**lowerCamelCase_:     Dict  ,)	->      None:
       super().__init__(**lowerCamelCase_							)
       UpperCAmelCase_     :       Optional[Any]  		=					size if size is not None else {"""height""": 224, """width""": 224}
       UpperCAmelCase_     :       str  		=					get_size_dict(lowerCamelCase_							)
       UpperCAmelCase_     :       Tuple  		=					do_resize
       UpperCAmelCase_     :       Optional[Any]  		=					do_rescale
       UpperCAmelCase_     :       Optional[Any]  		=					do_normalize
       UpperCAmelCase_     :       int  		=					size
       UpperCAmelCase_     :       List[Any]  		=					resample
       UpperCAmelCase_     :       Optional[Any]  		=					rescale_factor
       UpperCAmelCase_     :       Dict  		=					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
       UpperCAmelCase_     :       Any  		=					image_std if image_std is not None else IMAGENET_STANDARD_STD


   def       A__    (    self:     Union[str, Any]  ,lowerCamelCase_:     np.ndarray  ,lowerCamelCase_:     Dict[str, int]  ,lowerCamelCase_:     PILImageResampling = PILImageResampling.BILINEAR  ,lowerCamelCase_:     Optional[Union[str, ChannelDimension]] = None  ,**lowerCamelCase_:     Optional[int]  ,)	->      np.ndarray:
       UpperCAmelCase_     :       Dict  		=					get_size_dict(lowerCamelCase_							)
       if "height" not in size or "width" not in size:
           raise ValueError(F'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}'''							)
       return resize(
           lowerCamelCase_  ,size=(size["""height"""], size["""width"""])  ,resample=lowerCamelCase_  ,data_format=lowerCamelCase_  ,**lowerCamelCase_							)


   def       A__    (    self:     List[str]  ,lowerCamelCase_:     np.ndarray  ,lowerCamelCase_:     float  ,lowerCamelCase_:     Optional[Union[str, ChannelDimension]] = None  ,**lowerCamelCase_:     List[Any]							)	->      np.ndarray:
       return rescale(lowerCamelCase_  ,scale=lowerCamelCase_  ,data_format=lowerCamelCase_  ,**lowerCamelCase_							)


   def       A__    (    self:     Optional[int]  ,lowerCamelCase_:     np.ndarray  ,lowerCamelCase_:     Union[float, List[float]]  ,lowerCamelCase_:     Union[float, List[float]]  ,lowerCamelCase_:     Optional[Union[str, ChannelDimension]] = None  ,**lowerCamelCase_:     List[Any]  ,)	->      np.ndarray:
       return normalize(lowerCamelCase_  ,mean=lowerCamelCase_  ,std=lowerCamelCase_  ,data_format=lowerCamelCase_  ,**lowerCamelCase_							)






   def       A__    (    self:     Optional[int]  ,lowerCamelCase_:     ImageInput  ,lowerCamelCase_:     Optional[bool] = None  ,lowerCamelCase_:     Dict[str, int] = None  ,lowerCamelCase_:     PILImageResampling = None  ,lowerCamelCase_:     Optional[bool] = None  ,lowerCamelCase_:     Optional[float] = None  ,lowerCamelCase_:     Optional[bool] = None  ,lowerCamelCase_:     Optional[Union[float, List[float]]] = None  ,lowerCamelCase_:     Optional[Union[float, List[float]]] = None  ,lowerCamelCase_:     Optional[Union[str, TensorType]] = None  ,lowerCamelCase_:     Union[str, ChannelDimension] = ChannelDimension.FIRST  ,**lowerCamelCase_:     int  ,)	->      List[Any]:
       UpperCAmelCase_     :       str  		=					do_resize if do_resize is not None else self.do_resize
       UpperCAmelCase_     :       Dict  		=					do_rescale if do_rescale is not None else self.do_rescale
       UpperCAmelCase_     :       Dict  		=					do_normalize if do_normalize is not None else self.do_normalize
       UpperCAmelCase_     :       int  		=					resample if resample is not None else self.resample
       UpperCAmelCase_     :       str  		=					rescale_factor if rescale_factor is not None else self.rescale_factor
       UpperCAmelCase_     :       Dict  		=					image_mean if image_mean is not None else self.image_mean
       UpperCAmelCase_     :       int  		=					image_std if image_std is not None else self.image_std

       UpperCAmelCase_     :       Tuple  		=					size if size is not None else self.size
       UpperCAmelCase_     :       Union[str, Any]  		=					get_size_dict(lowerCamelCase_							)

       UpperCAmelCase_     :       Optional[int]  		=					make_list_of_images(lowerCamelCase_							)

       if not valid_images(lowerCamelCase_							):
           raise ValueError(
               """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
               """torch.Tensor, tf.Tensor or jax.ndarray."""							)

       if do_resize and size is None:
           raise ValueError("""Size must be specified if do_resize is True."""							)

       if do_rescale and rescale_factor is None:
           raise ValueError("""Rescale factor must be specified if do_rescale is True."""							)

       # All transformations expect numpy arrays.
       UpperCAmelCase_     :       Optional[Any]  		=					[to_numpy_array(lowerCamelCase_							) for image in images]

       if do_resize:
           UpperCAmelCase_     :       int  		=					[self.resize(image=lowerCamelCase_  ,size=lowerCamelCase_  ,resample=lowerCamelCase_							) for image in images]

       if do_rescale:
           UpperCAmelCase_     :       Optional[Any]  		=					[self.rescale(image=lowerCamelCase_  ,scale=lowerCamelCase_							) for image in images]

       if do_normalize:
           UpperCAmelCase_     :       str  		=					[self.normalize(image=lowerCamelCase_  ,mean=lowerCamelCase_  ,std=lowerCamelCase_							) for image in images]

       UpperCAmelCase_     :       Any  		=					[to_channel_dimension_format(lowerCamelCase_  ,lowerCamelCase_							) for image in images]

       UpperCAmelCase_     :       List[str]  		=					{"""pixel_values""": images}
       return BatchFeature(data=lowerCamelCase_  ,tensor_type=lowerCamelCase_							)
