
    khX                          d Z ddlZddZddZy)aP   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           	          | j                   dk  r| j                  |      S | j                  |t        t        d| j                               d      S )N   T)dimkeepdim)ndimnormtuplerange)x	norm_types     J/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/utils/agc.pyunitwise_normr      sC    vv{vvi   vviU5AFF+;%<dvKK    c                    t        | t        j                        r| g} | D ]  }|j                  |j	                         }|j                  j	                         }t        ||      j                  |      j                  |      }t        ||      }|||j                  d      z  z  }	t        j                  ||k  ||	      }
|j                  j	                         j                  |
        y )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> 9=DDDMRRS^_!&I>	IOOO,E!EFKK	H 4flK		i(	)r   )       @)g{Gz?gMbP?r'   )__doc__r   r   r&    r   r   <module>r*      s   " L)r   