





import collections
import json
import math
import os
import re
import time
from fnmatch import fnmatch
from typing import Dict

import requests
from slack_sdk import WebClient


__A				:    Union[str, Any]       =   WebClient(token=os.environ["CI_SLACK_BOT_TOKEN"])





def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__ )  ->				List[Any]:







			'''simple docstring'''

			UpperCAmelCase   		=				test_results.split(''' ''' )

			UpperCAmelCase   		=				0
			UpperCAmelCase   		=				0

			# When the output is short enough, the output is surrounded by = signs: "== OUTPUT =="
			# When it is too long, those signs are not present.
			UpperCAmelCase   		=				expressions[-2] if '''=''' in expressions[-1] else expressions[-1]

			for i, expression in enumerate(UpperCamelCase__ ):
						if "failed" in expression:
									failed += int(expressions[i - 1] )
						if "passed" in expression:
									success += int(expressions[i - 1] )

			return failed, success, time_spent





def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__ )  ->				str:







			'''simple docstring'''

			UpperCAmelCase   		=				{}
			UpperCAmelCase   		=				None
			UpperCAmelCase   		=				False
			for line in failures_short_lines.split('''\n''' ):
						if re.search(R'''_ \[doctest\]'''			,			UpperCamelCase__ ):
									UpperCAmelCase   		=				True
									UpperCAmelCase   		=				line.split(''' ''' )[2]
						elif in_error and not line.split(''' ''' )[0].isdigit():
									UpperCAmelCase   		=				line
									UpperCAmelCase   		=				False

			return failures

