o
    }oi3                     @   s4   d Z ddlZddlZddlmZ G dd deZdS )a  RAdam
Original source taken from https://github.com/LiyuanLucasLiu/RAdam

Copyright 2019 Liyuan Liu

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
    N)	Optimizerc                       s,   e Zd ZdZd fdd	Zdd	d
Z  ZS )RAdamzRAdam optimizerMbP?g?g+?:0yE>r   c                    s6   t ||||d}dd tdD | _t || dS )z
        Init

        :param params: parameters to optimize
        :param lr: learning rate
        :param betas: beta
        :param eps: numerical precision
        :param weight_decay: weight decay weight
        )lrbetasepsweight_decayc                 S   s   g | ]}g d qS ))NNN ).0_r   r   I/home/ubuntu/.local/lib/python3.10/site-packages/nemo/core/optim/radam.py
<listcomp>3   s    z"RAdam.__init__.<locals>.<listcomp>
   N)dictrangebuffersuper__init__)selfparamsr   r   r	   r
   defaults	__class__r   r   r   (   s   
zRAdam.__init__Nc                 C   s  d }|d ur	| }| j D ]9}|d D ]1}|jd u rq|jj }|jr)td|j }| j| }t|dkrLd|d< t	||d< t	||d< n|d 
||d< |d 
||d< |d |d }}	|d \}
}|	|j||d| d	 ||
j|d|
 d
 |d  d7  < | jt|d d  }|d |d kr|d |d }}nb|d |d< ||d  }dd|  d }|d|d  | d|   }||d< |dkr|d td| |d  |d  |d  | | |d   d|
|d    }n|d d|
|d    }||d< |d dkr!||d  |d  | |dkr8|	 |d }|| || n|| | |j| qq|S )Nr   z'RAdam does not support sparse gradientsr   stepexp_avg
exp_avg_sqr   g      ?)value)alpha   r         r      r
   r	   )param_groupsgraddatafloat	is_sparseRuntimeErrorstatelentorch
zeros_liketype_asmul_addcmul_add_r   intmathsqrtaddcdiv_copy_)r   closurelossgrouppr%   p_data_fp32r*   r   r   beta1beta2bufferedN_sma	step_sizebeta2_t	N_sma_maxdenomr   r   r   r   6   s~   




Cz
RAdam.step)r   r   r   r   )N)__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r   %   s    r   )rG   r3   r,   torch.optim.optimizerr   r   r   r   r   r   <module>   s
   