






import argparse
import gc
import json
import os
import shutil
import warnings

import torch

from transformers import LlamaConfig, LlamaForCausalLM, LlamaTokenizer


try:
	from transformers import LlamaTokenizerFast
except ImportError as e:
	warnings.warn(e)
	warnings.warn(
	    '''The converted tokenizer will be the `slow` tokenizer. To use the fast, update your `tokenizers` library and re-run the tokenizer conversion'''
	)
	lowerCamelCase : Optional[int]						   =       None
lowerCamelCase : str						   =       {
    '''7B''': 1_10_08,
    '''13B''': 1_38_24,
    '''30B''': 1_79_20,
    '''65B''': 2_20_16,
    '''70B''': 2_86_72,
}
lowerCamelCase : List[str]						   =       {
    '''7B''': 1,
    '''7Bf''': 1,
    '''13B''': 2,
    '''13Bf''': 2,
    '''30B''': 4,
    '''65B''': 8,
    '''70B''': 8,
    '''70Bf''': 8,
}
def       snake_case_		(     lowerCAmelCase_       :						str       ,       lowerCAmelCase_       :						Any=1       ,       lowerCAmelCase_       :						Optional[Any]=256      ):
							return multiple_of * ((int(ffn_dim_multiplier * int(8 * n / 3      )      ) + multiple_of - 1) // multiple_of)
def       snake_case_		(     lowerCAmelCase_       :						Tuple      ):
							with open(lowerCAmelCase_       ,       """r"""      ) as f:
														return json.load(lowerCAmelCase_      )
def       snake_case_		(     lowerCAmelCase_       :						Any       ,       lowerCAmelCase_       :						Union[str, Any]      ):
							with open(lowerCAmelCase_       ,       """w"""      ) as f:
														json.dump(lowerCAmelCase_       ,       lowerCAmelCase_      )
def       snake_case_		(     lowerCAmelCase_       :						Optional[Any]       ,       lowerCAmelCase_       :						List[Any]       ,       lowerCAmelCase_       :						int       ,       lowerCAmelCase_       :						Dict=True      ):
							os.makedirs(lowerCAmelCase_       ,       exist_ok=lowerCAmelCase_      )
							__lowercase      :							List[Any]				=     os.path.join(lowerCAmelCase_       ,       """tmp"""      )
							os.makedirs(lowerCAmelCase_       ,       exist_ok=lowerCAmelCase_      )

							__lowercase      :							str				=     read_json(os.path.join(lowerCAmelCase_       ,       """params.json"""      )      )
							__lowercase      :							Any				=     NUM_SHARDS[model_size]
							__lowercase      :							int				=     params["""n_layers"""]
							__lowercase      :							str				=     params["""n_heads"""]
							__lowercase      :							List[str]				=     n_heads // num_shards
							__lowercase      :							List[str]				=     params["""dim"""]
							__lowercase      :							Union[str, Any]				=     dim // n_heads
							__lowercase      :							Any				=     10_000.0
							__lowercase      :							Tuple				=     1.0 / (base ** (torch.arange(0       ,       lowerCAmelCase_       ,       2      ).float() / dims_per_head))

							if "n_kv_heads" in params:
														__lowercase      :							Union[str, Any]				=     params["""n_kv_heads"""]  # for GQA / MQA
														__lowercase      :							Union[str, Any]				=     n_heads_per_shard // num_key_value_heads
														__lowercase      :							Optional[Any]				=     dim // num_key_value_heads
							else:  # compatibility with other checkpoints
														__lowercase      :							Optional[Any]				=     n_heads
														__lowercase      :							Dict				=     n_heads_per_shard
														__lowercase      :							List[Any]				=     dim

							# permute for sliced rotary
							def permute(lowerCAmelCase_       :						List[Any]       ,       lowerCAmelCase_       :						str=n_heads       ,       lowerCAmelCase_       :						Optional[int]=dim       ,       lowerCAmelCase_       :						List[Any]=dim      ):
														return w.view(lowerCAmelCase_       ,       dima // n_heads // 2       ,       2       ,       lowerCAmelCase_      ).transpose(1       ,       2      ).reshape(lowerCAmelCase_       ,       lowerCAmelCase_      )

							print(F"Fetching all parameters from the checkpoint at {input_base_path}."      )
							# Load weights
							if model_size == "7B":
														# Not sharded
														# (The sharded implementation would also work, but this is simpler.)
														__lowercase      :							Optional[Any]				=     torch.load(os.path.join(lowerCAmelCase_       ,       """consolidated.00.pth"""      )       ,       map_location="""cpu"""      )
							else:
														# Sharded
														__lowercase      :							List[Any]				=     [
														    torch.load(os.path.join(lowerCAmelCase_       ,       F"consolidated.{i:02d}.pth"      )       ,       map_location="""cpu"""      )
														    for i in range(lowerCAmelCase_      )
														]
							__lowercase      :							List[str]				=     0
							__lowercase      :							Optional[int]				=     {"""weight_map""": {}}
							for layer_i in range(lowerCAmelCase_      ):
														__lowercase      :							Dict				=     F"pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin"
														if model_size == "7B":
																					# Unsharded
																					__lowercase      :							int				=     {
																					    F"model.layers.{layer_i}.self_attn.q_proj.weight": permute(
																					        loaded[F"layers.{layer_i}.attention.wq.weight"]      ),
																					    F"model.layers.{layer_i}.self_attn.k_proj.weight": permute(
																					        loaded[F"layers.{layer_i}.attention.wk.weight"]      ),
																					    F"model.layers.{layer_i}.self_attn.v_proj.weight": loaded[F"layers.{layer_i}.attention.wv.weight"],
																					    F"model.layers.{layer_i}.self_attn.o_proj.weight": loaded[F"layers.{layer_i}.attention.wo.weight"],
																					    F"model.layers.{layer_i}.mlp.gate_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w1.weight"],
																					    F"model.layers.{layer_i}.mlp.down_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w2.weight"],
																					    F"model.layers.{layer_i}.mlp.up_proj.weight": loaded[F"layers.{layer_i}.feed_forward.w3.weight"],
																					    F"model.layers.{layer_i}.input_layernorm.weight": loaded[F"layers.{layer_i}.attention_norm.weight"],
																					    F"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[F"layers.{layer_i}.ffn_norm.weight"],
																					}
														else:
																					# Sharded
																					# Note that attention.w{q,k,v,o}, feed_fordward.w[1,2,3], attention_norm.weight and ffn_norm.weight share
																					# the same storage object, saving attention_norm and ffn_norm will save other weights too, which is
																					# redundant as other weights will be stitched from multiple shards. To avoid that, they are cloned.

																					__lowercase      :							Any				=     {
																					    F"model.layers.{layer_i}.input_layernorm.weight": loaded[0][
																					        F"layers.{layer_i}.attention_norm.weight"
																					    ].clone(),
																					    F"model.layers.{layer_i}.post_attention_layernorm.weight": loaded[0][
																					        F"layers.{layer_i}.ffn_norm.weight"
																					    ].clone(),
																					}
																					__lowercase      :							Dict				=     permute(
																					    torch.cat(
																					        [
																					            loaded[i][F"layers.{layer_i}.attention.wq.weight"].view(lowerCAmelCase_       ,       lowerCAmelCase_       ,       lowerCAmelCase_      )
																					            for i in range(lowerCAmelCase_      )
																					        ]       ,       dim=0       ,       ).reshape(lowerCAmelCase_       ,       lowerCAmelCase_      )      )
																					__lowercase      :							Dict				=     permute(
																					    torch.cat(
																					        [
																					            loaded[i][F"layers.{layer_i}.attention.wk.weight"].view(
																					                lowerCAmelCase_       ,       lowerCAmelCase_       ,       lowerCAmelCase_      )
																					            for i in range(lowerCAmelCase_      )
																					        ]       ,       dim=0       ,       ).reshape(lowerCAmelCase_       ,       lowerCAmelCase_      )       ,       lowerCAmelCase_       ,       lowerCAmelCase_       ,       lowerCAmelCase_       ,       )
																					__lowercase      :							List[Any]				=     torch.cat(
																					    [
																					        loaded[i][F"layers.{layer_i}.attention.wv.weight"].view(
																					            lowerCAmelCase_       ,       lowerCAmelCase_       ,       lowerCAmelCase_      )
																					        for i in range(lowerCAmelCase_      )
																					    ]       ,       dim=0       ,       ).reshape(lowerCAmelCase_       ,       lowerCAmelCase_      )

																					__lowercase      :							Any				=     torch.cat(
																					    [loaded[i][F"layers.{layer_i}.attention.wo.weight"] for i in range(lowerCAmelCase_      )]       ,       dim=1      )
																					__lowercase      :							Union[str, Any]				=     torch.cat(
																					    [loaded[i][F"layers.{layer_i}.feed_forward.w1.weight"] for i in range(lowerCAmelCase_      )]       ,       dim=0      )
																					__lowercase      :							Optional[Any]				=     torch.cat(
																					    [loaded[i][F"layers.{layer_i}.feed_forward.w2.weight"] for i in range(lowerCAmelCase_      )]       ,       dim=1      )
																					__lowercase      :							List[Any]				=     torch.cat(
																					    [loaded[i][F"layers.{layer_i}.feed_forward.w3.weight"] for i in range(lowerCAmelCase_      )]       ,       dim=0      )

														__lowercase      :							List[str]				=     inv_freq
														for k, v in state_dict.items():
																					__lowercase      :							int				=     filename
																					param_count += v.numel()
														torch.save(lowerCAmelCase_       ,       os.path.join(lowerCAmelCase_       ,       lowerCAmelCase_      )      )

							__lowercase      :							Any				=     F"pytorch_model-{n_layers + 1}-of-{n_layers + 1}.bin"
							if model_size == "7B":
														# Unsharded
														__lowercase      :							List[Any]				=     {
														    """model.embed_tokens.weight""": loaded["""tok_embeddings.weight"""],
														    """model.norm.weight""": loaded["""norm.weight"""],
														    """lm_head.weight""": loaded["""output.weight"""],
														}
							else:
														__lowercase      :							List[Any]				=     {
														    """model.norm.weight""": loaded[0]["""norm.weight"""],
														    """model.embed_tokens.weight""": torch.cat(
														        [loaded[i]["""tok_embeddings.weight"""] for i in range(lowerCAmelCase_      )]       ,       dim=1      ),
														    """lm_head.weight""": torch.cat([loaded[i]["""output.weight"""] for i in range(lowerCAmelCase_      )]       ,       dim=0      ),
														}

							for k, v in state_dict.items():
														__lowercase      :							Union[str, Any]				=     filename
														param_count += v.numel()
							torch.save(lowerCAmelCase_       ,       os.path.join(lowerCAmelCase_       ,       lowerCAmelCase_      )      )

							# Write configs
							__lowercase      :							List[str]				=     {"""total_size""": param_count * 2}
							write_json(lowerCAmelCase_       ,       os.path.join(lowerCAmelCase_       ,       """pytorch_model.bin.index.json"""      )      )
							__lowercase      :							Union[str, Any]				=     params["""ffn_dim_multiplier"""] if """ffn_dim_multiplier""" in params else 1
							__lowercase      :							str				=     params["""multiple_of"""] if """multiple_of""" in params else 256
							__lowercase      :							Union[str, Any]				=     LlamaConfig(
							    hidden_size=lowerCAmelCase_       ,       intermediate_size=compute_intermediate_size(lowerCAmelCase_       ,       lowerCAmelCase_       ,       lowerCAmelCase_      )       ,       num_attention_heads=params["""n_heads"""]       ,       num_hidden_layers=params["""n_layers"""]       ,       rms_norm_eps=params["""norm_eps"""]       ,       num_key_value_heads=lowerCAmelCase_       ,       )
							config.save_pretrained(lowerCAmelCase_      )

							# Make space so we can load the model properly now.
							del state_dict
							del loaded
							gc.collect()

							print("""Loading the checkpoint in a Llama model."""      )
							__lowercase      :							Union[str, Any]				=     LlamaForCausalLM.from_pretrained(lowerCAmelCase_       ,       torch_dtype=torch.floataa       ,       low_cpu_mem_usage=lowerCAmelCase_      )
							# Avoid saving this as part of the config.
							del model.config._name_or_path

							print("""Saving in the Transformers format."""      )
							model.save_pretrained(lowerCAmelCase_       ,       safe_serialization=lowerCAmelCase_      )
							shutil.rmtree(lowerCAmelCase_      )
