






"""simple docstring"""





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import TvltImageProcessor






def 							A(    snake_case_		,  snake_case_=10		,  snake_case_=10		,  snake_case_=False		,  snake_case_=False						):
       """simple docstring"""





       lowercase__:							Tuple         =     []
       for i in range(image_processor_tester.num_frames						):
              video.append(np.random.randint(255		,  size=(image_processor_tester.num_channels, width, height)		,  dtype=np.uinta						)						)

       if not numpify and not torchify:
              # PIL expects the channel dimension as last dimension
              lowercase__:							int         =     [Image.fromarray(np.moveaxis(__UpperCamelCase		,  0		,  -1						)						) for frame in video]

       if torchify:
              lowercase__:							Optional[Any]         =     [torch.from_numpy(__UpperCamelCase						) for frame in video]

       return video






def 							A(    snake_case_		,  snake_case_=False		,  snake_case_=False		,  snake_case_=False						):
       """simple docstring"""





       assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

       lowercase__:							int         =     []
       for i in range(image_processor_tester.batch_size						):
              if equal_resolution:
                     lowercase__:							Optional[int]         =     image_processor_tester.max_resolution
              else:
                     lowercase__:							str         =     np.random.choice(
                         np.arange(image_processor_tester.min_resolution		,  image_processor_tester.max_resolution						)		,  2						)
                     lowercase__:							Dict         =     prepare_video(
                         image_processor_tester=__UpperCamelCase		,  width=__UpperCamelCase		,  height=__UpperCamelCase		,  numpify=__UpperCamelCase		,  torchify=__UpperCamelCase		,  )
              video_inputs.append(__UpperCamelCase						)

       return video_inputs







class 						_a  (		unittest.TestCase       ):





   '''simple docstring'''






   def __init__(       self					, UpperCAmelCase_					, UpperCAmelCase_=7					, UpperCAmelCase_=3					, UpperCAmelCase_=4					, UpperCAmelCase_=18					, UpperCAmelCase_=30					, UpperCAmelCase_=400					, UpperCAmelCase_=True					, UpperCAmelCase_=None					, UpperCAmelCase_=True					, UpperCAmelCase_=[0.5, 0.5, 0.5]					, UpperCAmelCase_=[0.5, 0.5, 0.5]					, UpperCAmelCase_=True					, UpperCAmelCase_=None					, )			->   Any:




          '''simple docstring'''




          lowercase__:							Union[str, Any]         =     size if size is not None else {'''shortest_edge''': 18}
          lowercase__:							List[Any]         =     crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

          lowercase__:							List[str]         =     parent
          lowercase__:							Optional[Any]         =     batch_size
          lowercase__:							Union[str, Any]         =     num_channels
          lowercase__:							Optional[int]         =     num_frames
          lowercase__:							Optional[int]         =     image_size
          lowercase__:							Union[str, Any]         =     min_resolution
          lowercase__:							List[str]         =     max_resolution
          lowercase__:							Dict         =     do_resize
          lowercase__:							Tuple         =     size
          lowercase__:							Optional[int]         =     do_normalize
          lowercase__:							Optional[int]         =     image_mean
          lowercase__:							Optional[Any]         =     image_std
          lowercase__:							str         =     do_center_crop
          lowercase__:							List[Any]         =     crop_size





   def __lowercase							(       self)			->   List[str]:




          '''simple docstring'''




          return {
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_normalize": self.do_normalize,
              "do_resize": self.do_resize,
              "size": self.size,
              "do_center_crop": self.do_center_crop,
              "crop_size": self.crop_size,
          }