class 	A_       :
	def __init__(    self							,					_A							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				title

				UpperCAmelCase   		=				doc_test_results['''time_spent'''].split(''','''		)[0]
				UpperCAmelCase   		=				doc_test_results['''success''']
				UpperCAmelCase   		=				doc_test_results['''failures''']
				UpperCAmelCase   		=				self.n_success + self.n_failures

				# Failures and success of the modeling tests
				UpperCAmelCase   		=				doc_test_results
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				[self._time_spent]
				UpperCAmelCase   		=				0

				for time in time_spent:
							UpperCAmelCase   		=				time.split(''':'''		)

							# Time can be formatted as xx:xx:xx, as .xx, or as x.xx if the time spent was less than a minute.
							if len(_A		) == 1:
										UpperCAmelCase   		=				[0, 0, time_parts[0]]

							UpperCAmelCase		,			UpperCAmelCase		,			UpperCAmelCase   		=				int(time_parts[0]		), int(time_parts[1]		), float(time_parts[2]		)
							total_secs += hours * 3_6_0_0 + minutes * 6_0 + seconds

				UpperCAmelCase		,			UpperCAmelCase		,			UpperCAmelCase   		=				total_secs // 3_6_0_0, (total_secs % 3_6_0_0) // 6_0, total_secs % 6_0
				return F"""{int(_A		)}h{int(_A		)}m{int(_A		)}s"""
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				return {"type": "header", "text": {"type": "plain_text", "text": self.title}}
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				return {
				    "type": "section",
				    "text": {
				        "type": "plain_text",
				        "text": F"""🌞 There were no failures: all {self.n_tests} tests passed. The suite ran in {self.time}.""",
				        "emoji": True,
				    },
				    "accessory": {
				        "type": "button",
				        "text": {"type": "plain_text", "text": "Check Action results", "emoji": True},
				        "url": F"""https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}""",
				    },
				}
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				return {
				    "type": "section",
				    "text": {
				        "type": "plain_text",
				        "text": (
				            F"""There were {self.n_failures} failures, out of {self.n_tests} tests.\nThe suite ran in"""
				            F""" {self.time}."""
				        ),
				        "emoji": True,
				    },
				    "accessory": {
				        "type": "button",
				        "text": {"type": "plain_text", "text": "Check Action results", "emoji": True},
				        "url": F"""https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}""",
				    },
				}
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				4_0
				UpperCAmelCase   		=				{k: v['''failed'''] for k, v in doc_test_results.items() if isinstance(_A							,					_A		)}

				UpperCAmelCase   		=				''''''
				for category, failures in category_failures.items():
							if len(_A		) == 0:
										continue

							if report != "":
										report += "\n\n"

							report += F"""*{category} failures*:""".ljust(line_length // 2		).rjust(line_length // 2		) + "\n"
							report += "`"
							report += "`\n`".join(_A		)
							report += "`"

				return {
				    "type": "section",
				    "text": {
				        "type": "mrkdwn",
				        "text": F"""The following examples had failures:\n\n\n{report}\n""",
				    },
				}
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				[self.header]

				if self.n_failures > 0:
							blocks.append(self.failures		)

				if self.n_failures > 0:
							blocks.extend([self.category_failures]		)

				if self.n_failures == 0:
							blocks.append(self.no_failures		)

				return json.dumps(_A		)
	@staticmethod
	def      _lowercase				(    ):






				'''simple docstring'''







				UpperCAmelCase   		=				[
				    {
				        '''type''': '''section''',
				        '''text''': {
				            '''type''': '''plain_text''',
				            '''text''': '''There was an issue running the tests.''',
				        },
				        '''accessory''': {
				            '''type''': '''button''',
				            '''text''': {'''type''': '''plain_text''', '''text''': '''Check Action results''', '''emoji''': True},
				            '''url''': F"""https://github.com/huggingface/transformers/actions/runs/{os.environ["GITHUB_RUN_ID"]}""",
				        },
				    }
				]

				print('''Sending the following payload'''		)
				print(json.dumps({'''blocks''': json.loads(_A		)}		)		)

				client.chat_postMessage(
				    channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY''']							,					text='''There was an issue running the tests.'''							,					blocks=_A							,					)
	def      _lowercase				(    self		):






				'''simple docstring'''







				print('''Sending the following payload'''		)
				print(json.dumps({'''blocks''': json.loads(self.payload		)}		)		)

				UpperCAmelCase   		=				F"""{self.n_failures} failures out of {self.n_tests} tests,""" if self.n_failures else '''All tests passed.'''

				UpperCAmelCase   		=				client.chat_postMessage(
				    channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY''']							,					blocks=self.payload							,					text=_A							,					)
	def      _lowercase				(    self							,					_A							,					_A							,					_A							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				''''''
				for key, value in failures.items():
							UpperCAmelCase   		=				value[:2_0_0] + ''' [Truncated]''' if len(_A		) > 2_5_0 else value
							failures_text += F"""*{key}*\n_{value}_\n\n"""

				UpperCAmelCase   		=				job_name
				UpperCAmelCase   		=				{'''type''': '''section''', '''text''': {'''type''': '''mrkdwn''', '''text''': text}}

				if job_link is not None:
							UpperCAmelCase   		=				{
							    '''type''': '''button''',
							    '''text''': {'''type''': '''plain_text''', '''text''': '''GitHub Action job''', '''emoji''': True},
							    '''url''': job_link,
							}

				return [
				    {"type": "header", "text": {"type": "plain_text", "text": title.upper(), "emoji": True}},
				    content,
				    {"type": "section", "text": {"type": "mrkdwn", "text": failures_text}},
				]







	def      _lowercase				(    self		):






				'''simple docstring'''







				if self.thread_ts is None:
							raise ValueError('''Can only post reply if a post has been made.'''		)

				UpperCAmelCase   		=				self.doc_test_results.pop('''job_link'''		)
				self.doc_test_results.pop('''failures'''		)
				self.doc_test_results.pop('''success'''		)
				self.doc_test_results.pop('''time_spent'''		)

				UpperCAmelCase   		=				sorted(self.doc_test_results.items()							,					key=lambda _A		: t[0]		)
				for job, job_result in sorted_dict:
							if len(job_result['''failures''']		):
										UpperCAmelCase   		=				F"""*Num failures* :{len(job_result["failed"]		)} \n"""
										UpperCAmelCase   		=				job_result['''failures''']
										UpperCAmelCase   		=				self.get_reply_blocks(_A							,					_A							,					_A							,					text=_A		)

										print('''Sending the following reply'''		)
										print(json.dumps({'''blocks''': blocks}		)		)

										client.chat_postMessage(
										    channel=os.environ['''CI_SLACK_CHANNEL_ID_DAILY''']							,					text=F"""Results for {job}"""							,					blocks=_A							,					thread_ts=self.thread_ts['''ts''']							,					)

										time.sleep(1		)





def 						__SCREAMING_SNAKE_CASE  (				)  ->				Dict:







			'''simple docstring'''

			UpperCAmelCase   		=				os.environ['''GITHUB_RUN_ID''']
			UpperCAmelCase   		=				F"""https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100"""
			UpperCAmelCase   		=				requests.get(UpperCamelCase__ ).json()
			UpperCAmelCase   		=				{}

			try:
						jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} )
						UpperCAmelCase   		=				math.ceil((result['''total_count'''] - 100) / 100 )

						for i in range(UpperCamelCase__ ):
									UpperCAmelCase   		=				requests.get(url + F"""&page={i + 2}""" ).json()
									jobs.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']} )

						return jobs
			except Exception as e:
						print('''Unknown error, could not fetch links.'''			,			UpperCamelCase__ )

			return {}





