




import os
from pathlib import Path
from typing import Dict, List

import fire
import torch

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.utils.logging import get_logger


__UpperCamelCase      : Any					      =   get_logger(__name__)





def 	__A	(			__lowerCamelCase							,    __lowerCamelCase					)   -> List[Any]:
	if text.startswith(__lowerCamelCase					):
		return text[len(__lowerCamelCase					) :]
	return text  # or whatever





def 	__A	(			__lowerCamelCase					)   -> int:
	return {remove_prefix(__lowerCamelCase							,    """model."""					): v for k, v in sd.items()}





def 	__A	(			__lowerCamelCase					)   -> List[str]:
	a		       =							{}
	for k in state_dicts[0].keys():
		a		       =							[sd[k] for sd in state_dicts]
		a		       =							sum(__lowerCamelCase					) / len(__lowerCamelCase					)
		assert isinstance(__lowerCamelCase							,    torch.Tensor					)
		a		       =							new_t
	return new_sd





def 	__A	(			__lowerCamelCase							,    __lowerCamelCase							,    __lowerCamelCase					)   -> None:
	a		       =							AutoModelForSeqaSeqLM.from_pretrained(__lowerCamelCase					)
	if os.path.isfile(__lowerCamelCase					):
		a		       =							[pl_ckpt_path]
	else:
		assert os.path.isdir(__lowerCamelCase					)
		a		       =							list(Path(__lowerCamelCase					).glob("""*.ckpt"""					)					)
		assert ckpt_files, f'could not find any ckpt files inside the {pl_ckpt_path} directory'

	if len(__lowerCamelCase					) > 1:
		logger.info(f'averaging the weights of {ckpt_files}'					)

	a		       =							[sanitize(torch.load(__lowerCamelCase							,    map_location="""cpu"""					)["""state_dict"""]					) for x in ckpt_files]
	a		       =							average_state_dicts(__lowerCamelCase					)

	a       ,				a		       =							hf_model.load_state_dict(__lowerCamelCase							,    strict=__lowerCamelCase					)
	assert not missing, f'missing keys: {missing}'
	hf_model.save_pretrained(__lowerCamelCase					)
	try:
		a		       =							AutoTokenizer.from_pretrained(__lowerCamelCase					)
		tok.save_pretrained(__lowerCamelCase					)
	except Exception:
		pass
		# dont copy tokenizer if cant


if __name__ == "__main__":
		fire.Fire(convert_pl_to_hf)







import doctest

import projectq
from projectq.ops import H, Measure





def 	__A	(			__lowerCamelCase					)   -> int:
	a		       =							quantum_engine.allocate_qubit()
	H | qubit
	Measure | qubit
	return int(__lowerCamelCase					)


if __name__ == "__main__":
		doctest.testmod()

		# initialises a new quantum backend
		__UpperCamelCase      : List[Any]					      =   projectq.MainEngine()

		# Generate a list of 10 random numbers
		__UpperCamelCase      : Optional[int]					      =   [get_random_number(quantum_engine) for _ in range(10)]

		# Flushes the quantum engine from memory
		quantum_engine.flush()

		print("Random numbers", random_numbers_list)