def       snake_case_		(     lowerCAmelCase_       :						Optional[int]       ,       lowerCAmelCase_       :						int      ):
							# Initialize the tokenizer based on the `spm` model
							__lowercase      :							Optional[Any]				=     LlamaTokenizer if LlamaTokenizerFast is None else LlamaTokenizerFast
							print(F"Saving a {tokenizer_class.__name__} to {tokenizer_path}."      )
							__lowercase      :							Optional[Any]				=     tokenizer_class(lowerCAmelCase_      )
							tokenizer.save_pretrained(lowerCAmelCase_      )
def       snake_case_		(     ):
							__lowercase      :							List[str]				=     argparse.ArgumentParser()
							parser.add_argument(
							    """--input_dir"""       ,       help="""Location of LLaMA weights, which contains tokenizer.model and model folders"""       ,       )
							parser.add_argument(
							    """--model_size"""       ,       choices=["""7B""", """7Bf""", """13B""", """13Bf""", """30B""", """65B""", """70B""", """70Bf""", """tokenizer_only"""]       ,       )
							parser.add_argument(
							    """--output_dir"""       ,       help="""Location to write HF model and tokenizer"""       ,       )
							parser.add_argument("""--safe_serialization"""       ,       type=lowerCAmelCase_       ,       help="""Whether or not to save using `safetensors`."""      )
							__lowercase      :							List[Any]				=     parser.parse_args()
							if args.model_size != "tokenizer_only":
														write_model(
														    model_path=args.output_dir       ,       input_base_path=os.path.join(args.input_dir       ,       args.model_size      )       ,       model_size=args.model_size       ,       safe_serialization=args.safe_serialization       ,       )
							__lowercase      :							List[Any]				=     os.path.join(args.input_dir       ,       """tokenizer.model"""      )
							write_tokenizer(args.output_dir       ,       lowerCAmelCase_      )


