




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


_snake_case            = WebClient(token=os.environ["CI_SLACK_BOT_TOKEN"])







def        lowerCAmelCase_			(  snake_case_  ):
     _A							:  List[str]    =		test_results.split(""" """  )

     _A							:  Optional[int]    =		0
     _A							:  Tuple    =		0

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

     for i, expression in enumerate(snake_case_  ):
          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_			(  snake_case_  ):
     _A							:  Dict    =		{}
     _A							:  Optional[Any]    =		None
     _A							:  Union[str, Any]    =		False
     for line in failures_short_lines.split("""\n"""  ):
          if re.search(r"""_ \[doctest\]""",snake_case_  ):
               _A							:  int    =		True
               _A							:  Optional[int]    =		line.split(""" """  )[2]
          elif in_error and not line.split(""" """  )[0].isdigit():
               _A							:  int    =		line
               _A							:  Dict    =		False

     return failures







class 		lowercase							:

     def __init__(						self	,  _a	,  _a       )		->							str:
          _A							:  Dict    =		title

          _A							:  Dict    =		doc_test_results["""time_spent"""].split(""","""       )[0]
          _A							:  Optional[Any]    =		doc_test_results["""success"""]
          _A							:  List[Any]    =		doc_test_results["""failures"""]
          _A							:  Any    =		self.n_success + self.n_failures

          # Failures and success of the modeling tests
          _A							:  List[str]    =		doc_test_results

     @property
     def       a__			(						self       )		->							str:
          _A							:  str    =		[self._time_spent]
          _A							:  List[Any]    =		0

          for time in time_spent:
               _A							:  Optional[int]    =		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:
                    _A							:  List[str]    =		[0, 0, time_parts[0]]

               _A							:  Any    =		int(time_parts[0]       ), int(time_parts[1]       ), float(time_parts[2]       )
               total_secs += hours * 3600 + minutes * 60 + seconds

          _A							:  Optional[Any]    =		total_secs // 3600, (total_secs % 3600) // 60, total_secs % 60
          return F'''{int(_a       )}h{int(_a       )}m{int(_a       )}s'''

     @property
     def       a__			(						self       )		->							Dict:
          return {"type": "header", "text": {"type": "plain_text", "text": self.title}}

     @property
     def       a__			(						self       )		->							Dict:
          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       )		->							Dict:
          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       )		->							Dict:
          _A							:  Tuple    =		40
          _A							:  Optional[Any]    =		{k: v["""failed"""] for k, v in doc_test_results.items() if isinstance(_a	,  _a       )}

          _A							:  Tuple    =		""""""
          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       )		->							str:
          _A							:  Tuple    =		[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__			(						)		->							List[str]:
          _A							:  Tuple    =		[
              {
                  """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:
          print("""Sending the following payload"""       )
          print(json.dumps({"""blocks""": json.loads(self.payload       )}       )       )

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

          _A							:  List[str]    =		client.chat_postMessage(
              channel=os.environ["""CI_SLACK_CHANNEL_ID_DAILY"""]	,  blocks=self.payload	,  text=_a	,  )

     def       a__			(						self	,  _a	,  _a	,  _a	,  _a       )		->							Tuple:
          _A							:  Dict    =		""""""
          for key, value in failures.items():
               _A							:  List[str]    =		value[:200] + """ [Truncated]""" if len(_a       ) > 250 else value
               failures_text += F'''*{key}*\n_{value}_\n\n'''

          _A							:  Any    =		job_name
          _A							:  str    =		{"""type""": """section""", """text""": {"""type""": """mrkdwn""", """text""": text}}

          if job_link is not None:
               _A							:  List[str]    =		{
                   """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       )		->							str:
          if self.thread_ts is None:
               raise ValueError("""Can only post reply if a post has been made."""       )

          _A							:  int    =		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"""       )

          _A							:  Tuple    =		sorted(self.doc_test_results.items()	,  key=lambda _a       : t[0]       )
          for job, job_result in sorted_dict:
               if len(job_result["""failures"""]       ):
                    _A							:  Tuple    =		F'''*Num failures* :{len(job_result["failed"]       )} \n'''
                    _A							:  Any    =		job_result["""failures"""]
                    _A							:  Tuple    =		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_			(  ):
     _A							:  Optional[int]    =		os.environ["""GITHUB_RUN_ID"""]
     _A							:  Tuple    =		f'''https://api.github.com/repos/huggingface/transformers/actions/runs/{run_id}/jobs?per_page=100'''
     _A							:  Tuple    =		requests.get(snake_case_  ).json()
     _A							:  str    =		{}

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

          for i in range(snake_case_  ):
               _A							:  Optional[Any]    =		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.""",snake_case_  )

     return {}







def        lowerCAmelCase_			(  snake_case_  ):
     _A							:  Union[str, Any]    =		{}

     if os.path.exists(snake_case_  ):
          _A							:  Optional[Any]    =		os.listdir(snake_case_  )
          for file in files:
               try:
                    with open(os.path.join(snake_case_,snake_case_  ),encoding="""utf-8"""  ) as f:
                         _A							:  int    =		f.read()
               except UnicodeDecodeError as e:
                    raise ValueError(f'''Could not open {os.path.join(snake_case_,snake_case_  )}.'''  ) from e

     return _artifact







def        lowerCAmelCase_			(  ):







     class 		lowercase							:

          def __init__(						self	,  _a       )		->							Optional[Any]:
               _A							:  str    =		name
               _A							:  Any    =		[]

          def __str__(						self       )		->							Optional[Any]:
               return self.name



          def       a__			(						self	,  _a       )		->							Tuple:
               self.paths.append({"""name""": self.name, """path""": path}       )


     _A							:  Dict[str, Artifact]    =		{}

     _A							:  Optional[int]    =		filter(os.path.isdir,os.listdir()  )
     for directory in directories:
          _A							:  Optional[int]    =		directory
          if artifact_name not in _available_artifacts:
               _A							:  Optional[int]    =		Artifact(snake_case_  )

               _available_artifacts[artifact_name].add_path(snake_case_  )

     return _available_artifacts


if __name__ == "__main__":
  _snake_case            = get_job_links()
  _snake_case            = retrieve_available_artifacts()

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

  # Link to the GitHub Action job
  _snake_case            = github_actions_job_links.get("run_doctests")

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

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

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

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

            _snake_case            = all_failures[test] if test in all_failures else "N/A"
            _snake_case            = failure
            break

  _snake_case            = Message("🤗 Results of the doc tests.", doc_test_results)
  message.post()
  message.post_reply()







import os
import re
import shutil
import sys
import tempfile
import unittest

import black


_snake_case            = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, "utils"))

