Remove deepcopy:pickle error, allow non-falsy num_workers=0#237
Open
gpwolfe wants to merge 1 commit into
Open
Conversation
Collaborator
|
@ipcamit can you please review this PR? |
There was a problem hiding this comment.
Pull request overview
This PR updates the PyTorch Lightning trainer to (1) avoid deepcopy failures when exporting models that include e3nn layers, (2) preserve/normalize graph tensor dtypes rather than always ending up with float64 from the RadialGraph C-extension, and (3) correctly honor num_workers=0 instead of treating it as falsy and falling back to SLURM defaults.
Changes:
- Remove
deepcopy(self.pl_model)during export and load the best checkpoint directly intoself.pl_modelbefore TorchScripting. - Add a post-transform dtype normalization step for precomputed graph fingerprints (coords/forces) when default dtype is float32.
- Fix
num_workersresolution so explicit0is respected and values are consistently cast toint.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+362
to
+374
| # of input dtype. Cast fingerprints back to match the model's default dtype. | ||
| if torch.get_default_dtype() == torch.float32: | ||
| for ds in [self.train_dataset, self.val_dataset]: | ||
| if ds is None: | ||
| continue | ||
| for config in ds: | ||
| fp = getattr(config, "fingerprint", None) | ||
| if fp is None: | ||
| continue | ||
| if hasattr(fp, "coords") and fp.coords is not None: | ||
| fp.coords = fp.coords.to(torch.float32) | ||
| if hasattr(fp, "forces") and fp.forces is not None: | ||
| fp.forces = fp.forces.to(torch.float32) |
Comment on lines
349
to
+365
| if self.dataset_manifest["dynamic_loading"]: | ||
| transform = self.configuration_transform | ||
| else: | ||
| transform = None | ||
|
|
||
| if not transform: | ||
| for config in self.train_dataset: | ||
| config.fingerprint = self.configuration_transform(config) | ||
| if self.val_dataset: | ||
| for config in self.val_dataset: | ||
| config.fingerprint = self.configuration_transform(config) | ||
|
|
||
| # RadialGraph C extension always upcasts coords/forces to float64 regardless | ||
| # of input dtype. Cast fingerprints back to match the model's default dtype. | ||
| if torch.get_default_dtype() == torch.float32: | ||
| for ds in [self.train_dataset, self.val_dataset]: | ||
| if ds is None: |
Comment on lines
+520
to
526
| self.pl_model.load_state_dict( | ||
| torch.load( | ||
| f"{self.current['run_dir']}/checkpoints/best_model.pth", | ||
| weights_only=False, | ||
| ) | ||
| ) | ||
| try: |
| # so mutating self.pl_model is safe. | ||
| self.pl_model.load_state_dict( | ||
| torch.load( | ||
| f"{self.current['run_dir']}/checkpoints/best_model.pth", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Include a summary of major changes in bullet points:
num_workers = os.getenv("SLURM_CPUS_PER_TASK", 1)due to evaluation as False. Allows non-multithread support on non-slurm system.Additional dependencies introduced (if any)
Checklist
Before a pull request can be merged, the following items must be checked:
type check your code.
Note that the CI system will run all the above checks. But it will be much more
efficient if you already fix most errors prior to submitting the PR.