def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__ )  ->				Tuple:







			'''simple docstring'''

			UpperCAmelCase   		=				{}

			if os.path.exists(UpperCamelCase__ ):
						UpperCAmelCase   		=				os.listdir(UpperCamelCase__ )
						for file in files:
									try:
												with open(os.path.join(UpperCamelCase__			,			UpperCamelCase__ )			,			encoding='''utf-8''' ) as f:
															UpperCAmelCase   		=				f.read()
									except UnicodeDecodeError as e:
												raise ValueError(F"""Could not open {os.path.join(UpperCamelCase__			,			UpperCamelCase__ )}.""" ) from e

			return _artifact





def 						__SCREAMING_SNAKE_CASE  (				)  ->				List[Any]:







			'''simple docstring'''

			class 	A_       :
				def __init__(    self							,					_A		):






							'''simple docstring'''







							UpperCAmelCase   		=				name
							UpperCAmelCase   		=				[]
				def __str__(    self		):






							'''simple docstring'''







							return self.name







				def      _lowercase				(    self							,					_A		):






							'''simple docstring'''







							self.paths.append({'''name''': self.name, '''path''': path}		)
			UpperCAmelCase   		=				{}

			UpperCAmelCase   		=				filter(os.path.isdir			,			os.listdir() )
			for directory in directories:
						UpperCAmelCase   		=				directory
						if artifact_name not in _available_artifacts:
									UpperCAmelCase   		=				Artifact(UpperCamelCase__ )

									_available_artifacts[artifact_name].add_path(UpperCamelCase__ )

			return _available_artifacts


if __name__ == "__main__":
				__A				:    Any       =   get_job_links()
				__A				:    Union[str, Any]       =   retrieve_available_artifacts()

				__A				:    List[Any]       =   collections.OrderedDict(
				    [
				        ("*.py", "API Examples"),
				        ("*.md", "MD Examples"),
				    ]
				)

				# This dict will contain all the information relative to each doc test category:
				# - failed: list of failed tests
				# - failures: dict in the format 'test': 'error_message'
				__A				:    str       =   {
				    v: {
				        "failed": [],
				        "failures": {},
				    }
				    for v in docs.values()
				}

				# Link to the GitHub Action job
				__A				:    Tuple       =   github_actions_job_links.get("run_doctests")

				__A				:    List[Any]       =   available_artifacts["doc_tests_gpu_test_reports"].paths[0]
				__A				:    Optional[int]       =   retrieve_artifact(artifact_path["name"])
				if "stats" in artifact:
								__A    , __A    , __A				:    List[Any]       =   handle_test_results(artifact["stats"])
								__A				:    List[Any]       =   failed
								__A				:    Optional[int]       =   success
								__A				:    int       =   time_spent[1:-1] + ", "

								__A				:    List[str]       =   extract_first_line_failure(artifact["failures_short"])
								for line in artifact["summary_short"].split("\n"):
												if re.search("FAILED", line):
																__A				:    str       =   line.replace("FAILED ", "")
																__A				:    List[Any]       =   line.split()[0].replace("\n", "")

																if "::" in line:
																				__A    , __A				:    List[str]       =   line.split("::")
																else:
																				__A    , __A				:    Union[str, Any]       =   line, line

																for file_regex in docs.keys():
																				if fnmatch(file_path, file_regex):
																								__A				:    Optional[int]       =   docs[file_regex]
																								doc_test_results[category]["failed"].append(test)

																								__A				:    Union[str, Any]       =   all_failures[test] if test in all_failures else "N/A"
																								__A				:    Any       =   failure
																								break

				__A				:    Optional[int]       =   Message("🤗 Results of the doc tests.", doc_test_results)
				message.post()
				message.post_reply()










