
import argparse
import collections

import jax
import jax.numpy as jnp
import torch
import torch.nn as nn
from clip.model import CLIP
from flax.training import checkpoints
from huggingface_hub import Repository

from transformers import (
    CLIPTokenizer,
    OwlViTConfig,
    OwlViTForObjectDetection,
    OwlViTImageProcessor,
    OwlViTModel,
    OwlViTProcessor,
)


_SCREAMING_SNAKE_CASE     :   Any							 =      {
    'vit_b32': {
        'embed_dim': 5_1_2,
        'image_resolution': 7_6_8,
        'context_length': 1_6,
        'vocab_size': 4_9_4_0_8,
        'vision_layers': 1_2,
        'vision_width': 7_6_8,
        'vision_patch_size': 3_2,
        'transformer_width': 5_1_2,
        'transformer_heads': 8,
        'transformer_layers': 1_2,
    },
    'vit_b16': {
        'embed_dim': 5_1_2,
        'image_resolution': 7_6_8,
        'context_length': 1_6,
        'vocab_size': 4_9_4_0_8,
        'vision_layers': 1_2,
        'vision_width': 7_6_8,
        'vision_patch_size': 1_6,
        'transformer_width': 5_1_2,
        'transformer_heads': 8,
        'transformer_layers': 1_2,
    },
    'vit_l14': {
        'embed_dim': 7_6_8,
        'image_resolution': 8_4_0,
        'context_length': 1_6,
        'vocab_size': 4_9_4_0_8,
        'vision_layers': 2_4,
        'vision_width': 1_0_2_4,
        'vision_patch_size': 1_4,
        'transformer_width': 7_6_8,
        'transformer_heads': 1_2,
        'transformer_layers': 1_2,
    },
}

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    List[str]				,    __UpperCamelCase			:    List[Any]=""				,    __UpperCamelCase			:    str="/"							)			->							Any:

				"""simple docstring"""







				A__	:						List[str]								=				[]

				for k, v in params.items():
								A__	:						Any								=				parent_key + sep + k if parent_key else k

								if isinstance(__UpperCamelCase				,    collections.MutableMapping							):
												items.extend(flatten_nested_dict(__UpperCamelCase				,    __UpperCamelCase				,    sep=__UpperCamelCase							).items()							)
								else:
												items.append((new_key, v)							)
				return dict(__UpperCamelCase							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Optional[Any]							)			->							List[str]:

				"""simple docstring"""







				return jax.tree_util.tree_map(lambda __UpperCamelCase							: x.astype(jnp.floataa							) if x.dtype == jnp.bfloataa else x				,    __UpperCamelCase							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    List[str]				,    __UpperCamelCase			:    int							)			->							Any:

				"""simple docstring"""







				A__							,				A__							,				A__	:						int								=				pt_attn_layer.in_proj_weight.chunk(3				,    dim=0							)
				A__							,				A__							,				A__	:						Dict								=				pt_attn_layer.in_proj_bias.chunk(3				,    dim=0							)

				A__	:						Optional[int]								=				pt_attn_layer.out_proj.weight
				A__	:						Dict								=				pt_attn_layer.out_proj.bias

				A__	:						Tuple								=				q_proj
				A__	:						int								=				q_proj_bias

				A__	:						str								=				k_proj
				A__	:						Optional[Any]								=				k_proj_bias

				A__	:						Dict								=				v_proj
				A__	:						Union[str, Any]								=				v_proj_bias

				A__	:						Dict								=				out_proj_weights
				A__	:						Union[str, Any]								=				out_proj_bias

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Optional[int]				,    __UpperCamelCase			:    Optional[Any]							)			->							Tuple:

				"""simple docstring"""







				copy_linear(hf_mlp.fca				,    pt_mlp.c_fc							)
				copy_linear(hf_mlp.fca				,    pt_mlp.c_proj							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Optional[int]				,    __UpperCamelCase			:    Optional[int]							)			->							int:

				"""simple docstring"""







				A__	:						List[str]								=				pt_linear.weight
				A__	:						List[str]								=				pt_linear.bias

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Optional[Any]				,    __UpperCamelCase			:    List[Any]							)			->							Tuple:

				"""simple docstring"""







				copy_linear(hf_layer.layer_norma				,    pt_layer.ln_a							)
				copy_linear(hf_layer.layer_norma				,    pt_layer.ln_a							)

				# copy MLP
				copy_mlp(hf_layer.mlp				,    pt_layer.mlp							)

				# copy attn
				copy_attn_layer(hf_layer.self_attn				,    pt_layer.attn							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Dict				,    __UpperCamelCase			:    Optional[int]							)			->							Optional[int]:

				"""simple docstring"""







				for hf_layer, pt_layer in zip(__UpperCamelCase				,    __UpperCamelCase							):
								copy_layer(__UpperCamelCase				,    __UpperCamelCase							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Tuple				,    __UpperCamelCase			:    Optional[Any]							)			->							Dict:

				"""simple docstring"""







				A__	:						Dict								=				pt_model.token_embedding.weight
				A__	:						Union[str, Any]								=				pt_model.positional_embedding

				# copy layer norm
				copy_linear(hf_encoder.final_layer_norm				,    pt_model.ln_final							)

				# copy hidden layers
				copy_layers(hf_encoder.encoder.layers				,    pt_model.transformer.resblocks							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    List[str]				,    __UpperCamelCase			:    List[str]							)			->							List[str]:

				"""simple docstring"""







				A__	:						int								=				pt_model.text_projection.data.T

				# copy text encoder
				copy_encoder(hf_model.text_model				,    __UpperCamelCase							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    int				,    __UpperCamelCase			:    List[Any]							)			->							List[Any]:

				"""simple docstring"""







				A__	:						Any								=				pt_model.visual.proj.data.T

				# copy layer norms
				copy_linear(hf_model.vision_model.pre_layernorm				,    pt_model.visual.ln_pre							)
				copy_linear(hf_model.vision_model.post_layernorm				,    pt_model.visual.ln_post							)

				# copy embeds
				A__	:						Tuple								=				pt_model.visual.conva.weight.data
				A__	:						str								=				pt_model.visual.class_embedding
				A__	:						Dict								=				pt_model.visual.positional_embedding.data

				# copy encoder
				copy_layers(hf_model.vision_model.encoder.layers				,    pt_model.visual.transformer.resblocks							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Dict				,    __UpperCamelCase			:    int							)			->							List[Any]:

				"""simple docstring"""







				A__	:						str								=				flatten_nested_dict(flax_params['''backbone''']['''merged_class_token''']							)

				A__	:						str								=				torch.from_numpy(flax_class_token_params['''scale''']							)
				A__	:						Tuple								=				torch.from_numpy(flax_class_token_params['''bias''']							)
				A__	:						Tuple								=				nn.Parameter(__UpperCamelCase							)
				A__	:						Dict								=				nn.Parameter(__UpperCamelCase							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Union[str, Any]				,    __UpperCamelCase			:    Union[str, Any]							)			->							List[str]:

				"""simple docstring"""







				A__	:						List[str]								=				hf_model.state_dict()
				A__	:						int								=				{}

				# Rename class prediction head flax params to pytorch HF
				A__	:						Dict								=				flatten_nested_dict(flax_params['''class_head''']							)

				for flax_key, v in flax_class_params.items():
								A__	:						Union[str, Any]								=				flax_key.replace('''/'''				,    '''.'''							)
								A__	:						List[Any]								=				torch_key.replace('''.kernel'''				,    '''.weight'''							)
								A__	:						List[Any]								=				torch_key.replace('''Dense_0'''				,    '''dense0'''							)
								A__	:						int								=				'''class_head.''' + torch_key

								if "weight" in torch_key and v.ndim == 2:
												A__	:						Optional[int]								=				v.T

								A__	:						Optional[Any]								=				nn.Parameter(torch.from_numpy(__UpperCamelCase							)							)

				# Rename box prediction box flax params to pytorch HF
				A__	:						List[str]								=				flatten_nested_dict(flax_params['''obj_box_head''']							)

				for flax_key, v in flax_box_params.items():
								A__	:						Dict								=				flax_key.replace('''/'''				,    '''.'''							)
								A__	:						int								=				torch_key.replace('''.kernel'''				,    '''.weight'''							)
								A__	:						str								=				torch_key.replace('''_'''				,    ''''''							).lower()
								A__	:						List[str]								=				'''box_head.''' + torch_key

								if "weight" in torch_key and v.ndim == 2:
												A__	:						List[Any]								=				v.T

								A__	:						Union[str, Any]								=				nn.Parameter(torch.from_numpy(__UpperCamelCase							)							)

				# Copy flax params to PyTorch params
				for name, param in new_params.items():
								if name in pt_params.keys():
												pt_params[name].copy_(__UpperCamelCase							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Optional[int]				,    __UpperCamelCase			:    Tuple							)			->							Tuple:

				"""simple docstring"""







				for k, v in flax_attn_params.items():
								if k.startswith('''transformer'''							):
												A__	:						List[Any]								=				k.replace('''transformer.resblocks'''				,    '''text_model.encoder.layers'''							)
								else:
												A__	:						Optional[int]								=				k.replace('''visual.transformer.resblocks'''				,    '''vision_model.encoder.layers'''							)

								A__	:						str								=				torch_key.replace('''attn'''				,    '''self_attn'''							)
								A__	:						Tuple								=				torch_key.replace('''key'''				,    '''k_proj'''							)
								A__	:						List[Any]								=				torch_key.replace('''value'''				,    '''v_proj'''							)
								A__	:						Optional[int]								=				torch_key.replace('''query'''				,    '''q_proj'''							)
								A__	:						Optional[int]								=				torch_key.replace('''out'''				,    '''out_proj'''							)

								if "bias" in torch_key and v.ndim == 2:
												A__	:						int								=				v.shape[0] * v.shape[1]
												A__	:						Union[str, Any]								=				v.reshape(__UpperCamelCase							)

								if "weight" in torch_key and "out" in torch_key:
												A__	:						Dict								=				(v.shape[0] * v.shape[1], v.shape[2])
												A__	:						str								=				v.reshape(__UpperCamelCase							).T

								if "weight" in torch_key and "out" not in torch_key:
												A__	:						Optional[Any]								=				(v.shape[0], v.shape[1] * v.shape[2])
												A__	:						List[str]								=				v.reshape(__UpperCamelCase							).T

								# Copy flax CLIP attn params to HF PyTorch params
								A__	:						Optional[Any]								=				torch.from_numpy(__UpperCamelCase							)
								hf_backbone.state_dict()[torch_key].copy_(__UpperCamelCase							)

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Any							)			->							int:

				"""simple docstring"""







				A__	:						int								=				{}
				A__	:						List[str]								=				[]

				for k, v in params.items():
								if "attn." in k:
												A__	:						Tuple								=				k[: k.rindex('''attn.'''							) + 5]
												if base in processed_attn_layers:
																continue

												processed_attn_layers.append(__UpperCamelCase							)
												A__	:						Union[str, Any]								=				params[base + '''out.weight'''].shape[-1]
												A__	:						Union[str, Any]								=				params[base + '''out.weight'''].reshape(__UpperCamelCase				,    __UpperCamelCase							).T
												A__	:						Any								=				params[base + '''out.bias''']
								else:
												A__	:						Any								=				v
				return new_params

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    str				,    __UpperCamelCase			:    Union[str, Any]							)			->							int:

				"""simple docstring"""







				A__	:						int								=				CLIP(**__UpperCamelCase							)
				torch_model.eval()
				A__	:						str								=				torch_model.state_dict()

				A__	:						List[Any]								=				flatten_nested_dict(flax_params['''backbone''']['''clip''']							)
				A__	:						List[str]								=				{}

				for flax_key, v in flax_clip_params.items():
								A__	:						Union[str, Any]								=				flax_key.replace('''/'''				,    '''.'''							)
								A__	:						str								=				torch_key.replace('''text.token_embedding.embedding'''				,    '''token_embedding.kernel'''							)

								if (
								    torch_key.startswith('''text.transformer'''							)
								    or torch_key.startswith('''text.text_projection'''							)
								    or torch_key.startswith('''text.ln_final'''							)
								    or torch_key.startswith('''text.positional_embedding'''							)
								):
												A__	:						Union[str, Any]								=				torch_key[5:]

								A__	:						int								=				torch_key.replace('''text_projection.kernel'''				,    '''text_projection'''							)
								A__	:						Union[str, Any]								=				torch_key.replace('''visual.proj.kernel'''				,    '''visual.proj'''							)
								A__	:						int								=				torch_key.replace('''.scale'''				,    '''.weight'''							)
								A__	:						Tuple								=				torch_key.replace('''.kernel'''				,    '''.weight'''							)

								if "conv" in torch_key or "downsample.0.weight" in torch_key:
												A__	:						List[str]								=				v.transpose(3				,    2				,    0				,    1							)

								elif "weight" in torch_key and v.ndim == 2 and "embedding" not in torch_key:
												# Fully connected layers are transposed, embeddings are not
												A__	:						Optional[Any]								=				v.T

								A__	:						Any								=				v

				A__	:						Dict								=				_convert_attn_layers(__UpperCamelCase							)
				new_torch_params.update(__UpperCamelCase							)
				A__	:						str								=				{}

				# Copy flax CLIP backbone params to PyTorch params
				for name, param in new_torch_params.items():
								if name in torch_clip_params.keys():
												A__	:						List[str]								=				torch.from_numpy(new_torch_params[name]							)
												torch_clip_params[name].copy_(__UpperCamelCase							)
								else:
												A__	:						Optional[Any]								=				param

				return torch_clip_params, torch_model, attn_params

