Skip to content

AttributeError in Checkpoint::reload_objects when save_handler is a callable object #2998

@sadra-barikbin

Description

@sadra-barikbin

🐛 Bug description

When using Checkpoint with a callable object as save_handler, calling reload_objects causes AttributeError. To reproduce:

def test_checkpoint_simple_recovery():
    from pickle import dump
    class SaveHandler:
        def __call__(self, checkpoint, filename):
            with open(filename, 'wb') as f:
                dump(checkpoint, f)
    to_save = {'model': DummyModel()}
    h = Checkpoint(to_save, SaveHandler())
    engine = Engine(lambda e, b: None)
    engine.state = State(epoch=0, iteration=1)
    h(engine)
    to_load = {"model": DummyModel()}
    h.reload_objects(to_load, global_step=1)

result:

>       path = self.save_handler.dirname / checkpoint_fp
E       AttributeError: 'SaveHandler' object has no attribute 'dirname'

It attempts to access dirname of the handler while dirname is only the attribute of the DiskSaver not even BaseSaveHandler.

Also in the Checkpoint docstring, it states String, method or callable class (by callable class it means callable object?) for save_handler type but in type checks we allow a callable function:

if not (
    isinstance(save_handler, str)
    or isinstance(save_handler, Path)
    or callable(save_handler)
    or isinstance(save_handler, BaseSaveHandler)
):
    raise TypeError(
        "Argument `save_handler` should be a string or Path object or callable or inherit from BaseSaveHandler"
    )
Checklist
  • ignite/handlers/checkpoint.py

• In the reload_objects method of the Checkpoint class, before the line path = self.save_handler.dirname / checkpoint_fp, add a condition to check if self.save_handler is an instance of DiskSaver.
• If self.save_handler is an instance of DiskSaver, keep the existing logic to get the path.
• If self.save_handler is not an instance of DiskSaver, handle the checkpoint reloading for the callable object. This might involve calling the save_handler with the appropriate arguments to reload the checkpoint.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions