




import argparse
import os
import re

import tensorflow as tf
import torch

from transformers import BertConfig, BertModel
from transformers.utils import logging


logging.set_verbosity_info()
_lowerCamelCase      :  Any      						=						logging.get_logger(__name__)




def 							_UpperCAmelCase  (UpperCamelCase_ : Dict      ,    UpperCamelCase_ : List[str]      ,    UpperCamelCase_ : Optional[int]       ):
				'''simple docstring'''
				_lowerCAmelCase    :    int										=      os.path.abspath(UpperCamelCase_       )
				logger.info(F"Converting TensorFlow checkpoint from {tf_path}"       )
				# Load weights from TF model
				_lowerCAmelCase    :    List[Any]										=      tf.train.list_variables(UpperCamelCase_       )
				_lowerCAmelCase    :    Optional[int]										=      []
				_lowerCAmelCase    :    Union[str, Any]										=      []
				_lowerCAmelCase    :    int										=      []
				for full_name, shape in init_vars:
								# logger.info(f"Loading TF weight {name} with shape {shape}")
								_lowerCAmelCase    :    Optional[Any]										=      full_name.split("""/"""       )
								if full_name == "_CHECKPOINTABLE_OBJECT_GRAPH" or name[0] in ["global_step", "save_counter"]:
												logger.info(F"Skipping non-model layer {full_name}"       )
												continue
								if "optimizer" in full_name:
												logger.info(F"Skipping optimization layer {full_name}"       )
												continue
								if name[0] == "model":
												# ignore initial 'model'
												_lowerCAmelCase    :    Union[str, Any]										=      name[1:]
								# figure out how many levels deep the name is
								_lowerCAmelCase    :    int										=      0
								for _name in name:
												if _name.startswith("""layer_with_weights"""       ):
																depth += 1
												else:
																break
								layer_depth.append(UpperCamelCase_       )
								# read data
								_lowerCAmelCase    :    str										=      tf.train.load_variable(UpperCamelCase_      ,    UpperCamelCase_       )
								names.append("""/""".join(UpperCamelCase_       )       )
								arrays.append(UpperCamelCase_       )
				logger.info(F"Read a total of {len(UpperCamelCase_       ):,} layers"       )

				# Sanity check
				if len(set(UpperCamelCase_       )       ) != 1:
								raise ValueError(F"Found layer names with different depths (layer depth {list(set(UpperCamelCase_       )       )})"       )
				_lowerCAmelCase    :    str										=      list(set(UpperCamelCase_       )       )[0]
				if layer_depth != 1:
								raise ValueError(
								    """The model contains more than just the embedding/encoder layers. This script does not handle MLM/NSP"""
								    """ heads."""       )

				# convert layers
				logger.info("""Converting weights..."""       )
				for full_name, array in zip(UpperCamelCase_      ,    UpperCamelCase_       ):
								_lowerCAmelCase    :    Tuple										=      full_name.split("""/"""       )
								_lowerCAmelCase    :    Optional[int]										=      model
								_lowerCAmelCase    :    Tuple										=      []
								for i, m_name in enumerate(UpperCamelCase_       ):
												if m_name == ".ATTRIBUTES":
																# variable names end with .ATTRIBUTES/VARIABLE_VALUE
																break
												if m_name.startswith("""layer_with_weights"""       ):
																_lowerCAmelCase    :    Optional[int]										=      int(m_name.split("""-"""       )[-1]       )
																if layer_num <= 2:
																				# embedding layers
																				# layer_num 0: word_embeddings
																				# layer_num 1: position_embeddings
																				# layer_num 2: token_type_embeddings
																				continue
																elif layer_num == 3:
																				# embedding LayerNorm
																				trace.extend(["""embeddings""", """LayerNorm"""]       )
																				_lowerCAmelCase    :    List[Any]										=      getattr(UpperCamelCase_      ,    """embeddings"""       )
																				_lowerCAmelCase    :    str										=      getattr(UpperCamelCase_      ,    """LayerNorm"""       )
																elif layer_num > 3 and layer_num < config.num_hidden_layers + 4:
																				# encoder layers
																				trace.extend(["""encoder""", """layer""", str(layer_num - 4       )]       )
																				_lowerCAmelCase    :    Tuple										=      getattr(UpperCamelCase_      ,    """encoder"""       )
																				_lowerCAmelCase    :    Optional[Any]										=      getattr(UpperCamelCase_      ,    """layer"""       )
																				_lowerCAmelCase    :    Tuple										=      pointer[layer_num - 4]
																elif layer_num == config.num_hidden_layers + 4:
																				# pooler layer
																				trace.extend(["""pooler""", """dense"""]       )
																				_lowerCAmelCase    :    Union[str, Any]										=      getattr(UpperCamelCase_      ,    """pooler"""       )
																				_lowerCAmelCase    :    Optional[Any]										=      getattr(UpperCamelCase_      ,    """dense"""       )
												elif m_name == "embeddings":
																trace.append("""embeddings"""       )
																_lowerCAmelCase    :    List[str]										=      getattr(UpperCamelCase_      ,    """embeddings"""       )
																if layer_num == 0:
																				trace.append("""word_embeddings"""       )
																				_lowerCAmelCase    :    List[str]										=      getattr(UpperCamelCase_      ,    """word_embeddings"""       )
																elif layer_num == 1:
																				trace.append("""position_embeddings"""       )
																				_lowerCAmelCase    :    List[str]										=      getattr(UpperCamelCase_      ,    """position_embeddings"""       )
																elif layer_num == 2:
																				trace.append("""token_type_embeddings"""       )
																				_lowerCAmelCase    :    Union[str, Any]										=      getattr(UpperCamelCase_      ,    """token_type_embeddings"""       )
																else:
																				raise ValueError(F"Unknown embedding layer with name {full_name}"       )
																trace.append("""weight"""       )
																_lowerCAmelCase    :    Union[str, Any]										=      getattr(UpperCamelCase_      ,    """weight"""       )
												elif m_name == "_attention_layer":
																# self-attention layer
																trace.extend(["""attention""", """self"""]       )
																_lowerCAmelCase    :    List[Any]										=      getattr(UpperCamelCase_      ,    """attention"""       )
																_lowerCAmelCase    :    Tuple										=      getattr(UpperCamelCase_      ,    """self"""       )
												elif m_name == "_attention_layer_norm":
																# output attention norm
																trace.extend(["""attention""", """output""", """LayerNorm"""]       )
																_lowerCAmelCase    :    Dict										=      getattr(UpperCamelCase_      ,    """attention"""       )
																_lowerCAmelCase    :    str										=      getattr(UpperCamelCase_      ,    """output"""       )
																_lowerCAmelCase    :    int										=      getattr(UpperCamelCase_      ,    """LayerNorm"""       )
												elif m_name == "_attention_output_dense":
																# output attention dense
																trace.extend(["""attention""", """output""", """dense"""]       )
																_lowerCAmelCase    :    Optional[Any]										=      getattr(UpperCamelCase_      ,    """attention"""       )
																_lowerCAmelCase    :    str										=      getattr(UpperCamelCase_      ,    """output"""       )
																_lowerCAmelCase    :    List[str]										=      getattr(UpperCamelCase_      ,    """dense"""       )
												elif m_name == "_output_dense":
																# output dense
																trace.extend(["""output""", """dense"""]       )
																_lowerCAmelCase    :    Dict										=      getattr(UpperCamelCase_      ,    """output"""       )
																_lowerCAmelCase    :    List[str]										=      getattr(UpperCamelCase_      ,    """dense"""       )
												elif m_name == "_output_layer_norm":
																# output dense
																trace.extend(["""output""", """LayerNorm"""]       )
																_lowerCAmelCase    :    Dict										=      getattr(UpperCamelCase_      ,    """output"""       )
																_lowerCAmelCase    :    Optional[int]										=      getattr(UpperCamelCase_      ,    """LayerNorm"""       )
												elif m_name == "_key_dense":
																# attention key
																trace.append("""key"""       )
																_lowerCAmelCase    :    List[str]										=      getattr(UpperCamelCase_      ,    """key"""       )
												elif m_name == "_query_dense":
																# attention query
																trace.append("""query"""       )
																_lowerCAmelCase    :    Tuple										=      getattr(UpperCamelCase_      ,    """query"""       )
												elif m_name == "_value_dense":
																# attention value
																trace.append("""value"""       )
																_lowerCAmelCase    :    int										=      getattr(UpperCamelCase_      ,    """value"""       )
												elif m_name == "_intermediate_dense":
																# attention intermediate dense
																trace.extend(["""intermediate""", """dense"""]       )
																_lowerCAmelCase    :    Union[str, Any]										=      getattr(UpperCamelCase_      ,    """intermediate"""       )
																_lowerCAmelCase    :    List[str]										=      getattr(UpperCamelCase_      ,    """dense"""       )
												elif m_name == "_output_layer_norm":
																# output layer norm
																trace.append("""output"""       )
																_lowerCAmelCase    :    Tuple										=      getattr(UpperCamelCase_      ,    """output"""       )
												# weights & biases
												elif m_name in ["bias", "beta"]:
																trace.append("""bias"""       )
																_lowerCAmelCase    :    str										=      getattr(UpperCamelCase_      ,    """bias"""       )
												elif m_name in ["kernel", "gamma"]:
																trace.append("""weight"""       )
																_lowerCAmelCase    :    Union[str, Any]										=      getattr(UpperCamelCase_      ,    """weight"""       )
												else:
																logger.warning(F"Ignored {m_name}"       )
        # for certain layers reshape is necessary
								_lowerCAmelCase    :    Any										=      """.""".join(UpperCamelCase_       )
								if re.match(R"""(\S+)\.attention\.self\.(key|value|query)\.(bias|weight)"""      ,    UpperCamelCase_       ) or re.match(
								    R"""(\S+)\.attention\.output\.dense\.weight"""      ,    UpperCamelCase_       ):
												_lowerCAmelCase    :    Union[str, Any]										=      array.reshape(pointer.data.shape       )
								if "kernel" in full_name:
												_lowerCAmelCase    :    int										=      array.transpose()
								if pointer.shape == array.shape:
												_lowerCAmelCase    :    int										=      torch.from_numpy(UpperCamelCase_       )
								else:
												raise ValueError(
												    F"Shape mismatch in layer {full_name}: Model expects shape {pointer.shape} but layer contains shape:"
												    F" {array.shape}"       )
								logger.info(F"Successfully set variable {full_name} to PyTorch layer {trace}"       )
				return model