@torch.no_grad()
def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Optional[Any]				,    __UpperCamelCase			:    str				,    __UpperCamelCase			:    Any				,    __UpperCamelCase			:    List[Any]				,    __UpperCamelCase			:    int=None							)			->							int:

				"""simple docstring"""







				A__	:						str								=				Repository(__UpperCamelCase				,    clone_from=F"google/{pytorch_dump_folder_path}"							)
				repo.git_pull()

				if config_path is not None:
								A__	:						Dict								=				OwlViTConfig.from_pretrained(__UpperCamelCase							)
				else:
								A__	:						List[Any]								=				OwlViTConfig()

				A__	:						Optional[int]								=				OwlViTModel(__UpperCamelCase							).eval()
				A__	:						List[Any]								=				OwlViTForObjectDetection(__UpperCamelCase							).eval()

				copy_text_model_and_projection(__UpperCamelCase				,    __UpperCamelCase							)
				copy_vision_model_and_projection(__UpperCamelCase				,    __UpperCamelCase							)
				A__	:						int								=				pt_backbone.logit_scale
				copy_flax_attn_params(__UpperCamelCase				,    __UpperCamelCase							)

				A__	:						List[Any]								=				hf_backbone
				copy_class_merge_token(__UpperCamelCase				,    __UpperCamelCase							)
				copy_class_box_heads(__UpperCamelCase				,    __UpperCamelCase							)

				# Save HF model
				hf_model.save_pretrained(repo.local_dir							)

				# Initialize image processor
				A__	:						Any								=				OwlViTImageProcessor(
				    size=config.vision_config.image_size				,    crop_size=config.vision_config.image_size							)
				# Initialize tokenizer
				A__	:						str								=				CLIPTokenizer.from_pretrained('''openai/clip-vit-base-patch32'''				,    pad_token='''!'''				,    model_max_length=16							)

				# Initialize processor
				A__	:						List[Any]								=				OwlViTProcessor(image_processor=__UpperCamelCase				,    tokenizer=__UpperCamelCase							)
				image_processor.save_pretrained(repo.local_dir							)
				processor.save_pretrained(repo.local_dir							)

				repo.git_add()
				repo.git_commit('''Upload model and processor'''							)
				repo.git_push()


