feat: add nframe=3 adsorption softmax training#5746
Conversation
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Pull request overview
This PR adds a softmax-weighted averaging behavior intended for an adsorption workflow that uses exactly 3 frames/samples per batch, and adjusts HDF5 opening behavior.
Changes:
- Open HDF5 files with
locking=Falsein the cached H5 loader. - Add softmax-weighted averaging across the batch in PyTorch
PropertyLossand enforcenbz == 3. - Add softmax-weighted averaging across frames in
DeepProperty.evaland enforcenframes == 3(with a debugprint).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
deepmd/utils/path.py |
Changes HDF5 file open behavior by disabling file locking. |
deepmd/pt/loss/property.py |
Introduces batch-level softmax averaging and hard-codes a batch size of 3 in the property loss. |
deepmd/infer/deep_property.py |
Introduces frame-level softmax averaging and hard-codes nframes == 3 (plus debug output). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # this method has cache to avoid duplicated | ||
| # loading from different DPH5Path | ||
| # However the file will be never closed? | ||
| return h5py.File(path, mode) | ||
| return h5py.File(path, mode, locking=False) |
| # ---- Softmax-weighted averaging over the batch added by YL---- | ||
| # model_pred[var_name]: (nbz, task_dim) | ||
| # 1) get a scalar score per sample (mean over task_dim) | ||
| # (If you want to favor smaller values, use `score_per_sample = -model_pred[var_name].mean(dim=1)`.) | ||
| score_per_sample = model_pred[var_name].mean(dim=1) # (nbz,) | ||
| weights = F.softmax(score_per_sample, dim=0) # (nbz,) | ||
| # 2) weighted average vector (1, task_dim) | ||
| avg_vec = (weights.unsqueeze(1) * model_pred[var_name]).sum(dim=0, keepdim=True) | ||
| # 3) replace all predictions with the averaged vector (broadcast over batch) | ||
| model_pred[var_name] = avg_vec.expand_as(model_pred[var_name]) | ||
| # ---------------------------------------------------- | ||
|
|
||
| nbz = model_pred[var_name].shape[0] | ||
| # =======Raise error when nbz!=3======= | ||
| if nbz != 3: | ||
| raise RuntimeError( | ||
| f"[PropertyLoss] Expected batch size nbz == 3 for softmax-avg, got nbz == {nbz}. " | ||
| "Ensure your DataLoader yields triples (batch_size=3, drop_last=True)." | ||
| ) | ||
|
|
| # --- softmax-weighted averaging over frames (minimal) --- | ||
| print(f"Nframes == {nframes}") | ||
| if nframes != 3: | ||
| raise RuntimeError(f"Expected nframes == 3, got {nframes}") | ||
| scores = property.mean(axis=1) # (3,) | ||
| # If you want to favor *smaller* values (e.g., energies), use: scores = -scores | ||
| w = np.exp(scores - scores.max()) | ||
| w /= w.sum() # (3,) | ||
| avg = (w[:, None] * property).sum(axis=0, keepdims=True) # (1, D) | ||
| property[:] = np.repeat(avg, nframes, axis=0) # (3, D) |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5746 +/- ##
==========================================
- Coverage 81.95% 76.02% -5.94%
==========================================
Files 714 1015 +301
Lines 73441 139629 +66188
Branches 3616 5764 +2148
==========================================
+ Hits 60187 106146 +45959
- Misses 12091 31831 +19740
- Partials 1163 1652 +489 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
No description provided.