Refactor NPE-C loss logic to Strategy Pattern#1755
Refactor NPE-C loss logic to Strategy Pattern#1755Sumit6307 wants to merge 4 commits intosbi-dev:mainfrom
Conversation
|
Hi @janfb, please have a look at this PR. Thanks! |
|
Hi @Sumit6307 , thank you for this PR as well. I assume you have read my comment under the PR (#1756 (comment)). That said, this contribution looks very good on a high level. I suggest the following:
|
@janfb Yes, I have read your comment under #1756. I’m glad to hear that the contribution looks good at a high level. I’ll go through #1241 and @michaeldeistler’s proposal in detail and review how my PR aligns with the planned new organization of SNPE methods. Based on that, I’ll update the implementation and clarify the alignment where needed. I’ll also run ruff and pyright locally and fix the remaining linting and type-checking issues so that CI passes. Please let me know if you’d prefer me to continue iterating on this within the current PR, or if it would be better to first open or move the discussion to a dedicated issue. Thanks for the guidance. |
|
Sounds good @Sumit6307 , let's discuss the implementation here in the PR. |
|
Hi @Sumit6307 are you planning to continue working on this? If not, no problem, please let us know 🙏 |
@janfb I have just pushed a major update to the branch that: 1 . Resolves all merge conflicts with the latest main branch. The PR is now ready for a fresh review. Thank you for your patience! 🙏" |
|
Hi @Sumit6307, thank you for the update and for your continued work on this! I took this reviewing as an opportunity to think more broadly about how we handle NPE OverviewYour extraction of the atomic and non-atomic loss into separate classes is the right
What this means for your PRYour work identified the right abstraction boundary and showed the extraction is feasible. However, the scope of what we want to do is larger than what this PR covers, and merging it as-is would mean immediately refactoring it again. So I'd like to offer you two options: (a) You take ownership of Phase 1 of the plan. This would mean updating your PR to:
I'm happy to provide more detailed guidance on any of these points. (b) We take it from here. This would be totally fine as well. You've done valuable exploratory work. We'd credit you as co-author in the commits. Technical issues with the current PR (for reference)In case you go with option (a), here are the specific issues to address:
Let me know which option you'd prefer, and thanks again for pushing this forward! P.S.: In case you were planning to apply for GSoC with us, just to manage expectations, |
Hi @janfb, Thank you for the detailed feedback and the broader context regarding #1241! I agree that a clean NPELossStrategy protocol and a unified npe_loss.py module will greatly simplify the architecture across I have just pushed the commits for Phase 1! The changes include:
Regarding GSoC: I completely understand you are all very busy with the upcoming deadline and the high volume of proposals, so please do not feel rushed to review this PR right away! I am officially submitting my GSoC proposal to NumFOCUS for Thank you again for all the valuable guidance you've provided so far. |
|
Hi @Sumit6307 Thanks for the update and sorry for the delayed response--we had a high load of PRs and other maintenance work recently due to GSoC. I will review this soon and I have also seen your PR #1826 on the analog refactor for NRE, that's great! Thanks for your patience 🙏 |
Summary
This PR refactors the NPE_C trainer to use the Strategy Pattern for its loss calculation logic. It isolates the "atomic" (sample-based) and "non-atomic" (analytical Gaussian) loss implementations into separate strategy classes, significantly improving the readability and maintainability of the core trainer class.
Motivation
The NPE_C class previously contained complex, non-trivial logic for switching between two very different loss calculation methods. This tightly coupled the mathematical details of the loss functions with the training loop. Validating the Single Responsibility Principle, this refactor:
Changes
_log_prob_proposal_posterior_atomic,_log_prob_proposal_posterior_mog, and _automatic_posterior_transformation.Verification