if __name__ == "__main__":
					_SCREAMING_SNAKE_CASE     :   str							 =      argparse.ArgumentParser()
					# Required parameters
					parser.add_argument(
					    '--owlvit_version',
					    default=None,
					    type=str,
					    required=True,
					    help='OWL-ViT model name [clip_b16, clip_b32, clip_l14].',
					)
					parser.add_argument(
					    '--owlvit_checkpoint', default=None, type=str, required=True, help='Path to flax model checkpoint.'
					)
					parser.add_argument('--hf_config', default=None, type=str, required=True, help='Path to HF model config.')
					parser.add_argument(
					    '--pytorch_dump_folder_path', default='hf_model', type=str, help='Path to the output PyTorch model.'
					)
					_SCREAMING_SNAKE_CASE     :   Union[str, Any]							 =      parser.parse_args()

					# Initialize PyToch clip model
					_SCREAMING_SNAKE_CASE     :   str							 =      args.owlvit_version
					if model_name == "clip_b16":
										_SCREAMING_SNAKE_CASE     :   Optional[Any]							 =      CONFIGS['vit_b16']
					elif model_name == "clip_b32":
										_SCREAMING_SNAKE_CASE     :   Dict							 =      CONFIGS['vit_b32']
					elif model_name == "clip_l14":
										_SCREAMING_SNAKE_CASE     :   Any							 =      CONFIGS['vit_l14']

					# Load from checkpoint and convert params to float-32
					_SCREAMING_SNAKE_CASE     :   Optional[Any]							 =      checkpoints.restore_checkpoint(args.owlvit_checkpoint, target=None)['optimizer']['target']
					_SCREAMING_SNAKE_CASE     :   Tuple							 =      jax.tree_util.tree_map(lambda x: x.astype(jnp.floataa) if x.dtype == jnp.bfloataa else x, variables)
					del variables

					# Convert CLIP backbone
					_SCREAMING_SNAKE_CASE ,  _SCREAMING_SNAKE_CASE ,  _SCREAMING_SNAKE_CASE     :   Any							 =      convert_clip_backbone(flax_params, torch_config)

					convert_owlvit_checkpoint(clip_pt, flax_params, attn_params, args.pytorch_dump_folder_path, args.hf_config)

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__		)