The closure passed to the optimizer is None when using fp16


I’m trying to use a new optimizer called Sharpness-Aware Minimization (SAM) in Lightning. This optimizer does two backward passes for every forward pass.

The SAM implementation I’m using can be found here. I don’t have any issues when running in fp32, but when using fp16 I’ve encountered the following error.

Traceback (most recent call last):                                                                                                                                                                [60/4905]
  File "pipe/", line 253, in <module>                                                                                                                                                              
    train_score, valid_score = run(hparams=hparams)                                                                                                                                                        
  File "pipe/", line 190, in run                                                                                                                                                              , dm)                                                                                                                                                                                 
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/trainer/", line 473, in fit                                                                          
    results = self.accelerator_backend.train()                                                                                                                                                             
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/accelerators/", line 66, in train                                                            
    results = self.train_or_test()                                                                                                                                                                         
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/accelerators/", line 69, in train_or_test                                                        
    results = self.trainer.train()                                                                                                                                                                         
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/trainer/", line 524, in train                                                                        
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/trainer/", line 572, in run_training_epoch                                                     
    batch_output = self.run_training_batch(batch, batch_idx, dataloader_idx)                                                                                                                               
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/trainer/", line 730, in run_training_batch                                                     
    self.optimizer_step(optimizer, opt_idx, batch_idx, train_step_and_backward_closure)                                                                                                                    
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/trainer/", line 513, in optimizer_step                                                         
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/core/", line 1261, in optimizer_step                                                               
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/core/", line 286, in step                                                                          
    self.__optimizer_step(*args, closure=closure, profiler_name=profiler_name, **kwargs)                                                                                                                   
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/core/", line 140, in __optimizer_step                                                              
    trainer.precision_connector.backend.optimizer_step(trainer, optimizer, closure)                                                                                                                        
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/pytorch_lightning/plugins/", line 82, in optimizer_step                                                             
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/torch/cuda/amp/", line 321, in step                                                                                
    retval = optimizer.step(*args, **kwargs)                                                                                                                                                               
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/torch/optim/", line 67, in wrapper                                                                                
    return wrapped(*args, **kwargs)
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/torch/autograd/", line 26, in decorate_context
    return func(*args, **kwargs)
  File "/home/gianluca/git/kaggle/ranzcr/src/ml/", line 107, in step
    loss = closure().detach()
  File "/home/gianluca/miniconda3/envs/ranzcr/lib/python3.7/site-packages/torch/autograd/", line 26, in decorate_context
    return func(*args, **kwargs)
TypeError: 'NoneType' object is not callable

It seems to complain that closure is None. In your experience, what could cause that? Is there anything generally I should do to ensure that an optimizer working on fp32 does also work on fp16?

Many thanks!

Looks like the problem is in the implementation.

it expects closure can’t be None, but in Lightning when using the native amp, closure is called before optimizer.step()
should be done this way:

In case of native amp closure is called outside the optimizer.step in lightning. That might be the reason here for the failure.

looks like closures aren’t supported with 16bit precision training.

I should have updated this thread! You are correct. We need to also ensure the closure is not None here.

Mmm… that’s interesting! I need to run some tests, but I suspect that might be the reason why the optimizer is failing when using precision=16. When using precision=32, the closure is never None.

To add to the confusion, it seems that when passing precision=16 to the Trainer we use PyTorch autocast (link) and not actually set the model to 16-bit precision.