def 							_UpperCAmelCase  (UpperCamelCase_ : str      ,    UpperCamelCase_ : List[str]      ,    UpperCamelCase_ : str       ):
				'''simple docstring'''
				logger.info(F"Loading model based on config from {config_path}..."       )
				_lowerCAmelCase    :    int										=      BertConfig.from_json_file(UpperCamelCase_       )
				_lowerCAmelCase    :    Optional[Any]										=      BertModel(UpperCamelCase_       )

				# Load weights from checkpoint
				logger.info(F"Loading weights from checkpoint {tf_checkpoint_path}..."       )
				load_tfa_weights_in_bert(UpperCamelCase_      ,    UpperCamelCase_      ,    UpperCamelCase_       )

				# Save pytorch-model
				logger.info(F"Saving PyTorch model to {pytorch_dump_path}..."       )
				torch.save(model.state_dict()      ,    UpperCamelCase_       )


if __name__ == "__main__":
	_lowerCamelCase      :  List[Any]      						=						argparse.ArgumentParser()
	parser.add_argument(
	    "--tf_checkpoint_path", type=str, required=True, help="Path to the TensorFlow 2.x checkpoint path."
	)
	parser.add_argument(
	    "--bert_config_file",
	    type=str,
	    required=True,
	    help="The config json file corresponding to the BERT model. This specifies the model architecture.",
	)
	parser.add_argument(
	    "--pytorch_dump_path",
	    type=str,
	    required=True,
	    help="Path to the output PyTorch model (must include filename).",
	)
	_lowerCamelCase      :  Union[str, Any]      						=						parser.parse_args()
	convert_tfa_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)







