text: """
  Implements Lamb Optimizer which allows training with large batches and large learning rates.
  This algorithm is similar to the LARS algorithm, just with a different base optimizer.
  
  Reference: https://github.com/cybertronai/pytorch-lamb/blob/master/pytorch_lamb/lamb.py
  """
  
  import torch
  from torch.optim import Optimizer
  
  
  class Lamb(Optimizer):
      def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-6, weight_decay=0):
          defaults = dict(
              lr=lr,
              betas=betas,
              eps=eps,
              weight_decay=weight_decay,
          )
          super(Lamb, self).__init__(params, defaults)
  
      def step(self, closure=None):
          loss = closure() if closure is not None else None
  
          for group in self.param_groups:
              for p in group['params']:
                  if p.grad is None:
                      continue
                  grad = p.grad.data
  
                  state = self.state[p]
  
                  # initialize state if not found
                  if len(state) == 0:
                      state['step'] = 0
                      state['exp_avg'] = torch.zeros_like(p.data)
                      state['exp_avg_sq'] = torch.zeros_like(p.data)
  
                  exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']
                  beta1, beta2 = group['betas']
                  state['step'] += 1
  
                  # m_t
                  exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
                  # v_t
                  exp_avg_sq.mul_(beta2).add_cmul_(grad, grad, value=1 - beta2)
  
                  step_size = group['lr']
                  weight_norm = p.data.pow(2).sum().sqrt().clamp(0, 10)
  
                  adam_step = exp_avg / exp_avg_sq.sqrt().add(group['eps'])
                  if group['weight_decay'] != 0:
                      adam_step.add_(p.data, alpha=group['weight_decay'])
  
                  adam_norm = adam_step.pow(2).sum().sqrt()
                  if weight_norm == 0 or adam_norm == 0:
                      trust_ratio = 1
                  else:
                      trust_ratio = weight_norm / adam_norm
  
                  state['weight_norm'] = weight_norm
                  state['adam_norm'] = adam_norm
                  state['trust_ratio'] = trust_ratio
  
                  p.data.add_(adam_step, alpha=-step_size * trust_ratio)
  
          return loss
  