
    RЦiX                     $    S r SSKrSS jrSS jrg)aO  Adaptive Gradient Clipping

An impl of AGC, as per (https://arxiv.org/abs/2102.06171):

@article{brock2021high,
  author={Andrew Brock and Soham De and Samuel L. Smith and Karen Simonyan},
  title={High-Performance Large-Scale Image Recognition Without Normalization},
  journal={arXiv preprint arXiv:},
  year={2021}
}

Code references:
  * Official JAX impl (paper authors): https://github.com/deepmind/deepmind-research/tree/master/nfnets
  * Phil Wang's PyTorch gist: https://gist.github.com/lucidrains/0d6560077edac419ab5d3aa29e674d5c

Hacked together by / Copyright 2021 Ross Wightman
    Nc           	          U R                   S::  a  U R                  U5      $ U R                  U[        [        SU R                   5      5      SS9$ )N   T)dimkeepdim)ndimnormtuplerange)x	norm_types     M/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/utils/agc.pyunitwise_normr      sC    vv{vvi   vviU5AFF+;%<dvKK    c                    [        U [        R                  5      (       a  U /n U  H  nUR                  c  M  UR	                  5       nUR                  R	                  5       n[        XSS9R                  US9R                  U5      n[        XcS9nXgUR                  SS9-  -  n	[        R                  " X:  Xi5      n
UR                  R	                  5       R                  U
5        M     g )N)r   )mingư>)
isinstancetorchTensorgraddetachr   clamp_mul_clampwherecopy_)
parametersclip_factorepsr   pp_datag_datamax_norm	grad_normclipped_grad	new_gradss              r   adaptive_clip_gradr&      s    *ell++ \
66> =DDDMRRS^_!&>	IOOO,E!EFKK	 4fK		i( r   )       @)g{Gz?gMbP?r'   )__doc__r   r   r&    r   r   <module>r*      s   " L)r   