





"""simple docstring"""


import argparse

import gdown
import numpy as np
import torch
from huggingface_hub import hf_hub_download

from transformers import (
    CLIPTokenizer,
    CLIPTokenizerFast,
    VideoMAEImageProcessor,
    XCLIPConfig,
    XCLIPModel,
    XCLIPProcessor,
    XCLIPTextConfig,
    XCLIPVisionConfig,
)



def 	lowercase_				(			__UpperCAmelCase    ,		__UpperCAmelCase				)						->    Tuple:
      lowerCAmelCase__      :			str   =	XCLIPTextConfig()

      # derive patch size from model name
      lowerCAmelCase__      :			Optional[Any]   =	model_name.find("""patch"""				)
      lowerCAmelCase__      :			int   =	int(model_name[start_idx + len("""patch"""				) : start_idx + len("""patch"""				) + 2]				)
      lowerCAmelCase__      :			int   =	XCLIPVisionConfig(patch_size=__UpperCAmelCase    ,		num_frames=__UpperCAmelCase				)

      if "large" in model_name:
            lowerCAmelCase__      :			Dict   =	768
            lowerCAmelCase__      :			Tuple   =	3072
            lowerCAmelCase__      :			List[Any]   =	12

            lowerCAmelCase__      :			Dict   =	1024
            lowerCAmelCase__      :			Any   =	4096
            lowerCAmelCase__      :			List[str]   =	16
            lowerCAmelCase__      :			List[Any]   =	24
            lowerCAmelCase__      :			List[Any]   =	768
            lowerCAmelCase__      :			Any   =	3072

      if model_name == "xclip-large-patch14-16-frames":
            lowerCAmelCase__      :			str   =	336

      lowerCAmelCase__      :			List[str]   =	XCLIPConfig.from_text_vision_configs(__UpperCAmelCase    ,		__UpperCAmelCase				)

      if "large" in model_name:
            lowerCAmelCase__      :			Optional[Any]   =	768

      return config



def 	lowercase_				(			__UpperCAmelCase				)						->    int:
      # text encoder
      if name == "token_embedding.weight":
            lowerCAmelCase__      :			int   =	name.replace("""token_embedding.weight"""    ,		"""text_model.embeddings.token_embedding.weight"""				)
      if name == "positional_embedding":
            lowerCAmelCase__      :			Optional[Any]   =	name.replace("""positional_embedding"""    ,		"""text_model.embeddings.position_embedding.weight"""				)
      if "ln_1" in name:
            lowerCAmelCase__      :			Any   =	name.replace("""ln_1"""    ,		"""layer_norm1"""				)
      if "ln_2" in name:
            lowerCAmelCase__      :			str   =	name.replace("""ln_2"""    ,		"""layer_norm2"""				)
      if "c_fc" in name:
            lowerCAmelCase__      :			Tuple   =	name.replace("""c_fc"""    ,		"""fc1"""				)
      if "c_proj" in name:
            lowerCAmelCase__      :			Any   =	name.replace("""c_proj"""    ,		"""fc2"""				)
      if name.startswith("""transformer.resblocks"""				):
            lowerCAmelCase__      :			str   =	name.replace("""transformer.resblocks"""    ,		"""text_model.encoder.layers"""				)
      if "attn.out_proj" in name and "message" not in name:
            lowerCAmelCase__      :			Optional[int]   =	name.replace("""attn.out_proj"""    ,		"""self_attn.out_proj"""				)
      if "ln_final" in name:
            lowerCAmelCase__      :			Optional[int]   =	name.replace("""ln_final"""    ,		"""text_model.final_layer_norm"""				)
      # visual encoder
      if name == "visual.class_embedding":
            lowerCAmelCase__      :			int   =	name.replace("""visual.class_embedding"""    ,		"""vision_model.embeddings.class_embedding"""				)
      if name == "visual.positional_embedding":
            lowerCAmelCase__      :			str   =	name.replace("""visual.positional_embedding"""    ,		"""vision_model.embeddings.position_embedding.weight"""				)
      if name.startswith("""visual.transformer.resblocks"""				):
            lowerCAmelCase__      :			Optional[int]   =	name.replace("""visual.transformer.resblocks"""    ,		"""vision_model.encoder.layers"""				)
      if "visual.conv1" in name:
            lowerCAmelCase__      :			Any   =	name.replace("""visual.conv1"""    ,		"""vision_model.embeddings.patch_embedding"""				)
      if "visual.ln_pre" in name:
            lowerCAmelCase__      :			int   =	name.replace("""visual.ln_pre"""    ,		"""vision_model.pre_layernorm"""				)
      if "visual.ln_post" in name:
            lowerCAmelCase__      :			List[str]   =	name.replace("""visual.ln_post"""    ,		"""vision_model.post_layernorm"""				)
      if "visual.proj" in name:
            lowerCAmelCase__      :			Dict   =	name.replace("""visual.proj"""    ,		"""visual_projection.weight"""				)
      if "text_projection" in name:
            lowerCAmelCase__      :			Union[str, Any]   =	name.replace("""text_projection"""    ,		"""text_projection.weight"""				)
      # things on top
      if "prompts_visual_proj" in name:
            lowerCAmelCase__      :			Any   =	name.replace("""prompts_visual_proj"""    ,		"""prompts_visual_projection"""				)
      if "prompts_visual_ln" in name:
            lowerCAmelCase__      :			Dict   =	name.replace("""prompts_visual_ln"""    ,		"""prompts_visual_layernorm"""				)
      # mit
      if name == "mit.positional_embedding":
            lowerCAmelCase__      :			str   =	name.replace("""positional"""    ,		"""position"""				)
      if name.startswith("""mit.resblocks"""				):
            lowerCAmelCase__      :			Dict   =	name.replace("""mit.resblocks"""    ,		"""mit.encoder.layers"""				)
      # prompts generator
      if name.startswith("""prompts_generator.norm"""				):
            lowerCAmelCase__      :			Optional[Any]   =	name.replace("""prompts_generator.norm"""    ,		"""prompts_generator.layernorm"""				)

      return name



