





import argparse
import json
import math
import os
import time
import traceback
import zipfile
from collections import Counter

import requests



def        _a						(    UpperCAmelCase			,  UpperCAmelCase=None     )				->				int:







   """simple docstring"""
   lowerCamelCase__   :						Any					   =							None
   if token is not None:
      lowerCamelCase__   :						Any					   =							{"""Accept""": """application/vnd.github+json""", """Authorization""": f"Bearer {token}"}

   lowerCamelCase__   :						str					   =							f"https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100"
   lowerCamelCase__   :						Tuple					   =							requests.get(UpperCAmelCase__			,  headers=UpperCAmelCase__     ).json()
   lowerCamelCase__   :						List[str]					   =							{}

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

      for i in range(UpperCAmelCase__     ):
         lowerCamelCase__   :						Union[str, Any]					   =							requests.get(url + f"&page={i + 2}"			,  headers=UpperCAmelCase__     ).json()
         job_links.update({job['''name''']: job['''html_url'''] for job in result['''jobs''']}     )

      return job_links
   except Exception:
      print(f"Unknown error, could not fetch links:\n{traceback.format_exc()}"     )

   return {}



def        _a						(    UpperCAmelCase			,  UpperCAmelCase=None     )				->				int:







   """simple docstring"""
   lowerCamelCase__   :						List[Any]					   =							None
   if token is not None:
      lowerCamelCase__   :						int					   =							{"""Accept""": """application/vnd.github+json""", """Authorization""": f"Bearer {token}"}

   lowerCamelCase__   :						List[Any]					   =							f"https://api.github.com/repos/huggingface/transformers/actions/runs/{worflow_run_id}/artifacts?per_page=100"
   lowerCamelCase__   :						List[Any]					   =							requests.get(UpperCAmelCase__			,  headers=UpperCAmelCase__     ).json()
   lowerCamelCase__   :						str					   =							{}

   try:
      artifacts.update({artifact['''name''']: artifact['''archive_download_url'''] for artifact in result['''artifacts''']}     )
      lowerCamelCase__   :						Any					   =							math.ceil((result['''total_count'''] - 100) / 100     )

      for i in range(UpperCAmelCase__     ):
         lowerCamelCase__   :						Any					   =							requests.get(url + f"&page={i + 2}"			,  headers=UpperCAmelCase__     ).json()
         artifacts.update({artifact['''name''']: artifact['''archive_download_url'''] for artifact in result['''artifacts''']}     )

      return artifacts
   except Exception:
      print(f"Unknown error, could not fetch links:\n{traceback.format_exc()}"     )

   return {}



def        _a						(    UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase     )				->				Optional[int]:







   """simple docstring"""
   lowerCamelCase__   :						str					   =							None
   if token is not None:
      lowerCamelCase__   :						Any					   =							{"""Accept""": """application/vnd.github+json""", """Authorization""": f"Bearer {token}"}

   lowerCamelCase__   :						Dict					   =							requests.get(UpperCAmelCase__			,  headers=UpperCAmelCase__			,  allow_redirects=UpperCAmelCase__     )
   lowerCamelCase__   :						List[str]					   =							result.headers["""Location"""]
   lowerCamelCase__   :						Tuple					   =							requests.get(UpperCAmelCase__			,  allow_redirects=UpperCAmelCase__     )
   lowerCamelCase__   :						Optional[Any]					   =							os.path.join(UpperCAmelCase__			,  f"{artifact_name}.zip"     )
   with open(UpperCAmelCase__			,  '''wb'''     ) as fp:
      fp.write(response.content     )



