



import argparse

import torch

from transformers import HubertConfig, HubertForSequenceClassification, WavaVecaFeatureExtractor, logging


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

__lowerCamelCase								=     ["""UtteranceLevel"""]




@torch.no_grad()
def       UpperCAmelCase__	(							__snake_case  ,			__snake_case  ,			__snake_case  ,			__snake_case							)				->	str:
  _A          =      torch.load(__snake_case  ,			map_location='''cpu'''							)
  if checkpoint["Config"]["downstream_expert"]["modelrc"]["select"] not in SUPPORTED_MODELS:
    raise NotImplementedError(F'''The supported s3prl models are {SUPPORTED_MODELS}'''							)

  _A          =      checkpoint['''Downstream''']

  _A          =      HubertConfig.from_pretrained(__snake_case							)
  _A          =      HubertForSequenceClassification.from_pretrained(__snake_case  ,			config=__snake_case							)
  _A          =      WavaVecaFeatureExtractor.from_pretrained(
      __snake_case  ,			return_attention_mask=__snake_case  ,			do_normalize=__snake_case							)

  if hf_congfig.use_weighted_layer_sum:
    _A          =      checkpoint['''Featurizer''']['''weights''']

  _A          =      downstream_dict['''projector.weight''']
  _A          =      downstream_dict['''projector.bias''']
  _A          =      downstream_dict['''model.post_net.linear.weight''']
  _A          =      downstream_dict['''model.post_net.linear.bias''']

  hf_feature_extractor.save_pretrained(__snake_case							)
  hf_model.save_pretrained(__snake_case							)


if __name__ == "__main__":
    __lowerCamelCase								=     argparse.ArgumentParser()
    parser.add_argument(
        """--base_model_name""", default=None, type=str, help="""Name of the huggingface pretrained base model."""
    )
    parser.add_argument("""--config_path""", default=None, type=str, help="""Path to the huggingface classifier config.""")
    parser.add_argument("""--checkpoint_path""", default=None, type=str, help="""Path to the s3prl checkpoint.""")
    parser.add_argument("""--model_dump_path""", default=None, type=str, help="""Path to the final converted model.""")
    __lowerCamelCase								=     parser.parse_args()
    convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)




import math
from timeit import timeit




def       UpperCAmelCase__	(							__snake_case							)				->	int:
  _A          =      0
  _A          =      abs(__snake_case							)
  while True:
    _A          =      n // 10
    digits += 1
    if n == 0:
      break
  return digits




def       UpperCAmelCase__	(							__snake_case							)				->	int:
  return 1 if n == 0 else math.floor(math.log(abs(__snake_case							)  ,			10							) + 1							)




def       UpperCAmelCase__	(							__snake_case							)				->	int:
  return len(str(abs(__snake_case							)							)							)




def       UpperCAmelCase__	(							)				->	None:
  from collections.abc import Callable

  def benchmark_a_function(__snake_case  ,			__snake_case							) -> None:
    _A          =      F'''{func.__name__}({value})'''
    _A          =      timeit(F'''__main__.{call}'''  ,			setup='''import __main__'''							)
    print(F'''{call}: {func(__snake_case							)} -- {timing} seconds'''							)

  for value in (262_144, 1_125_899_906_842_624, 1_267_650_600_228_229_401_496_703_205_376):
    for func in (num_digits, num_digits_fast, num_digits_faster):
      benchmark_a_function(__snake_case  ,			__snake_case							)
    print()


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    benchmark()