def 	lowercase_				(			__UpperCAmelCase    ,		__UpperCAmelCase				)						->    List[str]:
      for key in orig_state_dict.copy().keys():
            lowerCAmelCase__      :			List[str]   =	orig_state_dict.pop(__UpperCAmelCase				)

            if "attn.in_proj" in key:
                  lowerCAmelCase__      :			Dict   =	key.split("""."""				)
                  if key.startswith("""visual"""				):
                        lowerCAmelCase__      :			Optional[Any]   =	key_split[3]
                        lowerCAmelCase__      :			Union[str, Any]   =	config.vision_config.hidden_size
                        if "message_attn" in key:
                              if "weight" in key:
                                    lowerCAmelCase__      :			Optional[Any]   =	val[
                                        :dim, :
                                    ]
                                    lowerCAmelCase__      :			Union[str, Any]   =	val[
                                        dim : dim * 2, :
                                    ]
                                    lowerCAmelCase__      :			List[str]   =	val[
                                        -dim:, :
                                    ]
                              else:
                                    lowerCAmelCase__      :			List[str]   =	val[
                                        :dim
                                    ]
                                    lowerCAmelCase__      :			Any   =	val[
                                        dim : dim * 2
                                    ]
                                    lowerCAmelCase__      :			List[str]   =	val[
                                        -dim:
                                    ]
                        else:
                              if "weight" in key:
                                    lowerCAmelCase__      :			int   =	val[
                                        :dim, :
                                    ]
                                    lowerCAmelCase__      :			List[str]   =	val[
                                        dim : dim * 2, :
                                    ]
                                    lowerCAmelCase__      :			int   =	val[
                                        -dim:, :
                                    ]
                              else:
                                    lowerCAmelCase__      :			int   =	val[:dim]
                                    lowerCAmelCase__      :			int   =	val[
                                        dim : dim * 2
                                    ]
                                    lowerCAmelCase__      :			List[str]   =	val[-dim:]
                  elif key.startswith("""mit"""				):
                        lowerCAmelCase__      :			List[Any]   =	key_split[2]
                        lowerCAmelCase__      :			Any   =	config.vision_config.mit_hidden_size
                        if "weight" in key:
                              lowerCAmelCase__      :			Optional[Any]   =	val[:dim, :]
                              lowerCAmelCase__      :			Optional[int]   =	val[dim : dim * 2, :]
                              lowerCAmelCase__      :			Optional[int]   =	val[-dim:, :]
                        else:
                              lowerCAmelCase__      :			List[str]   =	val[:dim]
                              lowerCAmelCase__      :			int   =	val[dim : dim * 2]
                              lowerCAmelCase__      :			List[str]   =	val[-dim:]
                  else:
                        lowerCAmelCase__      :			int   =	key_split[2]
                        lowerCAmelCase__      :			List[str]   =	config.text_config.hidden_size
                        if "weight" in key:
                              lowerCAmelCase__      :			int   =	val[:dim, :]
                              lowerCAmelCase__      :			List[str]   =	val[
                                  dim : dim * 2, :
                              ]
                              lowerCAmelCase__      :			List[str]   =	val[-dim:, :]
                        else:
                              lowerCAmelCase__      :			Optional[int]   =	val[:dim]
                              lowerCAmelCase__      :			List[Any]   =	val[
                                  dim : dim * 2
                              ]
                              lowerCAmelCase__      :			Any   =	val[-dim:]
            else:
                  lowerCAmelCase__      :			Optional[Any]   =	rename_key(__UpperCAmelCase				)
                  if new_key_name in ["visual_projection.weight", "text_projection.weight"]:
                        lowerCAmelCase__      :			Union[str, Any]   =	val.T
                  lowerCAmelCase__      :			str   =	val

      return orig_state_dict