def        _a						(    UpperCAmelCase			,  UpperCAmelCase=None     )				->				Optional[int]:







   """simple docstring"""
   lowerCamelCase__   :						int					   =							[]
   lowerCamelCase__   :						List[Any]					   =							[]
   lowerCamelCase__   :						Tuple					   =							None

   with zipfile.ZipFile(UpperCAmelCase__     ) as z:
      for filename in z.namelist():
         if not os.path.isdir(UpperCAmelCase__     ):
            # read the file
            if filename in ["failures_line.txt", "summary_short.txt", "job_name.txt"]:
               with z.open(UpperCAmelCase__     ) as f:
                  for line in f:
                     lowerCamelCase__   :						List[str]					   =							line.decode('''UTF-8'''     ).strip()
                     if filename == "failures_line.txt":
                        try:
                           # `error_line` is the place where `error` occurs
                           lowerCamelCase__   :						List[str]					   =							line[: line.index(''': '''     )]
                           lowerCamelCase__   :						int					   =							line[line.index(''': '''     ) + len(''': '''     ) :]
                           errors.append([error_line, error]     )
                        except Exception:
                           # skip un-related lines
                           pass
                     elif filename == "summary_short.txt" and line.startswith('''FAILED '''     ):
                        # `test` is the test method that failed
                        lowerCamelCase__   :						int					   =							line[len('''FAILED '''     ) :]
                        failed_tests.append(UpperCAmelCase__     )
                     elif filename == "job_name.txt":
                        lowerCamelCase__   :						List[Any]					   =							line

   if len(UpperCAmelCase__     ) != len(UpperCAmelCase__     ):
      raise ValueError(
          f"`errors` and `failed_tests` should have the same number of elements. Got {len(UpperCAmelCase__     )} for `errors` "
          f"and {len(UpperCAmelCase__     )} for `failed_tests` instead. The test reports in {artifact_zip_path} have some"
          ''' problem.'''     )

   lowerCamelCase__   :						Optional[int]					   =							None
   if job_name and job_links:
      lowerCamelCase__   :						Tuple					   =							job_links.get(UpperCAmelCase__			,  UpperCAmelCase__     )

   # A list with elements of the form (line of error, error, failed test)
   lowerCamelCase__   :						Tuple					   =							[x + [y] + [job_link] for x, y in zip(UpperCAmelCase__			,  UpperCAmelCase__     )]

   return result



def        _a						(    UpperCAmelCase			,  UpperCAmelCase=None     )				->				List[Any]:







   """simple docstring"""
   lowerCamelCase__   :						Optional[int]					   =							[]

   lowerCamelCase__   :						List[str]					   =							[os.path.join(UpperCAmelCase__			,  UpperCAmelCase__     ) for p in os.listdir(UpperCAmelCase__     ) if p.endswith('''.zip'''     )]
   for p in paths:
      errors.extend(get_errors_from_single_artifact(UpperCAmelCase__			,  job_links=UpperCAmelCase__     )     )

   return errors



def        _a						(    UpperCAmelCase			,  UpperCAmelCase=None     )				->				int:







   """simple docstring"""
   lowerCamelCase__   :						Any					   =							Counter()
   counter.update([x[1] for x in logs]     )
   lowerCamelCase__   :						Tuple					   =							counter.most_common()
   lowerCamelCase__   :						Dict					   =							{}
   for error, count in counts:
      if error_filter is None or error not in error_filter:
         lowerCamelCase__   :						Any					   =							{"""count""": count, """failed_tests""": [(x[2], x[0]) for x in logs if x[1] == error]}

   lowerCamelCase__   :						Union[str, Any]					   =							dict(sorted(r.items()			,  key=lambda UpperCAmelCase     : item[1]["count"]			,  reverse=UpperCAmelCase__     )     )
   return r



def        _a						(    UpperCAmelCase     )				->				List[str]:







   """simple docstring"""
   lowerCamelCase__   :						Optional[int]					   =							test.split('''::'''     )[0]
   if test.startswith('''tests/models/'''     ):
      lowerCamelCase__   :						Optional[Any]					   =							test.split('''/'''     )[2]
   else:
      lowerCamelCase__   :						int					   =							None

   return test



def        _a						(    UpperCAmelCase			,  UpperCAmelCase=None     )				->				Optional[int]:







   """simple docstring"""
   lowerCamelCase__   :						Union[str, Any]					   =							[(x[0], x[1], get_model(x[2]     )) for x in logs]
   lowerCamelCase__   :						Optional[Any]					   =							[x for x in logs if x[2] is not None]
   lowerCamelCase__   :						str					   =							{x[2] for x in logs}

   lowerCamelCase__   :						Optional[Any]					   =							{}
   for test in tests:
      lowerCamelCase__   :						Optional[int]					   =							Counter()
      # count by errors in `test`
      counter.update([x[1] for x in logs if x[2] == test]     )
      lowerCamelCase__   :						Dict					   =							counter.most_common()
      lowerCamelCase__   :						List[str]					   =							{error: count for error, count in counts if (error_filter is None or error not in error_filter)}
      lowerCamelCase__   :						List[Any]					   =							sum(error_counts.values()     )
      if n_errors > 0:
         lowerCamelCase__   :						int					   =							{"""count""": n_errors, """errors""": error_counts}

   lowerCamelCase__   :						List[Any]					   =							dict(sorted(r.items()			,  key=lambda UpperCAmelCase     : item[1]["count"]			,  reverse=UpperCAmelCase__     )     )
   return r



