






from collections.abc import Generator
from math import sin

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  bytes					)		->			bytes:






	"""simple docstring"""



	if len(__magic_name__					) != 32:
		raise ValueError("""Input must be of length 32"""					)

	UpperCamelCase     :int          =	B""""""
	for i in [3, 2, 1, 0]:
		little_endian += string_aa[8 * i : 8 * i + 8]
	return little_endian

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  int					)		->			bytes:






	"""simple docstring"""



	if i < 0:
		raise ValueError("""Input must be non-negative"""					)

	UpperCamelCase     :Any          =	format(__magic_name__	,			"""08x"""					)[-8:]
	UpperCamelCase     :Union[str, Any]          =	B""""""
	for i in [3, 2, 1, 0]:
		little_endian_hex += hex_rep[2 * i : 2 * i + 2].encode("""utf-8"""					)
	return little_endian_hex

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  bytes					)		->			bytes:






	"""simple docstring"""



	UpperCamelCase     :str          =	B""""""
	for char in message:
		bit_string += format(__magic_name__	,			"""08b"""					).encode("""utf-8"""					)
	UpperCamelCase     :Any          =	format(len(__magic_name__					)	,			"""064b"""					).encode("""utf-8"""					)

	# Pad bit_string to a multiple of 512 chars
	bit_string += b"1"
	while len(__magic_name__					) % 512 != 448:
		bit_string += b"0"
	bit_string += to_little_endian(start_len[32:]					) + to_little_endian(start_len[:32]					)

	return bit_string

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  bytes					)		->			Generator[list[int], None, None]:






	"""simple docstring"""



	if len(__magic_name__					) % 512 != 0:
		raise ValueError("""Input must have length that's a multiple of 512"""					)

	for pos in range(0	,			len(__magic_name__					)	,			512					):
		UpperCamelCase     :Tuple          =	bit_string[pos : pos + 512]
		UpperCamelCase     :Optional[int]          =	[]
		for i in range(0	,			512	,			32					):
			block_words.append(int(to_little_endian(block[i : i + 32]					)	,			2					)					)
		yield block_words

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  int					)		->			int:






	"""simple docstring"""



	if i < 0:
		raise ValueError("""Input must be non-negative"""					)

	UpperCamelCase     :List[str]          =	format(__magic_name__	,			"""032b"""					)
	UpperCamelCase     :Any          =	""""""
	for c in i_str:
		new_str += "1" if c == "0" else "0"
	return int(__magic_name__	,			2					)

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  int	,			__magic_name__					:  int					)		->			int:






	"""simple docstring"""



	return (a + b) % 2**32

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  int	,			__magic_name__					:  int					)		->			int:






	"""simple docstring"""



	if i < 0:
		raise ValueError("""Input must be non-negative"""					)
	if shift < 0:
		raise ValueError("""Shift must be non-negative"""					)
	return ((i << shift) ^ (i >> (32 - shift))) % 2**32

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  bytes					)		->			bytes:






	"""simple docstring"""



	UpperCamelCase     :Tuple          =	preprocess(__magic_name__					)

	UpperCamelCase     :List[str]          =	[int(2**32 * abs(sin(i + 1					)					)					) for i in range(64					)]

	# Starting states
	UpperCamelCase     :Union[str, Any]          =	0X67_45_23_01
	UpperCamelCase     :Union[str, Any]          =	0XEF_CD_AB_89
	UpperCamelCase     :List[str]          =	0X98_BA_DC_FE
	UpperCamelCase     :int          =	0X10_32_54_76

	UpperCamelCase     :int          =	[
	    7,
	    12,
	    17,
	    22,
	    7,
	    12,
	    17,
	    22,
	    7,
	    12,
	    17,
	    22,
	    7,
	    12,
	    17,
	    22,
	    5,
	    9,
	    14,
	    20,
	    5,
	    9,
	    14,
	    20,
	    5,
	    9,
	    14,
	    20,
	    5,
	    9,
	    14,
	    20,
	    4,
	    11,
	    16,
	    23,
	    4,
	    11,
	    16,
	    23,
	    4,
	    11,
	    16,
	    23,
	    4,
	    11,
	    16,
	    23,
	    6,
	    10,
	    15,
	    21,
	    6,
	    10,
	    15,
	    21,
	    6,
	    10,
	    15,
	    21,
	    6,
	    10,
	    15,
	    21,
	]

	# Process bit string in chunks, each with 16 32-char words
	for block_words in get_block_words(__magic_name__					):
		UpperCamelCase     :Optional[Any]          =	aa
		UpperCamelCase     :Any          =	ba
		UpperCamelCase     :Tuple          =	ca
		UpperCamelCase     :List[str]          =	da

		# Hash current chunk
		for i in range(64					):
			if i <= 15:
				# f = (b & c) | (not_32(b) & d)     # Alternate definition for f
				UpperCamelCase     :int          =	d ^ (b & (c ^ d))
				UpperCamelCase     :Optional[int]          =	i
			elif i <= 31:
				# f = (d & b) | (not_32(d) & c)     # Alternate definition for f
				UpperCamelCase     :str          =	c ^ (d & (b ^ c))
				UpperCamelCase     :Union[str, Any]          =	(5 * i + 1) % 16
			elif i <= 47:
				UpperCamelCase     :str          =	b ^ c ^ d
				UpperCamelCase     :Optional[int]          =	(3 * i + 5) % 16
			else:
				UpperCamelCase     :List[str]          =	c ^ (b | not_aa(__magic_name__					))
				UpperCamelCase     :int          =	(7 * i) % 16
			UpperCamelCase     :Dict          =	(f + a + added_consts[i] + block_words[g]) % 2**32
			UpperCamelCase     :Tuple          =	d
			UpperCamelCase     :str          =	c
			UpperCamelCase     :Tuple          =	b
			UpperCamelCase     :Optional[Any]          =	sum_aa(__magic_name__	,			left_rotate_aa(__magic_name__	,			shift_amounts[i]					)					)

		# Add hashed chunk to running total
		UpperCamelCase     :List[str]          =	sum_aa(__magic_name__	,			__magic_name__					)
		UpperCamelCase     :str          =	sum_aa(__magic_name__	,			__magic_name__					)
		UpperCamelCase     :int          =	sum_aa(__magic_name__	,			__magic_name__					)
		UpperCamelCase     :Optional[Any]          =	sum_aa(__magic_name__	,			__magic_name__					)

	UpperCamelCase     :Optional[Any]          =	reformat_hex(__magic_name__					) + reformat_hex(__magic_name__					) + reformat_hex(__magic_name__					) + reformat_hex(__magic_name__					)
	return digest