import json
import os
from datetime import date
from pathlib import Path

from tabulate import DataRow, TableFormat, tabulate


_lowerCamelCase      :  Optional[Any]      						=						TableFormat(
    lineabove=None,
    linebelowheader=None,
    linebetweenrows=None,
    linebelow=None,
    headerrow=DataRow("", "|", "|"),
    datarow=DataRow("", "|", "|"),
    padding=1,
    with_header_hide=None,
)


_lowerCamelCase      :  Optional[int]      						=						[]
_lowerCamelCase      :  Tuple      						=						[]

_lowerCamelCase      :  List[str]      						=						{"type": "section", "text": {"type": "plain_text", "text": "No failed tests! 🤗", "emoji": True}}

_lowerCamelCase      :  Optional[Any]      						=						[
    {
        "type": "header",
        "text": {
            "type": "plain_text",
            "text": F'''🤗 Accelerate nightly {os.environ.get('TEST_TYPE', '')} test results''',
            "emoji": True,
        },
    }
]

_lowerCamelCase      :  str      						=						0
for log in Path().glob("*.log"):
	_lowerCamelCase      :  str      						=						0
	with open(log, "r") as f:
		for line in f:
			_lowerCamelCase      :  Optional[Any]      						=						json.loads(line)
			if line.get("nodeid", "") != "":
				_lowerCamelCase      :  str      						=						line["nodeid"]
				if line.get("duration", None) is not None:
					_lowerCamelCase      :  str      						=						F'''{line['duration']:.4f}'''
					if line.get("outcome", "") == "failed":
						section_num_failed += 1
						failed.append([test, duration, log.name.split("_")[0]])
						total_num_failed += 1
	group_info.append([str(log), section_num_failed, failed])
	_lowerCamelCase      :  Optional[int]      						=						[]
	log.unlink()