def        _a						(    UpperCAmelCase     )				->				Tuple:







   """simple docstring"""
   lowerCamelCase__   :						List[Any]					   =							"""| no. | error | status |"""
   lowerCamelCase__   :						Any					   =							"""|-:|:-|:-|"""
   lowerCamelCase__   :						Any					   =							[header, sep]
   for error in reduced_by_error:
      lowerCamelCase__   :						Optional[Any]					   =							reduced_by_error[error]["""count"""]
      lowerCamelCase__   :						List[Any]					   =							f"| {count} | {error[:100]} |  |"
      lines.append(UpperCAmelCase__     )

   return "\n".join(UpperCAmelCase__     )



def        _a						(    UpperCAmelCase     )				->				Dict:







   """simple docstring"""
   lowerCamelCase__   :						Union[str, Any]					   =							"""| model | no. of errors | major error | count |"""
   lowerCamelCase__   :						Optional[int]					   =							"""|-:|-:|-:|-:|"""
   lowerCamelCase__   :						Optional[int]					   =							[header, sep]
   for model in reduced_by_model:
      lowerCamelCase__   :						Dict					   =							reduced_by_model[model]["""count"""]
      lowerCamelCase__   :						str					   =							list(reduced_by_model[model]['''errors'''].items()     )[0]
      lowerCamelCase__   :						Optional[int]					   =							f"| {model} | {count} | {error[:60]} | {_count} |"
      lines.append(UpperCAmelCase__     )

   return "\n".join(UpperCAmelCase__     )


if __name__ == "__main__":
       _A					:    Optional[int]   =  argparse.ArgumentParser()
       # Required parameters
       parser.add_argument('--workflow_run_id', type=str, required=True, help='A GitHub Actions workflow run id.')
       parser.add_argument(
           '--output_dir',
           type=str,
           required=True,
           help='Where to store the downloaded artifacts and other result files.',
       )
       parser.add_argument('--token', default=None, type=str, help='A token that has actions:read permission.')
       _A					:    List[str]   =  parser.parse_args()

       os.makedirs(args.output_dir, exist_ok=True)

       _A					:    Tuple   =  get_job_links(args.workflow_run_id, token=args.token)
       _A					:    Any   =  {}
       # To deal with `workflow_call` event, where a job name is the combination of the job names in the caller and callee.
       # For example, `PyTorch 1.11 / Model tests (models/albert, single-gpu)`.
       if _job_links:
              for k, v in _job_links.items():
                     # This is how GitHub actions combine job names.
                     if " / " in k:
                            _A					:    Tuple   =  k.find(' / ')
                            _A					:    int   =  k[index + len(' / ') :]
                     _A					:    Optional[Any]   =  v
       with open(os.path.join(args.output_dir, 'job_links.json'), 'w', encoding='UTF-8') as fp:
              json.dump(job_links, fp, ensure_ascii=False, indent=4)

       _A					:    List[str]   =  get_artifacts_links(args.workflow_run_id, token=args.token)
       with open(os.path.join(args.output_dir, 'artifacts.json'), 'w', encoding='UTF-8') as fp:
              json.dump(artifacts, fp, ensure_ascii=False, indent=4)

       for idx, (name, url) in enumerate(artifacts.items()):
              download_artifact(name, url, args.output_dir, args.token)
              # Be gentle to GitHub
              time.sleep(1)

       _A					:    str   =  get_all_errors(args.output_dir, job_links=job_links)

       # `e[1]` is the error
       _A					:    List[Any]   =  Counter()
       counter.update([e[1] for e in errors])

       # print the top 30 most common test errors
       _A					:    Any   =  counter.most_common(30)
       for item in most_common:
              print(item)

       with open(os.path.join(args.output_dir, 'errors.json'), 'w', encoding='UTF-8') as fp:
              json.dump(errors, fp, ensure_ascii=False, indent=4)

       _A					:    Dict   =  reduce_by_error(errors)
       _A					:    int   =  reduce_by_model(errors)

       _A					:    str   =  make_github_table(reduced_by_error)
       _A					:    int   =  make_github_table_per_model(reduced_by_model)

       with open(os.path.join(args.output_dir, 'reduced_by_error.txt'), 'w', encoding='UTF-8') as fp:
              fp.write(sa)
       with open(os.path.join(args.output_dir, 'reduced_by_model.txt'), 'w', encoding='UTF-8') as fp:
              fp.write(sa)




'''simple docstring'''




import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowercase						: Optional[Any]						=						logging.get_logger(__name__)

