

'''simple docstring'''
import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import cached_download, hf_hub_download, hf_hub_url
from PIL import Image

from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig
from transformers.utils import logging


logging.set_verbosity_info()
lowerCamelCase			:				Any 			=					logging.get_logger(__name__)


def 	_lowerCAmelCase (						_UpperCamelCase						:					Optional[int]    )				->      List[Any]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =SwinConfig(
         embed_dim=1_92							,			depths=(2, 2, 18, 2)							,			num_heads=(6, 12, 24, 48)							,			window_size=12							,			out_features=['stage2', 'stage3', 'stage4']							,			)

     _SCREAMING_SNAKE_CASE							   =DetaConfig(
         backbone_config=_UpperCamelCase							,			num_queries=9_00							,			encoder_ffn_dim=20_48							,			decoder_ffn_dim=20_48							,			num_feature_levels=5							,			assign_first_stage=_UpperCamelCase							,			with_box_refine=_UpperCamelCase							,			two_stage=_UpperCamelCase							,			)

     # set labels
     _SCREAMING_SNAKE_CASE							   ='huggingface/label-files'
     if "o365" in model_name:
          _SCREAMING_SNAKE_CASE							   =3_66
          _SCREAMING_SNAKE_CASE							   ='object365-id2label.json'
     else:
          _SCREAMING_SNAKE_CASE							   =91
          _SCREAMING_SNAKE_CASE							   ='coco-detection-id2label.json'

     _SCREAMING_SNAKE_CASE							   =num_labels
     _SCREAMING_SNAKE_CASE							   =json.load(open(cached_download(hf_hub_url(_UpperCamelCase							,			_UpperCamelCase							,			repo_type='dataset'    )    )							,			'r'    )    )
     _SCREAMING_SNAKE_CASE							   ={int(_UpperCamelCase    ): v for k, v in idalabel.items()}
     _SCREAMING_SNAKE_CASE							   =idalabel
     _SCREAMING_SNAKE_CASE							   ={v: k for k, v in idalabel.items()}

     return config