import check_copies  # noqa: E402


# This is the reference code that will be used in the tests.
# If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated.
_snake_case            = "    def __init__(self, config):\n        super().__init__()\n        self.transform = BertPredictionHeadTransform(config)\n\n        # The output weights are the same as the input embeddings, but there is\n        # an output-only bias for each token.\n        self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n        self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n        # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n        self.decoder.bias = self.bias\n\n    def forward(self, hidden_states):\n        hidden_states = self.transform(hidden_states)\n        hidden_states = self.decoder(hidden_states)\n        return hidden_states\n"







class 		lowercase							(     unittest.TestCase	):

     def       a__			(						self       )		->							Union[str, Any]:
           _A							:  List[str]    =		tempfile.mkdtemp()
           os.makedirs(os.path.join(self.transformer_dir	,  """models/bert/"""       )       )
           _A							:  str    =		self.transformer_dir
           shutil.copy(
               os.path.join(_a	,  """src/transformers/models/bert/modeling_bert.py"""       )	,  os.path.join(self.transformer_dir	,  """models/bert/modeling_bert.py"""       )	,  )

     def       a__			(						self       )		->							Optional[int]:
           _A							:  List[str]    =		"""src/transformers"""
           shutil.rmtree(self.transformer_dir       )

     def       a__			(						self	,  _a	,  _a	,  _a	,  _a=None       )		->							Optional[Any]:
           _A							:  Optional[Any]    =		comment + F'''\nclass {class_name}(nn.Module):\n''' + class_code
           if overwrite_result is not None:
                 _A							:  List[str]    =		comment + F'''\nclass {class_name}(nn.Module):\n''' + overwrite_result
           _A							:  List[str]    =		black.Mode(target_versions={black.TargetVersion.PYaa}	,  line_length=119       )
           _A							:  Optional[int]    =		black.format_str(_a	,  mode=_a       )
           _A							:  Optional[Any]    =		os.path.join(self.transformer_dir	,  """new_code.py"""       )
           with open(_a	,  """w"""	,  newline="""\n"""       ) as f:
                 f.write(_a       )
           if overwrite_result is None:
                 self.assertTrue(len(check_copies.is_copy_consistent(_a       )       ) == 0       )
           else:
                 check_copies.is_copy_consistent(f.name	,  overwrite=_a       )
                 with open(_a	,  """r"""       ) as f:
                       self.assertTrue(f.read()	,  _a       )

     def       a__			(						self       )		->							str:
           _A							:  Union[str, Any]    =		check_copies.find_code_in_transformers("""models.bert.modeling_bert.BertLMPredictionHead"""       )
           self.assertEqual(_a	,  _a       )

     def       a__			(						self       )		->							int:
           # Base copy consistency
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead"""	,  """BertLMPredictionHead"""	,  REFERENCE_CODE + """\n"""	,  )

           # With no empty line at the end
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead"""	,  """BertLMPredictionHead"""	,  _a	,  )

           # Copy consistency with rename
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel"""	,  """TestModelLMPredictionHead"""	,  re.sub("""Bert"""	,  """TestModel"""	,  _a       )	,  )

           # Copy consistency with a really long name
           _A							:  List[str]    =		"""TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason"""
           self.check_copy_consistency(
               F'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}'''	,  F'''{long_class_name}LMPredictionHead'''	,  re.sub("""Bert"""	,  _a	,  _a       )	,  )

           # Copy consistency with overwrite
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel"""	,  """TestModelLMPredictionHead"""	,  _a	,  overwrite_result=re.sub("""Bert"""	,  """TestModel"""	,  _a       )	,  )



     def       a__			(						self       )		->							Tuple:
           _A							:  Union[str, Any]    =		check_copies.LOCALIZED_READMES["""README_zh-hans.md"""]

           _A							:  str    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the"""
               """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for"""
               """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong"""
               """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1."""
               """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),"""
               """ released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and"""
               """ lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same"""
               """ method has been applied to compress GPT2 into"""
               """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into"""
               """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),"""
               """ Multilingual BERT into"""
               """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German"""
               """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**"""
               """ (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders"""
               """ as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang"""
               """ Luong, Quoc V. Le, Christopher D. Manning."""
           )
           _A							:  str    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the"""
               """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n"""
           )
           _A							:  Any    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the"""
               """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1."""
               """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文"""
               """ [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and"""
               """ lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same"""
               """ method has been applied to compress GPT2 into"""
               """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into"""
               """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),"""
               """ Multilingual BERT into"""
               """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German"""
               """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自"""
               """ Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather"""
               """ than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,"""
               """ Christopher D. Manning 发布。\n"""
           )

           _A    ,     _A							:  Tuple    =		check_copies.convert_to_localized_md(
               _a	,  _a	,  localized_readme["""format_model_list"""]       )

           self.assertFalse(_a       )
           self.assertEqual(_a	,  _a       )

           _A    ,     _A							:  List[str]    =		check_copies.convert_to_localized_md(
               _a	,  _a	,  localized_readme["""format_model_list"""]       )

           # Check whether the number of models is equal to README.md after conversion.
           self.assertTrue(_a       )

           _A							:  Tuple    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the"""
               """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for"""
               """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong"""
               """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut."""
           )
           _A							:  Dict    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and"""
               """ the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n"""
           )
           _A							:  Optional[Any]    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the"""
               """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n"""
           )

           _A    ,     _A							:  Optional[int]    =		check_copies.convert_to_localized_md(
               _a	,  _a	,  localized_readme["""format_model_list"""]       )

           # Check if the model link is synchronized.
           self.assertEqual(_a	,  _a       )