if __name__ == "__main__":
	import doctest

	doctest.testmod()








import argparse

import torch

# Step 1. clone https://github.com/microsoft/unilm
# Step 2. git checkout to https://github.com/microsoft/unilm/commit/b94ec76c36f02fb2b0bf0dcb0b8554a2185173cd
# Step 3. cd unilm
# Step 4. ln -s $(realpath wavlm/modules.py) ./  # create simlink
# import classes
from unilm.wavlm.WavLM import WavLM as WavLMOrig
from unilm.wavlm.WavLM import WavLMConfig as WavLMConfigOrig

from transformers import WavLMConfig, WavLMModel, logging


logging.set_verbosity_info()
UpperCAmelCase_ :						Optional[Any]      =      logging.get_logger(__name__)

UpperCAmelCase_ :						Optional[Any]      =      {
    '''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.grep_linear''': '''encoder.layers.*.attention.gru_rel_pos_linear''',
    '''self_attn.relative_attention_bias''': '''encoder.layers.*.attention.rel_attn_embed''',
    '''self_attn.grep_a''': '''encoder.layers.*.attention.gru_rel_pos_const''',
    '''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''',
    '''w2v_model.layer_norm''': '''feature_projection.layer_norm''',
    '''quantizer.weight_proj''': '''quantizer.weight_proj''',
    '''quantizer.vars''': '''quantizer.codevectors''',
    '''project_q''': '''project_q''',
    '''final_proj''': '''project_hid''',
    '''w2v_encoder.proj''': '''ctc_proj''',
    '''mask_emb''': '''masked_spec_embed''',
}
UpperCAmelCase_ :						int      =      [
    '''ctc_proj''',
    '''quantizer.weight_proj''',
    '''quantizer.codevectors''',
    '''project_q''',
    '''project_hid''',
]

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  Dict	,			__magic_name__					:  Dict	,			__magic_name__					:  Union[str, Any]	,			__magic_name__					:  Tuple	,			__magic_name__					:  Optional[int]					)		->			Dict:






	"""simple docstring"""



	for attribute in key.split("""."""					):
		UpperCamelCase     :Dict          =	getattr(__magic_name__	,			__magic_name__					)

	if weight_type is not None:
		UpperCamelCase     :Optional[int]          =	getattr(__magic_name__	,			__magic_name__					).shape
	else:
		UpperCamelCase     :Optional[int]          =	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     :str          =	value
	elif weight_type == "weight_g":
		UpperCamelCase     :int          =	value
	elif weight_type == "weight_v":
		UpperCamelCase     :int          =	value
	elif weight_type == "bias":
		UpperCamelCase     :List[Any]          =	value
	else:
		UpperCamelCase     :Any          =	value

	logger.info(f"""{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}."""					)

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  Optional[Any]	,			__magic_name__					:  List[str]					)		->			Optional[Any]:






	"""simple docstring"""



	UpperCamelCase     :Union[str, Any]          =	[]
	UpperCamelCase     :Dict          =	fairseq_model.state_dict()

	UpperCamelCase     :int          =	hf_model.feature_extractor

	for name, value in fairseq_dict.items():
		UpperCamelCase     :str          =	False
		if "conv_layers" in name:
			load_conv_layer(
			    __magic_name__	,			__magic_name__	,			__magic_name__	,			__magic_name__	,			hf_model.config.feat_extract_norm == """group"""	,			)
			UpperCamelCase     :Union[str, Any]          =	True
		else:
			for key, mapped_key in MAPPING.items():
				if key in name or key.split("""w2v_model."""					)[-1] == name.split("""."""					)[0]:
					UpperCamelCase     :Optional[int]          =	True
					if "*" in mapped_key:
						UpperCamelCase     :List[Any]          =	name.split(__magic_name__					)[0].split("""."""					)[-2]
						UpperCamelCase     :int          =	mapped_key.replace("""*"""	,			__magic_name__					)
					if "weight_g" in name:
						UpperCamelCase     :List[Any]          =	"""weight_g"""
					elif "weight_v" in name:
						UpperCamelCase     :List[Any]          =	"""weight_v"""
					elif "bias" in name and "relative_attention_bias" not in name:
						UpperCamelCase     :Any          =	"""bias"""
					elif "weight" in name:
						# TODO: don't match quantizer.weight_proj
						UpperCamelCase     :List[str]          =	"""weight"""
					else:
						UpperCamelCase     :Optional[int]          =	None

					set_recursively(__magic_name__	,			__magic_name__	,			__magic_name__	,			__magic_name__	,			__magic_name__					)
				continue
		if not is_used:
			unused_weights.append(__magic_name__					)

	logger.warning(f"""Unused weights: {unused_weights}"""					)

