

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:     str							=      WebClient(token=os.environ["""CI_SLACK_BOT_TOKEN"""])



def    __lowerCAmelCase   (    A     ):
	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(lowercase_     ):
		if "failed" in expression:
			failed += int(expressions[i - 1]     )
		if "passed" in expression:
			success += int(expressions[i - 1]     )

	return failed, success, time_spent



def    __lowerCAmelCase   (    A     ):
	UpperCAmelCase_												=      {}
	UpperCAmelCase_												=      None
	UpperCAmelCase_												=      False
	for line in failures_short_lines.split("\n"     ):
		if re.search(r"_ \[doctest\]"							,  lowercase_     ):
			UpperCAmelCase_												=      True
			UpperCAmelCase_												=      line.split(" "     )[2]
		elif in_error and not line.split(" "     )[0].isdigit():
			UpperCAmelCase_												=      line
			UpperCAmelCase_												=      False

	return failures

class     __UpperCamelCase     :




	def __init__(		self						:    str		,						lowerCAmelCase						:    List[str]		,						lowerCAmelCase						:    Dict				):


		'''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    __A       (		self						:    List[Any]				):


		'''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_												=      int(time_parts[0]				), int(time_parts[1]				), float(time_parts[2]				)
			total_secs += hours * 3_600 + minutes * 60 + seconds

		UpperCAmelCase_												=      total_secs // 3_600, (total_secs % 3_600) // 60, total_secs % 60
		return F"{int(A__				)}h{int(A__				)}m{int(A__				)}s"




	@property
	def    __A       (		self						:    List[str]				):


		'''simple docstring'''







		return {"type": "header", "text": {"type": "plain_text", "text": self.title}}




	@property
	def    __A       (		self						:    Optional[int]				):


		'''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    __A       (		self						:    int				):


		'''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    __A       (		self						:    int				):


		'''simple docstring'''







		UpperCAmelCase_												=      40
		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    __A       (		self						:    Dict				):


		'''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    __A       (		):


		'''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    __A       (		self						:    Dict				):


		'''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    __A       (		self						:    List[Any]		,						lowerCAmelCase						:    Tuple		,						lowerCAmelCase						:    Tuple		,						lowerCAmelCase						:    Any		,						lowerCAmelCase						:    Optional[Any]				):


		'''simple docstring'''







		UpperCAmelCase_												=      """"""
		for key, value in failures.items():
			UpperCAmelCase_												=      value[:200] + """ [Truncated]""" if len(A__				) > 250 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    __A       (		self						:    Dict				):


		'''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 lowerCAmelCase				: 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    __lowerCAmelCase   (    ):
	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(lowercase_     ).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(lowercase_     ):
			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."							,  lowercase_     )

	return {}



def    __lowerCAmelCase   (    A     ):
	UpperCAmelCase_												=      {}

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

	return _artifact



def    __lowerCAmelCase   (    ):

	class     __UpperCamelCase     :




		def __init__(		self						:    str		,						lowerCAmelCase						:    List[Any]				):


			'''simple docstring'''







			UpperCAmelCase_												=      name
			UpperCAmelCase_												=      []




		def __str__(		self						:    Tuple				):


			'''simple docstring'''







			return self.name




		def    __A       (		self						:    str		,						lowerCAmelCase						:    int				):


			'''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(lowercase_     )

			_available_artifacts[artifact_name].add_path(lowercase_     )

	return _available_artifacts


if __name__ == "__main__":
				_a:     str							=      get_job_links()
				_a:     Dict							=      retrieve_available_artifacts()

				_a:     int							=      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:     int							=      {
				    v: {
				        'failed': [],
				        'failures': {},
				    }
				    for v in docs.values()
				}

				# Link to the GitHub Action job
				_a:     Optional[Any]							=      github_actions_job_links.get("""run_doctests""")

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

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

																if "::" in line:
																				_a:     Dict							=      line.split("""::""")
																else:
																				_a:     Dict							=      line, line

																for file_regex in docs.keys():
																				if fnmatch(file_path, file_regex):
																								_a:     List[str]							=      docs[file_regex]
																								doc_test_results[category]["failed"].append(test)

																								_a:     Optional[int]							=      all_failures[test] if test in all_failures else 'N/A'
																								_a:     List[str]							=      failure
																								break

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


from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast
from ...utils import logging


A_			:					List[Any]    			=     logging.get_logger(__name__)

A_			:					Any    			=     {
    'EleutherAI/gpt-neo-1.3B': 'https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json',
    # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo
}



class        _a		(__magic_name__							):


					'''simple docstring'''






					UpperCAmelCase__:  List[str]    				=			'''gpt_neo'''
					UpperCAmelCase__:  str    				=			['''past_key_values''']
					UpperCAmelCase__:  Dict    				=			{'''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''}






					def __init__(					self       ,			A__=5_0257       ,			A__=2048       ,			A__=2048       ,			A__=24       ,			A__=[[["global", "local"], 12]]       ,			A__=16       ,			A__=None       ,			A__=256       ,			A__="gelu_new"       ,			A__=0.0       ,			A__=0.0       ,			A__=0.0       ,			A__=0.1       ,			A__=1e-5       ,			A__=0.0_2       ,			A__=True       ,			A__=5_0256       ,			A__=5_0256       ,			**A__       ,			):
						A__				:       Optional[Any]	  =						vocab_size
						A__				:       Optional[int]	  =						max_position_embeddings
						A__				:       Optional[int]	  =						hidden_size
						A__				:       Dict	  =						num_layers
						A__				:       List[str]	  =						num_heads
						A__				:       Optional[int]	  =						intermediate_size
						A__				:       Union[str, Any]	  =						window_size
						A__				:       Optional[int]	  =						activation_function
						A__				:       Optional[Any]	  =						resid_dropout
						A__				:       Optional[int]	  =						embed_dropout
						A__				:       Any	  =						attention_dropout
						A__				:       Optional[Any]	  =						classifier_dropout
						A__				:       int	  =						layer_norm_epsilon
						A__				:       Tuple	  =						initializer_range
						A__				:       str	  =						use_cache

						A__				:       List[str]	  =						bos_token_id
						A__				:       List[Any]	  =						eos_token_id

						A__				:       Any	  =						attention_types
						A__				:       List[str]	  =						self.expand_attention_types_params(A__     )

						if len(self.attention_layers     ) != self.num_layers:
							raise ValueError(
							    """Configuration for convolutional module is incorrect. """
							    """It is required that `len(config.attention_layers)` == `config.num_layers` """
							    F"""but is `len(config.attention_layers) = {len(self.attention_layers     )}`, """
							    F"""`config.num_layers = {self.num_layers}`. """
							    """`config.attention_layers` is prepared using `config.attention_types`. """
							    """Please verify the value of `config.attention_types` argument."""     )

						super().__init__(bos_token_id=A__       ,			eos_token_id=A__       ,			**A__     )






					@staticmethod
					def     __A     (					A__     ):
						A__				:       int	  =						[]
						for item in attention_types:
							for _ in range(item[1]     ):
								attentions.extend(item[0]     )
						return attentions



