ray icon indicating copy to clipboard operation
ray copied to clipboard

[tune] BOHB failing to load checkpoint

Open msdejong opened this issue 7 years ago • 10 comments

OS: Linux Ubuntu 16.04 Ray version: 8.03 Python version: 3.6

Issue: Since switching from async hyperband with hyperopt to BOHB, I am occasionally getting Exceptions in the assert in https://github.com/ray-project/ray/blob/master/python/ray/tune/schedulers/hyperband.py line 386.

I have attempted to track down the cause. As far as I can ascertain, what is happening is that sometimes a trial is resumed without loading the checkpoint. This sets the time iteration to 0, which leads to a negative delta compared to the last result.

As far as I can tell, this happens only for the very last trial in a bracket, and only sometimes. Possibly related to on_trial_result in https://github.com/ray-project/ray/blob/master/python/ray/tune/schedulers/hb_bohb.py, though I'm not certain about that.

I am working on a minimum failing example, but that's turning out to be quite difficult, so I am wondering if the problem is obvious to either of you.

@richardliaw @lisadunlap

msdejong avatar Aug 22 '19 02:08 msdejong

What bracket size/reduction factor are you using?

Also, hm, does the pausing just not work?

On Wed, Aug 21, 2019 at 7:11 PM Michiel de Jong [email protected] wrote:

OS: Linux Ubuntu 16.04 Ray version: 8.03 Python version: 3.6

Issue: Since switching from async hyperband with hyperopt to BOHB, I am occasionally getting Exceptions in the assert in https://github.com/ray-project/ray/blob/master/python/ray/tune/schedulers/hyperband.py line 386.

I have attempted to track down the cause. As far as I can ascertain, what is happening is that sometimes a trial is resumed without loading the checkpoint. This sets the time iteration to 0, which leads to a negative delta compared to the last result.

As far as I can tell, this happens only for the very last trial in a bracket, and only sometimes. Possibly related to on_trial_result in https://github.com/ray-project/ray/blob/master/python/ray/tune/schedulers/hb_bohb.py, though I'm not certain about that.

I am working on a minimum failing example, but that's turning out to be quite difficult, so I am wondering if the problem is obvious to either of you.

@richardliaw https://github.com/richardliaw @lisadunlap https://github.com/lisadunlap

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ray-project/ray/issues/5501?email_source=notifications&email_token=ABCRZZPROQBWMCQNCEFN7NLQFXYV3A5CNFSM4IOQAI22YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HGVFJZA, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCRZZP6UJYOOLE655SGXULQFXYV3ANCNFSM4IOQAI2Q .

richardliaw avatar Aug 22 '19 02:08 richardliaw

Bracket size is endogenously determined, but I'm using a reduction factor of 3 and 10 epochs, leading to a 'large' bracket with 9 trials. Pausing and restoring after does work in general, it just sometimes fails for the last trial in a bracket. It appears that in the cases where it fails, restore is not called in the ray_trial_executor, the trial does have a checkpoint. Currently verifying / tracking further upstream

msdejong avatar Aug 22 '19 02:08 msdejong

Update: restore is called in the trial runner, but it's not called in the trainable, it goes straight to training the next epoch

msdejong avatar Aug 22 '19 04:08 msdejong

Restore should always go through the ray_trial_executor right?

richardliaw avatar Aug 22 '19 05:08 richardliaw

Pardon, yes, that's what I mean. It's called in ray_trial_executor, and I see trial.runner.restore_from_object.remote(value) being executed, and from that point the Trainable._save is never called and instead _train is immediately called.

msdejong avatar Aug 22 '19 05:08 msdejong

Actually, one thing from other users that might be an issue - what's your _save and _restore implementations look like?

richardliaw avatar Aug 28 '19 04:08 richardliaw

Have posted code below. The exact trainable works fine using hyperopt search space and async hyperband scheduler, which I was using before (and now again)

def _save(self, checkpoint_dir):
    path = os.path.join(checkpoint_dir, "checkpoint.pth")
    state = {'epoch': self.epoch, 
    'best_result': self.best_result,
    'best_epoch': self.best_epoch,
    'model_state_dict': self.model.state_dict(),
    'optimizer_state_dict': self.optimizer.state_dict()}
    if self.scheduler is not None:
        state['scheduler_state_dict'] = self.scheduler.state_dict()
    torch.save(state, path)

    return path

def _restore(self, checkpoint_path):
    
    checkpoint = torch.load(checkpoint_path)
    self.epoch = checkpoint['epoch']
    self.best_result = checkpoint['best_result']
    self.best_epoch = checkpoint['best_epoch']
    self.model.load_state_dict(checkpoint['model_state_dict'])
    self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    if self.scheduler is not None:
        self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])

msdejong avatar Aug 28 '19 04:08 msdejong

Ah, can you try using tune.util.validate_save_restore(use_object_store=True) to see if any errors are raised?

On Tue, Aug 27, 2019 at 9:53 PM Michiel de Jong [email protected] wrote:

Have posted code below. The exact trainable works fine using hyperopt search space and async hyperband scheduler, which I was using before (and now again)

def _save(self, checkpoint_dir): path = os.path.join(checkpoint_dir, "checkpoint.pth") state = {'epoch': self.epoch, 'best_result': self.best_result, 'best_epoch': self.best_epoch, 'model_state_dict': self.model.state_dict(), 'optimizer_state_dict': self.optimizer.state_dict()} if self.scheduler is not None: state['scheduler_state_dict'] = self.scheduler.state_dict() torch.save(state, path)

return path

def _restore(self, checkpoint_path):

checkpoint = torch.load(checkpoint_path)
self.epoch = checkpoint['epoch']
self.best_result = checkpoint['best_result']
self.best_epoch = checkpoint['best_epoch']
self.model.load_state_dict(checkpoint['model_state_dict'])
self.optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
if self.scheduler is not None:
    self.scheduler.load_state_dict(checkpoint['scheduler_state_dict'])

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ray-project/ray/issues/5501?email_source=notifications&email_token=ABCRZZJO6UZMQ6FESRETBBLQGYAGJA5CNFSM4IOQAI22YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5J4JBQ#issuecomment-525583494, or mute the thread https://github.com/notifications/unsubscribe-auth/ABCRZZM2LXB6IZNYEJAFRODQGYAGJANCNFSM4IOQAI2Q .

richardliaw avatar Aug 28 '19 05:08 richardliaw

Apologies for the delay, was OOO. I tried it and it worked without error. Also edited that script to allow for the use of GPU and no errors with that either.

msdejong avatar Aug 30 '19 02:08 msdejong

There's been a number of new releases since this issue was originally filed. If the problem persists, please re-open with a usable replication script, and we will try to solve the problem.

krfricke avatar Sep 16 '20 15:09 krfricke