def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  Any	,			__magic_name__					:  str	,			__magic_name__					:  int	,			__magic_name__					:  int	,			__magic_name__					:  List[str]					)		->			Dict:






	"""simple docstring"""



	UpperCamelCase     :Dict          =	full_name.split("""conv_layers."""					)[-1]
	UpperCamelCase     :int          =	name.split("""."""					)
	UpperCamelCase     :str          =	int(items[0]					)
	UpperCamelCase     :str          =	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     :Tuple          =	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     :Dict          =	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     :Tuple          =	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     :Union[str, Any]          =	value
			logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."""					)
	else:
		unused_weights.append(__magic_name__					)

@torch.no_grad()
def    SCREAMING_SNAKE_CASE_							(							__magic_name__					:  List[str]	,			__magic_name__					:  List[Any]	,			__magic_name__					:  str=None					)		->			int:






	"""simple docstring"""



	UpperCamelCase     :List[Any]          =	torch.load(__magic_name__					)
	UpperCamelCase     :List[Any]          =	WavLMConfigOrig(checkpoint["""cfg"""]					)
	UpperCamelCase     :int          =	WavLMOrig(__magic_name__					)
	model.load_state_dict(checkpoint["""model"""]					)
	model.eval()

	if config_path is not None:
		UpperCamelCase     :List[Any]          =	WavLMConfig.from_pretrained(__magic_name__					)
	else:
		UpperCamelCase     :Any          =	WavLMConfig()

	UpperCamelCase     :Dict          =	WavLMModel(__magic_name__					)

	recursively_load_weights(__magic_name__	,			__magic_name__					)

	hf_wavlm.save_pretrained(__magic_name__					)


if __name__ == "__main__":
	UpperCAmelCase_ :						Union[str, Any]      =      argparse.ArgumentParser()
	parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
	parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''')
	parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''')
	UpperCAmelCase_ :						Optional[int]      =      parser.parse_args()
	convert_wavlm_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)