_lowercase						: Tuple						=						{
    "BAAI/AltCLIP": "https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json",
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}





class     __magic_name__						(    _UpperCAmelCase):
			UpperCamelCase__				=       '''altclip_text_model'''







			def __init__(	self				:  Union[str, Any]				,			lowercase_				:  str=250002				,			lowercase_				:  Union[str, Any]=1024				,			lowercase_				:  Any=24				,			lowercase_				:  Union[str, Any]=16				,			lowercase_				:  Any=4096				,			lowercase_				:  Union[str, Any]="gelu"				,			lowercase_				:  Union[str, Any]=0.1				,			lowercase_				:  Union[str, Any]=0.1				,			lowercase_				:  int=514				,			lowercase_				:  Union[str, Any]=1				,			lowercase_				:  List[str]=0.02				,			lowercase_				:  Optional[int]=0.02				,			lowercase_				:  str=1E-05				,			lowercase_				:  List[str]=1				,			lowercase_				:  List[Any]=0				,			lowercase_				:  Dict=2				,			lowercase_				:  Union[str, Any]="absolute"				,			lowercase_				:  Any=True				,			lowercase_				:  Union[str, Any]=768				,			**lowercase_				:  Any				,			):
										super().__init__(pad_token_id=lowercase_				,			bos_token_id=lowercase_				,			eos_token_id=lowercase_				,			**lowercase_    )

										lowercase_					:			Union[str, Any]										=       vocab_size
										lowercase_					:			str										=       hidden_size
										lowercase_					:			Optional[Any]										=       num_hidden_layers
										lowercase_					:			int										=       num_attention_heads
										lowercase_					:			str										=       hidden_act
										lowercase_					:			List[str]										=       intermediate_size
										lowercase_					:			Optional[int]										=       hidden_dropout_prob
										lowercase_					:			str										=       attention_probs_dropout_prob
										lowercase_					:			str										=       max_position_embeddings
										lowercase_					:			List[str]										=       type_vocab_size
										lowercase_					:			Union[str, Any]										=       initializer_range
										lowercase_					:			List[Any]										=       initializer_factor
										lowercase_					:			str										=       layer_norm_eps
										lowercase_					:			Tuple										=       position_embedding_type
										lowercase_					:			List[Any]										=       use_cache
										lowercase_					:			Tuple										=       project_dim





class     __magic_name__						(    _UpperCAmelCase):
			UpperCamelCase__				=       '''altclip_vision_model'''







			def __init__(	self				:  Dict				,			lowercase_				:  Any=768				,			lowercase_				:  Dict=3072				,			lowercase_				:  Optional[Any]=512				,			lowercase_				:  Dict=12				,			lowercase_				:  Optional[int]=12				,			lowercase_				:  Optional[Any]=3				,			lowercase_				:  str=224				,			lowercase_				:  List[Any]=32				,			lowercase_				:  Union[str, Any]="quick_gelu"				,			lowercase_				:  Dict=1E-5				,			lowercase_				:  Optional[int]=0.0				,			lowercase_				:  Optional[Any]=0.02				,			lowercase_				:  Optional[Any]=1.0				,			**lowercase_				:  Dict				,			):
										super().__init__(**lowercase_    )

										lowercase_					:			Tuple										=       hidden_size
										lowercase_					:			Optional[Any]										=       intermediate_size
										lowercase_					:			Optional[Any]										=       projection_dim
										lowercase_					:			Tuple										=       num_hidden_layers
										lowercase_					:			List[Any]										=       num_attention_heads
										lowercase_					:			Any										=       num_channels
										lowercase_					:			Any										=       patch_size
										lowercase_					:			Dict										=       image_size
										lowercase_					:			Optional[Any]										=       initializer_range
										lowercase_					:			str										=       initializer_factor
										lowercase_					:			Any										=       attention_dropout
										lowercase_					:			Optional[int]										=       layer_norm_eps
										lowercase_					:			int										=       hidden_act







			@classmethod
			def 	SCREAMING_SNAKE_CASE_     (	cls				:  Optional[int]				,			lowercase_				:  Union[str, os.PathLike]				,			**lowercase_				:  Any    ):
										cls._set_token_in_kwargs(lowercase_    )

										lowercase_						,						lowercase_					:			str										=       cls.get_config_dict(lowercase_				,			**lowercase_    )

										# get the vision config dict if we are loading from AltCLIPConfig
										if config_dict.get("""model_type"""    ) == "altclip":
																	lowercase_					:			List[str]										=       config_dict["""vision_config"""]

										if "model_type" in config_dict and hasattr(cls				,			"""model_type"""    ) and config_dict["model_type"] != cls.model_type:
																	logger.warning(
																	    f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
																	    f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''    )

										return cls.from_dict(lowercase_				,			**lowercase_    )





