

A_       : Optional[int]         =			'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

def 						UpperCamelCase					(lowercase_: bytes					)     ->      bytes:
	# Make sure the supplied data is a bytes-like object
	if not isinstance(lowercase_    ,      lowercase_					):
		A__							:    str        =		f"""a bytes-like object is required, not '{data.__class__.__name__}'"""
		raise TypeError(lowercase_					)

	A__							:    Optional[Any]        =		"""""".join(bin(lowercase_					)[2:].zfill(8					) for byte in data					)

	A__							:    Optional[Any]        =		len(lowercase_					) % 6 != 0

	if padding_needed:
		# The padding that will be added later
		A__							:    Dict        =		b"""=""" * ((6 - len(lowercase_					) % 6) // 2)

		# Append binary_stream with arbitrary binary digits (0's by default) to make its
		# length a multiple of 6.
		binary_stream += "0" * (6 - len(lowercase_					) % 6)
	else:
		A__							:    int        =		b""""""

	# Encode every 6 binary digits to their corresponding Base64 character
	return (
	    "".join(
	        B64_CHARSET[int(binary_stream[index : index + 6]    ,      2					)]
	        for index in range(0    ,      len(lowercase_					)    ,      6					)					).encode()
	    + padding
	)

def 						UpperCamelCase					(lowercase_: str					)     ->      bytes:
	# Make sure encoded_data is either a string or a bytes-like object
	if not isinstance(lowercase_    ,      lowercase_					) and not isinstance(lowercase_    ,      lowercase_					):
		A__							:    Union[str, Any]        =		(
		    """argument should be a bytes-like object or ASCII string, """
		    f"""not '{encoded_data.__class__.__name__}'"""
		)
		raise TypeError(lowercase_					)

	# In case encoded_data is a bytes-like object, make sure it contains only
	# ASCII characters so we convert it to a string object
	if isinstance(lowercase_    ,      lowercase_					):
		try:
			A__							:    Union[str, Any]        =		encoded_data.decode("""utf-8"""					)
		except UnicodeDecodeError:
			raise ValueError("""base64 encoded data should only contain ASCII characters"""					)

	A__							:    List[Any]        =		encoded_data.count("""="""					)

	# Check if the encoded string contains non base64 characters
	if padding:
		assert all(
		    char in B64_CHARSET for char in encoded_data[:-padding]					), "Invalid base64 character(s) found."
	else:
		assert all(
		    char in B64_CHARSET for char in encoded_data					), "Invalid base64 character(s) found."

	# Check the padding
	assert len(lowercase_					) % 4 == 0 and padding < 3, "Incorrect padding"

	if padding:
		# Remove padding if there is one
		A__							:    str        =		encoded_data[:-padding]

		A__							:    Any        =		"""""".join(
		    bin(B64_CHARSET.index(lowercase_					)					)[2:].zfill(6					) for char in encoded_data					)[: -padding * 2]
	else:
		A__							:    Any        =		"""""".join(
		    bin(B64_CHARSET.index(lowercase_					)					)[2:].zfill(6					) for char in encoded_data					)

	A__							:    Any        =		[
	    int(binary_stream[index : index + 8]    ,      2					)
	    for index in range(0    ,      len(lowercase_					)    ,      8					)
	]

	return bytes(lowercase_					)


if __name__ == "__main__":
	import doctest

	doctest.testmod()






import argparse

import torch

from ...utils import logging
from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert


logging.set_verbosity_info()

def 						UpperCamelCase					(lowercase_: Optional[int]    ,      lowercase_: Union[str, Any]    ,      lowercase_: Optional[Any]					)     ->      Tuple:
	# Initialise PyTorch model
	A__							:    str        =		AlbertConfig.from_json_file(lowercase_					)
	print(f"""Building PyTorch model from configuration: {config}"""					)
	A__							:    List[Any]        =		AlbertForPreTraining(lowercase_					)

	# Load weights from tf checkpoint
	load_tf_weights_in_albert(lowercase_    ,      lowercase_    ,      lowercase_					)

	# Save pytorch-model
	print(f"""Save PyTorch model to {pytorch_dump_path}"""					)
	torch.save(model.state_dict()    ,      lowercase_					)


if __name__ == "__main__":
	A_       : int         =			argparse.ArgumentParser()
	# Required parameters
	parser.add_argument(
	    '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
	)
	parser.add_argument(
	    '--albert_config_file',
	    default=None,
	    type=str,
	    required=True,
	    help=(
	        'The config json file corresponding to the pre-trained ALBERT model. \n'
	        'This specifies the model architecture.'
	    ),
	)
	parser.add_argument(
	    '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
	)
	A_       : int         =			parser.parse_args()
	convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)



