The order of model.to and load_state_dict

This is more a pytorch question. The official torch doc shows that model.to is called after model.load_state_dict as follows:

device = torch.device("cuda")
model = TheModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH))
model.to(device)

However, the fastai learner
first calls model.to in def __post_init__() and then calls load_state_dict in def load().

So is it correct understanding that the order of calling model.to and load_state_dict doesn’t matter? Thank you.

It doesn’t matter in this case because the model was on the same device during the save (since it went through the to function).

1 Like