def 	_lowerCAmelCase (						_UpperCamelCase						:					List[Any]    )				->      str:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =[]

     # stem
     # fmt: off
     rename_keys.append(('backbone.0.body.patch_embed.proj.weight', 'model.backbone.model.embeddings.patch_embeddings.projection.weight')    )
     rename_keys.append(('backbone.0.body.patch_embed.proj.bias', 'model.backbone.model.embeddings.patch_embeddings.projection.bias')    )
     rename_keys.append(('backbone.0.body.patch_embed.norm.weight', 'model.backbone.model.embeddings.norm.weight')    )
     rename_keys.append(('backbone.0.body.patch_embed.norm.bias', 'model.backbone.model.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.0.body.layers.{i}.blocks.{j}.norm1.weight", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.norm1.bias", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.norm2.weight", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.norm2.bias", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias", f"model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias")    )

          if i < 3:
               rename_keys.append((f"backbone.0.body.layers.{i}.downsample.reduction.weight", f"model.backbone.model.encoder.layers.{i}.downsample.reduction.weight")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.downsample.norm.weight", f"model.backbone.model.encoder.layers.{i}.downsample.norm.weight")    )
               rename_keys.append((f"backbone.0.body.layers.{i}.downsample.norm.bias", f"model.backbone.model.encoder.layers.{i}.downsample.norm.bias")    )

     rename_keys.append(('backbone.0.body.norm1.weight', 'model.backbone.model.hidden_states_norms.stage2.weight')    )
     rename_keys.append(('backbone.0.body.norm1.bias', 'model.backbone.model.hidden_states_norms.stage2.bias')    )
     rename_keys.append(('backbone.0.body.norm2.weight', 'model.backbone.model.hidden_states_norms.stage3.weight')    )
     rename_keys.append(('backbone.0.body.norm2.bias', 'model.backbone.model.hidden_states_norms.stage3.bias')    )
     rename_keys.append(('backbone.0.body.norm3.weight', 'model.backbone.model.hidden_states_norms.stage4.weight')    )
     rename_keys.append(('backbone.0.body.norm3.bias', 'model.backbone.model.hidden_states_norms.stage4.bias')    )

     # transformer encoder
     for i in range(config.encoder_layers    ):
          rename_keys.append((f"transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight", f"model.encoder.layers.{i}.self_attn.sampling_offsets.weight")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias", f"model.encoder.layers.{i}.self_attn.sampling_offsets.bias")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.self_attn.attention_weights.weight", f"model.encoder.layers.{i}.self_attn.attention_weights.weight")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.self_attn.attention_weights.bias", f"model.encoder.layers.{i}.self_attn.attention_weights.bias")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.self_attn.value_proj.weight", f"model.encoder.layers.{i}.self_attn.value_proj.weight")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.self_attn.value_proj.bias", f"model.encoder.layers.{i}.self_attn.value_proj.bias")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.self_attn.output_proj.weight", f"model.encoder.layers.{i}.self_attn.output_proj.weight")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.self_attn.output_proj.bias", f"model.encoder.layers.{i}.self_attn.output_proj.bias")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.norm1.weight", f"model.encoder.layers.{i}.self_attn_layer_norm.weight")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.norm1.bias", f"model.encoder.layers.{i}.self_attn_layer_norm.bias")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.linear1.weight", f"model.encoder.layers.{i}.fc1.weight")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.linear1.bias", f"model.encoder.layers.{i}.fc1.bias")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.linear2.weight", f"model.encoder.layers.{i}.fc2.weight")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.linear2.bias", f"model.encoder.layers.{i}.fc2.bias")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.norm2.weight", f"model.encoder.layers.{i}.final_layer_norm.weight")    )
          rename_keys.append((f"transformer.encoder.layers.{i}.norm2.bias", f"model.encoder.layers.{i}.final_layer_norm.bias")    )

     # transformer decoder
     for i in range(config.decoder_layers    ):
          rename_keys.append((f"transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight", f"model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias", f"model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.cross_attn.attention_weights.weight", f"model.decoder.layers.{i}.encoder_attn.attention_weights.weight")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.cross_attn.attention_weights.bias", f"model.decoder.layers.{i}.encoder_attn.attention_weights.bias")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.cross_attn.value_proj.weight", f"model.decoder.layers.{i}.encoder_attn.value_proj.weight")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.cross_attn.value_proj.bias", f"model.decoder.layers.{i}.encoder_attn.value_proj.bias")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.cross_attn.output_proj.weight", f"model.decoder.layers.{i}.encoder_attn.output_proj.weight")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.cross_attn.output_proj.bias", f"model.decoder.layers.{i}.encoder_attn.output_proj.bias")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.norm1.weight", f"model.decoder.layers.{i}.encoder_attn_layer_norm.weight")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.norm1.bias", f"model.decoder.layers.{i}.encoder_attn_layer_norm.bias")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.self_attn.out_proj.weight", f"model.decoder.layers.{i}.self_attn.out_proj.weight")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.self_attn.out_proj.bias", f"model.decoder.layers.{i}.self_attn.out_proj.bias")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.norm2.weight", f"model.decoder.layers.{i}.self_attn_layer_norm.weight")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.norm2.bias", f"model.decoder.layers.{i}.self_attn_layer_norm.bias")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.linear1.weight", f"model.decoder.layers.{i}.fc1.weight")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.linear1.bias", f"model.decoder.layers.{i}.fc1.bias")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.linear2.weight", f"model.decoder.layers.{i}.fc2.weight")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.linear2.bias", f"model.decoder.layers.{i}.fc2.bias")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.norm3.weight", f"model.decoder.layers.{i}.final_layer_norm.weight")    )
          rename_keys.append((f"transformer.decoder.layers.{i}.norm3.bias", f"model.decoder.layers.{i}.final_layer_norm.bias")    )

     # fmt: on

     return rename_keys


def 	_lowerCAmelCase (						_UpperCamelCase						:					Optional[Any]							,			_UpperCamelCase						:					Tuple							,			_UpperCamelCase						:					List[str]    )				->      int:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =dct.pop(_UpperCamelCase    )
     _SCREAMING_SNAKE_CASE							   =val