def 	lowercase_				(			__UpperCAmelCase				)						->    Optional[Any]:
      if num_frames == 8:
            lowerCAmelCase__      :			Union[str, Any]   =	"""eating_spaghetti_8_frames.npy"""
      elif num_frames == 16:
            lowerCAmelCase__      :			Dict   =	"""eating_spaghetti.npy"""
      elif num_frames == 32:
            lowerCAmelCase__      :			Optional[Any]   =	"""eating_spaghetti_32_frames.npy"""
      lowerCAmelCase__      :			str   =	hf_hub_download(
          repo_id="""hf-internal-testing/spaghetti-video"""    ,		filename=__UpperCAmelCase    ,		repo_type="""dataset"""    ,		)
      lowerCAmelCase__      :			Any   =	np.load(__UpperCAmelCase				)
      return list(__UpperCAmelCase				)



def 	lowercase_				(			__UpperCAmelCase    ,		__UpperCAmelCase=None    ,		__UpperCAmelCase=False				)						->    Dict:
      lowerCAmelCase__      :			Union[str, Any]   =	{
          # fully supervised kinetics-400 checkpoints
          """xclip-base-patch32""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth""",
          """xclip-base-patch32-16-frames""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth"""
          ),
          """xclip-base-patch16""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth""",
          """xclip-base-patch16-16-frames""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth"""
          ),
          """xclip-large-patch14""": """https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb""",
          """xclip-large-patch14-16-frames""": """https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f""",
          # fully supervised kinetics-600 checkpoints
          """xclip-base-patch16-kinetics-600""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth"""
          ),
          """xclip-base-patch16-kinetics-600-16-frames""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth"""
          ),
          """xclip-large-patch14-kinetics-600""": """https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be""",
          # few shot
          """xclip-base-patch16-hmdb-2-shot""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth"""
          ),
          """xclip-base-patch16-hmdb-4-shot""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth"""
          ),
          """xclip-base-patch16-hmdb-8-shot""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth"""
          ),
          """xclip-base-patch16-hmdb-16-shot""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth"""
          ),
          """xclip-base-patch16-ucf-2-shot""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth"""
          ),
          """xclip-base-patch16-ucf-4-shot""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth"""
          ),
          """xclip-base-patch16-ucf-8-shot""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth"""
          ),
          """xclip-base-patch16-ucf-16-shot""": (
              """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth"""
          ),
          # zero shot
          """xclip-base-patch16-zero-shot""": """https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth""",
      }

      lowerCAmelCase__      :			Union[str, Any]   =	model_to_url[model_name]
      lowerCAmelCase__      :			str   =	8
      if "16-frames" in model_name:
            lowerCAmelCase__      :			Optional[Any]   =	16
      elif "shot" in model_name:
            lowerCAmelCase__      :			List[str]   =	32

      lowerCAmelCase__      :			int   =	get_xclip_config(__UpperCAmelCase    ,		__UpperCAmelCase				)
      lowerCAmelCase__      :			Union[str, Any]   =	XCLIPModel(__UpperCAmelCase				)
      model.eval()

      if "drive" in checkpoint_url:
            lowerCAmelCase__      :			Union[str, Any]   =	"""pytorch_model.bin"""
            gdown.cached_download(__UpperCAmelCase    ,		__UpperCAmelCase    ,		quiet=__UpperCAmelCase				)
            lowerCAmelCase__      :			Union[str, Any]   =	torch.load(__UpperCAmelCase    ,		map_location="""cpu"""				)["""model"""]
      else:
            lowerCAmelCase__      :			Any   =	torch.hub.load_state_dict_from_url(__UpperCAmelCase				)["""model"""]

      lowerCAmelCase__      :			Tuple   =	convert_state_dict(__UpperCAmelCase    ,		__UpperCAmelCase				)

      lowerCAmelCase__      :			Any   =	XCLIPModel(__UpperCAmelCase				)
      lowerCAmelCase__      :			int   =	model.load_state_dict(__UpperCAmelCase    ,		strict=__UpperCAmelCase				)
      assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"]
      model.eval()

      lowerCAmelCase__      :			Optional[Any]   =	336 if model_name == """xclip-large-patch14-16-frames""" else 224
      lowerCAmelCase__      :			List[Any]   =	VideoMAEImageProcessor(size=__UpperCAmelCase				)
      lowerCAmelCase__      :			int   =	CLIPTokenizer.from_pretrained("""openai/clip-vit-base-patch32"""				)
      lowerCAmelCase__      :			str   =	CLIPTokenizerFast.from_pretrained("""openai/clip-vit-base-patch32"""				)
      lowerCAmelCase__      :			Union[str, Any]   =	XCLIPProcessor(image_processor=__UpperCAmelCase    ,		tokenizer=__UpperCAmelCase				)

      lowerCAmelCase__      :			int   =	prepare_video(__UpperCAmelCase				)
      lowerCAmelCase__      :			Optional[Any]   =	processor(
          text=["""playing sports""", """eating spaghetti""", """go shopping"""]    ,		videos=__UpperCAmelCase    ,		return_tensors="""pt"""    ,		padding=__UpperCAmelCase				)

      print("""Shape of pixel values:"""    ,		inputs.pixel_values.shape				)

      with torch.no_grad():
            lowerCAmelCase__      :			str   =	model(**__UpperCAmelCase				)

      # Verify outputs
      lowerCAmelCase__      :			Dict   =	outputs.logits_per_video
      lowerCAmelCase__      :			int   =	logits_per_video.softmax(dim=1				)
      print("""Probs:"""    ,		__UpperCAmelCase				)
      # kinetics-400
      if model_name == "xclip-base-patch32":
            lowerCAmelCase__      :			Dict   =	torch.tensor([[0.0019, 0.9951, 0.0030]]				)
      elif model_name == "xclip-base-patch32-16-frames":
            lowerCAmelCase__      :			List[str]   =	torch.tensor([[7.09_99E-04, 9.98_83E-01, 4.55_80E-04]]				)
      elif model_name == "xclip-base-patch16":
            lowerCAmelCase__      :			Optional[Any]   =	torch.tensor([[0.0083, 0.9681, 0.0236]]				)
      elif model_name == "xclip-base-patch16-16-frames":
            lowerCAmelCase__      :			int   =	torch.tensor([[7.69_37E-04, 9.97_28E-01, 1.94_73E-03]]				)
      elif model_name == "xclip-large-patch14":
            lowerCAmelCase__      :			Dict   =	torch.tensor([[0.0062, 0.9864, 0.0075]]				)
      elif model_name == "xclip-large-patch14-16-frames":
            lowerCAmelCase__      :			Dict   =	torch.tensor([[3.38_77E-04, 9.99_37E-01, 2.88_88E-04]]				)
      # kinetics-600
      elif model_name == "xclip-base-patch16-kinetics-600":
            lowerCAmelCase__      :			int   =	torch.tensor([[0.0555, 0.8914, 0.0531]]				)
      elif model_name == "xclip-base-patch16-kinetics-600-16-frames":
            lowerCAmelCase__      :			Tuple   =	torch.tensor([[3.85_54E-04, 9.99_29E-01, 3.27_54E-04]]				)
      elif model_name == "xclip-large-patch14-kinetics-600":
            lowerCAmelCase__      :			Union[str, Any]   =	torch.tensor([[0.0036, 0.9920, 0.0045]]				)
      # few shot
      elif model_name == "xclip-base-patch16-hmdb-2-shot":
            lowerCAmelCase__      :			Any   =	torch.tensor([[7.18_90E-06, 9.99_94E-01, 5.65_59E-05]]				)
      elif model_name == "xclip-base-patch16-hmdb-4-shot":
            lowerCAmelCase__      :			Union[str, Any]   =	torch.tensor([[1.03_20E-05, 9.99_93E-01, 6.24_35E-05]]				)
      elif model_name == "xclip-base-patch16-hmdb-8-shot":
            lowerCAmelCase__      :			Optional[int]   =	torch.tensor([[4.13_77E-06, 9.99_90E-01, 9.83_86E-05]]				)
      elif model_name == "xclip-base-patch16-hmdb-16-shot":
            lowerCAmelCase__      :			Dict   =	torch.tensor([[4.13_47E-05, 9.99_62E-01, 3.34_11E-04]]				)
      elif model_name == "xclip-base-patch16-ucf-2-shot":
            lowerCAmelCase__      :			Optional[Any]   =	torch.tensor([[8.58_57E-05, 9.99_28E-01, 6.32_91E-04]]				)
      elif model_name == "xclip-base-patch16-ucf-4-shot":
            lowerCAmelCase__      :			List[str]   =	torch.tensor([[8.58_57E-05, 9.99_28E-01, 6.32_91E-04]]				)
      elif model_name == "xclip-base-patch16-ucf-8-shot":
            lowerCAmelCase__      :			Optional[int]   =	torch.tensor([[0.0027, 0.9904, 0.0070]]				)
      elif model_name == "xclip-base-patch16-ucf-16-shot":
            lowerCAmelCase__      :			Tuple   =	torch.tensor([[9.82_19E-04, 9.95_93E-01, 3.08_63E-03]]				)
      # zero shot
      elif model_name == "xclip-base-patch16-zero-shot":
            lowerCAmelCase__      :			Any   =	torch.tensor([[3.50_82E-04, 9.97_85E-01, 1.79_66E-03]]				)
      else:
            raise ValueError(f"""Model name {model_name} not supported"""				)
      assert torch.allclose(__UpperCAmelCase    ,		__UpperCAmelCase    ,		atol=1E-3				)
      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(__UpperCAmelCase				)

      if push_to_hub:
            print("""Pushing model, processor and slow tokenizer files to the hub..."""				)
            model.push_to_hub(__UpperCAmelCase    ,		organization="""nielsr"""				)
            processor.push_to_hub(__UpperCAmelCase    ,		organization="""nielsr"""				)
            slow_tokenizer.push_to_hub(__UpperCAmelCase    ,		organization="""nielsr"""				)