@require_torch
@require_vision
class 						_a  (		SCREAMING_SNAKE_CASE_							,	unittest.TestCase       ):





   '''simple docstring'''






   UpperCamelCase__     	=							TvltImageProcessor if is_vision_available() else None





   def __lowercase							(       self)			->   Any:




          '''simple docstring'''




          lowercase__:							Optional[Any]         =     TvltImageProcessorTester(self)





   @property
   def __lowercase							(       self)			->   Dict:




          '''simple docstring'''




          return self.image_processor_tester.prepare_image_processor_dict()





   def __lowercase							(       self)			->   Tuple:




          '''simple docstring'''




          lowercase__:							Optional[Any]         =     self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(UpperCamelCase__					, "image_mean"))
          self.assertTrue(hasattr(UpperCamelCase__					, "image_std"))
          self.assertTrue(hasattr(UpperCamelCase__					, "do_normalize"))
          self.assertTrue(hasattr(UpperCamelCase__					, "do_resize"))
          self.assertTrue(hasattr(UpperCamelCase__					, "do_center_crop"))
          self.assertTrue(hasattr(UpperCamelCase__					, "size"))





   def __lowercase							(       self)			->   Tuple:




          '''simple docstring'''




          lowercase__:							Optional[int]         =     self.image_processing_class(**self.image_processor_dict)
          # create random PIL videos
          lowercase__:							Optional[int]         =     prepare_video_inputs(self.image_processor_tester					, equal_resolution=UpperCamelCase__)
          for video in video_inputs:
                 self.assertIsInstance(UpperCamelCase__					, UpperCamelCase__)
                 self.assertIsInstance(video[0]					, Image.Image)

          # Test not batched input
          lowercase__:							Tuple         =     image_processor(video_inputs[0]					, return_tensors="pt").pixel_values
          self.assertEqual(
              encoded_videos.shape					, (
                  1,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size["height"],
                  self.image_processor_tester.crop_size["width"],
              )					, )

          # Test batched
          lowercase__:							List[str]         =     image_processor(UpperCamelCase__					, return_tensors="pt").pixel_values
          self.assertEqual(
              encoded_videos.shape					, (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size["height"],
                  self.image_processor_tester.crop_size["width"],
              )					, )





   def __lowercase							(       self)			->   Any:




          '''simple docstring'''




          lowercase__:							Optional[int]         =     self.image_processing_class(**self.image_processor_dict)
          # create random numpy tensors
          lowercase__:							List[str]         =     prepare_video_inputs(self.image_processor_tester					, equal_resolution=UpperCamelCase__					, numpify=UpperCamelCase__)
          for video in video_inputs:
                 self.assertIsInstance(UpperCamelCase__					, UpperCamelCase__)
                 self.assertIsInstance(video[0]					, np.ndarray)

          # Test not batched input
          lowercase__:							Any         =     image_processor(video_inputs[0]					, return_tensors="pt").pixel_values
          self.assertEqual(
              encoded_videos.shape					, (
                  1,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size["height"],
                  self.image_processor_tester.crop_size["width"],
              )					, )

          # Test batched
          lowercase__:							Tuple         =     image_processor(UpperCamelCase__					, return_tensors="pt").pixel_values
          self.assertEqual(
              encoded_videos.shape					, (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size["height"],
                  self.image_processor_tester.crop_size["width"],
              )					, )





   def __lowercase							(       self)			->   Optional[Any]:




          '''simple docstring'''




          lowercase__:							Any         =     self.image_processing_class(**self.image_processor_dict)
          # create random PyTorch tensors
          lowercase__:							Optional[int]         =     prepare_video_inputs(self.image_processor_tester					, equal_resolution=UpperCamelCase__					, torchify=UpperCamelCase__)
          for video in video_inputs:
                 self.assertIsInstance(UpperCamelCase__					, UpperCamelCase__)
                 self.assertIsInstance(video[0]					, torch.Tensor)

          # Test not batched input
          lowercase__:							int         =     image_processor(video_inputs[0]					, return_tensors="pt").pixel_values
          self.assertEqual(
              encoded_videos.shape					, (
                  1,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size["height"],
                  self.image_processor_tester.crop_size["width"],
              )					, )

          # Test batched
          lowercase__:							Union[str, Any]         =     image_processor(UpperCamelCase__					, return_tensors="pt").pixel_values
          self.assertEqual(
              encoded_videos.shape					, (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_frames,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size["height"],
                  self.image_processor_tester.crop_size["width"],
              )					, )








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					,							UpperCamelCase__					,							UpperCamelCase__=2					,							UpperCamelCase__=8					,							UpperCamelCase__=True					,							UpperCamelCase__=False					,							UpperCamelCase__=False					,							UpperCamelCase__=False					,							UpperCamelCase__=False					,							UpperCamelCase__=99					,							UpperCamelCase__=32					,							UpperCamelCase__=3					,							UpperCamelCase__=4					,							UpperCamelCase__=37					,							UpperCamelCase__=32					,							UpperCamelCase__=128					,							UpperCamelCase__=8					,							UpperCamelCase__=8					,							UpperCamelCase__=8					,							UpperCamelCase__=1.0					,							UpperCamelCase__=True					,							):
					A__	:						Dict								=				parent
					A__	:						Optional[int]								=				batch_size
					A__	:						Any								=				seq_length
					A__	:						Tuple								=				is_training
					A__	:						List[str]								=				use_token_type_ids
					A__	:						List[Any]								=				use_input_mask
					A__	:						Any								=				use_labels
					A__	:						Dict								=				use_mc_token_ids
					A__	:						Tuple								=				vocab_size
					A__	:						Any								=				hidden_size
					A__	:						List[Any]								=				num_hidden_layers
					A__	:						Union[str, Any]								=				num_attention_heads
					A__	:						Tuple								=				intermediate_size
					A__	:						Union[str, Any]								=				num_buckets
					A__	:						Dict								=				max_distance
					A__	:						str								=				prompt_length
					A__	:						int								=				prompt_types
					A__	:						int								=				segment_types
					A__	:						Union[str, Any]								=				init_std
					A__	:						Dict								=				return_dict





	def 	__snake_case					(      self		):
					A__	:						Optional[int]								=				{}
					A__	:						List[Any]								=				ids_tensor([self.batch_size, self.seq_length]					,							self.vocab_size		).type(torch.intaa		)
					A__	:						Dict								=				False

					A__	:						Union[str, Any]								=				self.get_config()

					return (config, input_ids)





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





	def 	__snake_case					(      self					,							UpperCamelCase__					,							UpperCamelCase__					,							*UpperCamelCase__		):
					A__	:						Tuple								=				CpmAntModel(config=UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.eval()

					A__	:						Union[str, Any]								=				model(**UpperCamelCase__		).last_hidden_state

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





	def 	__snake_case					(      self					,							UpperCamelCase__					,							UpperCamelCase__					,							*UpperCamelCase__		):
					A__	:						int								=				CpmAntForCausalLM(UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					A__	:						Any								=				input_ids['''input_ids'''].to(UpperCamelCase__		)
					model.eval()

					A__	:						Dict								=				model(**UpperCamelCase__		)
					self.parent.assertEqual(
					    model_output.logits.shape					,							(self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)					,							)





	def 	__snake_case					(      self		):
					A__							,				A__	:						List[str]								=				self.prepare_config_and_inputs()
					return config, inputs_dict







@require_torch
class 			UpperCamelCase__		(     SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      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 	__snake_case					(      self		):
					A__	:						Optional[int]								=				CpmAntModelTester(self		)
					A__	:						List[str]								=				ConfigTester(self					,							config_class=UpperCamelCase__		)





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





	def 	__snake_case					(      self		):
					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 	__snake_case					(      self		):
					A__							,				A__	:						Tuple								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_cpmant_model(UpperCamelCase__					,							UpperCamelCase__		)





	def 	__snake_case					(      self		):
					A__							,				A__	:						List[str]								=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_lm_head_model(UpperCamelCase__					,							UpperCamelCase__		)







@require_torch
class 			UpperCamelCase__		(     unittest.TestCase ):







	'''simple docstring'''





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

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







@require_torch
class 			UpperCamelCase__		(     unittest.TestCase ):







	'''simple docstring'''





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

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





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





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