if __name__ == "__main__":
	main()







import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
	from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import MobileNetVaImageProcessor

class 		lowerCAmelCase     ( __a			):

					'''simple docstring'''

					def       lowerCAmelCase				(  self				:	List[str]							)			->  Optional[int]:


												"""simple docstring"""

												__lowercase      :							str				=     self.config_class(**self.inputs_dict							)
												self.parent.assertTrue(hasattr(__a		,     """tf_padding"""							)							)
												self.parent.assertTrue(hasattr(__a		,     """depth_multiplier"""							)							)







class 		lowerCAmelCase     :

					'''simple docstring'''

					def __init__(  self				:	Union[str, Any]		,     __a				:	Tuple		,     __a				:	str=13		,     __a				:	Dict=3		,     __a				:	List[Any]=32		,     __a				:	Any=0.25		,     __a				:	Any=8		,     __a				:	Optional[int]=8		,     __a				:	Optional[int]=6		,     __a				:	Dict=32		,     __a				:	Tuple=True		,     __a				:	List[Any]=True		,     __a				:	Optional[int]=True		,     __a				:	Tuple="relu6"		,     __a				:	Optional[Any]=1280		,     __a				:	str=0.1		,     __a				:	str=0.02		,     __a				:	Optional[Any]=True		,     __a				:	Tuple=True		,     __a				:	Dict=10		,     __a				:	Optional[Any]=None		,     )			->  Any:


												"""simple docstring"""

												__lowercase      :							List[str]				=     parent
												__lowercase      :							Tuple				=     batch_size
												__lowercase      :							Dict				=     num_channels
												__lowercase      :							Optional[int]				=     image_size
												__lowercase      :							int				=     depth_multiplier
												__lowercase      :							str				=     depth_divisible_by
												__lowercase      :							int				=     min_depth
												__lowercase      :							Tuple				=     expand_ratio
												__lowercase      :							Optional[int]				=     tf_padding
												__lowercase      :							Dict				=     output_stride
												__lowercase      :							Dict				=     first_layer_is_expansion
												__lowercase      :							Optional[Any]				=     finegrained_output
												__lowercase      :							str				=     hidden_act
												__lowercase      :							Union[str, Any]				=     last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier							)
												__lowercase      :							Optional[int]				=     classifier_dropout_prob
												__lowercase      :							int				=     use_labels
												__lowercase      :							Optional[int]				=     is_training
												__lowercase      :							Dict				=     num_labels
												__lowercase      :							Tuple				=     initializer_range
												__lowercase      :							Optional[Any]				=     scope



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


												"""simple docstring"""

												__lowercase      :							Tuple				=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

												__lowercase      :							List[Any]				=     None
												__lowercase      :							Optional[Any]				=     None
												if self.use_labels:
																			__lowercase      :							List[Any]				=     ids_tensor([self.batch_size]		,     self.num_labels							)
																			__lowercase      :							Optional[int]				=     ids_tensor([self.batch_size, self.image_size, self.image_size]		,     self.num_labels							)

												__lowercase      :							List[Any]				=     self.get_config()

												return config, pixel_values, labels, pixel_labels



					def       lowerCAmelCase				(  self				:	str							)			->  Union[str, Any]:


												"""simple docstring"""

												return MobileNetVaConfig(
												    num_channels=self.num_channels		,     image_size=self.image_size		,     depth_multiplier=self.depth_multiplier		,     depth_divisible_by=self.depth_divisible_by		,     min_depth=self.min_depth		,     expand_ratio=self.expand_ratio		,     output_stride=self.output_stride		,     first_layer_is_expansion=self.first_layer_is_expansion		,     finegrained_output=self.finegrained_output		,     hidden_act=self.hidden_act		,     tf_padding=self.tf_padding		,     classifier_dropout_prob=self.classifier_dropout_prob		,     initializer_range=self.initializer_range		,     )



					def       lowerCAmelCase				(  self				:	Tuple		,     __a				:	Dict		,     __a				:	Tuple		,     __a				:	Optional[int]		,     __a				:	Union[str, Any]							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							Optional[int]				=     MobileNetVaModel(config=__a							)
												model.to(__a							)
												model.eval()
												__lowercase      :							Tuple				=     model(__a							)
												self.parent.assertEqual(
												    result.last_hidden_state.shape		,     (
												        self.batch_size,
												        self.last_hidden_size,
												        self.image_size // self.output_stride,
												        self.image_size // self.output_stride,
												    )		,     )
												self.parent.assertEqual(
												    result.pooler_output.shape		,     (self.batch_size, self.last_hidden_size)		,     )



					def       lowerCAmelCase				(  self				:	List[str]		,     __a				:	Optional[int]		,     __a				:	List[str]		,     __a				:	str		,     __a				:	Optional[int]							)			->  Tuple:


												"""simple docstring"""

												__lowercase      :							List[Any]				=     self.num_labels
												__lowercase      :							Dict				=     MobileNetVaForImageClassification(__a							)
												model.to(__a							)
												model.eval()
												__lowercase      :							Dict				=     model(__a		,     labels=__a							)
												self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)							)



					def       lowerCAmelCase				(  self				:	int		,     __a				:	List[str]		,     __a				:	Tuple		,     __a				:	Any		,     __a				:	List[str]							)			->  Optional[int]:


												"""simple docstring"""

												__lowercase      :							int				=     self.num_labels
												__lowercase      :							List[Any]				=     MobileNetVaForSemanticSegmentation(__a							)
												model.to(__a							)
												model.eval()
												__lowercase      :							Dict				=     model(__a							)
												self.parent.assertEqual(
												    result.logits.shape		,     (
												        self.batch_size,
												        self.num_labels,
												        self.image_size // self.output_stride,
												        self.image_size // self.output_stride,
												    )		,     )
												__lowercase      :							str				=     model(__a		,     labels=__a							)
												self.parent.assertEqual(
												    result.logits.shape		,     (
												        self.batch_size,
												        self.num_labels,
												        self.image_size // self.output_stride,
												        self.image_size // self.output_stride,
												    )		,     )



					def       lowerCAmelCase				(  self				:	Tuple							)			->  Optional[int]:


												"""simple docstring"""

												__lowercase      :							List[str]				=     self.prepare_config_and_inputs()
												__lowercase			,			__lowercase			,			__lowercase			,			__lowercase      :							List[str]				=     config_and_inputs
												__lowercase      :							List[Any]				=     {"""pixel_values""": pixel_values}
												return config, inputs_dict