def 	_lowerCAmelCase (						_UpperCamelCase						:					Optional[Any]							,			_UpperCamelCase						:					str    )				->      int:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =[int(backbone_config.embed_dim * 2**i    ) for i in range(len(backbone_config.depths    )    )]
     for i in range(len(backbone_config.depths    )    ):
          _SCREAMING_SNAKE_CASE							   =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)
               _SCREAMING_SNAKE_CASE							   =state_dict.pop(f"backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight"    )
               _SCREAMING_SNAKE_CASE							   =state_dict.pop(f"backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias"    )
               # next, add query, keys and values (in that order) to the state dict
               _SCREAMING_SNAKE_CASE							   =in_proj_weight[:dim, :]
               _SCREAMING_SNAKE_CASE							   =in_proj_bias[: dim]
               _SCREAMING_SNAKE_CASE							   =in_proj_weight[
                   dim : dim * 2, :
               ]
               _SCREAMING_SNAKE_CASE							   =in_proj_bias[
                   dim : dim * 2
               ]
               _SCREAMING_SNAKE_CASE							   =in_proj_weight[
                   -dim :, :
               ]
               _SCREAMING_SNAKE_CASE							   =in_proj_bias[-dim :]
               # fmt: on


def 	_lowerCAmelCase (						_UpperCamelCase						:					Dict							,			_UpperCamelCase						:					str    )				->      Optional[int]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =config.d_model
     for i in range(config.decoder_layers    ):
          # read in weights + bias of input projection layer of self-attention
          _SCREAMING_SNAKE_CASE							   =state_dict.pop(f"transformer.decoder.layers.{i}.self_attn.in_proj_weight"    )
          _SCREAMING_SNAKE_CASE							   =state_dict.pop(f"transformer.decoder.layers.{i}.self_attn.in_proj_bias"    )
          # next, add query, keys and values (in that order) to the state dict
          _SCREAMING_SNAKE_CASE							   =in_proj_weight[:hidden_size, :]
          _SCREAMING_SNAKE_CASE							   =in_proj_bias[:hidden_size]
          _SCREAMING_SNAKE_CASE							   =in_proj_weight[
              hidden_size : hidden_size * 2, :
          ]
          _SCREAMING_SNAKE_CASE							   =in_proj_bias[hidden_size : hidden_size * 2]
          _SCREAMING_SNAKE_CASE							   =in_proj_weight[-hidden_size:, :]
          _SCREAMING_SNAKE_CASE							   =in_proj_bias[-hidden_size:]


def 	_lowerCAmelCase (						)				->      List[str]:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   ='http://images.cocodataset.org/val2017/000000039769.jpg'
     _SCREAMING_SNAKE_CASE							   =Image.open(requests.get(_UpperCamelCase							,			stream=_UpperCamelCase    ).raw    )

     return im