def 		UpperCamelCase   (lowercase_:					Dict       , lowercase_:					Any       , lowercase_:					Union[str, Any]       , lowercase_:					Optional[Any]   )	-> Dict:
	import torch

	A__				:       List[Any]	  =						input.size()
	A__				:       List[str]	  =						len(lowercase_   )
	A__				:       Union[str, Any]	  =						shape[dimension]

	A__				:       Optional[int]	  =						torch.arange(0       , lowercase_       , lowercase_   )
	A__				:       str	  =						torch.div(sizedim - size       , lowercase_       , rounding_mode="""floor"""   ) + 1
	A__				:       Any	  =						torch.arange(lowercase_   ) + low_indices[:min_length][:, None]

	A__				:       Union[str, Any]	  =						[slice(lowercase_   )] * rank
	A__				:       Any	  =						indices
	A__				:       List[str]	  =						input[s]

	A__				:       Dict	  =						list(range(0       , rank + 1   )   )
	perm.append(perm.pop(dimension + 1   )   )

	return sliced.permute(lowercase_   )

def 		UpperCamelCase   (lowercase_:					str       , lowercase_:					int   )	-> int:
	import torch

	A__				:       Union[str, Any]	  =						torch.arange(1       , lowercase_   )
	A__				:       str	  =						torch.remainder(lowercase_       , lowercase_   )
	A__				:       Optional[Any]	  =						remainders == 0
	A__				:       Any	  =						candidates[divisor_indices]
	A__				:       Optional[Any]	  =						torch.max(lowercase_   )
	return largest_divisor, torch.div(lowercase_       , lowercase_       , rounding_mode="""floor"""   )



class        _a		(__magic_name__							):


					'''simple docstring'''






					@property
					def     __A     (					self     ):
						A__				:       Any	  =						OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}     )
						if self.use_past:
							self.fill_with_past_key_values_(A__       ,			direction="""inputs"""     )
							A__				:       Optional[Any]	  =						{0: """batch""", 1: """past_sequence + sequence"""}
						else:
							A__				:       List[str]	  =						{0: """batch""", 1: """sequence"""}

						return common_inputs






					@property
					def     __A     (					self     ):
						return self._config.num_heads






					def     __A     (					self       ,			A__       ,			A__ = -1       ,			A__ = -1       ,			A__ = False       ,			A__ = None       ,			):
						A__				:       int	  =						super(A__       ,			self     ).generate_dummy_inputs(
						    A__       ,			batch_size=A__       ,			seq_length=A__       ,			is_pair=A__       ,			framework=A__     )

						# We need to order the input in the way they appears in the forward()
						A__				:       Optional[int]	  =						OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}     )

						# Need to add the past_keys
						if self.use_past:
							if not is_torch_available():
								raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""     )
							else:
								import torch

								A__  ,  A__				:       Dict	  =						common_inputs["""input_ids"""].shape
								# Not using the same length for past_key_values
								A__				:       int	  =						seqlen + 2
								A__				:       int	  =						(
								    batch,
								    self.num_attention_heads,
								    past_key_values_length,
								    self._config.hidden_size // self.num_attention_heads,
								)
								A__				:       Dict	  =						[
								    (torch.zeros(A__     ), torch.zeros(A__     )) for _ in range(self.num_layers     )
								]

						A__				:       List[Any]	  =						common_inputs["""attention_mask"""]
						if self.use_past:
							A__				:       int	  =						ordered_inputs["""attention_mask"""].dtype
							A__				:       str	  =						torch.cat(
							    [ordered_inputs["""attention_mask"""], torch.ones(A__       ,			A__       ,			dtype=A__     )]       ,			dim=1     )

						return ordered_inputs






					@property
					def     __A     (					self     ):
						return 13



