





"""simple docstring"""






import argparse
import json

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import SegformerImageProcessor, SwinConfig, UperNetConfig, UperNetForSemanticSegmentation






def      lowercase__ (					lowerCAmelCase__   :			Tuple			)    ->			str:



			'''simple docstring'''
			a__  :	Dict		 =			3_8_4
			a__  :	Optional[Any]		 =			7
			if "tiny" in model_name:
						a__  :	Tuple		 =			9_6
						a__  :	Union[str, Any]		 =			(2, 2, 6, 2)
						a__  :	int		 =			(3, 6, 1_2, 2_4)
			elif "small" in model_name:
						a__  :	int		 =			9_6
						a__  :	List[str]		 =			(2, 2, 1_8, 2)
						a__  :	List[Any]		 =			(3, 6, 1_2, 2_4)
			elif "base" in model_name:
						a__  :	Dict		 =			1_2_8
						a__  :	Union[str, Any]		 =			(2, 2, 1_8, 2)
						a__  :	Optional[Any]		 =			(4, 8, 1_6, 3_2)
						a__  :	List[str]		 =			1_2
						a__  :	List[str]		 =			5_1_2
			elif "large" in model_name:
						a__  :	List[Any]		 =			1_9_2
						a__  :	Optional[int]		 =			(2, 2, 1_8, 2)
						a__  :	str		 =			(6, 1_2, 2_4, 4_8)
						a__  :	Optional[int]		 =			1_2
						a__  :	int		 =			7_6_8

			# set label information
			a__  :	List[Any]		 =			1_5_0
			a__  :	Dict		 =			"huggingface/label-files"
			a__  :	Tuple		 =			"ade20k-id2label.json"
			a__  :	Union[str, Any]		 =			json.load(open(hf_hub_download(lowerCAmelCase__							,			lowerCAmelCase__							,			repo_type="dataset"			)							,			"r"			)			)
			a__  :	int		 =			{int(lowerCAmelCase__			): v for k, v in idalabel.items()}
			a__  :	Tuple		 =			{v: k for k, v in idalabel.items()}

			a__  :	Tuple		 =			SwinConfig(
			    embed_dim=lowerCAmelCase__							,			depths=lowerCAmelCase__							,			num_heads=lowerCAmelCase__							,			window_size=lowerCAmelCase__							,			out_features=["stage1", "stage2", "stage3", "stage4"]							,			)
			a__  :	Optional[int]		 =			UperNetConfig(
			    backbone_config=lowerCAmelCase__							,			auxiliary_in_channels=lowerCAmelCase__							,			num_labels=lowerCAmelCase__							,			idalabel=lowerCAmelCase__							,			labelaid=lowerCAmelCase__							,			)

			return config






def      lowercase__ (					lowerCAmelCase__   :			List[str]			)    ->			List[Any]:



			'''simple docstring'''
			a__  :	int		 =			[]

			# fmt: off
			# stem
			rename_keys.append(("backbone.patch_embed.projection.weight", "backbone.embeddings.patch_embeddings.projection.weight")			)
			rename_keys.append(("backbone.patch_embed.projection.bias", "backbone.embeddings.patch_embeddings.projection.bias")			)
			rename_keys.append(("backbone.patch_embed.norm.weight", "backbone.embeddings.norm.weight")			)
			rename_keys.append(("backbone.patch_embed.norm.bias", "backbone.embeddings.norm.bias")			)
			# stages
			for i in range(len(config.backbone_config.depths			)			):
						for j in range(config.backbone_config.depths[i]			):
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.norm1.weight", F"backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.weight")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.norm1.bias", F"backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.bias")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_bias_table", F"backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_index", F"backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.weight", F"backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.bias", F"backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.norm2.weight", F"backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.weight")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.norm2.bias", F"backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.bias")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.weight", F"backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.bias", F"backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.ffn.layers.1.weight", F"backbone.encoder.layers.{i}.blocks.{j}.output.dense.weight")			)
									rename_keys.append((F"backbone.stages.{i}.blocks.{j}.ffn.layers.1.bias", F"backbone.encoder.layers.{i}.blocks.{j}.output.dense.bias")			)

						if i < 3:
									rename_keys.append((F"backbone.stages.{i}.downsample.reduction.weight", F"backbone.encoder.layers.{i}.downsample.reduction.weight")			)
									rename_keys.append((F"backbone.stages.{i}.downsample.norm.weight", F"backbone.encoder.layers.{i}.downsample.norm.weight")			)
									rename_keys.append((F"backbone.stages.{i}.downsample.norm.bias", F"backbone.encoder.layers.{i}.downsample.norm.bias")			)
						rename_keys.append((F"backbone.norm{i}.weight", F"backbone.hidden_states_norms.stage{i+1}.weight")			)
						rename_keys.append((F"backbone.norm{i}.bias", F"backbone.hidden_states_norms.stage{i+1}.bias")			)

			# decode head
			rename_keys.extend(
			    [
			        ("decode_head.conv_seg.weight", "decode_head.classifier.weight"),
			        ("decode_head.conv_seg.bias", "decode_head.classifier.bias"),
			        ("auxiliary_head.conv_seg.weight", "auxiliary_head.classifier.weight"),
			        ("auxiliary_head.conv_seg.bias", "auxiliary_head.classifier.bias"),
			    ]			)
			# fmt: on

			return rename_keys