@torch.no_grad()
def 	_lowerCAmelCase (						_UpperCamelCase						:					Dict							,			_UpperCamelCase						:					List[Any]							,			_UpperCamelCase						:					List[str]    )				->      Dict:
     """simple docstring"""




     _SCREAMING_SNAKE_CASE							   =get_deta_config(_UpperCamelCase    )

     # load original state dict
     if model_name == "deta-swin-large":
          _SCREAMING_SNAKE_CASE							   =hf_hub_download(repo_id='nielsr/deta-checkpoints'							,			filename='adet_swin_ft.pth'    )
     elif model_name == "deta-swin-large-o365":
          _SCREAMING_SNAKE_CASE							   =hf_hub_download(repo_id='jozhang97/deta-swin-l-o365'							,			filename='deta_swin_pt_o365.pth'    )
     else:
          raise ValueError(f"Model name {model_name} not supported"    )

     _SCREAMING_SNAKE_CASE							   =torch.load(_UpperCamelCase							,			map_location='cpu'    )['model']

     # original state dict
     for name, param in state_dict.items():
          print(_UpperCamelCase							,			param.shape    )

     # rename keys
     _SCREAMING_SNAKE_CASE							   =create_rename_keys(_UpperCamelCase    )
     for src, dest in rename_keys:
          rename_key(_UpperCamelCase							,			_UpperCamelCase							,			_UpperCamelCase    )
     read_in_swin_q_k_v(_UpperCamelCase							,			config.backbone_config    )
     read_in_decoder_q_k_v(_UpperCamelCase							,			_UpperCamelCase    )

     # fix some prefixes
     for key in state_dict.copy().keys():
          if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key:
               _SCREAMING_SNAKE_CASE							   =state_dict.pop(_UpperCamelCase    )
               _SCREAMING_SNAKE_CASE							   =val
          if "input_proj" in key:
               _SCREAMING_SNAKE_CASE							   =state_dict.pop(_UpperCamelCase    )
               _SCREAMING_SNAKE_CASE							   =val
          if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key:
               _SCREAMING_SNAKE_CASE							   =state_dict.pop(_UpperCamelCase    )
               _SCREAMING_SNAKE_CASE							   =val

    # finally, create HuggingFace model and load state dict
     _SCREAMING_SNAKE_CASE							   =DetaForObjectDetection(_UpperCamelCase    )
     model.load_state_dict(_UpperCamelCase    )
     model.eval()

     _SCREAMING_SNAKE_CASE							   ='cuda' if torch.cuda.is_available() else 'cpu'
     model.to(_UpperCamelCase    )

     # load image processor
     _SCREAMING_SNAKE_CASE							   =DetaImageProcessor(format='coco_detection'    )

     # verify our conversion on image
     _SCREAMING_SNAKE_CASE							   =prepare_img()
     _SCREAMING_SNAKE_CASE							   =processor(images=_UpperCamelCase							,			return_tensors='pt'    )
     _SCREAMING_SNAKE_CASE							   =encoding['pixel_values']
     _SCREAMING_SNAKE_CASE							   =model(pixel_values.to(_UpperCamelCase    )    )

     # verify logits
     print('Logits:'							,			outputs.logits[0, :3, :3]    )
     print('Boxes:'							,			outputs.pred_boxes[0, :3, :3]    )
     if model_name == "deta-swin-large":
          _SCREAMING_SNAKE_CASE							   =torch.tensor(
              [[-7.63_08, -2.84_85, -5.37_37], [-7.20_37, -4.55_05, -4.80_27], [-7.29_43, -4.26_11, -4.66_17]]    )
          _SCREAMING_SNAKE_CASE							   =torch.tensor([[0.49_87, 0.49_69, 0.99_99], [0.25_49, 0.54_98, 0.48_05], [0.54_98, 0.27_57, 0.05_69]]    )
     elif model_name == "deta-swin-large-o365":
          _SCREAMING_SNAKE_CASE							   =torch.tensor(
              [[-8.01_22, -3.57_20, -4.97_17], [-8.15_47, -3.68_86, -4.63_89], [-7.66_10, -3.61_94, -5.01_34]]    )
          _SCREAMING_SNAKE_CASE							   =torch.tensor([[0.25_23, 0.55_49, 0.48_81], [0.77_15, 0.41_49, 0.46_01], [0.55_03, 0.27_53, 0.05_75]]    )
     assert torch.allclose(outputs.logits[0, :3, :3]							,			expected_logits.to(_UpperCamelCase    )							,			atol=1E-4    )
     assert torch.allclose(outputs.pred_boxes[0, :3, :3]							,			expected_boxes.to(_UpperCamelCase    )							,			atol=1E-4    )
     print('Everything ok!'    )

     if pytorch_dump_folder_path:
          # Save model and processor
          logger.info(f"Saving PyTorch model and processor to {pytorch_dump_folder_path}..."    )
          Path(_UpperCamelCase    ).mkdir(exist_ok=_UpperCamelCase    )
          model.save_pretrained(_UpperCamelCase    )
          processor.save_pretrained(_UpperCamelCase    )

     # Push to hub
     if push_to_hub:
          print('Pushing model and processor to hub...'    )
          model.push_to_hub(f"jozhang97/{model_name}"    )
          processor.push_to_hub(f"jozhang97/{model_name}"    )