_lowerCamelCase      :  str      						=						""
_lowerCamelCase      :  Optional[Any]      						=						[]
if total_num_failed > 0:
	for name, num_failed, failed_tests in group_info:
		if num_failed > 0:
			if num_failed == 1:
				message += F"*{name[1:]}: {num_failed} failed test*\n"
			else:
				message += F"*{name[1:]}: {num_failed} failed tests*\n"
			_lowerCamelCase      :  Any      						=						[]
			_lowerCamelCase      :  Dict      						=						{}
			for test in failed_tests:
				_lowerCamelCase      :  Union[str, Any]      						=						test[0].split("::")
				_lowerCamelCase      :  str      						=						data[0].split("/")[-1]
				if data[0] not in filesafailed:
					_lowerCamelCase      :  List[str]      						=						[data[1:]]
				else:
					filesafailed[data[0]] += [data[1:]]
				failed_table.append(data)

			_lowerCamelCase      :  List[Any]      						=						[test[0] for test in failed_table]
			_lowerCamelCase      :  Optional[Any]      						=						list(set(files))
			# Count number of instances in failed_tests
			_lowerCamelCase      :  Union[str, Any]      						=						[]
			for file in individual_files:
				table.append([file, len(filesafailed[file])])

			_lowerCamelCase      :  Optional[Any]      						=						tabulate(
			    table,
			    headers=["Test Location", "Num Failed"],
			    tablefmt=hf_table_format,
			    stralign="right",
			)
			message += F"\n```\n{failed_table}\n```"
			all_filesafailed.append(filesafailed)
	if len(message) > 3_0_0_0:
		_lowerCamelCase      :  List[str]      						=						"Too many failed tests, please see the full report in the Action results."
		_lowerCamelCase      :  Tuple      						=						len(err) + 1_0
		_lowerCamelCase      :  Union[str, Any]      						=						message[: 3_0_0_0 - offset] + F'''\n...\n```\n{err}'''
	print(F'''### {message}''')
else:
	_lowerCamelCase      :  List[str]      						=						"No failed tests! 🤗"
	print(F'''## {message}''')
	payload.append(no_error_payload)

if os.environ.get("TEST_TYPE", "") != "":
	from slack_sdk import WebClient

	_lowerCamelCase      :  Optional[int]      						=						WebClient(token=os.environ["SLACK_API_TOKEN"])
	if message != "No failed tests! 🤗":
		_lowerCamelCase      :  int      						=						{
		    "type": "section",
		    "text": {
		        "type": "mrkdwn",
		        "text": message,
		    },
		}
		payload.append(md_report)
		_lowerCamelCase      :  List[Any]      						=						{
		    "type": "section",
		    "text": {
		        "type": "mrkdwn",
		        "text": "*For more details:*",
		    },
		    "accessory": {
		        "type": "button",
		        "text": {
		            "type": "plain_text",
		            "text": "Check Action results",
		            "emoji": True,
		        },
		        "url": F'''https://github.com/{os.environ['GITHUB_REPOSITORY']}/actions/runs/{os.environ['GITHUB_RUN_ID']}''',
		    },
		}
		payload.append(action_button)
		_lowerCamelCase      :  Union[str, Any]      						=						{
		    "type": "context",
		    "elements": [
		        {
		            "type": "plain_text",
		            "text": F'''Nightly {os.environ.get('TEST_TYPE')} test results for {date.today()}''',
		        }
		    ],
		}
		payload.append(date_report)
	_lowerCamelCase      :  int      						=						client.chat_postMessage(channel="#accelerate-ci-daily", text=message, blocks=payload)
	_lowerCamelCase      :  List[Any]      						=						response.data["ts"]
	for failed_file in all_filesafailed:
		for test_location, test_failures in failed_file.items():
			# Keep only the first instance of the test name
			_lowerCamelCase      :  Dict      						=						""
			for i, row in enumerate(test_failures):
				if row[0] != test_class:
					_lowerCamelCase      :  Optional[int]      						=						row[0]
				else:
					_lowerCamelCase      :  List[str]      						=						""

			_lowerCamelCase      :  int      						=						{
			    "type": "section",
			    "text": {
			        "type": "mrkdwn",
			        "text": F'''Test location: {test_location}\n```\n{tabulate(test_failures, headers=['Class', 'Test'], tablefmt=hf_table_format, stralign='right')}\n```''',
			    },
			}

			client.chat_postMessage(
			    channel="#accelerate-ci-daily",
			    thread_ts=ts,
			    blocks=[payload],
			)