def      lowercase__ (					lowerCAmelCase__   :			Dict							,			lowerCAmelCase__   :			Dict							,			lowerCAmelCase__   :			Dict			)    ->			List[str]:



			'''simple docstring'''
			a__  :	int		 =			dct.pop(lowerCAmelCase__			)
			a__  :	Any		 =			val






def      lowercase__ (					lowerCAmelCase__   :			Dict							,			lowerCAmelCase__   :			Any			)    ->			List[str]:



			'''simple docstring'''
			a__  :	str		 =			[int(backbone_config.embed_dim * 2**i			) for i in range(len(backbone_config.depths			)			)]
			for i in range(len(backbone_config.depths			)			):
						a__  :	int		 =			num_features[i]
						for j in range(backbone_config.depths[i]			):
									# fmt: off
									# read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias)
									a__  :	Any		 =			state_dict.pop(F"backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.weight"			)
									a__  :	Dict		 =			state_dict.pop(F"backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.bias"			)
									# next, add query, keys and values (in that order) to the state dict
									a__  :	Dict		 =			in_proj_weight[:dim, :]
									a__  :	List[str]		 =			in_proj_bias[: dim]
									a__  :	Optional[int]		 =			in_proj_weight[
									    dim : dim * 2, :
									]
									a__  :	List[str]		 =			in_proj_bias[
									    dim : dim * 2
									]
									a__  :	List[Any]		 =			in_proj_weight[
									    -dim :, :
									]
									a__  :	List[str]		 =			in_proj_bias[-dim :]
									# fmt: on