if __name__ == "__main__":
     lowerCamelCase			:				Union[str, Any] 			=					argparse.ArgumentParser()

     parser.add_argument(
         "--model_name",
         type=str,
         default="deta-swin-large",
         choices=["deta-swin-large", "deta-swin-large-o365"],
         help="Name of the model you'd like to convert.",
     )
     parser.add_argument(
         "--pytorch_dump_folder_path",
         default=None,
         type=str,
         help="Path to the folder to output PyTorch model.",
     )
     parser.add_argument(
         "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub."
     )
     lowerCamelCase			:				List[Any] 			=					parser.parse_args()
     convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)



'''simple docstring'''
import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
     from transformers import WhisperFeatureExtractor

if is_torch_available():
     import torch

lowerCamelCase			:				List[str] 			=					random.Random()


def 	_lowerCAmelCase (						_UpperCamelCase						:					Optional[Any]							,			_UpperCamelCase						:					List[str]=1.0							,			_UpperCamelCase						:					Optional[int]=None							,			_UpperCamelCase						:					Dict=None    )				->      Any:
     """simple docstring"""




     if rng is None:
          _SCREAMING_SNAKE_CASE							   =global_rng

     _SCREAMING_SNAKE_CASE							   =[]
     for batch_idx in range(shape[0]    ):
          values.append([]    )
          for _ in range(shape[1]    ):
               values[-1].append(rng.random() * scale    )

     return values