if __name__ == "__main__":
  _A													=      argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      """--model_name""",
      default="""xclip-base-patch32""",
      type=str,
      help="""Name of the model.""",
  )
  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."""
  )

  _A													=      parser.parse_args()
  convert_xclip_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 _lowerCamelCase    (					a_						):
   # to overwrite at feature extractactor specific tests
   _lowerCamelCase    :Optional[int]     				=     None
   _lowerCamelCase    :List[Any]     				=     None






   @property
   def 	_lowerCAmelCase   (		self       :		int   )			->						Tuple:

         """simple docstring"""
         return self.feat_extract_tester.prepare_feat_extract_dict()






   def 	_lowerCAmelCase   (		self       :		Tuple   )			->						List[Any]:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	self.feature_extraction_class(**self.feat_extract_dict   )
         self.assertTrue(hasattr(UpperCamelCase   ,     """feature_size"""   )   )
         self.assertTrue(hasattr(UpperCamelCase   ,     """sampling_rate"""   )   )
         self.assertTrue(hasattr(UpperCamelCase   ,     """padding_value"""   )   )






   def 	_lowerCAmelCase   (		self       :		Any   )			->						int:

         """simple docstring"""
         lowerCAmelCase__      :			Dict   =	self.feat_extract_tester.prepare_inputs_for_common()
         lowerCAmelCase__      :			List[Any]   =	self.feature_extraction_class(**self.feat_extract_dict   )
         lowerCAmelCase__      :			Union[str, Any]   =	feat_extract.model_input_names[0]

         lowerCAmelCase__      :			Any   =	BatchFeature({input_name: speech_inputs}   )

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

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

         lowerCAmelCase__      :			Dict   =	processed_features[input_name]

         if len(batch_features_input.shape   ) < 3:
               lowerCAmelCase__      :			int   =	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 	_lowerCAmelCase   (		self       :		Tuple   )			->						Optional[Any]:

         """simple docstring"""
         lowerCAmelCase__      :			int   =	self.feat_extract_tester.prepare_inputs_for_common(equal_length=UpperCamelCase   )
         lowerCAmelCase__      :			str   =	self.feature_extraction_class(**self.feat_extract_dict   )
         lowerCAmelCase__      :			Optional[int]   =	feat_extract.model_input_names[0]

         lowerCAmelCase__      :			Union[str, Any]   =	BatchFeature({input_name: speech_inputs}   ,     tensor_type="""pt"""   )

         lowerCAmelCase__      :			str   =	processed_features[input_name]

         if len(batch_features_input.shape   ) < 3:
               lowerCAmelCase__      :			int   =	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 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						int:

         """simple docstring"""
         lowerCAmelCase__      :			List[str]   =	self.feat_extract_tester.prepare_inputs_for_common(equal_length=UpperCamelCase   )
         lowerCAmelCase__      :			str   =	self.feature_extraction_class(**self.feat_extract_dict   )
         lowerCAmelCase__      :			Dict   =	feat_extract.model_input_names[0]

         lowerCAmelCase__      :			Tuple   =	BatchFeature({input_name: speech_inputs}   ,     tensor_type="""tf"""   )

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

         if len(batch_features_input.shape   ) < 3:
               lowerCAmelCase__      :			Optional[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)   )






   def 	_lowerCAmelCase   (		self       :		List[Any]   ,     UpperCamelCase       :		Union[str, Any]=False   )			->						int:

         """simple docstring"""
         def _inputs_have_equal_length(UpperCamelCase       :		int   ):
               lowerCAmelCase__      :			Optional[Any]   =	len(input[0]   )
               for input_slice in input[1:]:
                     if len(UpperCamelCase   ) != length:
                           return False
               return True

         def _inputs_are_equal(UpperCamelCase       :		int   ,     UpperCamelCase       :		Dict   ):
               if len(UpperCamelCase   ) != len(UpperCamelCase   ):
                     return False

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

         lowerCAmelCase__      :			str   =	self.feature_extraction_class(**self.feat_extract_dict   )
         lowerCAmelCase__      :			Dict   =	self.feat_extract_tester.prepare_inputs_for_common(numpify=UpperCamelCase   )
         lowerCAmelCase__      :			str   =	feat_extract.model_input_names[0]

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

         lowerCAmelCase__      :			Any   =	self.feat_extract_tester.seq_length_diff
         lowerCAmelCase__      :			List[str]   =	self.feat_extract_tester.max_seq_length + pad_diff
         lowerCAmelCase__      :			Optional[int]   =	self.feat_extract_tester.min_seq_length
         lowerCAmelCase__      :			Optional[int]   =	self.feat_extract_tester.batch_size
         lowerCAmelCase__      :			str   =	self.feat_extract_tester.feature_size

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

         lowerCAmelCase__      :			Tuple   =	feat_extract.pad(UpperCamelCase   ,     padding="""longest"""   )
         lowerCAmelCase__      :			Any   =	input_a[input_name]

         lowerCAmelCase__      :			List[str]   =	feat_extract.pad(UpperCamelCase   ,     padding="""max_length"""   ,     max_length=len(speech_inputs[-1]   )   )
         lowerCAmelCase__      :			Dict   =	input_a[input_name]

         lowerCAmelCase__      :			str   =	feat_extract.pad(UpperCamelCase   ,     padding="""longest"""   ,     return_tensors="""np"""   )
         lowerCAmelCase__      :			Optional[Any]   =	input_a[input_name]

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

         lowerCAmelCase__      :			Optional[int]   =	feat_extract.pad(
             UpperCamelCase   ,     padding="""max_length"""   ,     max_length=UpperCamelCase   ,     return_tensors="""np"""   )
         lowerCAmelCase__      :			int   =	input_a[input_name]

         self.assertFalse(_inputs_have_equal_length(UpperCamelCase   )   )
         self.assertTrue(_inputs_have_equal_length(UpperCamelCase   )   )
         self.assertTrue(_inputs_have_equal_length(UpperCamelCase   )   )
         self.assertTrue(_inputs_are_equal(UpperCamelCase   ,     UpperCamelCase   )   )
         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
         lowerCAmelCase__      :			Any   =	feat_extract.pad(UpperCamelCase   ,     pad_to_multiple_of=10   )
         lowerCAmelCase__      :			int   =	input_a[input_name]

         lowerCAmelCase__      :			Dict   =	feat_extract.pad(UpperCamelCase   ,     padding="""longest"""   ,     pad_to_multiple_of=10   )
         lowerCAmelCase__      :			Union[str, Any]   =	input_a[input_name]

         lowerCAmelCase__      :			Optional[int]   =	feat_extract.pad(
             UpperCamelCase   ,     padding="""max_length"""   ,     pad_to_multiple_of=10   ,     max_length=UpperCamelCase   )
         lowerCAmelCase__      :			str   =	input_a[input_name]

         lowerCAmelCase__      :			Union[str, Any]   =	feat_extract.pad(
             UpperCamelCase   ,     padding="""max_length"""   ,     pad_to_multiple_of=10   ,     max_length=UpperCamelCase   ,     return_tensors="""np"""   ,     )
         lowerCAmelCase__      :			List[Any]   =	input_a[input_name]

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

         lowerCAmelCase__      :			Optional[int]   =	pad_max_length if pad_max_length % 10 == 0 else (pad_max_length // 10 + 1) * 10
         self.assertTrue(all(len(UpperCamelCase   ) == 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
         lowerCAmelCase__      :			str   =	(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 	_lowerCAmelCase   (		self       :		Optional[Any]   ,     UpperCamelCase       :		int=False   )			->						List[str]:

         """simple docstring"""
         def _inputs_have_equal_length(UpperCamelCase       :		List[Any]   ):
               lowerCAmelCase__      :			List[str]   =	len(input[0]   )
               for input_slice in input[1:]:
                     if len(UpperCamelCase   ) != length:
                           return False
               return True

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

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

         lowerCAmelCase__      :			Dict   =	self.feature_extraction_class(**self.feat_extract_dict   )
         lowerCAmelCase__      :			Any   =	self.feat_extract_tester.prepare_inputs_for_common(numpify=UpperCamelCase   )
         lowerCAmelCase__      :			str   =	feat_extract.model_input_names[0]

         lowerCAmelCase__      :			Optional[Any]   =	BatchFeature({input_name: speech_inputs}   )

         # truncate to smallest
         lowerCAmelCase__      :			str   =	feat_extract.pad(
             UpperCamelCase   ,     padding="""max_length"""   ,     max_length=len(speech_inputs[0]   )   ,     truncation=UpperCamelCase   )
         lowerCAmelCase__      :			Dict   =	input_a[input_name]

         lowerCAmelCase__      :			str   =	feat_extract.pad(UpperCamelCase   ,     padding="""max_length"""   ,     max_length=len(speech_inputs[0]   )   )
         lowerCAmelCase__      :			Optional[int]   =	input_a[input_name]

         self.assertTrue(_inputs_have_equal_length(UpperCamelCase   )   )
         self.assertFalse(_inputs_have_equal_length(UpperCamelCase   )   )

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

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

         self.assertTrue(_inputs_have_equal_length(UpperCamelCase   )   )
         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(UpperCamelCase   )   )

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

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

         lowerCAmelCase__      :			Any   =	feat_extract.pad(
             UpperCamelCase   ,     padding="""max_length"""   ,     max_length=len(speech_inputs[1]   )   ,     return_tensors="""np"""   )
         lowerCAmelCase__      :			Dict   =	input_a[input_name]

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

         # 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(UpperCamelCase   )   )
         self.assertTrue(len(input_a[-1]   ) == len(speech_inputs[-1]   )   )

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

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

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

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

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

         lowerCAmelCase__      :			Any   =	feat_extract.pad(
             UpperCamelCase   ,     padding="""max_length"""   ,     max_length=len(speech_inputs[0]   )   ,     pad_to_multiple_of=UpperCamelCase   ,     )
         lowerCAmelCase__      :			Optional[Any]   =	input_a[input_name]

         # retrieve expected_length as multiple of pad_to_multiple_of
         lowerCAmelCase__      :			Union[str, Any]   =	len(speech_inputs[0]   )
         if expected_length % pad_to_multiple_of != 0:
               lowerCAmelCase__      :			Any   =	((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(UpperCamelCase   )   )
         self.assertFalse(_inputs_have_equal_length(UpperCamelCase   )   )






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

         """simple docstring"""
         self._check_padding(numpify=UpperCamelCase   )






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

         """simple docstring"""
         self._check_padding(numpify=UpperCamelCase   )






   def 	_lowerCAmelCase   (		self       :		Dict   )			->						Optional[int]:

         """simple docstring"""
         self._check_truncation(numpify=UpperCamelCase   )






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

         """simple docstring"""
         self._check_truncation(numpify=UpperCamelCase   )






   @require_torch
   def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						str:

         """simple docstring"""
         lowerCAmelCase__      :			int   =	self.feature_extraction_class(**self.feat_extract_dict   )
         lowerCAmelCase__      :			str   =	self.feat_extract_tester.prepare_inputs_for_common()
         lowerCAmelCase__      :			str   =	feat_extract.model_input_names[0]

         lowerCAmelCase__      :			Optional[int]   =	BatchFeature({input_name: speech_inputs}   )

         lowerCAmelCase__      :			Dict   =	feat_extract.pad(UpperCamelCase   ,     padding="""longest"""   ,     return_tensors="""np"""   )[input_name]
         lowerCAmelCase__      :			str   =	feat_extract.pad(UpperCamelCase   ,     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 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						str:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	self.feature_extraction_class(**self.feat_extract_dict   )
         lowerCAmelCase__      :			Tuple   =	self.feat_extract_tester.prepare_inputs_for_common()
         lowerCAmelCase__      :			List[Any]   =	feat_extract.model_input_names[0]

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

         lowerCAmelCase__      :			int   =	feat_extract.pad(UpperCamelCase   ,     padding="""longest"""   ,     return_tensors="""np"""   )[input_name]
         lowerCAmelCase__      :			Any   =	feat_extract.pad(UpperCamelCase   ,     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 	_lowerCAmelCase   (		self       :		List[Any]   )			->						int:

         """simple docstring"""
         lowerCAmelCase__      :			Any   =	self.feat_extract_dict
         lowerCAmelCase__      :			int   =	True
         lowerCAmelCase__      :			str   =	self.feature_extraction_class(**UpperCamelCase   )
         lowerCAmelCase__      :			int   =	self.feat_extract_tester.prepare_inputs_for_common()
         lowerCAmelCase__      :			List[Any]   =	[len(UpperCamelCase   ) for x in speech_inputs]
         lowerCAmelCase__      :			Optional[Any]   =	feat_extract.model_input_names[0]

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

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






   def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						Optional[Any]:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	self.feat_extract_dict
         lowerCAmelCase__      :			List[Any]   =	True
         lowerCAmelCase__      :			str   =	self.feature_extraction_class(**UpperCamelCase   )
         lowerCAmelCase__      :			int   =	self.feat_extract_tester.prepare_inputs_for_common()
         lowerCAmelCase__      :			Any   =	[len(UpperCamelCase   ) for x in speech_inputs]
         lowerCAmelCase__      :			int   =	feat_extract.model_input_names[0]

         lowerCAmelCase__      :			Optional[Any]   =	BatchFeature({input_name: speech_inputs}   )
         lowerCAmelCase__      :			Any   =	min(UpperCamelCase   )

         lowerCAmelCase__      :			Tuple   =	feat_extract.pad(
             UpperCamelCase   ,     padding="""max_length"""   ,     max_length=UpperCamelCase   ,     truncation=UpperCamelCase   ,     return_tensors="""np"""   )
         self.assertIn("""attention_mask"""   ,     UpperCamelCase   )
         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]   )





