Saving / loading hparams type in checkpoint
🐛 Bug
There seems to be an issue with saving or loading hyperparams type in checkpoints. Related to #924 (unrelated to #3998).
Please reproduce using the BoringModel and post here
Here is the BoringModel gist using the snippet from @awaelchli in #924
(running in colab has been unreliable, see #3998)
https://gist.github.com/chiragraman/c235f6f2a25b2432bde1a08ae6ed1b03
Behavior
With my local master at 155f4e9, I'm getting the following error:
type of hparams <class 'pytorch_lightning.utilities.parsing.AttributeDict'>
class of hparams type AttributeDict
accessing hparams 1 no problem
GPU available: True, used: False
TPU available: False, using: 0 TPU cores
/home/chirag/miniconda3/envs/ml/lib/python3.8/site-packages/pytorch_lightning/utilities/distributed.py:45: UserWarning: GPU available but not used. Set the --gpus flag when calling the script.
  warnings.warn(*args, **kwargs)
/home/chirag/miniconda3/envs/ml/lib/python3.8/site-packages/pytorch_lightning/utilities/distributed.py:45: UserWarning: The dataloader, val dataloader 0, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 8 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  warnings.warn(*args, **kwargs)
/home/chirag/miniconda3/envs/ml/lib/python3.8/site-packages/pytorch_lightning/utilities/distributed.py:45: UserWarning: The dataloader, train dataloader, does not have many workers which may be a bottleneck. Consider increasing the value of the `num_workers` argument` (try 8 which is the number of cpus on this machine) in the `DataLoader` init to improve performance.
  warnings.warn(*args, **kwargs)
Epoch 0: 100%|██████████████████| 2/2 [00:00<00:00, 427.58it/s, loss=2.280, v_num=11]
type of hparams <class 'pytorch_lightning.utilities.parsing.AttributeDict'>
class of hparams type AttributeDict
Traceback (most recent call last):
  File "/home/chirag/miniconda3/envs/ml/lib/python3.8/site-packages/pytorch_lightning/utilities/parsing.py", line 160, in __getattr__
    return self[key]
KeyError: 'something'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "ckpt-loading.py", line 118, in <module>
    run_test()
  File "ckpt-loading.py", line 106, in run_test
    model = BoringModel.load_from_checkpoint(ckpt_path)
  File "/home/chirag/miniconda3/envs/ml/lib/python3.8/site-packages/pytorch_lightning/core/saving.py", line 154, in load_from_checkpoint
    model = cls._load_model_state(checkpoint, strict=strict, **kwargs)
  File "/home/chirag/miniconda3/envs/ml/lib/python3.8/site-packages/pytorch_lightning/core/saving.py", line 198, in _load_model_state
    model = cls(**_cls_kwargs_filtered)
  File "ckpt-loading.py", line 40, in __init__
    print("accessing hparams", self.hparams.something, "no problem")
  File "/home/chirag/miniconda3/envs/ml/lib/python3.8/site-packages/pytorch_lightning/utilities/parsing.py", line 162, in __getattr__
    raise AttributeError(f'Missing attribute "{key}"') from exp
AttributeError: Missing attribute "something"

Also, for isolating the problem, look at this code:

  
    
      pytorch-lightning/pytorch_lightning/trainer/connectors/checkpoint_connector.py
    
    
        Lines 297 to 306
      in
      207ff72
    
  
  
    

        
          
           if model.hparams: 
        

        
          
               if hasattr(model, '_hparams_name'): 
        

        
          
                   checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_NAME] = model._hparams_name 
        

        
          
               # add arguments to the checkpoint 
        

        
          
               if OMEGACONF_AVAILABLE: 
        

        
          
                   checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_KEY] = model.hparams 
        

        
          
                   if isinstance(model.hparams, Container): 
        

        
          
                       checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_TYPE] = type(model.hparams) 
        

        
          
               else: 
        

        
          
                   checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_KEY] = dict(model.hparams) 
        
    
  


If you add a breakpoint or log line for debugging after the inner if, so that:
if isinstance(model.hparams, Container):
    print("Saving hparams type")
    checkpoint[LightningModule.CHECKPOINT_HYPER_PARAMS_TYPE] = type(model.hparams)

you should see that it doesn't get triggered.
Environment

PyTorch Version : 1.0.2
PyTorch Version (e.g., 1.0) : 1.6
OS (e.g., Linux): Linux
How you installed PyTorch (conda, pip, source): conda
Python version: 3.8.5