import argparse

import torch

from transformers import YosoConfig, YosoForMaskedLM





def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__ )  ->				Union[str, Any]:







			'''simple docstring'''

			if "model" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''model.'''			,			'''''' )
			if "norm1" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''norm1'''			,			'''attention.output.LayerNorm''' )
			if "norm2" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''norm2'''			,			'''output.LayerNorm''' )
			if "norm" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''norm'''			,			'''LayerNorm''' )
			if "transformer" in orig_key:
						UpperCAmelCase   		=				orig_key.split('''.''' )[0].split('''_''' )[-1]
						UpperCAmelCase   		=				orig_key.replace(F"""transformer_{layer_num}"""			,			F"""encoder.layer.{layer_num}""" )
			if "mha.attn" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''mha.attn'''			,			'''attention.self''' )
			if "mha" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''mha'''			,			'''attention''' )
			if "W_q" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''W_q'''			,			'''self.query''' )
			if "W_k" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''W_k'''			,			'''self.key''' )
			if "W_v" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''W_v'''			,			'''self.value''' )
			if "ff1" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''ff1'''			,			'''intermediate.dense''' )
			if "ff2" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''ff2'''			,			'''output.dense''' )
			if "ff" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''ff'''			,			'''output.dense''' )
			if "mlm_class" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''mlm.mlm_class'''			,			'''cls.predictions.decoder''' )
			if "mlm" in orig_key:
						UpperCAmelCase   		=				orig_key.replace('''mlm'''			,			'''cls.predictions.transform''' )
			if "cls" not in orig_key:
						UpperCAmelCase   		=				'''yoso.''' + orig_key

			return orig_key





def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__			,			UpperCamelCase__ )  ->				Dict:







			'''simple docstring'''

			for key in orig_state_dict.copy().keys():
						UpperCAmelCase   		=				orig_state_dict.pop(UpperCamelCase__ )

						if ("pooler" in key) or ("sen_class" in key):
									continue
						else:
									UpperCAmelCase   		=				val

			UpperCAmelCase   		=				orig_state_dict['''cls.predictions.decoder.bias''']
			UpperCAmelCase   		=				torch.arange(UpperCamelCase__ ).expand((1, -1) ) + 2

			return orig_state_dict





def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__			,			UpperCamelCase__			,			UpperCamelCase__ )  ->				int:







			'''simple docstring'''

			UpperCAmelCase   		=				torch.load(UpperCamelCase__			,			map_location='''cpu''' )['''model_state_dict''']
			UpperCAmelCase   		=				YosoConfig.from_json_file(UpperCamelCase__ )
			UpperCAmelCase   		=				YosoForMaskedLM(UpperCamelCase__ )

			UpperCAmelCase   		=				convert_checkpoint_helper(config.max_position_embeddings			,			UpperCamelCase__ )

			print(model.load_state_dict(UpperCamelCase__ ) )
			model.eval()
			model.save_pretrained(UpperCamelCase__ )

			print(F"""Checkpoint successfuly converted. Model saved at {pytorch_dump_path}""" )


if __name__ == "__main__":
				__A				:    Any       =   argparse.ArgumentParser()
				# Required parameters
				parser.add_argument(
				    "--pytorch_model_path", default=None, type=str, required=True, help="Path to YOSO pytorch checkpoint."
				)
				parser.add_argument(
				    "--config_file",
				    default=None,
				    type=str,
				    required=True,
				    help="The json file for YOSO model config.",
				)
				parser.add_argument(
				    "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
				)
				__A				:    List[str]       =   parser.parse_args()
				convert_yoso_checkpoint(args.pytorch_model_path, args.config_file, args.pytorch_dump_path)