class     __magic_name__						(    _UpperCAmelCase):
			UpperCamelCase__				=       '''altclip'''
			UpperCamelCase__				=       True







			def __init__(	self				:  Optional[int]				,			lowercase_				:  Dict=None				,			lowercase_				:  List[Any]=None				,			lowercase_				:  Tuple=768				,			lowercase_				:  List[str]=2.65_92				,			**lowercase_				:  List[Any]    ):
										# If `_config_dict` exist, we use them for the backward compatibility.
										# We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot
										# of confusion!).
										lowercase_					:			Dict										=       kwargs.pop("""text_config_dict"""				,			lowercase_    )
										lowercase_					:			str										=       kwargs.pop("""vision_config_dict"""				,			lowercase_    )

										super().__init__(**lowercase_    )

										# Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
										# `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
										# cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
										if text_config_dict is not None:
																	if text_config is None:
																								lowercase_					:			Dict										=       {}

																	# This is the complete result when using `text_config_dict`.
																	lowercase_					:			List[str]										=       AltCLIPTextConfig(**lowercase_    ).to_dict()

																	# Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
																	for key, value in _text_config_dict.items():
																								if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
																															# If specified in `text_config_dict`
																															if key in text_config_dict:
																																						lowercase_					:			Optional[Any]										=       (
																																						    f'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. '''
																																						    f'''The value `text_config_dict["{key}"]` will be used instead.'''
																																						)
																															# If inferred from default argument values (just to be super careful)
																															else:
																																						lowercase_					:			Tuple										=       (
																																						    f'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
																																						    f'''value `text_config["{key}"]` will be overriden.'''
																																						)
																															logger.warning(lowercase_    )

            # Update all values in `text_config` with the ones in `_text_config_dict`.
																	text_config.update(_text_config_dict    )

										if vision_config_dict is not None:
																	if vision_config is None:
																								lowercase_					:			int										=       {}

																	# This is the complete result when using `vision_config_dict`.
																	lowercase_					:			List[str]										=       AltCLIPVisionConfig(**lowercase_    ).to_dict()
																	# convert keys to string instead of integer
																	if "id2label" in _vision_config_dict:
																								lowercase_					:			List[str]										=       {
																								    str(lowercase_    ): value for key, value in _vision_config_dict["""id2label"""].items()
																								}

																	# Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
																	for key, value in _vision_config_dict.items():
																								if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
																															# If specified in `vision_config_dict`
																															if key in vision_config_dict:
																																						lowercase_					:			Any										=       (
																																						    f'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different '''
																																						    f'''values. The value `vision_config_dict["{key}"]` will be used instead.'''
																																						)
																															# If inferred from default argument values (just to be super careful)
																															else:
																																						lowercase_					:			List[str]										=       (
																																						    f'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
																																						    f'''The value `vision_config["{key}"]` will be overriden.'''
																																						)
																															logger.warning(lowercase_    )

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
																	vision_config.update(_vision_config_dict    )

										if text_config is None:
																	lowercase_					:			int										=       {}
																	logger.info("""`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."""    )

										if vision_config is None:
																	lowercase_					:			Optional[int]										=       {}
																	logger.info("""`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."""    )

										lowercase_					:			Optional[int]										=       AltCLIPTextConfig(**lowercase_    )
										lowercase_					:			Any										=       AltCLIPVisionConfig(**lowercase_    )

										lowercase_					:			List[Any]										=       projection_dim
										lowercase_					:			Optional[Any]										=       logit_scale_init_value
										lowercase_					:			int										=       1.0







			@classmethod
			def 	SCREAMING_SNAKE_CASE_     (	cls				:  Dict				,			lowercase_				:  AltCLIPTextConfig				,			lowercase_				:  AltCLIPVisionConfig				,			**lowercase_				:  Optional[int]    ):
										return cls(text_config=text_config.to_dict()				,			vision_config=vision_config.to_dict()				,			**lowercase_    )







			def 	SCREAMING_SNAKE_CASE_     (	self				:  Optional[int]    ):
										lowercase_					:			Union[str, Any]										=       copy.deepcopy(self.__dict__    )
										lowercase_					:			Optional[int]										=       self.text_config.to_dict()
										lowercase_					:			Any										=       self.vision_config.to_dict()
										lowercase_					:			List[str]										=       self.__class__.model_type
										return output