@require_torch
@require_torchaudio
class 							A__   (	unittest.TestCase  ):





  def __init__(		self				:     List[Any]							, _a				:     Tuple							, _a				:     Dict=7							, _a				:     List[Any]=400							, _a				:     List[str]=2000							, _a				:     Optional[Any]=10							, _a				:     Dict=160							, _a				:     Tuple=8							, _a				:     Any=0.0							, _a				:     Optional[Any]=4000							, _a				:     List[Any]=False							, _a				:     Dict=True							, )     ->   Union[str, Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =parent
       _SCREAMING_SNAKE_CASE							   =batch_size
       _SCREAMING_SNAKE_CASE							   =min_seq_length
       _SCREAMING_SNAKE_CASE							   =max_seq_length
       _SCREAMING_SNAKE_CASE							   =(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
       _SCREAMING_SNAKE_CASE							   =padding_value
       _SCREAMING_SNAKE_CASE							   =sampling_rate
       _SCREAMING_SNAKE_CASE							   =return_attention_mask
       _SCREAMING_SNAKE_CASE							   =do_normalize
       _SCREAMING_SNAKE_CASE							   =feature_size
       _SCREAMING_SNAKE_CASE							   =chunk_length
       _SCREAMING_SNAKE_CASE							   =hop_length





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





       '''simple docstring'''






       return {
           "feature_size": self.feature_size,
           "hop_length": self.hop_length,
           "chunk_length": self.chunk_length,
           "padding_value": self.padding_value,
           "sampling_rate": self.sampling_rate,
           "return_attention_mask": self.return_attention_mask,
           "do_normalize": self.do_normalize,
       }





  def 						A  (		self				:     Optional[Any]							, _a				:     Any=False							, _a				:     Union[str, Any]=False			)     ->   Optional[Any]:





       '''simple docstring'''






       def _flatten(_a				:     Union[str, Any]			):
            return list(itertools.chain(*_a			)			)

       if equal_length:
            _SCREAMING_SNAKE_CASE							   =[floats_list((self.max_seq_length, self.feature_size)			) for _ in range(self.batch_size			)]
       else:
            # make sure that inputs increase in size
            _SCREAMING_SNAKE_CASE							   =[
                floats_list((x, self.feature_size)			)
                for x in range(self.min_seq_length							, self.max_seq_length							, self.seq_length_diff			)
            ]
       if numpify:
            _SCREAMING_SNAKE_CASE							   =[np.asarray(_a			) for x in speech_inputs]
       return speech_inputs







@require_torch
@require_torchaudio
class 							A__   (	A__					,       unittest.TestCase  ):
  A__					=    WhisperFeatureExtractor if is_speech_available() else None





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =WhisperFeatureExtractionTester(self			)





  def 						A  (		self				:     str			)     ->   Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.feature_extraction_class(**self.feat_extract_dict			)

       with tempfile.TemporaryDirectory() as tmpdirname:
            _SCREAMING_SNAKE_CASE							   =feat_extract_first.save_pretrained(_a			)[0]
            check_json_file_has_correct_format(_a			)
            _SCREAMING_SNAKE_CASE							   =self.feature_extraction_class.from_pretrained(_a			)

       _SCREAMING_SNAKE_CASE							   =feat_extract_first.to_dict()
       _SCREAMING_SNAKE_CASE							   =feat_extract_second.to_dict()
       _SCREAMING_SNAKE_CASE							   =feat_extract_first.mel_filters
       _SCREAMING_SNAKE_CASE							   =feat_extract_second.mel_filters
       self.assertTrue(np.allclose(_a							, _a			)			)
       self.assertEqual(_a							, _a			)





  def 						A  (		self				:     Tuple			)     ->   Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.feature_extraction_class(**self.feat_extract_dict			)

       with tempfile.TemporaryDirectory() as tmpdirname:
            _SCREAMING_SNAKE_CASE							   =os.path.join(_a							, 'feat_extract.json'			)
            feat_extract_first.to_json_file(_a			)
            _SCREAMING_SNAKE_CASE							   =self.feature_extraction_class.from_json_file(_a			)

       _SCREAMING_SNAKE_CASE							   =feat_extract_first.to_dict()
       _SCREAMING_SNAKE_CASE							   =feat_extract_second.to_dict()
       _SCREAMING_SNAKE_CASE							   =feat_extract_first.mel_filters
       _SCREAMING_SNAKE_CASE							   =feat_extract_second.mel_filters
       self.assertTrue(np.allclose(_a							, _a			)			)
       self.assertEqual(_a							, _a			)





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
       # create three inputs of length 800, 1000, and 1200
       _SCREAMING_SNAKE_CASE							   =[floats_list((1, x)			)[0] for x in range(800							, 1400							, 200			)]
       _SCREAMING_SNAKE_CASE							   =[np.asarray(_a			) for speech_input in speech_inputs]

       # Test feature size
       _SCREAMING_SNAKE_CASE							   =feature_extractor(_a							, padding='max_length'							, return_tensors='np'			).input_features
       self.assertTrue(input_features.ndim == 3			)
       self.assertTrue(input_features.shape[-1] == feature_extractor.nb_max_frames			)
       self.assertTrue(input_features.shape[-2] == feature_extractor.feature_size			)

       # Test not batched input
       _SCREAMING_SNAKE_CASE							   =feature_extractor(speech_inputs[0]							, return_tensors='np'			).input_features
       _SCREAMING_SNAKE_CASE							   =feature_extractor(np_speech_inputs[0]							, return_tensors='np'			).input_features
       self.assertTrue(np.allclose(_a							, _a							, atol=1e-3			)			)

       # Test batched
       _SCREAMING_SNAKE_CASE							   =feature_extractor(_a							, return_tensors='np'			).input_features
       _SCREAMING_SNAKE_CASE							   =feature_extractor(_a							, return_tensors='np'			).input_features
       for enc_seq_a, enc_seq_a in zip(_a							, _a			):
            self.assertTrue(np.allclose(_a							, _a							, atol=1e-3			)			)

       # Test 2-D numpy arrays are batched.
       _SCREAMING_SNAKE_CASE							   =[floats_list((1, x)			)[0] for x in (800, 800, 800)]
       _SCREAMING_SNAKE_CASE							   =np.asarray(_a			)
       _SCREAMING_SNAKE_CASE							   =feature_extractor(_a							, return_tensors='np'			).input_features
       _SCREAMING_SNAKE_CASE							   =feature_extractor(_a							, return_tensors='np'			).input_features
       for enc_seq_a, enc_seq_a in zip(_a							, _a			):
            self.assertTrue(np.allclose(_a							, _a							, atol=1e-3			)			)

       # Test truncation required
       _SCREAMING_SNAKE_CASE							   =[floats_list((1, x)			)[0] for x in range(200							, (feature_extractor.n_samples + 500)							, 200			)]
       _SCREAMING_SNAKE_CASE							   =[np.asarray(_a			) for speech_input in speech_inputs]

       _SCREAMING_SNAKE_CASE							   =[x[: feature_extractor.n_samples] for x in speech_inputs]
       _SCREAMING_SNAKE_CASE							   =[np.asarray(_a			) for speech_input in speech_inputs_truncated]

       _SCREAMING_SNAKE_CASE							   =feature_extractor(_a							, return_tensors='np'			).input_features
       _SCREAMING_SNAKE_CASE							   =feature_extractor(_a							, return_tensors='np'			).input_features
       for enc_seq_a, enc_seq_a in zip(_a							, _a			):
            self.assertTrue(np.allclose(_a							, _a							, atol=1e-3			)			)





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





       '''simple docstring'''






       import torch

       _SCREAMING_SNAKE_CASE							   =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
       _SCREAMING_SNAKE_CASE							   =np.random.rand(100							, 32			).astype(np.floataa			)
       _SCREAMING_SNAKE_CASE							   =np_speech_inputs.tolist()

       for inputs in [py_speech_inputs, np_speech_inputs]:
            _SCREAMING_SNAKE_CASE							   =feature_extractor.pad([{'input_features': inputs}]							, return_tensors='np'			)
            self.assertTrue(np_processed.input_features.dtype == np.floataa			)
            _SCREAMING_SNAKE_CASE							   =feature_extractor.pad([{'input_features': inputs}]							, return_tensors='pt'			)
            self.assertTrue(pt_processed.input_features.dtype == torch.floataa			)





  def 						A  (		self				:     Tuple							, _a				:     str			)     ->   Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =load_dataset('hf-internal-testing/librispeech_asr_dummy'							, 'clean'							, split='validation'			)
       # automatic decoding with librispeech
       _SCREAMING_SNAKE_CASE							   =ds.sort('id'			).select(range(_a			)			)[:num_samples]['audio']

       return [x["array"] for x in speech_samples]





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =torch.tensor(
           [
               0.11_93, -0.09_46, -0.10_98, -0.01_96, 0.02_25, -0.06_90, -0.17_36, 0.09_51,
               0.09_71, -0.08_17, -0.07_02, 0.01_62, 0.02_60, 0.00_17, -0.01_92, -0.16_78,
               0.07_09, -0.18_67, -0.06_55, -0.02_74, -0.02_34, -0.18_84, -0.05_16, -0.05_54,
               -0.02_74, -0.14_25, -0.14_23, 0.08_37, 0.03_77, -0.08_54
           ]			)
       # fmt: on

       _SCREAMING_SNAKE_CASE							   =self._load_datasamples(1			)
       _SCREAMING_SNAKE_CASE							   =WhisperFeatureExtractor()
       _SCREAMING_SNAKE_CASE							   =feature_extractor(_a							, return_tensors='pt'			).input_features
       self.assertEqual(input_features.shape							, (1, 80, 3000)			)
       self.assertTrue(torch.allclose(input_features[0, 0, :30]							, _a							, atol=1e-4			)			)





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							   =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
       _SCREAMING_SNAKE_CASE							   =self._load_datasamples(1			)[0]
       _SCREAMING_SNAKE_CASE							   =((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535  # Rescale to [0, 65535] to show issue
       _SCREAMING_SNAKE_CASE							   =feat_extract.zero_mean_unit_var_norm([audio]							, attention_mask=_a			)[0]

       self.assertTrue(np.all(np.mean(_a			) < 1e-3			)			)
       self.assertTrue(np.all(np.abs(np.var(_a			) - 1			) < 1e-3			)			)
