# Buggy code
def on_save_checkpoint(self, checkpoint: Dict[str, Any]) -> None:
    checkpoint["dataset_parameters"] = getattr(
        self, "dataset_parameters", None
    )  # add dataset parameters for making fast predictions
    checkpoint["loss"] = cloudpickle.dumps(self.loss)  # restore loss
    checkpoint["output_transformer"] = cloudpickle.dumps(
        self.output_transformer
    )  # restore output transformer
    # hyper parameters are passed as arguments directly and not as single dictionary
    checkpoint["hparams_name"] = "kwargs"


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1741, in <module>
main()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals)  # execute the script
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "airflow_dags/predictions.py", line 167, in <module>
main()
File "airflow_dags/predictions.py", line 163, in main
create_predictions(**context)
File "airflow_dags/predictions.py", line 37, in create_predictions
tft = TemporalFusionTransformer.load_from_checkpoint(model_path, map_location=torch.device('cpu'))
File "/Users/dominique.vandamme/code/data-infrastructure/venv/lib/python3.7/site-packages/pytorch_lightning/core/saving.py", line 158, in load_from_checkpoint
model = cls._load_model_state(checkpoint, strict=strict, **kwargs)
File "/Users/dominique.vandamme/code/data-infrastructure/venv/lib/python3.7/site-packages/pytorch_lightning/core/saving.py", line 201, in _load_model_state
model.on_load_checkpoint(checkpoint)
File "/Users/dominique.vandamme/code/data-infrastructure/venv/lib/python3.7/site-packages/pytorch_forecasting/models/base_model.py", line 688, in on_load_checkpoint
self.loss = cloudpickle.loads(checkpoint["loss"])
File "/Users/dominique.vandamme/code/data-infrastructure/venv/lib/python3.7/site-packages/torch/storage.py", line 141, in _load_from_bytes
return torch.load(io.BytesIO(b))
File "/Users/dominique.vandamme/code/data-infrastructure/venv/lib/python3.7/site-packages/torch/serialization.py", line 595, in load
return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
File "/Users/dominique.vandamme/code/data-infrastructure/venv/lib/python3.7/site-packages/torch/serialization.py", line 774, in _legacy_load
result = unpickler.load()
File "/Users/dominique.vandamme/code/data-infrastructure/venv/lib/python3.7/site-packages/torch/serialization.py", line 730, in persistent_load
deserialized_objects[root_key] = restore_location(obj, location)
File "/Users/dominique.vandamme/code/data-infrastructure/venv/lib/python3.7/site-packages/torch/serialization.py", line 175, in default_restore_location
result = fn(storage, location)
File "/Users/dominique.vandamme/code/data-infrastructure/venv/lib/python3.7/site-packages/torch/serialization.py", line 151, in _cuda_deserialize
device = validate_cuda_device(location)
File "/Users/dominique.vandamme/code/data-infrastructure/venv/lib/python3.7/site-packages/torch/serialization.py", line 135, in validate_cuda_device
raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

# Fixed code
def on_save_checkpoint(self, checkpoint: Dict[str, Any]) -> None:
    checkpoint["dataset_parameters"] = getattr(
        self, "dataset_parameters", None
    )  # add dataset parameters for making fast predictions
    # hyper parameters are passed as arguments directly and not as single dictionary
    checkpoint["hparams_name"] = "kwargs"