


import colorsys
import io

import cva
import matplotlib as mpl
import matplotlib.colors as mplc
import matplotlib.figure as mplfigure
import numpy as np
import torch
from matplotlib.backends.backend_agg import FigureCanvasAgg

from utils import img_tensorize


__UpperCamelCase      						=    1_0_0_0







class     lowerCamelCase__		:






			"""simple docstring"""






			def __init__(							self ,  snake_case ,  snake_case=1.2 ,  snake_case="g" ,  snake_case=0.5 ,  snake_case="-" ,  snake_case="test_out.jpg" ,  snake_case=True ,  snake_case=False ,  snake_case=None ,  snake_case=None ,  snake_case=0.7 ,  ):

						'''simple docstring'''


						if isinstance(snake_case ,  torch.Tensor       ):
									UpperCamelCase__   =    img.numpy().astype("np.uint8"       )
						if isinstance(snake_case ,  snake_case       ):
									UpperCamelCase__   =    img_tensorize(snake_case       )
						assert isinstance(snake_case ,  np.ndarray       )

						UpperCamelCase__, UpperCamelCase__   =    img.shape[1], img.shape[0]
						UpperCamelCase__   =    mplfigure.Figure(frameon=snake_case       )
						UpperCamelCase__   =    fig.get_dpi()
						UpperCamelCase__   =    (width * scale + 1E-2) / dpi
						UpperCamelCase__   =    (height * scale + 1E-2) / dpi
						fig.set_size_inches(snake_case ,  snake_case       )
						UpperCamelCase__   =    fig.add_axes([0.0, 0.0, 1.0, 1.0]       )
						ax.axis("off"       )
						ax.set_xlim(0.0 ,  snake_case       )
						ax.set_ylim(snake_case       )

						UpperCamelCase__   =    saveas
						UpperCamelCase__   =    rgb
						UpperCamelCase__   =    pynb
						UpperCamelCase__   =    img
						UpperCamelCase__   =    edgecolor
						UpperCamelCase__   =    0.5
						UpperCamelCase__   =    linestyle
						UpperCamelCase__   =    int(np.sqrt(min(snake_case ,  snake_case       )       ) * scale // 3       )
						UpperCamelCase__   =    width
						UpperCamelCase__   =    height
						UpperCamelCase__   =    scale
						UpperCamelCase__   =    fig
						UpperCamelCase__   =    ax
						UpperCamelCase__   =    pad
						UpperCamelCase__   =    idaobj
						UpperCamelCase__   =    idaattr
						UpperCamelCase__   =    FigureCanvasAgg(snake_case       )




			def 			snake_case__			(							self ,  snake_case ,  snake_case=None       ):

						'''simple docstring'''


						if color is None:
									UpperCamelCase__   =    self.edgecolor
						((UpperCamelCase__), (UpperCamelCase__), (UpperCamelCase__), (UpperCamelCase__))   =    box
						UpperCamelCase__   =    xa - xa
						UpperCamelCase__   =    ya - ya
						self.ax.add_patch(
						    mpl.patches.Rectangle(
						        (xa, ya) ,  snake_case ,  snake_case ,  fill=snake_case ,  edgecolor=snake_case ,  linewidth=self.font_size // 3 ,  alpha=self.alpha ,  linestyle=self.linestyle ,  )       )




			def 			snake_case__			(							self ,  snake_case ,  snake_case=None ,  snake_case=None ,  snake_case=None ,  snake_case=None       ):

						'''simple docstring'''


						if len(boxes.shape       ) > 2:
									UpperCamelCase__   =    boxes[0]
						if len(obj_ids.shape       ) > 1:
									UpperCamelCase__   =    obj_ids[0]
						if len(obj_scores.shape       ) > 1:
									UpperCamelCase__   =    obj_scores[0]
						if len(attr_ids.shape       ) > 1:
									UpperCamelCase__   =    attr_ids[0]
						if len(attr_scores.shape       ) > 1:
									UpperCamelCase__   =    attr_scores[0]
						if isinstance(snake_case ,  torch.Tensor       ):
									UpperCamelCase__   =    boxes.numpy()
						if isinstance(snake_case ,  snake_case       ):
									UpperCamelCase__   =    np.array(snake_case       )
						assert isinstance(snake_case ,  np.ndarray       )
						UpperCamelCase__   =    np.prod(boxes[:, 2:] - boxes[:, :2] ,  axis=1       )
						UpperCamelCase__   =    np.argsort(-areas       ).tolist()
						UpperCamelCase__   =    boxes[sorted_idxs] if boxes is not None else None
						UpperCamelCase__   =    obj_ids[sorted_idxs] if obj_ids is not None else None
						UpperCamelCase__   =    obj_scores[sorted_idxs] if obj_scores is not None else None
						UpperCamelCase__   =    attr_ids[sorted_idxs] if attr_ids is not None else None
						UpperCamelCase__   =    attr_scores[sorted_idxs] if attr_scores is not None else None

						UpperCamelCase__   =    [self._random_color(maximum=1       ) for _ in range(len(snake_case       )       )]
						UpperCamelCase__   =    [assigned_colors[idx] for idx in sorted_idxs]
						if obj_ids is not None:
									UpperCamelCase__   =    self._create_text_labels_attr(snake_case ,  snake_case ,  snake_case ,  snake_case       )
									for i in range(len(snake_case       )       ):
												UpperCamelCase__   =    assigned_colors[i]
												self.add_box(boxes[i] ,  snake_case       )
												self.draw_labels(labels[i] ,  boxes[i] ,  snake_case       )




			def 			snake_case__			(							self ,  snake_case ,  snake_case ,  snake_case       ):

						'''simple docstring'''


						UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__   =    box
						UpperCamelCase__   =    (xa, ya)
						UpperCamelCase__   =    (ya - ya) * (xa - xa)
						UpperCamelCase__   =    _SMALL_OBJ * self.scale
						if instance_area < small or ya - ya < 40 * self.scale:
									if ya >= self.height - 5:
												UpperCamelCase__   =    (xa, ya)
									else:
												UpperCamelCase__   =    (xa, ya)

						UpperCamelCase__   =    (ya - ya) / np.sqrt(self.height * self.width       )
						UpperCamelCase__   =    self._change_color_brightness(snake_case ,  brightness_factor=0.7       )
						UpperCamelCase__   =    np.clip((height_ratio - 0.02) / 0.08 + 1 ,  1.2 ,  2       )
						font_size *= 0.75 * self.font_size

						self.draw_text(
						    text=snake_case ,  position=snake_case ,  color=snake_case ,  )




			def 			snake_case__			(							self ,  snake_case ,  snake_case ,  snake_case="g" ,  snake_case="left" ,  ):

						'''simple docstring'''


						UpperCamelCase__   =    0
						UpperCamelCase__   =    self.font_size
						UpperCamelCase__   =    np.maximum(list(mplc.to_rgb(snake_case       )       ) ,  0.2       )
						UpperCamelCase__   =    max(0.8 ,  np.max(snake_case       )       )
						UpperCamelCase__   =    {
						    "facecolor": "black",
						    "alpha": self.alpha,
						    "pad": self.pad,
						    "edgecolor": "none",
						}
						UpperCamelCase__, UpperCamelCase__   =    position
						self.ax.text(
						    snake_case ,  snake_case ,  snake_case ,  size=font_size * self.scale ,  family="sans-serif" ,  bbox=snake_case ,  verticalalignment="top" ,  horizontalalignment=snake_case ,  color=snake_case ,  zorder=10 ,  rotation=snake_case ,  )




			def 			snake_case__			(							self ,  snake_case=None       ):

						'''simple docstring'''


						if saveas is None:
									UpperCamelCase__   =    self.saveas
						if saveas.lower().endswith(".jpg"       ) or saveas.lower().endswith(".png"       ):
									cva.imwrite(
									    snake_case ,  self._get_buffer()[:, :, ::-1] ,  )
						else:
									self.fig.savefig(snake_case       )




			def 			snake_case__			(							self ,  snake_case ,  snake_case ,  snake_case ,  snake_case       ):

						'''simple docstring'''


						UpperCamelCase__   =    [self.idaobj[i] for i in classes]
						UpperCamelCase__   =    [self.idaattr[i] for i in attr_classes]
						UpperCamelCase__   =    [
						    F'''{label} {score:.2f} {attr} {attr_score:.2f}'''
						    for label, score, attr, attr_score in zip(snake_case ,  snake_case ,  snake_case ,  snake_case       )
						]
						return labels




			def 			snake_case__			(							self ,  snake_case ,  snake_case       ):

						'''simple docstring'''


						UpperCamelCase__   =    [self.idaobj[i] for i in classes]
						if scores is not None:
									if labels is None:
												UpperCamelCase__   =    ["{:.0f}%".format(s * 100       ) for s in scores]
									else:
												UpperCamelCase__   =    ["{} {:.0f}%".format(snake_case ,  s * 100       ) for li, s in zip(snake_case ,  snake_case       )]
						return labels




			def 			snake_case__			(							self ,  snake_case=255       ):

						'''simple docstring'''


						UpperCamelCase__   =    np.random.randint(0 ,  len(_COLORS       )       )
						UpperCamelCase__   =    _COLORS[idx] * maximum
						if not self.rgb:
									UpperCamelCase__   =    ret[::-1]
						return ret




			def 			snake_case__			(							self       ):

						'''simple docstring'''


						if not self.pynb:
									UpperCamelCase__, UpperCamelCase__   =    self.canvas.print_to_buffer()
									if (width, height) != (self.width, self.height):
												UpperCamelCase__   =    cva.resize(self.img ,  (width, height)       )
									else:
												UpperCamelCase__   =    self.img
						else:
									UpperCamelCase__   =    io.BytesIO()  # works for cairo backend
									self.canvas.print_rgba(snake_case       )
									UpperCamelCase__, UpperCamelCase__   =    self.width, self.height
									UpperCamelCase__   =    buf.getvalue()
									UpperCamelCase__   =    self.img

						UpperCamelCase__   =    np.frombuffer(snake_case ,  dtype="uint8"       )
						UpperCamelCase__   =    buffer.reshape(snake_case ,  snake_case ,  4       )
						UpperCamelCase__, UpperCamelCase__   =    np.split(snake_case ,  [3] ,  axis=2       )

						try:
									import numexpr as ne  # fuse them with numexpr

									UpperCamelCase__   =    ne.evaluate("img * (1 - alpha / 255.0) + rgb * (alpha / 255.0)"       )
						except ImportError:
									UpperCamelCase__   =    alpha.astype("float32"       ) / 255.0
									UpperCamelCase__   =    img * (1 - alpha) + rgb * alpha

						return visualized_image.astype("uint8"       )







			def 			snake_case__			(							self ,  snake_case ,  snake_case       ):

						'''simple docstring'''


						assert brightness_factor >= -1.0 and brightness_factor <= 1.0
						UpperCamelCase__   =    mplc.to_rgb(snake_case       )
						UpperCamelCase__   =    colorsys.rgb_to_hls(*mplc.to_rgb(snake_case       )       )
						UpperCamelCase__   =    polygon_color[1] + (brightness_factor * polygon_color[1])
						UpperCamelCase__   =    0.0 if modified_lightness < 0.0 else modified_lightness
						UpperCamelCase__   =    1.0 if modified_lightness > 1.0 else modified_lightness
						UpperCamelCase__   =    colorsys.hls_to_rgb(polygon_color[0] ,  snake_case ,  polygon_color[2]       )
						return modified_color



__UpperCamelCase      						=    (
    np.array(
        [
            0.000,
            0.447,
            0.741,
            0.850,
            0.325,
            0.098,
            0.929,
            0.694,
            0.125,
            0.494,
            0.184,
            0.556,
            0.466,
            0.674,
            0.188,
            0.301,
            0.745,
            0.933,
            0.635,
            0.078,
            0.184,
            0.300,
            0.300,
            0.300,
            0.600,
            0.600,
            0.600,
            1.000,
            0.000,
            0.000,
            1.000,
            0.500,
            0.000,
            0.749,
            0.749,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            1.000,
            0.667,
            0.000,
            1.000,
            0.333,
            0.333,
            0.000,
            0.333,
            0.667,
            0.000,
            0.333,
            1.000,
            0.000,
            0.667,
            0.333,
            0.000,
            0.667,
            0.667,
            0.000,
            0.667,
            1.000,
            0.000,
            1.000,
            0.333,
            0.000,
            1.000,
            0.667,
            0.000,
            1.000,
            1.000,
            0.000,
            0.000,
            0.333,
            0.500,
            0.000,
            0.667,
            0.500,
            0.000,
            1.000,
            0.500,
            0.333,
            0.000,
            0.500,
            0.333,
            0.333,
            0.500,
            0.333,
            0.667,
            0.500,
            0.333,
            1.000,
            0.500,
            0.667,
            0.000,
            0.500,
            0.667,
            0.333,
            0.500,
            0.667,
            0.667,
            0.500,
            0.667,
            1.000,
            0.500,
            1.000,
            0.000,
            0.500,
            1.000,
            0.333,
            0.500,
            1.000,
            0.667,
            0.500,
            1.000,
            1.000,
            0.500,
            0.000,
            0.333,
            1.000,
            0.000,
            0.667,
            1.000,
            0.000,
            1.000,
            1.000,
            0.333,
            0.000,
            1.000,
            0.333,
            0.333,
            1.000,
            0.333,
            0.667,
            1.000,
            0.333,
            1.000,
            1.000,
            0.667,
            0.000,
            1.000,
            0.667,
            0.333,
            1.000,
            0.667,
            0.667,
            1.000,
            0.667,
            1.000,
            1.000,
            1.000,
            0.000,
            1.000,
            1.000,
            0.333,
            1.000,
            1.000,
            0.667,
            1.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.167,
            0.000,
            0.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.167,
            0.000,
            0.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.143,
            0.143,
            0.143,
            0.857,
            0.857,
            0.857,
            1.000,
            1.000,
            1.000,
        ]
    )
    .astype(np.floataa)
    .reshape(-1, 3)
)






import argparse

import torch
from saprl.hub import distilhubert

from transformers import HubertConfig, HubertModel, WavaVecaFeatureExtractor, logging


logging.set_verbosity_info()
__UpperCamelCase      						=    logging.get_logger(__name__)

__UpperCamelCase      						=    {
    'post_extract_proj': 'feature_projection.projection',
    'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv',
    'self_attn.k_proj': 'encoder.layers.*.attention.k_proj',
    'self_attn.v_proj': 'encoder.layers.*.attention.v_proj',
    'self_attn.q_proj': 'encoder.layers.*.attention.q_proj',
    'self_attn.out_proj': 'encoder.layers.*.attention.out_proj',
    'self_attn_layer_norm': 'encoder.layers.*.layer_norm',
    'fc1': 'encoder.layers.*.feed_forward.intermediate_dense',
    'fc2': 'encoder.layers.*.feed_forward.output_dense',
    'final_layer_norm': 'encoder.layers.*.final_layer_norm',
    'encoder.layer_norm': 'encoder.layer_norm',
    'mask_emb': 'masked_spec_embed',
}
def     UpperCamelCase_(			_A				:int			,       _A				:Dict			,       _A				:int			,       _A				:List[str]			,       _A				:Optional[int]					)->						Any:
			for attribute in key.split("."					):
						UpperCamelCase__   =    getattr(_A			,       _A					)

			if weight_type is not None:
						UpperCamelCase__   =    getattr(_A			,       _A					).shape
			else:
						UpperCamelCase__   =    hf_pointer.shape

			assert hf_shape == value.shape, (
			    F'''Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be'''
			    F''' {value.shape} for {full_name}'''
			)

			if weight_type == "weight":
						UpperCamelCase__   =    value
			elif weight_type == "weight_g":
						UpperCamelCase__   =    value
			elif weight_type == "weight_v":
						UpperCamelCase__   =    value
			elif weight_type == "bias":
						UpperCamelCase__   =    value
			else:
						UpperCamelCase__   =    value

			logger.info(F'''{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}.'''					)
def     UpperCamelCase_(			_A				:Optional[Any]			,       _A				:Dict					)->						List[Any]:
			UpperCamelCase__   =    []
			UpperCamelCase__   =    fairseq_model.state_dict()

			UpperCamelCase__   =    hf_model.feature_extractor

			for name, value in fairseq_dict.items():
						UpperCamelCase__   =    False
						if "conv_layers" in name:
									load_conv_layer(
									    _A			,       _A			,       _A			,       _A			,       hf_model.config.feat_extract_norm == "group"			,       )
									UpperCamelCase__   =    True
						else:
									for key, mapped_key in MAPPING.items():
												UpperCamelCase__   =    mapped_key

												if key in name:
															UpperCamelCase__   =    True
															if "*" in mapped_key:
																		UpperCamelCase__   =    name.split(_A					)[0].split("."					)[-2]
																		UpperCamelCase__   =    mapped_key.replace("*"			,       _A					)
															if "weight_g" in name:
																		UpperCamelCase__   =    "weight_g"
															elif "weight_v" in name:
																		UpperCamelCase__   =    "weight_v"
															elif "weight" in name:
																		UpperCamelCase__   =    "weight"
															elif "bias" in name:
																		UpperCamelCase__   =    "bias"
															else:
																		UpperCamelCase__   =    None
															set_recursively(_A			,       _A			,       _A			,       _A			,       _A					)
												continue
						if not is_used:
									unused_weights.append(_A					)

			logger.warning(F'''Unused weights: {unused_weights}'''					)
def     UpperCamelCase_(			_A				:Any			,       _A				:List[Any]			,       _A				:Union[str, Any]			,       _A				:Dict			,       _A				:Union[str, Any]					)->						Optional[Any]:
			UpperCamelCase__   =    full_name.split("conv_layers."					)[-1]
			UpperCamelCase__   =    name.split("."					)
			UpperCamelCase__   =    int(items[0]					)
			UpperCamelCase__   =    int(items[1]					)

			if type_id == 0:
						if "bias" in name:
									assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
									    F'''{full_name} has size {value.shape}, but'''
									    F''' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.'''
									)
									UpperCamelCase__   =    value
									logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.'''					)
						elif "weight" in name:
									assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
									    F'''{full_name} has size {value.shape}, but'''
									    F''' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.'''
									)
									UpperCamelCase__   =    value
									logger.info(F'''Feat extract conv layer {layer_id} was initialized from {full_name}.'''					)
			elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
						if "bias" in name:
									assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
									    F'''{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was'''
									    " found."
									)
									UpperCamelCase__   =    value
									logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'''					)
						elif "weight" in name:
									assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
									    F'''{full_name} has size {value.shape}, but'''
									    F''' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.'''
									)
									UpperCamelCase__   =    value
									logger.info(F'''Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'''					)
			else:
						unused_weights.append(_A					)
def     UpperCamelCase_(			_A				:Dict					)->						Union[str, Any]:
			UpperCamelCase__   =    HubertConfig()
			UpperCamelCase__   =    model.config

			UpperCamelCase__   =    fs_config.activation_dropout
			UpperCamelCase__   =    False
			UpperCamelCase__   =    fs_config.attention_dropout
			UpperCamelCase__   =    False
			UpperCamelCase__   =    eval(fs_config.extractor_conv_feature_layers					)
			UpperCamelCase__   =    [x[0] for x in conv_layers]
			UpperCamelCase__   =    [x[1] for x in conv_layers]
			UpperCamelCase__   =    [x[2] for x in conv_layers]
			UpperCamelCase__   =    "gelu"
			UpperCamelCase__   =    "layer" if fs_config.extractor_mode == "layer_norm" else "group"
			UpperCamelCase__   =    False
			UpperCamelCase__   =    0.0
			UpperCamelCase__   =    0.0
			UpperCamelCase__   =    fs_config.activation_fn
			UpperCamelCase__   =    fs_config.dropout
			UpperCamelCase__   =    fs_config.encoder_embed_dim
			UpperCamelCase__   =    0.0_2
			UpperCamelCase__   =    fs_config.encoder_ffn_embed_dim
			UpperCamelCase__   =    1e-5
			UpperCamelCase__   =    0.0
			UpperCamelCase__   =    fs_config.encoder_attention_heads
			UpperCamelCase__   =    fs_config.conv_pos_groups
			UpperCamelCase__   =    fs_config.conv_pos
			UpperCamelCase__   =    len(_A					)
			UpperCamelCase__   =    fs_config.encoder_layers

			return config
@torch.no_grad()
def     UpperCamelCase_(			_A				:str			,       _A				:Union[str, Any]=None					)->						str:
			UpperCamelCase__   =    distilhubert().model.model

			if config_path is not None:
						UpperCamelCase__   =    HubertConfig.from_pretrained(_A					)
			else:
						UpperCamelCase__   =    convert_config(_A					)
			UpperCamelCase__   =    model.eval()

			UpperCamelCase__   =    WavaVecaFeatureExtractor(
			    feature_size=1			,       sampling_rate=1_60_00			,       padding_value=0			,       do_normalize=_A			,       return_attention_mask=_A			,       )
			UpperCamelCase__   =    HubertModel(_A					)

			recursively_load_weights(_A			,       _A					)

			feature_extractor.save_pretrained(_A					)
			hf_model.save_pretrained(_A					)


if __name__ == "__main__":
						__UpperCamelCase      						=    argparse.ArgumentParser()
						parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
						parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert')
						__UpperCamelCase      						=    parser.parse_args()
						convert_hubert_checkpoint(args.pytorch_dump_folder_path, args.config_path)