@require_torch
class 		lowerCAmelCase     ( __a      ,       __a      ,       unittest.TestCase			):

					'''simple docstring'''

					_A						:			Tuple									=      (
					    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
					    if is_torch_available()
					    else ()
					)
					_A						:			Optional[Any]									=      (
					    {
					        '''feature-extraction''': MobileNetVaModel,
					        '''image-classification''': MobileNetVaForImageClassification,
					        '''image-segmentation''': MobileNetVaForSemanticSegmentation,
					    }
					    if is_torch_available()
					    else {}
					)

					_A						:			Tuple									=      False
					_A						:			List[str]									=      False
					_A						:			List[str]									=      False
					_A						:			Optional[int]									=      False



					def       lowerCAmelCase				(  self				:	Optional[Any]							)			->  List[Any]:


												"""simple docstring"""

												__lowercase      :							Union[str, Any]				=     MobileNetVaModelTester(self							)
												__lowercase      :							int				=     MobileNetVaConfigTester(self		,     config_class=__a		,     has_text_modality=__a							)



					def       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  List[Any]:


												"""simple docstring"""

												self.config_tester.run_common_tests()



					@unittest.skip(reason="""MobileNetV2 does not use inputs_embeds"""							)
					def       lowerCAmelCase				(  self				:	List[str]							)			->  Union[str, Any]:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""MobileNetV2 does not support input and output embeddings"""							)
					def       lowerCAmelCase				(  self				:	Any							)			->  Tuple:


												"""simple docstring"""

												pass



					@unittest.skip(reason="""MobileNetV2 does not output attentions"""							)
					def       lowerCAmelCase				(  self				:	List[str]							)			->  int:


												"""simple docstring"""

												pass



					def       lowerCAmelCase				(  self				:	List[str]							)			->  Dict:


												"""simple docstring"""

												__lowercase			,			__lowercase      :							Optional[int]				=     self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			__lowercase      :							List[Any]				=     model_class(__a							)
																			__lowercase      :							Any				=     inspect.signature(model.forward							)
																			# signature.parameters is an OrderedDict => so arg_names order is deterministic
																			__lowercase      :							int				=     [*signature.parameters.keys()]

																			__lowercase      :							Any				=     ["""pixel_values"""]
																			self.assertListEqual(arg_names[:1]		,     __a							)



					def       lowerCAmelCase				(  self				:	Dict							)			->  Any:


												"""simple docstring"""

												__lowercase      :							Union[str, Any]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__a							)



					def       lowerCAmelCase				(  self				:	List[str]							)			->  Tuple:


												"""simple docstring"""

												def check_hidden_states_output(__a				:	List[Any]		,     __a				:	Tuple		,     __a				:	List[str]							):
																			__lowercase      :							Optional[Any]				=     model_class(__a							)
																			model.to(__a							)
																			model.eval()

																			with torch.no_grad():
																										__lowercase      :							List[Any]				=     model(**self._prepare_for_class(__a		,     __a							)							)

																			__lowercase      :							Tuple				=     outputs.hidden_states

																			__lowercase      :							str				=     16
																			self.assertEqual(len(__a							)		,     __a							)

												__lowercase			,			__lowercase      :							int				=     self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			__lowercase      :							Any				=     True
																			check_hidden_states_output(__a		,     __a		,     __a							)

																			# check that output_hidden_states also work using config
																			del inputs_dict["output_hidden_states"]
																			__lowercase      :							Union[str, Any]				=     True

																			check_hidden_states_output(__a		,     __a		,     __a							)



					def       lowerCAmelCase				(  self				:	Union[str, Any]							)			->  Any:


												"""simple docstring"""

												__lowercase      :							Dict				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__a							)



					def       lowerCAmelCase				(  self				:	List[str]							)			->  int:


												"""simple docstring"""

												__lowercase      :							Union[str, Any]				=     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_semantic_segmentation(*__a							)



					@slow
					def       lowerCAmelCase				(  self				:	Optional[int]							)			->  Optional[Any]:


												"""simple docstring"""

												for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__lowercase      :							Optional[int]				=     MobileNetVaModel.from_pretrained(__a							)
																			self.assertIsNotNone(__a							)







def       snake_case_		(     ):
							__lowercase      :							List[Any]				=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
							return image



@require_torch
@require_vision
class 		lowerCAmelCase     ( unittest.TestCase			):

					'''simple docstring'''

					@cached_property
					def       lowerCAmelCase				(  self				:	Any							)			->  Union[str, Any]:


												"""simple docstring"""

												return (
												    MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v2_1.0_224"""							) if is_vision_available() else None
												)



					@slow
					def       lowerCAmelCase				(  self				:	str							)			->  int:


												"""simple docstring"""

												__lowercase      :							Tuple				=     MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224"""							).to(__a							)

												__lowercase      :							str				=     self.default_image_processor
												__lowercase      :							Tuple				=     prepare_img()
												__lowercase      :							Tuple				=     image_processor(images=__a		,     return_tensors="""pt"""							).to(__a							)

												# forward pass
												with torch.no_grad():
																			__lowercase      :							str				=     model(**__a							)

												# verify the logits
												__lowercase      :							Union[str, Any]				=     torch.Size((1, 1001)							)
												self.assertEqual(outputs.logits.shape		,     __a							)

												__lowercase      :							str				=     torch.tensor([0.2445, -1.1993, 0.1905]							).to(__a							)

												self.assertTrue(torch.allclose(outputs.logits[0, :3]		,     __a		,     atol=1E-4							)							)



					@slow
					def       lowerCAmelCase				(  self				:	Tuple							)			->  Any:


												"""simple docstring"""

												__lowercase      :							int				=     MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""							)
												__lowercase      :							Dict				=     model.to(__a							)

												__lowercase      :							Tuple				=     MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""							)

												__lowercase      :							List[str]				=     prepare_img()
												__lowercase      :							Optional[int]				=     image_processor(images=__a		,     return_tensors="""pt"""							).to(__a							)

												# forward pass
												with torch.no_grad():
																			__lowercase      :							Union[str, Any]				=     model(**__a							)
												__lowercase      :							Any				=     outputs.logits

												# verify the logits
												__lowercase      :							Dict				=     torch.Size((1, 21, 65, 65)							)
												self.assertEqual(logits.shape		,     __a							)

												__lowercase      :							str				=     torch.tensor(
												    [
												        [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
												        [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
												        [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
												    ]		,     device=__a		,     )

												self.assertTrue(torch.allclose(logits[0, :3, :3, :3]		,     __a		,     atol=1E-4							)							)