Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Save model parameters without DataParallel module wrapper #17

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions ptychonn/_infer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,25 @@ def __init__(
model: typing.Optional[torch.nn.Module] = None,
model_params_path: typing.Optional[pathlib.Path] = None,
):
self.device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu")
print(f"Let's use {torch.cuda.device_count()} GPUs!")
self.model = ptychonn.model.ReconSmallPhaseModel(
) if model is None else model

if model is None or model_params_path is None:
self.model = ptychonn.model.ReconSmallPhaseModel()
if model_params_path is None:
with importlib.resources.path(
'ptychonn._infer',
'weights.pth',
) as model_params_path:
self.model.load_state_dict(
torch.load(model_params_path, map_location=self.device))
self.model.load_state_dict(torch.load(model_params_path))
else:
self.model = model
self.model.load_state_dict(
torch.load(model_params_path, map_location=self.device))
self.model.load_state_dict(torch.load(model_params_path))

self.device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu")
print(f"Let's use {torch.cuda.device_count()} GPUs!")

if torch.cuda.device_count() > 1:
self.model = torch.nn.DataParallel(self.model)

self.model = torch.nn.DataParallel(self.model) #Default all devices
self.model = self.model.to(self.device)

def setTestData(self, X_test: np.ndarray, batch_size: int):
Expand Down
17 changes: 10 additions & 7 deletions ptychonn/_train/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,10 @@ def initModel(self, model_params_path: pathlib.Path | None = None):

self.device = torch.device(
"cuda" if torch.cuda.is_available() else "cpu")
print(f"Let's use {torch.cuda.device_count()} GPUs!")

if torch.cuda.device_count() > 1:
logger.info("Let's use %d GPUs!", torch.cuda.device_count())
# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
self.model = torch.nn.DataParallel(
self.model,
device_ids=None, # Default all devices
)
self.model = torch.nn.DataParallel(self.model)

self.model = self.model.to(self.device)

Expand Down Expand Up @@ -414,7 +411,13 @@ def updateSavedModel(
fname = directory / f'best_model{ suffix }.pth'
logger.info("Saving best model as %s", fname)
os.makedirs(directory, exist_ok=True)
torch.save(model.state_dict(), fname)
if isinstance(model, (
torch.nn.DataParallel,
torch.nn.parallel.DistributedDataParallel,
)):
torch.save(model.module.state_dict(), fname)
else:
torch.save(model.state_dict(), fname)

def getSavedModelPath(self) -> pathlib.Path | None:
"""Return the path where `validate` will save the model weights"""
Expand Down