fix checkpoint generated by ddp training cannot be loaded (#1682)

* fix checkpoint generated by ddp training cannot be loaded

* fix typo

* reformatted nerfstudio/pipelines/base_pipeline.py

* rewritten as for loop

* add test case

* reformatted

* fix pylint test failed
This commit is contained in:
Zhensheng Yuan authored and GitHub committed 2023-04-01 15:56:47 -07:00
1 parent 2c12f5f205
commit e104a3029e
2 files changed
+36 -3

No files matched your search

+16 -3
View File
@@ -108,9 +108,22 @@ class Pipeline(nn.Module):
return self.model.device
def load_state_dict(self, state_dict: Mapping[str, Any], strict: bool = True):
model_state = {key[len("_model.") :]: value for key, value in state_dict.items() if key.startswith("_model.")}
is_ddp_model_state = True
model_state = {}
for key, value in state_dict.items():
if key.startswith("_model."):
# remove the "_model." prefix from key
model_state[key[len("_model.") :]] = value
# make sure that the "module." prefix comes from DDP,
# rather than an attribute of the model named "module"
if not key.startswith("_model.module."):
is_ddp_model_state = False
# remove "module." prefix added by DDP
if is_ddp_model_state:
model_state = {key[len("module.") :]: value for key, value in model_state.items()}
pipeline_state = {key: value for key, value in state_dict.items() if not key.startswith("_model.")}
self._model.load_state_dict(model_state, strict=strict)
self.model.load_state_dict(model_state, strict=strict)
super().load_state_dict(pipeline_state, strict=False)
@profiler.time_function
@@ -373,7 +386,7 @@ class VanillaPipeline(Pipeline):
state = {
(key[len("module.") :] if key.startswith("module.") else key): value for key, value in loaded_state.items()
}
self._model.update_to_step(step)
self.model.update_to_step(step)
self.load_state_dict(state, strict=True)
def get_training_callbacks(