def      lowercase__ (					lowerCAmelCase__   :			Dict			)    ->			List[Any]:



			'''simple docstring'''
			a__ ,	a__  :	List[str]		 =			x.shape
			a__  :	Tuple		 =			x.reshape(lowerCAmelCase__							,			4							,			in_channel // 4			)
			a__  :	List[str]		 =			x[:, [0, 2, 1, 3], :].transpose(1							,			2			).reshape(lowerCAmelCase__							,			lowerCAmelCase__			)
			return x






def      lowercase__ (					lowerCAmelCase__   :			Any			)    ->			Optional[Any]:



			'''simple docstring'''
			a__ ,	a__  :	int		 =			x.shape
			a__  :	str		 =			x.reshape(lowerCAmelCase__							,			in_channel // 4							,			4			)
			a__  :	Union[str, Any]		 =			x[:, :, [0, 2, 1, 3]].transpose(1							,			2			).reshape(lowerCAmelCase__							,			lowerCAmelCase__			)

			return x






def      lowercase__ (					lowerCAmelCase__   :			int			)    ->			int:



			'''simple docstring'''
			a__  :	Tuple		 =			x.shape[0]
			a__  :	int		 =			x.reshape(4							,			in_channel // 4			)
			a__  :	List[Any]		 =			x[[0, 2, 1, 3], :].transpose(0							,			1			).reshape(lowerCAmelCase__			)
			return x






def      lowercase__ (					lowerCAmelCase__   :			Union[str, Any]			)    ->			List[str]:



			'''simple docstring'''
			a__  :	List[Any]		 =			x.shape[0]
			a__  :	List[str]		 =			x.reshape(in_channel // 4							,			4			)
			a__  :	Dict		 =			x[:, [0, 2, 1, 3]].transpose(0							,			1			).reshape(lowerCAmelCase__			)
			return x






def      lowercase__ (					lowerCAmelCase__   :			Optional[Any]							,			lowerCAmelCase__   :			List[Any]							,			lowerCAmelCase__   :			List[Any]			)    ->			Optional[Any]:



			'''simple docstring'''
			a__  :	Dict		 =			{
			    "upernet-swin-tiny": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210531_112542-e380ad3e.pth",
			    "upernet-swin-small": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210526_192015-ee2fff1c.pth",
			    "upernet-swin-base": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K_20210531_125459-429057bf.pth",
			    "upernet-swin-large": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k_20220318_091743-9ba68901.pth",
			}
			a__  :	Any		 =			model_name_to_url[model_name]
			a__  :	Optional[Any]		 =			torch.hub.load_state_dict_from_url(lowerCAmelCase__							,			map_location="cpu"							,			file_name=lowerCAmelCase__			)[
			    "state_dict"
			]

			for name, param in state_dict.items():
						print(lowerCAmelCase__							,			param.shape			)

			a__  :	List[str]		 =			get_upernet_config(lowerCAmelCase__			)
			a__  :	str		 =			UperNetForSemanticSegmentation(lowerCAmelCase__			)
			model.eval()

			# replace "bn" => "batch_norm"
			for key in state_dict.copy().keys():
						a__  :	Optional[Any]		 =			state_dict.pop(lowerCAmelCase__			)
						if "bn" in key:
									a__  :	Optional[Any]		 =			key.replace("bn"							,			"batch_norm"			)
						a__  :	Optional[int]		 =			val

			# rename keys
			a__  :	List[Any]		 =			create_rename_keys(lowerCAmelCase__			)
			for src, dest in rename_keys:
						rename_key(lowerCAmelCase__							,			lowerCAmelCase__							,			lowerCAmelCase__			)
			read_in_q_k_v(lowerCAmelCase__							,			config.backbone_config			)

			# fix downsample parameters
			for key, value in state_dict.items():
						if "downsample" in key:
									if "reduction" in key:
												a__  :	int		 =			reverse_correct_unfold_reduction_order(lowerCAmelCase__			)
									if "norm" in key:
												a__  :	List[str]		 =			reverse_correct_unfold_norm_order(lowerCAmelCase__			)

			model.load_state_dict(lowerCAmelCase__			)

			# verify on image
			a__  :	Optional[Any]		 =			"https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg"
			a__  :	Tuple		 =			Image.open(requests.get(lowerCAmelCase__							,			stream=lowerCAmelCase__			).raw			).convert("RGB"			)

			a__  :	Dict		 =			SegformerImageProcessor()
			a__  :	int		 =			processor(lowerCAmelCase__							,			return_tensors="pt"			).pixel_values

			with torch.no_grad():
						a__  :	Optional[int]		 =			model(lowerCAmelCase__			)
						a__  :	Any		 =			outputs.logits

			print(logits.shape			)
			print("First values of logits:"							,			logits[0, 0, :3, :3]			)
			# assert values
			if model_name == "upernet-swin-tiny":
						a__  :	int		 =			torch.tensor(
						    [[-7.5958, -7.5958, -7.4302], [-7.5958, -7.5958, -7.4302], [-7.4797, -7.4797, -7.3068]]			)
			elif model_name == "upernet-swin-small":
						a__  :	str		 =			torch.tensor(
						    [[-7.1921, -7.1921, -6.9532], [-7.1921, -7.1921, -6.9532], [-7.0908, -7.0908, -6.8534]]			)
			elif model_name == "upernet-swin-base":
						a__  :	Optional[Any]		 =			torch.tensor(
						    [[-6.5851, -6.5851, -6.4330], [-6.5851, -6.5851, -6.4330], [-6.4763, -6.4763, -6.3254]]			)
			elif model_name == "upernet-swin-large":
						a__  :	str		 =			torch.tensor(
						    [[-7.5297, -7.5297, -7.3802], [-7.5297, -7.5297, -7.3802], [-7.4044, -7.4044, -7.2586]]			)
			print("Logits:"							,			outputs.logits[0, 0, :3, :3]			)
			assert torch.allclose(outputs.logits[0, 0, :3, :3]							,			lowerCAmelCase__							,			atol=1E-4			)
			print("Looks ok!"			)

			if pytorch_dump_folder_path is not None:
						print(F"Saving model {model_name} to {pytorch_dump_folder_path}"			)
						model.save_pretrained(lowerCAmelCase__			)
						print(F"Saving processor to {pytorch_dump_folder_path}"			)
						processor.save_pretrained(lowerCAmelCase__			)

			if push_to_hub:
						print(F"Pushing model and processor for {model_name} to hub"			)
						model.push_to_hub(F"openmmlab/{model_name}"			)
						processor.push_to_hub(F"openmmlab/{model_name}"			)


if __name__ == "__main__":
		__UpperCAmelCase						=			argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '''--model_name''',
		    default='''upernet-swin-tiny''',
		    type=str,
		    choices=[f"upernet-swin-{size}" for size in ['''tiny''', '''small''', '''base''', '''large''']],
		    help='''Name of the Swin + UperNet model you\'d like to convert.''',
		)
		parser.add_argument(
		    '''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model directory.'''
		)
		parser.add_argument(
		    '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.'''
		)

		__UpperCAmelCase						=			parser.parse_args()
		convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)






"""simple docstring"""






import numpy as np

from transformers import BatchFeature
from transformers.testing_utils import require_tf, require_torch

from .test_feature_extraction_common import FeatureExtractionSavingTestMixin



class 					__UpperCAmelCase							(						_UpperCamelCase  ):
				# to overwrite at feature extractactor specific tests
				__lowerCamelCase				:	int				     =				None
				__lowerCamelCase				:	Tuple				     =				None
				@property
				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	Optional[int]:



							'''simple docstring'''



							return self.feat_extract_tester.prepare_feat_extract_dict()
				def       UpperCAmelCase      (		self    :			Optional[Any]    )	->	Dict:



							'''simple docstring'''



							a__  :	List[str]		 =			self.feature_extraction_class(**self.feat_extract_dict    )
							self.assertTrue(hasattr(a_	, "feature_size"    )    )
							self.assertTrue(hasattr(a_	, "sampling_rate"    )    )
							self.assertTrue(hasattr(a_	, "padding_value"    )    )
				def       UpperCAmelCase      (		self    :			List[str]    )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	Any		 =			self.feat_extract_tester.prepare_inputs_for_common()
							a__  :	Union[str, Any]		 =			self.feature_extraction_class(**self.feat_extract_dict    )
							a__  :	List[str]		 =			feat_extract.model_input_names[0]

							a__  :	Tuple		 =			BatchFeature({input_name: speech_inputs}    )

							self.assertTrue(all(len(a_    ) == len(a_    ) for x, y in zip(a_	, processed_features[input_name]    )    )    )

							a__  :	Any		 =			self.feat_extract_tester.prepare_inputs_for_common(equal_length=a_    )
							a__  :	List[Any]		 =			BatchFeature({input_name: speech_inputs}	, tensor_type="np"    )

							a__  :	Union[str, Any]		 =			processed_features[input_name]

							if len(batch_features_input.shape    ) < 3:
										a__  :	List[Any]		 =			batch_features_input[:, :, None]

							self.assertTrue(
							    batch_features_input.shape
							    == (self.feat_extract_tester.batch_size, len(speech_inputs[0]    ), self.feat_extract_tester.feature_size)    )
				@require_torch
				def       UpperCAmelCase      (		self    :			Optional[Any]    )	->	Tuple:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			self.feat_extract_tester.prepare_inputs_for_common(equal_length=a_    )
							a__  :	List[Any]		 =			self.feature_extraction_class(**self.feat_extract_dict    )
							a__  :	Optional[Any]		 =			feat_extract.model_input_names[0]

							a__  :	Any		 =			BatchFeature({input_name: speech_inputs}	, tensor_type="pt"    )

							a__  :	Optional[int]		 =			processed_features[input_name]

							if len(batch_features_input.shape    ) < 3:
										a__  :	Any		 =			batch_features_input[:, :, None]

							self.assertTrue(
							    batch_features_input.shape
							    == (self.feat_extract_tester.batch_size, len(speech_inputs[0]    ), self.feat_extract_tester.feature_size)    )
				@require_tf
				def       UpperCAmelCase      (		self    :			Optional[Any]    )	->	str:



							'''simple docstring'''



							a__  :	Optional[int]		 =			self.feat_extract_tester.prepare_inputs_for_common(equal_length=a_    )
							a__  :	Optional[int]		 =			self.feature_extraction_class(**self.feat_extract_dict    )
							a__  :	str		 =			feat_extract.model_input_names[0]

							a__  :	List[str]		 =			BatchFeature({input_name: speech_inputs}	, tensor_type="tf"    )

							a__  :	Tuple		 =			processed_features[input_name]

							if len(batch_features_input.shape    ) < 3:
										a__  :	List[str]		 =			batch_features_input[:, :, None]

							self.assertTrue(
							    batch_features_input.shape
							    == (self.feat_extract_tester.batch_size, len(speech_inputs[0]    ), self.feat_extract_tester.feature_size)    )
				def       UpperCAmelCase      (		self    :			Dict	, a_    :			Optional[Any]=False    )	->	List[Any]:



							'''simple docstring'''



							def _inputs_have_equal_length(a_    :			int    ):
										a__  :	Any		 =			len(input[0]    )
										for input_slice in input[1:]:
													if len(a_    ) != length:
																return False
										return True

							def _inputs_are_equal(a_    :			List[Any]	, a_    :			str    ):
										if len(a_    ) != len(a_    ):
													return False

										for input_slice_a, input_slice_a in zip(a_	, a_    ):
													if not np.allclose(np.asarray(a_    )	, np.asarray(a_    )	, atol=1E-3    ):
																return False
										return True

							a__  :	int		 =			self.feature_extraction_class(**self.feat_extract_dict    )
							a__  :	int		 =			self.feat_extract_tester.prepare_inputs_for_common(numpify=a_    )
							a__  :	Tuple		 =			feat_extract.model_input_names[0]

							a__  :	List[str]		 =			BatchFeature({input_name: speech_inputs}    )

							a__  :	Optional[int]		 =			self.feat_extract_tester.seq_length_diff
							a__  :	Optional[Any]		 =			self.feat_extract_tester.max_seq_length + pad_diff
							a__  :	Union[str, Any]		 =			self.feat_extract_tester.min_seq_length
							a__  :	Tuple		 =			self.feat_extract_tester.batch_size
							a__  :	str		 =			self.feat_extract_tester.feature_size

							# test padding for List[int] + numpy
							a__  :	Any		 =			feat_extract.pad(a_	, padding=a_    )
							a__  :	List[str]		 =			input_a[input_name]

							a__  :	int		 =			feat_extract.pad(a_	, padding="longest"    )
							a__  :	Optional[Any]		 =			input_a[input_name]

							a__  :	Optional[Any]		 =			feat_extract.pad(a_	, padding="max_length"	, max_length=len(speech_inputs[-1]    )    )
							a__  :	Any		 =			input_a[input_name]

							a__  :	Tuple		 =			feat_extract.pad(a_	, padding="longest"	, return_tensors="np"    )
							a__  :	Optional[Any]		 =			input_a[input_name]

							# max_length parameter has to be provided when setting `padding="max_length"`
							with self.assertRaises(a_    ):
										feat_extract.pad(a_	, padding="max_length"    )[input_name]

							a__  :	Tuple		 =			feat_extract.pad(
							    a_	, padding="max_length"	, max_length=a_	, return_tensors="np"    )
							a__  :	Optional[Any]		 =			input_a[input_name]

							self.assertFalse(_inputs_have_equal_length(a_    )    )
							self.assertTrue(_inputs_have_equal_length(a_    )    )
							self.assertTrue(_inputs_have_equal_length(a_    )    )
							self.assertTrue(_inputs_are_equal(a_	, a_    )    )
							self.assertTrue(len(input_a[0]    ) == pad_min_length    )
							self.assertTrue(len(input_a[1]    ) == pad_min_length + pad_diff    )
							self.assertTrue(input_a.shape[:2] == (batch_size, len(input_a[0]    ))    )
							self.assertTrue(input_a.shape[:2] == (batch_size, pad_max_length)    )

							if feature_size > 1:
										self.assertTrue(input_a.shape[2] == input_a.shape[2] == feature_size    )

							# test padding for `pad_to_multiple_of` for List[int] + numpy
							a__  :	Optional[int]		 =			feat_extract.pad(a_	, pad_to_multiple_of=10    )
							a__  :	Optional[Any]		 =			input_a[input_name]

							a__  :	List[str]		 =			feat_extract.pad(a_	, padding="longest"	, pad_to_multiple_of=10    )
							a__  :	Dict		 =			input_a[input_name]

							a__  :	List[Any]		 =			feat_extract.pad(
							    a_	, padding="max_length"	, pad_to_multiple_of=10	, max_length=a_    )
							a__  :	Tuple		 =			input_a[input_name]

							a__  :	List[Any]		 =			feat_extract.pad(
							    a_	, padding="max_length"	, pad_to_multiple_of=10	, max_length=a_	, return_tensors="np"	, )
							a__  :	Optional[int]		 =			input_a[input_name]

							self.assertTrue(all(len(a_    ) % 10 == 0 for x in input_a    )    )
							self.assertTrue(_inputs_are_equal(a_	, a_    )    )

							a__  :	List[str]		 =			pad_max_length if pad_max_length % 10 == 0 else (pad_max_length // 10 + 1) * 10
							self.assertTrue(all(len(a_    ) == expected_mult_pad_length for x in input_a    )    )
							self.assertEqual(input_a.shape[:2]	, (batch_size, expected_mult_pad_length)    )

							if feature_size > 1:
										self.assertTrue(input_a.shape[2] == feature_size    )

							# Check padding value is correct
							a__  :	Optional[int]		 =			(np.ones(self.feat_extract_tester.feature_size    ) * feat_extract.padding_value).sum()
							self.assertTrue(
							    abs(np.asarray(input_a[0]    )[pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length)    )
							    < 1E-3    )
							self.assertTrue(
							    abs(
							        np.asarray(input_a[1]    )[pad_min_length + pad_diff :].sum()
							        - padding_vector_sum * (pad_max_length - pad_min_length - pad_diff)    )
							    < 1E-3    )
							self.assertTrue(
							    abs(
							        np.asarray(input_a[2]    )[pad_min_length + 2 * pad_diff :].sum()
							        - padding_vector_sum * (pad_max_length - pad_min_length - 2 * pad_diff)    )
							    < 1E-3    )
							self.assertTrue(
							    abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length)    ) < 1E-3    )
							self.assertTrue(
							    abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (expected_mult_pad_length - pad_min_length)    )
							    < 1E-3    )
				def       UpperCAmelCase      (		self    :			Dict	, a_    :			Optional[int]=False    )	->	List[str]:



							'''simple docstring'''



							def _inputs_have_equal_length(a_    :			List[Any]    ):
										a__  :	List[str]		 =			len(input[0]    )
										for input_slice in input[1:]:
													if len(a_    ) != length:
																return False
										return True

							def _inputs_are_equal(a_    :			Optional[int]	, a_    :			List[Any]    ):
										if len(a_    ) != len(a_    ):
													return False

										for input_slice_a, input_slice_a in zip(a_	, a_    ):
													if not np.allclose(np.asarray(a_    )	, np.asarray(a_    )	, atol=1E-3    ):
																return False
										return True

							a__  :	Dict		 =			self.feature_extraction_class(**self.feat_extract_dict    )
							a__  :	Union[str, Any]		 =			self.feat_extract_tester.prepare_inputs_for_common(numpify=a_    )
							a__  :	List[str]		 =			feat_extract.model_input_names[0]

							a__  :	str		 =			BatchFeature({input_name: speech_inputs}    )

							# truncate to smallest
							a__  :	int		 =			feat_extract.pad(
							    a_	, padding="max_length"	, max_length=len(speech_inputs[0]    )	, truncation=a_    )
							a__  :	Optional[Any]		 =			input_a[input_name]

							a__  :	int		 =			feat_extract.pad(a_	, padding="max_length"	, max_length=len(speech_inputs[0]    )    )
							a__  :	List[Any]		 =			input_a[input_name]

							self.assertTrue(_inputs_have_equal_length(a_    )    )
							self.assertFalse(_inputs_have_equal_length(a_    )    )

							# truncate to smallest with np
							a__  :	Union[str, Any]		 =			feat_extract.pad(
							    a_	, padding="max_length"	, max_length=len(speech_inputs[0]    )	, return_tensors="np"	, truncation=a_	, )
							a__  :	Tuple		 =			input_a[input_name]

							a__  :	str		 =			feat_extract.pad(
							    a_	, padding="max_length"	, max_length=len(speech_inputs[0]    )	, return_tensors="np"    )
							a__  :	Union[str, Any]		 =			input_a[input_name]

							self.assertTrue(_inputs_have_equal_length(a_    )    )
							self.assertTrue(input_a.shape[1] == len(speech_inputs[0]    )    )

							# since truncation forces padding to be smaller than longest input
							# function can't return `np.ndarray`, but has to return list
							self.assertFalse(_inputs_have_equal_length(a_    )    )

							# truncate to middle
							a__  :	str		 =			feat_extract.pad(
							    a_	, padding="max_length"	, max_length=len(speech_inputs[1]    )	, truncation=a_	, return_tensors="np"	, )
							a__  :	List[Any]		 =			input_a[input_name]

							a__  :	List[str]		 =			feat_extract.pad(
							    a_	, padding="max_length"	, max_length=len(speech_inputs[1]    )	, truncation=a_    )
							a__  :	str		 =			input_a[input_name]

							a__  :	Any		 =			feat_extract.pad(
							    a_	, padding="max_length"	, max_length=len(speech_inputs[1]    )	, return_tensors="np"    )
							a__  :	Optional[int]		 =			input_a[input_name]

							self.assertTrue(input_a.shape[1] == len(speech_inputs[1]    )    )
							self.assertTrue(_inputs_have_equal_length(a_    )    )
							self.assertTrue(_inputs_have_equal_length(a_    )    )
							self.assertTrue(_inputs_are_equal(a_	, a_    )    )

							# since truncation forces padding to be smaller than longest input
							# function can't return `np.ndarray`, but has to return list
							self.assertFalse(_inputs_have_equal_length(a_    )    )
							self.assertTrue(len(input_a[-1]    ) == len(speech_inputs[-1]    )    )

							# padding has to be max_length when setting `truncation=True`
							with self.assertRaises(a_    ):
										feat_extract.pad(a_	, truncation=a_    )[input_name]

							# padding has to be max_length when setting `truncation=True`
							with self.assertRaises(a_    ):
										feat_extract.pad(a_	, padding="longest"	, truncation=a_    )[input_name]

							# padding has to be max_length when setting `truncation=True`
							with self.assertRaises(a_    ):
										feat_extract.pad(a_	, padding="longest"	, truncation=a_    )[input_name]

							# max_length parameter has to be provided when setting `truncation=True` and padding="max_length"
							with self.assertRaises(a_    ):
										feat_extract.pad(a_	, padding="max_length"	, truncation=a_    )[input_name]

							# test truncation for `pad_to_multiple_of` for List[int] + numpy
							a__  :	Tuple		 =			12
							a__  :	Dict		 =			feat_extract.pad(
							    a_	, padding="max_length"	, max_length=len(speech_inputs[0]    )	, pad_to_multiple_of=a_	, truncation=a_	, )
							a__  :	Optional[int]		 =			input_a[input_name]

							a__  :	List[Any]		 =			feat_extract.pad(
							    a_	, padding="max_length"	, max_length=len(speech_inputs[0]    )	, pad_to_multiple_of=a_	, )
							a__  :	str		 =			input_a[input_name]

							# retrieve expected_length as multiple of pad_to_multiple_of
							a__  :	Dict		 =			len(speech_inputs[0]    )
							if expected_length % pad_to_multiple_of != 0:
										a__  :	Tuple		 =			((len(speech_inputs[0]    ) // pad_to_multiple_of) + 1) * pad_to_multiple_of

							self.assertTrue(len(input_a[0]    ) == expected_length    )
							self.assertTrue(_inputs_have_equal_length(a_    )    )
							self.assertFalse(_inputs_have_equal_length(a_    )    )
				def       UpperCAmelCase      (		self    :			List[str]    )	->	List[str]:



							'''simple docstring'''



							self._check_padding(numpify=a_    )
				def       UpperCAmelCase      (		self    :			Tuple    )	->	Tuple:



							'''simple docstring'''



							self._check_padding(numpify=a_    )
				def       UpperCAmelCase      (		self    :			int    )	->	Optional[Any]:



							'''simple docstring'''



							self._check_truncation(numpify=a_    )
				def       UpperCAmelCase      (		self    :			str    )	->	int:



							'''simple docstring'''



							self._check_truncation(numpify=a_    )
				@require_torch
				def       UpperCAmelCase      (		self    :			List[Any]    )	->	Tuple:



							'''simple docstring'''



							a__  :	Tuple		 =			self.feature_extraction_class(**self.feat_extract_dict    )
							a__  :	Tuple		 =			self.feat_extract_tester.prepare_inputs_for_common()
							a__  :	int		 =			feat_extract.model_input_names[0]

							a__  :	str		 =			BatchFeature({input_name: speech_inputs}    )

							a__  :	int		 =			feat_extract.pad(a_	, padding="longest"	, return_tensors="np"    )[input_name]
							a__  :	str		 =			feat_extract.pad(a_	, padding="longest"	, return_tensors="pt"    )[input_name]

							self.assertTrue(abs(input_np.astype(np.floataa    ).sum() - input_pt.numpy().astype(np.floataa    ).sum()    ) < 1E-2    )
				@require_tf
				def       UpperCAmelCase      (		self    :			Optional[Any]    )	->	str:



							'''simple docstring'''



							a__  :	int		 =			self.feature_extraction_class(**self.feat_extract_dict    )
							a__  :	Optional[int]		 =			self.feat_extract_tester.prepare_inputs_for_common()
							a__  :	Optional[Any]		 =			feat_extract.model_input_names[0]

							a__  :	Dict		 =			BatchFeature({input_name: speech_inputs}    )

							a__  :	List[Any]		 =			feat_extract.pad(a_	, padding="longest"	, return_tensors="np"    )[input_name]
							a__  :	List[str]		 =			feat_extract.pad(a_	, padding="longest"	, return_tensors="tf"    )[input_name]

							self.assertTrue(abs(input_np.astype(np.floataa    ).sum() - input_tf.numpy().astype(np.floataa    ).sum()    ) < 1E-2    )
				def       UpperCAmelCase      (		self    :			Optional[Any]    )	->	List[str]:



							'''simple docstring'''



							a__  :	Union[str, Any]		 =			self.feat_extract_dict
							a__  :	Dict		 =			True
							a__  :	Dict		 =			self.feature_extraction_class(**a_    )
							a__  :	List[Any]		 =			self.feat_extract_tester.prepare_inputs_for_common()
							a__  :	List[Any]		 =			[len(a_    ) for x in speech_inputs]
							a__  :	List[str]		 =			feat_extract.model_input_names[0]

							a__  :	int		 =			BatchFeature({input_name: speech_inputs}    )

							a__  :	Tuple		 =			feat_extract.pad(a_	, padding="longest"	, return_tensors="np"    )
							self.assertIn("attention_mask"	, a_    )
							self.assertListEqual(list(processed.attention_mask.shape    )	, list(processed[input_name].shape[:2]    )    )
							self.assertListEqual(processed.attention_mask.sum(-1    ).tolist()	, a_    )






				def       UpperCAmelCase      (		self    :			List[Any]    )	->	List[Any]:



							'''simple docstring'''



							a__  :	List[str]		 =			self.feat_extract_dict
							a__  :	Any		 =			True
							a__  :	Optional[Any]		 =			self.feature_extraction_class(**a_    )
							a__  :	List[Any]		 =			self.feat_extract_tester.prepare_inputs_for_common()
							a__  :	Optional[int]		 =			[len(a_    ) for x in speech_inputs]
							a__  :	Tuple		 =			feat_extract.model_input_names[0]

							a__  :	int		 =			BatchFeature({input_name: speech_inputs}    )
							a__  :	Tuple		 =			min(a_    )

							a__  :	int		 =			feat_extract.pad(
							    a_	, padding="max_length"	, max_length=a_	, truncation=a_	, return_tensors="np"    )
							self.assertIn("attention_mask"	, a_    )
							self.assertListEqual(
							    list(processed_pad.attention_mask.shape    )	, [processed_pad[input_name].shape[0], max_length]    )
							self.assertListEqual(
							    processed_pad.attention_mask[:, :max_length].sum(-1    ).tolist()	, [max_length for x in speech_inputs]    )