File size: 11,252 Bytes
c8b77b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""
Gradient Descent Optimizers
==========================

This module implements various gradient descent optimization algorithms
with proper mathematical formulations and PyTorch compatibility.

Algorithms implemented:
- SGD (Stochastic Gradient Descent)
- Adam (Adaptive Moment Estimation)
- AdamW (Adam with Decoupled Weight Decay)

All optimizers follow the mathematical principles and include proper
momentum, learning rate scheduling, and weight decay handling.
"""

import math
import logging
from abc import ABC, abstractmethod
from typing import Dict, List, Tuple, Any
import torch

logger = logging.getLogger(__name__)


class Optimizer(ABC):
    """Abstract base class for all optimizers"""
    
    def __init__(self, params: List[torch.Tensor], lr: float = 1e-3, **kwargs):
        self.params = params
        self.lr = lr
        self.step_count = 0
        self.state = {}
        
    @abstractmethod
    def step(self, closure=None):
        """Perform a single optimization step"""
        pass
        
    def zero_grad(self):
        """Zero the gradients of all parameters"""
        for param in self.params:
            if param.grad is not None:
                param.grad.zero_()
                
    def state_dict(self):
        """Return the state of the optimizer"""
        return {
            'state': self.state,
            'param_groups': [{'params': self.params, 'lr': self.lr}],
            'step_count': self.step_count
        }
        
    def load_state_dict(self, state_dict):
        """Load the state of the optimizer"""
        self.state = state_dict['state']
        self.lr = state_dict['param_groups'][0]['lr']
        self.step_count = state_dict['step_count']


class SGD(Optimizer):
    """
    Stochastic Gradient Descent optimizer
    
    Mathematical formulation:
    θ_{t+1} = θ_t - α * ∇_θ J(θ_t)
    
    With momentum:
    v_{t+1} = μ * v_t + ∇_θ J(θ_t)
    θ_{t+1} = θ_t - α * v_{t+1}
    """
    
    def __init__(self, params: List[torch.Tensor], lr: float = 1e-3, 
                 momentum: float = 0.0, weight_decay: float = 0.0, 
                 dampening: float = 0.0, nesterov: bool = False):
        super().__init__(params, lr)
        self.momentum = momentum
        self.weight_decay = weight_decay
        self.dampening = dampening
        self.nesterov = nesterov
        
        # Initialize momentum buffers
        for param in self.params:
            if momentum > 0:
                self.state[param] = {'momentum_buffer': torch.zeros_like(param)}
                
        logger.info(f"Initialized SGD optimizer: lr={lr}, momentum={momentum}, weight_decay={weight_decay}")
    
    def step(self, closure=None):
        """Perform SGD optimization step"""
        loss = None
        if closure is not None:
            loss = closure()
            
        for param in self.params:
            if param.grad is None:
                continue
                
            grad = param.grad.data
            
            # Apply weight decay
            if self.weight_decay != 0:
                grad = grad.add(param.data, alpha=self.weight_decay)
            
            # Apply momentum
            if self.momentum != 0:
                param_state = self.state[param]
                if 'momentum_buffer' not in param_state:
                    param_state['momentum_buffer'] = torch.zeros_like(param.data)
                
                momentum_buffer = param_state['momentum_buffer']
                momentum_buffer.mul_(self.momentum).add_(grad, alpha=1 - self.dampening)
                
                if self.nesterov:
                    grad = grad.add(momentum_buffer, alpha=self.momentum)
                else:
                    grad = momentum_buffer
            
            # Update parameters
            param.data.add_(grad, alpha=-self.lr)
            
        self.step_count += 1
        return loss


class Adam(Optimizer):
    """
    Adam (Adaptive Moment Estimation) optimizer
    
    Mathematical formulation:
    m_t = β₁ * m_{t-1} + (1 - β₁) * g_t
    v_t = β₂ * v_{t-1} + (1 - β₂) * g_t²
    m̂_t = m_t / (1 - β₁ᵗ)
    v̂_t = v_t / (1 - β₂ᵗ)
    θ_{t+1} = θ_t - α * m̂_t / (√v̂_t + ε)
    """
    
    def __init__(self, params: List[torch.Tensor], lr: float = 1e-3,
                 betas: Tuple[float, float] = (0.9, 0.999), eps: float = 1e-8,
                 weight_decay: float = 0.0, amsgrad: bool = False):
        super().__init__(params, lr)
        self.betas = betas
        self.eps = eps
        self.weight_decay = weight_decay
        self.amsgrad = amsgrad
        
        # Initialize moment estimates
        for param in self.params:
            self.state[param] = {
                'step': 0,
                'exp_avg': torch.zeros_like(param.data),
                'exp_avg_sq': torch.zeros_like(param.data)
            }
            if amsgrad:
                self.state[param]['max_exp_avg_sq'] = torch.zeros_like(param.data)
                
        logger.info(f"Initialized Adam optimizer: lr={lr}, betas={betas}, eps={eps}")
    
    def step(self, closure=None):
        """Perform Adam optimization step"""
        loss = None
        if closure is not None:
            loss = closure()
            
        for param in self.params:
            if param.grad is None:
                continue
                
            grad = param.grad.data
            
            # Apply weight decay
            if self.weight_decay != 0:
                grad = grad.add(param.data, alpha=self.weight_decay)
            
            param_state = self.state[param]
            exp_avg, exp_avg_sq = param_state['exp_avg'], param_state['exp_avg_sq']
            beta1, beta2 = self.betas
            
            param_state['step'] += 1
            bias_correction1 = 1 - beta1 ** param_state['step']
            bias_correction2 = 1 - beta2 ** param_state['step']
            
            # Update biased first moment estimate
            exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
            
            # Update biased second raw moment estimate
            exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
            
            if self.amsgrad:
                # Maintains the maximum of all 2nd moment running avg. of squared gradients
                torch.max(param_state['max_exp_avg_sq'], exp_avg_sq, out=param_state['max_exp_avg_sq'])
                # Use the max. for normalizing running avg. of squared gradients
                denom = (param_state['max_exp_avg_sq'].sqrt() / math.sqrt(bias_correction2)).add_(self.eps)
            else:
                denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(self.eps)
            
            # Update parameters
            step_size = self.lr / bias_correction1
            param.data.addcdiv_(exp_avg, denom, value=-step_size)
            
        self.step_count += 1
        return loss


class AdamW(Optimizer):
    """
    AdamW (Adam with Decoupled Weight Decay) optimizer
    
    Mathematical formulation:
    θ_t = θ_{t-1} - α * (m̂_t / (√v̂_t + ε) + λ * θ_{t-1})
    
    Where weight decay is applied directly to parameters, not gradients.
    """
    
    def __init__(self, params: List[torch.Tensor], lr: float = 1e-3,
                 betas: Tuple[float, float] = (0.9, 0.999), eps: float = 1e-8,
                 weight_decay: float = 0.01, amsgrad: bool = False):
        super().__init__(params, lr)
        self.betas = betas
        self.eps = eps
        self.weight_decay = weight_decay
        self.amsgrad = amsgrad
        
        # Initialize moment estimates
        for param in self.params:
            self.state[param] = {
                'step': 0,
                'exp_avg': torch.zeros_like(param.data),
                'exp_avg_sq': torch.zeros_like(param.data)
            }
            if amsgrad:
                self.state[param]['max_exp_avg_sq'] = torch.zeros_like(param.data)
                
        logger.info(f"Initialized AdamW optimizer: lr={lr}, betas={betas}, weight_decay={weight_decay}")
    
    def step(self, closure=None):
        """Perform AdamW optimization step"""
        loss = None
        if closure is not None:
            loss = closure()
            
        for param in self.params:
            if param.grad is None:
                continue
                
            grad = param.grad.data
            param_state = self.state[param]
            exp_avg, exp_avg_sq = param_state['exp_avg'], param_state['exp_avg_sq']
            beta1, beta2 = self.betas
            
            param_state['step'] += 1
            bias_correction1 = 1 - beta1 ** param_state['step']
            bias_correction2 = 1 - beta2 ** param_state['step']
            
            # Update biased first moment estimate
            exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
            
            # Update biased second raw moment estimate
            exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
            
            if self.amsgrad:
                # Maintains the maximum of all 2nd moment running avg. of squared gradients
                torch.max(param_state['max_exp_avg_sq'], exp_avg_sq, out=param_state['max_exp_avg_sq'])
                # Use the max. for normalizing running avg. of squared gradients
                denom = (param_state['max_exp_avg_sq'].sqrt() / math.sqrt(bias_correction2)).add_(self.eps)
            else:
                denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(self.eps)
            
            # Update parameters with decoupled weight decay
            step_size = self.lr / bias_correction1
            param.data.mul_(1 - self.lr * self.weight_decay)
            param.data.addcdiv_(exp_avg, denom, value=-step_size)
            
        self.step_count += 1
        return loss


class OptimizerFactory:
    """Factory class for creating optimizers"""
    
    @staticmethod
    def create_optimizer(optimizer_type: str, params: List[torch.Tensor], **kwargs) -> Optimizer:
        """Create an optimizer instance"""
        optimizers = {
            'sgd': SGD,
            'adam': Adam,
            'adamw': AdamW
        }
        
        if optimizer_type.lower() not in optimizers:
            raise ValueError(f"Unknown optimizer type: {optimizer_type}")
            
        optimizer_class = optimizers[optimizer_type.lower()]
        return optimizer_class(params, **kwargs)
    
    @staticmethod
    def get_default_config(optimizer_type: str) -> Dict[str, Any]:
        """Get default configuration for optimizer"""
        configs = {
            'sgd': {
                'lr': 1e-3,
                'momentum': 0.9,
                'weight_decay': 1e-4
            },
            'adam': {
                'lr': 1e-3,
                'betas': (0.9, 0.999),
                'eps': 1e-8,
                'weight_decay': 1e-4
            },
            'adamw': {
                'lr': 1e-3,
                'betas': (0.9, 0.999),
                'eps': 1e-8,
                'weight_decay': 0.01
            }
        }
        
        return configs.get(optimizer_type.lower(), {})