

"""simple docstring"""




import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
      from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
      import torch_xla.core.xla_model as xm
def   lowerCAmelCase						(					__UpperCamelCase      ):
 '''simple docstring'''





 if is_torch_version("""<"""							,    """2.0.0"""      ) or not hasattr(__UpperCamelCase							,    """_dynamo"""      ):
  return False
 return isinstance(__UpperCamelCase							,    torch._dynamo.eval_frame.OptimizedModule      )
def   lowerCAmelCase						(					__UpperCamelCase							,    __UpperCamelCase = True      ):
 '''simple docstring'''





 UpperCAmelCase__					:  List[Any]					   =					(torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

 UpperCAmelCase__					:  List[Any]					   =					is_compiled_module(__UpperCamelCase      )
 if is_compiled:
  UpperCAmelCase__					:  str					   =					model
  UpperCAmelCase__					:  str					   =					model._orig_mod

 if is_deepspeed_available():
  options += (DeepSpeedEngine,)

 while isinstance(__UpperCamelCase							,    __UpperCamelCase      ):
  UpperCAmelCase__					:  Dict					   =					model.module

 if not keep_fpaa_wrapper:
  UpperCAmelCase__					:  Any					   =					getattr(__UpperCamelCase							,    """forward"""      )
  UpperCAmelCase__					:  Union[str, Any]					   =					model.__dict__.pop("""_original_forward"""							,    __UpperCamelCase      )
  if original_forward is not None:
   while hasattr(__UpperCamelCase							,    """__wrapped__"""      ):
    UpperCAmelCase__					:  Optional[Any]					   =					forward.__wrapped__
    if forward == original_forward:
     break
   UpperCAmelCase__					:  Any					   =					forward
  if getattr(__UpperCamelCase							,    """_converted_to_transformer_engine"""							,    __UpperCamelCase      ):
   convert_model(__UpperCamelCase							,    to_transformer_engine=__UpperCamelCase      )

 if is_compiled:
  UpperCAmelCase__					:  List[str]					   =					model
  UpperCAmelCase__					:  Dict					   =					compiled_model

 return model
def   lowerCAmelCase						(					):
 '''simple docstring'''





 PartialState().wait_for_everyone()
def   lowerCAmelCase						(					__UpperCamelCase							,    __UpperCamelCase      ):
 '''simple docstring'''





 if PartialState().distributed_type == DistributedType.TPU:
  xm.save(__UpperCamelCase							,    __UpperCamelCase      )
 elif PartialState().local_process_index == 0:
  torch.save(__UpperCamelCase							,    __UpperCamelCase      )
@contextmanager
def   lowerCAmelCase						(					**__UpperCamelCase      ):
 '''simple docstring'''





 for key, value in kwargs.items():
  UpperCAmelCase__					:  List[Any]					   =					str(__UpperCamelCase      )

 yield

 for key in kwargs:
  if key.upper() in os.environ:
   del os.environ[key.upper()]
def   lowerCAmelCase						(					__UpperCamelCase      ):
 '''simple docstring'''





 if not hasattr(__UpperCamelCase							,    """__qualname__"""      ) and not hasattr(__UpperCamelCase							,    """__name__"""      ):
  UpperCAmelCase__					:  str					   =					getattr(__UpperCamelCase							,    """__class__"""							,    __UpperCamelCase      )
 if hasattr(__UpperCamelCase							,    """__qualname__"""      ):
  return obj.__qualname__
 if hasattr(__UpperCamelCase							,    """__name__"""      ):
  return obj.__name__
 return str(__UpperCamelCase      )
def   lowerCAmelCase						(					__UpperCamelCase							,    __UpperCamelCase      ):
 '''simple docstring'''





 for key, value in source.items():
  if isinstance(__UpperCamelCase							,    __UpperCamelCase      ):
   UpperCAmelCase__					:  Dict					   =					destination.setdefault(__UpperCamelCase							,    {}      )
   merge_dicts(__UpperCamelCase							,    __UpperCamelCase      )
  else:
   UpperCAmelCase__					:  Any					   =					value

 return destination




def   lowerCAmelCase						(					__UpperCamelCase = None      ):
 '''simple docstring'''





 if port is None:
  UpperCAmelCase__					:  List[Any]					   =					29500
 with socket.socket(socket.AF_INET							,    socket.SOCK_STREAM      ) as s:
  return s.connect_ex(("""localhost""", port)      ) == 0








"""simple docstring"""




from datetime import datetime as dt
import os

from github import Github


__UpperCAmelCase =				[
    'good first issue',
    'good second issue',
    'good difficult issue',
    'feature request',
    'new model',
    'wip',
]
def   lowerCAmelCase						(					):
 '''simple docstring'''





 UpperCAmelCase__					:  str					   =					Github(os.environ["""GITHUB_TOKEN"""]      )
 UpperCAmelCase__					:  int					   =					g.get_repo("""huggingface/transformers"""      )
 UpperCAmelCase__					:  Optional[int]					   =					repo.get_issues(state="""open"""      )

 for issue in open_issues:
  UpperCAmelCase__					:  Any					   =					sorted([comment for comment in issue.get_comments()]							,    key=lambda __UpperCamelCase      : i.created_at							,    reverse=__UpperCamelCase      )
  UpperCAmelCase__					:  Union[str, Any]					   =					comments[0] if len(__UpperCamelCase      ) > 0 else None
  if (
      last_comment is not None
      and last_comment.user.login == "github-actions[bot]"
      and (dt.utcnow() - issue.updated_at).days > 7
      and (dt.utcnow() - issue.created_at).days >= 30
      and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()      )
  ):
   # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.")
   issue.edit(state="""closed"""      )
  elif (
      (dt.utcnow() - issue.updated_at).days > 23
      and (dt.utcnow() - issue.created_at).days >= 30
      and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()      )
  ):
   # print(f"Would add stale comment to {issue.number}")
   issue.create_comment(
       """This issue has been automatically marked as stale because it has not had """
       """recent activity. If you think this still needs to be addressed """
       """please comment on this thread.\n\nPlease note that issues that do not follow the """
       """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """
       """are likely to be ignored."""      )


if __name__ == "__main__":
      main()





