import os import subprocess import math import torch import torch.distributed as dist import transformers from typing import Union, Iterable, List, Dict, Tuple, Optional class DictWithDotAccess(dict): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for arg in args: if isinstance(arg, dict): for key, value in arg.items(): if isinstance(value, dict): value = DictWithDotAccess(value) self[key] = value if kwargs: for key, value in kwargs.items(): if isinstance(value, dict): value = DictWithDotAccess(value) self[key] = value def __getattr__(self, attr): return self.get(attr) def is_dist_avail_and_initialized(): if not dist.is_available(): return False if not dist.is_initialized(): return False return True def get_rank(): if not is_dist_avail_and_initialized(): return 0 return dist.get_rank() def get_local_rank(): if not is_dist_avail_and_initialized(): return 0 return int(os.environ["LOCAL_RANK"]) def Print(*args): if get_rank() == 0: print(*args) def get_sha(): cwd = os.path.dirname(os.path.abspath(__file__)) def _run(command): return subprocess.check_output(command, cwd=cwd).decode("ascii").strip() sha = "N/A" diff = "clean" branch = "N/A" try: sha = _run(["git", "rev-parse", "HEAD"]) subprocess.check_output(["git", "diff"], cwd=cwd) diff = _run(["git", "diff-index", "HEAD"]) diff = "has uncommited changes" if diff else "clean" branch = _run(["git", "rev-parse", "--abbrev-ref", "HEAD"]) except Exception: pass message = f"sha: {sha}, status: {diff}, branch: {branch}" return message def patch_cosine_with_warmup_schedule(minimal_lr=0.0): def _get_cosine_schedule_with_warmup_lr_lambda( current_step: int, *, num_warmup_steps: int, num_training_steps: int, num_cycles: float ): if current_step < num_warmup_steps: return float(current_step) / float(max(1, num_warmup_steps)) progress = float(current_step - num_warmup_steps) / float(max(1, num_training_steps - num_warmup_steps)) return max(minimal_lr, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress))) transformers.optimization._get_cosine_schedule_with_warmup_lr_lambda = _get_cosine_schedule_with_warmup_lr_lambda # patch torch clip grad norm so that we can skip nan grad norm from torch import Tensor, inf from torch.utils._foreach_utils import _group_tensors_by_device_and_dtype, _has_foreach_support _tensor_or_tensors = Union[torch.Tensor, Iterable[torch.Tensor]] def clip_grad_norm_( parameters: _tensor_or_tensors, max_norm: float, norm_type: float = 2.0, error_if_nonfinite: bool = False, foreach: Optional[bool] = None) -> torch.Tensor: r"""Clips gradient norm of an iterable of parameters. The norm is computed over all gradients together, as if they were concatenated into a single vector. Gradients are modified in-place. Args: parameters (Iterable[Tensor] or Tensor): an iterable of Tensors or a single Tensor that will have gradients normalized max_norm (float): max norm of the gradients norm_type (float): type of the used p-norm. Can be ``'inf'`` for infinity norm. error_if_nonfinite (bool): if True, an error is thrown if the total norm of the gradients from :attr:`parameters` is ``nan``, ``inf``, or ``-inf``. Default: False (will switch to True in the future) foreach (bool): use the faster foreach-based implementation. If ``None``, use the foreach implementation for CUDA and CPU native tensors and silently fall back to the slow implementation for other device types. Default: ``None`` Returns: Total norm of the parameter gradients (viewed as a single vector). """ if isinstance(parameters, torch.Tensor): parameters = [parameters] grads = [p.grad for p in parameters if p.grad is not None] max_norm = float(max_norm) norm_type = float(norm_type) if len(grads) == 0: return torch.tensor(0.) if torch.isnan(max_norm) or torch.isinf(max_norm): for grad in grads: grad.zero_() print('>>>Found nan or inf max_norm, set grads to zero') return torch.tensor(0.) first_device = grads[0].device grouped_grads: Dict[Tuple[torch.device, torch.dtype], List[List[Tensor]]] \ = _group_tensors_by_device_and_dtype([[g.detach() for g in grads]]) # type: ignore[assignment] if norm_type == inf: norms = [g.detach().abs().max().to(first_device) for g in grads] total_norm = norms[0] if len(norms) == 1 else torch.max(torch.stack(norms)) else: norms = [] for ((device, _), [grads]) in grouped_grads.items(): if (foreach is None or foreach) and _has_foreach_support(grads, device=device): norms.extend(torch._foreach_norm(grads, norm_type)) elif foreach: raise RuntimeError(f'foreach=True was passed, but can\'t use the foreach API on {device.type} tensors') else: norms.extend([torch.norm(g, norm_type) for g in grads]) total_norm = torch.norm(torch.stack([norm.to(first_device) for norm in norms]), norm_type) if torch.isnan(total_norm) or torch.isinf(total_norm): for grad in grads: grad.zero_() print('>>>Found nan or inf total_norm, set grads to zero') return torch.tensor(0.) if error_if_nonfinite and torch.logical_or(total_norm.isnan(), total_norm.isinf()): raise RuntimeError( f'The total norm of order {norm_type} for gradients from ' '`parameters` is non-finite, so it cannot be clipped. To disable ' 'this error and scale the gradients by the non-finite norm anyway, ' 'set `error_if_nonfinite=False`') clip_coef = max_norm / (total_norm + 1e-6) # Note: multiplying by the clamped coef is redundant when the coef is clamped to 1, but doing so # avoids a `if clip_coef < 1:` conditional which can require a CPU <=> device synchronization # when the gradients do not reside in CPU memory. clip_coef_clamped = torch.clamp(clip_coef, max=1.0) for ((device, _), [grads]) in grouped_grads.items(): if (foreach is None or foreach) and _has_foreach_support(grads, device=device): torch._foreach_mul_(grads, clip_coef_clamped.to(device)) # type: ignore[call-overload] elif foreach: raise RuntimeError(f'foreach=True was passed, but can\'t use the foreach API on {device.type} tensors') else: clip_coef_clamped_device = clip_coef_clamped.to(device) for g in grads: g.detach().mul_(clip_coef_clamped_device) return total_norm def patch_torch_clip_grad_norm(): torch.nn.utils.clip_grad_norm_ = clip_grad_norm_